Skip to content
Myr

readable.js

This file

Path

lib/convert/readable.js

94 lines · 3.7 KiB · JavaScript

Last changed by

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

History of this file

Take it

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 1–5why, one step back
1import {Readable} from 'node:stream';
2import {callbackify} from 'node:util';3import {getReadable} from '../pipe/validate.js';4import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';5import {

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 6why, one step back
6 createDeferred,

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 7–11why, one step back
7 safeWaitForSubprocessStdin,
8 waitForSubprocessStdout,9 waitForSubprocess,10 destroyOtherStream,11} from './shared.js';

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 12why, one step back
12import {iterateOnSubprocessStream, DEFAULT_OBJECT_HIGH_WATER_MARK} from './loop.js';

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 13–14why, one step back
13
14// Create a `Readable` stream that forwards from `stdout` and awaits the subprocess

Improve handling of `encoding` option (#926)ehmickybefore adoptiond786fcf

lines 15–16why, one step back
15export const createReadable = ({subprocess, concurrentStreams, useBinaryEncoding}, {from, binary: binaryOption = true, preserveNewlines = true} = {}) => {
16 const binary = binaryOption || useBinaryEncoding;

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 17why, one step back
17 const {subprocessStdout, waitReadableDestroy} = getSubprocessStdout(subprocess, from, concurrentStreams);

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 18–19why, one step back
18 const {readableEncoding, readableObjectMode, readableHighWaterMark} = getReadableOptions(subprocessStdout, binary);
19 const {read, onStdoutDataDone} = getReadableMethods({subprocessStdout, subprocess, binary, preserveNewlines});

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 20why, one step back
20 const readable = new Readable({

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 21why, one step back
21 read,

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 22why, one step back
22 destroy: callbackify(onReadableDestroy.bind(undefined, {subprocessStdout, subprocess, waitReadableDestroy})),

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 23–25why, one step back
23 highWaterMark: readableHighWaterMark,
24 objectMode: readableObjectMode,25 encoding: readableEncoding,

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 26why, one step back
26 });

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 27why, one step back
27 onStdoutFinished({subprocessStdout, onStdoutDataDone, readable, subprocess});

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 28–37why, one step back
28 return readable;
29};3031// Retrieve `stdout` (or other stream depending on `from`)32export const getSubprocessStdout = (subprocess, from, concurrentStreams) => {33 const subprocessStdout = getReadable(subprocess, from);34 const waitReadableDestroy = addConcurrentStream(concurrentStreams, subprocessStdout, 'readableDestroy');35 return {subprocessStdout, waitReadableDestroy};36};37

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 38–40why, one step back
38export const getReadableOptions = ({readableEncoding, readableObjectMode, readableHighWaterMark}, binary) => binary
39 ? {readableEncoding, readableObjectMode, readableHighWaterMark}40 : {readableEncoding, readableObjectMode: true, readableHighWaterMark: DEFAULT_OBJECT_HIGH_WATER_MARK};

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 41why, one step back
41

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 42–43why, one step back
42export const getReadableMethods = ({subprocessStdout, subprocess, binary, preserveNewlines}) => {
43 const onStdoutDataDone = createDeferred();

Fix `lines` option (#957)ehmickybefore adoption15ab4e6

line 44why, one step back
44 const onStdoutData = iterateOnSubprocessStream({subprocessStdout, subprocess, binary, shouldEncode: !binary, preserveNewlines});

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 45why, one step back
45

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 46–47why, one step back
46 return {
47 read() {

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 48why, one step back
48 onRead(this, onStdoutData, onStdoutDataDone);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

line 49why, one step back
49 },

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 50why, one step back
50 onStdoutDataDone,

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 51–54why, one step back
51 };
52};5354// Forwards data from `stdout` to `readable`

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 55why, one step back
55const onRead = async (readable, onStdoutData, onStdoutDataDone) => {

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 56–57why, one step back
56 try {
57 const {value, done} = await onStdoutData.next();

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

lines 58–61why, one step back
58 if (done) {
59 onStdoutDataDone.resolve();60 } else {61 readable.push(value);

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 62–67why, one step back
62 }
63 } catch {}64};6566// When `subprocess.stdout` ends/aborts/errors, do the same on `readable`.67// Await the subprocess, for the same reason as above.

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 68why, one step back
68export const onStdoutFinished = async ({subprocessStdout, onStdoutDataDone, readable, subprocess, subprocessStdin}) => {

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 69–72why, one step back
69 try {
70 await waitForSubprocessStdout(subprocessStdout);71 await subprocess;72 await safeWaitForSubprocessStdin(subprocessStdin);

Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e

line 73why, one step back
73 await onStdoutDataDone;

Add `.readable()`, `.writable()` and `.duplex()` methods (#912)ehmickybefore adoption666f7f9

lines 74–94why, one step back
74
75 if (readable.readable) {76 readable.push(null);77 }78 } catch (error) {79 await safeWaitForSubprocessStdin(subprocessStdin);80 destroyOtherReadable(readable, error);81 }82};8384// When `readable` aborts/errors, do the same on `subprocess.stdout`85export const onReadableDestroy = async ({subprocessStdout, subprocess, waitReadableDestroy}, error) => {86 if (await waitForConcurrentStreams(waitReadableDestroy, subprocess)) {87 destroyOtherReadable(subprocessStdout, error);88 await waitForSubprocess(subprocess, error);89 }90};9192const destroyOtherReadable = (stream, error) => {93 destroyOtherStream(stream, stream.readable, error);94};