Stream collect
Collects a readable streams data as a string buffer or, for object streams, an array.
npm install stream-collect
collect( stream, [encoding], [cb] )
Collect the contents of a stream. The collected data will either be a Buffer
, String
or Array
depending on whether encoding
has been supplied, or it is an object stream.
Returns a Promise
that will resolve with the collected data.
stream
the stream to collectencoding
the encoding to return data incb
a callback that will be called with the collected data.
var collect = require('stream-collect');
var file = fs.createReadableStream( 'myfile' );
collect(file)
.then( function(theWholeFile) {
} );
collect( file, function(theWholeFile) {
} )
collect( file, 'base64' )
.then( function(theWholeFileInBase64) {
} );
collect.stream()
/ collect.PassThrough()
collect.stream()
creates a stream.PassThrough
with an additional 'collect' event that can also act as a Promise.
var collect = require('stream-collect');
var file = fs.createReadableStream( 'myfile' );
file.pipe( collect.stream() )
.on( 'collect', function(data) {
} );
var file = fs.createReadableStream( 'myfile' );
file.pipe( collect.stream() )
.then(data) {
} );
The collect
event is called as part of the end
event.
When collect is piped, any errors from the source stream will be passed to this stream and can be caught in then
and catch
when used as a Promise.
collect.PassThrough()
is an alternative name for collect.stream
. Both can be used with or without the new
operator.
collect.objectStream()
/ collect.PassThroughObject()
Returns a new collect.stream
with objectMode
set to true.
collect.addToStream(stream)
Augment any stream with a collect
event.
Returns the augmented stream.