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
14 lines (13 loc) • 576 B
text/typescript
/** you could use flats to re-join buffers, default buffer length is Infinity, which will enqueue when upstream drain */
export const buffers = chunks;
export function chunks<T>(n: number = Infinity) {
let chunks: T[] = [];
if (n <= 0) throw new Error("Buffer size must be greater than 0");
return new TransformStream<T, T[]>({
transform: async (chunk, ctrl) => {
chunks.push(chunk);
if (chunks.length >= n) ctrl.enqueue(chunks.splice(0, Infinity)); // clear chunks
},
flush: async (ctrl) => void (chunks.length && ctrl.enqueue(chunks)),
});
}