js: typecheck destructured parameters
-- * destructuring just takes the input argument and treats it as object. In cases like in src/Keyring/generateKey, where I forgot to change the old syntax, the fingerprint as string was destructured into an object without "pattern", which caused all Keys to be retrieved. So, methods with a destructuring now check if the first argument is an object and get a default empty object if no parameter is submitted. This allows the further use of destructured parameters, while still ensuring nothing vastly incorrect is used. * src/Kering.js, unittsets.js: fixed old syntax in method usage
This commit is contained in:
parent
53c5b9a265
commit
766d42c248
@ -49,7 +49,13 @@ export class GPGME_Keyring {
|
|||||||
* @static
|
* @static
|
||||||
* @async
|
* @async
|
||||||
*/
|
*/
|
||||||
getKeys ({ pattern, prepare_sync = false, search = false }){
|
getKeys ({ pattern, prepare_sync = false, search = false } = {}){
|
||||||
|
if (typeof arguments[0] !== 'object') {
|
||||||
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
||||||
|
}
|
||||||
|
if (arguments.length && typeof arguments[0] !== 'object') {
|
||||||
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
||||||
|
}
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
let msg = createMessage('keylist');
|
let msg = createMessage('keylist');
|
||||||
if (pattern) {
|
if (pattern) {
|
||||||
@ -387,7 +393,7 @@ export class GPGME_Keyring {
|
|||||||
* @return {Promise<Key|GPGME_Error>}
|
* @return {Promise<Key|GPGME_Error>}
|
||||||
* @async
|
* @async
|
||||||
*/
|
*/
|
||||||
generateKey ({ userId, algo = 'default', expires= 0, subkey_algo }){
|
generateKey ({ userId, algo = 'default', expires= 0, subkey_algo } = {}){
|
||||||
if (typeof userId !== 'string'
|
if (typeof userId !== 'string'
|
||||||
// eslint-disable-next-line no-use-before-define
|
// eslint-disable-next-line no-use-before-define
|
||||||
|| (algo && supportedKeyAlgos.indexOf(algo) < 0 )
|
|| (algo && supportedKeyAlgos.indexOf(algo) < 0 )
|
||||||
@ -409,13 +415,14 @@ export class GPGME_Keyring {
|
|||||||
}
|
}
|
||||||
msg.setParameter('expires', expires);
|
msg.setParameter('expires', expires);
|
||||||
msg.post().then(function (response){
|
msg.post().then(function (response){
|
||||||
me.getKeys(response.fingerprint, true).then(
|
me.getKeys({
|
||||||
// TODO prepare_sync?
|
pattern: response.fingerprint,
|
||||||
function (result){
|
prepare_sync: true
|
||||||
resolve(result);
|
}).then(function (result){
|
||||||
}, function (error){
|
resolve(result);
|
||||||
reject(error);
|
}, function (error){
|
||||||
});
|
reject(error);
|
||||||
|
});
|
||||||
}, function (error) {
|
}, function (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
@ -142,7 +142,10 @@ export class GpgME {
|
|||||||
* @async
|
* @async
|
||||||
*/
|
*/
|
||||||
encrypt ({ data, publicKeys, secretKeys, base64 = false, armor = true,
|
encrypt ({ data, publicKeys, secretKeys, base64 = false, armor = true,
|
||||||
wildcard, always_trust = true, additional = {} }){
|
wildcard, always_trust = true, additional = {} } = {}){
|
||||||
|
if (typeof arguments[0] !== 'object') {
|
||||||
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
||||||
|
}
|
||||||
if (!data || !publicKeys){
|
if (!data || !publicKeys){
|
||||||
return Promise.reject(gpgme_error('MSG_INCOMPLETE'));
|
return Promise.reject(gpgme_error('MSG_INCOMPLETE'));
|
||||||
}
|
}
|
||||||
@ -203,7 +206,10 @@ export class GpgME {
|
|||||||
* @returns {Promise<decrypt_result>} Decrypted Message and information
|
* @returns {Promise<decrypt_result>} Decrypted Message and information
|
||||||
* @async
|
* @async
|
||||||
*/
|
*/
|
||||||
decrypt ({ data, base64, expect }){
|
decrypt ({ data, base64, expect } = {}){
|
||||||
|
if (typeof arguments[0] !== 'object') {
|
||||||
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
||||||
|
}
|
||||||
if (!data){
|
if (!data){
|
||||||
return Promise.reject(gpgme_error('MSG_EMPTY'));
|
return Promise.reject(gpgme_error('MSG_EMPTY'));
|
||||||
}
|
}
|
||||||
@ -263,7 +269,10 @@ export class GpgME {
|
|||||||
* @returns {Promise<signResult>}
|
* @returns {Promise<signResult>}
|
||||||
* @async
|
* @async
|
||||||
*/
|
*/
|
||||||
sign ({ data, keys, mode = 'clearsign', base64 }){
|
sign ({ data, keys, mode = 'clearsign', base64 } = {}){
|
||||||
|
if (typeof arguments[0] !== 'object') {
|
||||||
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
||||||
|
}
|
||||||
if (!data){
|
if (!data){
|
||||||
return Promise.reject(gpgme_error('MSG_EMPTY'));
|
return Promise.reject(gpgme_error('MSG_EMPTY'));
|
||||||
}
|
}
|
||||||
@ -310,7 +319,10 @@ export class GpgME {
|
|||||||
* @returns {Promise<verifyResult>}
|
* @returns {Promise<verifyResult>}
|
||||||
*@async
|
*@async
|
||||||
*/
|
*/
|
||||||
verify ({ data, signature, base64 }){
|
verify ({ data, signature, base64 } = {}){
|
||||||
|
if (typeof arguments[0] !== 'object') {
|
||||||
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
||||||
|
}
|
||||||
if (!data){
|
if (!data){
|
||||||
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
return Promise.reject(gpgme_error('PARAM_WRONG'));
|
||||||
}
|
}
|
||||||
|
@ -287,13 +287,14 @@ function unittests (){
|
|||||||
|
|
||||||
it('Querying non-existing Key from Keyring', function (done){
|
it('Querying non-existing Key from Keyring', function (done){
|
||||||
let keyring = new GPGME_Keyring;
|
let keyring = new GPGME_Keyring;
|
||||||
keyring.getKeys(kp.invalidKeyFingerprint, true).then(
|
keyring.getKeys({
|
||||||
function (result){
|
pattern: kp.invalidKeyFingerprint,
|
||||||
expect(result).to.be.an('array');
|
prepare_sync: true
|
||||||
expect(result.length).to.equal(0);
|
}).then(function (result){
|
||||||
done();
|
expect(result).to.be.an('array');
|
||||||
}
|
expect(result.length).to.equal(0);
|
||||||
);
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user