aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-27 19:04:07 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-27 19:04:07 +0200
commit592fd62402b7f0fdb8dbe08605381d3a74d929f2 (patch)
treec312c9c9ab67c9c83dcc2d368ea15ceb7e06d697 /src
parentd381226f21f1d0605d06ccae56c38ab6b12f88f0 (diff)
fix emscripten module loading in node
Diffstat (limited to 'src')
-rw-r--r--src/crypto/emscLoader.js35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/crypto/emscLoader.js b/src/crypto/emscLoader.js
index 723b8ae36..eebe20c02 100644
--- a/src/crypto/emscLoader.js
+++ b/src/crypto/emscLoader.js
@@ -17,22 +17,41 @@
// @ts-nocheck
+
+/**
+ * This module loads the emscripten library, and is written in unchecked
+ * JavaScript since it needs to do environment detection and dynamically select
+ * the right way to load the library.
+ */
+
/**
* Load the taler emscripten lib.
*
- * If in a WebWorker, importScripts is used. Inside a browser,
- * the module must be globally available.
+ * If in a WebWorker, importScripts is used. Inside a browser, the module must
+ * be globally available. Inside node, require is used.
*/
-export default function getLib() {
- if (window.TalerEmscriptenLib) {
- return TalerEmscriptenLib;
- }
- if (importScripts) {
+export function getLib() {
+ if (typeof importScripts !== "undefined") {
importScripts('/src/emscripten/taler-emscripten-lib.js')
if (TalerEmscriptenLib) {
throw Error("can't import TalerEmscriptenLib");
}
return TalerEmscriptenLib
}
- throw Error("Can't find 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;
+ }
+ throw Error("Looks like running in browser, but TalerEmscriptenLib is not defined");
+ }
+ throw Error("Running in unsupported environment");
}