Skip to content
Myr

async.js

This file

Path

lib/stdio/async.js

67 lines · 2.6 KiB · JavaScript

Last changed by

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

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 any type with transforms (#709)ehmickybefore adoptioned8b2ce

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

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

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

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

line 44why, one step back
44 }

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

line 45why, one step back
45

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

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

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

lines 47–48why, one step back
47 pipeStdioOption(spawned, nonGeneratorStream, inputStreamsGroups);
48 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 49why, one step back
49 }

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 55–56why, one step back
55};
56

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 58–62why, one step back
58 if (type === 'native') {
59 return;60 }6162 if (direction === 'output') {

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

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

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 66–67why, one step back
66 }
67};