@@ -7,9 +7,10 @@ module Node.Stream
7
7
, Write ()
8
8
, Writable ()
9
9
, Duplex ()
10
- , setEncoding
11
10
, onData
12
11
, onDataString
12
+ , onDataEither
13
+ , setEncoding
13
14
, onEnd
14
15
, onClose
15
16
, onError
@@ -27,11 +28,14 @@ module Node.Stream
27
28
28
29
import Prelude
29
30
31
+ import Control.Bind ((<=<))
32
+ import Data.Either (Either (..))
30
33
import Node.Encoding
31
34
import Node.Buffer (Buffer ())
32
35
import Node.Buffer as Buffer
33
36
34
37
import Control.Monad.Eff
38
+ import Control.Monad.Eff.Exception (throw , EXCEPTION ())
35
39
import Control.Monad.Eff.Unsafe (unsafeInterleaveEff )
36
40
37
41
-- | A stream.
@@ -57,29 +61,46 @@ type Writable r = Stream (write :: Write | r)
57
61
-- | A duplex (readable _and_ writable stream)
58
62
type Duplex = Stream (read :: Read , write :: Write )
59
63
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
75
76
76
77
-- | 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
79
81
onDataString r enc cb = onData r $ \buf -> do
80
82
str <- unsafeInterleaveEff (Buffer .toString enc buf)
81
83
cb str
82
84
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
+
83
104
-- | Listen for `end` events.
84
105
foreign import onEnd :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
85
106
0 commit comments