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