/
Stream.purs
284 lines (245 loc) · 7.38 KB
/
Stream.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
-- | This module provides a low-level wrapper for the Node Stream API.
module Node.Stream
( Stream()
, Read()
, Readable()
, Write()
, Writable()
, Duplex()
, onData
, onDataString
, onDataEither
, setEncoding
, onReadable
, onEnd
, onClose
, onError
, resume
, pause
, isPaused
, pipe
, read
, readString
, readEither
, write
, writeString
, cork
, uncork
, setDefaultEncoding
, end
) where
import Prelude
import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Eff.Exception (throw, EXCEPTION(), Error())
import Control.Monad.Eff.Unsafe (unsafeCoerceEff)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..), fromMaybe)
import Node.Buffer (Buffer())
import Node.Buffer as Buffer
import Node.Encoding (Encoding)
-- | A stream.
-- |
-- | The type arguments track, in order:
-- |
-- | - Whether reading and/or writing from/to the stream are allowed.
-- | - Effects associated with reading/writing from/to this stream.
foreign import data Stream :: # Type -> # Effect -> Type
-- | A phantom type associated with _readable streams_.
data Read
-- | A readable stream.
type Readable r = Stream (read :: Read | r)
-- | A phantom type associated with _writable streams_.
data Write
-- | A writable stream.
type Writable r = Stream (write :: Write | r)
-- | A duplex (readable _and_ writable stream)
type Duplex = Stream (read :: Read, write :: Write)
foreign import undefined :: forall a. a
foreign import data Chunk :: Type
foreign import readChunkImpl
:: (forall l r. l -> Either l r)
-> (forall l r. r -> Either l r)
-> Chunk
-> Either String Buffer
readChunk :: Chunk -> Either String Buffer
readChunk = readChunkImpl Left Right
-- | Listen for `data` events, returning data in a Buffer. Note that this will fail
-- | if `setEncoding` has been called on the stream.
onData
:: forall w eff
. Readable w (exception :: EXCEPTION | eff)
-> (Buffer -> Eff (exception :: EXCEPTION | eff) Unit)
-> Eff (exception :: EXCEPTION | eff) Unit
onData r cb =
onDataEither r (cb <=< fromEither)
where
fromEither x =
case x of
Left _ ->
throw "Stream encoding should not be set"
Right buf ->
pure buf
read
:: forall w eff
. Readable w (exception :: EXCEPTION | eff)
-> Maybe Int
-> Eff (exception :: EXCEPTION | eff) (Maybe Buffer)
read r size = do
v <- readEither r size
case v of
Nothing -> pure Nothing
Just (Left _) -> throw "Stream encoding should not be set"
Just (Right b) -> pure (Just b)
readString
:: forall w eff
. Readable w (exception :: EXCEPTION | eff)
-> Maybe Int
-> Encoding
-> Eff (exception :: EXCEPTION | eff) (Maybe String)
readString r size enc = do
v <- readEither r size
case v of
Nothing -> pure Nothing
Just (Left _) -> throw "Stream encoding should not be set"
Just (Right buf) -> Just <$> (unsafeCoerceEff $ Buffer.toString enc buf)
readEither
:: forall w eff
. Readable w eff
-> Maybe Int
-> Eff eff (Maybe (Either String Buffer))
readEither r size = readImpl readChunk Nothing Just r (fromMaybe undefined size)
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))
-- | Listen for `data` events, returning data in a String, which will be
-- | decoded using the given encoding. Note that this will fail if `setEncoding`
-- | has been called on the stream.
onDataString
:: forall w eff
. Readable w (exception :: EXCEPTION | eff)
-> Encoding
-> (String -> Eff (exception :: EXCEPTION | eff) Unit)
-> Eff (exception :: EXCEPTION | eff) Unit
onDataString r enc cb = onData r (cb <=< unsafeCoerceEff <<< Buffer.toString enc)
-- | Listen for `data` events, returning data in an `Either String Buffer`. This
-- | function is provided for the (hopefully rare) case that `setEncoding` has
-- | been called on the stream.
onDataEither
:: forall r eff
. Readable r (exception :: EXCEPTION | eff)
-> (Either String Buffer -> Eff (exception :: EXCEPTION | eff) Unit)
-> Eff (exception :: EXCEPTION | eff) Unit
onDataEither r cb = onDataEitherImpl readChunk r cb
foreign import onDataEitherImpl
:: forall r eff
. (Chunk -> Either String Buffer)
-> Readable r eff
-> (Either String Buffer -> Eff eff Unit)
-> Eff eff Unit
foreign import setEncodingImpl
:: forall w eff
. Readable w eff
-> String
-> Eff eff Unit
-- | Set the encoding used to read chunks as strings from the stream. This
-- | function may be useful when you are passing a readable stream to some other
-- | JavaScript library, which already expects an encoding to be set.
-- |
-- | Where possible, you should try to use `onDataString` instead of this
-- | function.
setEncoding
:: forall w eff
. Readable w eff
-> Encoding
-> Eff eff Unit
setEncoding r enc = setEncodingImpl r (show enc)
-- | Listen for `readable` events.
foreign import onReadable
:: forall w eff
. Readable w eff
-> Eff eff Unit
-> Eff eff Unit
-- | Listen for `end` events.
foreign import onEnd
:: forall w eff
. Readable w eff
-> Eff eff Unit
-> Eff eff Unit
-- | Listen for `close` events.
foreign import onClose
:: forall w eff
. Readable w eff
-> Eff eff Unit
-> Eff eff Unit
-- | Listen for `error` events.
foreign import onError
:: forall w eff
. Readable w eff
-> (Error -> Eff eff Unit)
-> Eff eff Unit
-- | Resume reading from the stream.
foreign import resume :: forall w eff. Readable w eff -> Eff eff Unit
-- | Pause reading from the stream.
foreign import pause :: forall w eff. Readable w eff -> Eff eff Unit
-- | Check whether or not a stream is paused for reading.
foreign import isPaused :: forall w eff. Readable w eff -> Eff eff Boolean
-- | Read chunks from a readable stream and write them to a writable stream.
foreign import pipe
:: forall r w eff
. Readable w eff
-> Writable r eff
-> Eff eff (Writable r eff)
-- | Write a Buffer to a writable stream.
foreign import write
:: forall r eff
. Writable r eff
-> Buffer
-> Eff eff Unit
-> Eff eff Boolean
foreign import writeStringImpl
:: forall r eff
. Writable r eff
-> String
-> String
-> Eff eff Unit
-> Eff eff Boolean
-- | Write a string in the specified encoding to a writable stream.
writeString
:: forall r eff
. Writable r eff
-> Encoding
-> String
-> Eff eff Unit
-> Eff eff Boolean
writeString w enc = writeStringImpl w (show enc)
-- | Force buffering of writes.
foreign import cork :: forall r eff. Writable r eff -> Eff eff Unit
-- | Flush buffered data.
foreign import uncork :: forall r eff. Writable r eff -> Eff eff Unit
foreign import setDefaultEncodingImpl
:: forall r eff
. Writable r eff
-> String
-> Eff eff Unit
-- | Set the default encoding used to write strings to the stream. This function
-- | is useful when you are passing a writable stream to some other JavaScript
-- | library, which already expects a default encoding to be set. It has no
-- | effect on the behaviour of the `writeString` function (because that
-- | function ensures that the encoding is always supplied explicitly).
setDefaultEncoding
:: forall r eff
. Writable r eff
-> Encoding
-> Eff eff Unit
setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc)
-- | End writing data to the stream.
foreign import end
:: forall r eff
. Writable r eff
-> Eff eff Unit
-> Eff eff Unit