aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/synchronousWorker.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2019-08-16 00:15:19 +0200
committerFlorian Dold <florian.dold@gmail.com>2019-08-16 00:15:19 +0200
commitcc4eeec0318def17ac966be2d0ad8547ce782bc7 (patch)
tree876596e882b39a56ea75864164a2d7bb87fb7d16 /src/crypto/synchronousWorker.ts
parent0cab39f2b6e84a16467bf535150e1700a2fcf566 (diff)
downloadwallet-core-cc4eeec0318def17ac966be2d0ad8547ce782bc7.tar.xz
oops, actually pass arguments to emscripten
Diffstat (limited to 'src/crypto/synchronousWorker.ts')
-rw-r--r--src/crypto/synchronousWorker.ts91
1 files changed, 51 insertions, 40 deletions
diff --git a/src/crypto/synchronousWorker.ts b/src/crypto/synchronousWorker.ts
index 4d1ff964c..c8ee3be19 100644
--- a/src/crypto/synchronousWorker.ts
+++ b/src/crypto/synchronousWorker.ts
@@ -24,7 +24,9 @@ import fs = require("fs");
*/
export class SynchronousCryptoWorker {
private cachedEmscEnvironment: EmscEnvironment | undefined = undefined;
- private cachedEmscEnvironmentPromise: Promise<EmscEnvironment> | undefined = undefined;
+ private cachedEmscEnvironmentPromise:
+ | Promise<EmscEnvironment>
+ | undefined = undefined;
/**
* Function to be called when we receive a message from the worker thread.
@@ -64,52 +66,61 @@ export class SynchronousCryptoWorker {
return this.cachedEmscEnvironmentPromise;
}
- // Make sure that TypeScript doesn't try
- // to check the taler-emscripten-lib.
- const indirectRequire = require;
+ const binaryPath =
+ __dirname + "/../../../emscripten/taler-emscripten-lib.wasm";
+ console.log("reading from", binaryPath);
+ const wasmBinary = new Uint8Array(fs.readFileSync(binaryPath));
- const g = global;
+ let lib: any;
+
+ return new Promise((resolve, reject) => {
+ // Arguments passed to the emscripten prelude
+ const libArgs = {
+ wasmBinary,
+ onRuntimeInitialized: () => {
+ if (!lib) {
+ console.error("fatal emscripten initialization error");
+ return;
+ }
+ this.cachedEmscEnvironmentPromise = undefined;
+ this.cachedEmscEnvironment = new EmscEnvironment(lib);
+ resolve(this.cachedEmscEnvironment);
+ },
+ };
- // unavoidable hack, so that emscripten detects
- // the environment as node even though importScripts
- // is present.
+ // Make sure that TypeScript doesn't try
+ // to check the taler-emscripten-lib.
+ const indirectRequire = require;
- // @ts-ignore
- const savedImportScripts = g.importScripts;
- // @ts-ignore
- delete g.importScripts;
+ const g = global;
- // Assume that the code is run from the build/ directory.
- const libFn = indirectRequire(
- "../../../emscripten/taler-emscripten-lib.js",
- );
- const lib = libFn();
- // @ts-ignore
- g.importScripts = savedImportScripts;
+ // unavoidable hack, so that emscripten detects
+ // the environment as node even though importScripts
+ // is present.
- if (!lib) {
- throw Error("could not load taler-emscripten-lib.js");
- }
+ // @ts-ignore
+ const savedImportScripts = g.importScripts;
+ // @ts-ignore
+ delete g.importScripts;
- if (!lib.ccall) {
- throw Error(
- "sanity check failed: taler-emscripten lib does not have 'ccall'",
+ // Assume that the code is run from the build/ directory.
+ const libFn = indirectRequire(
+ "../../../emscripten/taler-emscripten-lib.js",
);
- }
-
- const binaryPath = __dirname + "/../../../emscripten/taler-emscripten-lib.wasm";
- console.log("reading from", binaryPath);
- const wasmBinary = new Uint8Array(fs.readFileSync(binaryPath));
-
- this.cachedEmscEnvironmentPromise = new Promise((resolve, reject) => {
- lib.wasmBinary = wasmBinary;
- lib.onRuntimeInitialized = () => {
- this.cachedEmscEnvironmentPromise = undefined;
- this.cachedEmscEnvironment = new EmscEnvironment(lib);
- resolve(this.cachedEmscEnvironment);
- };
+ const lib = libFn(libArgs);
+ // @ts-ignore
+ g.importScripts = savedImportScripts;
+
+ if (!lib) {
+ throw Error("could not load taler-emscripten-lib.js");
+ }
+
+ if (!lib.ccall) {
+ throw Error(
+ "sanity check failed: taler-emscripten lib does not have 'ccall'",
+ );
+ }
});
- return this.cachedEmscEnvironmentPromise;
}
private dispatchMessage(msg: any) {
@@ -157,7 +168,7 @@ export class SynchronousCryptoWorker {
return;
}
- this.handleRequest(operation, id, args).catch((e) => {
+ this.handleRequest(operation, id, args).catch(e => {
console.error("Error while handling crypto request:", e);
});
}