async.js
This file
Path
lib/stdio/async.js
104 lines · 4.4 KiB · JavaScript
Last changed by
Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
History of this fileTake it
Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 1why, one step back1import {createReadStream, createWriteStream} from 'node:fs';Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 2why, one step back2import {Buffer} from 'node:buffer';Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 3why, one step back3import {Readable, Writable} from 'node:stream';Improve streams pipe logic (#885)ehmickybefore adoption5abc429
line 4why, one step back4import mergeStreams from '@sindresorhus/merge-streams';Split files (#837)ehmickybefore adoption672c2d7
line 5why, one step back5import {isStandardStream, incrementMaxListeners} from '../utils.js';Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 6why, one step back6import {handleInput} from './handle.js';Improve stream cleanup and termination logic (#775)ehmickybefore adoption5aa90bd
line 7why, one step back7import {pipeStreams} from './pipeline.js';Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 8why, one step back8import {TYPE_TO_MESSAGE} from './type.js';Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 9why, one step back9import {generatorToDuplexStream, pipeGenerator} from './generator.js';Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 10–11why, one step back1011// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async modePrint output with `verbose` option (#884)ehmickybefore adoption6fda30f
line 12why, one step back12export const handleInputAsync = (options, verboseInfo) => handleInput(addPropertiesAsync, options, verboseInfo, false);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 13why, one step back13Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
lines 14–17why, one step back14const forbiddenIfAsync = ({type, optionName}) => {15 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]}.`);16};17Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 18–19why, one step back18const addPropertiesAsync = {19 input: {Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 20why, one step back20 generator: generatorToDuplexStream,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 21–28why, one step back21 fileUrl: ({value}) => ({stream: createReadStream(value)}),22 filePath: ({value}) => ({stream: createReadStream(value.file)}),23 webStream: ({value}) => ({stream: Readable.fromWeb(value)}),24 nodeStream: ({value}) => ({stream: value}),25 iterable: ({value}) => ({stream: Readable.from(value)}),26 string: ({value}) => ({stream: Readable.from(value)}),27 uint8Array: ({value}) => ({stream: Readable.from(Buffer.from(value))}),28 native() {},Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 29–30why, one step back29 },30 output: {Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 31why, one step back31 generator: generatorToDuplexStream,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 32–35why, one step back32 fileUrl: ({value}) => ({stream: createWriteStream(value)}),33 filePath: ({value}) => ({stream: createWriteStream(value.file)}),34 webStream: ({value}) => ({stream: Writable.fromWeb(value)}),35 nodeStream: ({value}) => ({stream: value}),Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 36why, one step back36 iterable: forbiddenIfAsync,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 37why, one step back37 string: forbiddenIfAsync,Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 38why, one step back38 uint8Array: forbiddenIfAsync,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 39why, one step back39 native() {},Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 40–43why, one step back40 },41};4243// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async modeFix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
line 44why, one step back44// When multiple input streams are used, we merge them to ensure the output stream ends only once each input stream has endedRefactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 45why, one step back45export const pipeOutputAsync = (subprocess, fileDescriptors, stdioState, controller) => {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 46why, one step back46 stdioState.subprocess = subprocess;Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
lines 47–48why, one step back47 const inputStreamsGroups = {};48Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 49–51why, one step back49 for (const {stdioItems, direction, fdNumber} of fileDescriptors) {50 for (const {stream} of stdioItems.filter(({type}) => type === 'generator')) {51 pipeGenerator(subprocess, stream, direction, fdNumber);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 52why, one step back52 }Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 53why, one step back53Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 54–55why, one step back54 for (const {stream} of stdioItems.filter(({type}) => type !== 'generator')) {55 pipeStdioItem({subprocess, stream, direction, fdNumber, inputStreamsGroups, controller});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 56why, one step back56 }Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 57why, one step back57 }Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
line 58why, one step back58Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 59why, one step back59 for (const [fdNumber, inputStreams] of Object.entries(inputStreamsGroups)) {Improve streams pipe logic (#885)ehmickybefore adoption5abc429
line 60why, one step back60 const inputStream = inputStreams.length === 1 ? inputStreams[0] : mergeStreams(inputStreams);Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 61why, one step back61 pipeStreams(inputStream, subprocess.stdio[fdNumber]);Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
line 62why, one step back62 }Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 63–64why, one step back63};64Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 65–66why, one step back65const pipeStdioItem = ({subprocess, stream, direction, fdNumber, inputStreamsGroups, controller}) => {66 if (stream === undefined) {Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 67–69why, one step back67 return;68 }69Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 70why, one step back70 setStandardStreamMaxListeners(stream, controller);Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
line 71why, one step back71Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 72why, one step back72 if (direction === 'output') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 73why, one step back73 pipeStreams(subprocess.stdio[fdNumber], stream);Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 74why, one step back74 } else {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 75why, one step back75 inputStreamsGroups[fdNumber] = [...(inputStreamsGroups[fdNumber] ?? []), stream];Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09
lines 76–77why, one step back76 }77};Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
line 78why, one step back78Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 79why, one step back79// Multiple subprocesses might be piping from/to `process.std*` at the same time.Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
lines 80–81why, one step back80// This is not necessarily an error and should not print a `maxListeners` warning.81const setStandardStreamMaxListeners = (stream, {signal}) => {Avoid code duplication with logic related to `maxListeners` (#831)ehmickybefore adoptioncd2a6c0
lines 82–83why, one step back82 if (isStandardStream(stream)) {83 incrementMaxListeners(stream, MAX_LISTENERS_INCREMENT, signal);Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
lines 84–89why, one step back84 }85};8687// `source.pipe(destination)` adds at most 1 listener for each event.88// If `stdin` option is an array, the values might be combined with `merge-streams`.89// That library also listens for `source` end, which adds 1 more listener.Avoid code duplication with logic related to `maxListeners` (#831)ehmickybefore adoptioncd2a6c0
line 90why, one step back90const MAX_LISTENERS_INCREMENT = 2;Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09
line 91why, one step back91Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 92–95why, one step back92// The stream error handling is performed by the piping logic above, which cannot be performed before subprocess spawning.93// If the subprocess spawning fails (e.g. due to an invalid command), the streams need to be manually destroyed.94// We need to create those streams before subprocess spawning, in case their creation fails, e.g. when passing an invalid generator as argument.95// Like this, an exception would be thrown, which would prevent spawning a subprocess.Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 96–101why, one step back96export const cleanupCustomStreams = fileDescriptors => {97 for (const {stdioItems} of fileDescriptors) {98 for (const {stream} of stdioItems) {99 if (stream !== undefined && !isStandardStream(stream)) {100 stream.destroy();101 }Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09
line 102why, one step back102 }Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 103–104why, one step back103 }104};