handle-sync.js
This file
Path
lib/stdio/handle-sync.js
57 lines · 2.0 KiB · JavaScript
Last changed by
Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–24why, one step back1import {readFileSync} from 'node:fs';2import {bufferToUint8Array} from '../utils/uint-array.js';3import {handleStdio} from './handle.js';4import {TYPE_TO_MESSAGE} from './type.js';56// Normalize `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode7export const handleStdioSync = (options, verboseInfo) => handleStdio(addPropertiesSync, options, verboseInfo, true);89const forbiddenIfSync = ({type, optionName}) => {10 throwInvalidSyncValue(optionName, TYPE_TO_MESSAGE[type]);11};1213const forbiddenNativeIfSync = ({optionName, value}) => {14 if (value === 'ipc' || value === 'overlapped') {15 throwInvalidSyncValue(optionName, `"${value}"`);16 }1718 return {};19};2021const throwInvalidSyncValue = (optionName, value) => {22 throw new TypeError(`The \`${optionName}\` option cannot be ${value} with synchronous methods.`);23};24Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 25–26why, one step back25// Create streams used internally for redirecting when using specific values for the `std*` options, in sync mode.26// For example, `stdin: {file}` reads the file synchronously, then passes it as the `input` option.Rename files (#977)ehmickybefore adoption4b2e316
lines 27–42why, one step back27const addProperties = {28 generator() {},29 asyncGenerator: forbiddenIfSync,30 webStream: forbiddenIfSync,31 nodeStream: forbiddenIfSync,32 webTransform: forbiddenIfSync,33 duplex: forbiddenIfSync,34 asyncIterable: forbiddenIfSync,35 native: forbiddenNativeIfSync,36};3738const addPropertiesSync = {39 input: {40 ...addProperties,41 fileUrl: ({value}) => ({contents: [bufferToUint8Array(readFileSync(value))]}),42 filePath: ({value: {file}}) => ({contents: [bufferToUint8Array(readFileSync(file))]}),Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 43why, one step back43 fileNumber: forbiddenIfSync,Rename files (#977)ehmickybefore adoption4b2e316
lines 44–57why, one step back44 iterable: ({value}) => ({contents: [...value]}),45 string: ({value}) => ({contents: [value]}),46 uint8Array: ({value}) => ({contents: [value]}),47 },48 output: {49 ...addProperties,50 fileUrl: ({value}) => ({path: value}),51 filePath: ({value: {file}}) => ({path: file}),52 fileNumber: ({value}) => ({path: value}),53 iterable: forbiddenIfSync,54 string: forbiddenIfSync,55 uint8Array: forbiddenIfSync,56 },57};