native.js
This file
Path
lib/stdio/native.js
106 lines · 3.7 KiB · JavaScript
Last changed by
Add more code comments (#984)ehmickybefore adoptionfdd8a9f
History of this fileTake it
The walk stopped after 200 commits: lines older than db95b51 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';Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
line 3why, one step back3import {isStream as isNodeStream} from 'is-stream';Rename files (#977)ehmickybefore adoption4b2e316
lines 4–6why, one step back4import {STANDARD_STREAMS} from '../utils/standard-stream.js';5import {bufferToUint8Array} from '../utils/uint-array.js';6import {serializeOptionValue} from '../arguments/fd-options.js';Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 7–12why, one step back78// 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`Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 13why, one step back13// - any file descriptor integer becomes `process.stdio[fdNumber]`Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
line 14why, one step back14// All of the above transformations tell Execa to perform manual piping.Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
lines 15–16why, one step back15export const handleNativeStream = ({stdioItem, stdioItem: {type}, isStdioArray, fdNumber, direction, isSync}) => {16 if (!isStdioArray || type !== 'native') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 17why, one step back17 return stdioItem;Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 18–19why, one step back18 }19Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
lines 20–24why, one step back20 return isSync21 ? handleNativeStreamSync({stdioItem, fdNumber, direction})22 : handleNativeStreamAsync({stdioItem, fdNumber});23};24Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 25–26why, one step back25// Synchronous methods use a different logic.26// 'inherit', file descriptors and process.std* are handled by readFileSync()/writeFileSync().Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
line 27why, one step back27const handleNativeStreamSync = ({stdioItem, stdioItem: {value, optionName}, fdNumber, direction}) => {Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 28–33why, one step back28 const targetFd = getTargetFd({29 value,30 optionName,31 fdNumber,32 direction,33 });Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
lines 34–77why, one step back34 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 }5859 return {type: 'uint8Array', value: bufferToUint8Array(readFileSync(targetFdNumber)), optionName};60};6162const getTargetFdNumber = (value, fdNumber) => {63 if (value === 'inherit') {64 return fdNumber;65 }6667 if (typeof value === 'number') {68 return value;69 }7071 const standardStreamIndex = STANDARD_STREAMS.indexOf(value);72 if (standardStreamIndex !== -1) {73 return standardStreamIndex;74 }75};7677const handleNativeStreamAsync = ({stdioItem, stdioItem: {value, optionName}, fdNumber}) => {Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
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};Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
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};Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
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};Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
lines 88–89why, one step back88 }89Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 90why, one step back90 return stdioItem;Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
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];Fix calling `.kill()` many times (#768)ehmickybefore adoptiondb95b51
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};