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 fileTake it
The walk stopped after 200 commits: lines older than ed8b2ce point at it.
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–3why, one step back1import {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 back4import {5 getStdioItemType,6 isRegularUrl,7 isUnknownStdioString,8 FILE_TYPES,9} from './type.js';Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 10why, one step back10import {getStreamDirection} from './direction.js';Rename files (#977)ehmickybefore adoption4b2e316
line 11why, one step back11import {normalizeStdioOption} from './stdio-option.js';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 12why, one step back12import {handleNativeStream} from './native.js';Rename files (#977)ehmickybefore adoption4b2e316
line 13why, one step back13import {handleInputOptions} from './input-option.js';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 14–15why, one step back1415// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeAdd more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 16–17why, one step back16// 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 back18export const handleStdio = (addProperties, options, verboseInfo, isSync) => {19 const stdio = normalizeStdioOption(options, isSync);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 20–26why, one step back20 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 back27 options.stdio = fileDescriptors.map(({stdioItems}) => forwardStdio(stdioItems));Improve `verbose` option (#962)ehmickybefore adoption58beaa6
line 28why, one step back28 return fileDescriptors;Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 29–30why, one step back29};30Improve `verbose` option (#962)ehmickybefore adoption58beaa6
line 31why, one step back31const getFileDescriptor = ({stdioOption, fdNumber, addProperties, options, isSync}) => {Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6
line 32why, one step back32 const optionName = getStreamName(fdNumber);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 33–38why, one step back33 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 back39 const direction = getStreamDirection(initialStdioItems, fdNumber, optionName);Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 40–46why, one step back40 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 back47 const normalizedStdioItems = normalizeTransforms(stdioItems, optionName, direction, options);Split files into smaller ones (#976)ehmickybefore adoptiond7c6853
line 48why, one step back48 const objectMode = getFdObjectMode(normalizedStdioItems, direction);Improve `verbose` option (#962)ehmickybefore adoption58beaa6
lines 49–50why, one step back49 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 back51 return {direction, objectMode, stdioItems: finalStdioItems};Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 52–53why, one step back52};53Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 54–56why, one step back54// 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 back57const initializeStdioItems = ({stdioOption, fdNumber, options, optionName}) => {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 58–67why, one step back58 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 back68 return {stdioItems, isStdioArray};Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 69–70why, one step back69};70Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 71–75why, one step back71const 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 back76Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 77why, one step back77const filterDuplicates = stdioItems => stdioItems.filter((stdioItemOne, indexOne) =>Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
lines 78–81why, one step back78 stdioItems.every((stdioItemTwo, indexTwo) => stdioItemOne.value !== stdioItemTwo.value79 || indexOne >= indexTwo80 || stdioItemOne.type === 'generator'81 || stdioItemOne.type === 'asyncGenerator'));Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 82–84why, one step back8283const validateStdioArray = (stdioItems, isStdioArray, optionName) => {84 if (stdioItems.length === 0) {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 85–91why, one step back85 throw new TypeError(`The \`${optionName}\` option must not be an empty array.`);86 }8788 if (!isStdioArray) {89 return;90 }91Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 92why, one step back92 for (const {value, optionName} of stdioItems) {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 93–102why, one step back93 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']);102Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 103–105why, one step back103const validateStreams = stdioItems => {104 for (const stdioItem of stdioItems) {105 validateFileStdio(stdioItem);Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 106–119why, one step back106 }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};119Improve `verbose` option (#962)ehmickybefore adoption58beaa6
lines 120–128why, one step back120const 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 back129};130Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 131–133why, one step back131// 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 back134const addStreamProperties = (stdioItem, addProperties, direction, options) => ({Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 135why, one step back135 ...stdioItem,Improve `encoding` option (#958)ehmickybefore adoption507b09c
line 136why, one step back136 ...addProperties[direction][stdioItem.type](stdioItem, options),Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 137why, one step back137});Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 138why, one step back138Improve `verbose` option (#962)ehmickybefore adoption58beaa6
lines 139–144why, one step back139// 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 back145 }146Improve `verbose` option (#962)ehmickybefore adoption58beaa6
lines 147–148why, one step back147 const [{type, value}] = stdioItems;148 return type === 'native' ? value : 'pipe';Add support for transforms with `execaSync()` (#945)ehmickybefore adoptionc5f9f09
line 149why, one step back149};