This commit is contained in:
Zef Hemel
2022-03-07 10:21:02 +01:00
parent b6046ca974
commit 653e77c4dd
32 changed files with 7274 additions and 323 deletions
+27 -13
View File
@@ -7,22 +7,39 @@ import path from "path";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
async function compile(filePath, sourceMap) {
let tempFile = "out.js";
async function compile(filePath, functionName, debug) {
let outFile = "out.js";
let inFile = filePath;
if (functionName) {
// Generate a new file importing just this one function and exporting it
inFile = "in.js";
await writeFile(
inFile,
`import {${functionName}} from "./${filePath}";
export default ${functionName};`
);
}
// TODO: Figure out how to make source maps work correctly with eval() code
let js = await esbuild.build({
entryPoints: [filePath],
entryPoints: [inFile],
bundle: true,
format: "iife",
globalName: "mod",
platform: "neutral",
sourcemap: sourceMap ? "inline" : false,
minify: true,
outfile: tempFile,
sourcemap: false, //sourceMap ? "inline" : false,
minify: !debug,
outfile: outFile,
});
let jsCode = (await readFile(tempFile)).toString();
let jsCode = (await readFile(outFile)).toString();
jsCode = jsCode.replace(/^var mod ?= ?/, "");
await unlink(tempFile);
await unlink(outFile);
if (inFile !== filePath) {
await unlink(inFile);
}
return jsCode;
}
@@ -35,13 +52,10 @@ async function bundle(manifestPath, sourceMaps) {
filePath = path.join(rootPath, def.path);
if (filePath.indexOf(":") !== -1) {
[filePath, jsFunctionName] = filePath.split(":");
} else if (!jsFunctionName) {
jsFunctionName = "default";
}
def.code = await compile(filePath, sourceMaps);
def.path = filePath;
def.functionName = jsFunctionName;
def.code = await compile(filePath, jsFunctionName, sourceMaps);
delete def.path;
}
return manifest;
}
+88
View File
@@ -0,0 +1,88 @@
declare global {
function syscall(id: string, name: string, args: any[]): Promise<any>;
}
import { safeRun } from "./util";
let func: Function | null = null;
let pendingRequests = new Map<string, (result: unknown) => void>();
self.syscall = async (id: string, name: string, args: any[]) => {
return await new Promise((resolve, reject) => {
pendingRequests.set(id, resolve);
self.postMessage({
type: "syscall",
id,
name,
args,
});
});
};
self.addEventListener("result", (event) => {
let customEvent = event as CustomEvent;
self.postMessage({
type: "result",
result: customEvent.detail,
});
});
self.addEventListener("app-error", (event) => {
let customEvent = event as CustomEvent;
self.postMessage({
type: "error",
reason: customEvent.detail,
});
});
function wrapScript(code: string): string {
return `const fn = ${code};
return fn["default"].apply(null, arguments);`;
}
self.addEventListener("message", (event) => {
safeRun(async () => {
let messageEvent = event;
let data = messageEvent.data;
switch (data.type) {
case "boot":
console.log("Booting", data.name);
func = new Function(wrapScript(data.code));
self.postMessage({
type: "inited",
});
break;
case "invoke":
if (!func) {
throw new Error("No function loaded");
}
try {
let result = await Promise.resolve(func(...(data.args || [])));
self.postMessage({
type: "result",
result: result,
});
} catch (e: any) {
self.postMessage({
type: "error",
reason: e.message,
});
throw e;
}
break;
case "syscall-response":
let id = data.id;
const lookup = pendingRequests.get(id);
if (!lookup) {
console.log(
"Current outstanding requests",
pendingRequests,
"looking up",
id
);
throw Error("Invalid request id");
}
pendingRequests.delete(id);
lookup(data.data);
}
});
});
+16 -23
View File
@@ -13,18 +13,17 @@ export class FunctionWorker {
private invokeReject?: (reason?: any) => void;
private plug: Plug<any>;
constructor(plug: Plug<any>, pathPrefix: string, name: string) {
let worker = window.Worker;
this.worker = new worker("/function_worker.js");
constructor(plug: Plug<any>, name: string, code: string) {
// let worker = window.Worker;
this.worker = new Worker(new URL("function_worker.ts", import.meta.url), {
type: "module",
});
// console.log("Starting worker", this.worker);
this.worker.onmessage = this.onmessage.bind(this);
this.worker.postMessage({
type: "boot",
prefix: pathPrefix,
name: name,
// @ts-ignore
userAgent: navigator.userAgent,
code: code,
});
this.inited = new Promise((resolve) => {
this.initCallback = resolve;
@@ -81,33 +80,31 @@ export interface PlugLoader<HookT> {
}
export class Plug<HookT> {
pathPrefix: string;
system: System<HookT>;
private runningFunctions: Map<string, FunctionWorker>;
public manifest?: Manifest<HookT>;
private name: string;
constructor(system: System<HookT>, pathPrefix: string, name: string) {
this.name = name;
this.pathPrefix = `${pathPrefix}/${name}`;
constructor(system: System<HookT>, name: string) {
this.system = system;
this.runningFunctions = new Map<string, FunctionWorker>();
}
async load(manifest: Manifest<HookT>) {
this.manifest = manifest;
await this.system.plugLoader.load(this.name, manifest);
await this.dispatchEvent("load");
}
async invoke(name: string, args: Array<any>): Promise<any> {
if (!this.runningFunctions.has(name)) {
this.runningFunctions.set(
let worker = this.runningFunctions.get(name);
if (!worker) {
worker = new FunctionWorker(
this,
name,
new FunctionWorker(this, this.pathPrefix, name)
this.manifest!.functions[name].code!
);
this.runningFunctions.set(name, worker);
}
return await this.runningFunctions.get(name)!.invoke(args);
return await worker.invoke(args);
}
async dispatchEvent(name: string, data?: any): Promise<any[]> {
@@ -137,13 +134,9 @@ export class Plug<HookT> {
export class System<HookT> {
protected plugs: Map<string, Plug<HookT>>;
protected pathPrefix: string;
registeredSyscalls: SysCallMapping;
plugLoader: PlugLoader<HookT>;
constructor(plugLoader: PlugLoader<HookT>, pathPrefix: string) {
this.plugLoader = plugLoader;
this.pathPrefix = pathPrefix;
constructor() {
this.plugs = new Map<string, Plug<HookT>>();
this.registeredSyscalls = {};
}
@@ -168,7 +161,7 @@ export class System<HookT> {
}
async load(name: string, manifest: Manifest<HookT>): Promise<Plug<HookT>> {
const plug = new Plug(this, this.pathPrefix, name);
const plug = new Plug(this, name);
await plug.load(manifest);
this.plugs.set(name, plug);
return plug;
+1 -2
View File
@@ -10,7 +10,6 @@ export interface Manifest<HookT> {
}
export interface FunctionDef {
path: string;
functionName?: string;
path?: string;
code?: string;
}
+2 -9
View File
@@ -1,13 +1,6 @@
export function safeRun(fn: () => Promise<void>) {
fn().catch((e) => {
console.error(e);
});
}
export function sleep(ms: number): Promise<void> {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, ms);
// console.error(e);
throw e;
});
}