diff options
author | Maximilian Krambach <[email protected]> | 2018-04-24 16:44:30 +0000 |
---|---|---|
committer | Maximilian Krambach <[email protected]> | 2018-04-24 16:44:30 +0000 |
commit | 461dd0c8b41683a91073b362d100ee5217ec53f6 (patch) | |
tree | 15ab814b70a583db52f7f9e664aaa016057d98d0 /lang/js/src/index.js | |
parent | js: don't allow message operation changes (diff) | |
download | gpgme-461dd0c8b41683a91073b362d100ee5217ec53f6.tar.gz gpgme-461dd0c8b41683a91073b362d100ee5217ec53f6.zip |
js: change in initialization ancd connection handling
--
* The Connection will now be started before an object is created, to
better account for failures.
* index.js: now exposes an init(), which returns a Promise of
configurable <GpgME | gpgmeGpgME_openPGPCompatibility> with an
established connection.
* TODO: There is currently no way to recover from a "connection lost"
* Connection.js offers Connection.isConnected, which toggles on port
closing.
Diffstat (limited to '')
-rw-r--r-- | lang/js/src/index.js | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/lang/js/src/index.js b/lang/js/src/index.js index f70bd2d8..0e2beda4 100644 --- a/lang/js/src/index.js +++ b/lang/js/src/index.js @@ -18,6 +18,40 @@ * SPDX-License-Identifier: LGPL-2.1+ */ -import { GpgME as gpgmejs } from "./gpgmejs"; -// import { GpgME_openPGPCompatibility as gpgmejs } from "./gpgmejs_openpgpjs"; -export default gpgmejs; +import { GpgME } from "./gpgmejs"; +import { GpgME_openPGPCompatibility } from "./gpgmejs_openpgpjs"; +import { Connection } from "./Connection"; + +/** + * Initializes a nativeMessaging Connection and returns a GPGMEjs object + * @param {*} conf Configuration. TBD + */ +function init( config = { + api_style: 'gpgme', // | gpgme_openpgpjs + null_expire_is_never: true // Boolean + }){ + 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 this takes some time (<5ms) to + // disconnect if there is no successfull connection. + let delayedreaction = function(){ + if (connection.isConnected === true){ + let gpgme = null; + if (config.api_style && config.api_style === 'gpgme_openpgpjs'){ + resolve( + new GpgME_openPGPCompatibility(connection)); + } else { + resolve(new GpgME(connection)); + } + } else { + reject('NO_CONNECT'); + } + }; + setTimeout(delayedreaction, 5); + }); +}; + +export default { + init: init +}
\ No newline at end of file |