silverbullet/web/syscalls/util.ts

39 lines
1.0 KiB
TypeScript
Raw Normal View History

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