contents.js
This file
Path
lib/io/contents.js
113 lines · 3.3 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–9why, one step back1import {setImmediate} from 'node:timers/promises';2import getStream, {getStreamAsArrayBuffer, getStreamAsArray} from 'get-stream';3import {isArrayBuffer} from '../utils/uint-array.js';4import {shouldLogOutput, logLines} from '../verbose/output.js';5import {iterateForResult} from './iterate.js';6import {handleMaxBuffer} from './max-buffer.js';7import {getStripFinalNewline} from './strip-newline.js';89// Retrieve `result.stdout|stderr|all|stdio[*]`Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 10why, one step back10export const getStreamOutput = async ({stream, onStreamEnd, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, allFds, verboseInfo, streamInfo}) => {Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 11–14why, one step back11 const logPromise = logOutputAsync({12 stream,13 onStreamEnd,14 fdNumber,100% test coverage (#1018)ehmickybefore adoption9f65b86
line 15why, one step back15 encoding,Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 16why, one step back16 allMixed,100% test coverage (#1018)ehmickybefore adoption9f65b86
line 17why, one step back17 verboseInfo,Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 18–19why, one step back18 streamInfo,19 });100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 20–21why, one step back2021 if (!buffer) {Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 22why, one step back22 await Promise.all([resumeStream(stream), logPromise]);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 23–25why, one step back23 return;24 }25Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 26why, one step back26 const stripFinalNewlineValue = getStripFinalNewline(stripFinalNewline, fdNumber, allFds);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 27–34why, one step back27 const iterable = iterateForResult({28 stream,29 onStreamEnd,30 lines,31 encoding,32 stripFinalNewline: stripFinalNewlineValue,33 allMixed,34 });Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 35–53why, one step back35 const [output] = await Promise.all([36 getStreamContents({37 stream,38 iterable,39 fdNumber,40 encoding,41 maxBuffer,42 lines,43 }),44 logPromise,45 ]);46 return output;47};4849const logOutputAsync = async ({stream, onStreamEnd, fdNumber, encoding, allMixed, verboseInfo, streamInfo: {fileDescriptors}}) => {50 if (!shouldLogOutput({51 stdioItems: fileDescriptors[fdNumber]?.stdioItems,52 encoding,53 verboseInfo,100% test coverage (#1018)ehmickybefore adoption9f65b86
line 54why, one step back54 fdNumber,Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 55–62why, one step back55 })) {56 return;57 }5859 const linesIterable = iterateForResult({60 stream,61 onStreamEnd,62 lines: true,100% test coverage (#1018)ehmickybefore adoption9f65b86
line 63why, one step back63 encoding,Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
lines 64–65why, one step back64 stripFinalNewline: true,65 allMixed,100% test coverage (#1018)ehmickybefore adoption9f65b86
line 66why, one step back66 });Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
line 67why, one step back67 await logLines(linesIterable, stream, fdNumber, verboseInfo);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 68–71why, one step back68};6970// When using `buffer: false`, users need to read `subprocess.stdout|stderr|all` right away71// See https://github.com/sindresorhus/execa/issues/730 and https://github.com/sindresorhus/execa/pull/729#discussion_r1465496310Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 72why, one step back72export const resumeStream = async stream => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 73–84why, one step back73 await setImmediate();74 if (stream.readableFlowing === null) {75 stream.resume();76 }77};7879const getStreamContents = async ({stream, stream: {readableObjectMode}, iterable, fdNumber, encoding, maxBuffer, lines}) => {80 try {81 if (readableObjectMode || lines) {82 return await getStreamAsArray(iterable, {maxBuffer});83 }84TweaksSindre Sorhusbefore adoption698a1b8
line 85why, one step back85 return encoding === 'buffer' ? new Uint8Array(await getStreamAsArrayBuffer(iterable, {maxBuffer})) : (await getStream(iterable, {maxBuffer}));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 86–109why, one step back86 } catch (error) {87 return handleBufferedData(handleMaxBuffer({88 error,89 stream,90 readableObjectMode,91 lines,92 encoding,93 fdNumber,94 }));95 }96};9798// On failure, `result.stdout|stderr|all` should contain the currently buffered stream99// They are automatically closed and flushed by Node.js when the subprocess exits100// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve101export const getBufferedData = async streamPromise => {102 try {103 return await streamPromise;104 } catch (error) {105 return handleBufferedData(error);106 }107};108109// Ensure we are returning Uint8Arrays when using `encoding: 'buffer'`Fix `verbose` discarding IPC messages when its function throwsSindre Sorhusbefore adoptionb548d8b
lines 110–113why, one step back110// The error might be any value thrown by the `verbose` function, including `null` or `undefined`111const handleBufferedData = error => isArrayBuffer(error?.bufferedData)112 ? new Uint8Array(error.bufferedData)113 : error?.bufferedData;