Cleanup and document worker.js
This commit is contained in:
parent
ceee85ef35
commit
4fd9fd3c7f
|
@ -27,39 +27,72 @@ var MAX_SIZE_RANDOM_BUFFER = 60000;
|
||||||
|
|
||||||
openpgp.crypto.random.randomBuffer.init(MAX_SIZE_RANDOM_BUFFER);
|
openpgp.crypto.random.randomBuffer.init(MAX_SIZE_RANDOM_BUFFER);
|
||||||
|
|
||||||
self.onmessage = function (event) {
|
/**
|
||||||
var msg = event.data || {},
|
* Handle messages from the main window.
|
||||||
options = msg.options || {};
|
* @param {Object} event Contains event type and data
|
||||||
|
*/
|
||||||
|
self.onmessage = function(event) {
|
||||||
|
var msg = event.data || {};
|
||||||
|
|
||||||
switch (msg.event) {
|
switch (msg.event) {
|
||||||
case 'configure':
|
case 'configure':
|
||||||
for (var i in msg.config) {
|
configure(msg.config);
|
||||||
openpgp.config[i] = msg.config[i];
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'seed-random':
|
case 'seed-random':
|
||||||
if (!(msg.buf instanceof Uint8Array)) {
|
seedRandom(msg.buf);
|
||||||
msg.buf = new Uint8Array(msg.buf);
|
|
||||||
}
|
|
||||||
openpgp.crypto.random.randomBuffer.set(msg.buf);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (typeof openpgp[msg.event] !== 'function') {
|
delegate(msg.event, msg.options || {});
|
||||||
throw new Error('Unknown Worker Event');
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse cloned packets
|
|
||||||
openpgp[msg.event](openpgp.packet.clone.parseClonedPackets(options, msg.event)).then(function(data) {
|
|
||||||
// clone packets (for web worker structured cloning algorithm)
|
|
||||||
response({ event:'method-return', data:openpgp.packet.clone.clonePackets(data) });
|
|
||||||
}).catch(function(e) {
|
|
||||||
response({ event:'method-return', err:e.message });
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set config from main context to worker context.
|
||||||
|
* @param {Object} config The openpgp configuration
|
||||||
|
*/
|
||||||
|
function configure(config) {
|
||||||
|
for (var i in config) {
|
||||||
|
openpgp.config[i] = config[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seed the library with entropy gathered window.crypto.getRandomValues
|
||||||
|
* as this api is only avalible in the main window.
|
||||||
|
* @param {ArrayBuffer} buffer Some random bytes
|
||||||
|
*/
|
||||||
|
function seedRandom(buffer) {
|
||||||
|
if (!(buffer instanceof Uint8Array)) {
|
||||||
|
buffer = new Uint8Array(buffer);
|
||||||
|
}
|
||||||
|
openpgp.crypto.random.randomBuffer.set(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic proxy function that handles all commands from the public api.
|
||||||
|
* @param {String} method The public api function to be delegated to the worker thread
|
||||||
|
* @param {Object} options The api function's options
|
||||||
|
*/
|
||||||
|
function delegate(method, options) {
|
||||||
|
if (typeof openpgp[method] !== 'function') {
|
||||||
|
throw new Error('Unknown Worker Event');
|
||||||
|
}
|
||||||
|
// parse cloned packets
|
||||||
|
options = openpgp.packet.clone.parseClonedPackets(options, method);
|
||||||
|
openpgp[method](options).then(function(data) {
|
||||||
|
// clone packets (for web worker structured cloning algorithm)
|
||||||
|
response({ event:'method-return', data:openpgp.packet.clone.clonePackets(data) });
|
||||||
|
}).catch(function(e) {
|
||||||
|
response({ event:'method-return', err:e.message });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Respond to the main window.
|
||||||
|
* @param {Object} event Contains event type and data
|
||||||
|
*/
|
||||||
function response(event) {
|
function response(event) {
|
||||||
if (openpgp.crypto.random.randomBuffer.size < MIN_SIZE_RANDOM_BUFFER) {
|
if (openpgp.crypto.random.randomBuffer.size < MIN_SIZE_RANDOM_BUFFER) {
|
||||||
self.postMessage({event: 'request-seed'});
|
self.postMessage({event: 'request-seed'});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user