0.39.2 • Published 5 years ago

@util.js/streams v0.39.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

@util.js/streams

JavaScript utility methods for Node.js streams

@util.js/streams is part of Util.js.

const streams = require("@util.js/streams");
const readable = streams.fromString("The programmer learneth");
streams.stringify(readable).then(console.log); // Outputs "The programmer learneth".

This class contains all the members of Stream in addition to:

Streams API

Kind: global class
Access: public

streams.Duplex

Kind: instance class of Streams
Access: public

new Streams#Duplex()

A stream that is both Readable and Writable (for example, a TCP socket connection or net.Socket).

See Node.js's documentation about Duplex.

streams.PassThrough

Kind: instance class of Streams
Access: public

new Streams#PassThrough()

A trivial implementation of a Transform stream that simply passes the input bytes across to the output.

See Node.js's documentation about PassThrough.

streams.Readable

Kind: instance class of Streams
Access: public

new Streams#Readable()

A stream from which data can be read, an abstraction of a source (for example, fs.createReadStream()).

See Node.js's documentation about Readable.

streams.Stream

Kind: instance class of Streams
Access: public

new Streams#Stream()

An abstraction of sources (a place where data can be read from) and destinations (a place where data can be written to).

See Node.js's documentation about Stream.

streams.Transform

Kind: instance class of Streams
Access: public

new Streams#Transform()

Duplex streams where the output is in some way related to the input (for example, zlib.createDeflate()).

See Node.js's documentation about Transform.

streams.Writable

Kind: instance class of Streams
Access: public

new Streams#Writable()

A stream to which data can be written, an abstraction of a destination (for example, fs.createWriteStream()).

See Node.js's documentation about Writable.

streams.finished(stream, callback) ⇒ undefined | Promise

Sends a notification when a stream is no longer readable, writable, or has experienced an error or a premature close event.

See Node.js's documentation about finished.

Kind: instance method of Streams
Returns: undefined | Promise - undefined if a callback is specified or a Promise that resolves once the stream has finished
Throws:

  • Error If the Node.js version does not implement stream.finished

Access: public

ParamTypeDescription
streamStreamA readable and/or writable stream
callbackfunctionA callback function that takes an optional error argument

streams.fromString(string, encoding) ⇒ Readable

Converts a string into a stream.

A Stack Overflow answer inspired this implementation.

Kind: instance method of Streams
Returns: Readable - A stream of the specified string
Throws:

  • TypeError If string is not a string

Access: public

ParamTypeDescription
stringstringThe string to convert into a stream
encodingstringEncoding of the string that must be a valid Buffer encoding, such as 'utf8' or 'ascii'

streams.newReadable(params) ⇒ Readable

Creates a new Readable stream.

See Node.js's documentation about Readable.

Kind: instance method of Streams
Returns: Readable - A new Readable instance
Access: public

ParamTypeDefaultDescription
paramsObject
params.highWaterMarknumber16384 (16kb) or 16 for objectMode streamsThe maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource
params.encodingstringnullIf specified, then buffers will be decoded to strings using the specified encoding
params.objectModebooleanfalseWhether this stream should behave as a stream of objects meaning that stream.read(n) returns a single value instead of a Buffer of size n
params.readfunctionImplementation for the stream._read() method
params.destroyfunctionImplementation for the stream._destroy() method

streams.newWritable(params) ⇒ Writable

Creates a new Writable stream.

See Node.js's documentation about Writable.

Kind: instance method of Streams
Returns: Writable - A new Writable instance
Access: public

ParamTypeDefaultDescription
paramsObject
params.highWaterMarknumber16384 (16kb) or 16 for objectMode streamsBuffer level when stream.write() starts returning false
params.decodeStringsbooleantrueWhether or not to decode strings into Buffers before passing them to stream._write()
params.objectModebooleanfalseWhether or not the stream.write(anyObj) is a valid operation. When set, it becomes possible to write JavaScript values other than string, Buffer or Uint8Array if supported by the stream implementation.
params.emitClosebooleantrueWhether or not the stream should emit 'close' after it has been destroyed
params.writefunctionImplementation for the stream._write() method
params.writevfunctionImplementation for the stream._writev() method
params.destroyfunctionImplementation for the stream._destroy() method
params.finalfunctionImplementation for the stream._final() method

streams.pipeline(callback) ⇒ undefined | Promise

Pipes between streams forwarding errors and properly cleaning up and notifies when the pipeline is complete via a callback or a Promise.

See Node.js's documentation about pipeline.

Kind: instance method of Streams
Returns: undefined | Promise - undefined if a callback is specified or a Promise that resolves once the pipeline is complete
Throws:

  • Error If the Node.js version does not implement stream.pipeline

Access: public

ParamTypeDescription
...streamsStreamTwo or more streams to pipe between
callbackfunctionA callback function that takes an optional error argument

streams.stringify(readable, callback) ⇒ undefined | Promise

Converts a stream into a string.

A Stack Overflow answer inspired this implementation.

Kind: instance method of Streams
Returns: undefined | Promise - undefined if a callback is specified or a Promise that resolves with the resulting string
Access: public

ParamTypeDescription
readableReadableThe stream to convert into a string
callbackfunctionA callback function that takes two arguments, a string and an error
0.39.2

5 years ago

0.39.1

5 years ago

0.39.0

5 years ago

0.38.0

5 years ago

0.37.1

5 years ago

0.37.0

5 years ago

0.36.1

5 years ago

0.36.0

5 years ago

0.35.0

5 years ago

0.34.0

5 years ago

0.33.1

5 years ago

0.33.0

5 years ago

0.32.0

5 years ago

0.31.0

5 years ago

0.30.0

5 years ago

0.29.1

5 years ago