loop.js
This file
Path
lib/convert/loop.js
118 lines · 4.2 KiB · JavaScript
Last changed by
Fix `lines` option (#957)ehmickybefore adoption15ab4e6
History of this fileTake it
Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 1–4why, one step back1import {on} from 'node:events';2import {getEncodingTransformGenerator} from '../stdio/encoding-transform.js';3import {getSplitLinesGenerator} from '../stdio/split.js';4Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 5–6why, one step back5// Iterate over lines of `subprocess.stdout`, used by `subprocess.readable|duplex|iterable()`6export const iterateOnSubprocessStream = ({subprocessStdout, subprocess, binary, shouldEncode, preserveNewlines}) => {Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 7–8why, one step back7 const controller = new AbortController();8 stopReadingOnExit(subprocess, controller);Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 9–16why, one step back9 return iterateOnStream({10 stream: subprocessStdout,11 controller,12 writableObjectMode: subprocessStdout.readableObjectMode,13 binary,14 shouldEncode,15 shouldSplit: true,16 preserveNewlines,Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 17–27why, one step back17 });18};1920const stopReadingOnExit = async (subprocess, controller) => {21 try {22 await subprocess;23 } catch {} finally {24 controller.abort();25 }26};27Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 28–62why, one step back28// Iterate over lines of `subprocess.stdout`, used by `result.stdout` + `lines: true` option29export const iterateForResult = ({stream, onStreamEnd, lines, encoding, stripFinalNewline}) => {30 const controller = new AbortController();31 stopReadingOnStreamEnd(controller, onStreamEnd);32 return iterateOnStream({33 stream,34 controller,35 writableObjectMode: false,36 binary: encoding === 'buffer',37 shouldEncode: true,38 shouldSplit: lines,39 preserveNewlines: !stripFinalNewline,40 });41};4243const stopReadingOnStreamEnd = async (controller, onStreamEnd) => {44 try {45 await onStreamEnd;46 } catch {} finally {47 controller.abort();48 }49};5051const iterateOnStream = ({stream, controller, writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines}) => {52 const onStdoutChunk = on(stream, 'data', {53 signal: controller.signal,54 highWaterMark: HIGH_WATER_MARK,55 // Backward compatibility with older name for this option56 // See https://github.com/nodejs/node/pull/52080#discussion_r152522786157 // @todo Remove after removing support for Node 2158 highWatermark: HIGH_WATER_MARK,59 });60 return iterateOnData({onStdoutChunk, controller, writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines});61};62Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 63–71why, one step back63// @todo: replace with `getDefaultHighWaterMark(true)` after dropping support for Node <18.17.064export const DEFAULT_OBJECT_HIGH_WATER_MARK = 16;6566// The `highWaterMark` of `events.on()` is measured in number of events, not in bytes.67// 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.68// Therefore, we use the value of `getDefaultHighWaterMark(true)`.69// Note: this option does not exist on Node 18, but this is ok since the logic works without it. It just consumes more memory.70const HIGH_WATER_MARK = DEFAULT_OBJECT_HIGH_WATER_MARK;71Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 72–73why, one step back72const iterateOnData = async function * ({onStdoutChunk, controller, writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines}) {73 const {encodeChunk, encodeChunkFinal, splitLines, splitLinesFinal} = getTransforms({writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines});Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 74–87why, one step back7475 try {76 for await (const [chunk] of onStdoutChunk) {77 yield * handleChunk(encodeChunk, splitLines, chunk);78 }79 } catch (error) {80 if (!controller.signal.aborted) {81 throw error;82 }83 } finally {84 yield * handleFinalChunks(encodeChunkFinal, splitLines, splitLinesFinal);85 }86};87Fix `lines` option (#957)ehmickybefore adoption15ab4e6
lines 88–96why, one step back88const getTransforms = ({writableObjectMode, binary, shouldEncode, shouldSplit, preserveNewlines}) => {89 const {90 transform: encodeChunk = identityGenerator,91 final: encodeChunkFinal = noopGenerator,92 } = getEncodingTransformGenerator(binary, writableObjectMode || !shouldEncode) ?? {};93 const {94 transform: splitLines = identityGenerator,95 final: splitLinesFinal = noopGenerator,96 } = getSplitLinesGenerator(binary, preserveNewlines, writableObjectMode || !shouldSplit, {}) ?? {};Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
line 97why, one step back97 return {encodeChunk, encodeChunkFinal, splitLines, splitLinesFinal};Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 98–99why, one step back98};99Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 100–106why, one step back100const identityGenerator = function * (chunk) {101 yield chunk;102};103104const noopGenerator = function * () {};105106const handleChunk = function * (encodeChunk, splitLines, chunk) {Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 107–108why, one step back107 for (const encodedChunk of encodeChunk(chunk)) {108 yield * splitLines(encodedChunk);Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 109–112why, one step back109 }110};111112const handleFinalChunks = function * (encodeChunkFinal, splitLines, splitLinesFinal) {Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 113–114why, one step back113 for (const encodedChunk of encodeChunkFinal()) {114 yield * splitLines(encodedChunk);Add `binary` and `preserveNewlines` options to `subprocess.readable()` (#922)ehmickybefore adoption56ee48e
lines 115–118why, one step back115 }116117 yield * splitLinesFinal();118};