async.js
This file
Path
lib/stdio/async.js
115 lines · 4.8 KiB · JavaScript
Last changed by
Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
History of this fileTake it
The walk stopped after 200 commits: lines older than 76ff50b point at it.
Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
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';Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6
line 3why, one step back3import {Readable, Writable, Duplex} 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';Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
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';Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
line 9why, one step back9import {generatorToDuplexStream, pipeTransform, TRANSFORM_TYPES} from './generator.js';Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
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 `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
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 tests (#934)ehmickybefore adoption52c4bde
lines 18–19why, one step back18const addProperties = {19 generator: generatorToDuplexStream,Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 20why, one step back20 asyncGenerator: generatorToDuplexStream,Refactor tests (#934)ehmickybefore adoption52c4bde
line 21why, one step back21 nodeStream: ({value}) => ({stream: value}),Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6
lines 22–26why, one step back22 webTransform({value: {transform, writableObjectMode, readableObjectMode}}) {23 const objectMode = writableObjectMode || readableObjectMode;24 const stream = Duplex.fromWeb(transform, {objectMode});25 return {stream};26 },Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
line 27why, one step back27 duplex: ({value: {transform}}) => ({stream: transform}),Refactor tests (#934)ehmickybefore adoption52c4bde
lines 28–30why, one step back28 native() {},29};30Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 31–32why, one step back31const addPropertiesAsync = {32 input: {Refactor tests (#934)ehmickybefore adoption52c4bde
line 33why, one step back33 ...addProperties,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 34why, one step back34 fileUrl: ({value}) => ({stream: createReadStream(value)}),Refactor tests (#934)ehmickybefore adoption52c4bde
line 35why, one step back35 filePath: ({value: {file}}) => ({stream: createReadStream(file)}),Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 36–37why, one step back36 webStream: ({value}) => ({stream: Readable.fromWeb(value)}),37 iterable: ({value}) => ({stream: Readable.from(value)}),Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
line 38why, one step back38 asyncIterable: ({value}) => ({stream: Readable.from(value)}),Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 39–40why, one step back39 string: ({value}) => ({stream: Readable.from(value)}),40 uint8Array: ({value}) => ({stream: Readable.from(Buffer.from(value))}),Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 41–42why, one step back41 },42 output: {Refactor tests (#934)ehmickybefore adoption52c4bde
line 43why, one step back43 ...addProperties,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 44why, one step back44 fileUrl: ({value}) => ({stream: createWriteStream(value)}),Refactor tests (#934)ehmickybefore adoption52c4bde
line 45why, one step back45 filePath: ({value: {file}}) => ({stream: createWriteStream(file)}),Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 46why, one step back46 webStream: ({value}) => ({stream: Writable.fromWeb(value)}),Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 47why, one step back47 iterable: forbiddenIfAsync,Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
line 48why, one step back48 asyncIterable: forbiddenIfAsync,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 49why, one step back49 string: forbiddenIfAsync,Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 50why, one step back50 uint8Array: forbiddenIfAsync,Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 51–54why, one step back51 },52};5354// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async modeFix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
line 55why, one step back55// 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 56why, one step back56export const pipeOutputAsync = (subprocess, fileDescriptors, stdioState, controller) => {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 57why, one step back57 stdioState.subprocess = subprocess;Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
lines 58–59why, one step back58 const inputStreamsGroups = {};59Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 60why, one step back60 for (const {stdioItems, direction, fdNumber} of fileDescriptors) {Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
line 61why, one step back61 for (const {stream} of stdioItems.filter(({type}) => TRANSFORM_TYPES.has(type))) {Refactor tests (#934)ehmickybefore adoption52c4bde
line 62why, one step back62 pipeTransform(subprocess, stream, direction, fdNumber);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 63why, one step back63 }Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 64why, one step back64Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
line 65why, one step back65 for (const {stream} of stdioItems.filter(({type}) => !TRANSFORM_TYPES.has(type))) {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 66why, one step back66 pipeStdioItem({subprocess, stream, direction, fdNumber, inputStreamsGroups, controller});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 67why, one step back67 }Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
line 68why, one step back68 }Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
line 69why, one step back69Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 70why, one step back70 for (const [fdNumber, inputStreams] of Object.entries(inputStreamsGroups)) {Improve streams pipe logic (#885)ehmickybefore adoption5abc429
line 71why, one step back71 const inputStream = inputStreams.length === 1 ? inputStreams[0] : mergeStreams(inputStreams);Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 72why, one step back72 pipeStreams(inputStream, subprocess.stdio[fdNumber]);Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca
line 73why, one step back73 }Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 74–75why, one step back74};75Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 76–77why, one step back76const pipeStdioItem = ({subprocess, stream, direction, fdNumber, inputStreamsGroups, controller}) => {77 if (stream === undefined) {Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 78–80why, one step back78 return;79 }80Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 81why, one step back81 setStandardStreamMaxListeners(stream, controller);Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
line 82why, one step back82Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
line 83why, one step back83 if (direction === 'output') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 84why, one step back84 pipeStreams(subprocess.stdio[fdNumber], stream);Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 85why, one step back85 } else {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 86why, one step back86 inputStreamsGroups[fdNumber] = [...(inputStreamsGroups[fdNumber] ?? []), stream];Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09
lines 87–88why, one step back87 }88};Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
line 89why, one step back89Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 90why, one step back90// 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 91–92why, one step back91// This is not necessarily an error and should not print a `maxListeners` warning.92const setStandardStreamMaxListeners = (stream, {signal}) => {Avoid code duplication with logic related to `maxListeners` (#831)ehmickybefore adoptioncd2a6c0
lines 93–94why, one step back93 if (isStandardStream(stream)) {94 incrementMaxListeners(stream, MAX_LISTENERS_INCREMENT, signal);Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc
lines 95–100why, one step back95 }96};9798// `source.pipe(destination)` adds at most 1 listener for each event.99// If `stdin` option is an array, the values might be combined with `merge-streams`.100// That library also listens for `source` end, which adds 1 more listener.Avoid code duplication with logic related to `maxListeners` (#831)ehmickybefore adoptioncd2a6c0
line 101why, one step back101const MAX_LISTENERS_INCREMENT = 2;Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09
line 102why, one step back102Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
lines 103–106why, one step back103// The stream error handling is performed by the piping logic above, which cannot be performed before subprocess spawning.104// If the subprocess spawning fails (e.g. due to an invalid command), the streams need to be manually destroyed.105// We need to create those streams before subprocess spawning, in case their creation fails, e.g. when passing an invalid generator as argument.106// Like this, an exception would be thrown, which would prevent spawning a subprocess.Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 107–112why, one step back107export const cleanupCustomStreams = fileDescriptors => {108 for (const {stdioItems} of fileDescriptors) {109 for (const {stream} of stdioItems) {110 if (stream !== undefined && !isStandardStream(stream)) {111 stream.destroy();112 }Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09
line 113why, one step back113 }Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b
lines 114–115why, one step back114 }115};