Refactor and renaming
This commit is contained in:
@@ -8,13 +8,13 @@ import yargs from "yargs";
|
||||
import { hideBin } from "yargs/helpers";
|
||||
import { Manifest } from "../types";
|
||||
import YAML from "yaml";
|
||||
import { preloadModules } from "../../silverbullet-common/preload_modules";
|
||||
import { mkdirSync } from "fs";
|
||||
|
||||
async function compile(
|
||||
filePath: string,
|
||||
functionName: string,
|
||||
debug: boolean,
|
||||
excludeModules: string[],
|
||||
meta = false
|
||||
) {
|
||||
let outFile = "_out.tmp";
|
||||
@@ -40,7 +40,7 @@ async function compile(
|
||||
minify: !debug,
|
||||
outfile: outFile,
|
||||
metafile: true,
|
||||
external: preloadModules,
|
||||
external: excludeModules,
|
||||
});
|
||||
|
||||
if (meta) {
|
||||
@@ -57,7 +57,11 @@ async function compile(
|
||||
return mod;})()`;
|
||||
}
|
||||
|
||||
async function bundle(manifestPath: string, sourceMaps: boolean) {
|
||||
async function bundle(
|
||||
manifestPath: string,
|
||||
sourceMaps: boolean,
|
||||
excludeModules: string[]
|
||||
) {
|
||||
const rootPath = path.dirname(manifestPath);
|
||||
const manifest = YAML.parse(
|
||||
(await readFile(manifestPath)).toString()
|
||||
@@ -70,7 +74,12 @@ async function bundle(manifestPath: string, sourceMaps: boolean) {
|
||||
[filePath, jsFunctionName] = filePath.split(":");
|
||||
}
|
||||
|
||||
def.code = await compile(filePath, jsFunctionName, sourceMaps);
|
||||
def.code = await compile(
|
||||
filePath,
|
||||
jsFunctionName,
|
||||
sourceMaps,
|
||||
excludeModules
|
||||
);
|
||||
delete def.path;
|
||||
}
|
||||
return manifest;
|
||||
@@ -79,9 +88,10 @@ async function bundle(manifestPath: string, sourceMaps: boolean) {
|
||||
async function buildManifest(
|
||||
manifestPath: string,
|
||||
distPath: string,
|
||||
debug: boolean
|
||||
debug: boolean,
|
||||
excludeModules: string[]
|
||||
) {
|
||||
let generatedManifest = await bundle(manifestPath, debug);
|
||||
let generatedManifest = await bundle(manifestPath, debug, excludeModules);
|
||||
const outFile =
|
||||
manifestPath.substring(
|
||||
0,
|
||||
@@ -106,20 +116,31 @@ async function run() {
|
||||
type: "string",
|
||||
default: ".",
|
||||
})
|
||||
.option("exclude", {
|
||||
type: "array",
|
||||
default: [],
|
||||
})
|
||||
.parse();
|
||||
if (args._.length === 0) {
|
||||
console.log(
|
||||
"Usage: plugos-bundle [--debug] [--dist <path>] <manifest.plug.yaml> <manifest2.plug.yaml> ..."
|
||||
"Usage: plugos-bundle [--debug] [--dist <path>] [--exclude package1 package2] -- <manifest.plug.yaml> <manifest2.plug.yaml> ..."
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log("Args", args);
|
||||
|
||||
async function buildAll() {
|
||||
mkdirSync(args.dist, { recursive: true });
|
||||
for (const plugManifestPath of args._) {
|
||||
let manifestPath = plugManifestPath as string;
|
||||
try {
|
||||
await buildManifest(manifestPath, args.dist, !!args.debug);
|
||||
await buildManifest(
|
||||
manifestPath,
|
||||
args.dist,
|
||||
!!args.debug,
|
||||
args.exclude
|
||||
);
|
||||
} catch (e) {
|
||||
console.error(`Error building ${manifestPath}:`, e);
|
||||
}
|
||||
|
||||
@@ -36,15 +36,21 @@ class NodeWorkerWrapper implements WorkerLike {
|
||||
}
|
||||
|
||||
// Look for the node_modules directory, to be passed to the worker to find e.g. the vm2 module
|
||||
let nodeModulesDir = __dirname;
|
||||
export let nodeModulesDir = __dirname;
|
||||
while (!fs.existsSync(nodeModulesDir + "/node_modules/vm2")) {
|
||||
nodeModulesDir = path.dirname(nodeModulesDir);
|
||||
}
|
||||
|
||||
export function createSandbox(plug: Plug<any>) {
|
||||
export function createSandbox(
|
||||
plug: Plug<any>,
|
||||
preloadedModules: string[] = []
|
||||
) {
|
||||
let worker = new Worker(workerCode, {
|
||||
eval: true,
|
||||
workerData: path.join(nodeModulesDir, "node_modules"),
|
||||
workerData: {
|
||||
nodeModulesPath: path.join(nodeModulesDir, "node_modules"),
|
||||
preloadedModules,
|
||||
},
|
||||
});
|
||||
return new Sandbox(plug, new NodeWorkerWrapper(worker));
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { preloadModules } from "../../silverbullet-common/preload_modules";
|
||||
const {
|
||||
parentPort,
|
||||
workerData: { preloadedModules, nodeModulesPath },
|
||||
} = require("worker_threads");
|
||||
|
||||
const { parentPort, workerData } = require("worker_threads");
|
||||
const { VM, VMScript } = require(`${workerData}/vm2`);
|
||||
const fetch = require(`${workerData}/node-fetch`);
|
||||
const WebSocket = require(`${workerData}/ws`);
|
||||
|
||||
// console.log("Process env", process.env);
|
||||
const { VM, VMScript } = require(`${nodeModulesPath}/vm2`);
|
||||
|
||||
let loadedFunctions = new Map<string, Function>();
|
||||
let pendingRequests = new Map<
|
||||
@@ -26,14 +24,14 @@ let vm = new VM({
|
||||
clearTimeout,
|
||||
setInterval,
|
||||
clearInterval,
|
||||
fetch,
|
||||
WebSocket,
|
||||
fetch: require(`${nodeModulesPath}/node-fetch`),
|
||||
WebSocket: require(`${nodeModulesPath}/ws`),
|
||||
// This is only going to be called for pre-bundled modules, we won't allow
|
||||
// arbitrary requiring of modules
|
||||
require: (moduleName: string): any => {
|
||||
// console.log("Loading", moduleName);
|
||||
if (preloadModules.includes(moduleName)) {
|
||||
return require(`${workerData}/${moduleName}`);
|
||||
if (preloadedModules.includes(moduleName)) {
|
||||
return require(`${nodeModulesPath}/${moduleName}`);
|
||||
} else {
|
||||
throw Error("Cannot import arbitrary modules");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
export abstract class EventEmitter<HandlerT> {
|
||||
private handlers: Partial<HandlerT>[] = [];
|
||||
|
||||
on(handlers: Partial<HandlerT>) {
|
||||
this.handlers.push(handlers);
|
||||
}
|
||||
|
||||
off(handlers: Partial<HandlerT>) {
|
||||
this.handlers = this.handlers.filter((h) => h !== handlers);
|
||||
}
|
||||
|
||||
emit(eventName: keyof HandlerT, ...args: any[]) {
|
||||
for (let handler of this.handlers) {
|
||||
let fn: any = handler[eventName];
|
||||
if (fn) {
|
||||
fn(...args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "@silverbulletmd/plugos",
|
||||
"name": "@plugos/plugos",
|
||||
"version": "0.0.1",
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Hook, Manifest, RuntimeEnvironment } from "./types";
|
||||
import { EventEmitter } from "../silverbullet-common/event";
|
||||
import { EventEmitter } from "./event";
|
||||
import { SandboxFactory } from "./sandbox";
|
||||
import { Plug } from "./plug";
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"include": ["bin/*", "environments/*", "hooks/**", "syscalls/*", "*"],
|
||||
"include": ["bin/*", "environments/*", "hooks/*", "syscalls/*", "*"],
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"strict": true,
|
||||
|
||||
Reference in New Issue
Block a user