@push-stream-std/push-tee (0.0.7)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-tee@0.0.7"@push-stream-std/push-tee": "0.0.7"About this package
@push-stream-std/push-tee
Split a push-stream source into multiple branch sources, each delivering values to its own sink.
Published as @push-stream-std/push-tee.
Synopsis
import tee from '@push-stream-std/push-tee';
const branches = tee(source, 2);
branches[0].pipe(sinkA);
branches[1].pipe(sinkB);
source.resume();
import tee from '@push-stream-std/push-tee';
const sink1 = collect(null, function (err, result) { console.log('sink1', result); });
const sink2 = collect(null, function (err, result) { console.log('sink2', result); });
const stream = tee(source, [sink1, sink2]);
Install
npm config set @push-stream-std:registry https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/
npm config set -- //hub.kl1.tenere.ai/api/packages/push-stream-std/npm/:_authToken "$PACKAGE_TOKEN"
npm install @push-stream-std/push-tee
Why
Splits a single push-stream source into multiple branch sources. When called with a count, it returns an array of branch sources; each can be independently piped to a sink. When called with an array of sinks, it returns the source stream directly and distributes values to each sink. Branches use per-branch buffers with a head pointer pattern for amortized O(1) dequeues. Values are buffered during initialization until all branches have sinks connected. All branches must end before the upstream source is released.
API
tee(source, countOrSinks)
Splits a source into multiple branches.
- source
object– A push-stream source withpipe(),resume(),abort(). - countOrSinks
number | sink[]– Either the number of branches to create, or an array of pre-existing sinks to distribute values to. - Returns When given a number:
branch[]– an array of branch source objects, each withpipe(),resume(),write(),end(),abort(),sink,source,paused,ended. When given sinks: the source stream object withpipe(),write(),end(),abort(),resume(),sink,paused,ended.
Each branch source forwards values from the upstream and manages its own buffer. Aborting a branch aborts the upstream and all remaining branches.
Tests
npm test
Tests cover splitting into 2 branches, buffering for late-connecting branches, delivering end to all branches, abort propagation, multiple simultaneous connected sinks, edge cases (empty source, single branch, immediate end), and streaming high-volume data through multiple sinks.
Requirements
Node.js 22 or later. ESM only.