Skip to content

api.service.Stream 1.5.0

This service provides commonly useful streams for use with other streams e.g. to collect or emit octets.

Static methods

collectContent()

This method creates an octet stream collecting all received octets to be provided as a single buffer or string once the stream has ended.

javascript
const sink = api.service.Stream.collectContent();

The resulting stream has these properties:

  • done is a promise resolved with all collected octets as Buffer once the stream has ended.
  • doneString is another promise resolved with a string represented by those collected octets assuming a UTF-8 encoding.

In addition, these methods are provided:

  • takeSnapshot() is returning a Buffer containing all octets collected so far.

concat() 1.5.2

This method creates an octet stream successively passing emissions of provided octet streams.

javascript
const merged = api.service.Stream.concat( source1, source2, source3 );

The method takes one or more arguments. Each argument is either a readable stream or a promise for such a readable stream.

emitContent()

This method creates an octet stream emitting some provided textual of binary content in chunks of configurable size.

javascript
const source = api.service.Stream.emitContent( "Hello World!", 3 );

The method takes up to three arguments:

  • the whole content to be emitted
  • the number of octets to emit per chunk
  • the number of milliseconds to wait every time another chunk can be emitted

The content may be a buffer or a string which is converted into a buffer representing that string in UTF-8 encoding. The chunk size defaults to that buffer's number of octets. The delay is 0 by default which results in emission of chunks without any delay.

monitorProgress()

This method creates an octet stream basically passing all octets it is receiving while counting them. It emits an event named "step" everytime a given limit of seen octets has been reached. These events are emitted with

  • the zero-based index into the list of limits indicating the one that has been reached and
  • a boolean indicating if the limit has been reached in the same chunk as the previous limit.

Limits are provided as separate arguments to the method.

javascript
const monitor = api.service.Stream.monitorProgress( 1024, 32768, 65536 );

someSource.pipe( monitor ).pipe( someSink );

monitor.on( "step", ( index, sameChunk ) => { /* add your action here */ } );

randomOctets()

This method creates an octet stream emitting a selected number of random octets in chunks of configured size.

javascript
const source = api.service.Stream.randomOctets( 65536, 1024 );

The method takes up to three arguments:

  • the number of random octets to emit in total
  • the number of octets to emit per chunk
  • the number of milliseconds to wait every time another chunk can be emitted

The number of total octets is mandatory. The chunk size defaults to that number of octets. The delay is 0 by default which results in emission of chunks without any delay.

Static properties

The service is exposing all classes implementing streams used by static methods described above.

Concatenation 1.5.2

This class implements a readable stream successively emitting octets received from one or more readable streams provided on construction.

It is returned by concat().

Sink

This class implements a writable stream collecting received chunks of octets resolving promises in properties done and doneString once its writable side has been closed.

It is returned by collectContent().

Source

This class implements a readable stream emitting some provided string or Buffer in chunks of a configurable size.

It is returned by emitContent().

RandomSource

This class implements a readable stream emitting arbitrary octets.

It is returned by randomOctets().

Progress

This class implements a transform stream counting the passing octets and emitting events after having seen customizable amounts of octets.

It is returned by monitorProgress().