diff options
author | Richard van der Hoff <richard@matrix.org> | 2016-12-12 16:52:03 +0000 |
---|---|---|
committer | Richard van der Hoff <richard@matrix.org> | 2016-12-12 16:52:03 +0000 |
commit | 1bf807bf33b2a407e4d8a3f9785b522b5d384913 (patch) | |
tree | 47c7d522eda0681171019e9ff7d3d027a5db59e3 /javascript | |
parent | f6c05be8c5d35e725a8a2ed5ad661398ac9f8cd2 (diff) |
Better handling of exceptions during loading
If we get an exception during load, don't define half of window.Olm (which
confuses apps).
This is a partial fix to https://github.com/vector-im/riot-web/issues/2726.
Diffstat (limited to 'javascript')
-rw-r--r-- | javascript/olm_post.js | 20 | ||||
-rw-r--r-- | javascript/olm_pre.js | 18 |
2 files changed, 24 insertions, 14 deletions
diff --git a/javascript/olm_post.js b/javascript/olm_post.js index 8951c11..54e42c5 100644 --- a/javascript/olm_post.js +++ b/javascript/olm_post.js @@ -419,4 +419,22 @@ olm_exports["get_library_version"] = restore_stack(function() { getValue(buf+2, 'i8'), ]; }); -}(); + +})(); + +// export the olm functions into the environment. +// +// make sure that we do this *after* populating olm_exports, so that we don't +// get a half-built window.Olm if there is an exception. + +if (typeof module !== 'undefined' && module.exports) { + // node / browserify + module.exports = olm_exports; +} + +if (typeof(window) !== 'undefined') { + // We've been imported directly into a browser. Define the global 'Olm' object. + // (we do this even if module.exports was defined, because it's useful to have + // Olm in the global scope for browserified and webpacked apps.) + window["Olm"] = olm_exports; +} diff --git a/javascript/olm_pre.js b/javascript/olm_pre.js index 50bf8c2..611488b 100644 --- a/javascript/olm_pre.js +++ b/javascript/olm_pre.js @@ -2,32 +2,24 @@ var olm_exports = {}; var get_random_values; var process; // Shadow the process object so that emscripten won't get // confused by browserify -if (typeof(global) !== 'undefined' && global["window"]) { - // We're running with browserify - module["exports"] = olm_exports; - global["window"]["Olm"] = olm_exports; - get_random_values = function(buf) { - window.crypto.getRandomValues(buf); - }; -} else if (typeof(window) !== 'undefined') { - // We've been imported directly into a browser. - window["Olm"] = olm_exports; + +if (typeof(window) !== 'undefined') { + // We've in a browser (directly, via browserify, or via webpack). 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); - } + }; process = global["process"]; } else { throw new Error("Cannot find global to attach library to"); } -var init = function() { +(function() { var module; // Shadow the Node 'module' object so that emscripten won't try // to fiddle with it. |