async.js
This file
Path
lib/stdio/async.js
97 lines · 4.1 KiB · JavaScript
Last changed by
Improve streams pipe logic (#885)ehmickybefore adoption5abc429
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,Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
lines 21–22why, one step back21 fileUrl: ({value}) => ({value: createReadStream(value)}),22 filePath: ({value}) => ({value: createReadStream(value.file)}),Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623
lines 23–24why, one step back23 webStream: ({value}) => ({value: Readable.fromWeb(value)}),24 iterable: ({value}) => ({value: Readable.from(value)}),Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 25–26why, one step back25 string: ({value}) => ({value: Readable.from(value)}),26 uint8Array: ({value}) => ({value: Readable.from(Buffer.from(value))}),Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 27–28why, one step back27 },28 output: {Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab
line 29why, one step back29 generator: generatorToDuplexStream,Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
lines 30–31why, one step back30 fileUrl: ({value}) => ({value: createWriteStream(value)}),31 filePath: ({value}) => ({value: createWriteStream(value.file)}),Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623
line 32why, one step back32 webStream: ({value}) => ({value: Writable.fromWeb(value)}),Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
lines 33–34why, one step back33 iterable: forbiddenIfAsync,34 uint8Array: forbiddenIfAsync,Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 35–38why, one step back35 },36};3738// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async modeFix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
line 39why, one step back39// When multiple input streams are used, we merge them to ensure the output stream ends only once each input stream has endedPrint output with `verbose` option (#884)ehmickybefore adoption6fda30f
lines 40–41why, one step back40export const pipeOutputAsync = (spawned, stdioStreamsGroups, stdioState, controller) => {41 stdioState.spawned = spawned;Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
lines 42–43why, one step back42 const inputStreamsGroups = {};43Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 44why, one step back44 for (const stdioStreams of stdioStreamsGroups) {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 45why, one step back45 for (const generatorStream of stdioStreams.filter(({type}) => type === 'generator')) {Fix error handling of `stdin` (#787)ehmickybefore adoption4328670
line 46why, one step back46 pipeGenerator(spawned, generatorStream);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 47why, one step back47 }Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 48why, one step back48Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 49why, one step back49 for (const nonGeneratorStream of stdioStreams.filter(({type}) => type !== 'generator')) {Improve stream cleanup and termination logic (#775)ehmickybefore adoption5aa90bd
line 50why, one step back50 pipeStdioOption(spawned, nonGeneratorStream, inputStreamsGroups, controller);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 51why, one step back51 }Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 52why, one step back52 }Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
line 53why, one step back53Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 54why, one step back54 for (const [fdNumber, inputStreams] of Object.entries(inputStreamsGroups)) {Improve streams pipe logic (#885)ehmickybefore adoption5abc429
lines 55–56why, one step back55 const inputStream = inputStreams.length === 1 ? inputStreams[0] : mergeStreams(inputStreams);56 pipeStreams(inputStream, spawned.stdio[fdNumber]);Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
line 57why, one step back57 }Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 58–59why, one step back58};59Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 60why, one step back60const pipeStdioOption = (spawned, {type, value, direction, fdNumber}, inputStreamsGroups, controller) => {Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 61–64why, one step back61 if (type === 'native') {62 return;63 }64Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
lines 65–66why, one step back65 setStandardStreamMaxListeners(value, controller);66Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 67why, one step back67 if (direction === 'output') {Improve streams pipe logic (#885)ehmickybefore adoption5abc429
line 68why, one step back68 pipeStreams(spawned.stdio[fdNumber], value);Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 69why, one step back69 } else {Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 70why, one step back70 inputStreamsGroups[fdNumber] = [...(inputStreamsGroups[fdNumber] ?? []), value];Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09
lines 71–72why, one step back71 }72};Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
lines 73–76why, one step back7374// Multiple processes might be piping from/to `process.std*` at the same time.75// This is not necessarily an error and should not print a `maxListeners` warning.76const setStandardStreamMaxListeners = (stream, {signal}) => {Avoid code duplication with logic related to `maxListeners` (#831)ehmickybefore adoptioncd2a6c0
lines 77–78why, one step back77 if (isStandardStream(stream)) {78 incrementMaxListeners(stream, MAX_LISTENERS_INCREMENT, signal);Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
lines 79–84why, one step back79 }80};8182// `source.pipe(destination)` adds at most 1 listener for each event.83// If `stdin` option is an array, the values might be combined with `merge-streams`.84// That library also listens for `source` end, which adds 1 more listener.Avoid code duplication with logic related to `maxListeners` (#831)ehmickybefore adoptioncd2a6c0
line 85why, one step back85const MAX_LISTENERS_INCREMENT = 2;Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09
lines 86–95why, one step back8687// The stream error handling is performed by the piping logic above, which cannot be performed before process spawning.88// If the process spawning fails (e.g. due to an invalid command), the streams need to be manually destroyed.89// We need to create those streams before process spawning, in case their creation fails, e.g. when passing an invalid generator as argument.90// Like this, an exception would be thrown, which would prevent spawning a process.91export const cleanupStdioStreams = stdioStreamsGroups => {92 for (const {value, type} of stdioStreamsGroups.flat()) {93 if (type !== 'native' && !isStandardStream(value)) {94 value.destroy();95 }Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 96–97why, one step back96 }97};