Now all communication happens over sockets
This commit is contained in:
+14
-84
@@ -1,78 +1,16 @@
|
||||
import { Manifest } from "./types";
|
||||
import { WebworkerSandbox } from "./worker_sandbox";
|
||||
|
||||
interface SysCallMapping {
|
||||
// TODO: Better typing
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export class FunctionWorker {
|
||||
private worker: Worker;
|
||||
private inited: Promise<any>;
|
||||
private initCallback: any;
|
||||
private invokeResolve?: (result?: any) => void;
|
||||
private invokeReject?: (reason?: any) => void;
|
||||
private plug: Plug<any>;
|
||||
|
||||
constructor(plug: Plug<any>, name: string, code: string) {
|
||||
// let worker = window.Worker;
|
||||
this.worker = new Worker(new URL("function_worker.ts", import.meta.url), {
|
||||
type: "module",
|
||||
});
|
||||
|
||||
this.worker.onmessage = this.onmessage.bind(this);
|
||||
this.worker.postMessage({
|
||||
type: "boot",
|
||||
name: name,
|
||||
code: code,
|
||||
});
|
||||
this.inited = new Promise((resolve) => {
|
||||
this.initCallback = resolve;
|
||||
});
|
||||
this.plug = plug;
|
||||
}
|
||||
|
||||
async onmessage(evt: MessageEvent) {
|
||||
let data = evt.data;
|
||||
if (!data) return;
|
||||
switch (data.type) {
|
||||
case "inited":
|
||||
this.initCallback();
|
||||
break;
|
||||
case "syscall":
|
||||
let result = await this.plug.system.syscall(data.name, data.args);
|
||||
|
||||
this.worker.postMessage({
|
||||
type: "syscall-response",
|
||||
id: data.id,
|
||||
data: result,
|
||||
});
|
||||
break;
|
||||
case "result":
|
||||
this.invokeResolve!(data.result);
|
||||
break;
|
||||
case "error":
|
||||
this.invokeReject!(data.reason);
|
||||
break;
|
||||
default:
|
||||
console.error("Unknown message type", data);
|
||||
}
|
||||
}
|
||||
|
||||
async invoke(args: Array<any>): Promise<any> {
|
||||
await this.inited;
|
||||
this.worker.postMessage({
|
||||
type: "invoke",
|
||||
args: args,
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
this.invokeResolve = resolve;
|
||||
this.invokeReject = reject;
|
||||
});
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.worker.terminate();
|
||||
}
|
||||
export interface Sandbox {
|
||||
isLoaded(name: string): boolean;
|
||||
load(name: string, code: string): Promise<void>;
|
||||
invoke(name: string, args: any[]): Promise<any>;
|
||||
stop(): void;
|
||||
}
|
||||
|
||||
export interface PlugLoader<HookT> {
|
||||
@@ -81,12 +19,13 @@ export interface PlugLoader<HookT> {
|
||||
|
||||
export class Plug<HookT> {
|
||||
system: System<HookT>;
|
||||
private runningFunctions: Map<string, FunctionWorker>;
|
||||
// private runningFunctions: Map<string, FunctionWorker>;
|
||||
functionWorker: WebworkerSandbox;
|
||||
public manifest?: Manifest<HookT>;
|
||||
|
||||
constructor(system: System<HookT>, name: string) {
|
||||
this.system = system;
|
||||
this.runningFunctions = new Map<string, FunctionWorker>();
|
||||
this.functionWorker = new WebworkerSandbox(this);
|
||||
}
|
||||
|
||||
async load(manifest: Manifest<HookT>) {
|
||||
@@ -95,16 +34,13 @@ export class Plug<HookT> {
|
||||
}
|
||||
|
||||
async invoke(name: string, args: Array<any>): Promise<any> {
|
||||
let worker = this.runningFunctions.get(name);
|
||||
if (!worker) {
|
||||
worker = new FunctionWorker(
|
||||
this,
|
||||
if (!this.functionWorker.isLoaded(name)) {
|
||||
await this.functionWorker.load(
|
||||
name,
|
||||
this.manifest!.functions[name].code!
|
||||
);
|
||||
this.runningFunctions.set(name, worker);
|
||||
}
|
||||
return await worker.invoke(args);
|
||||
return await this.functionWorker.invoke(name, args);
|
||||
}
|
||||
|
||||
async dispatchEvent(name: string, data?: any): Promise<any[]> {
|
||||
@@ -122,13 +58,7 @@ export class Plug<HookT> {
|
||||
}
|
||||
|
||||
async stop() {
|
||||
for (const [functionname, worker] of Object.entries(
|
||||
this.runningFunctions
|
||||
)) {
|
||||
console.log(`Stopping ${functionname}`);
|
||||
worker.stop();
|
||||
}
|
||||
this.runningFunctions = new Map<string, FunctionWorker>();
|
||||
this.functionWorker.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +71,7 @@ export class System<HookT> {
|
||||
this.registeredSyscalls = {};
|
||||
}
|
||||
|
||||
registerSyscalls(...registrationObjects: Array<SysCallMapping>) {
|
||||
registerSyscalls(...registrationObjects: SysCallMapping[]) {
|
||||
for (const registrationObject of registrationObjects) {
|
||||
for (let p in registrationObject) {
|
||||
this.registeredSyscalls[p] = registrationObject[p];
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<body>
|
||||
<script type="module">
|
||||
import "./function_worker";
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,11 +1,13 @@
|
||||
declare global {
|
||||
function syscall(id: string, name: string, args: any[]): Promise<any>;
|
||||
function syscall(id: number, name: string, args: any[]): Promise<any>;
|
||||
}
|
||||
import { ControllerMessage, WorkerMessage, WorkerMessageType } from "./types";
|
||||
import { safeRun } from "./util";
|
||||
let func: Function | null = null;
|
||||
let pendingRequests = new Map<string, (result: unknown) => void>();
|
||||
|
||||
self.syscall = async (id: string, name: string, args: any[]) => {
|
||||
let loadedFunctions = new Map<string, Function>();
|
||||
let pendingRequests = new Map<number, (result: unknown) => void>();
|
||||
|
||||
self.syscall = async (id: number, name: string, args: any[]) => {
|
||||
return await new Promise((resolve, reject) => {
|
||||
pendingRequests.set(id, resolve);
|
||||
self.postMessage({
|
||||
@@ -38,51 +40,56 @@ function wrapScript(code: string): string {
|
||||
return fn["default"].apply(null, arguments);`;
|
||||
}
|
||||
|
||||
self.addEventListener("message", (event) => {
|
||||
self.addEventListener("message", (event: { data: WorkerMessage }) => {
|
||||
safeRun(async () => {
|
||||
let messageEvent = event;
|
||||
let data = messageEvent.data;
|
||||
switch (data.type) {
|
||||
case "boot":
|
||||
case "load":
|
||||
console.log("Booting", data.name);
|
||||
func = new Function(wrapScript(data.code));
|
||||
loadedFunctions.set(data.name!, new Function(wrapScript(data.code!)));
|
||||
self.postMessage({
|
||||
type: "inited",
|
||||
});
|
||||
name: data.name,
|
||||
} as ControllerMessage);
|
||||
break;
|
||||
case "invoke":
|
||||
if (!func) {
|
||||
throw new Error("No function loaded");
|
||||
let fn = loadedFunctions.get(data.name!);
|
||||
if (!fn) {
|
||||
throw new Error(`Function not loaded: ${data.name}`);
|
||||
}
|
||||
try {
|
||||
let result = await Promise.resolve(func(...(data.args || [])));
|
||||
let result = await Promise.resolve(fn(...(data.args || [])));
|
||||
self.postMessage({
|
||||
type: "result",
|
||||
id: data.id,
|
||||
result: result,
|
||||
});
|
||||
} as ControllerMessage);
|
||||
} catch (e: any) {
|
||||
self.postMessage({
|
||||
type: "error",
|
||||
id: data.id,
|
||||
reason: e.message,
|
||||
});
|
||||
} as ControllerMessage);
|
||||
throw e;
|
||||
}
|
||||
|
||||
break;
|
||||
case "syscall-response":
|
||||
let id = data.id;
|
||||
const lookup = pendingRequests.get(id);
|
||||
let syscallId = data.id!;
|
||||
const lookup = pendingRequests.get(syscallId);
|
||||
if (!lookup) {
|
||||
console.log(
|
||||
"Current outstanding requests",
|
||||
pendingRequests,
|
||||
"looking up",
|
||||
id
|
||||
syscallId
|
||||
);
|
||||
throw Error("Invalid request id");
|
||||
}
|
||||
pendingRequests.delete(id);
|
||||
pendingRequests.delete(syscallId);
|
||||
lookup(data.data);
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,28 @@ export type EventHook = {
|
||||
events: { [key: string]: string[] };
|
||||
};
|
||||
|
||||
export type WorkerMessageType = "load" | "invoke" | "syscall-response";
|
||||
|
||||
export type WorkerMessage = {
|
||||
type: WorkerMessageType;
|
||||
id?: number;
|
||||
name?: string;
|
||||
code?: string;
|
||||
args?: any[];
|
||||
data?: any;
|
||||
};
|
||||
|
||||
export type ControllerMessageType = "inited" | "result" | "error" | "syscall";
|
||||
|
||||
export type ControllerMessage = {
|
||||
type: ControllerMessageType;
|
||||
id?: number;
|
||||
name?: string;
|
||||
reason?: string;
|
||||
args?: any[];
|
||||
result: any;
|
||||
};
|
||||
|
||||
export interface Manifest<HookT> {
|
||||
hooks: HookT & EventHook;
|
||||
functions: {
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { ControllerMessage, WorkerMessage } from "./types";
|
||||
import { Plug, Sandbox } from "./runtime";
|
||||
|
||||
export class WebworkerSandbox implements Sandbox {
|
||||
private worker: Worker;
|
||||
private reqId = 0;
|
||||
|
||||
private outstandingInits = new Map<string, () => void>();
|
||||
private outstandingInvocations = new Map<
|
||||
number,
|
||||
{ resolve: (result: any) => void; reject: (e: any) => void }
|
||||
>();
|
||||
private loadedFunctions = new Set<string>();
|
||||
|
||||
constructor(readonly plug: Plug<any>) {
|
||||
this.worker = new Worker(new URL("sandbox_worker.ts", import.meta.url), {
|
||||
type: "module",
|
||||
});
|
||||
|
||||
this.worker.onmessage = this.onmessage.bind(this);
|
||||
}
|
||||
|
||||
isLoaded(name: string) {
|
||||
return this.loadedFunctions.has(name);
|
||||
}
|
||||
|
||||
async load(name: string, code: string): Promise<void> {
|
||||
this.worker.postMessage({
|
||||
type: "load",
|
||||
name: name,
|
||||
code: code,
|
||||
} as WorkerMessage);
|
||||
return new Promise((resolve) => {
|
||||
this.loadedFunctions.add(name);
|
||||
this.outstandingInits.set(name, resolve);
|
||||
});
|
||||
}
|
||||
|
||||
async onmessage(evt: { data: ControllerMessage }) {
|
||||
let data = evt.data;
|
||||
if (!data) return;
|
||||
switch (data.type) {
|
||||
case "inited":
|
||||
let initCb = this.outstandingInits.get(data.name!);
|
||||
initCb && initCb();
|
||||
this.outstandingInits.delete(data.name!);
|
||||
break;
|
||||
case "syscall":
|
||||
let result = await this.plug.system.syscall(data.name!, data.args!);
|
||||
|
||||
this.worker.postMessage({
|
||||
type: "syscall-response",
|
||||
id: data.id,
|
||||
data: result,
|
||||
} as WorkerMessage);
|
||||
break;
|
||||
case "result":
|
||||
let resultCb = this.outstandingInvocations.get(data.id!);
|
||||
resultCb && resultCb.resolve(data.result);
|
||||
break;
|
||||
case "error":
|
||||
let errCb = this.outstandingInvocations.get(data.result.id!);
|
||||
errCb && errCb.reject(data.reason);
|
||||
break;
|
||||
default:
|
||||
console.error("Unknown message type", data);
|
||||
}
|
||||
}
|
||||
|
||||
async invoke(name: string, args: any[]): Promise<any> {
|
||||
this.reqId++;
|
||||
this.worker.postMessage({
|
||||
type: "invoke",
|
||||
id: this.reqId,
|
||||
name,
|
||||
args,
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
this.outstandingInvocations.set(this.reqId, { resolve, reject });
|
||||
});
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.worker.terminate();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user