Skip to content
Myr

handle-sync.js

This file

Path

lib/stdio/handle-sync.js

78 lines · 3.1 KiB · JavaScript

Last changed by

Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–13why, 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}) => {

Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c

line 14why, one step back
14 if (value === 'overlapped') {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 15–20why, one step back
15 throwInvalidSyncValue(optionName, `"${value}"`);
16 }1718 return {};19};20

Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4

lines 21–30why, one step back
21// 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};30

Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3

lines 31–37why, one step back
31// For the same reason, an input transform can only be used with `stdin`, which is fed with the `input` option
32const forbiddenInputTransformIfSync = ({optionName}) => {33 if (optionName !== 'stdin') {34 throw new TypeError(`Only the \`stdin\` option, not \`${optionName}\`, can be an input transform with synchronous methods.`);35 }36};37

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 38–57why, one step back
38const throwInvalidSyncValue = (optionName, value) => {
39 throw new TypeError(`The \`${optionName}\` option cannot be ${value} with synchronous methods.`);40};4142// Create streams used internally for redirecting when using specific values for the `std*` options, in sync mode.43// For example, `stdin: {file}` reads the file synchronously, then passes it as the `input` option.44const addProperties = {45 generator() {},46 asyncGenerator: forbiddenIfSync,47 webStream: forbiddenIfSync,48 nodeStream: forbiddenIfSync,49 webTransform: forbiddenIfSync,50 duplex: forbiddenIfSync,51 asyncIterable: forbiddenIfSync,52 native: forbiddenNativeIfSync,53};5455const addPropertiesSync = {56 input: {57 ...addProperties,

Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3

line 58why, one step back
58 generator: forbiddenInputTransformIfSync,

Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4

line 59why, one step back
59 native: forbiddenNativeInputIfSync,

Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3

line 60why, one step back
60 // Files and iterables are read lazily, so that a file which cannot be read, or an iterable which throws, is reported like any other early error, instead of being thrown as a raw error

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 61–62why, one step back
61 fileUrl: ({value}) => ({contents: () => [bufferToUint8Array(readFileSync(value))]}),
62 filePath: ({value: {file}}) => ({contents: () => [bufferToUint8Array(readFileSync(file))]}),

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 63why, one step back
63 fileNumber: forbiddenIfSync,

Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3

line 64why, one step back
64 iterable: ({value}) => ({contents: () => [...value]}),

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 65why, one step back
65 string: ({value}) => ({contents: [value]}),

Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2

lines 66–67why, one step back
66 // `contents` is already set when reading from a file descriptor
67 uint8Array: ({value, contents = [value]}) => ({contents}),

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 68–71why, one step back
68 },
69 output: {70 ...addProperties,71 fileUrl: ({value}) => ({path: value}),

Allow appending to files (#1166)ehmickybefore adoption94a75df

line 72why, one step back
72 filePath: ({value: {file, append}}) => ({path: file, append}),

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 73–78why, one step back
73 fileNumber: ({value}) => ({path: value}),
74 iterable: forbiddenIfSync,75 string: forbiddenIfSync,76 uint8Array: forbiddenIfSync,77 },78};