Skip to content
Myr

output-async.js

This file

Path

lib/io/output-async.js

82 lines · 3.0 KiB · JavaScript

Last changed by

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–12why, one step back
1import mergeStreams from '@sindresorhus/merge-streams';
2import {isStandardStream} from '../utils/standard-stream.js';3import {incrementMaxListeners} from '../utils/max-listeners.js';4import {TRANSFORM_TYPES} from '../stdio/type.js';5import {pipeStreams} from './pipeline.js';67// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async mode8// When multiple input streams are used, we merge them to ensure the output stream ends only once each input stream has ended9export const pipeOutputAsync = (subprocess, fileDescriptors, controller) => {10 const pipeGroups = new Map();1112 for (const [fdNumber, {stdioItems, direction}] of Object.entries(fileDescriptors)) {

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 13–14why, one step back
13 const transformItems = stdioItems.filter(({type}) => TRANSFORM_TYPES.has(type));
14 for (const {stream} of transformItems) {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 15–17why, one step back
15 pipeTransform(subprocess, stream, direction, fdNumber);
16 }17

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 18–19why, one step back
18 const nonTransformItems = stdioItems.filter(({type}) => !TRANSFORM_TYPES.has(type));
19 for (const {stream} of nonTransformItems) {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 20–30why, one step back
20 pipeStdioItem({
21 subprocess,22 stream,23 direction,24 fdNumber,25 pipeGroups,26 controller,27 });28 }29 }30

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

line 31why, one step back
31 for (const [outputStream, inputStreams] of pipeGroups) {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 32–82why, one step back
32 const inputStream = inputStreams.length === 1 ? inputStreams[0] : mergeStreams(inputStreams);
33 pipeStreams(inputStream, outputStream);34 }35};3637// When using transforms, `subprocess.stdin|stdout|stderr|stdio` is directly mutated38const pipeTransform = (subprocess, stream, direction, fdNumber) => {39 if (direction === 'output') {40 pipeStreams(subprocess.stdio[fdNumber], stream);41 } else {42 pipeStreams(stream, subprocess.stdio[fdNumber]);43 }4445 const streamProperty = SUBPROCESS_STREAM_PROPERTIES[fdNumber];46 if (streamProperty !== undefined) {47 subprocess[streamProperty] = stream;48 }4950 subprocess.stdio[fdNumber] = stream;51};5253const SUBPROCESS_STREAM_PROPERTIES = ['stdin', 'stdout', 'stderr'];5455// Most `std*` option values involve piping `subprocess.std*` to a stream.56// The stream is either passed by the user or created internally.57const pipeStdioItem = ({subprocess, stream, direction, fdNumber, pipeGroups, controller}) => {58 if (stream === undefined) {59 return;60 }6162 setStandardStreamMaxListeners(stream, controller);6364 const [inputStream, outputStream] = direction === 'output'65 ? [stream, subprocess.stdio[fdNumber]]66 : [subprocess.stdio[fdNumber], stream];67 const outputStreams = pipeGroups.get(inputStream) ?? [];68 pipeGroups.set(inputStream, [...outputStreams, outputStream]);69};7071// Multiple subprocesses might be piping from/to `process.std*` at the same time.72// This is not necessarily an error and should not print a `maxListeners` warning.73const setStandardStreamMaxListeners = (stream, {signal}) => {74 if (isStandardStream(stream)) {75 incrementMaxListeners(stream, MAX_LISTENERS_INCREMENT, signal);76 }77};7879// `source.pipe(destination)` adds at most 1 listener for each event.80// If `stdin` option is an array, the values might be combined with `merge-streams`.81// That library also listens for `source` end, which adds 1 more listener.82const MAX_LISTENERS_INCREMENT = 2;