Skip to content

Commit c9b4f75

Browse files
author
Thomas Oberndörfer
committed
Add getter method for worker to high level API. Initialization options for AsyncProxy: path and worker.
1 parent eb7f854 commit c9b4f75

3 files changed

Lines changed: 55 additions & 32 deletions

File tree

src/openpgp.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,29 @@ if (typeof Promise === 'undefined') {
4646
require('es6-promise').polyfill();
4747
}
4848

49-
var asyncProxy; // instance of the asyncproxy
49+
var asyncProxy = null; // instance of the asyncproxy
5050

5151
/**
5252
* Set the path for the web worker script and create an instance of the async proxy
53-
* @param {String} path relative path to the worker scripts
53+
* @param {Object} [options.path=String] relative path to the worker scripts, default: 'openpgp.worker.js'
54+
* [options.worker=module:async_proxy~AsyncProxy] initialized AsyncProxy object to be used as a worker
55+
* @return {Boolean} true if worker created successfully
5456
*/
55-
function initWorker(path) {
56-
asyncProxy = new AsyncProxy(path);
57+
function initWorker(options) {
58+
if (!options.worker &&
59+
(typeof window === 'undefined' || !window.Worker)) {
60+
return false;
61+
}
62+
asyncProxy = new AsyncProxy(options);
63+
return true;
64+
}
65+
66+
/**
67+
* Returns a reference to the async proxy if the worker was initialized with openpgp.initWorker()
68+
* @return {module:worker/async_proxy~AsyncProxy|null} the async proxy or null if not initialized
69+
*/
70+
function getWorker() {
71+
return asyncProxy;
5772
}
5873

5974
/**
@@ -68,7 +83,7 @@ function encryptMessage(keys, text) {
6883
keys = [keys];
6984
}
7085

71-
if (useWorker()) {
86+
if (asyncProxy) {
7287
return asyncProxy.encryptMessage(keys, text);
7388
}
7489

@@ -95,7 +110,7 @@ function signAndEncryptMessage(publicKeys, privateKey, text) {
95110
publicKeys = [publicKeys];
96111
}
97112

98-
if (useWorker()) {
113+
if (asyncProxy) {
99114
return asyncProxy.signAndEncryptMessage(publicKeys, privateKey, text);
100115
}
101116

@@ -119,7 +134,7 @@ function signAndEncryptMessage(publicKeys, privateKey, text) {
119134
* @static
120135
*/
121136
function decryptMessage(privateKey, msg) {
122-
if (useWorker()) {
137+
if (asyncProxy) {
123138
return asyncProxy.decryptMessage(privateKey, msg);
124139
}
125140

@@ -145,7 +160,7 @@ function decryptAndVerifyMessage(privateKey, publicKeys, msg) {
145160
publicKeys = [publicKeys];
146161
}
147162

148-
if (useWorker()) {
163+
if (asyncProxy) {
149164
return asyncProxy.decryptAndVerifyMessage(privateKey, publicKeys, msg);
150165
}
151166

@@ -174,7 +189,7 @@ function signClearMessage(privateKeys, text) {
174189
privateKeys = [privateKeys];
175190
}
176191

177-
if (useWorker()) {
192+
if (asyncProxy) {
178193
return asyncProxy.signClearMessage(privateKeys, text);
179194
}
180195

@@ -199,7 +214,7 @@ function verifyClearSignedMessage(publicKeys, msg) {
199214
publicKeys = [publicKeys];
200215
}
201216

202-
if (useWorker()) {
217+
if (asyncProxy) {
203218
return asyncProxy.verifyClearSignedMessage(publicKeys, msg);
204219
}
205220

@@ -229,7 +244,7 @@ function verifyClearSignedMessage(publicKeys, msg) {
229244
*/
230245
function generateKeyPair(options) {
231246
// use web worker if web crypto apis are not supported
232-
if (!util.getWebCrypto() && useWorker()) {
247+
if (!util.getWebCrypto() && asyncProxy) {
233248
return asyncProxy.generateKeyPair(options);
234249
}
235250

@@ -259,22 +274,6 @@ function generateKeyPair(options) {
259274
// helper functions
260275
//
261276

262-
/**
263-
* Are we in a browser and do we support worker?
264-
*/
265-
function useWorker() {
266-
if (typeof window === 'undefined' || !window.Worker) {
267-
return false;
268-
}
269-
270-
if (!asyncProxy) {
271-
console.log('You need to set the worker path!');
272-
return false;
273-
}
274-
275-
return true;
276-
}
277-
278277
/**
279278
* Command pattern that wraps synchronous code into a promise
280279
* @param {function} cmd The synchronous function with a return value
@@ -307,6 +306,7 @@ function onError(message, error) {
307306
}
308307

309308
exports.initWorker = initWorker;
309+
exports.getWorker = getWorker;
310310
exports.encryptMessage = encryptMessage;
311311
exports.signAndEncryptMessage = signAndEncryptMessage;
312312
exports.decryptMessage = decryptMessage;

src/worker/async_proxy.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ var INITIAL_RANDOM_SEED = 50000, // random bytes seeded to worker
3939
* @constructor
4040
* @param {String} path The path to the worker or 'openpgp.worker.js' by default
4141
*/
42-
function AsyncProxy(path) {
43-
this.worker = new Worker(path || 'openpgp.worker.js');
42+
function AsyncProxy(options) {
43+
if (options && options.worker) {
44+
this.worker = options.worker;
45+
} else {
46+
this.worker = new Worker(options && options.path || 'openpgp.worker.js');
47+
}
4448
this.worker.onmessage = this.onMessage.bind(this);
4549
this.worker.onerror = function(e) {
4650
throw new Error('Unhandled error in openpgp worker: ' + e.message + ' (' + e.filename + ':' + e.lineno + ')');

test/worker/api.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,31 @@ var priv_key_de =
172172
expect(privKeyDE).to.exist;
173173
}
174174

175+
describe('Init Worker', function() {
176+
177+
this.timeout(0);
178+
179+
it('openpgp.getWorker method', function (done) {
180+
expect(openpgp.getWorker()).to.be.null;
181+
var workerAvailable = openpgp.initWorker({path: '../dist/openpgp.worker.js'});
182+
expect(workerAvailable).to.be.true;
183+
expect(openpgp.getWorker()).to.exist;
184+
privKeyRSA = openpgp.key.readArmored(priv_key_rsa).keys[0];
185+
expect(privKeyRSA.primaryKey.isDecrypted).to.be.false;
186+
openpgp.getWorker().decryptKeyPacket(privKeyRSA, [privKeyRSA.primaryKey.getKeyId()], 'hello world').then(function(key) {
187+
expect(key.primaryKey.isDecrypted).to.be.true;
188+
done();
189+
}).catch(done);
190+
});
191+
192+
});
193+
175194
describe('High level API', function() {
176195

177196
this.timeout(0);
178197

179198
before(function() {
180-
openpgp.initWorker('../dist/openpgp.worker.js');
199+
openpgp.initWorker({path: '../dist/openpgp.worker.js'});
181200
initKeys();
182201
});
183202

@@ -353,7 +372,7 @@ describe('High level API', function() {
353372
});
354373

355374
it('Depleted random buffer in worker gives error', function (done) {
356-
var wProxy = new openpgp.AsyncProxy('../dist/openpgp.worker.js');
375+
var wProxy = new openpgp.AsyncProxy({path: '../dist/openpgp.worker.js'});
357376
wProxy.worker = new Worker('../dist/openpgp.worker.js');
358377
wProxy.worker.onmessage = wProxy.onMessage.bind(wProxy);
359378
wProxy.seedRandom(10);
@@ -385,7 +404,7 @@ describe('High level API', function() {
385404
var msg, proxy;
386405

387406
beforeEach(function() {
388-
proxy = new openpgp.AsyncProxy('../dist/openpgp.worker.js');
407+
proxy = new openpgp.AsyncProxy({path: '../dist/openpgp.worker.js'});
389408
initKeys();
390409
msg = openpgp.message.fromText(plaintext).encrypt([pubKeyRSA]);
391410
});

0 commit comments

Comments
 (0)