Skip to content
Myr

result.js

This file

Path

lib/return/result.js

210 lines · 4.4 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.

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 1why, one step back
1import {getSignalDescription} from '../terminate/signal.js';

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

line 2why, one step back
2import {getAllSync, getAllSyncFds} from '../resolve/all-sync.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 3–4why, one step back
3import {getDurationMs} from './duration.js';
4import {getFinalError} from './final-error.js';

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

line 5why, one step back
5import {createMessages, getErrorCode} from './message.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 6–12why, one step back
6
7// Object returned on subprocess success8export const makeSuccessResult = ({9 command,10 escapedCommand,11 stdio,12 all,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 13why, one step back
13 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 14–23why, one step back
14 options: {cwd},
15 startTime,16}) => omitUndefinedProperties({17 command,18 escapedCommand,19 cwd,20 durationMs: getDurationMs(startTime),21 failed: false,22 timedOut: false,23 isCanceled: false,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 24why, one step back
24 isGracefullyCanceled: false,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 25–26why, one step back
25 isTerminated: false,
26 isMaxBuffer: false,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 27why, one step back
27 isForcefullyTerminated: false,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 28–32why, one step back
28 exitCode: 0,
29 stdout: stdio[1],30 stderr: stdio[2],31 all,32 stdio,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 33why, one step back
33 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 34–45why, one step back
34 pipedFrom: [],
35});3637// Object returned on subprocess failure before spawning38export const makeEarlyError = ({39 error,40 command,41 escapedCommand,42 fileDescriptors,43 options,44 startTime,45 isSync,

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

lines 46–47why, one step back
46 stdio = Array.from({length: fileDescriptors.length}),
47 all,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 48–54why, one step back
48}) => makeError({
49 error,50 command,51 escapedCommand,52 startTime,53 timedOut: false,54 isCanceled: false,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 55why, one step back
55 isGracefullyCanceled: false,

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 56why, one step back
56 isMaxBuffer: false,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 57why, one step back
57 isForcefullyTerminated: false,

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

lines 58–59why, one step back
58 stdio,
59 all,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 60why, one step back
60 ipcOutput: [],

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 61–64why, one step back
61 options,
62 isSync,63});64

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

lines 65–84why, one step back
65// When the subprocess fails to spawn, its output is empty, with the same shape as when it spawns but prints nothing
66export const getEarlyErrorOutput = (fileDescriptors, options) => {67 const stdio = fileDescriptors.map((fileDescriptor, fdNumber) => getEmptyOutput(fileDescriptor, fdNumber, options));68 return {stdio, all: getAllSync(stdio, getAllSyncFds(stdio), options)};69};7071const getEmptyOutput = ({direction, objectMode}, fdNumber, {stdio, buffer, lines, encoding}) => {72 if (direction === 'input' || !PIPED_STDIO.has(stdio[fdNumber]) || !buffer[fdNumber]) {73 return;74 }7576 if (objectMode || lines[fdNumber]) {77 return [];78 }7980 return encoding === 'buffer' ? new Uint8Array() : '';81};8283const PIPED_STDIO = new Set(['pipe', 'overlapped']);84

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 85–92why, one step back
85// Object returned on subprocess failure
86export const makeError = ({87 error: originalError,88 command,89 escapedCommand,90 startTime,91 timedOut,92 isCanceled,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 93why, one step back
93 isGracefullyCanceled,

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 94why, one step back
94 isMaxBuffer,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 95why, one step back
95 isForcefullyTerminated,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 96–99why, one step back
96 exitCode: rawExitCode,
97 signal: rawSignal,98 stdio,99 all,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 100why, one step back
100 ipcOutput,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 101–108why, one step back
101 options: {
102 timeoutDuration,103 timeout = timeoutDuration,104 forceKillAfterDelay,105 killSignal,106 cwd,107 maxBuffer,108 },

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 109–114why, one step back
109 isSync,
110}) => {111 const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal);112 const {originalMessage, shortMessage, message} = createMessages({113 stdio,114 all,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 115why, one step back
115 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 116–122why, one step back
116 originalError,
117 signal,118 signalDescription,119 exitCode,120 escapedCommand,121 timedOut,122 isCanceled,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 123why, one step back
123 isGracefullyCanceled,

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 124why, one step back
124 isMaxBuffer,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 125–127why, one step back
125 isForcefullyTerminated,
126 forceKillAfterDelay,127 killSignal,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 128–139why, one step back
128 maxBuffer,
129 timeout,130 cwd,131 });132 const error = getFinalError(originalError, message, isSync);133 Object.assign(error, getErrorProperties({134 error,135 command,136 escapedCommand,137 startTime,138 timedOut,139 isCanceled,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 140why, one step back
140 isGracefullyCanceled,

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 141why, one step back
141 isMaxBuffer,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 142why, one step back
142 isForcefullyTerminated,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 143–147why, one step back
143 exitCode,
144 signal,145 signalDescription,146 stdio,147 all,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 148why, one step back
148 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 149–162why, one step back
149 cwd,
150 originalMessage,151 shortMessage,152 }));153 return error;154};155156const getErrorProperties = ({157 error,158 command,159 escapedCommand,160 startTime,161 timedOut,162 isCanceled,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 163why, one step back
163 isGracefullyCanceled,

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 164why, one step back
164 isMaxBuffer,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 165why, one step back
165 isForcefullyTerminated,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 166–170why, one step back
166 exitCode,
167 signal,168 signalDescription,169 stdio,170 all,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 171why, one step back
171 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 172–184why, one step back
172 cwd,
173 originalMessage,174 shortMessage,175}) => omitUndefinedProperties({176 shortMessage,177 originalMessage,178 command,179 escapedCommand,180 cwd,181 durationMs: getDurationMs(startTime),182 failed: true,183 timedOut,184 isCanceled,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 185why, one step back
185 isGracefullyCanceled,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 186–187why, one step back
186 isTerminated: signal !== undefined,
187 isMaxBuffer,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 188why, one step back
188 isForcefullyTerminated,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 189–191why, one step back
189 exitCode,
190 signal,191 signalDescription,

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

line 192why, one step back
192 code: getErrorCode(error.cause),

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 193–196why, one step back
193 stdout: stdio[1],
194 stderr: stdio[2],195 all,196 stdio,

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 197why, one step back
197 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 198–207why, one step back
198 pipedFrom: [],
199});200201const omitUndefinedProperties = result => Object.fromEntries(Object.entries(result).filter(([, value]) => value !== undefined));202203// `signal` and `exitCode` emitted on `subprocess.on('exit')` event can be `null`.204// We normalize them to `undefined`205const normalizeExitPayload = (rawExitCode, rawSignal) => {206 const exitCode = rawExitCode === null ? undefined : rawExitCode;207 const signal = rawSignal === null ? undefined : rawSignal;

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 208why, one step back
208 const signalDescription = signal === undefined ? undefined : getSignalDescription(rawSignal);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 209–210why, one step back
209 return {exitCode, signal, signalDescription};
210};