Skip to content

Commit

Permalink
service.ts: fix /exec base64 on webOS 1
Browse files Browse the repository at this point in the history
  • Loading branch information
throwaway96 committed Mar 9, 2024
1 parent 909b147 commit faaa23b
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,22 +703,32 @@ function runService(): void {

const payload = message.payload as ExecPayload;

const encoding = nodeVersion.major === 0 && nodeVersion.minor < 12 ? null : 'buffer';

child_process.exec(payload.command, { encoding }, (error, stdout, stderr) => {
function commonResponse(error: child_process.ExecException | null, stdout: Buffer, stderr: Buffer) {
const response = {
error,
stdoutString: stdout.toString(),
stdoutBytes: stdout.toString('base64'),
stderrString: stderr.toString(),
stderrBytes: stderr.toString('base64'),
};
if (error) {
if (error !== null) {
message.respond(makeError(error.message, response));
} else {
message.respond(makeSuccess(response));
}
});
}

if (nodeVersion.major !== 0 && nodeVersion.minor >= 12) {
child_process.exec(payload.command, { encoding: 'buffer' }, commonResponse);
} else {
/* Node.js v0.10.x doesn't provide callback with Buffers, so fake it */
child_process.exec(
payload.command,
{ encoding: 'binary' },
(error: child_process.ExecException | null, stdout: string, stderr: string) => {
commonResponse(error, Buffer.from(stdout, 'binary'), Buffer.from(stderr, 'binary'));
},
);
}
});

/**
Expand Down

0 comments on commit faaa23b

Please sign in to comment.