Skip to content
Myr

loop.js

This file

Path

lib/convert/loop.js

101 lines · 3.6 KiB · JavaScript

Last changed by

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

History of this file

Take it

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

lines 1–4why, one step back
1import {on} from 'node:events';
2import {getEncodingTransformGenerator} from '../stdio/encoding-transform.js';3import {getSplitLinesGenerator} from '../stdio/split.js';4

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 5why, one step back
5export const iterateOnStdout = ({subprocessStdout, subprocess, binary, preserveNewlines, isStream}) => {

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

lines 6–15why, one step back
6 const controller = new AbortController();
7 stopReadingOnExit(subprocess, controller);8 const onStdoutChunk = on(subprocessStdout, 'data', {9 signal: controller.signal,10 highWaterMark: HIGH_WATER_MARK,11 // Backward compatibility with older name for this option12 // See https://github.com/nodejs/node/pull/52080#discussion_r152522786113 // @todo Remove after removing support for Node 2114 highWatermark: HIGH_WATER_MARK,15 });

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 16why, one step back
16 const onStdoutData = iterateOnData({subprocessStdout, onStdoutChunk, controller, binary, preserveNewlines, isStream});

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

lines 17–36why, one step back
17 return onStdoutData;
18};1920const stopReadingOnExit = async (subprocess, controller) => {21 try {22 await subprocess;23 } catch {} finally {24 controller.abort();25 }26};2728// @todo: replace with `getDefaultHighWaterMark(true)` after dropping support for Node <18.17.029export const DEFAULT_OBJECT_HIGH_WATER_MARK = 16;3031// The `highWaterMark` of `events.on()` is measured in number of events, not in bytes.32// Not knowing the average amount of bytes per `data` event, we use the same heuristic as streams in objectMode, since they have the same issue.33// Therefore, we use the value of `getDefaultHighWaterMark(true)`.34// Note: this option does not exist on Node 18, but this is ok since the logic works without it. It just consumes more memory.35const HIGH_WATER_MARK = DEFAULT_OBJECT_HIGH_WATER_MARK;36

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 37why, one step back
37const iterateOnData = async function * ({subprocessStdout, onStdoutChunk, controller, binary, preserveNewlines, isStream}) {

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

lines 38–42why, one step back
38 const {
39 encodeChunk = identityGenerator,40 encodeChunkFinal = noopGenerator,41 splitLines = identityGenerator,42 splitLinesFinal = noopGenerator,

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 43why, one step back
43 } = getTransforms({subprocessStdout, binary, preserveNewlines, isStream});

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

lines 44–57why, one step back
44
45 try {46 for await (const [chunk] of onStdoutChunk) {47 yield * handleChunk(encodeChunk, splitLines, chunk);48 }49 } catch (error) {50 if (!controller.signal.aborted) {51 throw error;52 }53 } finally {54 yield * handleFinalChunks(encodeChunkFinal, splitLines, splitLinesFinal);55 }56};57

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 58why, one step back
58const getTransforms = ({subprocessStdout, binary, preserveNewlines, isStream}) => {

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

lines 59–68why, one step back
59 if (subprocessStdout.readableObjectMode) {
60 return {};61 }6263 const writableObjectMode = false;6465 if (!binary) {66 return getTextTransforms(binary, preserveNewlines, writableObjectMode);67 }68

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

line 69why, one step back
69 return isStream ? {} : getBinaryTransforms(binary, writableObjectMode);

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

lines 70–72why, one step back
70};
7172const getTextTransforms = (binary, preserveNewlines, writableObjectMode) => {

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 73–74why, one step back
73 const {transform: encodeChunk, final: encodeChunkFinal} = getEncodingTransformGenerator(binary, writableObjectMode, false);
74 const {transform: splitLines, final: splitLinesFinal} = getSplitLinesGenerator({binary, preserveNewlines, writableObjectMode, state: {}});

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

lines 75–77why, one step back
75 return {encodeChunk, encodeChunkFinal, splitLines, splitLinesFinal};
76};77

Prevent handling binary data like text data (#924)ehmickybefore adoptionaf3aefd

lines 78–79why, one step back
78const getBinaryTransforms = (binary, writableObjectMode) => {
79 const {transform: encodeChunk} = getEncodingTransformGenerator(binary, writableObjectMode, false);

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 80–82why, one step back
80 return {encodeChunk};
81};82

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

lines 83–89why, one step back
83const identityGenerator = function * (chunk) {
84 yield chunk;85};8687const noopGenerator = function * () {};8889const handleChunk = function * (encodeChunk, splitLines, chunk) {

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 90–91why, one step back
90 for (const encodedChunk of encodeChunk(chunk)) {
91 yield * splitLines(encodedChunk);

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

lines 92–95why, one step back
92 }
93};9495const handleFinalChunks = function * (encodeChunkFinal, splitLines, splitLinesFinal) {

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 96–97why, one step back
96 for (const encodedChunk of encodeChunkFinal()) {
97 yield * splitLines(encodedChunk);

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

lines 98–101why, one step back
98 }
99100 yield * splitLinesFinal();101};