aboutsummaryrefslogtreecommitdiff
path: root/src/crypto
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-27 22:55:52 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-27 22:55:52 +0200
commit478a089e521de81ca47fc54518bc6a241823d9ae (patch)
tree434360d5090263688950ae180184c96f590ab9ff /src/crypto
parent9a1b2c8ccc5f079dae4966dfd011f4076a53dc20 (diff)
downloadwallet-core-478a089e521de81ca47fc54518bc6a241823d9ae.tar.xz
fix module loading for node under fake web workers
Diffstat (limited to 'src/crypto')
-rw-r--r--src/crypto/cryptoApi.ts27
-rw-r--r--src/crypto/emscLoader.js24
2 files changed, 36 insertions, 15 deletions
diff --git a/src/crypto/cryptoApi.ts b/src/crypto/cryptoApi.ts
index a386eab42..b291cd17d 100644
--- a/src/crypto/cryptoApi.ts
+++ b/src/crypto/cryptoApi.ts
@@ -38,6 +38,9 @@ import {
OfferRecord,
CoinWithDenom,
} from "../wallet";
+import * as timer from "../timer";
+
+import { startWorker } from "./startWorker";
/**
@@ -57,7 +60,7 @@ interface WorkerState {
/**
* Timer to terminate the worker if it's not busy enough.
*/
- terminationTimerHandle: number|null;
+ terminationTimerHandle: timer.TimerHandle|null;
}
interface WorkItem {
@@ -73,6 +76,8 @@ interface WorkItem {
}
+
+
/**
* Number of different priorities. Each priority p
* must be 0 <= p < NUM_PRIO.
@@ -98,7 +103,7 @@ export class CryptoApi {
ws.currentWorkItem = work;
this.numBusy++;
if (!ws.w) {
- let w = new Worker("/dist/cryptoWorker-bundle.js");
+ let w = startWorker();
w.onmessage = (m: MessageEvent) => this.handleWorkerMessage(ws, m);
w.onerror = (e: ErrorEvent) => this.handleWorkerError(ws, e);
ws.w = w;
@@ -114,7 +119,8 @@ export class CryptoApi {
resetWorkerTimeout(ws: WorkerState) {
if (ws.terminationTimerHandle != null) {
- clearTimeout(ws.terminationTimerHandle);
+ ws.terminationTimerHandle.clear();
+ ws.terminationTimerHandle = null;
}
let destroy = () => {
// terminate worker if it's idle
@@ -123,7 +129,7 @@ export class CryptoApi {
ws.w = null;
}
};
- ws.terminationTimerHandle = window.setTimeout(destroy, 20 * 1000);
+ ws.terminationTimerHandle = timer.after(20 * 1000, destroy);
}
handleWorkerError(ws: WorkerState, e: ErrorEvent) {
@@ -182,7 +188,14 @@ export class CryptoApi {
}
constructor() {
- this.workers = new Array<WorkerState>((navigator as any)["hardwareConcurrency"] || 2);
+ let concurrency = 2;
+ try {
+ // only works in the browser
+ concurrency = (navigator as any)["hardwareConcurrency"];
+ } catch (e) {
+ // ignore
+ }
+ this.workers = new Array<WorkerState>(concurrency);
for (let i = 0; i < this.workers.length; i++) {
this.workers[i] = {
@@ -199,7 +212,7 @@ export class CryptoApi {
private doRpc<T>(operation: string, priority: number,
...args: any[]): Promise<T> {
- let start = performance.now();
+ let start = timer.performanceNow();
let p = new Promise((resolve, reject) => {
let rpcId = this.nextRpcId++;
@@ -228,7 +241,7 @@ export class CryptoApi {
});
return p.then((r: T) => {
- console.log(`rpc ${operation} took ${performance.now() - start}ms`);
+ console.log(`rpc ${operation} took ${timer.performanceNow() - start}ms`);
return r;
});
}
diff --git a/src/crypto/emscLoader.js b/src/crypto/emscLoader.js
index eebe20c02..190f007f1 100644
--- a/src/crypto/emscLoader.js
+++ b/src/crypto/emscLoader.js
@@ -31,6 +31,22 @@
* be globally available. Inside node, require is used.
*/
export function getLib() {
+ if (typeof require !== "undefined") {
+ // Make sure that TypeScript doesn't try
+ // to check the taler-emscripten-lib.
+ const indirectRequire = require;
+ const g = global as any;
+ // unavoidable hack, so that emscripten detects
+ // the environment as node even though importScripts
+ // is present.
+ const savedImportScripts = g.importScripts;
+ delete g.importScripts;
+ // Assume that the code is run from the build/ directory.
+ const lib = indirectRequire("../../../emscripten/taler-emscripten-lib.js");
+ g.importScripts = savedImportScripts;
+ return lib;
+ }
+
if (typeof importScripts !== "undefined") {
importScripts('/src/emscripten/taler-emscripten-lib.js')
if (TalerEmscriptenLib) {
@@ -39,14 +55,6 @@ export function getLib() {
return TalerEmscriptenLib
}
- if (typeof require !== "undefined") {
- // Make sure that TypeScript doesn't try
- // to check the taler-emscripten-lib.
- const fn = require;
- // Assume that the code is run from the build/ directory.
- return fn("../../../emscripten/taler-emscripten-lib.js");
- }
-
if (typeof window !== "undefined") {
if (window.TalerEmscriptenLib) {
return TalerEmscriptenLib;