@push-stream-std/push-stream-base (0.0.4)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-stream-base@0.0.4"@push-stream-std/push-stream-base": "0.0.4"About this package
push-stream-base
Shared base classes and utilities for push-stream-std libraries.
Published as @push-stream-std/push-stream-base.
Synopsis
import { createSource, createSink, createThrough, createDuplex } from '@push-stream-std/push-stream-base';
const source = createSource(function resume() {
if (this.sink.paused) return;
this.sink.write('ping');
this.sink.end();
});
const sink = createSink(
function write(data) { console.log('received', data); },
function end(err) { console.log('done', err); }
);
source.pipe(sink);
import { createThrough, isSink, isSource } from '@push-stream-std/push-stream-base';
const mapper = createThrough(function write(data) {
return data.toUpperCase();
});
isSource(source); // true
isSink(sink); // true
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-stream-base
Description
Implements the canonical push-stream patterns from Dominic Tarr's specification. Sources start paused: true, sinks start paused: false. The pipe() method connects streams and invokes resume() to initiate data flow. Through streams use a head pointer pattern with periodic compaction (splice(0, head) when head > 1024) for amortized O(1) buffer operations instead of the O(n) shift() approach.
API
createSource(onResume)
Creates a push-stream source.
- onResume
(this) => any– Called on each resume loop iteration. Returned values control flow:false,null, orundefined– Stop the loop.number– Return without further processing.- Any other value – Continue emitting.
- Returns A source object with
pipe(),resume(),abort(),sink,paused, andended.
createSink(onWrite, onEnd)
Creates a push-stream sink.
- onWrite
(data) => void(optional) – Called with each written value. - onEnd
(err) => void(optional) – Called on stream end.errisnullfor normal end, or the error for abort/error. - Returns A sink object with
write(),end(),abort(),source,paused, andended.
createThrough(onWrite, onEnd)
Creates a push-stream through (transform) stream.
- onWrite
(data) => any(optional) – Transforms incoming data. Returnnullorundefinedto suppress the value. - onEnd
(err) => void(optional) – Called on stream end. - Returns A through object with
pipe(),write(),end(),abort(),resume(),source,sink,paused, andended. Uses an internal buffer with head pointer compaction.
createDuplex(sourceFactory, sinkFactory)
Creates a push-stream duplex (independent source and sink, not internally connected).
- sourceFactory
() => source(optional) – Factory returning the source side. - sinkFactory
() => sink(optional) – Factory returning the sink side. - Returns A duplex object with
pipe(),resume(),abort(),source,sink,paused,ended, andresolve(actualDuplex)for deferred resolution.
isSink(obj)
Returns true if obj has write and end methods (type guard for sinks).
isSource(obj)
Returns true if obj has pipe and resume methods (type guard for sources).
Tests
npm test
Requirements
Node.js 18 or later. ESM only.
License
MIT