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 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–16why, one step back11};1213const forbiddenIfSync = ({type, optionName}) => {14 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]} in sync mode.`);15};16Refactor tests (#934)ehmickybefore adoption52c4bde
lines 17–20why, one step back17const addProperties = {18 generator: forbiddenIfSync,19 webStream: forbiddenIfSync,20 nodeStream: forbiddenIfSync,Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb
line 21why, one step back21 duplex: forbiddenIfSync,Refactor tests (#934)ehmickybefore adoption52c4bde
lines 22–25why, one step back22 iterable: forbiddenIfSync,23 native() {},24};25Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 26–27why, one step back26const addPropertiesSync = {27 input: {Refactor tests (#934)ehmickybefore adoption52c4bde
line 28why, one step back28 ...addProperties,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 29–32why, one step back29 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 back33 },34 output: {Refactor tests (#934)ehmickybefore adoption52c4bde
line 35why, one step back35 ...addProperties,Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 36–38why, one step back36 fileUrl: ({value}) => ({path: value}),37 filePath: ({value: {file}}) => ({path: file}),38 string: forbiddenIfSync,Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 39why, one step back39 uint8Array: forbiddenIfSync,Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 40–42why, one step back40 },41};42Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 43–49why, one step back43const 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 back50 return;Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 51why, one step back51 }Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 52why, one step back52Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 53–55why, one step back53 options.input = allContents.length === 154 ? allContents[0]55 : allContents.map(contents => serializeContents(contents)).join('');Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 56why, one step back56};Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
line 57why, one step back57Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 58why, one step back58const serializeContents = contents => typeof contents === 'string' ? contents : uint8ArrayToString(contents);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 59–60why, one step back5960// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync modeRefactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 61why, one step back61export const pipeOutputSync = (fileDescriptors, {output}) => {Refactor `reject` option (#868)ehmickybefore adoption1f8a55c
line 62why, one step back62 if (output === null) {Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 63–65why, one step back63 return;64 }65Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
lines 66–68why, one step back66 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 back69 }Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 70–72why, one step back70 }71};72Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 73why, one step back73const pipeStdioItemSync = (result, type, path, direction) => {Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 74–77why, one step back74 if (result === null || direction === 'input') {75 return;76 }77Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
line 78why, one step back78 if (type === 'fileUrl' || type === 'filePath') {Refactor `stdio` option (#927)ehmickybefore adoptionc9cdd2d
line 79why, one step back79 writeFileSync(path, result);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 80–81why, one step back80 }81};