2023-12-12 13:15:21 +00:00
|
|
|
import { Hook } from "./types.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { EventEmitter } from "./event.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import type { SandboxFactory } from "./sandbox.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { Plug } from "./plug.ts";
|
2023-12-06 17:44:48 +00:00
|
|
|
import { InMemoryManifestCache, ManifestCache } from "./manifest_cache.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> = {
|
2022-10-10 12:50:21 +00:00
|
|
|
plugLoaded: (plug: Plug<HookT>) => void | Promise<void>;
|
|
|
|
plugUnloaded: (name: string) => void | Promise<void>;
|
2022-03-23 14:41:12 +00:00
|
|
|
};
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
// Passed to every syscall, allows to pass in additional context that the syscall may use
|
2022-03-31 12:28:07 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-12-06 17:44:48 +00:00
|
|
|
export type SystemOptions = {
|
|
|
|
manifestCache?: ManifestCache<any>;
|
|
|
|
plugFlushTimeout?: number;
|
|
|
|
};
|
|
|
|
|
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>();
|
2022-03-29 09:21:32 +00:00
|
|
|
protected enabledHooks = new Set<Hook<HookT>>();
|
2022-03-23 14:41:12 +00:00
|
|
|
|
2023-12-06 17:44:48 +00:00
|
|
|
/**
|
|
|
|
* @param env either an environment or undefined for hybrid mode
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
readonly env: string | undefined,
|
|
|
|
readonly options: SystemOptions = {},
|
|
|
|
) {
|
2022-03-23 14:41:12 +00:00
|
|
|
super();
|
2023-12-06 17:44:48 +00:00
|
|
|
if (!options.manifestCache) {
|
|
|
|
options.manifestCache = new InMemoryManifestCache();
|
|
|
|
}
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
get loadedPlugs(): Map<string, Plug<HookT>> {
|
|
|
|
return this.plugs;
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
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) {
|
2022-10-10 12:50:21 +00:00
|
|
|
for (const [name, callback] of Object.entries(registrationObject)) {
|
2022-04-03 16:42:12 +00:00
|
|
|
this.registeredSyscalls.set(name, {
|
2022-03-25 11:03:06 +00:00
|
|
|
requiredPermissions: requiredCapabilities,
|
|
|
|
callback,
|
|
|
|
});
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
syscallWithContext(
|
2022-03-25 11:03:06 +00:00
|
|
|
ctx: SyscallContext,
|
|
|
|
name: string,
|
2022-10-10 12:50:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
localSyscall(
|
2022-09-14 07:32:47 +00:00
|
|
|
contextPlugName: string,
|
|
|
|
syscallName: string,
|
2022-10-10 12:50:21 +00:00
|
|
|
args: any[],
|
2022-09-14 07:32:47 +00:00
|
|
|
): Promise<any> {
|
|
|
|
return this.syscallWithContext(
|
2023-10-03 12:16:33 +00:00
|
|
|
{ plug: this.plugs.get(contextPlugName)! },
|
2022-09-14 07:32:47 +00:00
|
|
|
syscallName,
|
2022-10-10 12:50:21 +00:00
|
|
|
args,
|
2022-09-14 07:32:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-23 14:41:12 +00:00
|
|
|
async load(
|
2023-05-23 18:53:53 +00:00
|
|
|
workerUrl: URL,
|
2023-12-06 17:44:48 +00:00
|
|
|
name: string,
|
|
|
|
hash: number,
|
2022-10-10 12:50:21 +00:00
|
|
|
sandboxFactory: SandboxFactory<HookT>,
|
2022-03-23 14:41:12 +00:00
|
|
|
): Promise<Plug<HookT>> {
|
2023-12-06 17:44:48 +00:00
|
|
|
const plug = new Plug(this, workerUrl, name, hash, sandboxFactory);
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
// Wait for worker to boot, and pass back its manifest
|
|
|
|
await plug.ready;
|
2023-08-20 17:54:31 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
// and there it is!
|
|
|
|
const manifest = plug.manifest!;
|
|
|
|
|
|
|
|
// Validate the manifest
|
2022-03-23 14:41:12 +00:00
|
|
|
let errors: string[] = [];
|
2022-03-29 09:21:32 +00:00
|
|
|
for (const feature of this.enabledHooks) {
|
2023-05-23 18:53:53 +00:00
|
|
|
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(", ")}`);
|
|
|
|
}
|
2023-05-23 18:53:53 +00:00
|
|
|
if (this.plugs.has(manifest.name)) {
|
|
|
|
this.unload(manifest.name);
|
|
|
|
}
|
2023-12-10 12:23:42 +00:00
|
|
|
console.log("Activated plug", manifest.name);
|
2023-05-23 18:53:53 +00:00
|
|
|
this.plugs.set(manifest.name, plug);
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
await this.emit("plugLoaded", plug);
|
2022-03-23 14:41:12 +00:00
|
|
|
return plug;
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
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) {
|
2023-05-23 18:53:53 +00:00
|
|
|
return;
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
2023-05-23 18:53:53 +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);
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
unloadAll(): Promise<void[]> {
|
2022-03-23 14:41:12 +00:00
|
|
|
return Promise.all(
|
2022-10-10 12:50:21 +00:00
|
|
|
Array.from(this.plugs.keys()).map(this.unload.bind(this)),
|
2022-03-23 14:41:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|