Skip to content
Myr

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 file

Take it

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 1–2why, one step back
1import {finished} from 'node:stream/promises';
2import mergeStreams from '@sindresorhus/merge-streams';

Rename files (#977)ehmickybefore adoption4b2e316

lines 3–4why, one step back
3import {incrementMaxListeners} from '../utils/max-listeners.js';
4import {pipeStreams} from '../io/pipeline.js';

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 5–6why, one step back
5
6// The piping behavior is like Bash.

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 7why, one step back
7// In particular, when one subprocess exits, the other is not terminated by a signal.

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 8why, one step back
8// 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 back
9// 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 back
10// 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 back
11// 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 back
13 const mergedStream = MERGED_STREAMS.has(destinationStream)

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 14–15why, one step back
14 ? pipeMoreSubprocessStream(sourceStream, destinationStream)
15 : pipeFirstSubprocessStream(sourceStream, destinationStream);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 16why, one step back
16 incrementMaxListeners(sourceStream, SOURCE_LISTENERS_PER_PIPE, maxListenersController.signal);

Improve streams pipe logic (#885)ehmickybefore adoption5abc429

lines 17–18why, one step back
17 incrementMaxListeners(destinationStream, DESTINATION_LISTENERS_PER_PIPE, maxListenersController.signal);
18 cleanupMergedStreamsMap(destinationStream);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 19–22why, one step back
19 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 back
23const pipeFirstSubprocessStream = (sourceStream, destinationStream) => {

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 24why, one step back
24 const mergedStream = mergeStreams([sourceStream]);

Improve streams pipe logic (#885)ehmickybefore adoption5abc429

line 25why, one step back
25 pipeStreams(mergedStream, destinationStream);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 26–29why, one step back
26 MERGED_STREAMS.set(destinationStream, mergedStream);
27 return mergedStream;28};29

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 30why, one step back
30const pipeMoreSubprocessStream = (sourceStream, destinationStream) => {

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 31–35why, one step back
31 const mergedStream = MERGED_STREAMS.get(destinationStream);
32 mergedStream.add(sourceStream);33 return mergedStream;34};35

Improve streams pipe logic (#885)ehmickybefore adoption5abc429

line 36why, one step back
36const cleanupMergedStreamsMap = async destinationStream => {

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 37–48why, one step back
37 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 back
49// 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;