Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add result.output #54

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const getResult = async (nodeChildProcess, input, context) => {
try {
await Promise.race([onClose, ...onStreamErrors(instance)]);
checkFailure(context, getErrorOutput(instance));
return getOutput(context);
return getOutputs(context);
} catch (error) {
await Promise.allSettled([onClose]);
throw getResultError(error, instance, context);
Expand All @@ -134,7 +134,7 @@ const useInput = (instance, input) => {
}
};

const initState = () => ({stdout: '', stderr: ''});
const initState = () => ({stdout: '', stderr: '', output: ''});

const bufferOutput = (stream, {state}, streamName) => {
if (!stream) {
Expand All @@ -148,7 +148,9 @@ const bufferOutput = (stream, {state}, streamName) => {

state.isIterating = false;
stream.on('data', chunk => {
state[streamName] += chunk;
for (const outputName of [streamName, 'output']) {
state[outputName] += chunk;
}
});
};

Expand All @@ -168,7 +170,7 @@ const IGNORED_CODES = new Set(['ERR_STREAM_PREMATURE_CLOSE', 'EPIPE']);
const getResultError = (error, instance, context) => Object.assign(
getErrorInstance(error, context),
getErrorOutput(instance),
getOutput(context),
getOutputs(context),
);

const getErrorInstance = (error, {command}) => error?.message.startsWith('Command ')
Expand All @@ -181,14 +183,15 @@ const getErrorOutput = ({exitCode, signalCode}) => ({
...(signalCode === null ? {} : {signalName: signalCode}),
});

const getOutput = ({state: {stdout, stderr}, command, start}) => ({
stdout: stripNewline(stdout),
stderr: stripNewline(stderr),
const getOutputs = ({state: {stdout, stderr, output}, command, start}) => ({
stdout: getOutput(stdout),
stderr: getOutput(stderr),
output: getOutput(output),
command,
durationMs: Number(process.hrtime.bigint() - start) / 1e6,
});

const stripNewline = input => input?.at(-1) === '\n'
const getOutput = input => input?.at(-1) === '\n'
? input.slice(0, input.at(-2) === '\r' ? -2 : -1)
: input;

Expand Down
Loading