Appearance
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 asBuffer
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 aBuffer
containing all octets collected so far.
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.