Skip to content
Myr

sync.js

This file

Path

lib/stdio/sync.js

81 lines · 2.5 KiB · JavaScript

Last changed by

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

History of this file

Take it

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4

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

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

lines 4–6why, one step back
4import {TYPE_TO_MESSAGE} from './type.js';
56// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync mode

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

line 7why, one step back
7export const handleInputSync = (options, verboseInfo) => {

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 8–10why, one step back
8 const {fileDescriptors} = handleInput(addPropertiesSync, options, verboseInfo, true);
9 addInputOptionSync(fileDescriptors, options);10 return fileDescriptors;

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 11–16why, one step back
11};
1213const forbiddenIfSync = ({type, optionName}) => {14 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]} in sync mode.`);15};16

Refactor tests (#934)ehmickybefore adoption52c4bde

lines 17–20why, one step back
17const addProperties = {
18 generator: forbiddenIfSync,19 webStream: forbiddenIfSync,20 nodeStream: forbiddenIfSync,

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 21why, one step back
21 duplex: forbiddenIfSync,

Refactor tests (#934)ehmickybefore adoption52c4bde

lines 22–25why, one step back
22 iterable: forbiddenIfSync,
23 native() {},24};25

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 26–27why, one step back
26const addPropertiesSync = {
27 input: {

Refactor tests (#934)ehmickybefore adoption52c4bde

line 28why, one step back
28 ...addProperties,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 29–32why, one step back
29 fileUrl: ({value}) => ({contents: bufferToUint8Array(readFileSync(value))}),
30 filePath: ({value: {file}}) => ({contents: bufferToUint8Array(readFileSync(file))}),31 string: ({value}) => ({contents: value}),32 uint8Array: ({value}) => ({contents: value}),

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

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

Refactor tests (#934)ehmickybefore adoption52c4bde

line 35why, one step back
35 ...addProperties,

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 36–38why, one step back
36 fileUrl: ({value}) => ({path: value}),
37 filePath: ({value: {file}}) => ({path: file}),38 string: forbiddenIfSync,

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

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

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 40–42why, one step back
40 },
41};42

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 43–49why, one step back
43const addInputOptionSync = (fileDescriptors, options) => {
44 const allContents = fileDescriptors45 .filter(({direction}) => direction === 'input')46 .flatMap(({stdioItems}) => stdioItems)47 .map(({contents}) => contents)48 .filter(contents => contents !== undefined);49 if (allContents.length === 0) {

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

line 50why, one step back
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

line 52why, one step back
52

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 53–55why, one step back
53 options.input = allContents.length === 1
54 ? allContents[0]55 : allContents.map(contents => serializeContents(contents)).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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 58why, one step back
58const serializeContents = contents => typeof contents === 'string' ? contents : uint8ArrayToString(contents);

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

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 61why, one step back
61export const pipeOutputSync = (fileDescriptors, {output}) => {

Refactor `reject` option (#868)ehmickybefore adoption1f8a55c

line 62why, one step back
62 if (output === null) {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 63–65why, one step back
63 return;
64 }65

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

lines 66–68why, one step back
66 for (const {stdioItems, fdNumber, direction} of fileDescriptors) {
67 for (const {type, path} of stdioItems) {68 pipeStdioItemSync(output[fdNumber], type, path, direction);

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

line 69why, one step back
69 }

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 70–72why, one step back
70 }
71};72

Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d

line 73why, one step back
73const pipeStdioItemSync = (result, type, path, direction) => {

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 74–77why, one step back
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` option (#927)ehmickybefore adoptionc9cdd2d

line 79why, one step back
79 writeFileSync(path, result);

Refactor `stdio` options (#623)ehmickybefore adoption2d76897

lines 80–81why, one step back
80 }
81};