aboutsummaryrefslogtreecommitdiffstats
path: root/lang/js/src
diff options
context:
space:
mode:
authorMaximilian Krambach <[email protected]>2018-05-25 09:53:24 +0000
committerMaximilian Krambach <[email protected]>2018-05-25 09:53:24 +0000
commiteff27d6387b1cad2ef9901fa03dbee2ea86c786a (patch)
treeaebdc295b1cd60152fbcfe55f8b7ce69916e40e1 /lang/js/src
parentjs: adding sign method (diff)
downloadgpgme-eff27d6387b1cad2ef9901fa03dbee2ea86c786a.tar.gz
gpgme-eff27d6387b1cad2ef9901fa03dbee2ea86c786a.zip
js: use version operation for connection checks
-- * src/Connection.js: isConnected was renamed to checkConnection, that returns a promise with either version information or Boolean * Connection checks have been adapted to reflect that checkConnection returns a Promise * BrowsertestExtension: tests/signTest.js was missing from my last commit
Diffstat (limited to 'lang/js/src')
-rw-r--r--lang/js/src/Connection.js60
-rw-r--r--lang/js/src/Errors.js4
-rw-r--r--lang/js/src/Keyring.js58
-rw-r--r--lang/js/src/gpgmejs.js8
-rw-r--r--lang/js/src/index.js22
5 files changed, 75 insertions, 77 deletions
diff --git a/lang/js/src/Connection.js b/lang/js/src/Connection.js
index 9c2a6428..07df5def 100644
--- a/lang/js/src/Connection.js
+++ b/lang/js/src/Connection.js
@@ -25,7 +25,7 @@
*/
import { permittedOperations } from './permittedOperations'
import { gpgme_error } from "./Errors"
-import { GPGME_Message } from "./Message";
+import { GPGME_Message, createMessage } from "./Message";
/**
* A Connection handles the nativeMessaging interaction.
@@ -34,18 +34,42 @@ export class Connection{
constructor(){
this.connect();
- let me = this;
}
/**
- * (Simple) Connection check.
- * @returns {Boolean} true if the onDisconnect event has not been fired.
- * Please note that the event listener of the port takes some time
- * (5 ms seems enough) to react after the port is created. Then this will
- * return undefined
+ * Retrieves the information about the backend.
+ * @param {Boolean} details (optional) If set to false, the promise will
+ * just return a connection status
+ * @returns {Promise<Object>}
+ * {String} The property 'gpgme': Version number of gpgme
+ * {Array<Object>} 'info' Further information about the backends.
+ * Example:
+ * "protocol": "OpenPGP",
+ * "fname": "/usr/bin/gpg",
+ * "version": "2.2.6",
+ * "req_version": "1.4.0",
+ * "homedir": "default"
*/
- get isConnected(){
- return this._isConnected;
+ checkConnection(details = true){
+ if (details === true) {
+ return this.post(createMessage('version'));
+ } else {
+ let me = this;
+ return new Promise(function(resolve,reject) {
+ Promise.race([
+ me.post(createMessage('version')),
+ new Promise(function(resolve, reject){
+ setTimeout(function(){
+ reject(gpgme_error('CONN_TIMEOUT'));
+ }, 500);
+ })
+ ]).then(function(result){
+ resolve(true);
+ }, function(reject){
+ resolve(false);
+ });
+ });
+ }
}
/**
@@ -54,6 +78,7 @@ export class Connection{
disconnect() {
if (this._connection){
this._connection.disconnect();
+ this._connection = null;
}
}
@@ -61,17 +86,8 @@ export class Connection{
* Opens a nativeMessaging port.
*/
connect(){
- if (this._isConnected === true){
- gpgme_error('CONN_ALREADY_CONNECTED');
- } else {
- this._isConnected = true;
+ if (!this._connection){
this._connection = chrome.runtime.connectNative('gpgmejson');
- let me = this;
- this._connection.onDisconnect.addListener(
- function(){
- me._isConnected = false;
- }
- );
}
}
@@ -82,8 +98,8 @@ export class Connection{
* information.
*/
post(message){
- if (!this.isConnected){
- return Promise.reject(gpgme_error('CONN_DISCONNECTED'));
+ if (!this._connection) {
+
}
if (!message || !message instanceof GPGME_Message){
return Promise.reject(gpgme_error('PARAM_WRONG'), message);
@@ -199,7 +215,7 @@ class Answer{
if (!this._response.hasOwnProperty(key)){
this._response[key] = [];
}
- this._response.push(msg[key]);
+ this._response[key].push(msg[key]);
}
else {
return gpgme_error('CONN_UNEXPECTED_ANSWER');
diff --git a/lang/js/src/Errors.js b/lang/js/src/Errors.js
index bfe3a2f4..7e98f319 100644
--- a/lang/js/src/Errors.js
+++ b/lang/js/src/Errors.js
@@ -25,10 +25,6 @@ const err_list = {
+ ' established.',
type: 'error'
},
- 'CONN_DISCONNECTED': {
- msg:'Connection with the nativeMessaging host was lost.',
- type: 'error'
- },
'CONN_EMPTY_GPG_ANSWER':{
msg: 'The nativeMessaging answer was empty.',
type: 'error'
diff --git a/lang/js/src/Keyring.js b/lang/js/src/Keyring.js
index 4596035a..80792f77 100644
--- a/lang/js/src/Keyring.js
+++ b/lang/js/src/Keyring.js
@@ -36,10 +36,7 @@ export class GPGME_Keyring {
}
get connection(){
if (this._connection instanceof Connection){
- if (this._connection.isConnected){
- return this._connection;
- }
- return gpgme_error('CONN_DISCONNECTED');
+ return this._connection;
}
return gpgme_error('CONN_NO_CONNECT');
}
@@ -51,36 +48,35 @@ export class GPGME_Keyring {
*
*/
getKeys(pattern, include_secret){
- let msg = createMessage('listkeys');
- if (msg instanceof Error){
- return Promise.reject(msg);
- }
- if (pattern && typeof(pattern) === 'string'){
- msg.setParameter('pattern', pattern);
- }
- if (include_secret){
- msg.setParameter('with-secret', true);
- }
let me = this;
-
- this.connection.post(msg).then(function(result){
- let fpr_list = [];
- let resultset = [];
- if (!Array.isArray(result.keys)){
- //TODO check assumption keys = Array<String fingerprints>
- fpr_list = [result.keys];
- } else {
- fpr_list = result.keys;
+ return new Promise(function(resolve, reject) {
+ let msg;
+ msg = createMessage('listkeys');
+ if (pattern && typeof(pattern) === 'string'){
+ msg.setParameter('pattern', pattern);
}
- for (let i=0; i < fpr_list.length; i++){
- let newKey = new GPGME_Key(fpr_list[i], me._connection);
- if (newKey instanceof GPGME_Key){
- resultset.push(newKey);
- }
+ if (include_secret){
+ msg.setParameter('with-secret', true);
}
- return Promise.resolve(resultset);
- }, function(error){
- //TODO error handling
+ me.connection.post(msg).then(function(result){
+ let fpr_list = [];
+ let resultset = [];
+ if (!Array.isArray(result.keys)){
+ //TODO check assumption keys = Array<String fingerprints>
+ fpr_list = [result.keys];
+ } else {
+ fpr_list = result.keys;
+ }
+ for (let i=0; i < fpr_list.length; i++){
+ let newKey = new GPGME_Key(fpr_list[i], me._connection);
+ if (newKey instanceof GPGME_Key){
+ resultset.push(newKey);
+ }
+ }
+ resolve(resultset);
+ }, function(error){
+ reject(error);
+ });
});
}
diff --git a/lang/js/src/gpgmejs.js b/lang/js/src/gpgmejs.js
index 1e76655e..c182c175 100644
--- a/lang/js/src/gpgmejs.js
+++ b/lang/js/src/gpgmejs.js
@@ -44,13 +44,7 @@ export class GpgME {
}
get connection(){
- if (this._connection){
- if (this._connection.isConnected === true){
- return this._connection;
- }
- return undefined;
- }
- return undefined;
+ return this._connection;
}
set Keyring(keyring){
diff --git a/lang/js/src/index.js b/lang/js/src/index.js
index 8527b3f3..7f969fee 100644
--- a/lang/js/src/index.js
+++ b/lang/js/src/index.js
@@ -34,20 +34,16 @@ function init(config){
}
return new Promise(function(resolve, reject){
let connection = new Connection;
- // TODO: Delayed reaction is ugly. We need to listen to the port's
- // event listener in isConnected, but in some cases this takes some
- // time (<5ms) to disconnect if there is no successfull connection.
- let delayedreaction = function(){
- if (connection === undefined) {
+ connection.checkConnection(false).then(
+ function(result){
+ if (result === true) {
+ resolve(new GpgME(connection, _conf));
+ } else {
+ reject(gpgme_error('CONN_NO_CONNECT'));
+ }
+ }, function(error){
reject(gpgme_error('CONN_NO_CONNECT'));
- }
- if (connection.isConnected === true){
- resolve(new GpgME(connection, _conf));
- } else {
- reject(gpgme_error('CONN_NO_CONNECT'));
- }
- };
- setTimeout(delayedreaction, 5);
+ });
});
}