handle-sync.js
This file
Path
lib/stdio/handle-sync.js
70 lines · 2.7 KiB · JavaScript
Last changed by
Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–13why, 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}) => {Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c
line 14why, one step back14 if (value === 'overlapped') {Rename files (#977)ehmickybefore adoption4b2e316
lines 15–20why, one step back15 throwInvalidSyncValue(optionName, `"${value}"`);16 }1718 return {};19};20Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
lines 21–30why, one step back21// A native input pipe on an additional file descriptor cannot be fed in sync mode, since there is no way to write to it after spawning, so it would hang.22const forbiddenNativeInputIfSync = stdioItem => {23 const {optionName, value} = stdioItem;24 if (value === 'pipe' && optionName !== 'stdin') {25 throw new TypeError(`Only the \`stdin\` option, not \`${optionName}\`, can be an input pipe with synchronous methods.`);26 }2728 return forbiddenNativeIfSync(stdioItem);29};30Rename files (#977)ehmickybefore adoption4b2e316
lines 31–34why, one step back31const throwInvalidSyncValue = (optionName, value) => {32 throw new TypeError(`The \`${optionName}\` option cannot be ${value} with synchronous methods.`);33};34Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 35–36why, one step back35// Create streams used internally for redirecting when using specific values for the `std*` options, in sync mode.36// For example, `stdin: {file}` reads the file synchronously, then passes it as the `input` option.Rename files (#977)ehmickybefore adoption4b2e316
lines 37–50why, one step back37const addProperties = {38 generator() {},39 asyncGenerator: forbiddenIfSync,40 webStream: forbiddenIfSync,41 nodeStream: forbiddenIfSync,42 webTransform: forbiddenIfSync,43 duplex: forbiddenIfSync,44 asyncIterable: forbiddenIfSync,45 native: forbiddenNativeIfSync,46};4748const addPropertiesSync = {49 input: {50 ...addProperties,Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4
line 51why, one step back51 native: forbiddenNativeInputIfSync,Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 52–54why, one step back52 // Files are read lazily, so that a file which cannot be read is reported like any other early error, instead of being thrown as a raw error53 fileUrl: ({value}) => ({contents: () => [bufferToUint8Array(readFileSync(value))]}),54 filePath: ({value: {file}}) => ({contents: () => [bufferToUint8Array(readFileSync(file))]}),Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 55why, one step back55 fileNumber: forbiddenIfSync,Rename files (#977)ehmickybefore adoption4b2e316
lines 56–57why, one step back56 iterable: ({value}) => ({contents: [...value]}),57 string: ({value}) => ({contents: [value]}),Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 58–59why, one step back58 // `contents` is already set when reading from a file descriptor59 uint8Array: ({value, contents = [value]}) => ({contents}),Rename files (#977)ehmickybefore adoption4b2e316
lines 60–63why, one step back60 },61 output: {62 ...addProperties,63 fileUrl: ({value}) => ({path: value}),Allow appending to files (#1166)ehmickybefore adoption94a75df
line 64why, one step back64 filePath: ({value: {file, append}}) => ({path: file, append}),Rename files (#977)ehmickybefore adoption4b2e316
lines 65–70why, one step back65 fileNumber: ({value}) => ({path: value}),66 iterable: forbiddenIfSync,67 string: forbiddenIfSync,68 uint8Array: forbiddenIfSync,69 },70};