More event refactoring work
This commit is contained in:
+28
-22
@@ -1,3 +1,4 @@
|
||||
import { HttpSpacePrimitives } from "../../common/spaces/http_space_primitives.ts";
|
||||
import { SysCallMapping } from "../../plugos/system.ts";
|
||||
import { SyscallResponse } from "../../server/rpc.ts";
|
||||
import { Client } from "../client.ts";
|
||||
@@ -5,29 +6,34 @@ import { Client } from "../client.ts";
|
||||
export function proxySyscalls(client: Client, names: string[]): SysCallMapping {
|
||||
const syscalls: SysCallMapping = {};
|
||||
for (const name of names) {
|
||||
syscalls[name] = async (_ctx, ...args: any[]) => {
|
||||
if (!client.remoteSpacePrimitives) {
|
||||
throw new Error("Not supported");
|
||||
}
|
||||
const resp = await client.remoteSpacePrimitives.authenticatedFetch(
|
||||
`${client.remoteSpacePrimitives.url}/.rpc`,
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
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;
|
||||
}
|
||||
syscalls[name] = (_ctx, ...args: any[]) => {
|
||||
return proxySyscall(client.remoteSpacePrimitives, name, args);
|
||||
};
|
||||
}
|
||||
return syscalls;
|
||||
}
|
||||
|
||||
export async function proxySyscall(
|
||||
httpSpacePrimitives: HttpSpacePrimitives,
|
||||
name: string,
|
||||
args: any[],
|
||||
): Promise<any> {
|
||||
const resp = await httpSpacePrimitives.authenticatedFetch(
|
||||
`${httpSpacePrimitives.url}/.rpc`,
|
||||
{
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user