1818-- |
1919-- | #### Implementation
2020-- |
21- -- | The reading functions in this module all operate on a `Readable` stream
21+ -- | The `read*` functions (not to be confused with the `readable*` functions)
22+ -- | in this module all operate on a `Readable` stream
2223-- | in
2324-- | [“paused mode”](https://nodejs.org/docs/latest/api/stream.html#stream_two_reading_modes).
2425-- |
8081-- |
8182-- | If a write fails then it will `throwError` in the `Aff`.
8283module Node.Stream.Aff
83- ( readSome
84+ ( readableToStringUtf8
85+ , readableToString
86+ , readableToBuffers
87+ , readSome
8488 , readAll
8589 , readN
8690 , write
@@ -105,11 +109,75 @@ import Effect.Exception (catchException)
105109import Effect.Ref as Ref
106110import Node.Buffer (Buffer )
107111import Node.Buffer as Buffer
112+ import Node.Encoding (Encoding (..))
108113import Node.Encoding as Encoding
109- import Node.EventEmitter (once )
110- import Node.Stream (Readable , Writable , closeH , drainH , endH , errorH , readable , readableH )
114+ import Node.EventEmitter (on , once )
115+ import Node.Stream (Readable , Writable , closeH , dataH , drainH , endH , errorH , readable , readableH )
111116import Node.Stream as Stream
112117
118+ -- | Works on streams in "flowing" mode.
119+ -- | Reads all of the stream's contents into a buffer
120+ -- | and converts the result into a UTF8-encoded String.
121+ readableToStringUtf8
122+ :: forall m w
123+ . MonadAff m
124+ => Readable w
125+ -> m String
126+ readableToStringUtf8 r = readableToString r UTF8
127+
128+ -- | Works on streams in "flowing" mode.
129+ -- | Reads all of the stream's contents into a buffer
130+ -- | and converts the result into a String using the provided encoding.
131+ readableToString
132+ :: forall m w
133+ . MonadAff m
134+ => Readable w
135+ -> Encoding
136+ -> m String
137+ readableToString r enc = do
138+ bufs <- readableToBuffers r
139+ liftEffect $ Buffer .toString enc =<< Buffer .concat bufs
140+
141+ -- | Works on streams in "flowing" mode.
142+ -- | Reads all of the stream's buffered contents into an array.
143+ readableToBuffers
144+ :: forall m w
145+ . MonadAff m
146+ => Readable w
147+ -> m (Array Buffer )
148+ readableToBuffers r = liftAff $ makeAff \complete -> do
149+ bufs <- liftST $ Array.ST .new
150+ dataRef <- Ref .new (mempty :: Effect Unit )
151+ let removeData = join $ Ref .read dataRef
152+
153+ removeError <- r # once errorH \err -> do
154+ removeData
155+ complete $ Left err
156+
157+ removeClose <- r # once closeH do
158+ -- Don't error, instead return whatever we've read.
159+ removeError
160+ removeData
161+ result <- liftST $ Array.ST .unsafeFreeze bufs
162+ complete $ Right result
163+
164+ removeEnd <- r # once endH do
165+ removeError
166+ removeClose
167+ removeData
168+ result <- liftST $ Array.ST .unsafeFreeze bufs
169+ complete $ Right result
170+
171+ rmData <- r # on dataH \buf ->
172+ void $ liftST $ Array.ST .push buf bufs
173+
174+ Ref .write rmData dataRef
175+ pure $ effectCanceler do
176+ removeError
177+ removeClose
178+ removeEnd
179+ removeData
180+
113181-- | Works on streams in "paused" mode.
114182-- | Wait until there is some data available from the stream, then read it.
115183-- |
0 commit comments