duplicate.js
This file
Path
lib/stdio/duplicate.js
129 lines · 5.2 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import path from 'node:path';Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
line 2why, one step back2import {fileURLToPath} from 'node:url';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 3–9why, one step back3import {4 SPECIAL_DUPLICATE_TYPES_SYNC,5 SPECIAL_DUPLICATE_TYPES,6 FORBID_DUPLICATE_TYPES,7 TYPE_TO_MESSAGE,8} from './type.js';9Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
lines 10–14why, one step back10// `{file: './output.txt'}` and a `file:` URL are two ways to target the same file, so both must be compared together11const isFileType = type => SPECIAL_DUPLICATE_TYPES_SYNC.has(type);1213// The file that a `filePath` or `fileUrl` stdio item targets, as an absolute path14export const getFileTarget = ({type, value}) => type === 'filePath'TweaksSindre Sorhusbefore adoption698a1b8
line 15why, one step back15 ? path.resolve(value.file)Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
lines 16–17why, one step back16 : fileURLToPath(value);17100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 18–20why, one step back18// Duplicates in the same file descriptor is most likely an error.19// However, this can be useful with generators.20export const filterDuplicates = stdioItems => stdioItems.filter((stdioItemOne, indexOne) =>Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 21why, one step back21 stdioItems.every((stdioItemTwo, indexTwo) => !hasSameValueAndDirection(stdioItemOne, stdioItemTwo)100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 22–24why, one step back22 || indexOne >= indexTwo23 || stdioItemOne.type === 'generator'24 || stdioItemOne.type === 'asyncGenerator'));Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 25–27why, one step back2526const hasSameValueAndDirection = (stdioItemOne, stdioItemTwo) => stdioItemOne.value === stdioItemTwo.value27 && stdioItemOne.direction === stdioItemTwo.direction;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 28–30why, one step back2829// Check if two file descriptors are sharing the same target.30// For example `{stdout: {file: './output.txt'}, stderr: {file: './output.txt'}}`.Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
line 31why, one step back31// The same applies to two items of a single file descriptor, e.g. `{stdout: [{file: './output.txt'}, {file: './output.txt'}]}`.100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 32–68why, one step back32export const getDuplicateStream = ({stdioItem: {type, value, optionName}, direction, fileDescriptors, isSync}) => {33 const otherStdioItems = getOtherStdioItems(fileDescriptors, type);34 if (otherStdioItems.length === 0) {35 return;36 }3738 if (isSync) {39 validateDuplicateStreamSync({40 otherStdioItems,41 type,42 value,43 optionName,44 direction,45 });46 return;47 }4849 if (SPECIAL_DUPLICATE_TYPES.has(type)) {50 return getDuplicateStreamInstance({51 otherStdioItems,52 type,53 value,54 optionName,55 direction,56 });57 }5859 if (FORBID_DUPLICATE_TYPES.has(type)) {60 validateDuplicateTransform({61 otherStdioItems,62 type,63 value,64 optionName,65 });66 }67};68Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
line 69why, one step back69// Values shared by multiple file descriptors, including the other items of the file descriptor currently being built100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 70–71why, one step back70const getOtherStdioItems = (fileDescriptors, type) => fileDescriptors71 .flatMap(({direction, stdioItems}) => stdioItemsFix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
lines 72–74why, one step back72 .map(stdioItem => ({...stdioItem, direction})),73 )74 .filter(stdioItem => isSameDuplicateType(stdioItem.type, type));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 75why, one step back75Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
lines 76–78why, one step back76// A `filePath` and a `fileUrl` are compared together, since they can target the same file77const isSameDuplicateType = (otherType, type) => otherType === type || (isFileType(otherType) && isFileType(type));78100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 79–97why, one step back79// With `execaSync()`, do not allow setting a file path both in input and output80const validateDuplicateStreamSync = ({otherStdioItems, type, value, optionName, direction}) => {81 if (SPECIAL_DUPLICATE_TYPES_SYNC.has(type)) {82 getDuplicateStreamInstance({83 otherStdioItems,84 type,85 value,86 optionName,87 direction,88 });89 }90};9192// When two file descriptors share the file or stream, we need to re-use the same underlying stream.93// Otherwise, the stream would be closed twice when piping ends.94// This is only an issue with output file descriptors.95// This is not a problem with generator functions since those create a new instance for each file descriptor.96// We also forbid input and output file descriptors sharing the same file or stream, since that does not make sense.97const getDuplicateStreamInstance = ({otherStdioItems, type, value, optionName, direction}) => {Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
line 98why, one step back98 const duplicateStdioItems = otherStdioItems.filter(stdioItem => hasSameValue(stdioItem, type, value));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 99–105why, one step back99 if (duplicateStdioItems.length === 0) {100 return;101 }102103 const differentStdioItem = duplicateStdioItems.find(stdioItem => stdioItem.direction !== direction);104 throwOnDuplicateStream(differentStdioItem, optionName, type);105Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
lines 106–108why, one step back106 // Output streams must be re-used so they are not closed twice.107 // Web streams must be re-used in the input direction too, since they cannot be converted twice: reading one locks it.108 return direction === 'output' || type === 'webStream' ? duplicateStdioItems[0].stream : undefined;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 109–110why, one step back109};110Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
lines 111–114why, one step back111// `type` and `value` are the current item's, `otherStdioItem` is the other one112const hasSameValue = (otherStdioItem, type, value) => isFileType(otherStdioItem.type)113 ? getFileTarget(otherStdioItem) === getFileTarget({type, value})114 : otherStdioItem.value === value;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 115–129why, one step back115116// We do not allow two file descriptors to share the same Duplex or TransformStream.117// This is because those are set directly to `subprocess.std*`.118// For example, this could result in `subprocess.stdout` and `subprocess.stderr` being the same value.119// This means reading from either would get data from both stdout and stderr.120const validateDuplicateTransform = ({otherStdioItems, type, value, optionName}) => {121 const duplicateStdioItem = otherStdioItems.find(({value: {transform}}) => transform === value.transform);122 throwOnDuplicateStream(duplicateStdioItem, optionName, type);123};124125const throwOnDuplicateStream = (stdioItem, optionName, type) => {126 if (stdioItem !== undefined) {127 throw new TypeError(`The \`${stdioItem.optionName}\` and \`${optionName}\` options must not target ${TYPE_TO_MESSAGE[type]} that is the same.`);128 }129};