result.js
This file
Path
lib/return/result.js
155 lines · 3.0 KiB · JavaScript
Last changed by
Do not set `undefined` properties on the return value (#992)ehmickybefore adoption0d33b6d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import {signalsByName} from 'human-signals';import {getDurationMs} from './duration.js';import {getFinalError} from './final-error.js';import {createMessages} from './message.js';
// Object returned on subprocess successexport const makeSuccessResult = ({ command, escapedCommand, stdio, all, options: {cwd}, startTime,}) => omitUndefinedProperties({ command, escapedCommand, cwd, durationMs: getDurationMs(startTime), failed: false, timedOut: false, isCanceled: false, isTerminated: false, isMaxBuffer: false, exitCode: 0, stdout: stdio[1], stderr: stdio[2], all, stdio, pipedFrom: [],});
// Object returned on subprocess failure before spawningexport const makeEarlyError = ({ error, command, escapedCommand, fileDescriptors, options, startTime, isSync,}) => makeError({ error, command, escapedCommand, startTime, timedOut: false, isCanceled: false, isMaxBuffer: false, stdio: Array.from({length: fileDescriptors.length}), options, isSync,});
// Object returned on subprocess failureexport const makeError = ({ error: originalError, command, escapedCommand, startTime, timedOut, isCanceled, isMaxBuffer, exitCode: rawExitCode, signal: rawSignal, stdio, all, options: {timeoutDuration, timeout = timeoutDuration, cwd, maxBuffer}, isSync,}) => { const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal); const {originalMessage, shortMessage, message} = createMessages({ stdio, all, originalError, signal, signalDescription, exitCode, escapedCommand, timedOut, isCanceled, isMaxBuffer, maxBuffer, timeout, cwd, }); const error = getFinalError(originalError, message, isSync); Object.assign(error, getErrorProperties({ error, command, escapedCommand, startTime, timedOut, isCanceled, isMaxBuffer, exitCode, signal, signalDescription, stdio, all, cwd, originalMessage, shortMessage, })); return error;};
const getErrorProperties = ({ error, command, escapedCommand, startTime, timedOut, isCanceled, isMaxBuffer, exitCode, signal, signalDescription, stdio, all, cwd, originalMessage, shortMessage,}) => omitUndefinedProperties({ shortMessage, originalMessage, command, escapedCommand, cwd, durationMs: getDurationMs(startTime), failed: true, timedOut, isCanceled, isTerminated: signal !== undefined, isMaxBuffer, exitCode, signal, signalDescription, code: error.cause?.code, stdout: stdio[1], stderr: stdio[2], all, stdio, pipedFrom: [],});
const omitUndefinedProperties = result => Object.fromEntries(Object.entries(result).filter(([, value]) => value !== undefined));
// `signal` and `exitCode` emitted on `subprocess.on('exit')` event can be `null`.// We normalize them to `undefined`const normalizeExitPayload = (rawExitCode, rawSignal) => { const exitCode = rawExitCode === null ? undefined : rawExitCode; const signal = rawSignal === null ? undefined : rawSignal; const signalDescription = signal === undefined ? undefined : signalsByName[rawSignal].description; return {exitCode, signal, signalDescription};};