Skip to content
Myr

readable.js

This file

Path

lib/convert/readable.js

113 lines · 3.9 KiB · JavaScript

Last changed by

Refactor `subprocess.kill(error)` (#1023)ehmickybefore adoption9de7335

History of this file

Take it

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

lines 1–2why, one step back
1import {Readable} from 'node:stream';
2import {callbackify} from 'node:util';

Rename files (#977)ehmickybefore adoption4b2e316

line 3why, one step back
3import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

line 4why, one step back
4import {getFromStream} from '../arguments/fd-options.js';

Rename files (#977)ehmickybefore adoption4b2e316

line 5why, one step back
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';

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

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

Improve `encoding` option (#958)ehmickybefore adoption507b09c

lines 16–17why, one step back
16export const createReadable = ({subprocess, concurrentStreams, encoding}, {from, binary: binaryOption = true, preserveNewlines = true} = {}) => {
17 const binary = binaryOption || BINARY_ENCODINGS.has(encoding);

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

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

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

line 19why, one step back
19 const {readableEncoding, readableObjectMode, readableHighWaterMark} = getReadableOptions(subprocessStdout, binary);

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 20–26why, one step back
20 const {read, onStdoutDataDone} = getReadableMethods({
21 subprocessStdout,22 subprocess,23 binary,24 encoding,25 preserveNewlines,26 });

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

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

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

line 28why, one step back
28 read,

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

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

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

lines 30–32why, one step back
30 highWaterMark: readableHighWaterMark,
31 objectMode: readableObjectMode,32 encoding: readableEncoding,

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

line 33why, one step back
33 });

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 34–39why, one step back
34 onStdoutFinished({
35 subprocessStdout,36 onStdoutDataDone,37 readable,38 subprocess,39 });

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

lines 40–44why, one step back
40 return readable;
41};4243// Retrieve `stdout` (or other stream depending on `from`)44export const getSubprocessStdout = (subprocess, from, concurrentStreams) => {

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

line 45why, one step back
45 const subprocessStdout = getFromStream(subprocess, from);

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

lines 46–49why, one step back
46 const waitReadableDestroy = addConcurrentStream(concurrentStreams, subprocessStdout, 'readableDestroy');
47 return {subprocessStdout, waitReadableDestroy};48};49

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

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

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

line 53why, one step back
53

Improve `encoding` option (#958)ehmickybefore adoption507b09c

line 54why, one step back
54export const getReadableMethods = ({subprocessStdout, subprocess, binary, encoding, preserveNewlines}) => {

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

line 55why, one step back
55 const onStdoutDataDone = createDeferred();

Upgrade xo (#980)ehmickybefore adoptionccfa412

lines 56–63why, one step back
56 const onStdoutData = iterateOnSubprocessStream({
57 subprocessStdout,58 subprocess,59 binary,60 shouldEncode: !binary,61 encoding,62 preserveNewlines,63 });

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

line 64why, one step back
64

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

lines 65–66why, one step back
65 return {
66 read() {

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

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

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

line 68why, one step back
68 },

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

line 69why, one step back
69 onStdoutDataDone,

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

lines 70–73why, one step back
70 };
71};7273// Forwards data from `stdout` to `readable`

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

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

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

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

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

lines 77–80why, one step back
77 if (done) {
78 onStdoutDataDone.resolve();79 } else {80 readable.push(value);

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

lines 81–86why, one step back
81 }
82 } catch {}83};8485// When `subprocess.stdout` ends/aborts/errors, do the same on `readable`.86// Await the subprocess, for the same reason as above.

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

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

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

lines 88–91why, one step back
88 try {
89 await waitForSubprocessStdout(subprocessStdout);90 await subprocess;91 await safeWaitForSubprocessStdin(subprocessStdin);

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

line 92why, one step back
92 await onStdoutDataDone;

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

lines 93–113why, one step back
93
94 if (readable.readable) {95 readable.push(null);96 }97 } catch (error) {98 await safeWaitForSubprocessStdin(subprocessStdin);99 destroyOtherReadable(readable, error);100 }101};102103// When `readable` aborts/errors, do the same on `subprocess.stdout`104export const onReadableDestroy = async ({subprocessStdout, subprocess, waitReadableDestroy}, error) => {105 if (await waitForConcurrentStreams(waitReadableDestroy, subprocess)) {106 destroyOtherReadable(subprocessStdout, error);107 await waitForSubprocess(subprocess, error);108 }109};110111const destroyOtherReadable = (stream, error) => {112 destroyOtherStream(stream, stream.readable, error);113};