handle-async.js
This file
Path
lib/stdio/handle-async.js
87 lines · 4.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–4why, one step back1import {createReadStream, createWriteStream} from 'node:fs';2import {Buffer} from 'node:buffer';3import {Readable, Writable, Duplex} from 'node:stream';4import {generatorToStream} from '../transform/generator.js';Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
line 5why, one step back5import {getInputEncoding} from '../arguments/encoding-option.js';Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
line 6why, one step back6import {isSerializableInput, throwOnSerializableInput} from '../io/input-sync.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 7–23why, one step back7import {handleStdio} from './handle.js';8import {TYPE_TO_MESSAGE} from './type.js';910// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async mode11export const handleStdioAsync = (options, verboseInfo) => handleStdio(addPropertiesAsync, options, verboseInfo, false);1213const forbiddenIfAsync = ({type, optionName}) => {14 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]}.`);15};1617// Create streams used internally for piping when using specific values for the `std*` options, in async mode.18// For example, `stdout: {file}` creates a file stream, which is piped from/to.19const addProperties = {20 fileNumber: forbiddenIfAsync,21 generator: generatorToStream,22 asyncGenerator: generatorToStream,23 nodeStream: ({value}) => ({stream: value}),Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 24–30why, one step back24 // `Duplex.fromWeb()` uses the same `objectMode` for both sides25 webTransform: ({value: {transform, writableObjectMode, readableObjectMode}}) => ({26 stream: Duplex.from({27 writable: Writable.fromWeb(transform.writable, {objectMode: writableObjectMode}),28 readable: Readable.fromWeb(transform.readable, {objectMode: readableObjectMode}),29 }),30 }),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 31–34why, one step back31 duplex: ({value: {transform}}) => ({stream: transform}),32 native() {},33};34Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 35–60why, one step back35// Without transforms in object mode, iterable values are written to the subprocess, which only accepts strings and Uint8Arrays.36// `Readable.from()` emits the values of synchronous iterables on a later tick, where `subprocess.stdin.write()` throwing would crash the current process. Throwing while iterating errors the stream instead.37// Synchronous iterables must stay synchronous, otherwise their order would change when they are combined with other input sources.38const validateIterable = function * (iterable, optionName) {39 for (const value of iterable) {40 validateIterableValue(value, optionName);41 yield value;42 }43};4445const validateAsyncIterable = async function * (iterable, optionName) {46 for await (const value of iterable) {47 validateIterableValue(value, optionName);48 yield value;49 }50};5152const validateIterableValue = (value, optionName) => {53 if (!isSerializableInput(value)) {54 throwOnSerializableInput(optionName, value);55 }56};5758// Empty input is not passed as a chunk, otherwise binary transforms, which do not split lines, would run on it59const readableFromChunk = chunk => Readable.from(chunk.length === 0 ? [] : [chunk]);60Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 61–63why, one step back61// Input transforms use UTF-8 with text encodings, since those only apply to the output62const generatorToInputStream = (stdioItem, {encoding}) => generatorToStream(stdioItem, {encoding: getInputEncoding(encoding)});63100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 64–66why, one step back64const addPropertiesAsync = {65 input: {66 ...addProperties,Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
lines 67–68why, one step back67 generator: generatorToInputStream,68 asyncGenerator: generatorToInputStream,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 69–71why, one step back69 fileUrl: ({value}) => ({stream: createReadStream(value)}),70 filePath: ({value: {file}}) => ({stream: createReadStream(file)}),71 webStream: ({value}) => ({stream: Readable.fromWeb(value)}),Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 72–75why, one step back72 iterable: ({value, optionName}, options, objectMode) => ({stream: Readable.from(objectMode ? value : validateIterable(value, optionName))}),73 asyncIterable: ({value, optionName}, options, objectMode) => ({stream: Readable.from(objectMode ? value : validateAsyncIterable(value, optionName))}),74 string: ({value}) => ({stream: readableFromChunk(value)}),75 uint8Array: ({value}) => ({stream: readableFromChunk(Buffer.from(value))}),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 76–79why, one step back76 },77 output: {78 ...addProperties,79 fileUrl: ({value}) => ({stream: createWriteStream(value)}),Allow appending to files (#1166)ehmickybefore adoption94a75df
line 80why, one step back80 filePath: ({value: {file, append}}) => ({stream: createWriteStream(file, append ? {flags: 'a'} : {})}),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 81–87why, one step back81 webStream: ({value}) => ({stream: Writable.fromWeb(value)}),82 iterable: forbiddenIfAsync,83 asyncIterable: forbiddenIfAsync,84 string: forbiddenIfAsync,85 uint8Array: forbiddenIfAsync,86 },87};