From f9ddb49ec68e29e2f768d2a95bc085982a06b6dc Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 11 May 2022 11:49:27 +0200 Subject: [PATCH] Plug manger rebuild --- .DS_Store | Bin 6148 -> 6148 bytes packages/plugos/compile.ts | 12 ++- .../plugos/environments/sandbox_worker.ts | 3 +- packages/plugos/syscalls/esbuild.ts | 7 +- packages/plugs/plugmanager/plugmanager.ts | 94 ++++++++---------- packages/server/_in.ts | 1 + packages/server/express_server.ts | 2 +- 7 files changed, 57 insertions(+), 62 deletions(-) create mode 100644 packages/server/_in.ts diff --git a/.DS_Store b/.DS_Store index eb0ea6a6e472ee80d59a7b3cd1117882c38c2fe8..826bfb4a39977f4eec665ca1a739a5b728af26a6 100644 GIT binary patch delta 20 bcmZoMXffDe$jrz-*@h{ak#ln&bD0PLJ7xu0 delta 16 XcmZoMXffDe$UNDaDS7i0<^mA_FK`7; diff --git a/packages/plugos/compile.ts b/packages/plugos/compile.ts index dbc2ad4..11c5679 100644 --- a/packages/plugos/compile.ts +++ b/packages/plugos/compile.ts @@ -4,22 +4,26 @@ import path from "path"; export async function compile( filePath: string, - functionName: string = "", + functionName: string | undefined = undefined, debug: boolean = false, excludeModules: string[] = [], meta = false ): Promise { - let outFile = path.join(path.dirname(filePath), "_out.tmp"); + let outFile = path.resolve(path.dirname(filePath), "_out.tmp"); let inFile = filePath; if (functionName) { // Generate a new file importing just this one function and exporting it - inFile = "_in.ts"; + inFile = path.resolve(path.dirname(filePath), "_in.ts"); await writeFile( inFile, - `import {${functionName}} from "./${filePath}";export default ${functionName};` + `import {${functionName}} from "./${path.basename( + filePath + )}";export default ${functionName};` ); } + // console.log("In:", inFile); + // console.log("Outfile:", outFile); // TODO: Figure out how to make source maps work correctly with eval() code let result = await esbuild.build({ diff --git a/packages/plugos/environments/sandbox_worker.ts b/packages/plugos/environments/sandbox_worker.ts index 0f365a5..cca927a 100644 --- a/packages/plugos/environments/sandbox_worker.ts +++ b/packages/plugos/environments/sandbox_worker.ts @@ -93,7 +93,8 @@ self.addEventListener("message", (event: { data: WorkerMessage }) => { id: data.id, error: e.message, }); - throw e; + console.error("Error invoking function", data.name, e.message); + // throw e; } break; diff --git a/packages/plugos/syscalls/esbuild.ts b/packages/plugos/syscalls/esbuild.ts index f0f0888..7dff6e8 100644 --- a/packages/plugos/syscalls/esbuild.ts +++ b/packages/plugos/syscalls/esbuild.ts @@ -15,7 +15,8 @@ export function esbuildSyscalls(): SysCallMapping { "esbuild.compile": async ( ctx, filename: string, - code: string + code: string, + functionName?: string ): Promise => { let tmpDir = `${tmpdir()}/plugos-${Math.random()}`; await mkdir(tmpDir, { recursive: true }); @@ -34,7 +35,9 @@ export function esbuildSyscalls(): SysCallMapping { } await writeFile(`${tmpDir}/${filename}`, code); - let jsCode = await compile(`${tmpDir}/${filename}`, "", false, ["yaml"]); + let jsCode = await compile(`${tmpDir}/${filename}`, functionName, false, [ + "yaml", + ]); await rm(tmpDir, { recursive: true }); return jsCode; }, diff --git a/packages/plugs/plugmanager/plugmanager.ts b/packages/plugs/plugmanager/plugmanager.ts index fdd563b..acd5f47 100644 --- a/packages/plugs/plugmanager/plugmanager.ts +++ b/packages/plugs/plugmanager/plugmanager.ts @@ -58,69 +58,55 @@ export async function checkCommand() { async function compileDefinition(text: string): Promise { let tree = await parseMarkdown(text); - let pageMeta = extractMeta(tree); - - if (!pageMeta.name) { - throw new Error("No 'name' specified in page meta"); - } - - addParentPointers(tree); - let allHeaders = collectNodesOfType(tree, "ATXHeading2"); - let manifest: Manifest = { - name: pageMeta.name, - functions: {}, - }; - for (let t of allHeaders) { - let parent = t.parent!; - let headerIdx = parent.children!.indexOf(t); - let headerTitle = t.children![1].text!.trim(); - if (!headerTitle.startsWith("function ")) { + let codeNodes = collectNodesOfType(tree, "FencedCode"); + let manifest: Manifest | undefined; + let code: string | undefined; + let language = "js"; + for (let codeNode of codeNodes) { + let codeInfo = findNodeOfType(codeNode, "CodeInfo")!.children![0].text!; + let codeText = findNodeOfType(codeNode, "CodeText")!.children![0].text!; + if (codeInfo === "yaml") { + manifest = YAML.parse(codeText); continue; } - let functionName = headerTitle - .substring("function ".length) - .replace(/[^\w]/g, "_"); - let meta: any; - let code: string | undefined; - let language = "js"; - for (let i = headerIdx + 1; i < parent.children!.length; i++) { - let child = parent.children![i]; - if (child.type === "FencedCode") { - let codeInfo = findNodeOfType(child, "CodeInfo")!.children![0].text!; - let codeText = findNodeOfType(child, "CodeText")!.children![0].text!; - if (codeInfo === "yaml") { - meta = YAML.parse(codeText); - continue; - } - if (codeInfo === "typescript" || codeInfo === "ts") { - language = "ts"; - } - code = codeText; - } - - if (child.type?.startsWith("ATXHeading")) { - break; - } - } - if (code) { - let compiled = await invokeFunction( - "server", - "compileJS", - `file.${language}`, - code - ); - manifest.functions[functionName] = meta; - manifest.functions[functionName].code = compiled; + if (codeInfo === "typescript" || codeInfo === "ts") { + language = "ts"; } + code = codeText; } + + if (!manifest) { + throw new Error("No meta found"); + } + + if (!code) { + throw new Error("No code found"); + } + + manifest.functions = manifest.functions || {}; + + for (let [name, func] of Object.entries(manifest.functions)) { + let compiled = await invokeFunction( + "server", + "compileJS", + `file.${language}`, + code, + name + ); + func.code = compiled; + } + + console.log("Doing the whole manifest thing"); + return manifest; } export async function compileJS( filename: string, - code: string + code: string, + functionName: string ): Promise { - return self.syscall("esbuild.compile", filename, code); + return self.syscall("esbuild.compile", filename, code, functionName); } async function listPlugs(): Promise { @@ -151,7 +137,7 @@ export async function updatePlugs() { } let plugYaml = codeTextNode.children![0].text; let plugList = YAML.parse(plugYaml!); - // console.log("Plug YAML", plugList); + console.log("Plug YAML", plugList); let allPlugNames: string[] = []; for (let plugUri of plugList) { let [protocol, ...rest] = plugUri.split(":"); diff --git a/packages/server/_in.ts b/packages/server/_in.ts new file mode 100644 index 0000000..0f3dbd3 --- /dev/null +++ b/packages/server/_in.ts @@ -0,0 +1 @@ +import {pullDataCommand} from ".//var/folders/s2/4nqrw2192hngtxg672qzc0nr0000gn/T/plugos-0.8739407042390945/file.js";export default pullDataCommand; \ No newline at end of file diff --git a/packages/server/express_server.ts b/packages/server/express_server.ts index 127dc3a..83ae9ed 100644 --- a/packages/server/express_server.ts +++ b/packages/server/express_server.ts @@ -94,7 +94,7 @@ export class ExpressServer { sandboxSyscalls(this.system), jwtSyscalls() ); - this.system.addHook(new EndpointHook(this.app, "/_/")); + this.system.addHook(new EndpointHook(this.app, "/_")); this.eventHook.addLocalListener( "get-plug:builtin",