all-async.js
This file
Path
lib/resolve/all-async.js
70 lines · 2.9 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
line 1why, one step back1import mergeStreams from '@sindresorhus/merge-streams';Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 2–3why, one step back2import {setAllStream} from '../utils/standard-stream.js';3import {resumeStream} from '../io/contents.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 4–6why, one step back4import {waitForSubprocessStream} from './stdio.js';56// `all` interleaves `stdout` and `stderr`Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 7–10why, one step back7export const makeAllStream = ({stdout, stderr}, {all}) => {8 if (!all) {9 return;10 }100% test coverage (#1018)ehmickybefore adoption9f65b86
line 11why, one step back11Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 12why, one step back12 const fileDescriptors = [[stdout, 1], [stderr, 2]].filter(([stream]) => stream);TweaksSindre Sorhusbefore adoption698a1b8
line 13why, one step back13 return fileDescriptors.length === 0 ? undefined : setAllStream(mergeStreams(fileDescriptors.map(([stream]) => stream)));Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 14–22why, one step back14};1516// The file descriptors which `result.all` contains, which the `all` options must follow17// Unlike `subprocess.all`, this excludes the file descriptors which are not buffered, since their output is not included18export const getAllFds = ({stdout, stderr}, [, bufferStdout, bufferStderr]) => [19 ...(stdout !== null && bufferStdout ? [1] : []),20 ...(stderr !== null && bufferStderr ? [2] : []),21];22100% test coverage (#1018)ehmickybefore adoption9f65b86
line 23why, one step back23// Read the contents of `subprocess.all` and|or wait for its completionFix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 24–44why, one step back24export const waitForAllStream = ({subprocess, all, encoding, buffer, maxBuffer, lines, stripFinalNewline, verboseInfo, streamInfo}) => {25 const {stream, buffer: bufferAll, drainedStream} = getAllStream(subprocess, all, buffer);26 return Promise.all([27 waitForSubprocessStream({28 stream,29 buffer: bufferAll,30 fdNumber: 'all',31 encoding,32 maxBuffer: maxBuffer[1] + maxBuffer[2],33 lines: lines[1] || lines[2],34 allFds: getAllFds(subprocess, buffer),35 allMixed: getAllMixed(subprocess, all),36 stripFinalNewline,37 verboseInfo,38 streamInfo,39 }),40 // When only one of the two file descriptors is buffered, its stream is read instead of the merged one.41 // The merged one is then drained, otherwise it would apply backpressure on that file descriptor and hang the subprocess.42 drainedStream === undefined ? undefined : resumeStream(drainedStream),43 ]).then(([output]) => output);44};100% test coverage (#1018)ehmickybefore adoption9f65b86
line 45why, one step back45Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 46why, one step back46const getAllStream = ({stdout, stderr}, all, [, bufferStdout, bufferStderr]) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 47–51why, one step back47 const buffer = bufferStdout || bufferStderr;48 if (!buffer) {49 return {stream: all, buffer};50 }51Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 52–55why, one step back52 if (!all) {53 return {stream: undefined, buffer};54 }55100% test coverage (#1018)ehmickybefore adoption9f65b86
line 56why, one step back56 if (!bufferStdout) {Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 57why, one step back57 return {stream: stderr, buffer, drainedStream: all};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 58–59why, one step back58 }59TweaksSindre Sorhusbefore adoption698a1b8
line 60why, one step back60 return bufferStderr ? {stream: all, buffer} : {stream: stdout, buffer, drainedStream: all};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 61–66why, one step back61};6263// When `subprocess.stdout` is in objectMode but not `subprocess.stderr` (or the opposite), we need to use both:64// - `getStreamAsArray()` for the chunks in objectMode, to return as an array without changing each chunk65// - `getStreamAsArrayBuffer()` or `getStream()` for the chunks not in objectMode, to convert them from Buffers to string or Uint8Array66// We do this by emulating the Buffer -> string|Uint8Array conversion performed by `get-stream` with our own, which is identical.Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 67why, one step back67const getAllMixed = ({stdout, stderr}, all) => all100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 68–70why, one step back68 && stdout69 && stderr70 && stdout.readableObjectMode !== stderr.readableObjectMode;