@@ -11,13 +11,17 @@ module Node.Stream
1111 , onDataString
1212 , onDataEither
1313 , setEncoding
14+ , onReadable
1415 , onEnd
1516 , onClose
1617 , onError
1718 , resume
1819 , pause
1920 , isPaused
2021 , pipe
22+ , read
23+ , readString
24+ , readEither
2125 , write
2226 , writeString
2327 , cork
@@ -29,6 +33,7 @@ module Node.Stream
2933import Prelude
3034
3135import Control.Bind ((<=<))
36+ import Data.Maybe (Maybe (..), maybe )
3237import Data.Either (Either (..))
3338import Node.Encoding
3439import Node.Buffer (Buffer ())
@@ -61,6 +66,11 @@ type Writable r = Stream (write :: Write | r)
6166-- | A duplex (readable _and_ writable stream)
6267type Duplex = Stream (read :: Read , write :: Write )
6368
69+ foreign import data Chunk :: *
70+ readChunk :: Chunk -> Either String Buffer
71+ readChunk = readChunkImpl Left Right
72+ foreign import readChunkImpl :: (forall l r . l -> Either l r ) -> (forall l r . r -> Either l r ) -> Chunk -> Either String Buffer
73+
6474-- | Listen for `data` events, returning data in a Buffer. Note that this will fail
6575-- | if `setEncoding` has been called on the stream.
6676onData :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> (Buffer -> Eff (err :: EXCEPTION | eff ) Unit ) -> Eff (err :: EXCEPTION | eff ) Unit
@@ -70,23 +80,44 @@ onData r cb =
7080 fromEither x =
7181 case x of
7282 Left _ ->
73- throw " Node.Stream.onData: Stream encoding should not be set"
83+ throw " Stream encoding should not be set"
7484 Right buf ->
7585 pure buf
7686
87+ read :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> Eff (err :: EXCEPTION | eff ) (Maybe Buffer )
88+ read r = do
89+ v <- readEither r
90+ case v of
91+ Nothing -> pure Nothing
92+ Just (Left _) -> throw " Stream encoding should not be set"
93+ Just (Right b) -> pure (Just b)
94+
95+ readString :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> Encoding -> Eff (err :: EXCEPTION | eff ) (Maybe String )
96+ readString r enc = do
97+ v <- readEither r
98+ case v of
99+ Nothing -> pure Nothing
100+ Just (Left _) -> throw " Stream encoding should not be set"
101+ Just (Right buf) -> Just <$> (unsafeInterleaveEff $ Buffer .toString enc buf)
102+
103+ readEither :: forall w eff . Readable w eff -> Eff eff (Maybe (Either String Buffer ))
104+ readEither = readImpl readChunk Nothing Just
105+
106+ foreign import readImpl :: forall r eff . (Chunk -> Either String Buffer ) -> (forall a . Maybe a ) -> (forall a . a -> Maybe a ) -> Readable r eff -> Eff eff (Maybe (Either String Buffer ))
107+
77108-- | Listen for `data` events, returning data in a String, which will be
78109-- | decoded using the given encoding. Note that this will fail if `setEncoding`
79110-- | has been called on the stream.
80111onDataString :: forall w eff . Readable w (err :: EXCEPTION | eff ) -> Encoding -> (String -> Eff (err :: EXCEPTION | eff ) Unit ) -> Eff (err :: EXCEPTION | eff ) Unit
81112onDataString r enc cb = onData r (cb <=< unsafeInterleaveEff <<< Buffer .toString enc)
82113
83- 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
84-
85114-- | Listen for `data` events, returning data in an `Either String Buffer`. This
86115-- | function is provided for the (hopefully rare) case that `setEncoding` has
87116-- | been called on the stream.
88- onDataEither :: forall w eff . Readable w eff -> (Either String Buffer -> Eff eff Unit ) -> Eff eff Unit
89- onDataEither = onDataEitherImpl Left Right
117+ onDataEither :: forall r eff . Readable r (err :: EXCEPTION | eff ) -> (Either String Buffer -> Eff (err :: EXCEPTION | eff ) Unit ) -> Eff (err :: EXCEPTION | eff ) Unit
118+ onDataEither r cb = onDataEitherImpl readChunk r cb
119+
120+ foreign import onDataEitherImpl :: forall r eff . (Chunk -> Either String Buffer ) -> Readable r eff -> (Either String Buffer -> Eff eff Unit ) -> Eff eff Unit
90121
91122foreign import setEncodingImpl :: forall w eff . Readable w eff -> String -> Eff eff Unit
92123
@@ -99,6 +130,9 @@ foreign import setEncodingImpl :: forall w eff. Readable w eff -> String -> Eff
99130setEncoding :: forall w eff . Readable w eff -> Encoding -> Eff eff Unit
100131setEncoding r enc = setEncodingImpl r (show enc)
101132
133+ -- | Listen for `readable` events.
134+ foreign import onReadable :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
135+
102136-- | Listen for `end` events.
103137foreign import onEnd :: forall w eff . Readable w eff -> Eff eff Unit -> Eff eff Unit
104138
0 commit comments