@@ -7,9 +7,10 @@ module Node.Stream
77 , Write ()
88 , Writable ()
99 , Duplex ()
10- , setEncoding
1110 , onData
1211 , onDataString
12+ , onDataEither
13+ , setEncoding
1314 , onEnd
1415 , onClose
1516 , onError
@@ -27,11 +28,14 @@ module Node.Stream
2728
2829import Prelude
2930
31+ import Control.Bind ((<=<))
32+ import Data.Either (Either (..))
3033import Node.Encoding
3134import Node.Buffer (Buffer ())
3235import Node.Buffer as Buffer
3336
3437import Control.Monad.Eff
38+ import Control.Monad.Eff.Exception (throw , EXCEPTION ())
3539import Control.Monad.Eff.Unsafe (unsafeInterleaveEff )
3640
3741-- | A stream.
@@ -57,29 +61,46 @@ type Writable r = Stream (write :: Write | r)
5761-- | A duplex (readable _and_ writable stream)
5862type Duplex = Stream (read :: Read , write :: Write )
5963
60- foreign import setEncodingImpl :: forall w eff . Readable w eff -> String -> Eff eff Unit
61-
62- -- | Set the encoding used to read chunks as strings from the stream. This
63- -- | function is useful when you are passing a readable stream to some other
64- -- | JavaScript library, which already expects an encoding to be set. It
65- -- | has no effect on the behaviour of the `onDataString` function (because
66- -- | that function ensures that the encoding is always supplied explicitly).
67- setEncoding :: forall w eff . Readable w eff -> Encoding -> Eff eff Unit
68- setEncoding r enc = setEncodingImpl r (show enc)
69-
70- foreign import onDataImpl :: forall w eff a . Readable w eff -> (a -> Eff eff Unit ) -> Eff eff Unit
71-
72- -- | Listen for `data` events, returning data in a Buffer.
73- onData :: forall w eff . Readable w eff -> (Buffer -> Eff eff Unit ) -> Eff eff Unit
74- onData = onDataImpl
64+ -- | Listen for `data` events, returning data in a Buffer. Note that this will fail
65+ -- | if `setEncoding` has been called on the stream.
66+ onData :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> (Buffer -> Eff (err :: EXCEPTION | eff ) Unit ) -> Eff (err :: EXCEPTION | eff ) Unit
67+ onData r cb =
68+ onDataEither r (cb <=< fromEither)
69+ where
70+ fromEither x =
71+ case x of
72+ Left _ ->
73+ throw " Node.Stream.onData: Stream encoding should not be set"
74+ Right buf ->
75+ pure buf
7576
7677-- | Listen for `data` events, returning data in a String, which will be
77- -- | decoded using the given encoding.
78- onDataString :: forall w eff . Readable w eff -> Encoding -> (String -> Eff eff Unit ) -> Eff eff Unit
78+ -- | decoded using the given encoding. Note that this will fail if `setEncoding`
79+ -- | has been called on the stream.
80+ onDataString :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> Encoding -> (String -> Eff (err :: EXCEPTION | eff ) Unit ) -> Eff (err :: EXCEPTION | eff ) Unit
7981onDataString r enc cb = onData r $ \buf -> do
8082 str <- unsafeInterleaveEff (Buffer .toString enc buf)
8183 cb str
8284
85+ foreign import onDataEitherImpl :: forall w eff . (forall l r . l -> Either l r ) -> (forall l r . r -> Either l r ) -> Readable w eff -> (Either String Buffer -> Eff eff Unit ) -> Eff eff Unit
86+
87+ -- | Listen for `data` events, returning data in an `Either String Buffer`. This
88+ -- | function is provided for the (hopefully rare) case that `setEncoding` has
89+ -- | been called on the stream.
90+ onDataEither :: forall w eff . Readable w eff -> (Either String Buffer -> Eff eff Unit ) -> Eff eff Unit
91+ onDataEither = onDataEitherImpl Left Right
92+
93+ foreign import setEncodingImpl :: forall w eff . Readable w eff -> String -> Eff eff Unit
94+
95+ -- | Set the encoding used to read chunks as strings from the stream. This
96+ -- | function may be useful when you are passing a readable stream to some other
97+ -- | JavaScript library, which already expects an encoding to be set.
98+ -- |
99+ -- | Where possible, you should try to use `onDataString` instead of this
100+ -- | function.
101+ setEncoding :: forall w eff . Readable w eff -> Encoding -> Eff eff Unit
102+ setEncoding r enc = setEncodingImpl r (show enc)
103+
83104-- | Listen for `end` events.
84105foreign import onEnd :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
85106
0 commit comments