iterable.js
This file
Path
lib/convert/iterable.js
34 lines · 1.1 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';import {getFromStream} from '../arguments/fd-options.js';import {iterateOnSubprocessStream} from '../io/iterate.js';import {copyOptions, checkBooleanOption} from '../utils/options.js';
// Convert the subprocess to an async iterableexport const createIterable = (subprocess, encoding, options) => { const {from, binary: binaryOption = false, preserveNewlines = false} = copyOptions(options); checkBooleanOption(binaryOption, 'binary'); checkBooleanOption(preserveNewlines, 'preserveNewlines'); const binary = binaryOption || BINARY_ENCODINGS.has(encoding); const subprocessStdout = getFromStream(subprocess, from); const onStdoutData = iterateOnSubprocessStream({ subprocessStdout, subprocess, binary, shouldEncode: true, encoding, preserveNewlines, }); return iterateOnStdoutData(onStdoutData, subprocessStdout, subprocess);};
const iterateOnStdoutData = async function * (onStdoutData, subprocessStdout, subprocess) { try { yield * onStdoutData; } finally { if (subprocessStdout.readable) { subprocessStdout.destroy(); }
await subprocess; }};