1
0
silverbullet/plugos/plug.ts

98 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-08-04 16:56:55 +00:00
import { Manifest } from "./types.ts";
import { Sandbox } from "./sandbox.ts";
import { System } from "./system.ts";
2022-10-13 13:16:18 +00:00
import { AssetBundle, AssetJson } from "./asset_bundle/bundle.ts";
2022-03-23 14:41:12 +00:00
export class Plug<HookT> {
2023-08-04 16:56:55 +00:00
readonly runtimeEnv?: string;
public grantedPermissions: string[] = [];
public sandbox: Sandbox<HookT>;
// Resolves once the worker has been loaded
ready: Promise<void>;
// Only available after ready resolves
2022-03-23 14:41:12 +00:00
public manifest?: Manifest<HookT>;
2022-10-13 13:16:18 +00:00
public assets?: AssetBundle;
2022-03-23 14:41:12 +00:00
2022-03-25 11:03:06 +00:00
constructor(
private system: System<HookT>,
public workerUrl: URL,
private sandboxFactory: (plug: Plug<HookT>) => Sandbox<HookT>,
2022-03-25 11:03:06 +00:00
) {
this.runtimeEnv = system.env;
2022-03-23 14:41:12 +00:00
// Kick off worker
this.sandbox = this.sandboxFactory(this);
this.ready = this.sandbox.ready.then(() => {
this.manifest = this.sandbox.manifest!;
this.assets = new AssetBundle(
this.manifest.assets ? this.manifest.assets as AssetJson : {},
);
// TODO: These need to be explicitly granted, not just taken
this.grantedPermissions = this.manifest.requiredPermissions || [];
2022-11-01 14:00:59 +00:00
});
}
get name(): string | undefined {
return this.manifest?.name;
2022-03-25 11:03:06 +00:00
}
// Invoke a syscall
2022-03-25 11:03:06 +00:00
syscall(name: string, args: any[]): Promise<any> {
return this.system.syscallWithContext({ plug: this }, name, args);
2022-03-23 14:41:12 +00:00
}
// Checks if a function can be invoked (it may be restricted on its execution environment)
async canInvoke(name: string) {
await this.ready;
const funDef = this.manifest!.functions[name];
2022-03-23 14:41:12 +00:00
if (!funDef) {
throw new Error(`Function ${name} not found in manifest`);
}
return !funDef.env || !this.runtimeEnv || funDef.env === this.runtimeEnv;
2022-03-23 14:41:12 +00:00
}
// Invoke a function
async invoke(name: string, args: any[]): Promise<any> {
// Ensure the worker is fully up and running
await this.ready;
// Before we access the manifest
const funDef = this.manifest!.functions[name];
if (!funDef) {
throw new Error(`Function ${name} not found in manifest`);
}
const sandbox = this.sandbox!;
if (funDef.redirect) {
// Function redirect, look up
// deno-lint-ignore no-this-alias
let plug: Plug<HookT> | undefined = this;
if (funDef.redirect.indexOf(".") !== -1) {
const [plugName, functionName] = funDef.redirect.split(".");
plug = this.system.loadedPlugs.get(plugName);
if (!plug) {
throw Error(`Plug ${plugName} redirected to not found`);
}
name = functionName;
} else {
name = funDef.redirect;
2022-03-23 14:41:12 +00:00
}
return plug.invoke(name, args);
}
if (!await this.canInvoke(name)) {
throw new Error(
`Function ${name} is not available in ${this.runtimeEnv}`,
);
2022-03-23 14:41:12 +00:00
}
return await sandbox.invoke(name, args);
2022-03-23 14:41:12 +00:00
}
2022-10-15 17:02:56 +00:00
stop() {
if (this.sandbox) {
this.sandbox.stop();
}
2022-03-23 14:41:12 +00:00
}
}