readable.js
This file
Path
lib/convert/readable.js
134 lines · 4.6 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
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–5why, one step back1import {Readable} from 'node:stream';2import {callbackify} from 'node:util';3import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';4import {getFromStream} from '../arguments/fd-options.js';5import {iterateOnSubprocessStream, DEFAULT_OBJECT_HIGH_WATER_MARK} from '../io/iterate.js';Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335
line 6why, one step back6import {createDeferred} from '../utils/deferred.js';Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 7why, one step back7import {copyOptions, checkBooleanOption} from '../utils/options.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 8–11why, one step back8import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';9import {10 safeWaitForSubprocessStdin,11 waitForSubprocessStdout,Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
line 12why, one step back12 throwDestroyError,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 13–16why, one step back13 destroyOtherStream,14} from './shared.js';1516// Create a `Readable` stream that forwards from `stdout` and awaits the subprocessHarden some thingsSindre Sorhusbefore adoption4a0a121
line 17why, one step back17export const createReadable = ({subprocess, concurrentStreams, encoding}, options) => {TweaksSindre Sorhusbefore adoption698a1b8
line 18why, one step back18 const {from, binary: binaryOption = true, preserveNewlines = true} = copyOptions(options);Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 19–20why, one step back19 checkBooleanOption(binaryOption, 'binary');20 checkBooleanOption(preserveNewlines, 'preserveNewlines');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 21–102why, one step back21 const binary = binaryOption || BINARY_ENCODINGS.has(encoding);22 const {subprocessStdout, waitReadableDestroy} = getSubprocessStdout(subprocess, from, concurrentStreams);23 const {readableEncoding, readableObjectMode, readableHighWaterMark} = getReadableOptions(subprocessStdout, binary);24 const {read, onStdoutDataDone} = getReadableMethods({25 subprocessStdout,26 subprocess,27 binary,28 encoding,29 preserveNewlines,30 });31 const readable = new Readable({32 read,33 destroy: callbackify(onReadableDestroy.bind(undefined, {subprocessStdout, subprocess, waitReadableDestroy})),34 highWaterMark: readableHighWaterMark,35 objectMode: readableObjectMode,36 encoding: readableEncoding,37 });38 onStdoutFinished({39 subprocessStdout,40 onStdoutDataDone,41 readable,42 subprocess,43 });44 return readable;45};4647// Retrieve `stdout` (or other stream depending on `from`)48export const getSubprocessStdout = (subprocess, from, concurrentStreams) => {49 const subprocessStdout = getFromStream(subprocess, from);50 const waitReadableDestroy = addConcurrentStream(concurrentStreams, subprocessStdout, 'readableDestroy');51 return {subprocessStdout, waitReadableDestroy};52};5354export const getReadableOptions = ({readableEncoding, readableObjectMode, readableHighWaterMark}, binary) => binary55 ? {readableEncoding, readableObjectMode, readableHighWaterMark}56 : {readableEncoding, readableObjectMode: true, readableHighWaterMark: DEFAULT_OBJECT_HIGH_WATER_MARK};5758export const getReadableMethods = ({subprocessStdout, subprocess, binary, encoding, preserveNewlines}) => {59 const onStdoutDataDone = createDeferred();60 const onStdoutData = iterateOnSubprocessStream({61 subprocessStdout,62 subprocess,63 binary,64 shouldEncode: !binary,65 encoding,66 preserveNewlines,67 });6869 return {70 read() {71 onRead(this, onStdoutData, onStdoutDataDone);72 },73 onStdoutDataDone,74 };75};7677// Forwards data from `stdout` to `readable`78const onRead = async (readable, onStdoutData, onStdoutDataDone) => {79 try {80 const {value, done} = await onStdoutData.next();81 if (done) {82 onStdoutDataDone.resolve();83 } else {84 readable.push(value);85 }86 } catch {}87};8889// When `subprocess.stdout` ends/aborts/errors, do the same on `readable`.90// Await the subprocess, for the same reason as above.91export const onStdoutFinished = async ({subprocessStdout, onStdoutDataDone, readable, subprocess, subprocessStdin}) => {92 try {93 await waitForSubprocessStdout(subprocessStdout);94 await subprocess;95 await safeWaitForSubprocessStdin(subprocessStdin);96 await onStdoutDataDone;9798 if (readable.readable) {99 readable.push(null);100 }101 } catch (error) {102 await safeWaitForSubprocessStdin(subprocessStdin);Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 103why, one step back103 destroyOtherReadable(readable, await getPrematureCloseError(subprocess, error));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 104why, one step back104 }Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
lines 105–118why, one step back105};106107const getPrematureCloseError = async (subprocess, error) => {108 if (error.code !== 'ERR_STREAM_PREMATURE_CLOSE') {109 return error;110 }111112 try {113 await subprocess;114 } catch (subprocessError) {115 return subprocessError;116 }117118 return error;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 119–122why, one step back119};120121// When `readable` aborts/errors, do the same on `subprocess.stdout`122export const onReadableDestroy = async ({subprocessStdout, subprocess, waitReadableDestroy}, error) => {Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
lines 123–126why, one step back123 if (await waitForConcurrentStreams(waitReadableDestroy, subprocess)) {124 // Breaking out of `for await (const chunk of readable)` or cancelling `subprocess.readableStream()` destroys `readable` with an `AbortError`. Like `readable.destroy()`, this aborts `subprocess.stdout` instead of making it fail.125 destroyOtherReadable(subprocessStdout, error?.code === 'ABORT_ERR' ? undefined : error);126 await subprocess;100% test coverage (#1018)ehmickybefore adoption9f65b86
line 127why, one step back127 }Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454
line 128why, one step back128Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
line 129why, one step back129 throwDestroyError(error);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 130–134why, one step back130};131132const destroyOtherReadable = (stream, error) => {133 destroyOtherStream(stream, stream.readable, error);134};