Skip to content
Myr

duplicate.js

This file

Path

lib/stdio/duplicate.js

116 lines · 3.9 KiB · JavaScript

Last changed by

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

History of this file

Take it

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

lines 1–116why, one step back
1import {
2 SPECIAL_DUPLICATE_TYPES_SYNC,3 SPECIAL_DUPLICATE_TYPES,4 FORBID_DUPLICATE_TYPES,5 TYPE_TO_MESSAGE,6} from './type.js';78// Duplicates in the same file descriptor is most likely an error.9// However, this can be useful with generators.10export const filterDuplicates = stdioItems => stdioItems.filter((stdioItemOne, indexOne) =>11 stdioItems.every((stdioItemTwo, indexTwo) => stdioItemOne.value !== stdioItemTwo.value12 || indexOne >= indexTwo13 || stdioItemOne.type === 'generator'14 || stdioItemOne.type === 'asyncGenerator'));1516// Check if two file descriptors are sharing the same target.17// For example `{stdout: {file: './output.txt'}, stderr: {file: './output.txt'}}`.18export const getDuplicateStream = ({stdioItem: {type, value, optionName}, direction, fileDescriptors, isSync}) => {19 const otherStdioItems = getOtherStdioItems(fileDescriptors, type);20 if (otherStdioItems.length === 0) {21 return;22 }2324 if (isSync) {25 validateDuplicateStreamSync({26 otherStdioItems,27 type,28 value,29 optionName,30 direction,31 });32 return;33 }3435 if (SPECIAL_DUPLICATE_TYPES.has(type)) {36 return getDuplicateStreamInstance({37 otherStdioItems,38 type,39 value,40 optionName,41 direction,42 });43 }4445 if (FORBID_DUPLICATE_TYPES.has(type)) {46 validateDuplicateTransform({47 otherStdioItems,48 type,49 value,50 optionName,51 });52 }53};5455// Values shared by multiple file descriptors56const getOtherStdioItems = (fileDescriptors, type) => fileDescriptors57 .flatMap(({direction, stdioItems}) => stdioItems58 .filter(stdioItem => stdioItem.type === type)59 .map((stdioItem => ({...stdioItem, direction}))));6061// With `execaSync()`, do not allow setting a file path both in input and output62const validateDuplicateStreamSync = ({otherStdioItems, type, value, optionName, direction}) => {63 if (SPECIAL_DUPLICATE_TYPES_SYNC.has(type)) {64 getDuplicateStreamInstance({65 otherStdioItems,66 type,67 value,68 optionName,69 direction,70 });71 }72};7374// When two file descriptors share the file or stream, we need to re-use the same underlying stream.75// Otherwise, the stream would be closed twice when piping ends.76// This is only an issue with output file descriptors.77// This is not a problem with generator functions since those create a new instance for each file descriptor.78// We also forbid input and output file descriptors sharing the same file or stream, since that does not make sense.79const getDuplicateStreamInstance = ({otherStdioItems, type, value, optionName, direction}) => {80 const duplicateStdioItems = otherStdioItems.filter(stdioItem => hasSameValue(stdioItem, value));81 if (duplicateStdioItems.length === 0) {82 return;83 }8485 const differentStdioItem = duplicateStdioItems.find(stdioItem => stdioItem.direction !== direction);86 throwOnDuplicateStream(differentStdioItem, optionName, type);8788 return direction === 'output' ? duplicateStdioItems[0].stream : undefined;89};9091const hasSameValue = ({type, value}, secondValue) => {92 if (type === 'filePath') {93 return value.path === secondValue.path;94 }9596 if (type === 'fileUrl') {97 return value.href === secondValue.href;98 }99100 return value === secondValue;101};102103// We do not allow two file descriptors to share the same Duplex or TransformStream.104// This is because those are set directly to `subprocess.std*`.105// For example, this could result in `subprocess.stdout` and `subprocess.stderr` being the same value.106// This means reading from either would get data from both stdout and stderr.107const validateDuplicateTransform = ({otherStdioItems, type, value, optionName}) => {108 const duplicateStdioItem = otherStdioItems.find(({value: {transform}}) => transform === value.transform);109 throwOnDuplicateStream(duplicateStdioItem, optionName, type);110};111112const throwOnDuplicateStream = (stdioItem, optionName, type) => {113 if (stdioItem !== undefined) {114 throw new TypeError(`The \`${stdioItem.optionName}\` and \`${optionName}\` options must not target ${TYPE_TO_MESSAGE[type]} that is the same.`);115 }116};