Fixes #193: Allowing plug overrides

This commit is contained in:
Zef Hemel
2023-08-20 19:54:31 +02:00
parent 136682ebd3
commit 2a10d50094
9 changed files with 146 additions and 40 deletions
+12 -1
View File
@@ -1,7 +1,8 @@
import { Hook } from "./types.ts";
import { Hook, Manifest } from "./types.ts";
import { EventEmitter } from "./event.ts";
import type { SandboxFactory } from "./sandbox.ts";
import { Plug } from "./plug.ts";
import { deepObjectMerge } from "$sb/lib/json.ts";
export interface SysCallMapping {
[key: string]: (ctx: SyscallContext, ...args: any) => Promise<any> | any;
@@ -95,11 +96,21 @@ export class System<HookT> extends EventEmitter<SystemEvents<HookT>> {
async load(
workerUrl: URL,
sandboxFactory: SandboxFactory<HookT>,
// Mapping plug name -> manifest overrides
manifestOverrides?: Record<string, Partial<Manifest<HookT>>>,
): Promise<Plug<HookT>> {
const plug = new Plug(this, workerUrl, sandboxFactory);
// Wait for worker to boot, and pass back its manifest
await plug.ready;
if (manifestOverrides && manifestOverrides[plug.manifest!.name]) {
plug.manifest = deepObjectMerge(
plug.manifest,
manifestOverrides[plug.manifest!.name],
);
console.log("New manifest", plug.manifest);
}
// and there it is!
const manifest = plug.manifest!;