| 1 |
import {on, once} from 'node:events'; |
| 2 |
import {PassThrough as PassThroughStream, getDefaultHighWaterMark} from 'node:stream'; |
| 3 |
import {finished} from 'node:stream/promises'; |
| 4 |
|
| 5 |
export default function mergeStreams(streams) { |
| 6 |
if (!Array.isArray(streams)) { |
| 7 |
throw new TypeError(`Expected an array, got \`${typeof streams}\`.`); |
| 8 |
} |
| 9 |
|
| 10 |
for (const stream of streams) { |
| 11 |
validateStream(stream); |
| 12 |
} |
| 13 |
|
| 14 |
const objectMode = streams.some(({readableObjectMode}) => readableObjectMode); |
| 15 |
const highWaterMark = getHighWaterMark(streams, objectMode); |
| 16 |
const passThroughStream = new MergedStream({ |
| 17 |
objectMode, |
| 18 |
writableHighWaterMark: highWaterMark, |
| 19 |
readableHighWaterMark: highWaterMark, |
| 20 |
}); |
| 21 |
|
| 22 |
for (const stream of streams) { |
| 23 |
passThroughStream.add(stream); |
| 24 |
} |
| 25 |
|
| 26 |
return passThroughStream; |
| 27 |
} |
| 28 |
|
| 29 |
const getHighWaterMark = (streams, objectMode) => { |
| 30 |
if (streams.length === 0) { |
| 31 |
return getDefaultHighWaterMark(objectMode); |
| 32 |
} |
| 33 |
|
| 34 |
const highWaterMarks = streams |
| 35 |
.filter(({readableObjectMode}) => readableObjectMode === objectMode) |
| 36 |
.map(({readableHighWaterMark}) => readableHighWaterMark); |
| 37 |
return Math.max(...highWaterMarks); |
| 38 |
}; |
| 39 |
|
| 40 |
class MergedStream extends PassThroughStream { |
| 41 |
#streams = new Set([]); |
| 42 |
#ended = new Set([]); |
| 43 |
#aborted = new Set([]); |
| 44 |
#onFinished; |
| 45 |
#unpipeEvent = Symbol('unpipe'); |
| 46 |
#streamPromises = new WeakMap(); |
| 47 |
|
| 48 |
add(stream) { |
| 49 |
validateStream(stream); |
| 50 |
|
| 51 |
if (this.#streams.has(stream)) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
this.#streams.add(stream); |
| 56 |
|
| 57 |
this.#onFinished ??= onMergedStreamFinished(this, this.#streams, this.#unpipeEvent); |
| 58 |
const streamPromise = endWhenStreamsDone({ |
| 59 |
passThroughStream: this, |
| 60 |
stream, |
| 61 |
streams: this.#streams, |
| 62 |
ended: this.#ended, |
| 63 |
aborted: this.#aborted, |
| 64 |
onFinished: this.#onFinished, |
| 65 |
unpipeEvent: this.#unpipeEvent, |
| 66 |
}); |
| 67 |
this.#streamPromises.set(stream, streamPromise); |
| 68 |
|
| 69 |
stream.pipe(this, {end: false}); |
| 70 |
} |
| 71 |
|
| 72 |
async remove(stream) { |
| 73 |
validateStream(stream); |
| 74 |
|
| 75 |
if (!this.#streams.has(stream)) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
const streamPromise = this.#streamPromises.get(stream); |
| 80 |
if (streamPromise === undefined) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
this.#streamPromises.delete(stream); |
| 85 |
|
| 86 |
stream.unpipe(this); |
| 87 |
await streamPromise; |
| 88 |
return true; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
const onMergedStreamFinished = async (passThroughStream, streams, unpipeEvent) => { |
| 93 |
updateMaxListeners(passThroughStream, PASSTHROUGH_LISTENERS_COUNT); |
| 94 |
const controller = new AbortController(); |
| 95 |
|
| 96 |
try { |
| 97 |
await Promise.race([ |
| 98 |
onMergedStreamEnd(passThroughStream, controller), |
| 99 |
onInputStreamsUnpipe(passThroughStream, streams, unpipeEvent, controller), |
| 100 |
]); |
| 101 |
} finally { |
| 102 |
controller.abort(); |
| 103 |
updateMaxListeners(passThroughStream, -PASSTHROUGH_LISTENERS_COUNT); |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
const onMergedStreamEnd = async (passThroughStream, {signal}) => { |
| 108 |
try { |
| 109 |
await finished(passThroughStream, {signal, cleanup: true}); |
| 110 |
} catch (error) { |
| 111 |
errorOrAbortStream(passThroughStream, error); |
| 112 |
throw error; |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
const onInputStreamsUnpipe = async (passThroughStream, streams, unpipeEvent, {signal}) => { |
| 117 |
for await (const [unpipedStream] of on(passThroughStream, 'unpipe', {signal})) { |
| 118 |
if (streams.has(unpipedStream)) { |
| 119 |
unpipedStream.emit(unpipeEvent); |
| 120 |
} |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
const validateStream = stream => { |
| 125 |
if (typeof stream?.pipe !== 'function') { |
| 126 |
throw new TypeError(`Expected a readable stream, got: \`${typeof stream}\`.`); |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
const endWhenStreamsDone = async ({passThroughStream, stream, streams, ended, aborted, onFinished, unpipeEvent}) => { |
| 131 |
updateMaxListeners(passThroughStream, PASSTHROUGH_LISTENERS_PER_STREAM); |
| 132 |
const controller = new AbortController(); |
| 133 |
|
| 134 |
try { |
| 135 |
await Promise.race([ |
| 136 |
afterMergedStreamFinished(onFinished, stream, controller), |
| 137 |
onInputStreamEnd({ |
| 138 |
passThroughStream, |
| 139 |
stream, |
| 140 |
streams, |
| 141 |
ended, |
| 142 |
aborted, |
| 143 |
controller, |
| 144 |
}), |
| 145 |
onInputStreamUnpipe({ |
| 146 |
stream, |
| 147 |
streams, |
| 148 |
ended, |
| 149 |
aborted, |
| 150 |
unpipeEvent, |
| 151 |
controller, |
| 152 |
}), |
| 153 |
]); |
| 154 |
} finally { |
| 155 |
controller.abort(); |
| 156 |
updateMaxListeners(passThroughStream, -PASSTHROUGH_LISTENERS_PER_STREAM); |
| 157 |
} |
| 158 |
|
| 159 |
if (streams.size > 0 && streams.size === ended.size + aborted.size) { |
| 160 |
if (ended.size === 0 && aborted.size > 0) { |
| 161 |
abortStream(passThroughStream); |
| 162 |
} else { |
| 163 |
endStream(passThroughStream); |
| 164 |
} |
| 165 |
} |
| 166 |
}; |
| 167 |
|
| 168 |
const afterMergedStreamFinished = async (onFinished, stream, {signal}) => { |
| 169 |
try { |
| 170 |
await onFinished; |
| 171 |
if (!signal.aborted) { |
| 172 |
abortStream(stream); |
| 173 |
} |
| 174 |
} catch (error) { |
| 175 |
if (!signal.aborted) { |
| 176 |
errorOrAbortStream(stream, error); |
| 177 |
} |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
const onInputStreamEnd = async ({passThroughStream, stream, streams, ended, aborted, controller: {signal}}) => { |
| 182 |
try { |
| 183 |
await finished(stream, { |
| 184 |
signal, |
| 185 |
cleanup: true, |
| 186 |
readable: true, |
| 187 |
writable: false, |
| 188 |
}); |
| 189 |
if (streams.has(stream)) { |
| 190 |
ended.add(stream); |
| 191 |
} |
| 192 |
} catch (error) { |
| 193 |
if (signal.aborted || !streams.has(stream)) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
if (isAbortError(error)) { |
| 198 |
aborted.add(stream); |
| 199 |
} else { |
| 200 |
errorStream(passThroughStream, error); |
| 201 |
} |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
const onInputStreamUnpipe = async ({stream, streams, ended, aborted, unpipeEvent, controller: {signal}}) => { |
| 206 |
await once(stream, unpipeEvent, {signal}); |
| 207 |
|
| 208 |
if (!stream.readable) { |
| 209 |
return once(signal, 'abort', {signal}); |
| 210 |
} |
| 211 |
|
| 212 |
streams.delete(stream); |
| 213 |
ended.delete(stream); |
| 214 |
aborted.delete(stream); |
| 215 |
}; |
| 216 |
|
| 217 |
const endStream = stream => { |
| 218 |
if (stream.writable) { |
| 219 |
stream.end(); |
| 220 |
} |
| 221 |
}; |
| 222 |
|
| 223 |
const errorOrAbortStream = (stream, error) => { |
| 224 |
if (isAbortError(error)) { |
| 225 |
abortStream(stream); |
| 226 |
} else { |
| 227 |
errorStream(stream, error); |
| 228 |
} |
| 229 |
}; |
| 230 |
|
| 231 |
// This is the error thrown by `finished()` on `stream.destroy()` |
| 232 |
const isAbortError = error => error?.code === 'ERR_STREAM_PREMATURE_CLOSE'; |
| 233 |
|
| 234 |
const abortStream = stream => { |
| 235 |
if (stream.readable || stream.writable) { |
| 236 |
stream.destroy(); |
| 237 |
} |
| 238 |
}; |
| 239 |
|
| 240 |
// `stream.destroy(error)` crashes the process with `uncaughtException` if no `error` event listener exists on `stream`. |
| 241 |
// We take care of error handling on user behalf, so we do not want this to happen. |
| 242 |
const errorStream = (stream, error) => { |
| 243 |
if (!stream.destroyed) { |
| 244 |
stream.once('error', noop); |
| 245 |
stream.destroy(error); |
| 246 |
} |
| 247 |
}; |
| 248 |
|
| 249 |
const noop = () => {}; |
| 250 |
|
| 251 |
const updateMaxListeners = (passThroughStream, increment) => { |
| 252 |
const maxListeners = passThroughStream.getMaxListeners(); |
| 253 |
if (maxListeners !== 0 && maxListeners !== Number.POSITIVE_INFINITY) { |
| 254 |
passThroughStream.setMaxListeners(maxListeners + increment); |
| 255 |
} |
| 256 |
}; |
| 257 |
|
| 258 |
// Number of times `passThroughStream.on()` is called regardless of streams: |
| 259 |
// - once due to `finished(passThroughStream)` |
| 260 |
// - once due to `on(passThroughStream)` |
| 261 |
const PASSTHROUGH_LISTENERS_COUNT = 2; |
| 262 |
|
| 263 |
// Number of times `passThroughStream.on()` is called per stream: |
| 264 |
// - once due to `stream.pipe(passThroughStream)` |
| 265 |
const PASSTHROUGH_LISTENERS_PER_STREAM = 1; |