@@ -33,15 +33,14 @@ module Node.Stream
33
33
import Prelude
34
34
35
35
import Control.Bind ((<=<))
36
- import Data.Maybe (Maybe (..), maybe , fromMaybe )
36
+ import Control.Monad.Eff (Eff )
37
+ import Control.Monad.Eff.Exception (throw , EXCEPTION (), Error ())
38
+ import Control.Monad.Eff.Unsafe (unsafeInterleaveEff )
37
39
import Data.Either (Either (..))
38
- import Node.Encoding
40
+ import Data.Maybe ( Maybe (..), fromMaybe )
39
41
import Node.Buffer (Buffer ())
40
42
import Node.Buffer as Buffer
41
-
42
- import Control.Monad.Eff
43
- import Control.Monad.Eff.Exception (throw , EXCEPTION (), Error ())
44
- import Control.Monad.Eff.Unsafe (unsafeInterleaveEff )
43
+ import Node.Encoding (Encoding )
45
44
46
45
-- | A stream.
47
46
-- |
@@ -69,13 +68,23 @@ type Duplex = Stream (read :: Read, write :: Write)
69
68
foreign import undefined :: forall a . a
70
69
71
70
foreign import data Chunk :: *
71
+
72
+ foreign import readChunkImpl
73
+ :: (forall l r . l -> Either l r )
74
+ -> (forall l r . r -> Either l r )
75
+ -> Chunk
76
+ -> Either String Buffer
77
+
72
78
readChunk :: Chunk -> Either String Buffer
73
79
readChunk = readChunkImpl Left Right
74
- foreign import readChunkImpl :: (forall l r . l -> Either l r ) -> (forall l r . r -> Either l r ) -> Chunk -> Either String Buffer
75
80
76
81
-- | Listen for `data` events, returning data in a Buffer. Note that this will fail
77
82
-- | if `setEncoding` has been called on the stream.
78
- onData :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> (Buffer -> Eff (err :: EXCEPTION | eff ) Unit ) -> Eff (err :: EXCEPTION | eff ) Unit
83
+ onData
84
+ :: forall w eff
85
+ . Readable w (err :: EXCEPTION | eff )
86
+ -> (Buffer -> Eff (err :: EXCEPTION | eff ) Unit )
87
+ -> Eff (err :: EXCEPTION | eff ) Unit
79
88
onData r cb =
80
89
onDataEither r (cb <=< fromEither)
81
90
where
@@ -86,63 +95,121 @@ onData r cb =
86
95
Right buf ->
87
96
pure buf
88
97
89
- read :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> Maybe Int -> Eff (err :: EXCEPTION | eff ) (Maybe Buffer )
98
+ read
99
+ :: forall w eff
100
+ . Readable w (err :: EXCEPTION | eff )
101
+ -> Maybe Int
102
+ -> Eff (err :: EXCEPTION | eff ) (Maybe Buffer )
90
103
read r size = do
91
104
v <- readEither r size
92
105
case v of
93
- Nothing -> pure Nothing
94
- Just (Left _) -> throw " Stream encoding should not be set"
95
- Just (Right b) -> pure (Just b)
96
-
97
- readString :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> Maybe Int -> Encoding -> Eff (err :: EXCEPTION | eff ) (Maybe String )
106
+ Nothing -> pure Nothing
107
+ Just (Left _) -> throw " Stream encoding should not be set"
108
+ Just (Right b) -> pure (Just b)
109
+
110
+ readString
111
+ :: forall w eff
112
+ . Readable w (err :: EXCEPTION | eff )
113
+ -> Maybe Int
114
+ -> Encoding
115
+ -> Eff (err :: EXCEPTION | eff ) (Maybe String )
98
116
readString r size enc = do
99
117
v <- readEither r size
100
118
case v of
101
119
Nothing -> pure Nothing
102
120
Just (Left _) -> throw " Stream encoding should not be set"
103
121
Just (Right buf) -> Just <$> (unsafeInterleaveEff $ Buffer .toString enc buf)
104
122
105
- readEither :: forall w eff . Readable w eff -> Maybe Int -> Eff eff (Maybe (Either String Buffer ))
123
+ readEither
124
+ :: forall w eff
125
+ . Readable w eff
126
+ -> Maybe Int
127
+ -> Eff eff (Maybe (Either String Buffer ))
106
128
readEither r size = readImpl readChunk Nothing Just r (fromMaybe undefined size)
107
129
108
- foreign import readImpl :: forall r eff . (Chunk -> Either String Buffer ) -> (forall a . Maybe a ) -> (forall a . a -> Maybe a ) -> Readable r eff -> Int -> Eff eff (Maybe (Either String Buffer ))
130
+ foreign import readImpl
131
+ :: forall r eff
132
+ . (Chunk -> Either String Buffer )
133
+ -> (forall a . Maybe a )
134
+ -> (forall a . a -> Maybe a )
135
+ -> Readable r eff
136
+ -> Int
137
+ -> Eff eff (Maybe (Either String Buffer ))
109
138
110
139
-- | Listen for `data` events, returning data in a String, which will be
111
140
-- | decoded using the given encoding. Note that this will fail if `setEncoding`
112
141
-- | has been called on the stream.
113
- onDataString :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> Encoding -> (String -> Eff (err :: EXCEPTION | eff ) Unit ) -> Eff (err :: EXCEPTION | eff ) Unit
142
+ onDataString
143
+ :: forall w eff
144
+ . Readable w (err :: EXCEPTION | eff )
145
+ -> Encoding
146
+ -> (String -> Eff (err :: EXCEPTION | eff ) Unit )
147
+ -> Eff (err :: EXCEPTION | eff ) Unit
114
148
onDataString r enc cb = onData r (cb <=< unsafeInterleaveEff <<< Buffer .toString enc)
115
149
116
150
-- | Listen for `data` events, returning data in an `Either String Buffer`. This
117
151
-- | function is provided for the (hopefully rare) case that `setEncoding` has
118
152
-- | been called on the stream.
119
- onDataEither :: forall r eff . Readable r (err :: EXCEPTION | eff ) -> (Either String Buffer -> Eff (err :: EXCEPTION | eff ) Unit ) -> Eff (err :: EXCEPTION | eff ) Unit
153
+ onDataEither
154
+ :: forall r eff
155
+ . Readable r (err :: EXCEPTION | eff )
156
+ -> (Either String Buffer -> Eff (err :: EXCEPTION | eff ) Unit )
157
+ -> Eff (err :: EXCEPTION | eff ) Unit
120
158
onDataEither r cb = onDataEitherImpl readChunk r cb
121
159
122
- foreign import onDataEitherImpl :: forall r eff . (Chunk -> Either String Buffer ) -> Readable r eff -> (Either String Buffer -> Eff eff Unit ) -> Eff eff Unit
160
+ foreign import onDataEitherImpl
161
+ :: forall r eff
162
+ . (Chunk -> Either String Buffer )
163
+ -> Readable r eff
164
+ -> (Either String Buffer -> Eff eff Unit )
165
+ -> Eff eff Unit
123
166
124
- foreign import setEncodingImpl :: forall w eff . Readable w eff -> String -> Eff eff Unit
167
+ foreign import setEncodingImpl
168
+ :: forall w eff
169
+ . Readable w eff
170
+ -> String
171
+ -> Eff eff Unit
125
172
126
173
-- | Set the encoding used to read chunks as strings from the stream. This
127
174
-- | function may be useful when you are passing a readable stream to some other
128
175
-- | JavaScript library, which already expects an encoding to be set.
129
176
-- |
130
177
-- | Where possible, you should try to use `onDataString` instead of this
131
178
-- | function.
132
- setEncoding :: forall w eff . Readable w eff -> Encoding -> Eff eff Unit
179
+ setEncoding
180
+ :: forall w eff
181
+ . Readable w eff
182
+ -> Encoding
183
+ -> Eff eff Unit
133
184
setEncoding r enc = setEncodingImpl r (show enc)
134
185
135
186
-- | Listen for `readable` events.
136
- foreign import onReadable :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
187
+ foreign import onReadable
188
+ :: forall w eff
189
+ . Readable w eff
190
+ -> Eff eff Unit
191
+ -> Eff eff Unit
137
192
138
193
-- | Listen for `end` events.
139
- foreign import onEnd :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
194
+ foreign import onEnd
195
+ :: forall w eff
196
+ . Readable w eff
197
+ -> Eff eff Unit
198
+ -> Eff eff Unit
140
199
141
200
-- | Listen for `close` events.
142
- foreign import onClose :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
201
+ foreign import onClose
202
+ :: forall w eff
203
+ . Readable w eff
204
+ -> Eff eff Unit
205
+ -> Eff eff Unit
143
206
144
207
-- | Listen for `error` events.
145
- foreign import onError :: forall w eff . Readable w eff -> (Error -> Eff eff Unit ) -> Eff eff Unit
208
+ foreign import onError
209
+ :: forall w eff
210
+ . Readable w eff
211
+ -> (Error -> Eff eff Unit )
212
+ -> Eff eff Unit
146
213
147
214
-- | Resume reading from the stream.
148
215
foreign import resume :: forall w eff . Readable w eff -> Eff eff Unit
@@ -154,15 +221,36 @@ foreign import pause :: forall w eff. Readable w eff -> Eff eff Unit
154
221
foreign import isPaused :: forall w eff . Readable w eff -> Eff eff Boolean
155
222
156
223
-- | Read chunks from a readable stream and write them to a writable stream.
157
- foreign import pipe :: forall r w eff . Readable w eff -> Writable r eff -> Eff eff (Writable r eff )
224
+ foreign import pipe
225
+ :: forall r w eff
226
+ . Readable w eff
227
+ -> Writable r eff
228
+ -> Eff eff (Writable r eff )
158
229
159
230
-- | Write a Buffer to a writable stream.
160
- foreign import write :: forall r eff . Writable r eff -> Buffer -> Eff eff Unit -> Eff eff Boolean
161
-
162
- foreign import writeStringImpl :: forall r eff . Writable r eff -> String -> String -> Eff eff Unit -> Eff eff Boolean
231
+ foreign import write
232
+ :: forall r eff
233
+ . Writable r eff
234
+ -> Buffer
235
+ -> Eff eff Unit
236
+ -> Eff eff Boolean
237
+
238
+ foreign import writeStringImpl
239
+ :: forall r eff
240
+ . Writable r eff
241
+ -> String
242
+ -> String
243
+ -> Eff eff Unit
244
+ -> Eff eff Boolean
163
245
164
246
-- | Write a string in the specified encoding to a writable stream.
165
- writeString :: forall r eff . Writable r eff -> Encoding -> String -> Eff eff Unit -> Eff eff Boolean
247
+ writeString
248
+ :: forall r eff
249
+ . Writable r eff
250
+ -> Encoding
251
+ -> String
252
+ -> Eff eff Unit
253
+ -> Eff eff Boolean
166
254
writeString w enc = writeStringImpl w (show enc)
167
255
168
256
-- | Force buffering of writes.
@@ -171,16 +259,27 @@ foreign import cork :: forall r eff. Writable r eff -> Eff eff Unit
171
259
-- | Flush buffered data.
172
260
foreign import uncork :: forall r eff . Writable r eff -> Eff eff Unit
173
261
174
- foreign import setDefaultEncodingImpl :: forall r eff . Writable r eff -> String -> Eff eff Unit
262
+ foreign import setDefaultEncodingImpl
263
+ :: forall r eff
264
+ . Writable r eff
265
+ -> String
266
+ -> Eff eff Unit
175
267
176
268
-- | Set the default encoding used to write strings to the stream. This function
177
269
-- | is useful when you are passing a writable stream to some other JavaScript
178
270
-- | library, which already expects a default encoding to be set. It has no
179
271
-- | effect on the behaviour of the `writeString` function (because that
180
272
-- | function ensures that the encoding is always supplied explicitly).
181
- setDefaultEncoding :: forall r eff . Writable r eff -> Encoding -> Eff eff Unit
273
+ setDefaultEncoding
274
+ :: forall r eff
275
+ . Writable r eff
276
+ -> Encoding
277
+ -> Eff eff Unit
182
278
setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc)
183
279
184
280
-- | End writing data to the stream.
185
- foreign import end :: forall r eff . Writable r eff -> Eff eff Unit -> Eff eff Unit
186
-
281
+ foreign import end
282
+ :: forall r eff
283
+ . Writable r eff
284
+ -> Eff eff Unit
285
+ -> Eff eff Unit
0 commit comments