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 fileTake 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 back1import {Buffer} from 'node:buffer';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 2why, one step back2import {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 back3import {splitLinesSync} from '../transform/split.js';4import {getStripFinalNewline} from '../io/strip-newline.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 5why, one step back5Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 6–11why, one step back6// 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];11100% test coverage (#1018)ehmickybefore adoption9f65b86
line 12why, one step back12// Retrieve `result.all` with synchronous methodsFix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 13why, one step back13export const getAllSync = ([, stdout, stderr], allFds, options) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 14–18why, one step back14 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 back19 return splitAllLines(stderr, allFds, options);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 20–22why, one step back20 }2122 if (stderr === undefined) {Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
line 23why, one step back23 return splitAllLines(stdout, allFds, options);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 24–25why, one step back24 }25Fix `result.all` and `subprocess.all` when only some file descriptors are bufferedSindre Sorhusbefore adoption758768f
lines 26–30why, one step back26 // 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 back31 }32Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
lines 33–34why, one step back33 // 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 back35 if (Array.isArray(stdout) || Array.isArray(stderr)) {Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
line 36why, one step back36 return [stdout, stderr].filter(value => value.length > 0).flat();100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 37–38why, one step back37 }38Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
lines 39–43why, one step back39 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 back44};Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
lines 45–49why, one step back4546// 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 back5051const 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 splitTweaksSindre Sorhusbefore adoption698a1b8
line 54why, one step back54const 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 back55 ? value56 : splitLinesSync(value, !getStripFinalNewline(options.stripFinalNewline, 'all', allFds), false);