Tweaking
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ export type ControllerMessage = {
|
||||
name?: string;
|
||||
args?: any[];
|
||||
error?: string;
|
||||
stack?: string;
|
||||
level?: LogLevel;
|
||||
message?: string;
|
||||
result?: any;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user