Skip to content
Myr

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 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,

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

lines 21–22why, one step back
21 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 back
23 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 back
25 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 back
27 },
28 output: {

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

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

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

lines 30–31why, one step back
30 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 back
32 webStream: ({value}) => ({value: Writable.fromWeb(value)}),

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

lines 33–34why, one step back
33 iterable: forbiddenIfAsync,
34 uint8Array: forbiddenIfAsync,

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 35–38why, one step back
35 },
36};3738// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async mode

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

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

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

lines 40–41why, one step back
40export const pipeOutputAsync = (spawned, stdioStreamsGroups, stdioState, controller) => {
41 stdioState.spawned = spawned;

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

lines 42–43why, one step back
42 const inputStreamsGroups = {};
43

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

line 44why, one step back
44 for (const stdioStreams of stdioStreamsGroups) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 45why, one step back
45 for (const generatorStream of stdioStreams.filter(({type}) => type === 'generator')) {

Fix error handling of `stdin` (#787)ehmickybefore adoption4328670

line 46why, one step back
46 pipeGenerator(spawned, generatorStream);

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

line 47why, one step back
47 }

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

line 48why, one step back
48

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 49why, one step back
49 for (const nonGeneratorStream of stdioStreams.filter(({type}) => type !== 'generator')) {

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

line 50why, one step back
50 pipeStdioOption(spawned, nonGeneratorStream, inputStreamsGroups, controller);

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

line 51why, one step back
51 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 52why, one step back
52 }

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

line 53why, one step back
53

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

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

Improve streams pipe logic (#885)ehmickybefore adoption5abc429

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 58–59why, one step back
58};
59

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

line 60why, one step back
60const pipeStdioOption = (spawned, {type, value, direction, fdNumber}, inputStreamsGroups, controller) => {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 61–64why, one step back
61 if (type === 'native') {
62 return;63 }64

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

lines 65–66why, one step back
65 setStandardStreamMaxListeners(value, controller);
66

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Improve streams pipe logic (#885)ehmickybefore adoption5abc429

line 68why, one step back
68 pipeStreams(spawned.stdio[fdNumber], value);

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

line 69why, one step back
69 } else {

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

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

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

lines 71–72why, one step back
71 }
72};

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

lines 73–76why, one step back
73
74// 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 back
77 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 back
79 }
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 back
85const MAX_LISTENERS_INCREMENT = 2;

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

lines 86–95why, one step back
86
87// 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 back
96 }
97};