streamcat

2.6.5 • Public • Published

streamcat

Concatenate Node JS Streams!

You can concatenate the result of Promises, non async buffers and other streams allowing you to sequence things into an asynchronous Stream.

Usage

Example, print the contents of myfileA followed by myfileB, this is equivalent to using cat(1): cat myfileA myfileB.

var streamCat = require('streamcat');
 
streamCat([
    fs.createReadStream('myfileA'),
    fs.createReadStream('myfileB')
]).pipe(process.stdout);

API

streamCat(listOfStreamsBuffersOrPromises)

streamCat will take a list of ReadStreams, Buffers, or Promises returning a ReadStream or Buffer as its only argument and returns a ReadStream that will stream the concatenation of each item in the list.

You can mix Buffer, ReadStream, and Promise usage, in this instance, the String "Some content" will be streamed between myfileA and myfileB:

streamCat([
    fs.createReadStream('myfileA'),
    new Buffer("Some content"),
    fs.createReadStream('myfileB')
]).pipe(process.stdout);

Example using Promises that has the same result as the above example, the value in the Promise is resolved asynchronously:

streamCat([
    fs.createReadStream('myfileA'),
    Promise.resolve(new Buffer("Some content")),
    fs.createReadStream('myfileB')
]).pipe(process.stdout);
 

Error Handling

The streamCat function can take an optional options object as its second argument.

errorMapper

The errorMapper option allows you to specify a function that can transform an error from any of the contatenated streams before emitting the error on the returned stream.

The errorMapper must return an error object.

{
    errorMapper: function(error) {
        error.message = error.message + ": my custom context";
        return error;
    }
}

License

MIT

Package Sidebar

Install

npm i streamcat

Weekly Downloads

7

Version

2.6.5

License

MIT

Unpacked Size

20.8 kB

Total Files

18

Last publish

Collaborators

  • samgiles
  • the-ft