Skip to content
Myr

sync.js

This file

Path

lib/stdio/sync.js

79 lines · 2.4 KiB · JavaScript

Last changed by

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

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 => {

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

lines 9–11why, one step back
9 const stdioStreamsGroups = handleInput(addPropertiesSync, options);
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: {

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Fix `stdio` option (#634)ehmickybefore adoption640ae39

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 37–39why, one step back
37 webStream: forbiddenIfSync,
38 nodeStream: forbiddenIfSync,39 iterable: forbiddenIfSync,

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

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

Fix `stdio` option (#634)ehmickybefore adoption640ae39

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 42–44why, one step back
42 },
43};44

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

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

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 49why, one step back
49 }

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

lines 50–52why, one step back
50
51 options.input = inputs.length === 152 ? inputs[0].value

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

line 54why, one step back
54};

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

line 55why, one step back
55

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 68–75why, one step back
68 }
69};7071const pipeStdioOptionSync = (result, {type, value, direction}) => {72 if (result === null || direction === 'input') {73 return;74 }75

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 77–79why, one step back
77 writeFileSync(value, result);
78 }79};