validate.js
This file
Path
lib/pipe/validate.js
196 lines · 6.6 KiB · JavaScript
Last changed by
Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
History of this fileTake it
Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
line 1why, one step back1import {execa} from '../async.js';Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
line 2why, one step back2import {create$} from '../script.js';Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
line 3why, one step back3import {normalizeArguments} from '../arguments/options.js';Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 4why, one step back4import {STANDARD_STREAMS_ALIASES} from '../utils.js';Add `result|error.durationMs` (#896)ehmickybefore adoption7088091
line 5why, one step back5import {getStartTime} from '../return/duration.js';Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 6why, one step back6Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 7why, one step back7export const normalizePipeArguments = ({source, sourcePromise, boundOptions}, ...args) => {Add `result|error.durationMs` (#896)ehmickybefore adoption7088091
line 8why, one step back8 const startTime = getStartTime();Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
lines 9–12why, one step back9 const {10 destination,11 destinationStream,12 destinationError,Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 13–16why, one step back13 from,14 unpipeSignal,15 } = getDestinationStream(boundOptions, args);16 const {sourceStream, sourceError} = getSourceStream(source, from);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 17why, one step back17 const {options: sourceOptions, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
lines 18–25why, one step back18 return {19 sourcePromise,20 sourceStream,21 sourceOptions,22 sourceError,23 destination,24 destinationStream,25 destinationError,Rename `signal` option to `unpipeSignal` (#852)ehmickybefore adoption9dc1141
line 26why, one step back26 unpipeSignal,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 27why, one step back27 fileDescriptors,Add `result|error.durationMs` (#896)ehmickybefore adoption7088091
line 28why, one step back28 startTime,Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 29why, one step back29 };Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 30–31why, one step back30};31Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 32why, one step back32const getDestinationStream = (boundOptions, args) => {Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 33why, one step back33 try {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 34–39why, one step back34 const {35 destination,36 pipeOptions: {from, to, unpipeSignal} = {},37 } = getDestination(boundOptions, ...args);38 const destinationStream = getWritable(destination, to);39 return {destination, destinationStream, from, unpipeSignal};Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 40–44why, one step back40 } catch (error) {41 return {destinationError: error};42 }43};44Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 45why, one step back45const getDestination = (boundOptions, firstArgument, ...args) => {Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
line 46why, one step back46 if (Array.isArray(firstArgument)) {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 47–48why, one step back47 const destination = create$({...boundOptions, ...PIPED_SUBPROCESS_OPTIONS})(firstArgument, ...args);48 return {destination, pipeOptions: boundOptions};Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
lines 49–50why, one step back49 }50Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
line 51why, one step back51 if (typeof firstArgument === 'string' || firstArgument instanceof URL) {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 52–53why, one step back52 if (Object.keys(boundOptions).length > 0) {53 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 54–56why, one step back54 }5556 const [rawFile, rawArgs, rawOptions] = normalizeArguments(firstArgument, ...args);Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 57why, one step back57 const destination = execa(rawFile, rawArgs, {...rawOptions, ...PIPED_SUBPROCESS_OPTIONS});Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
line 58why, one step back58 return {destination, pipeOptions: rawOptions};Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
lines 59–60why, one step back59 }60Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 61why, one step back61 if (SUBPROCESS_OPTIONS.has(firstArgument)) {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 62why, one step back62 if (Object.keys(boundOptions).length > 0) {Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
lines 63–66why, one step back63 throw new TypeError('Please use .pipe(options)`command` or .pipe($(options)`command`) instead of .pipe(options)($`command`).');64 }6566 return {destination: firstArgument, pipeOptions: args[0]};Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
lines 67–68why, one step back67 }68Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 69why, one step back69 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 70why, one step back70};Improve `verbose` option (#883)ehmickybefore adoption7d0943f
line 71why, one step back71Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 72why, one step back72const PIPED_SUBPROCESS_OPTIONS = {stdin: 'pipe', piped: true};Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 73why, one step back73Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 74why, one step back74export const SUBPROCESS_OPTIONS = new WeakMap();Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 75why, one step back75Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
line 76why, one step back76export const getWritable = (destination, to = 'stdin') => {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 77why, one step back77 const isWritable = true;Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 78–79why, one step back78 const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(destination);79 const fdNumber = getFdNumber(fileDescriptors, to, isWritable);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 80why, one step back80 const destinationStream = destination.stdio[fdNumber];Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 81why, one step back81Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 82–84why, one step back82 if (destinationStream === null) {83 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, to, options, isWritable));84 }Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 85why, one step back85Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 86–91why, one step back86 return destinationStream;87};8889const getSourceStream = (source, from) => {90 try {91 const sourceStream = getReadable(source, from);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 92–97why, one step back92 return {sourceStream};93 } catch (error) {94 return {sourceError: error};95 }96};97Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
line 98why, one step back98export const getReadable = (source, from = 'stdout') => {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 99why, one step back99 const isWritable = false;Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 100–101why, one step back100 const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);101 const fdNumber = getFdNumber(fileDescriptors, from, isWritable);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 102why, one step back102 const sourceStream = fdNumber === 'all' ? source.all : source.stdio[fdNumber];Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 103why, one step back103Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 104–110why, one step back104 if (sourceStream === null || sourceStream === undefined) {105 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, from, options, isWritable));106 }107108 return sourceStream;109};110Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 111why, one step back111const getFdNumber = (fileDescriptors, fdName, isWritable) => {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 112why, one step back112 const fdNumber = parseFdNumber(fdName, isWritable);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 113why, one step back113 validateFdNumber(fdNumber, fdName, isWritable, fileDescriptors);Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 114–115why, one step back114 return fdNumber;115};Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 116why, one step back116Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 117–119why, one step back117const parseFdNumber = (fdName, isWritable) => {118 if (fdName === 'all') {119 return fdName;Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 120–121why, one step back120 }121Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 122–123why, one step back122 if (STANDARD_STREAMS_ALIASES.includes(fdName)) {123 return STANDARD_STREAMS_ALIASES.indexOf(fdName);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 124–125why, one step back124 }125Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 126–140why, one step back126 const regexpResult = FD_REGEXP.exec(fdName);127 if (regexpResult !== null) {128 return Number(regexpResult[1]);129 }130131 const {validOptions, defaultValue} = isWritable132 ? {validOptions: '"stdin"', defaultValue: 'stdin'}133 : {validOptions: '"stdout", "stderr", "all"', defaultValue: 'stdout'};134 throw new TypeError(`"${getOptionName(isWritable)}" must not be "${fdName}".135It must be ${validOptions} or "fd3", "fd4" (and so on).136It is optional and defaults to "${defaultValue}".`);137};138139const FD_REGEXP = /^fd(\d+)$/;140Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 141why, one step back141const validateFdNumber = (fdNumber, fdName, isWritable, fileDescriptors) => {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 142why, one step back142 const usedDescriptor = getUsedDescriptor(fdNumber);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 143–144why, one step back143 const fileDescriptor = fileDescriptors.find(fileDescriptor => fileDescriptor.fdNumber === usedDescriptor);144 if (fileDescriptor === undefined) {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 145why, one step back145 throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. That file descriptor does not exist.Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 146–148why, one step back146Please set the "stdio" option to ensure that file descriptor exists.`);147 }148Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 149why, one step back149 if (fileDescriptor.direction === 'input' && !isWritable) {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 150why, one step back150 throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a readable stream, not writable.`);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 151–152why, one step back151 }152Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 153why, one step back153 if (fileDescriptor.direction !== 'input' && isWritable) {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 154why, one step back154 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 155why, one step back155 }Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 156–157why, one step back156};157Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 158–160why, one step back158const getInvalidStdioOptionMessage = (fdNumber, fdName, options, isWritable) => {159 if (fdNumber === 'all' && !options.all) {160 return 'The "all" option must be true to use "from: \'all\'".';Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 161–162why, one step back161 }162Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 163–164why, one step back163 const {optionName, optionValue} = getInvalidStdioOption(fdNumber, options);164 return `The "${optionName}: ${serializeOptionValue(optionValue)}" option is incompatible with using "${getOptionName(isWritable)}: ${serializeOptionValue(fdName)}".Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 165–167why, one step back165Please set this option with "pipe" instead.`;166};167Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 168why, one step back168const getInvalidStdioOption = (fdNumber, {stdin, stdout, stderr, stdio}) => {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 169why, one step back169 const usedDescriptor = getUsedDescriptor(fdNumber);Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 170why, one step back170Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 171–174why, one step back171 if (usedDescriptor === 0 && stdin !== undefined) {172 return {optionName: 'stdin', optionValue: stdin};173 }174Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 175why, one step back175 if (usedDescriptor === 1 && stdout !== undefined) {Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 176–178why, one step back176 return {optionName: 'stdout', optionValue: stdout};177 }178Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 179why, one step back179 if (usedDescriptor === 2 && stderr !== undefined) {Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 180–182why, one step back180 return {optionName: 'stderr', optionValue: stderr};181 }182Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40
line 183why, one step back183 return {optionName: `stdio[${usedDescriptor}]`, optionValue: stdio[usedDescriptor]};Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 184why, one step back184};Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 185–186why, one step back185186const getUsedDescriptor = fdNumber => fdNumber === 'all' ? 1 : fdNumber;Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 187–189why, one step back187188const serializeOptionValue = optionValue => {189 if (typeof optionValue === 'string') {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 190why, one step back190 return `'${optionValue}'`;Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 191–194why, one step back191 }192193 return typeof optionValue === 'number' ? `${optionValue}` : 'Stream';194};Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 195–196why, one step back195196const getOptionName = isWritable => isWritable ? 'to' : 'from';