validate.js
This file
Path
lib/pipe/validate.js
194 lines · 6.6 KiB · JavaScript
Last changed by
Allow template strings with all methods (#933)ehmickybefore adoption8074d85
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';Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 4why, one step back4Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 5why, one step back5export const normalizePipeArguments = ({source, sourcePromise, boundOptions, createNested}, ...args) => {Add `result|error.durationMs` (#896)ehmickybefore adoption7088091
line 6why, one step back6 const startTime = getStartTime();Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
lines 7–10why, one step back7 const {8 destination,9 destinationStream,10 destinationError,Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 11–12why, one step back11 from,12 unpipeSignal,Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 13why, one step back13 } = getDestinationStream(boundOptions, createNested, args);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 14why, one step back14 const {sourceStream, sourceError} = getSourceStream(source, from);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 15why, one step back15 const {options: sourceOptions, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
lines 16–23why, one step back16 return {17 sourcePromise,18 sourceStream,19 sourceOptions,20 sourceError,21 destination,22 destinationStream,23 destinationError,Rename `signal` option to `unpipeSignal` (#852)ehmickybefore adoption9dc1141
line 24why, one step back24 unpipeSignal,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 25why, one step back25 fileDescriptors,Add `result|error.durationMs` (#896)ehmickybefore adoption7088091
line 26why, one step back26 startTime,Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 27why, one step back27 };Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 28–29why, one step back28};29Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 30why, one step back30const getDestinationStream = (boundOptions, createNested, args) => {Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 31why, one step back31 try {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 32–34why, one step back32 const {33 destination,34 pipeOptions: {from, to, unpipeSignal} = {},Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 35why, one step back35 } = getDestination(boundOptions, createNested, ...args);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 36–37why, one step back36 const destinationStream = getWritable(destination, to);37 return {destination, destinationStream, from, unpipeSignal};Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 38–42why, one step back38 } catch (error) {39 return {destinationError: error};40 }41};42Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 43why, one step back43const getDestination = (boundOptions, createNested, firstArgument, ...args) => {Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
line 44why, one step back44 if (Array.isArray(firstArgument)) {Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 45why, one step back45 const destination = createNested(mapDestinationArguments, boundOptions)(firstArgument, ...args);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 46why, one step back46 return {destination, pipeOptions: boundOptions};Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
lines 47–48why, one step back47 }48Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
line 49why, one step back49 if (typeof firstArgument === 'string' || firstArgument instanceof URL) {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 50–51why, one step back50 if (Object.keys(boundOptions).length > 0) {51 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 52–54why, one step back52 }5354 const [rawFile, rawArgs, rawOptions] = normalizeArguments(firstArgument, ...args);Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 55why, one step back55 const destination = createNested(mapDestinationArguments)(rawFile, rawArgs, rawOptions);Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
line 56why, one step back56 return {destination, pipeOptions: rawOptions};Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86
lines 57–58why, one step back57 }58Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 59why, one step back59 if (SUBPROCESS_OPTIONS.has(firstArgument)) {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 60why, one step back60 if (Object.keys(boundOptions).length > 0) {Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d
lines 61–64why, one step back61 throw new TypeError('Please use .pipe(options)`command` or .pipe($(options)`command`) instead of .pipe(options)($`command`).');62 }6364 return {destination: firstArgument, pipeOptions: args[0]};Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
lines 65–66why, one step back65 }66Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 67why, one step back67 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 68why, one step back68};Improve `verbose` option (#883)ehmickybefore adoption7d0943f
line 69why, one step back69Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 70why, one step back70const mapDestinationArguments = ({options}) => ({options: {...options, stdin: 'pipe', piped: true}});Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 71why, one step back71Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 72why, one step back72export const SUBPROCESS_OPTIONS = new WeakMap();Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4
line 73why, one step back73Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
line 74why, one step back74export const getWritable = (destination, to = 'stdin') => {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 75why, one step back75 const isWritable = true;Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 76–77why, one step back76 const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(destination);77 const fdNumber = getFdNumber(fileDescriptors, to, isWritable);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 78why, one step back78 const destinationStream = destination.stdio[fdNumber];Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 79why, one step back79Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 80–82why, one step back80 if (destinationStream === null) {81 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, to, options, isWritable));82 }Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 83why, one step back83Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 84–89why, one step back84 return destinationStream;85};8687const getSourceStream = (source, from) => {88 try {89 const sourceStream = getReadable(source, from);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 90–95why, one step back90 return {sourceStream};91 } catch (error) {92 return {sourceError: error};93 }94};95Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9
line 96why, one step back96export const getReadable = (source, from = 'stdout') => {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 97why, one step back97 const isWritable = false;Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 98–99why, one step back98 const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);99 const fdNumber = getFdNumber(fileDescriptors, from, isWritable);Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 100why, one step back100 const sourceStream = fdNumber === 'all' ? source.all : source.stdio[fdNumber];Improve piping processes (#834)ehmickybefore adoption1a47ea2
line 101why, one step back101Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 102–108why, one step back102 if (sourceStream === null || sourceStream === undefined) {103 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, from, options, isWritable));104 }105106 return sourceStream;107};108Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 109why, one step back109const getFdNumber = (fileDescriptors, fdName, isWritable) => {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 110why, one step back110 const fdNumber = parseFdNumber(fdName, isWritable);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 111why, one step back111 validateFdNumber(fdNumber, fdName, isWritable, fileDescriptors);Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 112–113why, one step back112 return fdNumber;113};Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 114why, one step back114Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 115–117why, one step back115const parseFdNumber = (fdName, isWritable) => {116 if (fdName === 'all') {117 return fdName;Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 118–119why, one step back118 }119Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 120–121why, one step back120 if (STANDARD_STREAMS_ALIASES.includes(fdName)) {121 return STANDARD_STREAMS_ALIASES.indexOf(fdName);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 122–123why, one step back122 }123Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
lines 124–138why, one step back124 const regexpResult = FD_REGEXP.exec(fdName);125 if (regexpResult !== null) {126 return Number(regexpResult[1]);127 }128129 const {validOptions, defaultValue} = isWritable130 ? {validOptions: '"stdin"', defaultValue: 'stdin'}131 : {validOptions: '"stdout", "stderr", "all"', defaultValue: 'stdout'};132 throw new TypeError(`"${getOptionName(isWritable)}" must not be "${fdName}".133It must be ${validOptions} or "fd3", "fd4" (and so on).134It is optional and defaults to "${defaultValue}".`);135};136137const FD_REGEXP = /^fd(\d+)$/;138Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 139why, one step back139const validateFdNumber = (fdNumber, fdName, isWritable, fileDescriptors) => {Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91
line 140why, one step back140 const usedDescriptor = getUsedDescriptor(fdNumber);Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 141–142why, one step back141 const fileDescriptor = fileDescriptors.find(fileDescriptor => fileDescriptor.fdNumber === usedDescriptor);142 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;Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 185–187why, one step back185186const serializeOptionValue = optionValue => {187 if (typeof optionValue === 'string') {Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
line 188why, one step back188 return `'${optionValue}'`;Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 189–192why, one step back189 }190191 return typeof optionValue === 'number' ? `${optionValue}` : 'Stream';192};Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2
lines 193–194why, one step back193194const getOptionName = isWritable => isWritable ? 'to' : 'from';