Skip to content
Myr

sync.js

This file

Path

lib/stdio/sync.js

81 lines · 2.5 KiB · JavaScript

Last changed by

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

History of this file

Take it

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 1why, one step back
1import {readFileSync, writeFileSync} from 'node:fs';

Fix `stdio` option (#634)ehmickybefore adoption640ae39

line 2why, one step back
2import {isStream as isNodeStream} from 'is-stream';

Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297

line 3why, one step back
3import {handleInput} from './handle.js';

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 4why, one step back
4import {TYPE_TO_MESSAGE} from './type.js';

Validate `input` option's type (#668)ehmickybefore adoption65742c6

line 5why, one step back
5import {bufferToUint8Array} from './utils.js';

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 6–8why, one step back
6
7// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode8export const handleInputSync = options => {

Fix `encoding` option (#696)ehmickybefore adoptionb6f9fa2

line 9why, one step back
9 const stdioStreamsGroups = handleInput(addPropertiesSync, options, true);

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

lines 10–11why, one step back
10 addInputOptionSync(stdioStreamsGroups, options);
11 return stdioStreamsGroups;

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 12–13why, one step back
12};
13

Fix `stdio` option (#634)ehmickybefore adoption640ae39

lines 14–17why, one step back
14const forbiddenIfStreamSync = ({value, optionName}) => {
15 if (isNodeStream(value)) {16 forbiddenIfSync({type: 'nodeStream', optionName});17 }

Refactor `stdio` option logic (#639)ehmickybefore adoptiond6b4553

lines 18–19why, one step back
18
19 return {};

Fix `stdio` option (#634)ehmickybefore adoption640ae39

lines 20–21why, one step back
20};
21

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 22–27why, one step back
22const forbiddenIfSync = ({type, optionName}) => {
23 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]} in sync mode.`);24};2526const addPropertiesSync = {27 input: {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 28why, one step back
28 generator: forbiddenIfSync,

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

lines 29–30why, one step back
29 fileUrl: ({value}) => ({value: bufferToUint8Array(readFileSync(value)), type: 'uint8Array'}),
30 filePath: ({value}) => ({value: bufferToUint8Array(readFileSync(value.file)), type: 'uint8Array'}),

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 31–33why, one step back
31 webStream: forbiddenIfSync,
32 nodeStream: forbiddenIfSync,33 iterable: forbiddenIfSync,

Fix `stdio` option (#634)ehmickybefore adoption640ae39

line 34why, one step back
34 native: forbiddenIfStreamSync,

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 35–36why, one step back
35 },
36 output: {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 37why, one step back
37 generator: forbiddenIfSync,

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

line 38why, one step back
38 filePath: ({value}) => ({value: value.file}),

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 39–41why, one step back
39 webStream: forbiddenIfSync,
40 nodeStream: forbiddenIfSync,41 iterable: forbiddenIfSync,

Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4

line 42why, one step back
42 uint8Array: forbiddenIfSync,

Fix `stdio` option (#634)ehmickybefore adoption640ae39

line 43why, one step back
43 native: forbiddenIfStreamSync,

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 44–46why, one step back
44 },
45};46

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

lines 47–48why, one step back
47const addInputOptionSync = (stdioStreamsGroups, options) => {
48 const inputs = stdioStreamsGroups.flat().filter(({type}) => type === 'string' || type === 'uint8Array');

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 49–50why, one step back
49 if (inputs.length === 0) {
50 return;

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 51why, one step back
51 }

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

lines 52–54why, one step back
52
53 options.input = inputs.length === 154 ? inputs[0].value

Validate `input` option's type (#668)ehmickybefore adoption65742c6

line 55why, one step back
55 : inputs.map(stdioStream => serializeInput(stdioStream)).join('');

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 56why, one step back
56};

Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25

line 57why, one step back
57

Validate `input` option's type (#668)ehmickybefore adoption65742c6

line 58why, one step back
58const serializeInput = ({type, value}) => type === 'string' ? value : new TextDecoder().decode(value);

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 59–60why, one step back
59
60// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

line 61why, one step back
61export const pipeOutputSync = (stdioStreamsGroups, result) => {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 62–65why, one step back
62 if (result.output === null) {
63 return;64 }65

Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3

lines 66–69why, one step back
66 for (const stdioStreams of stdioStreamsGroups) {
67 for (const stdioStream of stdioStreams) {68 pipeStdioOptionSync(result.output[stdioStream.index], stdioStream);69 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 70–77why, one step back
70 }
71};7273const pipeStdioOptionSync = (result, {type, value, direction}) => {74 if (result === null || direction === 'input') {75 return;76 }77

Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13

line 78why, one step back
78 if (type === 'fileUrl' || type === 'filePath') {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 79–81why, one step back
79 writeFileSync(value, result);
80 }81};