handle-async.js
This file
Path
lib/stdio/handle-async.js
66 lines · 2.9 KiB · JavaScript
Last changed by
Add more code comments (#984)ehmickybefore adoptionfdd8a9f
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–15why, one step back1import {createReadStream, createWriteStream} from 'node:fs';2import {Buffer} from 'node:buffer';3import {Readable, Writable, Duplex} from 'node:stream';4import {isStandardStream} from '../utils/standard-stream.js';5import {generatorToStream} from '../transform/generator.js';6import {handleStdio} from './handle.js';7import {TYPE_TO_MESSAGE} from './type.js';89// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async mode10export const handleStdioAsync = (options, verboseInfo) => handleStdio(addPropertiesAsync, options, verboseInfo, false);1112const forbiddenIfAsync = ({type, optionName}) => {13 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]}.`);14};15Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 16–17why, one step back16// Create streams used internally for piping when using specific values for the `std*` options, in async mode.17// For example, `stdout: {file}` creates a file stream, which is piped from/to.Rename files (#977)ehmickybefore adoption4b2e316
lines 18–66why, one step back18const addProperties = {19 generator: generatorToStream,20 asyncGenerator: generatorToStream,21 nodeStream: ({value}) => ({stream: value}),22 webTransform({value: {transform, writableObjectMode, readableObjectMode}}) {23 const objectMode = writableObjectMode || readableObjectMode;24 const stream = Duplex.fromWeb(transform, {objectMode});25 return {stream};26 },27 duplex: ({value: {transform}}) => ({stream: transform}),28 native() {},29};3031const addPropertiesAsync = {32 input: {33 ...addProperties,34 fileUrl: ({value}) => ({stream: createReadStream(value)}),35 filePath: ({value: {file}}) => ({stream: createReadStream(file)}),36 webStream: ({value}) => ({stream: Readable.fromWeb(value)}),37 iterable: ({value}) => ({stream: Readable.from(value)}),38 asyncIterable: ({value}) => ({stream: Readable.from(value)}),39 string: ({value}) => ({stream: Readable.from(value)}),40 uint8Array: ({value}) => ({stream: Readable.from(Buffer.from(value))}),41 },42 output: {43 ...addProperties,44 fileUrl: ({value}) => ({stream: createWriteStream(value)}),45 filePath: ({value: {file}}) => ({stream: createWriteStream(file)}),46 webStream: ({value}) => ({stream: Writable.fromWeb(value)}),47 iterable: forbiddenIfAsync,48 asyncIterable: forbiddenIfAsync,49 string: forbiddenIfAsync,50 uint8Array: forbiddenIfAsync,51 },52};5354// The stream error handling is performed by the piping logic above, which cannot be performed before subprocess spawning.55// If the subprocess spawning fails (e.g. due to an invalid command), the streams need to be manually destroyed.56// We need to create those streams before subprocess spawning, in case their creation fails, e.g. when passing an invalid generator as argument.57// Like this, an exception would be thrown, which would prevent spawning a subprocess.58export const cleanupCustomStreams = fileDescriptors => {59 for (const {stdioItems} of fileDescriptors) {60 for (const {stream} of stdioItems) {61 if (stream !== undefined && !isStandardStream(stream)) {62 stream.destroy();63 }64 }65 }66};