From ed1dffb4748e686e525883b5efde9288b8e003d3 Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Wed, 3 Jul 2019 16:19:44 +0200 Subject: js: improve connection check -- * Connection.js: In some cases, the browser disconnect does not happen inmediately (e.g. wrong extension for the app). I added a delay of 25 ms to see if the connection was closed by the browser. Also, I tried to make the checkConnection more readable. --- lang/js/BrowserTestExtension/tests/startup.js | 12 ++++++ lang/js/src/Connection.js | 60 ++++++++++++++++----------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/lang/js/BrowserTestExtension/tests/startup.js b/lang/js/BrowserTestExtension/tests/startup.js index 34333e6b..c1c576cc 100644 --- a/lang/js/BrowserTestExtension/tests/startup.js +++ b/lang/js/BrowserTestExtension/tests/startup.js @@ -44,4 +44,16 @@ describe('GPGME context', function (){ done(); }); }); + + it('Error message on unsuccessful connection (timeout)', function (done) { + let prm = Gpgmejs.init({ timeout: 1 }); + prm.then( + null, + function (error){ + expect(error).to.be.an('error'); + expect(error.code).to.equal('CONN_TIMEOUT'); + done(); + } + ); + }); }); \ No newline at end of file diff --git a/lang/js/src/Connection.js b/lang/js/src/Connection.js index 923698a4..e9ab3b13 100644 --- a/lang/js/src/Connection.js +++ b/lang/js/src/Connection.js @@ -99,25 +99,29 @@ export class Connection{ timeout = 1000; } const msg = createMessage('version'); - if (details === true) { - return this.post(msg); - } else { - let me = this; - return new Promise(function (resolve) { - Promise.race([ - me.post(msg), - new Promise(function (resolve, reject){ - setTimeout(function (){ - reject(gpgme_error('CONN_TIMEOUT')); - }, timeout); - }) - ]).then(function (){ // success + const prm = Promise.race([ + this.post(msg), + new Promise(function (resolve, reject){ + setTimeout(function (){ + reject(gpgme_error('CONN_TIMEOUT')); + }, timeout); + }) + ]); + return new Promise( function (resolve, reject) { + prm.then(function (success){ + if (details === true ) { + resolve(success); + } else { resolve(true); - }, function (){ // failure + } + }, function (error) { + if (details === true ) { + reject(error); + } else { resolve(false); - }); + } }); - } + }); } /** @@ -141,14 +145,6 @@ export class Connection{ this.disconnect(); return Promise.reject(gpgme_error('MSG_INCOMPLETE')); } - if (this.isDisconnected) { - if ( this.isNativeHostUnknown === true) { - return Promise.reject(gpgme_error('CONN_NO_CONFIG')); - } else { - return Promise.reject(gpgme_error( - 'CONN_NO_CONNECT', this._connectionError)); - } - } let chunksize = message.chunksize; const me = this; const nativeCommunication = new Promise(function (resolve, reject){ @@ -185,6 +181,20 @@ export class Connection{ }; me._connection.onMessage.addListener(listener); me._connection.postMessage(message.message); + + // check for browser messaging errors after a while + // (browsers' extension permission checks take some time) + setTimeout( () => { + if (me.isDisconnected) { + if ( me.isNativeHostUnknown === true) { + return reject(gpgme_error('CONN_NO_CONFIG')); + } else { + return reject(gpgme_error( + 'CONN_NO_CONNECT', me._connectionError)); + } + } + }, 25); + }); if (permittedOperations[message.operation].pinentry === true) { return nativeCommunication; @@ -193,7 +203,7 @@ export class Connection{ nativeCommunication, new Promise(function (resolve, reject){ setTimeout(function (){ - me._connection.disconnect(); + me.disconnect(); reject(gpgme_error('CONN_TIMEOUT')); }, 5000); }) -- cgit v1.2.1