js: use destructured option parameters

--
* Adds to f0409bbdaf and makes use of
  destructuring, allowing for defaults, and cleaning up the
  validation.
This commit is contained in:
Maximilian Krambach 2018-08-23 12:15:59 +02:00
parent 60dc499abd
commit 3fd6837fce
3 changed files with 66 additions and 107 deletions

View File

@ -35,7 +35,7 @@ export class GPGME_Keyring {
/**
* Queries Keys (all Keys or a subset) from gnupg.
*
* @param {Object} options
* @param {Object} options:
* @param {String | Array<String>} options.pattern (optional) A pattern to
* search for in userIds or KeyIds.
* @param {Boolean} options.prepare_sync (optional) if set to true, most
@ -49,24 +49,14 @@ export class GPGME_Keyring {
* @static
* @async
*/
getKeys (options){
if (options && typeof options !== 'object'){
return Promise.reject(gpgme_error('PARAM_WRONG'));
}
getKeys ({ pattern, prepare_sync = false, search = false }){
return new Promise(function (resolve, reject) {
let msg = createMessage('keylist');
if (options && options.pattern) {
if (
typeof options.pattern === 'string'
|| Array.isArray(options.pattern)
){
msg.setParameter('keys', options.pattern);
} else {
reject(gpgme_error('PARAM_WRONG'));
}
if (pattern) {
msg.setParameter('keys', pattern);
}
msg.setParameter('sigs', true);
if (options && options.search === true){
if (search === true){
msg.setParameter('locate', true);
}
msg.post().then(function (result){
@ -75,11 +65,11 @@ export class GPGME_Keyring {
resolve([]);
} else {
let secondrequest;
if (options && options.prepare_sync === true) {
if (prepare_sync === true) {
secondrequest = function () {
let msg2 = createMessage('keylist');
if (options.pattern){
msg2.setParameter('keys', options.pattern);
if (pattern){
msg2.setParameter('keys', pattern);
}
msg2.setParameter('secret', true);
return msg2.post();
@ -91,7 +81,7 @@ export class GPGME_Keyring {
}
secondrequest().then(function (answer) {
for (let i=0; i < result.keys.length; i++){
if (options.prepare_sync === true){
if (prepare_sync === true){
if (answer && answer.keys) {
for (let j=0;
j < answer.keys.length; j++ ){
@ -111,7 +101,7 @@ export class GPGME_Keyring {
}
}
let k = createKey(result.keys[i].fingerprint,
!options.prepare_sync, result.keys[i]);
!prepare_sync, result.keys[i]);
resultset.push(k);
}
resolve(resultset);
@ -147,27 +137,19 @@ export class GPGME_Keyring {
* @static
* @async
*/
getKeysArmored (options) {
if (options && typeof options !== 'object'){
return Promise.reject(gpgme_error('PARAM_WRONG'));
}
getKeysArmored ({ pattern, with_secret_fpr }) {
return new Promise(function (resolve, reject) {
let msg = createMessage('export');
msg.setParameter('armor', true);
if (options.with_secret_fpr === true) {
if (with_secret_fpr === true) {
msg.setParameter('with-sec-fprs', true);
}
if (options.pattern){
if (
typeof options.pattern === 'string'
|| Array.isArray(options.pattern)
){
msg.setParameter('keys', options.pattern);
}
if (pattern){
msg.setParameter('keys', pattern);
}
msg.post().then(function (answer){
const result = { armored: answer.data };
if (options.with_secret_fpr === true){
if (with_secret_fpr === true){
if (answer.hasOwnProperty('sec-fprs')){
result.secret_fprs = answer['sec-fprs'];
} else {
@ -404,39 +386,27 @@ export class GPGME_Keyring {
* @return {Promise<Key|GPGME_Error>}
* @async
*/
generateKey (options){
if (!options
|| typeof options !== 'object'
|| typeof options.userId !== 'string'
generateKey ({ userId, algo = 'default', expires= 0, subkey_algo }){
if (typeof userId !== 'string'
// eslint-disable-next-line no-use-before-define
|| ( options.algo && supportedKeyAlgos.indexOf(options.algo) < 0 )
|| ( options.expires && !(
Number.isInteger(options.expires) || options.expires < 0 ) )
|| (algo && supportedKeyAlgos.indexOf(algo) < 0 )
|| (!Number.isInteger(expires) || expires < 0 )
){
return Promise.reject(gpgme_error('PARAM_WRONG'));
}
// eslint-disable-next-line no-use-before-define
if (options.subkey_algo && supportedKeyAlgos.indexOf(
options.subkey_algo) < 0
){
if (subkey_algo && supportedKeyAlgos.indexOf(subkey_algo) < 0){
return Promise.reject(gpgme_error('PARAM_WRONG'));
}
let me = this;
return new Promise(function (resolve, reject){
let msg = createMessage('createkey');
msg.setParameter('userid', options.userId);
if (!options.algo){
options.algo === 'default';
}
msg.setParameter('algo', options.algo);
if (options.subkey_algo) {
msg.setParameter('subkey-algo', options.subkey_algo );
}
if (options.expires){
msg.setParameter('expires', options.expires);
} else {
msg.setParameter('expires', 0);
msg.setParameter('userid', userId);
msg.setParameter('algo', algo);
if (subkey_algo) {
msg.setParameter('subkey-algo',subkey_algo );
}
msg.setParameter('expires', expires);
msg.post().then(function (response){
me.getKeys(response.fingerprint, true).then(
// TODO prepare_sync?

View File

@ -134,43 +134,39 @@ export class GpgME {
* message and additional info.
* @async
*/
encrypt (options){
if (!options || (typeof options !== 'object')){
return Promise.reject(gpgme_error('PARAM_WRONG'));
}
if (!options.hasOwnProperty('data')
|| !options.hasOwnProperty('publicKeys')
){
encrypt ({ data, publicKeys, secretKeys, base64 = false, armor = true,
wildcard, additional = {} }){
if (!data || !publicKeys){
return Promise.reject(gpgme_error('MSG_INCOMPLETE'));
}
let msg = createMessage('encrypt');
if (msg instanceof Error){
return Promise.reject(msg);
}
if (!options.hasOwnProperty('armor')){
options.armor = true;
}
msg.setParameter('armor', options.armor);
msg.setParameter('armor', armor);
if (options.base64 === true) {
if (base64 === true) {
msg.setParameter('base64', true);
}
let pubkeys = toKeyIdArray(options.publicKeys);
let pubkeys = toKeyIdArray(publicKeys);
if (!pubkeys.length) {
return Promise.reject(gpgme_error('MSG_NO_KEYS'));
}
msg.setParameter('keys', pubkeys);
let sigkeys = toKeyIdArray(options.secretKeys);
let sigkeys = toKeyIdArray(secretKeys);
if (sigkeys.length > 0) {
msg.setParameter('signing_keys', sigkeys);
}
putData(msg, options.data);
if (options.wildcard === true){
putData(msg, data);
if (wildcard === true){
msg.setParameter('throw-keyids', true);
}
if (options.additional){
let additional_Keys = Object.keys(options.additional);
if (additional){
let additional_Keys = Object.keys(additional);
for (let k = 0; k < additional_Keys.length; k++) {
try {
msg.setParameter(additional_Keys[k],
options.additional[additional_Keys[k]]);
additional[additional_Keys[k]]);
}
catch (error){
return Promise.reject(error);
@ -197,11 +193,8 @@ export class GpgME {
* @returns {Promise<decrypt_result>} Decrypted Message and information
* @async
*/
decrypt (options){
if (!options || (typeof options !== 'object')){
return Promise.reject('PARAM_WRONG');
}
if (!options.data){
decrypt ({ data, base64, expect }){
if (!data){
return Promise.reject(gpgme_error('MSG_EMPTY'));
}
let msg = createMessage('decrypt');
@ -209,13 +202,13 @@ export class GpgME {
if (msg instanceof Error){
return Promise.reject(msg);
}
if (options.base64 === true){
if (base64 === true){
msg.setParameter('base64', true);
}
if (options.expect === 'base64' || options.expect === 'uint8'){
msg.expected = options.expect;
if (expect === 'base64' || expect === 'uint8'){
msg.expected = expect;
}
putData(msg, options.data);
putData(msg, data);
return new Promise(function (resolve, reject){
msg.post().then(function (result){
let _result = { data: result.data };
@ -260,38 +253,32 @@ export class GpgME {
* @returns {Promise<signResult>}
* @async
*/
sign (options){
if (
!options || (typeof options !== 'object')){
return Promise.reject(gpgme_error('PARAM_WRONG'));
}
if (!options.data){
sign ({ data, keys, mode = 'clearsign', base64 }){
if (!data){
return Promise.reject(gpgme_error('MSG_EMPTY'));
}
if (!options.mode) {
options.mode = 'clearsign';
}
let key_arr = toKeyIdArray(options.keys);
let key_arr = toKeyIdArray(keys);
if (key_arr.length === 0){
return Promise.reject(gpgme_error('MSG_NO_KEYS'));
}
let msg = createMessage('sign');
let msg = createMessage('sign');
msg.setParameter('keys', key_arr);
if (options.base64 === true){
if (base64 === true){
msg.setParameter('base64', true);
}
msg.setParameter('mode', options.mode);
putData(msg, options.data);
msg.setParameter('mode', mode);
putData(msg, data);
return new Promise(function (resolve,reject) {
msg.post().then( function (message) {
if (options.mode === 'clearsign'){
if (mode === 'clearsign'){
resolve({
data: message.data }
);
} else if (options.mode === 'detached') {
} else if (mode === 'detached') {
resolve({
data: options.data,
data: data,
signature: message.data
});
}
@ -313,23 +300,23 @@ export class GpgME {
* @returns {Promise<verifyResult>}
*@async
*/
verify (options){
if (!options || (typeof options !== 'object') || !options.data){
verify ({ data, signature, base64 }){
if (!data){
return Promise.reject(gpgme_error('PARAM_WRONG'));
}
let msg = createMessage('verify');
let dt = putData(msg, options.data);
let dt = putData(msg, data);
if (dt instanceof Error){
return Promise.reject(dt);
}
if (options.signature){
if (signature){
if (typeof signature !== 'string'){
return Promise.reject(gpgme_error('PARAM_WRONG'));
} else {
msg.setParameter('signature', signature);
}
}
if (options.base64 === true){
if (base64 === true){
msg.setParameter('base64', true);
}
return new Promise(function (resolve, reject){

View File

@ -262,7 +262,7 @@ function unittests (){
it('Loading Keys from Keyring, to be used synchronously',
function (done){
let keyring = new GPGME_Keyring;
keyring.getKeys(null, true).then(function (result){
keyring.getKeys({ prepare_sync: true }).then(function (result){
expect(result).to.be.an('array');
expect(result[0].get('hasSecret')).to.be.a('boolean');
done();
@ -273,7 +273,9 @@ function unittests (){
it('Loading specific Key from Keyring, to be used synchronously',
function (done){
let keyring = new GPGME_Keyring;
keyring.getKeys(kp.validKeyFingerprint, true).then(
keyring.getKeys({
pattern: kp.validKeyFingerprint,
prepare_sync: true }).then(
function (result){
expect(result).to.be.an('array');
expect(result[0].get('hasSecret')).to.be.a('boolean');