UNPKG

@techmely/utils

Version:

Collection of helpful JavaScript / TypeScript utils

47 lines (43 loc) 1.15 kB
/*! * @techmely/utils * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com> * MIT Licensed */ import { invariant } from "./chunk-JBXZG2HJ.mjs"; import { isArray } from "./chunk-BD32CMSL.mjs"; import "./chunk-5IEGYJY4.mjs"; import "./chunk-5EZFB54B.mjs"; import "./chunk-3RC6KXF4.mjs"; import "./chunk-NYLAFCGV.mjs"; // src/merge-streams.ts import { PassThrough } from "node: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 };