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/nodeProcessWorker.ts | |
parent | 8f5b6ffd7d02986176c95c8800a8292555e2702b (diff) | |
download | wallet-core-a1e0fc3b88ed4305f942fadbea66b29a3934721c.tar.xz |
crypto worker refactoring
Diffstat (limited to 'src/crypto/nodeProcessWorker.ts')
-rw-r--r-- | src/crypto/nodeProcessWorker.ts | 64 |
1 files changed, 43 insertions, 21 deletions
diff --git a/src/crypto/nodeProcessWorker.ts b/src/crypto/nodeProcessWorker.ts index b5a2e8b44..c5d0f2e71 100644 --- a/src/crypto/nodeProcessWorker.ts +++ b/src/crypto/nodeProcessWorker.ts @@ -1,3 +1,5 @@ +import { CryptoWorkerFactory } from "./cryptoApi"; + /* This file is part of TALER (C) 2016 GNUnet e.V. @@ -17,11 +19,29 @@ // tslint:disable:no-var-requires -const path = require("path"); -const fork = require("child_process").fork; +import { CryptoWorker } from "./cryptoWorker"; + +import path = require("path"); +import child_process = require("child_process"); const nodeWorkerEntry = path.join(__dirname, "nodeWorkerEntry.js"); + +export class NodeCryptoWorkerFactory implements CryptoWorkerFactory { + startWorker(): CryptoWorker { + if (typeof require === "undefined") { + throw Error("cannot make worker, require(...) not defined"); + } + const workerCtor = require("./nodeProcessWorker").Worker; + const workerPath = __dirname + "/cryptoWorker.js"; + return new workerCtor(workerPath); + } + + getConcurrency(): number { + return 4; + } +} + /** * Worker implementation that uses node subprocesses. */ @@ -38,33 +58,35 @@ export class Worker { */ onerror: undefined | ((m: any) => void); - constructor(scriptFilename: string) { - this.child = fork(nodeWorkerEntry); + private dispatchMessage(msg: any) { + if (this.onmessage) { + this.onmessage({ data: msg }); + } else { + console.warn("no handler for worker event 'message' defined") + } + } + + private dispatchError(msg: any) { + if (this.onerror) { + this.onerror({ data: msg }); + } else { + console.warn("no handler for worker event 'error' defined") + } + } + + constructor() { + this.child = child_process.fork(nodeWorkerEntry); this.onerror = undefined; this.onmessage = undefined; this.child.on("error", (e: any) => { - if (this.onerror) { - this.onerror(e); - } + this.dispatchError(e); }); this.child.on("message", (msg: any) => { - const message = JSON.parse(msg); - - if (!message.error && this.onmessage) { - this.onmessage(message); - } - - if (message.error && this.onerror) { - const error = new Error(message.error); - error.stack = message.stack; - - this.onerror(error); - } + console.log("nodeProcessWorker got child message", msg); + this.dispatchMessage(msg); }); - - this.child.send({scriptFilename, cwd: process.cwd()}); } /** |