1
0
silverbullet/plugos/plug.ts

117 lines
3.3 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";
2023-12-09 17:08:46 +00:00
import { AssetBundle } 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 plug's manifest is available
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
// Time of last function invocation
unloadTimeout?: number;
2022-03-25 11:03:06 +00:00
constructor(
private system: System<HookT>,
public workerUrl: URL,
readonly name: string,
private hash: number,
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
this.scheduleUnloadTimeout();
this.sandbox = this.sandboxFactory(this);
// Retrieve the manifest asynchonously, which may either come from a cache or be loaded from the worker
this.ready = system.options.manifestCache!.getManifest(this, this.hash)
.then(
(manifest) => {
this.manifest = manifest;
// TODO: These need to be explicitly granted, not just taken
this.grantedPermissions = manifest.requiredPermissions || [];
},
);
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
}
2023-12-17 14:10:00 +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
}
scheduleUnloadTimeout() {
if (!this.system.options.plugFlushTimeout) {
return;
}
// Reset the unload timeout, if set
if (this.unloadTimeout) {
clearTimeout(this.unloadTimeout);
}
this.unloadTimeout = setTimeout(() => {
this.stop();
}, this.system.options.plugFlushTimeout);
}
// Invoke a function
async invoke(name: string, args: any[]): Promise<any> {
// Ensure the worker is fully up and running
await this.ready;
this.scheduleUnloadTimeout();
// 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 sandbox.invoke(name, args);
2022-03-23 14:41:12 +00:00
}
2022-10-15 17:02:56 +00:00
stop() {
console.log("Stopping sandbox for", this.name);
this.sandbox.stop();
2022-03-23 14:41:12 +00:00
}
}