input-sync.js
This file
Path
lib/io/input-sync.js
47 lines · 2.3 KiB · JavaScript
Last changed by
Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
History of this fileTake it
The walk stopped after 200 commits: lines older than c8a6ed7 point at it.
Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
lines 1–3why, one step back1import {runGeneratorsSync} from '../transform/generator.js';2import {joinToUint8Array, isUint8Array} from '../utils/uint-array.js';3import {TYPE_TO_MESSAGE} from '../stdio/type.js';Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 4why, one step back4import {getInputEncoding} from '../arguments/encoding-option.js';Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
lines 5–28why, one step back56// Apply `stdin`/`input`/`inputFile` options, before spawning, in sync mode, by converting it to the `input` option7export const addInputOptionsSync = (fileDescriptors, options) => {8 for (const fdNumber of getInputFdNumbers(fileDescriptors)) {9 addInputOptionSync(fileDescriptors, fdNumber, options);10 }11};1213const getInputFdNumbers = fileDescriptors => new Set(Object.entries(fileDescriptors)14 .filter(([, {direction}]) => direction === 'input')15 .map(([fdNumber]) => Number(fdNumber)));1617const addInputOptionSync = (fileDescriptors, fdNumber, options) => {18 const {stdioItems} = fileDescriptors[fdNumber];19 const allStdioItems = stdioItems.filter(({contents}) => contents !== undefined);20 if (allStdioItems.length === 0) {21 return;22 }2324 if (fdNumber !== 0) {25 const [{type, optionName}] = allStdioItems;26 throw new TypeError(`Only the \`stdin\` option, not \`${optionName}\`, can be ${TYPE_TO_MESSAGE[type]} with synchronous methods.`);27 }28Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
lines 29–32why, one step back29 // All input sources are merged into a single list of chunks, like asynchronous methods merge input streams, so that transforms receive a single stream of data: line splitting spans all sources and `final` runs only once.30 // Files are read lazily, so that a read failure is reported like any other early error31 const chunks = allStdioItems.flatMap(({contents}) => typeof contents === 'function' ? contents() : contents);32 options.input = applyInputGeneratorsSync(chunks, stdioItems, options.encoding);Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
lines 33–34why, one step back33};34Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
line 35why, one step back35const applyInputGeneratorsSync = (chunks, stdioItems, encoding) => {Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 36why, one step back36 // Input transforms use UTF-8 with text encodings, since those only apply to the outputFix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
line 37why, one step back37 const newContents = runGeneratorsSync(chunks, stdioItems, getInputEncoding(encoding), true);Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
lines 38–47why, one step back38 validateSerializable(newContents);39 return joinToUint8Array(newContents);40};4142const validateSerializable = newContents => {43 const invalidItem = newContents.find(item => typeof item !== 'string' && !isUint8Array(item));44 if (invalidItem !== undefined) {45 throw new TypeError(`The \`stdin\` option is invalid: when passing objects as input, a transform must be used to serialize them to strings or Uint8Arrays: ${invalidItem}.`);46 }47};