Skip to content
Myr

readable.js

This file

Path

lib/convert/readable.js

131 lines · 4.2 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than c8a6ed7 point at it.

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 1–5why, one step back
1import {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 back
6import {createDeferred} from '../utils/deferred.js';

TweaksSindre Sorhusbefore adoption698a1b8

line 7why, one step back
7import {copyOptions} from '../utils/options.js';

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 8–16why, one step back
8import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';
9import {10 safeWaitForSubprocessStdin,11 waitForSubprocessStdout,12 waitForSubprocess,13 destroyOtherStream,14} from './shared.js';1516// Create a `Readable` stream that forwards from `stdout` and awaits the subprocess

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 17why, one step back
17export const createReadable = ({subprocess, concurrentStreams, encoding}, options) => {

TweaksSindre Sorhusbefore adoption698a1b8

line 18why, one step back
18 const {from, binary: binaryOption = true, preserveNewlines = true} = copyOptions(options);

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 19–100why, one step back
19 const binary = binaryOption || BINARY_ENCODINGS.has(encoding);
20 const {subprocessStdout, waitReadableDestroy} = getSubprocessStdout(subprocess, from, concurrentStreams);21 const {readableEncoding, readableObjectMode, readableHighWaterMark} = getReadableOptions(subprocessStdout, binary);22 const {read, onStdoutDataDone} = getReadableMethods({23 subprocessStdout,24 subprocess,25 binary,26 encoding,27 preserveNewlines,28 });29 const readable = new Readable({30 read,31 destroy: callbackify(onReadableDestroy.bind(undefined, {subprocessStdout, subprocess, waitReadableDestroy})),32 highWaterMark: readableHighWaterMark,33 objectMode: readableObjectMode,34 encoding: readableEncoding,35 });36 onStdoutFinished({37 subprocessStdout,38 onStdoutDataDone,39 readable,40 subprocess,41 });42 return readable;43};4445// Retrieve `stdout` (or other stream depending on `from`)46export const getSubprocessStdout = (subprocess, from, concurrentStreams) => {47 const subprocessStdout = getFromStream(subprocess, from);48 const waitReadableDestroy = addConcurrentStream(concurrentStreams, subprocessStdout, 'readableDestroy');49 return {subprocessStdout, waitReadableDestroy};50};5152export const getReadableOptions = ({readableEncoding, readableObjectMode, readableHighWaterMark}, binary) => binary53 ? {readableEncoding, readableObjectMode, readableHighWaterMark}54 : {readableEncoding, readableObjectMode: true, readableHighWaterMark: DEFAULT_OBJECT_HIGH_WATER_MARK};5556export const getReadableMethods = ({subprocessStdout, subprocess, binary, encoding, preserveNewlines}) => {57 const onStdoutDataDone = createDeferred();58 const onStdoutData = iterateOnSubprocessStream({59 subprocessStdout,60 subprocess,61 binary,62 shouldEncode: !binary,63 encoding,64 preserveNewlines,65 });6667 return {68 read() {69 onRead(this, onStdoutData, onStdoutDataDone);70 },71 onStdoutDataDone,72 };73};7475// Forwards data from `stdout` to `readable`76const onRead = async (readable, onStdoutData, onStdoutDataDone) => {77 try {78 const {value, done} = await onStdoutData.next();79 if (done) {80 onStdoutDataDone.resolve();81 } else {82 readable.push(value);83 }84 } catch {}85};8687// When `subprocess.stdout` ends/aborts/errors, do the same on `readable`.88// Await the subprocess, for the same reason as above.89export const onStdoutFinished = async ({subprocessStdout, onStdoutDataDone, readable, subprocess, subprocessStdin}) => {90 try {91 await waitForSubprocessStdout(subprocessStdout);92 await subprocess;93 await safeWaitForSubprocessStdin(subprocessStdin);94 await onStdoutDataDone;9596 if (readable.readable) {97 readable.push(null);98 }99 } catch (error) {100 await safeWaitForSubprocessStdin(subprocessStdin);

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 101why, one step back
101 destroyOtherReadable(readable, await getPrematureCloseError(subprocess, error));

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

line 102why, one step back
102 }

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

lines 103–116why, one step back
103};
104105const getPrematureCloseError = async (subprocess, error) => {106 if (error.code !== 'ERR_STREAM_PREMATURE_CLOSE') {107 return error;108 }109110 try {111 await subprocess;112 } catch (subprocessError) {113 return subprocessError;114 }115116 return error;

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 117–120why, one step back
117};
118119// When `readable` aborts/errors, do the same on `subprocess.stdout`120export const onReadableDestroy = async ({subprocessStdout, subprocess, waitReadableDestroy}, error) => {

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 121–122why, one step back
121 if (!await waitForConcurrentStreams(waitReadableDestroy, subprocess)) {
122 return;

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

line 123why, one step back
123 }

Require Node.js 22 (#1243)Sindre Sorhusbefore adoption04b4454

lines 124–126why, one step back
124
125 destroyOtherReadable(subprocessStdout, error);126 await waitForSubprocess(subprocess, error);

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

lines 127–131why, one step back
127};
128129const destroyOtherReadable = (stream, error) => {130 destroyOtherStream(stream, stream.readable, error);131};