Merge pull request #274 from mailvelope/get-worker
Add getter method for worker to high level API. Initialization options for AsyncProxy: path and worker.
This commit is contained in:
commit
06b66d7f62
|
@ -37,6 +37,7 @@ module.exports = {
|
|||
compression: enums.compression.zip,
|
||||
integrity_protect: true,
|
||||
rsa_blinding: true,
|
||||
useWebCrypto: true,
|
||||
|
||||
show_version: true,
|
||||
show_comment: true,
|
||||
|
|
|
@ -46,14 +46,31 @@ if (typeof Promise === 'undefined') {
|
|||
require('es6-promise').polyfill();
|
||||
}
|
||||
|
||||
var asyncProxy; // instance of the asyncproxy
|
||||
var asyncProxy = null; // instance of the asyncproxy
|
||||
|
||||
/**
|
||||
* Set the path for the web worker script and create an instance of the async proxy
|
||||
* @param {String} path relative path to the worker scripts
|
||||
* @param {String} path relative path to the worker scripts, default: 'openpgp.worker.js'
|
||||
* @param {Object} [options.worker=Object] alternative to path parameter:
|
||||
* web worker initialized with 'openpgp.worker.js'
|
||||
* @return {Boolean} true if worker created successfully
|
||||
*/
|
||||
function initWorker(path) {
|
||||
asyncProxy = new AsyncProxy(path);
|
||||
function initWorker(path, options) {
|
||||
if (options && options.worker ||
|
||||
typeof window !== 'undefined' && window.Worker) {
|
||||
asyncProxy = new AsyncProxy(path, options);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reference to the async proxy if the worker was initialized with openpgp.initWorker()
|
||||
* @return {module:worker/async_proxy~AsyncProxy|null} the async proxy or null if not initialized
|
||||
*/
|
||||
function getWorker() {
|
||||
return asyncProxy;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,7 +85,7 @@ function encryptMessage(keys, text) {
|
|||
keys = [keys];
|
||||
}
|
||||
|
||||
if (useWorker()) {
|
||||
if (asyncProxy) {
|
||||
return asyncProxy.encryptMessage(keys, text);
|
||||
}
|
||||
|
||||
|
@ -95,7 +112,7 @@ function signAndEncryptMessage(publicKeys, privateKey, text) {
|
|||
publicKeys = [publicKeys];
|
||||
}
|
||||
|
||||
if (useWorker()) {
|
||||
if (asyncProxy) {
|
||||
return asyncProxy.signAndEncryptMessage(publicKeys, privateKey, text);
|
||||
}
|
||||
|
||||
|
@ -119,7 +136,7 @@ function signAndEncryptMessage(publicKeys, privateKey, text) {
|
|||
* @static
|
||||
*/
|
||||
function decryptMessage(privateKey, msg) {
|
||||
if (useWorker()) {
|
||||
if (asyncProxy) {
|
||||
return asyncProxy.decryptMessage(privateKey, msg);
|
||||
}
|
||||
|
||||
|
@ -145,7 +162,7 @@ function decryptAndVerifyMessage(privateKey, publicKeys, msg) {
|
|||
publicKeys = [publicKeys];
|
||||
}
|
||||
|
||||
if (useWorker()) {
|
||||
if (asyncProxy) {
|
||||
return asyncProxy.decryptAndVerifyMessage(privateKey, publicKeys, msg);
|
||||
}
|
||||
|
||||
|
@ -174,7 +191,7 @@ function signClearMessage(privateKeys, text) {
|
|||
privateKeys = [privateKeys];
|
||||
}
|
||||
|
||||
if (useWorker()) {
|
||||
if (asyncProxy) {
|
||||
return asyncProxy.signClearMessage(privateKeys, text);
|
||||
}
|
||||
|
||||
|
@ -199,7 +216,7 @@ function verifyClearSignedMessage(publicKeys, msg) {
|
|||
publicKeys = [publicKeys];
|
||||
}
|
||||
|
||||
if (useWorker()) {
|
||||
if (asyncProxy) {
|
||||
return asyncProxy.verifyClearSignedMessage(publicKeys, msg);
|
||||
}
|
||||
|
||||
|
@ -229,7 +246,7 @@ function verifyClearSignedMessage(publicKeys, msg) {
|
|||
*/
|
||||
function generateKeyPair(options) {
|
||||
// use web worker if web crypto apis are not supported
|
||||
if (!util.getWebCrypto() && useWorker()) {
|
||||
if (!util.getWebCrypto() && asyncProxy) {
|
||||
return asyncProxy.generateKeyPair(options);
|
||||
}
|
||||
|
||||
|
@ -259,22 +276,6 @@ function generateKeyPair(options) {
|
|||
// helper functions
|
||||
//
|
||||
|
||||
/**
|
||||
* Are we in a browser and do we support worker?
|
||||
*/
|
||||
function useWorker() {
|
||||
if (typeof window === 'undefined' || !window.Worker) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!asyncProxy) {
|
||||
console.log('You need to set the worker path!');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Command pattern that wraps synchronous code into a promise
|
||||
* @param {function} cmd The synchronous function with a return value
|
||||
|
@ -307,6 +308,7 @@ function onError(message, error) {
|
|||
}
|
||||
|
||||
exports.initWorker = initWorker;
|
||||
exports.getWorker = getWorker;
|
||||
exports.encryptMessage = encryptMessage;
|
||||
exports.signAndEncryptMessage = signAndEncryptMessage;
|
||||
exports.decryptMessage = decryptMessage;
|
||||
|
|
|
@ -71,3 +71,9 @@ module.exports.fromClone = function (clone) {
|
|||
keyid.bytes = clone.bytes;
|
||||
return keyid;
|
||||
};
|
||||
|
||||
module.exports.fromId = function (hex) {
|
||||
var keyid = new Keyid();
|
||||
keyid.read(util.hex2bin(hex));
|
||||
return keyid;
|
||||
};
|
||||
|
|
|
@ -38,9 +38,15 @@ var INITIAL_RANDOM_SEED = 50000, // random bytes seeded to worker
|
|||
* Initializes a new proxy and loads the web worker
|
||||
* @constructor
|
||||
* @param {String} path The path to the worker or 'openpgp.worker.js' by default
|
||||
* @param {Object} [options.worker=Object] alternative to path parameter:
|
||||
* web worker initialized with 'openpgp.worker.js'
|
||||
*/
|
||||
function AsyncProxy(path) {
|
||||
function AsyncProxy(path, options) {
|
||||
if (options && options.worker) {
|
||||
this.worker = options.worker;
|
||||
} else {
|
||||
this.worker = new Worker(path || 'openpgp.worker.js');
|
||||
}
|
||||
this.worker.onmessage = this.onMessage.bind(this);
|
||||
this.worker.onerror = function(e) {
|
||||
throw new Error('Unhandled error in openpgp worker: ' + e.message + ' (' + e.filename + ':' + e.lineno + ')');
|
||||
|
|
|
@ -172,6 +172,25 @@ var priv_key_de =
|
|||
expect(privKeyDE).to.exist;
|
||||
}
|
||||
|
||||
describe('Init Worker', function() {
|
||||
|
||||
this.timeout(0);
|
||||
|
||||
it('openpgp.getWorker method', function (done) {
|
||||
expect(openpgp.getWorker()).to.be.null;
|
||||
var workerAvailable = openpgp.initWorker('../dist/openpgp.worker.js');
|
||||
expect(workerAvailable).to.be.true;
|
||||
expect(openpgp.getWorker()).to.exist;
|
||||
privKeyRSA = openpgp.key.readArmored(priv_key_rsa).keys[0];
|
||||
expect(privKeyRSA.primaryKey.isDecrypted).to.be.false;
|
||||
openpgp.getWorker().decryptKeyPacket(privKeyRSA, [privKeyRSA.primaryKey.getKeyId()], 'hello world').then(function(key) {
|
||||
expect(key.primaryKey.isDecrypted).to.be.true;
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('High level API', function() {
|
||||
|
||||
this.timeout(0);
|
||||
|
|
Loading…
Reference in New Issue
Block a user