Add timeout to promise, return undefined on error

This commit is contained in:
Yarmo Mackenbach 2020-12-20 22:44:37 +01:00
parent 7daececb99
commit 686f0e2a5e
2 changed files with 104 additions and 90 deletions

View file

@ -117,7 +117,6 @@ const verify = async (input, fingerprint, opts) => {
const res = await verify(user.notations, fingerprintFromKey, opts)
resolve(res)
} catch (e) {
console.error(`Claim verification failed: ${user.userData.id}`, e)
reject(e)
}
})
@ -136,7 +135,6 @@ const verify = async (input, fingerprint, opts) => {
const res = await verify(uri, fingerprint, opts)
resolve(res)
} catch (e) {
console.error(`Claim verification failed: ${uri}`, e)
reject(e)
}
})
@ -149,8 +147,13 @@ const verify = async (input, fingerprint, opts) => {
})
}
const promiseClaim = new Promise(async (resolve, reject) => {
let objResult = {
isVerified: null,
errors: [],
serviceproviderData: null,
}
const uri = input.replace(/^\s+|\s+$/g, '')
let verifErrors = []
if (!fingerprint) {
fingerprint = null
@ -164,13 +167,14 @@ const verify = async (input, fingerprint, opts) => {
opts = mergeOptions(defaultOpts, opts ? opts : {})
if (!validUrl.isUri(uri)) {
throw new Error('Invalid URI')
objResult.errors.push('invalid_uri')
reject(objResult)
}
const spMatches = serviceproviders.match(uri, opts)
if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) {
return spMatches
resolve(spMatches)
}
let claimVerificationDone = false,
@ -191,7 +195,7 @@ const verify = async (input, fingerprint, opts) => {
try {
proofData = await spData.customRequestHandler(spData, opts)
} catch (e) {
verifErrors.push('custom_request_handler_failed')
objResult.errors.push('custom_request_handler_failed')
}
} else {
switch (opts.proxyPolicy) {
@ -232,7 +236,7 @@ const verify = async (input, fingerprint, opts) => {
} catch(er) {}
break;
default:
verifErrors.push('invalid_proxy_policy')
objResult.errors.push('invalid_proxy_policy')
}
}
@ -243,7 +247,7 @@ const verify = async (input, fingerprint, opts) => {
claimVerificationDone = true
}
} else {
verifErrors.push('unsuccessful_claim_verification')
objResult.errors.push('unsuccessful_claim_verification')
}
iSp++
@ -255,11 +259,21 @@ const verify = async (input, fingerprint, opts) => {
}
}
return {
isVerified: claimVerificationResult.isVerified,
errors: verifErrors,
serviceproviderData: spData,
objResult.isVerified = claimVerificationResult.isVerified
objResult.serviceproviderData = spData
resolve(objResult)
})
const promiseTimeout = new Promise((res) => {
const objResult = {
isVerified: null,
errors: 'verification_timed_out',
serviceproviderData: null,
}
setTimeout(() => res(objResult), 5000)
})
return await Promise.race([promiseClaim, promiseTimeout])
}
exports.verify = verify

View file

@ -62,13 +62,13 @@ describe('claims.verify', () => {
expect(doipjs.claims.verify).to.be.a('function')
expect(doipjs.claims.verify).to.have.length(3)
})
it('should throw an error for non-valid URIs', () => {
it('should return undefined for non-valid URIs', () => {
return expect(doipjs.claims.verify('noURI')).to.eventually.be.rejectedWith(
'Invalid URI'
undefined
)
return expect(
doipjs.claims.verify('domain.org')
).to.eventually.be.rejectedWith('Invalid URI')
).to.eventually.be.rejectedWith(undefined)
})
doipjs.serviceproviders.list.forEach((spName, i) => {