Skip to content
Myr

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 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';

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 4why, one step back
4

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

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

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

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

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

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

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

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

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

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 25why, one step back
25 fileDescriptors,

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

line 26why, one step back
26 startTime,

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

line 27why, one step back
27 };

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 31why, one step back
31 try {

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

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

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

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 69why, one step back
69

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

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

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

line 71why, one step back
71

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

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

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

line 73why, one step back
73

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

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 79why, one step back
79

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 83why, one step back
83

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

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

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

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 101why, one step back
101

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

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

line 114why, one step back
114

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 141–142why, one step back
141 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 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;

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 185–187why, one step back
185
186const serializeOptionValue = optionValue => {187 if (typeof optionValue === 'string') {

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

line 188why, one step back
188 return `'${optionValue}'`;

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 189–192why, one step back
189 }
190191 return typeof optionValue === 'number' ? `${optionValue}` : 'Stream';192};

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

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