@@ -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 */
121136function 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 */
230245function 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
309308exports . initWorker = initWorker ;
309+ exports . getWorker = getWorker ;
310310exports . encryptMessage = encryptMessage ;
311311exports . signAndEncryptMessage = signAndEncryptMessage ;
312312exports . decryptMessage = decryptMessage ;
0 commit comments