validate.js
This file
Path
lib/pipe/validate.js
140 lines · 4.8 KiB · JavaScript
Last changed by
Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa
History of this fileTake it
`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
line 1why, one step back1import {STANDARD_STREAMS_ALIASES, isExecaChildProcess} from '../utils.js';Split files (#837)ehmickybefore adoption672c2d7
line 2why, one step back2import {makeEarlyError} from '../return/error.js';Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 3–4why, one step back3import {abortSourceStream, endDestinationStream} from './streaming.js';4Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa
line 5why, one step back5export const normalizePipeArguments = (destination, from, {source, sourcePromise, stdioStreamsGroups, options}) => {Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 6–8why, one step back6 const {destinationStream, destinationError} = getDestinationStream(destination);7 const {sourceStream, sourceError} = getSourceStream(source, stdioStreamsGroups, from, options);8 handlePipeArgumentsError({sourceStream, sourceError, destinationStream, destinationError, stdioStreamsGroups, options});Fix failure of series of `.pipe()` (#849)ehmickybefore adoption45744fa
line 9why, one step back9 return {source, sourcePromise, sourceStream, destinationStream};Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 10–14why, one step back10};1112const getDestinationStream = destination => {13 try {14 if (!isExecaChildProcess(destination)) {Improve spawning errors (#847)ehmickybefore adoption381dee2
line 15why, one step back15 throw new TypeError(`The first argument must be an Execa child process: ${destination}`);Improve piping processes (#834)ehmickybefore adoption1a47ea2
lines 16–140why, one step back16 }1718 const destinationStream = destination.stdin;19 if (destinationStream === null) {20 throw new TypeError('The destination child process\'s stdin must be available. Please set its "stdin" option to "pipe".');21 }2223 return {destinationStream};24 } catch (error) {25 return {destinationError: error};26 }27};2829const getSourceStream = (source, stdioStreamsGroups, from, options) => {30 try {31 const streamIndex = getStreamIndex(stdioStreamsGroups, from);32 const sourceStream = streamIndex === 'all' ? source.all : source.stdio[streamIndex];3334 if (sourceStream === null || sourceStream === undefined) {35 throw new TypeError(getInvalidStdioOptionMessage(streamIndex, from, options));36 }3738 return {sourceStream};39 } catch (error) {40 return {sourceError: error};41 }42};4344const getStreamIndex = (stdioStreamsGroups, from = 'stdout') => {45 const streamIndex = STANDARD_STREAMS_ALIASES.includes(from)46 ? STANDARD_STREAMS_ALIASES.indexOf(from)47 : from;4849 if (streamIndex === 'all') {50 return streamIndex;51 }5253 if (streamIndex === 0) {54 throw new TypeError('The "from" option must not be "stdin".');55 }5657 if (!Number.isInteger(streamIndex) || streamIndex < 0) {58 throw new TypeError(`The "from" option must not be "${streamIndex}".59It must be "stdout", "stderr", "all" or a file descriptor integer.60It is optional and defaults to "stdout".`);61 }6263 const stdioStreams = stdioStreamsGroups[streamIndex];64 if (stdioStreams === undefined) {65 throw new TypeError(`The "from" option must not be ${streamIndex}. That file descriptor does not exist.66Please set the "stdio" option to ensure that file descriptor exists.`);67 }6869 if (stdioStreams[0].direction === 'input') {70 throw new TypeError(`The "from" option must not be ${streamIndex}. It must be a readable stream, not writable.`);71 }7273 return streamIndex;74};7576const getInvalidStdioOptionMessage = (streamIndex, from, options) => {77 if (streamIndex === 'all' && !options.all) {78 return 'The "all" option must be true to use `childProcess.pipe(destinationProcess, {from: "all"})`.';79 }8081 const {optionName, optionValue} = getInvalidStdioOption(streamIndex, options);82 const pipeOptions = from === undefined ? '' : `, {from: ${serializeOptionValue(from)}}`;83 return `The \`${optionName}: ${serializeOptionValue(optionValue)}\` option is incompatible with using \`childProcess.pipe(destinationProcess${pipeOptions})\`.84Please set this option with "pipe" instead.`;85};8687const getInvalidStdioOption = (streamIndex, {stdout, stderr, stdio}) => {88 const usedIndex = streamIndex === 'all' ? 1 : streamIndex;8990 if (usedIndex === 1 && stdout !== undefined) {91 return {optionName: 'stdout', optionValue: stdout};92 }9394 if (usedIndex === 2 && stderr !== undefined) {95 return {optionName: 'stderr', optionValue: stderr};96 }9798 return {optionName: `stdio[${usedIndex}]`, optionValue: stdio[usedIndex]};99};100101const serializeOptionValue = optionValue => {102 if (typeof optionValue === 'string') {103 return `"${optionValue}"`;104 }105106 return typeof optionValue === 'number' ? `${optionValue}` : 'Stream';107};108109const handlePipeArgumentsError = ({sourceStream, sourceError, destinationStream, destinationError, stdioStreamsGroups, options}) => {110 const error = getPipeArgumentsError({sourceStream, sourceError, destinationStream, destinationError});111 if (error !== undefined) {112 throw createNonCommandError({error, stdioStreamsGroups, options});113 }114};115116const getPipeArgumentsError = ({sourceStream, sourceError, destinationStream, destinationError}) => {117 if (sourceError !== undefined && destinationError !== undefined) {118 return destinationError;119 }120121 if (destinationError !== undefined) {122 abortSourceStream(sourceStream);123 return destinationError;124 }125126 if (sourceError !== undefined) {127 endDestinationStream(destinationStream);128 return sourceError;129 }130};131132export const createNonCommandError = ({error, stdioStreamsGroups, options}) => makeEarlyError({133 error,134 command: PIPE_COMMAND_MESSAGE,135 escapedCommand: PIPE_COMMAND_MESSAGE,136 stdioStreamsGroups,137 options,138});139140const PIPE_COMMAND_MESSAGE = 'source.pipe(destination)';