2022-10-10 12:50:21 +00:00
|
|
|
import { Hook, Manifest, RuntimeEnvironment } from "./types.ts";
|
|
|
|
import { EventEmitter } from "./event.ts";
|
2022-11-01 08:57:20 +00:00
|
|
|
import { Sandbox, SandboxFactory } from "./sandbox.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { Plug } from "./plug.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
|
|
|
}
|
|
|
|
|
2022-04-26 17:04:36 +00:00
|
|
|
export type SystemJSON<HookT> = Manifest<HookT>[];
|
2022-03-24 09:48:56 +00:00
|
|
|
|
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>;
|
2022-11-01 08:57:20 +00:00
|
|
|
sandboxInitialized(sandbox: Sandbox, plug: Plug<HookT>): void | Promise<void>;
|
2022-10-10 12:50:21 +00:00
|
|
|
plugUnloaded: (name: string) => void | Promise<void>;
|
2022-03-23 14:41:12 +00:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-03-23 14:41:12 +00:00
|
|
|
export class System<HookT> extends EventEmitter<SystemEvents<HookT>> {
|
2022-04-03 16:42:12 +00:00
|
|
|
readonly runtimeEnv: RuntimeEnvironment;
|
2022-03-23 14:41:12 +00:00
|
|
|
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
|
|
|
|
|
|
|
constructor(env: RuntimeEnvironment) {
|
|
|
|
super();
|
|
|
|
this.runtimeEnv = env;
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
// Mock the plug
|
|
|
|
{ plug: { name: contextPlugName } as any },
|
|
|
|
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(
|
|
|
|
manifest: Manifest<HookT>,
|
2022-10-10 12:50:21 +00:00
|
|
|
sandboxFactory: SandboxFactory<HookT>,
|
2022-03-23 14:41:12 +00:00
|
|
|
): Promise<Plug<HookT>> {
|
2022-04-26 17:04:36 +00:00
|
|
|
const name = manifest.name;
|
2022-03-23 14:41:12 +00:00
|
|
|
if (this.plugs.has(name)) {
|
|
|
|
await this.unload(name);
|
|
|
|
}
|
|
|
|
// Validate
|
|
|
|
let errors: string[] = [];
|
2022-03-29 09:21:32 +00:00
|
|
|
for (const feature of this.enabledHooks) {
|
2022-03-23 14:41:12 +00:00
|
|
|
errors = [...errors, ...feature.validateManifest(manifest)];
|
|
|
|
}
|
|
|
|
if (errors.length > 0) {
|
|
|
|
throw new Error(`Invalid manifest: ${errors.join(", ")}`);
|
|
|
|
}
|
|
|
|
// Ok, let's load this thing!
|
2022-03-25 11:03:06 +00:00
|
|
|
const plug = new Plug(this, name, sandboxFactory);
|
2022-04-26 17:04:36 +00:00
|
|
|
console.log("Loading", name);
|
2022-03-23 14:41:12 +00:00
|
|
|
await plug.load(manifest);
|
|
|
|
this.plugs.set(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
async 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) {
|
|
|
|
throw Error(`Plug ${name} not found`);
|
|
|
|
}
|
|
|
|
await 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON(): SystemJSON<HookT> {
|
2022-10-10 12:50:21 +00:00
|
|
|
const plugJSON: Manifest<HookT>[] = [];
|
|
|
|
for (const [_, plug] of this.plugs) {
|
2022-03-23 14:41:12 +00:00
|
|
|
if (!plug.manifest) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-26 17:04:36 +00:00
|
|
|
plugJSON.push(plug.manifest);
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
return plugJSON;
|
|
|
|
}
|
|
|
|
|
|
|
|
async replaceAllFromJSON(
|
|
|
|
json: SystemJSON<HookT>,
|
2022-10-10 12:50:21 +00:00
|
|
|
sandboxFactory: SandboxFactory<HookT>,
|
2022-03-23 14:41:12 +00:00
|
|
|
) {
|
|
|
|
await this.unloadAll();
|
2022-10-10 12:50:21 +00:00
|
|
|
for (const manifest of json) {
|
2022-07-11 07:08:22 +00:00
|
|
|
// console.log("Loading plug", manifest.name);
|
2022-04-26 17:04:36 +00:00
|
|
|
await this.load(manifest, sandboxFactory);
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|