1
0
silverbullet/web/syscalls/util.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-08-27 12:13:18 +00:00
import { HttpSpacePrimitives } from "../../common/spaces/http_space_primitives.ts";
2023-08-28 15:12:15 +00:00
import { SyscallContext, SysCallMapping } from "../../plugos/system.ts";
2023-08-26 06:31:51 +00:00
import { SyscallResponse } from "../../server/rpc.ts";
import { Client } from "../client.ts";
export function proxySyscalls(client: Client, names: string[]): SysCallMapping {
const syscalls: SysCallMapping = {};
for (const name of names) {
2023-08-28 15:12:15 +00:00
syscalls[name] = (ctx, ...args: any[]) => {
return proxySyscall(ctx, client.httpSpacePrimitives, name, args);
2023-08-26 06:31:51 +00:00
};
}
return syscalls;
}
2023-08-27 12:13:18 +00:00
export async function proxySyscall(
2023-08-28 15:12:15 +00:00
ctx: SyscallContext,
2023-08-27 12:13:18 +00:00
httpSpacePrimitives: HttpSpacePrimitives,
name: string,
args: any[],
): Promise<any> {
const resp = await httpSpacePrimitives.authenticatedFetch(
`${httpSpacePrimitives.url}/.rpc`,
{
method: "POST",
body: JSON.stringify({
2023-08-28 15:12:15 +00:00
ctx: ctx.plug.name,
2023-08-27 12:13:18 +00:00
operation: "syscall",
name,
args,
}),
},
);
const result: SyscallResponse = await resp.json();
if (result.error) {
console.error("Remote syscall error", result.error);
throw new Error(result.error);
} else {
return result.result;
}
}