Skip to content
Myr

async.js

This file

Path

lib/stdio/async.js

46 lines · 1.4 KiB · JavaScript

Last changed by

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

History of this file

Take it

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 1–2why, one step back
1import {createReadStream, createWriteStream} from 'node:fs';
2import {Readable, Writable} from 'node:stream';

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 4–9why, one step back
4
5// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async mode6export const handleInputAsync = options => handleInput(addPropertiesAsync, options);78const addPropertiesAsync = {9 input: {

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 10–12why, one step back
10 filePath: ({value}) => ({value: createReadStream(value), autoDestroy: true}),
11 webStream: ({value}) => ({value: Readable.fromWeb(value), autoDestroy: true}),12 iterable: ({value}) => ({value: Readable.from(value), autoDestroy: true}),

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 13–14why, one step back
13 },
14 output: {

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 15–16why, one step back
15 filePath: ({value}) => ({value: createWriteStream(value), autoDestroy: true}),
16 webStream: ({value}) => ({value: Writable.fromWeb(value), autoDestroy: true}),

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 17–24why, one step back
17 iterable({optionName}) {
18 throw new TypeError(`The \`${optionName}\` option cannot be an iterable.`);19 },20 },21};2223// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async mode24export const pipeOutputAsync = (spawned, stdioStreams) => {

Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553

lines 25–26why, one step back
25 for (const stdioStream of stdioStreams) {
26 pipeStdioOption(spawned.stdio[stdioStream.index], stdioStream);

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 27–46why, one step back
27 }
28};2930const pipeStdioOption = (childStream, {type, value, direction}) => {31 if (type === 'native') {32 return;33 }3435 if (direction === 'output') {36 childStream.pipe(value);37 return;38 }3940 if (type === 'stringOrBuffer') {41 childStream.end(value);42 return;43 }4445 value.pipe(childStream);46};