handle-async.js
This file
Path
lib/stdio/handle-async.js
52 lines · 2.1 KiB · JavaScript
Last changed by
Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–14why, 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';5import {handleStdio} from './handle.js';6import {TYPE_TO_MESSAGE} from './type.js';78// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async mode9export const handleStdioAsync = (options, verboseInfo) => handleStdio(addPropertiesAsync, options, verboseInfo, false);1011const forbiddenIfAsync = ({type, optionName}) => {12 throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]}.`);13};14Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 15–16why, one step back15// Create streams used internally for piping when using specific values for the `std*` options, in async mode.16// For example, `stdout: {file}` creates a file stream, which is piped from/to.Rename files (#977)ehmickybefore adoption4b2e316
line 17why, one step back17const addProperties = {Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
line 18why, one step back18 fileNumber: forbiddenIfAsync,Rename files (#977)ehmickybefore adoption4b2e316
lines 19–52why, one step back19 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};