Skip to content
Snippets Groups Projects
Commit bede3a84 authored by Mark Haines's avatar Mark Haines
Browse files

Tweak the javascript bindings so that they will work with Node.js

parent 36d17d1c
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@ function array_from_string(string) {
function random_stack(size) {
var ptr = stack(size);
var array = new Uint8Array(Module['HEAPU8'].buffer, ptr, size);
window.crypto.getRandomValues(array);
get_random_values(array);
return ptr;
}
......@@ -139,7 +139,7 @@ Account.prototype['unpickle'] = restore_stack(function(key, pickle) {
var key_array = array_from_string(key);
var key_buffer = stack(key_array);
var pickle_array = array_from_string(pickle);
var pickle_buffer = stack(pickle_length);
var pickle_buffer = stack(pickle_array);
account_method(Module['_olm_unpickle_account'])(
this.ptr, key_buffer, key_array.length, pickle_buffer,
pickle_array.length
......@@ -280,5 +280,7 @@ Session.prototype['decrypt'] = restore_stack(function(
return Pointer_stringify(plaintext_buffer, plaintext_length);
});
return {"Account": Account, "Session": Session};
olm_exports["Account"] = Account;
olm_exports["Session"] = Session;
}();
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.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment