@techmely/utils
Version:
Collection of helpful JavaScript / TypeScript utils
41 lines (37 loc) • 976 B
JavaScript
/*!
* @techmely/utils
* Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com>
* MIT Licensed
*/
import {
invariant,
isArray
} from "./chunk-XMT5IFPR.js";
// src/mergeStreams.ts
import { PassThrough } from "stream";
function mergeStreams(streams) {
invariant(isArray(streams), "Expect input an array streams");
const passThroughStream = new PassThrough({ objectMode: true });
if (streams.length === 0) {
passThroughStream.end();
return passThroughStream;
}
let streamsCount = streams.length;
for (const stream of streams) {
invariant(!(typeof stream?.pipe === "function"), "Expect a stream, got ");
stream.pipe(passThroughStream, { end: false });
stream.on("end", () => {
streamsCount--;
if (streamsCount === 0) {
passThroughStream.end();
}
});
stream.on("error", (error) => {
passThroughStream.emit("error", error);
});
}
return passThroughStream;
}
export {
mergeStreams
};