Skip to content
Myr

handle.js

This file

Path

lib/stdio/handle.js

149 lines · 5.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 ed8b2ce point at it.

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–3why, one step back
1import {getStreamName} from '../utils/standard-stream.js';
2import {normalizeTransforms} from '../transform/normalize.js';3import {getFdObjectMode} from '../transform/object-mode.js';

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 4–9why, one step back
4import {
5 getStdioItemType,6 isRegularUrl,7 isUnknownStdioString,8 FILE_TYPES,9} from './type.js';

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 10why, one step back
10import {getStreamDirection} from './direction.js';

Rename files (#977)ehmickybefore adoption4b2e316

line 11why, one step back
11import {normalizeStdioOption} from './stdio-option.js';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 12why, one step back
12import {handleNativeStream} from './native.js';

Rename files (#977)ehmickybefore adoption4b2e316

line 13why, one step back
13import {handleInputOptions} from './input-option.js';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 14–15why, one step back
14
15// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync mode

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 16–17why, one step back
16// They are converted into an array of `fileDescriptors`.
17// Each `fileDescriptor` is normalized, validated and contains all information necessary for further handling.

Rename files (#977)ehmickybefore adoption4b2e316

lines 18–19why, one step back
18export const handleStdio = (addProperties, options, verboseInfo, isSync) => {
19 const stdio = normalizeStdioOption(options, isSync);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 20–26why, one step back
20 const fileDescriptors = stdio.map((stdioOption, fdNumber) => getFileDescriptor({
21 stdioOption,22 fdNumber,23 addProperties,24 options,25 isSync,26 }));

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 27why, one step back
27 options.stdio = fileDescriptors.map(({stdioItems}) => forwardStdio(stdioItems));

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

line 28why, one step back
28 return fileDescriptors;

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 29–30why, one step back
29};
30

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

line 31why, one step back
31const getFileDescriptor = ({stdioOption, fdNumber, addProperties, options, isSync}) => {

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 32why, one step back
32 const optionName = getStreamName(fdNumber);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 33–38why, one step back
33 const {stdioItems: initialStdioItems, isStdioArray} = initializeStdioItems({
34 stdioOption,35 fdNumber,36 options,37 optionName,38 });

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

line 39why, one step back
39 const direction = getStreamDirection(initialStdioItems, fdNumber, optionName);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 40–46why, one step back
40 const stdioItems = initialStdioItems.map(stdioItem => handleNativeStream({
41 stdioItem,42 isStdioArray,43 fdNumber,44 direction,45 isSync,46 }));

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

line 47why, one step back
47 const normalizedStdioItems = normalizeTransforms(stdioItems, optionName, direction, options);

Split files into smaller ones (#976)ehmickybefore adoptiond7c6853

line 48why, one step back
48 const objectMode = getFdObjectMode(normalizedStdioItems, direction);

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

lines 49–50why, one step back
49 validateFileObjectMode(normalizedStdioItems, objectMode);
50 const finalStdioItems = normalizedStdioItems.map(stdioItem => addStreamProperties(stdioItem, addProperties, direction, options));

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 51why, one step back
51 return {direction, objectMode, stdioItems: finalStdioItems};

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 52–53why, one step back
52};
53

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 54–56why, one step back
54// We make sure passing an array with a single item behaves the same as passing that item without an array.
55// This is what users would expect.56// For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.

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

line 57why, one step back
57const initializeStdioItems = ({stdioOption, fdNumber, options, optionName}) => {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 58–67why, one step back
58 const values = Array.isArray(stdioOption) ? stdioOption : [stdioOption];
59 const initialStdioItems = [60 ...values.map(value => initializeStdioItem(value, optionName)),61 ...handleInputOptions(options, fdNumber),62 ];6364 const stdioItems = filterDuplicates(initialStdioItems);65 const isStdioArray = stdioItems.length > 1;66 validateStdioArray(stdioItems, isStdioArray, optionName);67 validateStreams(stdioItems);

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

line 68why, one step back
68 return {stdioItems, isStdioArray};

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 69–70why, one step back
69};
70

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 71–75why, one step back
71const initializeStdioItem = (value, optionName) => ({
72 type: getStdioItemType(value, optionName),73 value,74 optionName,75});

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 76why, one step back
76

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 77why, one step back
77const filterDuplicates = stdioItems => stdioItems.filter((stdioItemOne, indexOne) =>

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 78–81why, one step back
78 stdioItems.every((stdioItemTwo, indexTwo) => stdioItemOne.value !== stdioItemTwo.value
79 || indexOne >= indexTwo80 || stdioItemOne.type === 'generator'81 || stdioItemOne.type === 'asyncGenerator'));

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 82–84why, one step back
82
83const validateStdioArray = (stdioItems, isStdioArray, optionName) => {84 if (stdioItems.length === 0) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 85–91why, one step back
85 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);
86 }8788 if (!isStdioArray) {89 return;90 }91

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 92why, one step back
92 for (const {value, optionName} of stdioItems) {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 93–102why, one step back
93 if (INVALID_STDIO_ARRAY_OPTIONS.has(value)) {
94 throw new Error(`The \`${optionName}\` option must not include \`${value}\`.`);95 }96 }97};9899// Using those `stdio` values together with others for the same stream does not make sense, so we make it fail.100// However, we do allow it if the array has a single item.101const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);102

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 103–105why, one step back
103const validateStreams = stdioItems => {
104 for (const stdioItem of stdioItems) {105 validateFileStdio(stdioItem);

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 106–119why, one step back
106 }
107};108109const validateFileStdio = ({type, value, optionName}) => {110 if (isRegularUrl(value)) {111 throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.112For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);113 }114115 if (isUnknownStdioString(type, value)) {116 throw new TypeError(`The \`${optionName}: { file: '...' }\` option must be used instead of \`${optionName}: '...'\`.`);117 }118};119

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

lines 120–128why, one step back
120const validateFileObjectMode = (stdioItems, objectMode) => {
121 if (!objectMode) {122 return;123 }124125 const fileStdioItem = stdioItems.find(({type}) => FILE_TYPES.has(type));126 if (fileStdioItem !== undefined) {127 throw new TypeError(`The \`${fileStdioItem.optionName}\` option cannot use both files and transforms in objectMode.`);128 }

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 129–130why, one step back
129};
130

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 131–133why, one step back
131// Some `stdio` values require Execa to create streams.
132// For example, file paths create file read/write streams.133// Those transformations are specified in `addProperties`, which is both direction-specific and type-specific.

Improve `encoding` option (#958)ehmickybefore adoption507b09c

line 134why, one step back
134const addStreamProperties = (stdioItem, addProperties, direction, options) => ({

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 135why, one step back
135 ...stdioItem,

Improve `encoding` option (#958)ehmickybefore adoption507b09c

line 136why, one step back
136 ...addProperties[direction][stdioItem.type](stdioItem, options),

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 137why, one step back
137});

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 138why, one step back
138

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

lines 139–144why, one step back
139// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `subprocess.std*`.
140// When the `std*: Array` option is used, we emulate some of the native values ('inherit', Node.js stream and file descriptor integer). To do so, we also need to pipe to `subprocess.std*`.141// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `subprocess.std*`.142const forwardStdio = stdioItems => {143 if (stdioItems.length > 1) {144 return stdioItems.some(({value}) => value === 'overlapped') ? 'overlapped' : 'pipe';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

lines 145–146why, one step back
145 }
146

Improve `verbose` option (#962)ehmickybefore adoption58beaa6

lines 147–148why, one step back
147 const [{type, value}] = stdioItems;
148 return type === 'native' ? value : 'pipe';

Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09

line 149why, one step back
149};