js: reactivate timeout on connection
authorMaximilian Krambach <maximilian.krambach@intevation.de>
Wed, 25 Apr 2018 09:32:21 +0000 (11:32 +0200)
committerMaximilian Krambach <maximilian.krambach@intevation.de>
Wed, 25 Apr 2018 09:32:21 +0000 (11:32 +0200)
--
* A timeout of 5 seconds is activated for functions that do not require
  a pinentry. This definition is written to src/permittedOperations.js
* testapplication.js now alerts the proper error codes and messages.
* src/Errors.js fixed two typos in error handling

lang/js/src/Connection.js
lang/js/src/Errors.js
lang/js/src/permittedOperations.js
lang/js/testapplication.js

index 8bc3d42..4270be5 100644 (file)
@@ -118,14 +118,18 @@ export class Connection{
             };
 
             me._connection.onMessage.addListener(listener);
-            me._connection.postMessage(message.message);
-
-            //TBD: needs to be aware if there is a pinentry pending
-            // setTimeout(
-            //     function(){
-            //         me.disconnect();
-            //         reject(GPGMEJS_Error('CONN_TIMEOUT'));
-            //     }, timeout);
+            let timeout = new Promise(function(resolve, reject){
+                setTimeout(function(){
+                    reject(GPGMEJS_Error('CONN_TIMEOUT'));
+                }, 5000);
+            });
+            if (permittedOperations[message.operation].pinentry){
+                return me._connection.postMessage(message.message);
+            } else {
+                return Promise.race([timeout,
+                    me._connection.postMessage(message.message)
+                ]);
+            }
         });
      }
 };
index c49bfe2..04b13e1 100644 (file)
@@ -104,12 +104,12 @@ export function GPGMEJS_Error(code = 'GENERIC_ERROR'){
         if (errors.hasOwnProperty(code)){
             code = 'GENERIC_ERROR';
         }
-        if (error.type === 'error'){
+        if (errors.type === 'error'){
             return {code: 'code',
                     msg: errors[code].msg
                 };
         }
-        if (error.type === 'warning'){
+        if (errors.type === 'warning'){
             console.log(code + ': ' + error[code].msg);
         }
         return undefined;
index 3c11b8e..892f4f2 100644 (file)
@@ -23,6 +23,8 @@
   * operation: <Object>
       required: Array<String>
       optional: Array<String>
+      pinentry: Boolean If a pinentry dialog is expected, and a timeout of
+                5000 ms would be too short
       answer: <Object>
           type: <String< The content type of answer expected
           data: Array<String> The payload property of the answer. May be
@@ -59,6 +61,7 @@ export const permittedOperations = {
     },
 
     decrypt: {
+        pinentry: true,
         required: ['data'],
         optional: [
             'protocol',
index f47299e..b2cb4c2 100644 (file)
@@ -33,7 +33,7 @@ document.addEventListener('DOMContentLoaded', function() {
                         document.getElementById('answer').value = answer.data;
                         }
                     }, function(errormsg){
-                        alert('Error: '+ errormsg);
+                        alert( errormsg.code + ' ' + errormsg.msg);
                 });
             });
 
@@ -47,7 +47,7 @@ document.addEventListener('DOMContentLoaded', function() {
                         document.getElementById('answer').value = answer.data;
                     }
                 }, function(errormsg){
-                    alert('Error: '+ errormsg);
+                    alert( errormsg.code + ' ' + errormsg.msg);
             });
         });
     },