Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 7264bcf point at it.

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 1–2why, one step back
1import {readFileSync} from 'node:fs';
2import tty from 'node:tty';

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

line 3why, one step back
3import {isStream as isNodeStream} from 'is-stream';

Rename files (#977)ehmickybefore adoption4b2e316

lines 4–6why, one step back
4import {STANDARD_STREAMS} from '../utils/standard-stream.js';
5import {bufferToUint8Array} from '../utils/uint-array.js';6import {serializeOptionValue} from '../arguments/fd-options.js';

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

lines 7–14why, one step back
7
8// 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.

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 15–16why, one step back
15export const handleNativeStream = ({stdioItem, stdioItem: {type}, isStdioArray, fdNumber, direction, isSync}) => {
16 if (!isStdioArray || type !== 'native') {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 17why, one step back
17 return stdioItem;

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

lines 18–19why, one step back
18 }
19

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 20–24why, one step back
20 return isSync
21 ? handleNativeStreamSync({stdioItem, fdNumber, direction})22 : handleNativeStreamAsync({stdioItem, fdNumber});23};24

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 25–26why, one step back
25// 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 back
27const handleNativeStreamSync = ({stdioItem, stdioItem: {value, optionName}, fdNumber, direction}) => {

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 28–33why, one step back
28 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 back
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 }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}) => {

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

line 78why, one step back
78 if (value === 'inherit') {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 79why, one step back
79 return {type: 'nodeStream', value: getStandardStream(fdNumber, value, optionName), optionName};

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

lines 80–82why, one step back
80 }
8182 if (typeof value === 'number') {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 83why, one step back
83 return {type: 'nodeStream', value: getStandardStream(value, value, optionName), optionName};

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

lines 84–86why, one step back
84 }
8586 if (isNodeStream(value, {checkOpen: false})) {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 87why, one step back
87 return {type: 'nodeStream', value, optionName};

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

lines 88–89why, one step back
88 }
89

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 90why, one step back
90 return stdioItem;

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

lines 91–106why, one step back
91};
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_r143590570798const getStandardStream = (fdNumber, value, optionName) => {99 const standardStream = STANDARD_STREAMS[fdNumber];100101 if (standardStream === undefined) {102 throw new TypeError(`The \`${optionName}: ${value}\` option is invalid: no such standard stream.`);103 }104105 return standardStream;106};