input-sync.js
This file
Path
lib/io/input-sync.js
58 lines · 3.0 KiB · JavaScript
Last changed by
Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
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';100% test coverage (#1018)ehmickybefore adoption9f65b86
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
line 29why, 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.Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 30–31why, one step back30 // Files and iterables are read lazily, so that a read failure is reported like any other early error31 const chunks = allStdioItems.flatMap(({type, contents}) => getChunks(type, typeof contents === 'function' ? contents() : contents));Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
line 32why, one step back32 options.input = applyInputGeneratorsSync(chunks, stdioItems, options.encoding);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 33–34why, one step back33};34Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 35–38why, one step back35// Like with asynchronous methods, an empty input is not passed as a chunk, otherwise binary transforms, which do not split lines, would run on it.36// Iterables are left as is, since each of their items is a chunk.37const getChunks = (type, chunks) => type === 'iterable' ? chunks : chunks.filter(chunk => chunk.length > 0);38Fix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
line 39why, one step back39const applyInputGeneratorsSync = (chunks, stdioItems, encoding) => {Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 40why, one step back40 // Input transforms use UTF-8 with text encodings, since those only apply to the outputFix synchronous input transforms and `inputFile` errorsSindre Sorhusbefore adoption516a5e2
line 41why, one step back41 const newContents = runGeneratorsSync(chunks, stdioItems, getInputEncoding(encoding), true);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 42–46why, one step back42 validateSerializable(newContents);43 return joinToUint8Array(newContents);44};4546const validateSerializable = newContents => {Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
line 47why, one step back47 const invalidItem = newContents.find(item => !isSerializableInput(item));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 48why, one step back48 if (invalidItem !== undefined) {Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
line 49why, one step back49 throwOnSerializableInput('stdin', invalidItem);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 50why, one step back50 }Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 51–57why, one step back51};5253// Values written to the subprocess, which is not in object mode54export const isSerializableInput = item => typeof item === 'string' || isUint8Array(item);5556export const throwOnSerializableInput = (optionName, invalidItem) => {57 throw new TypeError(`The \`${optionName}\` option is invalid: when passing objects as input, a transform must be used to serialize them to strings or Uint8Arrays: ${invalidItem}.`);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 58why, one step back58};