Skip to content
Myr

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 file

Take 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 back
1import {createReadStream, createWriteStream} from 'node:fs';

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 2why, one step back
2import {Buffer} from 'node:buffer';

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 3why, one step back
3import {Readable, Writable, Duplex} from 'node:stream';

Improve streams pipe logic (#885)ehmickybefore adoption5abc429

line 4why, one step back
4import mergeStreams from '@sindresorhus/merge-streams';

Split files (#837)ehmickybefore adoption672c2d7

line 5why, one step back
5import {isStandardStream, incrementMaxListeners} from '../utils.js';

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 6why, one step back
6import {handleInput} from './handle.js';

Improve stream cleanup and termination logic (#775)ehmickybefore adoption5aa90bd

line 7why, one step back
7import {pipeStreams} from './pipeline.js';

Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4

line 8why, one step back
8import {TYPE_TO_MESSAGE} from './type.js';

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 9why, one step back
9import {generatorToDuplexStream, pipeTransform, TRANSFORM_TYPES} from './generator.js';

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

lines 10–11why, one step back
10
11// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async mode

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

line 12why, one step back
12export const handleInputAsync = (options, verboseInfo) => handleInput(addPropertiesAsync, options, verboseInfo, false);

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 13why, one step back
13

Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4

lines 14–17why, one step back
14const forbiddenIfAsync = ({type, optionName}) => {
15 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]}.`);16};17

Refactor tests (#934)ehmickybefore adoption52c4bde

lines 18–19why, one step back
18const addProperties = {
19 generator: generatorToDuplexStream,

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 20why, one step back
20 asyncGenerator: generatorToDuplexStream,

Refactor tests (#934)ehmickybefore adoption52c4bde

line 21why, one step back
21 nodeStream: ({value}) => ({stream: value}),

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

lines 22–26why, one step back
22 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 back
27 duplex: ({value: {transform}}) => ({stream: transform}),

Refactor tests (#934)ehmickybefore adoption52c4bde

lines 28–30why, one step back
28 native() {},
29};30

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

lines 31–32why, one step back
31const addPropertiesAsync = {
32 input: {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 33why, one step back
33 ...addProperties,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 34why, one step back
34 fileUrl: ({value}) => ({stream: createReadStream(value)}),

Refactor tests (#934)ehmickybefore adoption52c4bde

line 35why, one step back
35 filePath: ({value: {file}}) => ({stream: createReadStream(file)}),

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 36–37why, one step back
36 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 back
38 asyncIterable: ({value}) => ({stream: Readable.from(value)}),

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 39–40why, one step back
39 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 back
41 },
42 output: {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 43why, one step back
43 ...addProperties,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 44why, one step back
44 fileUrl: ({value}) => ({stream: createWriteStream(value)}),

Refactor tests (#934)ehmickybefore adoption52c4bde

line 45why, one step back
45 filePath: ({value: {file}}) => ({stream: createWriteStream(file)}),

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 46why, one step back
46 webStream: ({value}) => ({stream: Writable.fromWeb(value)}),

Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4

line 47why, one step back
47 iterable: forbiddenIfAsync,

Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d

line 48why, one step back
48 asyncIterable: forbiddenIfAsync,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 49why, one step back
49 string: forbiddenIfAsync,

Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4

line 50why, one step back
50 uint8Array: forbiddenIfAsync,

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

lines 51–54why, one step back
51 },
52};5354// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async mode

Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca

line 55why, one step back
55// When multiple input streams are used, we merge them to ensure the output stream ends only once each input stream has ended

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 56why, one step back
56export const pipeOutputAsync = (subprocess, fileDescriptors, stdioState, controller) => {

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

line 57why, one step back
57 stdioState.subprocess = subprocess;

Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca

lines 58–59why, one step back
58 const inputStreamsGroups = {};
59

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 60why, one step back
60 for (const {stdioItems, direction, fdNumber} of fileDescriptors) {

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 61why, one step back
61 for (const {stream} of stdioItems.filter(({type}) => TRANSFORM_TYPES.has(type))) {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 62why, one step back
62 pipeTransform(subprocess, stream, direction, fdNumber);

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 63why, one step back
63 }

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 64why, one step back
64

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 65why, one step back
65 for (const {stream} of stdioItems.filter(({type}) => !TRANSFORM_TYPES.has(type))) {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 66why, one step back
66 pipeStdioItem({subprocess, stream, direction, fdNumber, inputStreamsGroups, controller});

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 67why, one step back
67 }

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 68why, one step back
68 }

Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca

line 69why, one step back
69

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 70why, one step back
70 for (const [fdNumber, inputStreams] of Object.entries(inputStreamsGroups)) {

Improve streams pipe logic (#885)ehmickybefore adoption5abc429

line 71why, one step back
71 const inputStream = inputStreams.length === 1 ? inputStreams[0] : mergeStreams(inputStreams);

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

line 72why, one step back
72 pipeStreams(inputStream, subprocess.stdio[fdNumber]);

Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca

line 73why, one step back
73 }

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

lines 74–75why, one step back
74};
75

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 76–77why, one step back
76const 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 back
78 return;
79 }80

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 81why, one step back
81 setStandardStreamMaxListeners(stream, controller);

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

line 82why, one step back
82

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

line 83why, one step back
83 if (direction === 'output') {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 84why, one step back
84 pipeStreams(subprocess.stdio[fdNumber], stream);

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 85why, one step back
85 } else {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 86why, one step back
86 inputStreamsGroups[fdNumber] = [...(inputStreamsGroups[fdNumber] ?? []), stream];

Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09

lines 87–88why, one step back
87 }
88};

Fix `maxListeners` warning when running multiple processes in parallel (#785)ehmickybefore adoptiond50c4cc

line 89why, one step back
89

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

line 90why, one step back
90// 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 back
91// 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 back
93 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 back
95 }
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 back
101const MAX_LISTENERS_INCREMENT = 2;

Destroy internal streams when process exits early (#770)ehmickybefore adoption8db0b09

line 102why, one step back
102

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

lines 103–106why, one step back
103// 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 back
107export 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 back
113 }

Refactor `timeout` option to use `async/await` (#650)ehmickybefore adoption76ff50b

lines 114–115why, one step back
114 }
115};