direction.js
This file
Path
lib/stdio/direction.js
76 lines · 2.8 KiB · JavaScript
Last changed by
Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
History of this fileTake it
The walk stopped after 200 commits: lines older than db95b51 point at it.
Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 1–8why, one step back1import 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';8Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 9why, one step back9// For `stdio[fdNumber]` beyond stdin/stdout/stderr, we need to guess whether the value passed is intended for inputs or outputs.Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
line 10why, one step back10// This allows us to know whether to pipe _into_ or _from_ the stream.Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 11why, one step back11// When `stdio[fdNumber]` is a single value, this guess is fairly straightforward.Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
line 12why, one step back12// However, when it is an array instead, we also need to make sure the different values are not incompatible with each other.Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 13–14why, one step back13export const getStreamDirection = (stdioItems, fdNumber, optionName) => {14 const directions = stdioItems.map(stdioItem => getStdioItemDirection(stdioItem, fdNumber));Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 15–16why, one step back1516 if (directions.includes('input') && directions.includes('output')) {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 17why, one step back17 throw new TypeError(`The \`${optionName}\` option must not be an array of both readable and writable values.`);Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 18–19why, one step back18 }19Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 20why, one step back20 return directions.find(Boolean) ?? DEFAULT_DIRECTION;Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 21–22why, one step back21};22Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 23why, one step back23const getStdioItemDirection = ({type, value}, fdNumber) => KNOWN_DIRECTIONS[fdNumber] ?? guessStreamDirection[type](value);Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 24–33why, one step back2425// `stdin`/`stdout`/`stderr` have a known direction26const KNOWN_DIRECTIONS = ['input', 'output', 'output'];2728const anyDirection = () => undefined;29const alwaysInput = () => 'input';3031// `string` can only be added through the `input` option, i.e. does not need to be handled here32const guessStreamDirection = {33 generator: anyDirection,Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 34why, one step back34 asyncGenerator: anyDirection,Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 35–37why, one step back35 fileUrl: anyDirection,36 filePath: anyDirection,37 iterable: alwaysInput,Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
line 38why, one step back38 asyncIterable: alwaysInput,Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
line 39why, one step back39 uint8Array: alwaysInput,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 40–42why, one step back40 webStream: value => isWritableStream(value) ? 'output' : 'input',41 nodeStream(value) {42 if (!isNodeReadableStream(value, {checkOpen: false})) {Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 43–45why, one step back43 return 'output';44 }45Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 46why, one step back46 return isNodeWritableStream(value, {checkOpen: false}) ? undefined : 'input';Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
line 47why, one step back47 },Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6
line 48why, one step back48 webTransform: anyDirection,Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
line 49why, one step back49 duplex: anyDirection,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 50–51why, one step back50 native(value) {51 const standardStreamDirection = getStandardStreamDirection(value);Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 52–53why, one step back52 if (standardStreamDirection !== undefined) {53 return standardStreamDirection;Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 54–55why, one step back54 }55Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 56–57why, one step back56 if (isNodeStream(value, {checkOpen: false})) {57 return guessStreamDirection.nodeStream(value);Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 58–59why, one step back58 }59 },Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 60–61why, one step back60};61Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 62–63why, one step back62const getStandardStreamDirection = value => {63 if ([0, process.stdin].includes(value)) {Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 64–66why, one step back64 return 'input';65 }66Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 67why, one step back67 if ([1, 2, process.stdout, process.stderr].includes(value)) {Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 68–69why, one step back68 return 'output';69 }Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 70–76why, one step back70};7172// When ambiguous, we initially keep the direction as `undefined`.73// This allows arrays of `stdio` values to resolve the ambiguity.74// For example, `stdio[3]: DuplexStream` is ambiguous, but `stdio[3]: [DuplexStream, WritableStream]` is not.75// When the ambiguity remains, we default to `output` since it is the most common use case for additional file descriptors.76const DEFAULT_DIRECTION = 'output';