Skip to content
Myr

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 file

Take it

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 1why, one step back
1import {normalizeArguments} from '../arguments/normalize.js';

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

line 2why, one step back
2import {STANDARD_STREAMS_ALIASES} from '../utils.js';

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 3why, one step back
3import {getStartTime} from '../return/duration.js';

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

line 4why, one step back
4import {serializeOptionValue} from '../stdio/native.js';

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 5why, one step back
5

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 6why, one step back
6export const normalizePipeArguments = ({source, sourcePromise, boundOptions, createNested}, ...args) => {

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 7why, one step back
7 const startTime = getStartTime();

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

lines 8–11why, one step back
8 const {
9 destination,10 destinationStream,11 destinationError,

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 12–13why, one step back
12 from,
13 unpipeSignal,

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 14why, one step back
14 } = getDestinationStream(boundOptions, createNested, args);

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 15why, one step back
15 const {sourceStream, sourceError} = getSourceStream(source, from);

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 16why, one step back
16 const {options: sourceOptions, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

lines 17–24why, one step back
17 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 back
25 unpipeSignal,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 26why, one step back
26 fileDescriptors,

Add `result|error.durationMs` (#896)ehmickybefore adoption7088091

line 27why, one step back
27 startTime,

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

line 28why, one step back
28 };

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 31why, one step back
31const getDestinationStream = (boundOptions, createNested, args) => {

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 32why, one step back
32 try {

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 33–35why, one step back
33 const {
34 destination,35 pipeOptions: {from, to, unpipeSignal} = {},

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 36why, one step back
36 } = getDestination(boundOptions, createNested, ...args);

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 37–38why, one step back
37 const destinationStream = getWritable(destination, to);
38 return {destination, destinationStream, from, unpipeSignal};

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 39–43why, one step back
39 } catch (error) {
40 return {destinationError: error};41 }42};43

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 44why, one step back
44const getDestination = (boundOptions, createNested, firstArgument, ...args) => {

Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86

line 45why, one step back
45 if (Array.isArray(firstArgument)) {

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 46why, one step back
46 const destination = createNested(mapDestinationArguments, boundOptions)(firstArgument, ...args);

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 47why, one step back
47 return {destination, pipeOptions: boundOptions};

Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86

lines 48–49why, one step back
48 }
49

Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d

line 50why, one step back
50 if (typeof firstArgument === 'string' || firstArgument instanceof URL) {

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 51–52why, one step back
51 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 back
53 }
5455 const [rawFile, rawArgs, rawOptions] = normalizeArguments(firstArgument, ...args);

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 56why, one step back
56 const destination = createNested(mapDestinationArguments)(rawFile, rawArgs, rawOptions);

Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d

line 57why, one step back
57 return {destination, pipeOptions: rawOptions};

Improve `.pipe()` method (#859)ehmickybefore adoption2ef9e86

lines 58–59why, one step back
58 }
59

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 60why, one step back
60 if (SUBPROCESS_OPTIONS.has(firstArgument)) {

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 61why, one step back
61 if (Object.keys(boundOptions).length > 0) {

Add `.pipe(command, args, options)` shortcut (#864)ehmickybefore adoption08f8e5d

lines 62–65why, one step back
62 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 back
66 }
67

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 68why, one step back
68 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 back
69};

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 70why, one step back
70

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

line 71why, one step back
71const mapDestinationArguments = ({options}) => ({options: {...options, stdin: 'pipe', piped: true}});

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

line 72why, one step back
72

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 73why, one step back
73export const SUBPROCESS_OPTIONS = new WeakMap();

Fix errors thrown by `.pipe()` (#854)ehmickybefore adoption46ba4b4

line 74why, one step back
74

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 75why, one step back
75export const getWritable = (destination, to = 'stdin') => {

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 76why, one step back
76 const isWritable = true;

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 77–78why, one step back
77 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 back
79 const destinationStream = destination.stdio[fdNumber];

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 80why, one step back
80

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 81–83why, one step back
81 if (destinationStream === null) {
82 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, to, options, isWritable));83 }

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 84why, one step back
84

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 85–90why, one step back
85 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 back
91 return {sourceStream};
92 } catch (error) {93 return {sourceError: error};94 }95};96

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 97why, one step back
97export const getReadable = (source, from = 'stdout') => {

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 98why, one step back
98 const isWritable = false;

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 99–100why, one step back
99 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 back
101 const sourceStream = fdNumber === 'all' ? source.all : source.stdio[fdNumber];

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 102why, one step back
102

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 103–109why, one step back
103 if (sourceStream === null || sourceStream === undefined) {
104 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, from, options, isWritable));105 }106107 return sourceStream;108};109

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 110why, one step back
110const getFdNumber = (fileDescriptors, fdName, isWritable) => {

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

line 111why, one step back
111 const fdNumber = parseFdNumber(fdName, isWritable);

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 112why, one step back
112 validateFdNumber(fdNumber, fdName, isWritable, fileDescriptors);

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

lines 113–114why, one step back
113 return fdNumber;
114};

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 115why, one step back
115

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

