Skip to content
Myr

all-sync.js

This file

Path

lib/resolve/all-sync.js

56 lines · 2.6 KiB · JavaScript

Last changed by

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

History of this file

Take it

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

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

line 1why, one step back
1import {Buffer} from 'node:buffer';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 2why, one step back
2import {isUint8Array, concatUint8Arrays} from '../utils/uint-array.js';

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

lines 3–4why, one step back
3import {splitLinesSync} from '../transform/split.js';
4import {getStripFinalNewline} from '../io/strip-newline.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 5why, one step back
5

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

lines 6–11why, one step back
6// The file descriptors which `result.all` interleaves. Those are the ones with an output, like with the `all` stream.
7export const getAllSyncFds = ([, stdout, stderr]) => [8 ...(stdout === undefined ? [] : [1]),9 ...(stderr === undefined ? [] : [2]),10];11

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 12why, one step back
12// Retrieve `result.all` with synchronous methods

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

line 13why, one step back
13export const getAllSync = ([, stdout, stderr], allFds, options) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 14–18why, one step back
14 if (!options.all) {
15 return;16 }1718 if (stdout === undefined) {

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

line 19why, one step back
19 return splitAllLines(stderr, allFds, options);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 20–22why, one step back
20 }
2122 if (stderr === undefined) {

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

line 23why, one step back
23 return splitAllLines(stdout, allFds, options);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 24–25why, one step back
24 }
25

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

lines 26–30why, one step back
26 // Like asynchronous methods, `all` is split into lines as soon as the `lines` option applies to either file descriptor.
27 // A file descriptor which uses the `lines` option is already split with its own `stripFinalNewline` value, like `result.stdout` and `result.stderr` are. The other one is split with the `stripFinalNewline` value of `result.all`.28 // This differs from asynchronous methods, which cannot tell which file descriptor the merged output's last newline belongs to.29 if (hasLines(options)) {30 return [splitAllLines(stdout, allFds, options), splitAllLines(stderr, allFds, options)].flat();

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 31–32why, one step back
31 }
32

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

lines 33–34why, one step back
33 // Without the `lines` option, arrays can only come from object mode, which does not apply to the other file descriptor.
34 // Like asynchronous methods, that other file descriptor is omitted when it is empty.

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

line 35why, one step back
35 if (Array.isArray(stdout) || Array.isArray(stderr)) {

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

line 36why, one step back
36 return [stdout, stderr].filter(value => value.length > 0).flat();

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 37–38why, one step back
37 }
38

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

lines 39–43why, one step back
39 if (isUint8Array(stdout) && isUint8Array(stderr)) {
40 return concatUint8Arrays([stdout, stderr]);41 }4243 return BASE64_ENCODINGS.has(options.encoding) ? concatBase64(stdout, stderr, options.encoding) : `${stdout}${stderr}`;

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 44why, one step back
44};

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

lines 45–49why, one step back
45
46// Unlike other encodings, base64 strings cannot be concatenated, since the first one might end with padding47const concatBase64 = (stdout, stderr, encoding) => Buffer.concat([Buffer.from(stdout, encoding), Buffer.from(stderr, encoding)]).toString(encoding);4849const BASE64_ENCODINGS = new Set(['base64', 'base64url']);

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

lines 50–53why, one step back
50
51const hasLines = ({lines}) => Boolean(lines[1] || lines[2]);5253// Each file descriptor is either already split into lines, or is a string which still needs to be split

TweaksSindre Sorhusbefore adoption698a1b8

line 54why, one step back
54const splitAllLines = (value, allFds, options) => value === undefined || Array.isArray(value) || !hasLines(options)

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

lines 55–56why, one step back
55 ? value
56 : splitLinesSync(value, !getStripFinalNewline(options.stripFinalNewline, 'all', allFds), false);