handle-async.js
This file
Path
lib/stdio/handle-async.js
58 lines · 2.5 KiB · JavaScript
Last changed by
Fix the `encoding` option and line splitting in transformsSindre Sorhusbefore adoption6adb3c3
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import {createReadStream, createWriteStream} from 'node:fs';import {Buffer} from 'node:buffer';import {Readable, Writable, Duplex} from 'node:stream';import {generatorToStream} from '../transform/generator.js';import {getInputEncoding} from '../arguments/encoding-option.js';import {handleStdio} from './handle.js';import {TYPE_TO_MESSAGE} from './type.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async modeexport const handleStdioAsync = (options, verboseInfo) => handleStdio(addPropertiesAsync, options, verboseInfo, false);
const forbiddenIfAsync = ({type, optionName}) => { throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]}.`);};
// Create streams used internally for piping when using specific values for the `std*` options, in async mode.// For example, `stdout: {file}` creates a file stream, which is piped from/to.const addProperties = { fileNumber: forbiddenIfAsync, generator: generatorToStream, asyncGenerator: generatorToStream, nodeStream: ({value}) => ({stream: value}), webTransform({value: {transform, writableObjectMode, readableObjectMode}}) { const objectMode = writableObjectMode || readableObjectMode; const stream = Duplex.fromWeb(transform, {objectMode}); return {stream}; }, duplex: ({value: {transform}}) => ({stream: transform}), native() {},};
// Input transforms use UTF-8 with text encodings, since those only apply to the outputconst generatorToInputStream = (stdioItem, {encoding}) => generatorToStream(stdioItem, {encoding: getInputEncoding(encoding)});
const addPropertiesAsync = { input: { ...addProperties, generator: generatorToInputStream, asyncGenerator: generatorToInputStream, fileUrl: ({value}) => ({stream: createReadStream(value)}), filePath: ({value: {file}}) => ({stream: createReadStream(file)}), webStream: ({value}) => ({stream: Readable.fromWeb(value)}), iterable: ({value}) => ({stream: Readable.from(value)}), asyncIterable: ({value}) => ({stream: Readable.from(value)}), string: ({value}) => ({stream: Readable.from(value)}), uint8Array: ({value}) => ({stream: Readable.from(Buffer.from(value))}), }, output: { ...addProperties, fileUrl: ({value}) => ({stream: createWriteStream(value)}), filePath: ({value: {file, append}}) => ({stream: createWriteStream(file, append ? {flags: 'a'} : {})}), webStream: ({value}) => ({stream: Writable.fromWeb(value)}), iterable: forbiddenIfAsync, asyncIterable: forbiddenIfAsync, string: forbiddenIfAsync, uint8Array: forbiddenIfAsync, },};