mirror of
https://github.com/edcommonwealth/Dashboard.git
synced 2026-03-07 21:38:14 -08:00
39 lines
918 B
JavaScript
39 lines
918 B
JavaScript
import {PassThrough as PassThroughStream} from 'node:stream';
|
|
|
|
export default function mergeStreams(streams) {
|
|
if (!Array.isArray(streams)) {
|
|
throw new TypeError(`Expected an array, got \`${typeof streams}\`.`);
|
|
}
|
|
|
|
const passThroughStream = new PassThroughStream({objectMode: true});
|
|
passThroughStream.setMaxListeners(Number.POSITIVE_INFINITY);
|
|
|
|
if (streams.length === 0) {
|
|
passThroughStream.end();
|
|
return passThroughStream;
|
|
}
|
|
|
|
let activeStreams = streams.length;
|
|
|
|
for (const stream of streams) {
|
|
if (!(typeof stream?.pipe === 'function')) {
|
|
throw new TypeError(`Expected a stream, got: \`${typeof stream}\`.`);
|
|
}
|
|
|
|
stream.pipe(passThroughStream, {end: false});
|
|
|
|
stream.on('end', () => {
|
|
activeStreams--;
|
|
|
|
if (activeStreams === 0) {
|
|
passThroughStream.end();
|
|
}
|
|
});
|
|
|
|
stream.on('error', error => {
|
|
passThroughStream.emit('error', error);
|
|
});
|
|
}
|
|
|
|
return passThroughStream;
|
|
}
|