diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-08-16 15:03:52 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-08-16 15:03:52 +0200 |
commit | a1e0fc3b88ed4305f942fadbea66b29a3934721c (patch) | |
tree | cb4887bdb68f76eeeb7c4baecc47e5ceb2d4498c /src/crypto/nodeWorkerEntry.ts | |
parent | 8f5b6ffd7d02986176c95c8800a8292555e2702b (diff) | |
download | wallet-core-a1e0fc3b88ed4305f942fadbea66b29a3934721c.tar.xz |
crypto worker refactoring
Diffstat (limited to 'src/crypto/nodeWorkerEntry.ts')
-rw-r--r-- | src/crypto/nodeWorkerEntry.ts | 105 |
1 files changed, 54 insertions, 51 deletions
diff --git a/src/crypto/nodeWorkerEntry.ts b/src/crypto/nodeWorkerEntry.ts index 9e2647ff6..1ed16f870 100644 --- a/src/crypto/nodeWorkerEntry.ts +++ b/src/crypto/nodeWorkerEntry.ts @@ -17,61 +17,64 @@ // tslint:disable:no-var-requires -const fs = require("fs"); -const vm = require("vm"); - -process.once("message", (obj: any) => { - const g: any = global as any; - - (g as any).self = { - addEventListener: (event: "error" | "message", fn: (x: any) => void) => { - if (event === "error") { - g.onerror = fn; - } else if (event === "message") { - g.onmessage = fn; - } - }, - close: () => { - process.exit(0); - }, - onerror: (err: any) => { - const str: string = JSON.stringify({error: err.message, stack: err.stack}); - if (process.send) { - process.send(str); - } - }, - onmessage: undefined, - postMessage: (msg: any) => { - const str: string = JSON.stringify({data: msg}); - if (process.send) { - process.send(str); - } - }, - }; - - g.__dirname = obj.cwd; - g.__filename = __filename; - g.importScripts = (...files: string[]) => { - if (files.length > 0) { - vm.createScript(files.map((file) => fs.readFileSync(file, "utf8")).join("\n")).runInThisContext(); - } - }; +import fs = require("fs"); +import vm = require("vm"); +import { NodeEmscriptenLoader } from "./nodeEmscriptenLoader"; +import { CryptoImplementation } from "./cryptoImplementation"; - Object.keys(g.self).forEach((key) => { - g[key] = g.self[key]; - }); +const loader = new NodeEmscriptenLoader(); + +async function handleRequest(operation: string, id: number, args: string[]) { + let emsc = await loader.getEmscriptenEnvironment(); - process.on("message", (msg: any) => { - try { - (g.onmessage || g.self.onmessage || (() => undefined))(JSON.parse(msg)); - } catch (err) { - (g.onerror || g.self.onerror || (() => undefined))(err); + const impl = new CryptoImplementation(emsc); + + if (!(operation in impl)) { + console.error(`crypto operation '${operation}' not found`); + return; + } + + try { + const result = (impl as any)[operation](...args); + if (process.send) { + process.send({ result, id }); + } else { + console.error("process.send not available"); } - }); + } catch (e) { + console.log("error during operation", e); + return; + } +} - process.on("uncaughtException", (err: any) => { - (g.onerror || g.self.onerror || (() => undefined))(err); +process.on("message", (msgStr: any) => { + console.log("got message in node worker entry", msgStr); + + console.log("typeof msg", typeof msgStr); + + const msg = JSON.parse(msgStr); + + const args = msg.data.args; + if (!Array.isArray(args)) { + console.error("args must be array"); + return; + } + const id = msg.data.id; + if (typeof id !== "number") { + console.error("RPC id must be number"); + return; + } + const operation = msg.data.operation; + if (typeof operation !== "string") { + console.error("RPC operation must be string"); + return; + } + + handleRequest(operation, id, args).catch((e) => { + console.error("error in node worker", e); }); +}); - require(obj.scriptFilename); +process.on("uncaughtException", (err: any) => { + console.log("uncaught exception in node worker entry", err); }); |