diff options
author | Mark Haines <mark.haines@matrix.org> | 2015-07-14 16:45:00 +0100 |
---|---|---|
committer | Mark Haines <mark.haines@matrix.org> | 2015-07-14 16:45:00 +0100 |
commit | bede3a84998f74d0d4da93bb6017712630c72857 (patch) | |
tree | 2157ed6b28851ab04a5e14b3a8e967460863e053 /javascript/olm_pre.js | |
parent | 36d17d1cb248db595a2318cd6af6fc4a1952cbb2 (diff) |
Tweak the javascript bindings so that they will work with Node.js
Diffstat (limited to 'javascript/olm_pre.js')
-rw-r--r-- | javascript/olm_pre.js | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/javascript/olm_pre.js b/javascript/olm_pre.js index c7b0d5e..5a348af 100644 --- a/javascript/olm_pre.js +++ b/javascript/olm_pre.js @@ -1 +1,29 @@ -Olm = function() { +var olm_exports = {}; +var get_random_values; +if (typeof(window) !== 'undefined') { + // We've been imported directly into a browser. + window["Olm"] = olm_exports; + get_random_values = function(buf) { + window.crypto.getRandomValues(buf); + }; +} else if (global.window) { + // We're running with browserify + global.window["Olm"] = olm_exports; + get_random_values = function(buf) { + window.crypto.getRandomValues(buf); + }; +} else if (module["exports"]) { + // We're running in node. + module["exports"] = olm_exports; + var nodeCrypto = require("crypto"); + get_random_values = function(buf) { + var bytes = nodeCrypto.randomBytes(buf.length); + buf.set(bytes); + } +} else { + throw new Error("Cannot find global to attach library to"); +} + +var init = function() { + var module; // Shadow the Node 'module' object so that emscripten won't try + // to fiddle with it. |