@@ -33,15 +33,14 @@ module Node.Stream
3333import Prelude
3434
3535import 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 )
3739import Data.Either (Either (..))
38- import Node.Encoding
40+ import Data.Maybe ( Maybe (..), fromMaybe )
3941import Node.Buffer (Buffer ())
4042import 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 )
4544
4645-- | A stream.
4746-- |
@@ -69,13 +68,23 @@ type Duplex = Stream (read :: Read, write :: Write)
6968foreign import undefined :: forall a . a
7069
7170foreign 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+
7278readChunk :: Chunk -> Either String Buffer
7379readChunk = 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
7580
7681-- | Listen for `data` events, returning data in a Buffer. Note that this will fail
7782-- | 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
7988onData r cb =
8089 onDataEither r (cb <=< fromEither)
8190 where
@@ -86,63 +95,121 @@ onData r cb =
8695 Right buf ->
8796 pure buf
8897
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 )
90103read r size = do
91104 v <- readEither r size
92105 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 )
98116readString r size enc = do
99117 v <- readEither r size
100118 case v of
101119 Nothing -> pure Nothing
102120 Just (Left _) -> throw " Stream encoding should not be set"
103121 Just (Right buf) -> Just <$> (unsafeInterleaveEff $ Buffer .toString enc buf)
104122
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 ))
106128readEither r size = readImpl readChunk Nothing Just r (fromMaybe undefined size)
107129
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 ))
109138
110139-- | Listen for `data` events, returning data in a String, which will be
111140-- | decoded using the given encoding. Note that this will fail if `setEncoding`
112141-- | 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
114148onDataString r enc cb = onData r (cb <=< unsafeInterleaveEff <<< Buffer .toString enc)
115149
116150-- | Listen for `data` events, returning data in an `Either String Buffer`. This
117151-- | function is provided for the (hopefully rare) case that `setEncoding` has
118152-- | 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
120158onDataEither r cb = onDataEitherImpl readChunk r cb
121159
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
123166
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
125172
126173-- | Set the encoding used to read chunks as strings from the stream. This
127174-- | function may be useful when you are passing a readable stream to some other
128175-- | JavaScript library, which already expects an encoding to be set.
129176-- |
130177-- | Where possible, you should try to use `onDataString` instead of this
131178-- | 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
133184setEncoding r enc = setEncodingImpl r (show enc)
134185
135186-- | 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
137192
138193-- | 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
140199
141200-- | 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
143206
144207-- | 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
146213
147214-- | Resume reading from the stream.
148215foreign 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
154221foreign import isPaused :: forall w eff . Readable w eff -> Eff eff Boolean
155222
156223-- | 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 )
158229
159230-- | 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
163245
164246-- | 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
166254writeString w enc = writeStringImpl w (show enc)
167255
168256-- | Force buffering of writes.
@@ -171,16 +259,27 @@ foreign import cork :: forall r eff. Writable r eff -> Eff eff Unit
171259-- | Flush buffered data.
172260foreign import uncork :: forall r eff . Writable r eff -> Eff eff Unit
173261
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
175267
176268-- | Set the default encoding used to write strings to the stream. This function
177269-- | is useful when you are passing a writable stream to some other JavaScript
178270-- | library, which already expects a default encoding to be set. It has no
179271-- | effect on the behaviour of the `writeString` function (because that
180272-- | 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
182278setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc)
183279
184280-- | 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