Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 1why, one step back
1import 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 back
2import {setAllStream} from '../utils/standard-stream.js';
3import {resumeStream} from '../io/contents.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 4–6why, one step back
4import {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 back
7export const makeAllStream = ({stdout, stderr}, {all}) => {
8 if (!all) {9 return;10 }

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 11why, one step back
11

Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f

line 12why, one step back
12 const fileDescriptors = [[stdout, 1], [stderr, 2]].filter(([stream]) => stream);

TweaksSindre Sorhusbefore adoption698a1b8

line 13why, one step back
13 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 back
14};
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];22

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 23why, one step back
23// Read the contents of `subprocess.all` and|or wait for its completion

Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f

lines 24–44why, one step back
24export 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 back
45

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 46why, one step back
46const getAllStream = ({stdout, stderr}, all, [, bufferStdout, bufferStderr]) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 47–51why, one step back
47 const buffer = bufferStdout || bufferStderr;
48 if (!buffer) {49 return {stream: all, buffer};50 }51

Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f

lines 52–55why, one step back
52 if (!all) {
53 return {stream: undefined, buffer};54 }55

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 56why, one step back
56 if (!bufferStdout) {

Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f

line 57why, one step back
57 return {stream: stderr, buffer, drainedStream: all};

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 58–59why, one step back
58 }
59

TweaksSindre Sorhusbefore adoption698a1b8

line 60why, one step back
60 return bufferStderr ? {stream: all, buffer} : {stream: stdout, buffer, drainedStream: all};

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 61–66why, one step back
61};
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 back
67const getAllMixed = ({stdout, stderr}, all) => all

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 68–70why, one step back
68 && stdout
69 && stderr70 && stdout.readableObjectMode !== stderr.readableObjectMode;