Skip to content
Myr

pipe-arguments.js

This file

Path

lib/pipe/pipe-arguments.js

125 lines · 4.6 KiB · JavaScript

Last changed by

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–3why, one step back
1import {normalizeParameters} from '../methods/parameters.js';
2import {getStartTime} from '../return/duration.js';3import {SUBPROCESS_OPTIONS, getToStream, getFromStream} from '../arguments/fd-options.js';

Fix deno node:process execPath compatibility (#1160)CJbefore adoptionf3a1bf3

line 4why, one step back
4import {isDenoExecPath} from '../arguments/file-url.js';

TweaksSindre Sorhusbefore adoption698a1b8

line 5why, one step back
5import {copyOptions} from '../utils/options.js';

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 6why, one step back
6import {validateAbortSignal} from '../utils/abort-signal.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 7–20why, one step back
7
8// Normalize and validate arguments passed to `source.pipe(destination)`9export const normalizePipeArguments = ({source, sourcePromise, boundOptions, createNested}, ...pipeArguments) => {10 const startTime = getStartTime();11 const {12 destination,13 destinationStream,14 destinationError,15 from,16 unpipeSignal,17 } = getDestinationStream(boundOptions, createNested, pipeArguments);18 const {sourceStream, sourceError} = getSourceStream(source, from);19 const {options: sourceOptions, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);20 return {

Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d

line 21why, one step back
21 source,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 22–35why, one step back
22 sourcePromise,
23 sourceStream,24 sourceOptions,25 sourceError,26 destination,27 destinationStream,28 destinationError,29 unpipeSignal,30 fileDescriptors,31 startTime,32 };33};3435const getDestinationStream = (boundOptions, createNested, pipeArguments) => {

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 36–37why, one step back
36 let destinationInfo;
37

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 38why, one step back
38 try {

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 39–46why, one step back
39 destinationInfo = getDestination(boundOptions, createNested, ...pipeArguments);
40 } catch (error) {41 return {destinationError: error};42 }4344 const {destination, pipeOptions} = destinationInfo;4546 try {

Report `null` pipe options as a rejectionSindre Sorhusbefore adoptionecda56b

line 47why, one step back
47 const {from, to, unpipeSignal} = copyOptions(pipeOptions);

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 48why, one step back
48 validateAbortSignal(unpipeSignal, 'unpipeSignal');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 49–56why, one step back
49 const destinationStream = getToStream(destination, to);
50 return {51 destination,52 destinationStream,53 from,54 unpipeSignal,55 };56 } catch (error) {

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

line 57why, one step back
57 abortDestination(destination, pipeArguments[0]);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 58–61why, one step back
58 return {destinationError: error};
59 }60};61

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 62–64why, one step back
62/*
63The destination has already been spawned at that point, since the `to` option is validated afterwards.64When Execa spawned it, nothing else can end it, so it is terminated. Otherwise, it would keep running, e.g. waiting for its `stdin` forever, which also prevents the current process from exiting. A destination passed by the user is left to them.

TweaksSindre Sorhusbefore adoption698a1b8

line 65why, one step back
65In both cases, its error must be handled, otherwise it becomes an unhandled rejection which crashes the current process, e.g. with `source.pipe(execa('command'), {to: 'fd9'})`. This does not prevent the user from awaiting it.

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 66–74why, one step back
66*/
67const abortDestination = (destination, firstArgument) => {68 if (destination !== firstArgument) {69 destination.kill();70 }7172 destination.catch(() => {});73};74

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 75–84why, one step back
75// Piping subprocesses can use three syntaxes:
76// - source.pipe('command', commandArguments, pipeOptionsOrDestinationOptions)77// - source.pipe`command commandArgument` or source.pipe(pipeOptionsOrDestinationOptions)`command commandArgument`78// - source.pipe(execa(...), pipeOptions)79const getDestination = (boundOptions, createNested, firstArgument, ...pipeArguments) => {80 if (Array.isArray(firstArgument)) {81 const destination = createNested(mapDestinationArguments, boundOptions)(firstArgument, ...pipeArguments);82 return {destination, pipeOptions: boundOptions};83 }84

Fix deno node:process execPath compatibility (#1160)CJbefore adoptionf3a1bf3

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 86–106why, one step back
86 if (Object.keys(boundOptions).length > 0) {
87 throw new TypeError('Please use .pipe("file", ..., options) or .pipe(execa("file", ..., options)) instead of .pipe(options)("file", ...).');88 }8990 const [rawFile, rawArguments, rawOptions] = normalizeParameters(firstArgument, ...pipeArguments);91 const destination = createNested(mapDestinationArguments)(rawFile, rawArguments, rawOptions);92 return {destination, pipeOptions: rawOptions};93 }9495 if (SUBPROCESS_OPTIONS.has(firstArgument)) {96 if (Object.keys(boundOptions).length > 0) {97 throw new TypeError('Please use .pipe(options)`command` or .pipe($(options)`command`) instead of .pipe(options)($`command`).');98 }99100 return {destination: firstArgument, pipeOptions: pipeArguments[0]};101 }102103 throw new TypeError(`The first argument must be a template string, an options object, or an Execa subprocess: ${firstArgument}`);104};105106// Force `stdin: 'pipe'` with the destination subprocess

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 107–110why, one step back
107const mapDestinationArguments = ({options}) => ({
108 options: {109 __proto__: null,110 ...options,

Fix `.pipe()` with the `stdio` option and orphaned destination subprocessesSindre Sorhusbefore adoption8d3f0a9

lines 111–112why, one step back
111 // Piping writes to the destination's `stdin`, which is forced even if the user set another value.
112 // However, `stdin` cannot be combined with `stdio`, which is needed for `to: 'fd3'` and higher.

TweaksSindre Sorhusbefore adoption698a1b8

line 113why, one step back
113 ...((options.stdio === undefined) && {stdin: 'pipe'}),

Harden some thingsSindre Sorhusbefore adoption4a0a121

lines 114–116why, one step back
114 piped: true,
115 },116});

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 117–125why, one step back
117
118const getSourceStream = (source, from) => {119 try {120 const sourceStream = getFromStream(source, from);121 return {sourceStream};122 } catch (error) {123 return {sourceError: error};124 }125};