Skip to content
Myr

async.js

This file

Path

lib/stdio/async.js

72 lines · 2.8 KiB · JavaScript

Last changed by

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

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

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

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

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

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

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

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

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 11why, one step back
11

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 16–17why, one step back
16const addPropertiesAsync = {
17 input: {

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

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

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

lines 19–20why, one step back
19 fileUrl: ({value}) => ({value: createReadStream(value)}),
20 filePath: ({value}) => ({value: createReadStream(value.file)}),

Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623

lines 21–22why, one step back
21 webStream: ({value}) => ({value: Readable.fromWeb(value)}),
22 iterable: ({value}) => ({value: Readable.from(value)}),

Validate `input` option's type (#668)ehmickybefore adoption65742c6

lines 23–24why, one step back
23 string: ({value}) => ({value: Readable.from(value)}),
24 uint8Array: ({value}) => ({value: Readable.from(Buffer.from(value))}),

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 25–26why, one step back
25 },
26 output: {

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

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

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

lines 28–29why, one step back
28 fileUrl: ({value}) => ({value: createWriteStream(value)}),
29 filePath: ({value}) => ({value: createWriteStream(value.file)}),

Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623

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

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

lines 31–32why, one step back
31 iterable: forbiddenIfAsync,
32 uint8Array: forbiddenIfAsync,

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

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

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

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

line 38why, one step back
38export const pipeOutputAsync = (spawned, stdioStreamsGroups) => {

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

lines 39–40why, one step back
39 const inputStreamsGroups = {};
40

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

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

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

lines 42–46why, one step back
42 const generatorStreams = sortGeneratorStreams(stdioStreams.filter(({type}) => type === 'generator'));
43 const nonGeneratorStreams = stdioStreams.filter(({type}) => type !== 'generator');4445 for (const generatorStream of generatorStreams) {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

lines 48–51why, one step back
48
49 for (const nonGeneratorStream of nonGeneratorStreams) {50 pipeStdioOption(spawned, nonGeneratorStream, inputStreamsGroups);51 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 52why, one step back
52 }

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

lines 53–57why, one step back
53
54 for (const [index, inputStreams] of Object.entries(inputStreamsGroups)) {55 const value = inputStreams.length === 1 ? inputStreams[0] : mergeStreams(inputStreams);56 value.pipe(spawned.stdio[index]);57 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 58why, one step back
58};

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

lines 59–60why, one step back
59
60const sortGeneratorStreams = generatorStreams => generatorStreams[0]?.direction === 'input' ? generatorStreams.reverse() : generatorStreams;

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 61why, one step back
61

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

line 62why, one step back
62const pipeStdioOption = (spawned, {type, value, direction, index}, inputStreamsGroups) => {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 63–67why, one step back
63 if (type === 'native') {
64 return;65 }6667 if (direction === 'output') {

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

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

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

lines 69–70why, one step back
69 } else {
70 inputStreamsGroups[index] = [...(inputStreamsGroups[index] ?? []), value];

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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