UNPKG

sflow

Version:

sflow is a powerful and highly-extensible library designed for processing and manipulating streams of data effortlessly. Inspired by the functional programming paradigm, it provides a rich set of utilities for transforming streams, including chunking, fil

18 lines (17 loc) 819 B
/** * Create a TransformStream that tees (forks) the stream, passing one branch to the given function or writable stream. * @warning Uses `ReadableStream.tee()` internally. If the forked branch is consumed slower than the main branch, * the tee buffer will grow unboundedly in memory (no backpressure between tee branches). */ export const tees: { <T>(fn: (s: ReadableStream<T>) => undefined | any): TransformStream<T, T>; <T>(stream?: WritableStream<T>): TransformStream<T, T>; } = (arg) => { if (!arg) return new TransformStream(); if (arg instanceof WritableStream) return tees((s) => s.pipeTo(arg)); const fn = arg as (s: ReadableStream<unknown>) => unknown; const { writable, readable } = new TransformStream(); const [a, b] = readable.tee(); void fn(a); return { writable, readable: b }; };