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.
This commit is contained in:
Maximilian Krambach 2019-07-03 16:19:44 +02:00
parent f5e27a12d3
commit ed1dffb474
2 changed files with 47 additions and 25 deletions

View File

@ -44,4 +44,16 @@ describe('GPGME context', function (){
done(); 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();
}
);
});
}); });

View File

@ -99,25 +99,29 @@ export class Connection{
timeout = 1000; timeout = 1000;
} }
const msg = createMessage('version'); const msg = createMessage('version');
if (details === true) { const prm = Promise.race([
return this.post(msg); this.post(msg),
} else { new Promise(function (resolve, reject){
let me = this; setTimeout(function (){
return new Promise(function (resolve) { reject(gpgme_error('CONN_TIMEOUT'));
Promise.race([ }, timeout);
me.post(msg), })
new Promise(function (resolve, reject){ ]);
setTimeout(function (){ return new Promise( function (resolve, reject) {
reject(gpgme_error('CONN_TIMEOUT')); prm.then(function (success){
}, timeout); if (details === true ) {
}) resolve(success);
]).then(function (){ // success } else {
resolve(true); resolve(true);
}, function (){ // failure }
}, function (error) {
if (details === true ) {
reject(error);
} else {
resolve(false); resolve(false);
}); }
}); });
} });
} }
/** /**
@ -141,14 +145,6 @@ export class Connection{
this.disconnect(); this.disconnect();
return Promise.reject(gpgme_error('MSG_INCOMPLETE')); 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; let chunksize = message.chunksize;
const me = this; const me = this;
const nativeCommunication = new Promise(function (resolve, reject){ const nativeCommunication = new Promise(function (resolve, reject){
@ -185,6 +181,20 @@ export class Connection{
}; };
me._connection.onMessage.addListener(listener); me._connection.onMessage.addListener(listener);
me._connection.postMessage(message.message); 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) { if (permittedOperations[message.operation].pinentry === true) {
return nativeCommunication; return nativeCommunication;
@ -193,7 +203,7 @@ export class Connection{
nativeCommunication, nativeCommunication,
new Promise(function (resolve, reject){ new Promise(function (resolve, reject){
setTimeout(function (){ setTimeout(function (){
me._connection.disconnect(); me.disconnect();
reject(gpgme_error('CONN_TIMEOUT')); reject(gpgme_error('CONN_TIMEOUT'));
}, 5000); }, 5000);
}) })