diff --git a/src/openpgp.js b/src/openpgp.js index 955e9147..f70d9f9c 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -50,17 +50,19 @@ var asyncProxy = null; // instance of the asyncproxy /** * Set the path for the web worker script and create an instance of the async proxy - * @param {Object} [options.path=String] relative path to the worker scripts, default: 'openpgp.worker.js' - * [options.worker=module:async_proxy~AsyncProxy] initialized AsyncProxy object to be used as a worker + * @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(options) { - if (!options.worker && - (typeof window === 'undefined' || !window.Worker)) { +function initWorker(path, options) { + if (options && options.worker || + typeof window !== 'undefined' && window.Worker) { + asyncProxy = new AsyncProxy(path, options); + return true; + } else { return false; } - asyncProxy = new AsyncProxy(options); - return true; } /** diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index 7a6c9e33..6d08b07a 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -38,12 +38,14 @@ 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(options) { +function AsyncProxy(path, options) { if (options && options.worker) { this.worker = options.worker; } else { - this.worker = new Worker(options && options.path || 'openpgp.worker.js'); + this.worker = new Worker(path || 'openpgp.worker.js'); } this.worker.onmessage = this.onMessage.bind(this); this.worker.onerror = function(e) { diff --git a/test/worker/api.js b/test/worker/api.js index 413d29d5..1fea6b07 100644 --- a/test/worker/api.js +++ b/test/worker/api.js @@ -178,7 +178,7 @@ describe('Init Worker', function() { it('openpgp.getWorker method', function (done) { expect(openpgp.getWorker()).to.be.null; - var workerAvailable = openpgp.initWorker({path: '../dist/openpgp.worker.js'}); + 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]; @@ -196,7 +196,7 @@ describe('High level API', function() { this.timeout(0); before(function() { - openpgp.initWorker({path: '../dist/openpgp.worker.js'}); + openpgp.initWorker('../dist/openpgp.worker.js'); initKeys(); }); @@ -372,7 +372,7 @@ describe('High level API', function() { }); it('Depleted random buffer in worker gives error', function (done) { - var wProxy = new openpgp.AsyncProxy({path: '../dist/openpgp.worker.js'}); + var wProxy = new openpgp.AsyncProxy('../dist/openpgp.worker.js'); wProxy.worker = new Worker('../dist/openpgp.worker.js'); wProxy.worker.onmessage = wProxy.onMessage.bind(wProxy); wProxy.seedRandom(10); @@ -404,7 +404,7 @@ describe('High level API', function() { var msg, proxy; beforeEach(function() { - proxy = new openpgp.AsyncProxy({path: '../dist/openpgp.worker.js'}); + proxy = new openpgp.AsyncProxy('../dist/openpgp.worker.js'); initKeys(); msg = openpgp.message.fromText(plaintext).encrypt([pubKeyRSA]); });