1
0
silverbullet/plugos/system.ts

152 lines
4.0 KiB
TypeScript
Raw Normal View History

2023-08-20 17:54:31 +00:00
import { Hook, Manifest } from "./types.ts";
import { EventEmitter } from "./event.ts";
import type { SandboxFactory } from "./sandbox.ts";
import { Plug } from "./plug.ts";
2023-08-20 17:54:31 +00:00
import { deepObjectMerge } from "$sb/lib/json.ts";
2022-03-23 14:41:12 +00:00
2022-03-24 09:48:56 +00:00
export interface SysCallMapping {
2022-03-25 11:03:06 +00:00
[key: string]: (ctx: SyscallContext, ...args: any) => Promise<any> | any;
2022-03-23 14:41:12 +00:00
}
export type SystemEvents<HookT> = {
plugLoaded: (plug: Plug<HookT>) => void | Promise<void>;
plugUnloaded: (name: string) => void | Promise<void>;
2022-03-23 14:41:12 +00:00
};
// Passed to every syscall, allows to pass in additional context that the syscall may use
export type SyscallContext = {
plug: Plug<any>;
2022-03-25 11:03:06 +00:00
};
type SyscallSignature = (
ctx: SyscallContext,
...args: any[]
) => Promise<any> | any;
type Syscall = {
requiredPermissions: string[];
callback: SyscallSignature;
};
2022-03-23 14:41:12 +00:00
export class System<HookT> extends EventEmitter<SystemEvents<HookT>> {
protected plugs = new Map<string, Plug<HookT>>();
2022-03-25 11:03:06 +00:00
protected registeredSyscalls = new Map<string, Syscall>();
protected enabledHooks = new Set<Hook<HookT>>();
2022-03-23 14:41:12 +00:00
2023-08-04 16:56:55 +00:00
constructor(readonly env?: string) {
2022-03-23 14:41:12 +00:00
super();
}
get loadedPlugs(): Map<string, Plug<HookT>> {
return this.plugs;
}
addHook(feature: Hook<HookT>) {
this.enabledHooks.add(feature);
2022-03-23 14:41:12 +00:00
feature.apply(this);
}
2022-03-25 11:03:06 +00:00
registerSyscalls(
requiredCapabilities: string[],
...registrationObjects: SysCallMapping[]
) {
2022-03-23 14:41:12 +00:00
for (const registrationObject of registrationObjects) {
for (const [name, callback] of Object.entries(registrationObject)) {
this.registeredSyscalls.set(name, {
2022-03-25 11:03:06 +00:00
requiredPermissions: requiredCapabilities,
callback,
});
2022-03-23 14:41:12 +00:00
}
}
}
syscallWithContext(
2022-03-25 11:03:06 +00:00
ctx: SyscallContext,
name: string,
args: any[],
2022-03-25 11:03:06 +00:00
): Promise<any> {
const syscall = this.registeredSyscalls.get(name);
if (!syscall) {
2022-03-23 14:41:12 +00:00
throw Error(`Unregistered syscall ${name}`);
}
2022-03-25 11:03:06 +00:00
for (const permission of syscall.requiredPermissions) {
if (!ctx.plug) {
throw Error(`Syscall ${name} requires permission and no plug is set`);
}
if (!ctx.plug.grantedPermissions.includes(permission)) {
throw Error(`Missing permission '${permission}' for syscall ${name}`);
}
2022-03-23 14:41:12 +00:00
}
2022-03-25 11:03:06 +00:00
return Promise.resolve(syscall.callback(ctx, ...args));
2022-03-23 14:41:12 +00:00
}
localSyscall(
contextPlugName: string,
syscallName: string,
args: any[],
): Promise<any> {
return this.syscallWithContext(
// Mock the plug
{ plug: { name: contextPlugName } as any },
syscallName,
args,
);
}
2022-03-23 14:41:12 +00:00
async load(
workerUrl: URL,
sandboxFactory: SandboxFactory<HookT>,
2023-08-20 17:54:31 +00:00
// Mapping plug name -> manifest overrides
manifestOverrides?: Record<string, Partial<Manifest<HookT>>>,
2022-03-23 14:41:12 +00:00
): Promise<Plug<HookT>> {
const plug = new Plug(this, workerUrl, sandboxFactory);
// Wait for worker to boot, and pass back its manifest
await plug.ready;
2023-08-20 17:54:31 +00:00
if (manifestOverrides && manifestOverrides[plug.manifest!.name]) {
plug.manifest = deepObjectMerge(
plug.manifest,
manifestOverrides[plug.manifest!.name],
);
// console.log("New manifest", plug.manifest);
2023-08-20 17:54:31 +00:00
}
// and there it is!
const manifest = plug.manifest!;
// Validate the manifest
2022-03-23 14:41:12 +00:00
let errors: string[] = [];
for (const feature of this.enabledHooks) {
errors = [...errors, ...feature.validateManifest(plug.manifest!)];
2022-03-23 14:41:12 +00:00
}
if (errors.length > 0) {
throw new Error(`Invalid manifest: ${errors.join(", ")}`);
}
if (this.plugs.has(manifest.name)) {
this.unload(manifest.name);
}
console.log("Loaded plug", manifest.name);
this.plugs.set(manifest.name, plug);
await this.emit("plugLoaded", plug);
2022-03-23 14:41:12 +00:00
return plug;
}
unload(name: string) {
2022-04-26 18:31:31 +00:00
// console.log("Unloading", name);
2022-03-23 14:41:12 +00:00
const plug = this.plugs.get(name);
if (!plug) {
return;
2022-03-23 14:41:12 +00:00
}
plug.stop();
2022-04-26 17:04:36 +00:00
this.emit("plugUnloaded", name);
2022-03-23 14:41:12 +00:00
this.plugs.delete(name);
}
unloadAll(): Promise<void[]> {
2022-03-23 14:41:12 +00:00
return Promise.all(
Array.from(this.plugs.keys()).map(this.unload.bind(this)),
2022-03-23 14:41:12 +00:00
);
}
}