Skip to content
Myr

fd-options.js

This file

Path

lib/arguments/fd-options.js

105 lines · 4.0 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

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–60why, one step back
1import {parseFd} from './specific.js';
23// Retrieve stream targeted by the `to` option4export const getToStream = (destination, to = 'stdin') => {5 const isWritable = true;6 const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(destination);7 const fdNumber = getFdNumber(fileDescriptors, to, isWritable);8 const destinationStream = destination.stdio[fdNumber];910 if (destinationStream === null) {11 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, to, options, isWritable));12 }1314 return destinationStream;15};1617// Retrieve stream targeted by the `from` option18export const getFromStream = (source, from = 'stdout') => {19 const isWritable = false;20 const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);21 const fdNumber = getFdNumber(fileDescriptors, from, isWritable);22 const sourceStream = fdNumber === 'all' ? source.all : source.stdio[fdNumber];2324 if (sourceStream === null || sourceStream === undefined) {25 throw new TypeError(getInvalidStdioOptionMessage(fdNumber, from, options, isWritable));26 }2728 return sourceStream;29};3031// Keeps track of the options passed to each Execa call32export const SUBPROCESS_OPTIONS = new WeakMap();3334const getFdNumber = (fileDescriptors, fdName, isWritable) => {35 const fdNumber = parseFdNumber(fdName, isWritable);36 validateFdNumber(fdNumber, fdName, isWritable, fileDescriptors);37 return fdNumber;38};3940const parseFdNumber = (fdName, isWritable) => {41 const fdNumber = parseFd(fdName);42 if (fdNumber !== undefined) {43 return fdNumber;44 }4546 const {validOptions, defaultValue} = isWritable47 ? {validOptions: '"stdin"', defaultValue: 'stdin'}48 : {validOptions: '"stdout", "stderr", "all"', defaultValue: 'stdout'};49 throw new TypeError(`"${getOptionName(isWritable)}" must not be "${fdName}".50It must be ${validOptions} or "fd3", "fd4" (and so on).51It is optional and defaults to "${defaultValue}".`);52};5354const validateFdNumber = (fdNumber, fdName, isWritable, fileDescriptors) => {55 const fileDescriptor = fileDescriptors[getUsedDescriptor(fdNumber)];56 if (fileDescriptor === undefined) {57 throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. That file descriptor does not exist.58Please set the "stdio" option to ensure that file descriptor exists.`);59 }60

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 62–64why, one step back
62 throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a readable stream, not writable.`);
63 }64

TweaksSindre Sorhusbefore adoption698a1b8

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

Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4

lines 66–67why, one step back
66 throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a writable stream, not readable.
67If you meant to use it as input, please set its "stdio" option to \`{value: 'pipe', input: true}\`.`);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 68–91why, one step back
68 }
69};7071const getInvalidStdioOptionMessage = (fdNumber, fdName, options, isWritable) => {72 if (fdNumber === 'all' && !options.all) {73 return 'The "all" option must be true to use "from: \'all\'".';74 }7576 const {optionName, optionValue} = getInvalidStdioOption(fdNumber, options);77 return `The "${optionName}: ${serializeOptionValue(optionValue)}" option is incompatible with using "${getOptionName(isWritable)}: ${serializeOptionValue(fdName)}".78Please set this option with "pipe" instead.`;79};8081const getInvalidStdioOption = (fdNumber, {stdin, stdout, stderr, stdio}) => {82 const usedDescriptor = getUsedDescriptor(fdNumber);8384 if (usedDescriptor === 0 && stdin !== undefined) {85 return {optionName: 'stdin', optionValue: stdin};86 }8788 if (usedDescriptor === 1 && stdout !== undefined) {89 return {optionName: 'stdout', optionValue: stdout};90 }91

TweaksSindre Sorhusbefore adoption698a1b8

line 92why, one step back
92 return usedDescriptor === 2 && stderr !== undefined ? {optionName: 'stderr', optionValue: stderr} : {optionName: `stdio[${usedDescriptor}]`, optionValue: stdio[usedDescriptor]};

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 93–105why, one step back
93};
9495const getUsedDescriptor = fdNumber => fdNumber === 'all' ? 1 : fdNumber;9697const getOptionName = isWritable => isWritable ? 'to' : 'from';9899export const serializeOptionValue = value => {100 if (typeof value === 'string') {101 return `'${value}'`;102 }103104 return typeof value === 'number' ? `${value}` : 'Stream';105};