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

Fix /exec on webOS 1 #172

Merged
merged 3 commits into from
Mar 5, 2024
Merged
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
29 changes: 26 additions & 3 deletions services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
}
})();

const nodeVersion = (() => {
try {
// Just in case there's a build/pre-release suffix.
const core = process.versions.node.split(/[-+]/, 1)[0] as string;
const [major, minor = 0, patch = 0] = core.split('.').map((x) => parseInt(x, 10));
return { major, minor, patch };
} catch (err) {
console.warn('getting nodeVersion failed:', err);
return { major: 0, minor: 0, patch: 0 };
}
})();

// Maps internal setting field name with filesystem flag name.
type FlagName = string;
const availableFlags = {
Expand Down Expand Up @@ -600,9 +612,20 @@
* Executes a shell command and responds with exit code, stdout and stderr.
*/
type ExecPayload = { command: string };
service.register('exec', (message) => {
service.register('exec', (message: Message) => {
if (!('command' in message.payload)) {
message.respond(makeError('missing "command"'));
return;
} else if (typeof message.payload['command'] !== 'string') {

Check failure on line 619 in services/service.ts

View workflow job for this annotation

GitHub Actions / build / build

Unnecessary 'else' after 'return'

Check failure on line 619 in services/service.ts

View workflow job for this annotation

GitHub Actions / build / build

["command"] is better written in dot notation
throwaway96 marked this conversation as resolved.
Show resolved Hide resolved
message.respond(makeError('"command" is not a string'));
return;
}

const payload = message.payload as ExecPayload;
child_process.exec(payload.command, { encoding: 'buffer' }, (error, stdout, stderr) => {

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

child_process.exec(payload.command, { encoding }, (error, stdout, stderr) => {
const response = {
error,
stdoutString: stdout.toString(),
Expand All @@ -624,7 +647,7 @@
service.register('spawn', (message) => {
const payload = message.payload as ExecPayload;
const respond = (event: string, args: Record<string, any>) => message.respond({ event, ...args });
const proc = child_process.spawn('/bin/sh', ['-c', payload.command]);
const proc = child_process.spawn('/bin/sh', ['-c', '--', payload.command]);
proc.stdout.on('data', (data) =>
respond('stdoutData', {
stdoutString: data.toString(),
Expand Down
Loading