Skip to content
Myr

handle-sync.js

This file

Path

lib/stdio/handle-sync.js

56 lines · 1.9 KiB · JavaScript

Last changed by

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–24why, one step back
1import {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};24

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 25–26why, one step back
25// 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–56why, one step back
27const 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))]}),43 iterable: ({value}) => ({contents: [...value]}),44 string: ({value}) => ({contents: [value]}),45 uint8Array: ({value}) => ({contents: [value]}),46 },47 output: {48 ...addProperties,49 fileUrl: ({value}) => ({path: value}),50 filePath: ({value: {file}}) => ({path: file}),51 fileNumber: ({value}) => ({path: value}),52 iterable: forbiddenIfSync,53 string: forbiddenIfSync,54 uint8Array: forbiddenIfSync,55 },56};