Skip to content
Myr

direction.js

This file

Path

lib/stdio/direction.js

120 lines · 5.1 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–12why, one step back
1import process from 'node:process';
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[fdNumber]` 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.11// When `stdio[fdNumber]` 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.

TweaksSindre Sorhusbefore adoption698a1b8

lines 13–14why, one step back
13export const getStreamDirection = ({stdioItems, isStdioArray, fdNumber, optionName}) => {
14 const directions = stdioItems.map(stdioItem => getStdioItemDirection(stdioItem, fdNumber, isStdioArray));

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 15–22why, one step back
15
16 if (directions.includes('input') && directions.includes('output')) {17 throw new TypeError(`The \`${optionName}\` option must not be an array of both readable and writable values.`);18 }1920 return directions.find(Boolean) ?? DEFAULT_DIRECTION;21};22

TweaksSindre Sorhusbefore adoption698a1b8

line 23why, one step back
23const getStdioItemDirection = (stdioItem, fdNumber, isStdioArray) => {

Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387

lines 24–29why, one step back
24 const knownDirection = KNOWN_DIRECTIONS[fdNumber];
25 const direction = getRequestedDirection(stdioItem);26 if (knownDirection === undefined) {27 return direction;28 }29

TweaksSindre Sorhusbefore adoption698a1b8

line 30why, one step back
30 validateKnownDirection(stdioItem, knownDirection, isStdioArray);

Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387

lines 31–33why, one step back
31 return knownDirection;
32};33

Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4

lines 34–35why, one step back
34// `direction` is set when the user explicitly requests it, e.g. `{value: 'pipe', input: true}`.
35// It takes precedence over guessing, but not over the fixed direction of `stdin`/`stdout`/`stderr`.

Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba

lines 36–45why, one step back
36// The explicitly requested `direction` must not contradict a value that is intrinsically the other direction.
37// For example, `{value: writableStream, input: true}` is invalid since a writable value can only be an output.38const getRequestedDirection = ({type, value, direction, optionName}) => {39 const guessedDirection = guessStreamDirection[type](value);40 if (direction === 'input' && guessedDirection === 'output') {41 throw new TypeError(`The \`${optionName}\` option is invalid: \`input: true\` cannot be used with a writable value, which is always an output.`);42 }4344 return direction ?? guessedDirection;45};

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 46why, one step back
46

Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387

lines 47–50why, one step back
47// `stdin`/`stdout`/`stderr` have a fixed direction, which each value must not contradict.
48// Otherwise, asynchronous methods pipe in the wrong direction and crash, e.g. with `dest.end is not a function`.49// Only values whose direction is intrinsic to a stream are checked: other values (e.g. `Uint8Array`) are already50// rejected in the wrong direction when their stream properties are added.

TweaksSindre Sorhusbefore adoption698a1b8

lines 51–53why, one step back
51// A single file descriptor 0, 1 or 2, e.g. `stdin: 1`, is passed as is to `child_process.spawn()` instead of being piped by Execa, so it is not checked. Those are often the same terminal, which is both readable and writable.
52const validateKnownDirection = ({type, value, optionName}, knownDirection, isStdioArray) => {53 if (!STREAM_TYPES.has(type) || (!isStdioArray && getStandardStreamDirection(value) !== undefined)) {

Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387

lines 54–57why, one step back
54 return;
55 }5657 const guessedDirection = guessStreamDirection[type](value);

TweaksSindre Sorhusbefore adoption698a1b8

lines 58–59why, one step back
58 if (guessedDirection === undefined || guessedDirection === knownDirection) {
59 return;

Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387

line 60why, one step back
60 }

TweaksSindre Sorhusbefore adoption698a1b8

lines 61–63why, one step back
61
62 const valueName = guessedDirection === 'input' ? 'readable' : 'writable';63 throw new TypeError(`The \`${optionName}\` option is invalid: a ${valueName} value is always an ${guessedDirection}, but \`${optionName}\` is an ${knownDirection}.`);

Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387

lines 64–67why, one step back
64};
6566const STREAM_TYPES = new Set(['native', 'nodeStream', 'webStream']);67

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 68–73why, one step back
68// `stdin`/`stdout`/`stderr` have a known direction
69const KNOWN_DIRECTIONS = ['input', 'output', 'output'];7071const anyDirection = () => undefined;72const alwaysInput = () => 'input';73

Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387

line 74why, one step back
74// `string` can only be added through the `input` option, i.e. on `stdin`, so it is always an input

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 75–82why, one step back
75const guessStreamDirection = {
76 generator: anyDirection,77 asyncGenerator: anyDirection,78 fileUrl: anyDirection,79 filePath: anyDirection,80 iterable: alwaysInput,81 asyncIterable: alwaysInput,82 uint8Array: alwaysInput,

Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387

line 83why, one step back
83 string: alwaysInput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 84–120why, one step back
84 webStream: value => isWritableStream(value) ? 'output' : 'input',
85 nodeStream(value) {86 if (!isNodeReadableStream(value, {checkOpen: false})) {87 return 'output';88 }8990 return isNodeWritableStream(value, {checkOpen: false}) ? undefined : 'input';91 },92 webTransform: anyDirection,93 duplex: anyDirection,94 native(value) {95 const standardStreamDirection = getStandardStreamDirection(value);96 if (standardStreamDirection !== undefined) {97 return standardStreamDirection;98 }99100 if (isNodeStream(value, {checkOpen: false})) {101 return guessStreamDirection.nodeStream(value);102 }103 },104};105106const getStandardStreamDirection = value => {107 if ([0, process.stdin].includes(value)) {108 return 'input';109 }110111 if ([1, 2, process.stdout, process.stderr].includes(value)) {112 return 'output';113 }114};115116// When ambiguous, we initially keep the direction as `undefined`.117// This allows arrays of `stdio` values to resolve the ambiguity.118// For example, `stdio[3]: DuplexStream` is ambiguous, but `stdio[3]: [DuplexStream, WritableStream]` is not.119// When the ambiguity remains, we default to `output` since it is the most common use case for additional file descriptors.120const DEFAULT_DIRECTION = 'output';