native.js
This file
Path
lib/stdio/native.js
103 lines · 3.8 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–58why, one step back1import {readFileSync} from 'node:fs';2import tty from 'node:tty';3import {isStream as isNodeStream} from 'is-stream';4import {STANDARD_STREAMS} from '../utils/standard-stream.js';5import {bufferToUint8Array} from '../utils/uint-array.js';6import {serializeOptionValue} from '../arguments/fd-options.js';78// When we use multiple `stdio` values for the same streams, we pass 'pipe' to `child_process.spawn()`.9// We then emulate the piping done by core Node.js.10// To do so, we transform the following values:11// - Node.js streams are marked as `type: nodeStream`12// - 'inherit' becomes `process.stdin|stdout|stderr`13// - any file descriptor integer becomes `process.stdio[fdNumber]`14// All of the above transformations tell Execa to perform manual piping.15export const handleNativeStream = ({stdioItem, stdioItem: {type}, isStdioArray, fdNumber, direction, isSync}) => {16 if (!isStdioArray || type !== 'native') {17 return stdioItem;18 }1920 return isSync21 ? handleNativeStreamSync({stdioItem, fdNumber, direction})22 : handleNativeStreamAsync({stdioItem, fdNumber});23};2425// Synchronous methods use a different logic.26// 'inherit', file descriptors and process.std* are handled by readFileSync()/writeFileSync().27const handleNativeStreamSync = ({stdioItem, stdioItem: {value, optionName}, fdNumber, direction}) => {28 const targetFd = getTargetFd({29 value,30 optionName,31 fdNumber,32 direction,33 });34 if (targetFd !== undefined) {35 return targetFd;36 }3738 if (isNodeStream(value, {checkOpen: false})) {39 throw new TypeError(`The \`${optionName}: Stream\` option cannot both be an array and include a stream with synchronous methods.`);40 }4142 return stdioItem;43};4445const getTargetFd = ({value, optionName, fdNumber, direction}) => {46 const targetFdNumber = getTargetFdNumber(value, fdNumber);47 if (targetFdNumber === undefined) {48 return;49 }5051 if (direction === 'output') {52 return {type: 'fileNumber', value: targetFdNumber, optionName};53 }5455 if (tty.isatty(targetFdNumber)) {56 throw new TypeError(`The \`${optionName}: ${serializeOptionValue(value)}\` option is invalid: it cannot be a TTY with synchronous methods.`);57 }58Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 59–60why, one step back59 // The file descriptor is read lazily, like `inputFile`, so that a read failure is reported like any other early error, instead of being thrown as a raw error60 return {type: 'uint8Array', contents: () => [bufferToUint8Array(readFileSync(targetFdNumber))], optionName};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 61–86why, one step back61};6263const getTargetFdNumber = (value, fdNumber) => {64 if (value === 'inherit') {65 return fdNumber;66 }6768 if (typeof value === 'number') {69 return value;70 }7172 const standardStreamIndex = STANDARD_STREAMS.indexOf(value);73 if (standardStreamIndex !== -1) {74 return standardStreamIndex;75 }76};7778const handleNativeStreamAsync = ({stdioItem, stdioItem: {value, optionName}, fdNumber}) => {79 if (value === 'inherit') {80 return {type: 'nodeStream', value: getStandardStream(fdNumber, value, optionName), optionName};81 }8283 if (typeof value === 'number') {84 return {type: 'nodeStream', value: getStandardStream(value, value, optionName), optionName};85 }86TweaksSindre Sorhusbefore adoption698a1b8
line 87why, one step back87 return isNodeStream(value, {checkOpen: false}) ? {type: 'nodeStream', value, optionName} : stdioItem;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 88–103why, one step back88};8990// Node.js does not allow to easily retrieve file descriptors beyond stdin/stdout/stderr as streams.91// - `fs.createReadStream()`/`fs.createWriteStream()` with the `fd` option do not work with character devices that use blocking reads/writes (such as interactive TTYs).92// - Using a TCP `Socket` would work but be rather complex to implement.93// Since this is an edge case, we simply throw an error message.94// See https://github.com/sindresorhus/execa/pull/643#discussion_r143590570795const getStandardStream = (fdNumber, value, optionName) => {96 const standardStream = STANDARD_STREAMS[fdNumber];9798 if (standardStream === undefined) {99 throw new TypeError(`The \`${optionName}: ${value}\` option is invalid: no such standard stream.`);100 }101102 return standardStream;103};