validate.js
This file
Path
lib/pipe/validate.js
186 lines · 6.4 KiB · JavaScript
Last changed by
Simplify implementation (#955)ehmickybefore adoption8ad9ee7
History of this fileTake it
Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 1why, one step back1import {normalizeArguments} from '../arguments/normalize.js';Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 2why, one step back2import {STANDARD_STREAMS_ALIASES} from '../utils.js';Add `result|error.durationMs` (#896)ehmickybefore adoption7088091
line 3why, one step back3import {getStartTime} from '../return/duration.js';Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc
line 4why, one step back4import {serializeOptionValue} from '../stdio/native.js';Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 5why, one step back5Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 6why, one step back6export const normalizePipeArguments = ({source, sourcePromise, boundOptions, createNested}, ...args) => {Add `result|error.durationMs` (#896)ehmickybefore adoption7088091
line 7why, one step back7 const startTime = getStartTime();Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
lines 8–11why, one step back8 const {9 destination,10 destinationStream,11 destinationError,Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 12–13why, one step back12 from,13 unpipeSignal,Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 14why, one step back14 } = getDestinationStream(boundOptions, createNested, args);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 15why, one step back15 const {sourceStream, sourceError} = getSourceStream(source, from);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 16why, one step back16 const {options: sourceOptions, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
lines 17–24why, one step back17 return {18 sourcePromise,19 sourceStream,20 sourceOptions,21 sourceError,22 destination,23 destinationStream,24 destinationError,Rename `signal` option to `unpipeSignal` (#852)ehmickybefore adoption9dc1141
line 25why, one step back25 unpipeSignal,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 26why, one step back26 fileDescriptors,Add `result|error.durationMs` (#896)ehmickybefore adoption7088091
line 27why, one step back27 startTime,Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 28why, one step back28 };Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 29–30why, one step back29};30Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 31why, one step back31const getDestinationStream = (boundOptions, createNested, args) => {Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 32why, one step back32 try {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 33–35why, one step back33 const {34 destination,35 pipeOptions: {from, to, unpipeSignal} = {},Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 36why, one step back36 } = getDestination(boundOptions, createNested, ...args);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 37–38why, one step back37 const destinationStream = getWritable(destination, to);38 return {destination, destinationStream, from, unpipeSignal};Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 39–43why, one step back39 } catch (error) {40 return {destinationError: error};41 }42};43Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 44why, one step back44const getDestination = (boundOptions, createNested, firstArgument, ...args) => {Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
line 45why, one step back45 if (Array.isArray(firstArgument)) {Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 46why, one step back46 const destination = createNested(mapDestinationArguments, boundOptions)(firstArgument, ...args);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 47why, one step back47 return {destination, pipeOptions: boundOptions};Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
lines 48–49why, one step back48 }49Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
line 50why, one step back50 if (typeof firstArgument === 'string' || firstArgument instanceof URL) {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 51–52why, one step back51 if (Object.keys(boundOptions).length > 0) {52 throw new TypeError('Please use .pipe("file", ..., options) or .pipe(execa("file", ..., options)) instead of .pipe(options)("file", ...).');Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
lines 53–55why, one step back53 }5455 const [rawFile, rawArgs, rawOptions] = normalizeArguments(firstArgument, ...args);Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 56why, one step back56 const destination = createNested(mapDestinationArguments)(rawFile, rawArgs, rawOptions);Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
line 57why, one step back57 return {destination, pipeOptions: rawOptions};Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
lines 58–59why, one step back58 }59Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 60why, one step back60 if (SUBPROCESS_OPTIONS.has(firstArgument)) {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 61why, one step back61 if (Object.keys(boundOptions).length > 0) {Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
lines 62–65why, one step back62 throw new TypeError('Please use .pipe(options)`command` or .pipe($(options)`command`) instead of .pipe(options)($`command`).');63 }6465 return {destination: firstArgument, pipeOptions: args[0]};Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
lines 66–67why, one step back66 }67Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 68why, one step back68 throw new TypeError(`The first argument must be a template string, an options object, or an Execa subprocess: ${firstArgument}`);Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 69why, one step back69};Improve `verbose` option (#883)ehmickybefore adoption7d0943f
line 70why, one step back70Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 71why, one step back71const mapDestinationArguments = ({options}) => ({options: {...options, stdin: 'pipe', piped: true}});Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 72why, one step back72Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 73why, one step back73export const SUBPROCESS_OPTIONS = new WeakMap();Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 74why, one step back74Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
line 75why, one step back75export const getWritable = (destination, to = 'stdin') => {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 76why, one step back76 const isWritable = true;Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 77–78why, one step back77 const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(destination);78 const fdNumber = getFdNumber(fileDescriptors, to, isWritable);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 79why, one step back79 const destinationStream = destination.stdio[fdNumber];Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 80why, one step back80Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 81–83why, one step back81 if (destinationStream === null) {82 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, to, options, isWritable));83 }Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 84why, one step back84Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 85–90why, one step back85 return destinationStream;86};8788const getSourceStream = (source, from) => {89 try {90 const sourceStream = getReadable(source, from);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 91–96why, one step back91 return {sourceStream};92 } catch (error) {93 return {sourceError: error};94 }95};96Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
line 97why, one step back97export const getReadable = (source, from = 'stdout') => {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 98why, one step back98 const isWritable = false;Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 99–100why, one step back99 const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);100 const fdNumber = getFdNumber(fileDescriptors, from, isWritable);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 101why, one step back101 const sourceStream = fdNumber === 'all' ? source.all : source.stdio[fdNumber];Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 102why, one step back102Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 103–109why, one step back103 if (sourceStream === null || sourceStream === undefined) {104 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, from, options, isWritable));105 }106107 return sourceStream;108};109Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 110why, one step back110const getFdNumber = (fileDescriptors, fdName, isWritable) => {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 111why, one step back111 const fdNumber = parseFdNumber(fdName, isWritable);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 112why, one step back112 validateFdNumber(fdNumber, fdName, isWritable, fileDescriptors);Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 113–114why, one step back113 return fdNumber;114};Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 115why, one step back115Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 116–118why, one step back116const parseFdNumber = (fdName, isWritable) => {117 if (fdName === 'all') {118 return fdName;Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 119–120why, one step back119 }120Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 121–122why, one step back121 if (STANDARD_STREAMS_ALIASES.includes(fdName)) {122 return STANDARD_STREAMS_ALIASES.indexOf(fdName);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 123–124why, one step back123 }124Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 125–139why, one step back125 const regexpResult = FD_REGEXP.exec(fdName);126 if (regexpResult !== null) {127 return Number(regexpResult[1]);128 }129130 const {validOptions, defaultValue} = isWritable131 ? {validOptions: '"stdin"', defaultValue: 'stdin'}132 : {validOptions: '"stdout", "stderr", "all"', defaultValue: 'stdout'};133 throw new TypeError(`"${getOptionName(isWritable)}" must not be "${fdName}".134It must be ${validOptions} or "fd3", "fd4" (and so on).135It is optional and defaults to "${defaultValue}".`);136};137138const FD_REGEXP = /^fd(\d+)$/;139Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 140why, one step back140const validateFdNumber = (fdNumber, fdName, isWritable, fileDescriptors) => {Simplify implementation (#955)ehmickybefore adoption8ad9ee7
line 141why, one step back141 const fileDescriptor = fileDescriptors[getUsedDescriptor(fdNumber)];Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 142why, one step back142 if (fileDescriptor === undefined) {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 143why, one step back143 throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. That file descriptor does not exist.Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 144–146why, one step back144Please set the "stdio" option to ensure that file descriptor exists.`);145 }146Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 147why, one step back147 if (fileDescriptor.direction === 'input' && !isWritable) {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 148why, one step back148 throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a readable stream, not writable.`);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 149–150why, one step back149 }150Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 151why, one step back151 if (fileDescriptor.direction !== 'input' && isWritable) {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 152why, one step back152 throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a writable stream, not readable.`);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 153why, one step back153 }Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 154–155why, one step back154};155Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 156–158why, one step back156const getInvalidStdioOptionMessage = (fdNumber, fdName, options, isWritable) => {157 if (fdNumber === 'all' && !options.all) {158 return 'The "all" option must be true to use "from: \'all\'".';Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 159–160why, one step back159 }160Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 161–162why, one step back161 const {optionName, optionValue} = getInvalidStdioOption(fdNumber, options);162 return `The "${optionName}: ${serializeOptionValue(optionValue)}" option is incompatible with using "${getOptionName(isWritable)}: ${serializeOptionValue(fdName)}".Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 163–165why, one step back163Please set this option with "pipe" instead.`;164};165Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 166why, one step back166const getInvalidStdioOption = (fdNumber, {stdin, stdout, stderr, stdio}) => {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 167why, one step back167 const usedDescriptor = getUsedDescriptor(fdNumber);Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 168why, one step back168Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 169–172why, one step back169 if (usedDescriptor === 0 && stdin !== undefined) {170 return {optionName: 'stdin', optionValue: stdin};171 }172Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 173why, one step back173 if (usedDescriptor === 1 && stdout !== undefined) {Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 174–176why, one step back174 return {optionName: 'stdout', optionValue: stdout};175 }176Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 177why, one step back177 if (usedDescriptor === 2 && stderr !== undefined) {Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 178–180why, one step back178 return {optionName: 'stderr', optionValue: stderr};179 }180Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 181why, one step back181 return {optionName: `stdio[${usedDescriptor}]`, optionValue: stdio[usedDescriptor]};Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 182why, one step back182};Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 183–184why, one step back183184const getUsedDescriptor = fdNumber => fdNumber === 'all' ? 1 : fdNumber;Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 185–186why, one step back185186const getOptionName = isWritable => isWritable ? 'to' : 'from';