pull/3/head
Zef Hemel 2022-05-13 17:05:52 +02:00
parent 3c5048ac25
commit 7d01f77318
12 changed files with 70 additions and 55 deletions

View File

@ -12,7 +12,7 @@ import { compile, sandboxCompileModule } from "../compile";
async function bundle(
manifestPath: string,
sourceMaps: boolean,
debug: boolean,
excludeModules: string[]
) {
const rootPath = path.dirname(manifestPath);
@ -40,7 +40,7 @@ async function bundle(
def.code = await compile(
filePath,
jsFunctionName,
sourceMaps,
debug,
allModulesToExclude
);
delete def.path;

View File

@ -35,7 +35,7 @@ export async function compile(
format: "iife",
globalName: "mod",
platform: "browser",
sourcemap: false, //sourceMap ? "inline" : false,
sourcemap: false, //debug ? "inline" : false,
minify: !debug,
outfile: outFile,
metafile: true,
@ -45,7 +45,7 @@ export async function compile(
if (meta) {
let text = await esbuild.analyzeMetafile(result.metafile);
console.log("Bundle info for", functionName, text);
// console.log("Bundle info for", functionName, text);
}
let jsCode = (await readFile(outFile)).toString();
@ -77,6 +77,7 @@ export async function sandboxCompile(
filename: string,
code: string,
functionName?: string,
debug: boolean = false,
installModules: string[] = [],
globalModules: string[] = []
): Promise<string> {
@ -102,11 +103,10 @@ export async function sandboxCompile(
}
await writeFile(`${tmpDir}/${filename}`, code);
let jsCode = await compile(
`${tmpDir}/${filename}`,
functionName,
false,
debug,
globalModules
);
await rm(tmpDir, { recursive: true });
@ -127,6 +127,7 @@ export async function sandboxCompileModule(
// `export * from "${cleanModulesName}${path ? path : ""}";`,
`module.exports = require("${cleanModulesName}${path ? path : ""}");`,
undefined,
true,
[modulePart],
globalModules
);

View File

@ -120,10 +120,12 @@ parentPort.on("message", (data: any) => {
result: result && JSON.parse(JSON.stringify(result)),
});
} catch (e: any) {
// console.error("Error caught", e, "Stack", e.stack);
parentPort.postMessage({
type: "result",
id: data.id,
error: e.message,
stack: e.stack,
});
}
break;

View File

@ -96,8 +96,9 @@ self.addEventListener("message", (event: { data: WorkerMessage }) => {
type: "result",
id: data.id,
error: e.message,
stack: e.stack,
});
console.error("Error invoking function", data.name, e.message);
// console.error("Error invoking function", data.name, e.message);
// throw e;
}

View File

@ -13,6 +13,7 @@ export type ControllerMessage = {
name?: string;
args?: any[];
error?: string;
stack?: string;
level?: LogLevel;
message?: string;
result?: any;

View File

@ -114,7 +114,10 @@ export class Sandbox {
let resultCbs = this.outstandingInvocations.get(data.id!);
this.outstandingInvocations.delete(data.id!);
if (data.error) {
resultCbs && resultCbs.reject(new Error(data.error));
resultCbs &&
resultCbs.reject(
new Error(`${data.error}\nStack trace: ${data.stack}`)
);
} else {
resultCbs && resultCbs.resolve(data.result);
}

View File

@ -1,14 +1,7 @@
import { compile } from "../compile";
import { sandboxCompile, sandboxCompileModule } from "../compile";
import { SysCallMapping } from "../system";
import { tmpdir } from "os";
import { mkdir, rm, symlink, writeFile } from "fs/promises";
import { nodeModulesDir } from "../environments/node_sandbox";
const exposedModules = [
"@silverbulletmd/plugos-silverbullet-syscall",
"@plugos/plugos-syscall",
"yaml",
];
import globalModules from "../../common/dist/global.plug.json";
import * as ts from "typescript";
@ -69,36 +62,26 @@ export function esbuildSyscalls(): SysCallMapping {
ctx,
filename: string,
code: string,
functionName?: string
functionName?: string,
excludeModules: string[] = []
): Promise<string> => {
let tmpDir = await prepareCompileEnv(filename, code);
let jsCode = await compile(`${tmpDir}/${filename}`, functionName, false, [
"yaml",
"handlebars",
]);
await rm(tmpDir, { recursive: true });
return jsCode;
return await sandboxCompile(
filename,
code,
functionName,
true,
[],
[...Object.keys(globalModules.dependencies), ...excludeModules]
);
},
"esbuild.compileModule": async (
ctx,
moduleName: string
): Promise<string> => {
return await sandboxCompileModule(
moduleName,
Object.keys(globalModules.dependencies)
);
},
};
}
async function prepareCompileEnv(filename: string, code: string) {
let tmpDir = `${tmpdir()}/plugos-${Math.random()}`;
await mkdir(tmpDir, { recursive: true });
const srcNodeModules = `${nodeModulesDir}/node_modules`;
const targetNodeModules = `${tmpDir}/node_modules`;
await mkdir(`${targetNodeModules}/@silverbulletmd`, { recursive: true });
await mkdir(`${targetNodeModules}/@plugos`, { recursive: true });
for (const exposedModule of exposedModules) {
await symlink(
`${srcNodeModules}/${exposedModule}`,
`${targetNodeModules}/${exposedModule}`,
"dir"
);
}
await writeFile(`${tmpDir}/${filename}`, code);
return tmpDir;
}

View File

@ -8,7 +8,7 @@
"license": "MIT",
"scripts": {
"generate": "lezer-generator query/query.grammar -o query/parse-query.js",
"watch": "plugos-bundle --dist ../common/dist global.plug.yaml && plugos-bundle -w --dist dist --exclude @lezer/lr yaml handlebars -- */*.plug.yaml",
"watch": "plugos-bundle --dist ../common/dist global.plug.yaml && plugos-bundle --debug -w --dist dist --exclude @lezer/lr yaml handlebars -- */*.plug.yaml",
"build": "plugos-bundle --dist ../common/dist global.plug.yaml && plugos-bundle --dist dist --exclude @lezer/lr yaml handlebars -- */*.plug.yaml",
"test": "jest build/test"
},

View File

@ -24,8 +24,10 @@ functions:
compileJS:
path: "./plugmanager.ts:compileJS"
env: server
compileModule:
path: "./plugmanager.ts:compileModule"
env: server
getPlugPlugMd:
path: "./plugmanager.ts:getPlugPlugMd"
events:
- get-plug:plugmd
- get-plug:plugmd

View File

@ -5,6 +5,7 @@ import {
findNodeOfType,
} from "@silverbulletmd/common/tree";
import {
flashNotification,
getText,
hideBhs,
showBhs,
@ -84,6 +85,13 @@ async function compileDefinition(text: string): Promise<Manifest> {
throw new Error("No code found");
}
manifest.dependencies = manifest.dependencies || {};
for (let [dep, depSpec] of Object.entries(manifest.dependencies)) {
let compiled = await invokeFunction("server", "compileModule", depSpec);
manifest.dependencies![dep] = compiled;
}
manifest.functions = manifest.functions || {};
for (let [name, func] of Object.entries(manifest.functions)) {
@ -92,7 +100,8 @@ async function compileDefinition(text: string): Promise<Manifest> {
"compileJS",
`file.${language}`,
code,
name
name,
Object.keys(manifest.dependencies)
);
func.code = compiled;
}
@ -105,9 +114,21 @@ async function compileDefinition(text: string): Promise<Manifest> {
export async function compileJS(
filename: string,
code: string,
functionName: string
functionName: string,
excludeModules: string[]
): Promise<string> {
return self.syscall("esbuild.compile", filename, code, functionName);
// console.log("Compiling JS", filename, excludeModules);
return self.syscall(
"esbuild.compile",
filename,
code,
functionName,
excludeModules
);
}
export async function compileModule(moduleName: string): Promise<string> {
return self.syscall("esbuild.compileModule", moduleName);
}
async function listPlugs(): Promise<string[]> {
@ -122,7 +143,9 @@ export async function listCommand() {
}
export async function updatePlugsCommand() {
flashNotification("Updating plugs...");
await invokeFunction("server", "updatePlugs");
flashNotification("And... done!");
await reloadPlugs();
}
@ -137,7 +160,6 @@ export async function updatePlugs() {
return;
}
let plugYaml = codeTextNode.children![0].text;
console.log("YAML", YAML);
let plugList = YAML.parse(plugYaml!);
console.log("Plug YAML", plugList);
let allPlugNames: string[] = [];

View File

@ -339,7 +339,7 @@ export class ExpressServer {
res.send(result);
} catch (e: any) {
res.status(500);
console.log("Error invoking function", e);
// console.log("Error invoking function", e);
return res.send(e.message);
}
}

View File

@ -603,7 +603,7 @@ export class Editor {
editor!.focus();
if (cmd) {
cmd.run().catch((e) => {
console.error("Error running command", e);
console.error("Error running command", e.message);
});
}
}}