lines 116–118why, one step back
116const parseFdNumber = (fdName, isWritable) => {
117 if (fdName === 'all') {118 return fdName;

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 119–120why, one step back
119 }
120

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

lines 121–122why, one step back
121 if (STANDARD_STREAMS_ALIASES.includes(fdName)) {
122 return STANDARD_STREAMS_ALIASES.indexOf(fdName);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 123–124why, one step back
123 }
124

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

lines 125–139why, one step back
125 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+)$/;139

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 140why, one step back
140const validateFdNumber = (fdNumber, fdName, isWritable, fileDescriptors) => {

Simplify implementation (#955)ehmickybefore adoption8ad9ee7

line 141why, one step back
141 const fileDescriptor = fileDescriptors[getUsedDescriptor(fdNumber)];

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 142why, one step back
142 if (fileDescriptor === undefined) {

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

line 143why, one step back
143 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 back
144Please set the "stdio" option to ensure that file descriptor exists.`);
145 }146

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 147why, one step back
147 if (fileDescriptor.direction === 'input' && !isWritable) {

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

line 148why, one step back
148 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 back
149 }
150

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 151why, one step back
151 if (fileDescriptor.direction !== 'input' && isWritable) {

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

line 152why, one step back
152 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 back
153 }

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 154–155why, one step back
154};
155

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 156–158why, one step back
156const 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 back
159 }
160

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 161–162why, one step back
161 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 back
163Please set this option with "pipe" instead.`;
164};165

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

line 166why, one step back
166const getInvalidStdioOption = (fdNumber, {stdin, stdout, stderr, stdio}) => {

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

line 167why, one step back
167 const usedDescriptor = getUsedDescriptor(fdNumber);

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 168why, one step back
168

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 169–172why, one step back
169 if (usedDescriptor === 0 && stdin !== undefined) {
170 return {optionName: 'stdin', optionValue: stdin};171 }172

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 173why, one step back
173 if (usedDescriptor === 1 && stdout !== undefined) {

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 174–176why, one step back
174 return {optionName: 'stdout', optionValue: stdout};
175 }176

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 177why, one step back
177 if (usedDescriptor === 2 && stderr !== undefined) {

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 178–180why, one step back
178 return {optionName: 'stderr', optionValue: stderr};
179 }180

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 181why, one step back
181 return {optionName: `stdio[${usedDescriptor}]`, optionValue: stdio[usedDescriptor]};

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 182why, one step back
182};

Rename `from|to: 3` to `from|to: 'fd3'` (#920)ehmickybefore adoption8dfdd91

lines 183–184why, one step back
183
184const getUsedDescriptor = fdNumber => fdNumber === 'all' ? 1 : fdNumber;

Allow piping process to any file descriptor (#903)ehmickybefore adoption6642cd2

lines 185–186why, one step back
185
186const getOptionName = isWritable => isWritable ? 'to' : 'from';