streaming.js
This file
Path
lib/pipe/streaming.js
51 lines · 2.4 KiB · JavaScript
Last changed by
Rename files (#977)ehmickybefore adoption4b2e316
History of this fileTake it
Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 1–2why, one step back1import {finished} from 'node:stream/promises';2import mergeStreams from '@sindresorhus/merge-streams';Rename files (#977)ehmickybefore adoption4b2e316
lines 3–4why, one step back3import {incrementMaxListeners} from '../utils/max-listeners.js';4import {pipeStreams} from '../io/pipeline.js';Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 5–6why, one step back56// The piping behavior is like Bash.Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 7why, one step back7// In particular, when one subprocess exits, the other is not terminated by a signal.Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 8why, one step back8// Instead, its stdout (for the source) or stdin (for the destination) closes.Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 9why, one step back9// If the subprocess uses it, it will make it error with SIGPIPE or EPIPE (for the source) or end (for the destination).Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 10why, one step back10// If it does not use it, it will continue running.Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 11–12why, one step back11// This allows for subprocesses to gracefully exit and lower the coupling between subprocesses.12export const pipeSubprocessStream = (sourceStream, destinationStream, maxListenersController) => {Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 13why, one step back13 const mergedStream = MERGED_STREAMS.has(destinationStream)Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 14–15why, one step back14 ? pipeMoreSubprocessStream(sourceStream, destinationStream)15 : pipeFirstSubprocessStream(sourceStream, destinationStream);Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 16why, one step back16 incrementMaxListeners(sourceStream, SOURCE_LISTENERS_PER_PIPE, maxListenersController.signal);Improve streams pipe logic (#885)ehmickybefore adoption5abc429
lines 17–18why, one step back17 incrementMaxListeners(destinationStream, DESTINATION_LISTENERS_PER_PIPE, maxListenersController.signal);18 cleanupMergedStreamsMap(destinationStream);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 19–22why, one step back19 return mergedStream;20};2122// We use `merge-streams` to allow for multiple sources to pipe to the same destination.Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 23why, one step back23const pipeFirstSubprocessStream = (sourceStream, destinationStream) => {Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 24why, one step back24 const mergedStream = mergeStreams([sourceStream]);Improve streams pipe logic (#885)ehmickybefore adoption5abc429
line 25why, one step back25 pipeStreams(mergedStream, destinationStream);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 26–29why, one step back26 MERGED_STREAMS.set(destinationStream, mergedStream);27 return mergedStream;28};29Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 30why, one step back30const pipeMoreSubprocessStream = (sourceStream, destinationStream) => {Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 31–35why, one step back31 const mergedStream = MERGED_STREAMS.get(destinationStream);32 mergedStream.add(sourceStream);33 return mergedStream;34};35Improve streams pipe logic (#885)ehmickybefore adoption5abc429
line 36why, one step back36const cleanupMergedStreamsMap = async destinationStream => {Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 37–48why, one step back37 try {38 await finished(destinationStream, {cleanup: true, readable: false, writable: true});39 } catch {}4041 MERGED_STREAMS.delete(destinationStream);42};4344const MERGED_STREAMS = new WeakMap();4546// Number of listeners set up on `sourceStream` by each `sourceStream.pipe(destinationStream)`47// Those are added by `merge-streams`48const SOURCE_LISTENERS_PER_PIPE = 2;Improve streams pipe logic (#885)ehmickybefore adoption5abc429
lines 49–51why, one step back49// Number of listeners set up on `destinationStream` by each `sourceStream.pipe(destinationStream)`50// Those are added by `finished()` in `cleanupMergedStreamsMap()`51const DESTINATION_LISTENERS_PER_PIPE = 1;