@@ -27,17 +27,19 @@ module Node.Stream
2727import Prelude
2828
2929import Node.Encoding
30+ import Node.Buffer (Buffer ())
31+ import Node.Buffer as Buffer
3032
3133import Control.Monad.Eff
34+ import Control.Monad.Eff.Unsafe (unsafeInterleaveEff )
3235
3336-- | A stream.
3437-- |
3538-- | The type arguments track, in order:
3639-- |
3740-- | - Whether reading and/or writing from/to the stream are allowed.
3841-- | - Effects associated with reading/writing from/to this stream.
39- -- | - The type of chunks which will be read from/written to this stream (`String` or `Buffer`).
40- foreign import data Stream :: # * -> # ! -> * -> *
42+ foreign import data Stream :: # * -> # ! -> *
4143
4244-- | A phantom type associated with _readable streams_.
4345data Read
@@ -54,56 +56,66 @@ type Writable r = Stream (write :: Write | r)
5456-- | A duplex (readable _and_ writable stream)
5557type Duplex = Stream (read :: Read , write :: Write )
5658
57- foreign import setEncodingImpl :: forall w eff . Readable w eff String -> String -> Eff eff Unit
59+ foreign import setEncodingImpl :: forall w eff . Readable w eff -> String -> Eff eff Unit
5860
59- -- | Set the encoding used to read chunks from the stream.
60- setEncoding :: forall w eff . Readable w eff String -> Encoding -> Eff eff Unit
61+ -- | Set the encoding used to read chunks as strings from the stream.
62+ setEncoding :: forall w eff . Readable w eff -> Encoding -> Eff eff Unit
6163setEncoding r enc = setEncodingImpl r (show enc)
6264
63- -- | Listen for `data` events.
64- foreign import onData :: forall w eff a . Readable w eff a -> (a -> Eff eff Unit ) -> Eff eff Unit
65+ foreign import onDataImpl :: forall w eff a . Readable w eff -> (a -> Eff eff Unit ) -> Eff eff Unit
66+
67+ -- | Listen for `data` events, returning data in a Buffer.
68+ onData :: forall w eff . Readable w eff -> (Buffer -> Eff eff Unit ) -> Eff eff Unit
69+ onData = onDataImpl
70+
71+ -- | Listen for `data` events, returning data in a String, decoded with the
72+ -- | given encoding.
73+ onDataString :: forall w eff . Readable w eff -> Encoding -> (String -> Eff eff Unit ) -> Eff eff Unit
74+ onDataString r enc cb = onData r $ \buf -> do
75+ str <- unsafeInterleaveEff (Buffer .toString enc buf)
76+ cb str
6577
6678-- | Listen for `end` events.
67- foreign import onEnd :: forall w eff a . Readable w eff a -> Eff eff Unit -> Eff eff Unit
79+ foreign import onEnd :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
6880
6981-- | Listen for `close` events.
70- foreign import onClose :: forall w eff a . Readable w eff a -> Eff eff Unit -> Eff eff Unit
82+ foreign import onClose :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
7183
7284-- | Listen for `error` events.
73- foreign import onError :: forall w eff a . Readable w eff a -> Eff eff Unit -> Eff eff Unit
85+ foreign import onError :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
7486
7587-- | Resume reading from the stream.
76- foreign import resume :: forall w eff a . Readable w eff a -> Eff eff Unit
88+ foreign import resume :: forall w eff . Readable w eff -> Eff eff Unit
7789
7890-- | Pause reading from the stream.
79- foreign import pause :: forall w eff a . Readable w eff a -> Eff eff Unit
91+ foreign import pause :: forall w eff . Readable w eff -> Eff eff Unit
8092
8193-- | Check whether or not a stream is paused for reading.
82- foreign import isPaused :: forall w eff a . Readable w eff a -> Eff eff Boolean
94+ foreign import isPaused :: forall w eff . Readable w eff -> Eff eff Boolean
8395
8496-- | Read chunks from a readable stream and write them to a writable stream.
85- foreign import pipe :: forall r w eff a . Readable w eff a -> Writable r eff a -> Eff eff (Writable r eff a )
97+ foreign import pipe :: forall r w eff . Readable w eff -> Writable r eff -> Eff eff (Writable r eff )
8698
87- -- | Write a chunk to a writable stream.
88- foreign import write :: forall r eff a . Writable r eff String -> a -> Eff eff Unit -> Eff eff Boolean
99+ -- | Write a Buffer to a writable stream.
100+ foreign import write :: forall r eff . Writable r eff -> Buffer -> Eff eff Unit -> Eff eff Boolean
89101
90- foreign import writeStringImpl :: forall r eff . Writable r eff String -> String -> String -> Eff eff Unit -> Eff eff Boolean
102+ foreign import writeStringImpl :: forall r eff . Writable r eff -> String -> String -> Eff eff Unit -> Eff eff Boolean
91103
92104-- | Write a string in the specified encoding to a writable stream.
93- writeString :: forall r eff . Writable r eff String -> Encoding -> String -> Eff eff Unit -> Eff eff Boolean
105+ writeString :: forall r eff . Writable r eff -> Encoding -> String -> Eff eff Unit -> Eff eff Boolean
94106writeString w enc = writeStringImpl w (show enc)
95107
96108-- | Force buffering of writes.
97- foreign import cork :: forall r eff a . Writable r eff a -> Eff eff Unit
109+ foreign import cork :: forall r eff . Writable r eff -> Eff eff Unit
98110
99111-- | Flush buffered data.
100- foreign import uncork :: forall r eff a . Writable r eff a -> Eff eff Unit
112+ foreign import uncork :: forall r eff . Writable r eff -> Eff eff Unit
101113
102- foreign import setDefaultEncodingImpl :: forall r eff . Writable r eff String -> String -> Eff eff Unit
114+ foreign import setDefaultEncodingImpl :: forall r eff . Writable r eff -> String -> Eff eff Unit
103115
104- -- | Set the default encoding used to write chunks to the stream.
105- setDefaultEncoding :: forall r eff . Writable r eff String -> Encoding -> Eff eff Unit
116+ -- | Set the default encoding used to write chunks to the stream. This
117+ setDefaultEncoding :: forall r eff . Writable r eff -> Encoding -> Eff eff Unit
106118setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc)
107119
108120-- | End writing data to the stream.
109- foreign import end :: forall r eff a . Writable r eff a -> Eff eff Unit -> Eff eff Unit
121+ foreign import end :: forall r eff . Writable r eff -> Eff eff Unit -> Eff eff Unit
0 commit comments