2018-04-18 14:38:06 +00:00
|
|
|
/* gpgme.js - Javascript integration for gpgme
|
|
|
|
* Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik
|
|
|
|
*
|
|
|
|
* This file is part of GPGME.
|
|
|
|
*
|
|
|
|
* GPGME is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
2018-04-10 09:33:14 +00:00
|
|
|
*
|
2018-04-18 14:38:06 +00:00
|
|
|
* GPGME is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2018-10-18 03:56:27 +00:00
|
|
|
* License along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2018-04-18 14:38:06 +00:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1+
|
2018-06-06 11:05:53 +00:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Maximilian Krambach <mkrambach@intevation.de>
|
2018-04-10 09:33:14 +00:00
|
|
|
*/
|
2018-04-18 14:38:06 +00:00
|
|
|
|
2018-06-06 11:05:53 +00:00
|
|
|
|
|
|
|
import { GpgME } from './gpgmejs';
|
|
|
|
import { gpgme_error } from './Errors';
|
|
|
|
import { Connection } from './Connection';
|
2018-04-24 16:44:30 +00:00
|
|
|
|
|
|
|
/**
|
2018-09-05 16:46:28 +00:00
|
|
|
* Main entry point for gpgme.js. It initializes by testing the nativeMessaging
|
|
|
|
* connection once, and then offers the available functions as method of the
|
|
|
|
* response object.
|
|
|
|
* An unsuccessful attempt will reject as a GPGME_Error.
|
2018-09-19 08:56:36 +00:00
|
|
|
* @param {Object} config (optional) configuration options
|
|
|
|
* @param {Number} config.timeout set the timeout for the initial connection
|
|
|
|
* check. On some machines and operating systems a default timeout of 500 ms is
|
|
|
|
* too low, so a higher number might be attempted.
|
2018-09-05 16:46:28 +00:00
|
|
|
* @returns {Promise<GpgME>}
|
2018-06-11 13:10:43 +00:00
|
|
|
* @async
|
2018-04-24 16:44:30 +00:00
|
|
|
*/
|
2018-10-17 11:13:31 +00:00
|
|
|
function init ({ timeout = 1000 } = {}){
|
2018-08-20 13:12:01 +00:00
|
|
|
return new Promise(function (resolve, reject){
|
2018-08-20 10:12:43 +00:00
|
|
|
const connection = new Connection;
|
2018-09-19 08:56:36 +00:00
|
|
|
connection.checkConnection(false, timeout).then(
|
2018-08-20 13:12:01 +00:00
|
|
|
function (result){
|
2018-05-25 09:53:24 +00:00
|
|
|
if (result === true) {
|
2018-08-20 10:12:43 +00:00
|
|
|
resolve(new GpgME());
|
2018-05-25 09:53:24 +00:00
|
|
|
} else {
|
|
|
|
reject(gpgme_error('CONN_NO_CONNECT'));
|
|
|
|
}
|
2018-08-20 13:12:01 +00:00
|
|
|
}, function (){ // unspecific connection error. Should not happen
|
2018-05-03 12:12:10 +00:00
|
|
|
reject(gpgme_error('CONN_NO_CONNECT'));
|
2018-06-06 11:05:53 +00:00
|
|
|
});
|
2018-04-24 16:44:30 +00:00
|
|
|
});
|
2018-04-26 15:13:34 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:12:01 +00:00
|
|
|
const exportvalue = { init:init };
|
2018-08-01 10:51:12 +00:00
|
|
|
export default exportvalue;
|