What is stream-splicer?
The stream-splicer npm package allows you to create a stream that can be dynamically spliced with other streams. This is particularly useful for building complex streaming pipelines where you need to insert, remove, or replace streams on the fly.
What are stream-splicer's main functionalities?
Dynamic Stream Splicing
This feature allows you to dynamically splice streams into a pipeline. In this example, a through stream is created that converts input to uppercase. The stream-splicer package is used to create a stream pipeline that can be dynamically modified.
const splicer = require('stream-splicer');
const through = require('through2');
const stream = splicer([
through(function (chunk, enc, callback) {
this.push(chunk.toString().toUpperCase());
callback();
})
]);
stream.write('hello');
stream.write('world');
stream.end();
stream.pipe(process.stdout);
Inserting Streams
This feature allows you to insert new streams into an existing pipeline. In this example, a new stream is inserted that reverses the input string.
const splicer = require('stream-splicer');
const through = require('through2');
const stream = splicer([
through(function (chunk, enc, callback) {
this.push(chunk.toString().toUpperCase());
callback();
})
]);
const newStream = through(function (chunk, enc, callback) {
this.push(chunk.toString().split('').reverse().join(''));
callback();
});
stream.splice(1, 0, newStream);
stream.write('hello');
stream.write('world');
stream.end();
stream.pipe(process.stdout);
Removing Streams
This feature allows you to remove streams from an existing pipeline. In this example, the second stream that reverses the input string is removed from the pipeline.
const splicer = require('stream-splicer');
const through = require('through2');
const stream = splicer([
through(function (chunk, enc, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}),
through(function (chunk, enc, callback) {
this.push(chunk.toString().split('').reverse().join(''));
callback();
})
]);
stream.splice(1, 1);
stream.write('hello');
stream.write('world');
stream.end();
stream.pipe(process.stdout);
Other packages similar to stream-splicer
through2
The through2 package is a thin wrapper around Node.js streams.Transform. It simplifies the creation of transform streams. Unlike stream-splicer, through2 does not provide dynamic splicing capabilities but is often used in conjunction with other stream libraries to build pipelines.
multipipe
The multipipe package allows you to compose multiple streams into a single pipeline. It provides error handling and simplifies the process of connecting streams. However, it does not support dynamic splicing of streams like stream-splicer.
pumpify
The pumpify package combines multiple streams into a single duplex stream. It handles errors and ensures proper cleanup. While it is useful for creating complex pipelines, it does not offer the dynamic splicing capabilities of stream-splicer.
stream-splicer
streaming pipeline with a mutable configuration
This module is similar to
stream-combiner,
but with a pipeline configuration that can be changed at runtime.
example
This example begins with an HTTP header parser that waits for an empty line to
signify the end of the header. At that point, it switches to a streaming json
parser to operate on the HTTP body.
var splicer = require('stream-splicer');
var through = require('through2');
var JSONStream = require('JSONStream');
var split = require('split');
var headerData = {};
var headers = through.obj(function (buf, enc, next) {
var line = buf.toString('utf8');
if (line === '') {
this.push(headerData);
pipeline.splice(1, 1, JSONStream.parse([ 'rows', true ]));
}
else {
var m = /^(\S+):(.+)/.exec(line);
var key = m && m[1].trim();
var value = m && m[2].trim();
if (m) headerData[key] = value;
}
next();
});
var pipeline = splicer([ split(), headers, JSONStream.stringify() ]);
process.stdin.pipe(pipeline).pipe(process.stdout);
intput:
GET / HTTP/1.1
Host: substack.net
User-Agent: echo
{"rows":["beep","boop"]}
output:
$ echo -ne 'GET / HTTP/1.1\nHost: substack.net\nUser-Agent: echo\n\n{"rows":["beep","boop"]}\n' | node example/header.js
[
{"Host":"substack.net","User-Agent":"echo"}
,
"beep"
,
"boop"
]
methods
var splicer = require('stream-splicer')
var pipeline = splicer(streams, opts)
Create a pipeline
duplex stream given an array of streams
. Each stream
will be piped to the next. Writes to pipeline
get written to the first stream
and data for reads from pipeline
come from the last stream.
For example, for streams [ a, b, c, d ]
, this pipeline is constructed
internally:
a.pipe(b).pipe(c).pipe(d)
Input will get written into a
. Output will be read from d
.
var pipeline = splicer.obj(streams, opts)
Create a pipeline
with opts.objectMode
set to true for convenience.
var removed = pipeline.splice(index, howMany, stream, ...)
Splice the pipeline starting at index
, removing howMany
streams and
replacing them with each additional stream
argument provided.
The streams that were removed from the splice and returned.
pipeline.push(stream, ...)
Push one or more streams to the end of the pipeline.
var stream = pipeline.pop()
Pop a stream from the end of the pipeline.
pipeline.unshift(stream, ...)
Unshift one or more streams to the begining of the pipeline.
var stream = pipeline.shift()
Shift a stream from the begining of the pipeline.
install
With npm do:
npm install stream-splicer
license
MIT