Skip to content
Myr

direction.js

This file

Path

lib/stdio/direction.js

68 lines · 2.7 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

line 1why, one step back
1import process from 'node:process';

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

lines 2–10why, one step back
2import {
3 isStream as isNodeStream,4 isReadableStream as isNodeReadableStream,5 isWritableStream as isNodeWritableStream,6} from 'is-stream';7import {isWritableStream} from './type.js';89// For `stdio[index]` beyond stdin/stdout/stderr, we need to guess whether the value passed is intended for inputs or outputs.10// This allows us to know whether to pipe _into_ or _from_ the stream.

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

lines 11–12why, one step back
11// When `stdio[index]` is a single value, this guess is fairly straightforward.
12// However, when it is an array instead, we also need to make sure the different values are not incompatible with each other.

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

lines 13–14why, one step back
13export const addStreamDirection = stdioStreams => {
14 const directions = stdioStreams.map(stdioStream => getStreamDirection(stdioStream));

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

lines 15–16why, one step back
15
16 if (directions.includes('input') && directions.includes('output')) {

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 17why, one step back
17 throw new TypeError(`The \`${stdioStreams[0].optionName}\` option must not be an array of both readable and writable values.`);

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

lines 18–20why, one step back
18 }
1920 const direction = directions.find(Boolean);

Simplify `stdio` logic (#663)ehmickybefore adoption66d0efb

line 21why, one step back
21 return stdioStreams.map(stdioStream => addDirection(stdioStream, direction));

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

lines 22–23why, one step back
22};
23

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

lines 24–28why, one step back
24const getStreamDirection = stdioStream => KNOWN_DIRECTIONS[stdioStream.index] ?? guessStreamDirection[stdioStream.type](stdioStream.value);
2526// `stdin`/`stdout`/`stderr` have a known direction27const KNOWN_DIRECTIONS = ['input', 'output', 'output'];28

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

lines 29–31why, one step back
29const anyDirection = () => undefined;
30const alwaysInput = () => 'input';31

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

line 32why, one step back
32// `string` can only be added through the `input` option, i.e. does not need to be handled here

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

line 33why, one step back
33const guessStreamDirection = {

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

line 34why, one step back
34 generator: anyDirection,

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

lines 35–38why, one step back
35 fileUrl: anyDirection,
36 filePath: anyDirection,37 iterable: alwaysInput,38 uint8Array: alwaysInput,

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

lines 39–47why, one step back
39 webStream: stdioOption => isWritableStream(stdioOption) ? 'output' : 'input',
40 nodeStream(stdioOption) {41 if (isNodeReadableStream(stdioOption)) {42 return isNodeWritableStream(stdioOption) ? undefined : 'input';43 }4445 return 'output';46 },47 native(stdioOption) {

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

lines 48–55why, one step back
48 if ([0, process.stdin].includes(stdioOption)) {
49 return 'input';50 }5152 if ([1, 2, process.stdout, process.stderr].includes(stdioOption)) {53 return 'output';54 }55

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

lines 56–63why, one step back
56 if (isNodeStream(stdioOption)) {
57 return guessStreamDirection.nodeStream(stdioOption);58 }59 },60};6162const addDirection = (stdioStream, direction = DEFAULT_DIRECTION) => ({...stdioStream, direction});63

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

lines 64–66why, one step back
64// When ambiguous, we initially keep the direction as `undefined`.
65// This allows arrays of `stdio` values to resolve the ambiguity.66// For example, `stdio[3]: DuplexStream` is ambiguous, but `stdio[3]: [DuplexStream, WritableStream]` is not.

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

lines 67–68why, one step back
67// When the ambiguity remains, we default to `output` since it is the most common use case for additional file descriptors.
68const DEFAULT_DIRECTION = 'output';