2023-08-04 16:56:55 +00:00
|
|
|
import { Manifest } from "./types.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
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;
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
public grantedPermissions: string[] = [];
|
|
|
|
public sandbox: Sandbox<HookT>;
|
|
|
|
|
2023-12-06 17:44:48 +00:00
|
|
|
// Resolves once the plug's manifest is available
|
2023-05-23 18:53:53 +00:00
|
|
|
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
|
|
|
|
2023-12-06 17:44:48 +00:00
|
|
|
// Time of last function invocation
|
|
|
|
unloadTimeout?: number;
|
|
|
|
|
2022-03-25 11:03:06 +00:00
|
|
|
constructor(
|
2023-05-23 18:53:53 +00:00
|
|
|
private system: System<HookT>,
|
|
|
|
public workerUrl: URL,
|
2023-12-06 17:44:48 +00:00
|
|
|
readonly name: string,
|
|
|
|
private hash: number,
|
2023-05-23 18:53:53 +00:00
|
|
|
private sandboxFactory: (plug: Plug<HookT>) => Sandbox<HookT>,
|
2022-03-25 11:03:06 +00:00
|
|
|
) {
|
2023-01-08 11:24:12 +00:00
|
|
|
this.runtimeEnv = system.env;
|
2022-03-23 14:41:12 +00:00
|
|
|
|
2023-12-06 17:44:48 +00:00
|
|
|
this.scheduleUnloadTimeout();
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
this.sandbox = this.sandboxFactory(this);
|
2023-12-06 17:44:48 +00:00
|
|
|
// 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 || [];
|
|
|
|
},
|
2023-05-23 18:53:53 +00:00
|
|
|
);
|
2022-03-25 11:03:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +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)
|
|
|
|
*/
|
2023-05-23 18:53:53 +00:00
|
|
|
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`);
|
|
|
|
}
|
2023-01-08 11:24:12 +00:00
|
|
|
return !funDef.env || !this.runtimeEnv || funDef.env === this.runtimeEnv;
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 17:44:48 +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);
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
// Invoke a function
|
2022-10-28 14:17:40 +00:00
|
|
|
async invoke(name: string, args: any[]): Promise<any> {
|
2023-05-23 18:53:53 +00:00
|
|
|
// Ensure the worker is fully up and running
|
|
|
|
await this.ready;
|
|
|
|
|
2023-12-06 17:44:48 +00:00
|
|
|
this.scheduleUnloadTimeout();
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
// Before we access the manifest
|
2022-10-28 14:17:40 +00:00
|
|
|
const funDef = this.manifest!.functions[name];
|
|
|
|
if (!funDef) {
|
|
|
|
throw new Error(`Function ${name} not found in manifest`);
|
|
|
|
}
|
2022-11-01 08:57:20 +00:00
|
|
|
const sandbox = this.sandbox!;
|
2022-10-28 14:17:40 +00:00
|
|
|
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
|
|
|
}
|
2022-10-28 14:17:40 +00:00
|
|
|
return plug.invoke(name, args);
|
|
|
|
}
|
2023-05-23 18:53:53 +00:00
|
|
|
if (!await this.canInvoke(name)) {
|
|
|
|
throw new Error(
|
|
|
|
`Function ${name} is not available in ${this.runtimeEnv}`,
|
|
|
|
);
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
return sandbox.invoke(name, args);
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 17:02:56 +00:00
|
|
|
stop() {
|
2023-12-06 17:44:48 +00:00
|
|
|
console.log("Stopping sandbox for", this.name);
|
|
|
|
this.sandbox.stop();
|
2022-03-23 14:41:12 +00:00
|
|
|
}
|
|
|
|
}
|