silverbullet/web/syscalls/shell.ts

34 lines
894 B
TypeScript
Raw Normal View History

2024-07-30 23:33:33 +08:00
import type { SysCallMapping } from "../../lib/plugos/system.ts";
2023-07-24 15:36:33 +08:00
import type { Client } from "../client.ts";
export function shellSyscalls(
2023-07-24 15:36:33 +08:00
client: Client,
): SysCallMapping {
return {
"shell.run": async (
_ctx,
cmd: string,
args: string[],
): Promise<{ stdout: string; stderr: string; code: number }> => {
if (!client.httpSpacePrimitives) {
throw new Error("Not supported in fully local mode");
}
const resp = client.httpSpacePrimitives.authenticatedFetch(
2024-01-14 01:07:02 +08:00
`${client.httpSpacePrimitives.url}/.rpc/shell`,
{
method: "POST",
body: JSON.stringify({
cmd,
args,
}),
},
);
const { code, stderr, stdout } = await (await resp).json();
if (code !== 0) {
throw new Error(stderr);
}
return { code, stderr, stdout };
},
};
}