mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2026-03-21 23:53:27 +00:00
Fix rare race condition with webworker tasks, fix minified web worker filename default
This commit is contained in:
@@ -39,13 +39,24 @@ export default function AsyncProxy({ path='openpgp.worker.js', worker, config }
|
||||
throw new Error('Unhandled error in openpgp worker: ' + e.message + ' (' + e.filename + ':' + e.lineno + ')');
|
||||
};
|
||||
this.seedRandom(INITIAL_RANDOM_SEED);
|
||||
// FIFO
|
||||
this.tasks = [];
|
||||
|
||||
if (config) {
|
||||
this.worker.postMessage({ event:'configure', config });
|
||||
}
|
||||
|
||||
// Cannot rely on task order being maintained, use object keyed by request ID to track tasks
|
||||
this.tasks = {};
|
||||
this.currentID = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get new request ID
|
||||
* @return {integer} New unique request ID
|
||||
*/
|
||||
AsyncProxy.prototype.getID = function() {
|
||||
return this.currentID++;
|
||||
};
|
||||
|
||||
/**
|
||||
* Message handling
|
||||
*/
|
||||
@@ -55,11 +66,12 @@ AsyncProxy.prototype.onMessage = function(event) {
|
||||
case 'method-return':
|
||||
if (msg.err) {
|
||||
// fail
|
||||
this.tasks.shift().reject(new Error(msg.err));
|
||||
this.tasks[msg.id].reject(new Error(msg.err));
|
||||
} else {
|
||||
// success
|
||||
this.tasks.shift().resolve(msg.data);
|
||||
this.tasks[msg.id].resolve(msg.data);
|
||||
}
|
||||
delete this.tasks[msg.id];
|
||||
break;
|
||||
case 'request-seed':
|
||||
this.seedRandom(RANDOM_SEED_REQUEST);
|
||||
@@ -106,11 +118,13 @@ AsyncProxy.prototype.terminate = function() {
|
||||
* @return {Promise} see the corresponding public api functions for their return types
|
||||
*/
|
||||
AsyncProxy.prototype.delegate = function(method, options) {
|
||||
const id = this.getID();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// clone packets (for web worker structured cloning algorithm)
|
||||
this.worker.postMessage({ event:method, options:packet.clone.clonePackets(options) }, util.getTransferables.call(util, options));
|
||||
this.worker.postMessage({ id:id, event:method, options:packet.clone.clonePackets(options) }, util.getTransferables.call(util, options));
|
||||
|
||||
// remember to handle parsing cloned packets from worker
|
||||
this.tasks.push({ resolve: data => resolve(packet.clone.parseClonedPackets(data, method)), reject });
|
||||
this.tasks[id] = { resolve: data => resolve(packet.clone.parseClonedPackets(data, method)), reject };
|
||||
});
|
||||
};
|
||||
|
||||
@@ -44,7 +44,7 @@ self.onmessage = function(event) {
|
||||
break;
|
||||
|
||||
default:
|
||||
delegate(msg.event, msg.options || {});
|
||||
delegate(msg.id, msg.event, msg.options || {});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -75,18 +75,18 @@ function seedRandom(buffer) {
|
||||
* @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) {
|
||||
function delegate(id, method, options) {
|
||||
if (typeof openpgp[method] !== 'function') {
|
||||
response({ event:'method-return', err:'Unknown Worker Event' });
|
||||
response({ id:id, event:'method-return', err:'Unknown Worker Event' });
|
||||
return;
|
||||
}
|
||||
// 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) });
|
||||
response({ id:id, event:'method-return', data:openpgp.packet.clone.clonePackets(data) });
|
||||
}).catch(function(e) {
|
||||
response({ event:'method-return', err:e.message });
|
||||
response({ id:id, event:'method-return', err:e.message });
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user