Product
Introducing Java Support in Socket
We're excited to announce that Socket now supports the Java programming language.
stream-splicer
Advanced tools
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.
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);
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.
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.
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.
streaming pipeline with a mutable configuration
This module is similar to stream-combiner, but with a pipeline configuration that can be changed at runtime.
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('jsonstream2');
var split = require('split2');
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"
]
var splicer = require('stream-splicer')
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
.
If any of the elements in streams
are arrays, they will be converted into
nested pipelines. This is useful if you want to expose a hookable pipeline with
grouped insertion points.
Create a pipeline
with opts.objectMode
set to true for convenience.
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.
Push one or more streams to the end of the pipeline.
Pop a stream from the end of the pipeline.
Unshift one or more streams to the begining of the pipeline.
Shift a stream from the begining of the pipeline.
Return the stream at index index, ...
. Indexes can be negative.
Multiple indexes will traverse into nested pipelines.
The number of streams in the pipeline
With npm do:
npm install stream-splicer
MIT
FAQs
streaming pipeline with a mutable configuration
The npm package stream-splicer receives a total of 733,283 weekly downloads. As such, stream-splicer popularity was classified as popular.
We found that stream-splicer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 40 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.
Security News
Deno 2.0 is now available with enhanced package management, full Node.js and npm compatibility, improved performance, and support for major JavaScript frameworks.