UNPKG

@techmely/utils

Version:

Collection of helpful JavaScript / TypeScript utils

67 lines (60 loc) 1.96 kB
/*! * @techmely/utils * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com> * MIT Licensed */ "use strict"; (() => { var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) { if (typeof require !== "undefined") return require.apply(this, arguments); throw Error('Dynamic require of "' + x + '" is not supported'); }); // src/mergeStreams.ts var import_node_stream = __require("node:stream"); // src/invariant.ts var prefix = "Invariant failed"; function invariant(condition, message) { if (condition) { return; } if (typeof message === "string" || typeof message === "function") { const provided = typeof message === "function" ? message() : message; const value = provided ? `${prefix}: ${provided}` : prefix; throw new Error(value); } if (message) throw message; throw new Error(prefix); } // src/isArray.ts function isArray(val) { return val && Array.isArray(val); } // src/mergeStreams.ts function mergeStreams(streams) { invariant(isArray(streams), "Expect input an array streams"); const passThroughStream = new import_node_stream.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; } })();