direction.js
This file
Path
lib/stdio/direction.js
77 lines · 2.8 KiB · JavaScript
Last changed by
Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
History of this fileTake it
Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
line 1why, one step back1import process from 'node:process';Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 2–8why, one step back2import {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.Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
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.Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
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));Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
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.`);Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 18–19why, one step back18 }19Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 20why, one step back20 return directions.find(Boolean) ?? DEFAULT_DIRECTION;Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
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);Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 24–27why, one step back2425// `stdin`/`stdout`/`stderr` have a known direction26const KNOWN_DIRECTIONS = ['input', 'output', 'output'];27Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
lines 28–30why, one step back28const anyDirection = () => undefined;29const alwaysInput = () => 'input';30Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 31why, one step back31// `string` can only be added through the `input` option, i.e. does not need to be handled hereRefactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
line 32why, one step back32const guessStreamDirection = {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 33why, one step back33 generator: anyDirection,Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
lines 34–37why, one step back34 fileUrl: anyDirection,35 filePath: anyDirection,36 iterable: alwaysInput,37 uint8Array: alwaysInput,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 38–40why, one step back38 webStream: value => isWritableStream(value) ? 'output' : 'input',39 nodeStream(value) {40 const standardStreamDirection = getStandardStreamDirection(value);Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 41–42why, one step back41 if (standardStreamDirection !== undefined) {42 return standardStreamDirection;Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 43–44why, one step back43 }44Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 45why, one step back45 if (!isNodeReadableStream(value, {checkOpen: false})) {Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 46–48why, one step back46 return 'output';47 }48Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 49why, one step back49 return isNodeWritableStream(value, {checkOpen: false}) ? undefined : 'input';Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
line 50why, one step back50 },Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 51–52why, one step back51 native(value) {52 const standardStreamDirection = getStandardStreamDirection(value);Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 53–54why, one step back53 if (standardStreamDirection !== undefined) {54 return standardStreamDirection;Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 55–56why, one step back55 }56Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 57–58why, one step back57 if (isNodeStream(value, {checkOpen: false})) {58 return guessStreamDirection.nodeStream(value);Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 59–60why, one step back59 }60 },Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 61–62why, one step back61};62Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 63–64why, one step back63const getStandardStreamDirection = value => {64 if ([0, process.stdin].includes(value)) {Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 65–67why, one step back65 return 'input';66 }67Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 68why, one step back68 if ([1, 2, process.stdout, process.stderr].includes(value)) {Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
lines 69–70why, one step back69 return 'output';70 }Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 71–72why, one step back71};72Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c
lines 73–75why, one step back73// When ambiguous, we initially keep the direction as `undefined`.74// This allows arrays of `stdio` values to resolve the ambiguity.75// For example, `stdio[3]: DuplexStream` is ambiguous, but `stdio[3]: [DuplexStream, WritableStream]` is not.Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553
lines 76–77why, one step back76// When the ambiguity remains, we default to `output` since it is the most common use case for additional file descriptors.77const DEFAULT_DIRECTION = 'output';