sync.js
This file
Path
lib/stdio/sync.js
80 lines · 2.5 KiB · JavaScript
Last changed by
Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4
History of this fileTake it
Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 1why, one step back1import {readFileSync, writeFileSync} from 'node:fs';Stricter values for the `encoding` option (#928)ehmickybefore adoption477bbc4
line 2why, one step back2import {bufferToUint8Array, uint8ArrayToString} from '../utils.js';Split `input`/`inputFile` options to their own file (#629)ehmickybefore adoption0b8e297
line 3why, one step back3import {handleInput} from './handle.js';Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 4–6why, one step back4import {TYPE_TO_MESSAGE} from './type.js';56// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in sync modePrint output with `verbose` option (#884)ehmickybefore adoption6fda30f
line 7why, one step back7export const handleInputSync = (options, verboseInfo) => {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 8–10why, one step back8 const {fileDescriptors} = handleInput(addPropertiesSync, options, verboseInfo, true);9 addInputOptionSync(fileDescriptors, options);10 return fileDescriptors;Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 11–18why, one step back11};1213const forbiddenIfSync = ({type, optionName}) => {14 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]} in sync mode.`);15};1617const addPropertiesSync = {18 input: {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 19why, one step back19 generator: forbiddenIfSync,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 20–21why, one step back20 fileUrl: ({value}) => ({contents: bufferToUint8Array(readFileSync(value))}),21 filePath: ({value: {file}}) => ({contents: bufferToUint8Array(readFileSync(file))}),Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 22–24why, one step back22 webStream: forbiddenIfSync,23 nodeStream: forbiddenIfSync,24 iterable: forbiddenIfSync,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 25–27why, one step back25 string: ({value}) => ({contents: value}),26 uint8Array: ({value}) => ({contents: value}),27 native() {},Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 28–29why, one step back28 },29 output: {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 30why, one step back30 generator: forbiddenIfSync,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 31–32why, one step back31 fileUrl: ({value}) => ({path: value}),32 filePath: ({value: {file}}) => ({path: file}),Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 33–35why, one step back33 webStream: forbiddenIfSync,34 nodeStream: forbiddenIfSync,35 iterable: forbiddenIfSync,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 36why, one step back36 string: forbiddenIfSync,Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 37why, one step back37 uint8Array: forbiddenIfSync,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 38why, one step back38 native() {},Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 39–41why, one step back39 },40};41Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 42–48why, one step back42const addInputOptionSync = (fileDescriptors, options) => {43 const allContents = fileDescriptors44 .filter(({direction}) => direction === 'input')45 .flatMap(({stdioItems}) => stdioItems)46 .map(({contents}) => contents)47 .filter(contents => contents !== undefined);48 if (allContents.length === 0) {Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 49why, one step back49 return;Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 50why, one step back50 }Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 51why, one step back51Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 52–54why, one step back52 options.input = allContents.length === 153 ? allContents[0]54 : allContents.map(contents => serializeContents(contents)).join('');Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 55why, one step back55};Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 56why, one step back56Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 57why, one step back57const serializeContents = contents => typeof contents === 'string' ? contents : uint8ArrayToString(contents);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 58–59why, one step back5859// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync modeRefactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 60why, one step back60export const pipeOutputSync = (fileDescriptors, {output}) => {Refactor `reject` option (#868)ehmickybefore adoption1f8a55c
line 61why, one step back61 if (output === null) {Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 62–64why, one step back62 return;63 }64Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 65–67why, one step back65 for (const {stdioItems, fdNumber, direction} of fileDescriptors) {66 for (const {type, path} of stdioItems) {67 pipeStdioItemSync(output[fdNumber], type, path, direction);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 68why, one step back68 }Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 69–71why, one step back69 }70};71Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 72why, one step back72const pipeStdioItemSync = (result, type, path, direction) => {Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 73–76why, one step back73 if (result === null || direction === 'input') {74 return;75 }76Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
line 77why, one step back77 if (type === 'fileUrl' || type === 'filePath') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 78why, one step back78 writeFileSync(path, result);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 79–80why, one step back79 }80};