Skip to content
Myr

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 file

Take it

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

line 1why, one step back
1import {execa} from '../async.js';

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

line 2why, one step back
2import {create$} from '../script.js';

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

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

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

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 6why, one step back
6

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

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

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

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

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

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

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

lines 13–16why, one step back
13 from,
14 unpipeSignal,15 } = getDestinationStream(boundOptions, args);16 const {sourceStream, sourceError} = getSourceStream(source, from);

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 27why, one step back
27 fileDescriptors,

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

line 28why, one step back
28 startTime,

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

line 29why, one step back
29 };

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 33why, one step back
33 try {

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

lines 34–39why, one step back
34 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 back
40 } catch (error) {
41 return {destinationError: error};42 }43};44

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

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

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

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

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

lines 47–48why, one step back
47 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 back
49 }
50

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

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

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

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

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

line 57why, one step back
57 const destination = execa(rawFile, rawArgs, {...rawOptions, ...PIPED_SUBPROCESS_OPTIONS});

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

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

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

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

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

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

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

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

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

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

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

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

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

line 71why, one step back
71

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

line 72why, one step back
72const PIPED_SUBPROCESS_OPTIONS = {stdin: 'pipe', piped: true};

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

line 73why, one step back
73

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

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

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

line 75why, one step back
75

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

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 81why, one step back
81

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 85why, one step back
85

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

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

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

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 103why, one step back
103

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

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

line 116why, one step back
116

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

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

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

line 154why, one step back
154 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 back
155 }

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 156–157why, one step back
156};
157

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

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

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

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

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

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 170why, one step back
170

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

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

line 184why, one step back
184};

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

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

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

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

Improve piping processes (#834)ehmickybefore adoption1a47ea2

lines 191–194why, one step back
191 }
192193 return typeof optionValue === 'number' ? `${optionValue}` : 'Stream';194};

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

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