Skip to content
Myr

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 file

Take it

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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';

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 3why, one step back
3import {Readable, Writable} 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';

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

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';

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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 `stdio` options (#623)ehmickybefore adoption2d76897

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 `stdio` options (#623)ehmickybefore adoption2d76897

lines 18–19why, one step back
18const addPropertiesAsync = {
19 input: {

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 21–28why, one step back
21 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 back
29 },
30 output: {

Separate generator -> Duplex utility (#698)ehmickybefore adoption214b6ab

line 31why, one step back
31 generator: generatorToDuplexStream,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 32–35why, one step back
32 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 back
36 iterable: forbiddenIfAsync,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 39why, one step back
39 native() {},

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 40–43why, one step back
40 },
41};4243// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async mode

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

line 44why, one step back
44// 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 45why, one step back
45export const pipeOutputAsync = (subprocess, fileDescriptors, stdioState, controller) => {

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

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

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

lines 47–48why, one step back
47 const inputStreamsGroups = {};
48

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 49–51why, one step back
49 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 back
52 }

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

line 53why, one step back
53

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 54–55why, one step back
54 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 back
56 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 57why, one step back
57 }

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

line 58why, one step back
58

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

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

Improve streams pipe logic (#885)ehmickybefore adoption5abc429

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

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

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

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

line 62why, one step back
62 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 63–64why, one step back
63};
64

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 65–66why, one step back
65const pipeStdioItem = ({subprocess, stream, direction, fdNumber, inputStreamsGroups, controller}) => {
66 if (stream === undefined) {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 67–69why, one step back
67 return;
68 }69

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

line 71why, one step back
71

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

line 74why, one step back
74 } else {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

lines 76–77why, one step back
76 }
77};

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

line 78why, one step back
78

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

line 79why, one step back
79// 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 back
80// 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 back
82 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 back
84 }
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 back
90const MAX_LISTENERS_INCREMENT = 2;

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

line 91why, one step back
91

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

lines 92–95why, one step back
92// 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 back
96export 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 back
102 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 103–104why, one step back
103 }
104};