streaming.js
This file
Path
lib/pipe/streaming.js
94 lines · 4.1 KiB · JavaScript
Last changed by
Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–24why, one step back1import {finished} from 'node:stream/promises';2import mergeStreams from '@sindresorhus/merge-streams';3import {incrementMaxListeners} from '../utils/max-listeners.js';4import {pipeStreams} from '../io/pipeline.js';56// The piping behavior is like Bash.7// In particular, when one subprocess exits, the other is not terminated by a signal.8// Instead, its stdout (for the source) or stdin (for the destination) closes.9// If the subprocess uses it, it will make it error with SIGPIPE or EPIPE (for the source) or end (for the destination).10// If it does not use it, it will continue running.11// This allows for subprocesses to gracefully exit and lower the coupling between subprocesses.12export const pipeSubprocessStream = (sourceStream, destinationStream, maxListenersController) => {13 const mergedStream = MERGED_STREAMS.has(destinationStream)14 ? pipeMoreSubprocessStream(sourceStream, destinationStream)15 : pipeFirstSubprocessStream(sourceStream, destinationStream);16 incrementMaxListeners(sourceStream, SOURCE_LISTENERS_PER_PIPE, maxListenersController.signal);17 incrementMaxListeners(destinationStream, DESTINATION_LISTENERS_PER_PIPE, maxListenersController.signal);18 cleanupMergedStreamsMap(destinationStream);19 return mergedStream;20};2122// We use `merge-streams` to allow for multiple sources to pipe to the same destination.23const pipeFirstSubprocessStream = (sourceStream, destinationStream) => {24 const mergedStream = mergeStreams([sourceStream]);Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
lines 25–26why, one step back25 addMergeSource(mergedStream, sourceStream);26 pipeStreams(mergedStream, destinationStream, () => unpipeSourcesOnDestinationEnd(mergedStream));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 27–33why, one step back27 MERGED_STREAMS.set(destinationStream, mergedStream);28 return mergedStream;29};3031const pipeMoreSubprocessStream = (sourceStream, destinationStream) => {32 const mergedStream = MERGED_STREAMS.get(destinationStream);33 mergedStream.add(sourceStream);Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
line 34why, one step back34 addMergeSource(mergedStream, sourceStream);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 35why, one step back35 return mergedStream;Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
lines 36–76why, one step back36};3738/*39One source stream can be piped to multiple destinations, e.g. `subprocess.pipe(first)` and `subprocess.pipe(second)`.40When a destination ends, its source must only be aborted when it is its last destination, so the other destinations keep receiving data.41Sources with several destinations are then only removed from the ended destination's merge stream, instead of being aborted.42This also holds when a source has been piped to one destination and unpiped from another one.43*/44const MERGE_SOURCES = new WeakMap();45const SOURCE_MERGES = new WeakMap();4647const addMergeSource = (mergedStream, sourceStream) => {48 addToSetMap(MERGE_SOURCES, mergedStream, sourceStream);49 addToSetMap(SOURCE_MERGES, sourceStream, mergedStream);50};5152const addToSetMap = (map, key, value) => {53 if (!map.has(key)) {54 map.set(key, new Set());55 }5657 map.get(key).add(value);58};5960// Stop piping a source into a destination, e.g. with the `unpipeSignal` option61export const unpipeSubprocessStream = async (mergedStream, sourceStream) => {62 MERGE_SOURCES.get(mergedStream)?.delete(sourceStream);63 SOURCE_MERGES.get(sourceStream)?.delete(mergedStream);64 await mergedStream.remove(sourceStream);65};6667const unpipeSourcesOnDestinationEnd = async mergedStream => {68 for (const sourceStream of MERGE_SOURCES.get(mergedStream)) {69 const mergedStreams = SOURCE_MERGES.get(sourceStream);70 mergedStreams.delete(mergedStream);71 if (mergedStreams.size > 0) {72 // Other destinations still need that source, so it must not be aborted73 // eslint-disable-next-line no-await-in-loop74 await mergedStream.remove(sourceStream);75 }76 }100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 77–94why, one step back77};7879const cleanupMergedStreamsMap = async destinationStream => {80 try {81 await finished(destinationStream, {cleanup: true, readable: false, writable: true});82 } catch {}8384 MERGED_STREAMS.delete(destinationStream);85};8687const MERGED_STREAMS = new WeakMap();8889// Number of listeners set up on `sourceStream` by each `sourceStream.pipe(destinationStream)`90// Those are added by `merge-streams`91const SOURCE_LISTENERS_PER_PIPE = 2;92// Number of listeners set up on `destinationStream` by each `sourceStream.pipe(destinationStream)`93// Those are added by `finished()` in `cleanupMergedStreamsMap()`94const DESTINATION_LISTENERS_PER_PIPE = 1;