native.js
This file
Path
lib/stdio/native.js
106 lines · 3.6 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 d69fd23 point at it.
Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
lines 1–2why, one step back1import {readFileSync} from 'node:fs';2import tty from 'node:tty';Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
line 3why, one step back3import {isStream as isNodeStream} from 'is-stream';Split files (#837)ehmickybefore adoption672c2d7
line 4why, one step back4import {STANDARD_STREAMS} from '../utils.js';Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
line 5why, one step back5import {bufferToUint8Array} from './uint-array.js';Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
lines 6–11why, one step back67// When we use multiple `stdio` values for the same streams, we pass 'pipe' to `child_process.spawn()`.8// We then emulate the piping done by core Node.js.9// To do so, we transform the following values:10// - Node.js streams are marked as `type: nodeStream`11// - 'inherit' becomes `process.stdin|stdout|stderr`Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 12why, one step back12// - any file descriptor integer becomes `process.stdio[fdNumber]`Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
line 13why, one step back13// All of the above transformations tell Execa to perform manual piping.Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
lines 14–15why, one step back14export const handleNativeStream = ({stdioItem, stdioItem: {type}, isStdioArray, fdNumber, direction, isSync}) => {15 if (!isStdioArray || type !== 'native') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 16why, one step back16 return stdioItem;Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
lines 17–18why, one step back17 }18Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
lines 19–77why, one step back19 return isSync20 ? handleNativeStreamSync({stdioItem, fdNumber, direction})21 : handleNativeStreamAsync({stdioItem, fdNumber});22};2324const handleNativeStreamSync = ({stdioItem, stdioItem: {value, optionName}, fdNumber, direction}) => {25 const targetFd = getTargetFd({value, optionName, fdNumber, direction});26 if (targetFd !== undefined) {27 return targetFd;28 }2930 if (isNodeStream(value, {checkOpen: false})) {31 throw new TypeError(`The \`${optionName}: Stream\` option cannot both be an array and include a stream with synchronous methods.`);32 }3334 return stdioItem;35};3637const getTargetFd = ({value, optionName, fdNumber, direction}) => {38 const targetFdNumber = getTargetFdNumber(value, fdNumber);39 if (targetFdNumber === undefined) {40 return;41 }4243 if (direction === 'output') {44 return {type: 'fileNumber', value: targetFdNumber, optionName};45 }4647 if (tty.isatty(targetFdNumber)) {48 throw new TypeError(`The \`${optionName}: ${serializeOptionValue(value)}\` option is invalid: it cannot be a TTY with synchronous methods.`);49 }5051 return {type: 'uint8Array', value: bufferToUint8Array(readFileSync(targetFdNumber)), optionName};52};5354const getTargetFdNumber = (value, fdNumber) => {55 if (value === 'inherit') {56 return fdNumber;57 }5859 if (typeof value === 'number') {60 return value;61 }6263 const standardStreamIndex = STANDARD_STREAMS.indexOf(value);64 if (standardStreamIndex !== -1) {65 return standardStreamIndex;66 }67};6869export const serializeOptionValue = value => {70 if (typeof value === 'string') {71 return `'${value}'`;72 }7374 return typeof value === 'number' ? `${value}` : 'Stream';75};7677const handleNativeStreamAsync = ({stdioItem, stdioItem: {value, optionName}, fdNumber}) => {Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
line 78why, one step back78 if (value === 'inherit') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 79why, one step back79 return {type: 'nodeStream', value: getStandardStream(fdNumber, value, optionName), optionName};Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
lines 80–82why, one step back80 }8182 if (typeof value === 'number') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 83why, one step back83 return {type: 'nodeStream', value: getStandardStream(value, value, optionName), optionName};Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
lines 84–85why, one step back84 }85Upgrade `is-stream` to `4.0.1` (#835)ehmickybefore adoption9cd62aa
line 86why, one step back86 if (isNodeStream(value, {checkOpen: false})) {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 87why, one step back87 return {type: 'nodeStream', value, optionName};Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
lines 88–89why, one step back88 }89Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 90why, one step back90 return stdioItem;Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
lines 91–97why, one step back91};9293// Node.js does not allow to easily retrieve file descriptors beyond stdin/stdout/stderr as streams.94// - `fs.createReadStream()`/`fs.createWriteStream()` with the `fd` option do not work with character devices that use blocking reads/writes (such as interactive TTYs).95// - Using a TCP `Socket` would work but be rather complex to implement.96// Since this is an edge case, we simply throw an error message.97// See https://github.com/sindresorhus/execa/pull/643#discussion_r1435905707Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
lines 98–99why, one step back98const getStandardStream = (fdNumber, value, optionName) => {99 const standardStream = STANDARD_STREAMS[fdNumber];Refactor execa promise to use `async/await` (#653)ehmickybefore adoptiond69fd23
lines 100–106why, one step back100101 if (standardStream === undefined) {102 throw new TypeError(`The \`${optionName}: ${value}\` option is invalid: no such standard stream.`);103 }104105 return standardStream;106};