Large "query" plug refactor into "directive"

This commit is contained in:
Zef Hemel
2022-10-28 16:17:40 +02:00
parent 366564f2ec
commit 540af411a0
47 changed files with 1000 additions and 870 deletions
+4 -1
View File
@@ -72,8 +72,11 @@ export async function bundle(
// Functions
for (const def of Object.values(manifest.functions || {})) {
if (!def.path) {
continue;
}
let jsFunctionName = "default",
filePath = path.join(rootPath, def.path!);
filePath = path.join(rootPath, def.path);
if (filePath.indexOf(":") !== -1) {
[filePath, jsFunctionName] = filePath.split(":");
}
+1 -1
View File
@@ -50,7 +50,7 @@ export function createSandbox(plug: Plug<any>) {
permissions: {
// Allow network access and servers (main use case: fetch)
net: true,
// This is required for console loggin to work, apparently?
// This is required for console logging to work, apparently?
env: true,
// No talking to native code
ffi: false,
+21 -5
View File
@@ -52,12 +52,28 @@ export class Plug<HookT> {
return !funDef.env || funDef.env === this.runtimeEnv;
}
async invoke(name: string, args: Array<any>): Promise<any> {
if (!this.sandbox.isLoaded(name)) {
const funDef = this.manifest!.functions[name];
if (!funDef) {
throw new Error(`Function ${name} not found in manifest`);
async invoke(name: string, args: any[]): Promise<any> {
const funDef = this.manifest!.functions[name];
if (!funDef) {
throw new Error(`Function ${name} not found in manifest`);
}
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;
}
return plug.invoke(name, args);
}
if (!this.sandbox.isLoaded(name)) {
if (!this.canInvoke(name)) {
throw new Error(
`Function ${name} is not available in ${this.runtimeEnv}`,
+8
View File
@@ -40,6 +40,12 @@ Deno.test("Run a deno sandbox", async () => {
};
})()`,
},
redirectTest: {
redirect: "addTen",
},
redirectTest2: {
redirect: "test.addTen",
},
addNumbersSyscall: {
code: `(() => {
return {
@@ -90,6 +96,8 @@ Deno.test("Run a deno sandbox", async () => {
createSandbox,
);
assertEquals(await plug.invoke("addTen", [10]), 20);
assertEquals(await plug.invoke("redirectTest", [10]), 20);
assertEquals(await plug.invoke("redirectTest2", [10]), 20);
for (let i = 0; i < 100; i++) {
assertEquals(await plug.invoke("addNumbersSyscall", [10, i]), 10 + i);
}
+5
View File
@@ -16,7 +16,12 @@ export interface Manifest<HookT> {
}
export type FunctionDef<HookT> = {
// Read the function from this path and inline it
// Format: filename:functionName
path?: string;
// Reuse an
// Format: plugName.functionName
redirect?: string;
code?: string;
env?: RuntimeEnvironment;
} & HookT;