Module

Node.Stream.Aff

Package
purescript-node-streams-aff
Repository
jamesdbrock/purescript-node-streams-aff

Asynchronous I/O with Node.js Stream.

Open file streams with Node.FS.Stream.

Open process streams with Node.Process.

All I/O errors will be thrown through the Aff MonadError class instance.

Reading

Implementation

The reading functions in this module all operate on a Readable stream in “paused mode”.

Internally the reading functions use the readable.read([size]) function and are subject to the caveats of that function.

Results

The result of a reading function may be chunked into more than one Buffer. The fst element of the result Tuple is an Array Buffer of what was read. To concatenate the result into a single Buffer, use Node.Buffer.concat :: Array Buffer -> Buffer.

input :: Buffer <- liftEffect <<< concat <<< fst =<< readSome stdin

To calculate the number of bytes read, use Node.Buffer.size :: Buffer -> m Int.

Tuple inputs _ :: Array Buffer <- readSome stdin
bytesRead :: Int
    <- liftEffect $ Array.foldM (\a b -> (a+_) <$> size b) 0 inputs

The snd element of the result Tuple is a Boolean flag which is true if the stream has not reached End-Of-File (and also if the stream has not errored or been destroyed.) If the flag is false then no more bytes will ever be produced by the stream.

Canceller argument

The reading functions suffixed with underscore take a canceller argument.

The canceller argument is an action to perform in the event that this Aff is cancelled. For example, to destroy the stream in the event that the Aff is cancelled pass Node.Stream.destroy as the canceller.

Writing

Implementation

The writing functions in this module all operate on a Writeable stream.

Internally the writing functions will call the writable.write(chunk[, encoding][, callback]) function on each of the Buffers, and will asychronously wait if there is “backpressure” from the stream.

The writing functions will complete after all the data is flushed to the stream.

Canceller argument

The writing functions suffixed with underscore take a canceller argument.

The canceller argument is an action to perform in the event that this Aff is cancelled.

#readSome Source

readSome :: forall m r. MonadAff m => Readable r -> m (Tuple (Array Buffer) Boolean)

Wait until there is some data available from the stream, then read it.

This function is useful for streams like stdin which never reach End-Of-File.

#readSome_ Source

readSome_ :: forall m r. MonadAff m => Readable r -> (Readable r -> Effect Unit) -> m (Tuple (Array Buffer) Boolean)

readSome with a canceller argument.

#readAll Source

readAll :: forall m r. MonadAff m => Readable r -> m (Tuple (Array Buffer) Boolean)

Read all data until the end of the stream. Note that stdin will never end.

#readAll_ Source

readAll_ :: forall m r. MonadAff m => Readable r -> (Readable r -> Effect Unit) -> m (Tuple (Array Buffer) Boolean)

readAll with a canceller argument.

#readN Source

readN :: forall m r. MonadAff m => Readable r -> Int -> m (Tuple (Array Buffer) Boolean)

Wait for N bytes to become available from the stream.

If more than N bytes are available on the stream, then completes with N bytes and leaves the rest in the stream’s internal buffer.

If the end of the stream is reached before N bytes are available, then completes with less than N bytes.

#readN_ Source

readN_ :: forall m r. MonadAff m => Readable r -> (Readable r -> Effect Unit) -> Int -> m (Tuple (Array Buffer) Boolean)

readN with a canceller argument.

#write Source

write :: forall m w. MonadAff m => Writable w -> Array Buffer -> m Unit

Write to a stream.

Will complete after the data is flushed to the stream.

#write_ Source

write_ :: forall m w. MonadAff m => Writable w -> (Writable w -> Effect Unit) -> Array Buffer -> m Unit

write with a canceller argument.

#writableClose Source

writableClose :: forall m w. MonadAff m => Writable w -> m Unit

Close a Writable file stream.

Will complete after the file stream is closed.