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 fileTake it
Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 1why, one step back1import {readFileSync, writeFileSync} from 'node:fs';Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 2why, one step back2import {isStream as isNodeStream} from 'is-stream';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
line 4why, one step back4import {TYPE_TO_MESSAGE} from './type.js';Validate `input` option's type (#668)ehmickybefore adoption65742c6
line 5why, one step back5import {bufferToUint8Array} from './utils.js';Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 6–8why, one step back67// 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 back9 const stdioStreamsGroups = handleInput(addPropertiesSync, options, true);Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
lines 10–11why, one step back10 addInputOptionSync(stdioStreamsGroups, options);11 return stdioStreamsGroups;Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 12–13why, one step back12};13Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 14–17why, one step back14const 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 back1819 return {};Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 20–21why, one step back20};21Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 22–27why, one step back22const 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 back28 generator: forbiddenIfSync,Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
lines 29–30why, one step back29 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 back31 webStream: forbiddenIfSync,32 nodeStream: forbiddenIfSync,33 iterable: forbiddenIfSync,Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 34why, one step back34 native: forbiddenIfStreamSync,Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 35–36why, one step back35 },36 output: {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 37why, one step back37 generator: forbiddenIfSync,Improve syntax of `std*: filePath` option (#671)ehmickybefore adoption90e0e13
line 38why, one step back38 filePath: ({value}) => ({value: value.file}),Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 39–41why, one step back39 webStream: forbiddenIfSync,40 nodeStream: forbiddenIfSync,41 iterable: forbiddenIfSync,Allow `stdin: uint8Array` option (#670)ehmickybefore adoption2ac81d4
line 42why, one step back42 uint8Array: forbiddenIfSync,Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 43why, one step back43 native: forbiddenIfStreamSync,Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 44–46why, one step back44 },45};46Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
lines 47–48why, one step back47const 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 back49 if (inputs.length === 0) {50 return;Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 51why, one step back51 }Allow using the `input`, `inputFile` and `stdin` options together (#666)ehmickybefore adoptionbeabd25
lines 52–54why, one step back5253 options.input = inputs.length === 154 ? inputs[0].valueValidate `input` option's type (#668)ehmickybefore adoption65742c6
line 55why, one step back55 : inputs.map(stdioStream => serializeInput(stdioStream)).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 back57Validate `input` option's type (#668)ehmickybefore adoption65742c6
line 58why, one step back58const serializeInput = ({type, value}) => type === 'string' ? value : new TextDecoder().decode(value);Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 59–60why, one step back5960// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync modeSimplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
line 61why, one step back61export const pipeOutputSync = (stdioStreamsGroups, result) => {Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 62–65why, one step back62 if (result.output === null) {63 return;64 }65Simplify `std*` options logic (#691)ehmickybefore adoption98b0ee3
lines 66–69why, one step back66 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 back70 }71};7273const pipeStdioOptionSync = (result, {type, value, direction}) => {74 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` options (#623)ehmickybefore adoption2d76897
lines 79–81why, one step back79 writeFileSync(value, result);80 }81};