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) const res = await verify(user.notations, fingerprintFromKey, opts)
resolve(res) resolve(res)
} catch (e) { } catch (e) {
console.error(`Claim verification failed: ${user.userData.id}`, e)
reject(e) reject(e)
} }
}) })
@ -136,7 +135,6 @@ const verify = async (input, fingerprint, opts) => {
const res = await verify(uri, fingerprint, opts) const res = await verify(uri, fingerprint, opts)
resolve(res) resolve(res)
} catch (e) { } catch (e) {
console.error(`Claim verification failed: ${uri}`, e)
reject(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, '') const uri = input.replace(/^\s+|\s+$/g, '')
let verifErrors = []
if (!fingerprint) { if (!fingerprint) {
fingerprint = null fingerprint = null
@ -164,13 +167,14 @@ const verify = async (input, fingerprint, opts) => {
opts = mergeOptions(defaultOpts, opts ? opts : {}) opts = mergeOptions(defaultOpts, opts ? opts : {})
if (!validUrl.isUri(uri)) { if (!validUrl.isUri(uri)) {
throw new Error('Invalid URI') objResult.errors.push('invalid_uri')
reject(objResult)
} }
const spMatches = serviceproviders.match(uri, opts) const spMatches = serviceproviders.match(uri, opts)
if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) {
return spMatches resolve(spMatches)
} }
let claimVerificationDone = false, let claimVerificationDone = false,
@ -191,7 +195,7 @@ const verify = async (input, fingerprint, opts) => {
try { try {
proofData = await spData.customRequestHandler(spData, opts) proofData = await spData.customRequestHandler(spData, opts)
} catch (e) { } catch (e) {
verifErrors.push('custom_request_handler_failed') objResult.errors.push('custom_request_handler_failed')
} }
} else { } else {
switch (opts.proxyPolicy) { switch (opts.proxyPolicy) {
@ -232,7 +236,7 @@ const verify = async (input, fingerprint, opts) => {
} catch(er) {} } catch(er) {}
break; break;
default: default:
verifErrors.push('invalid_proxy_policy') objResult.errors.push('invalid_proxy_policy')
} }
} }
@ -243,7 +247,7 @@ const verify = async (input, fingerprint, opts) => {
claimVerificationDone = true claimVerificationDone = true
} }
} else { } else {
verifErrors.push('unsuccessful_claim_verification') objResult.errors.push('unsuccessful_claim_verification')
} }
iSp++ iSp++
@ -255,11 +259,21 @@ const verify = async (input, fingerprint, opts) => {
} }
} }
return { objResult.isVerified = claimVerificationResult.isVerified
isVerified: claimVerificationResult.isVerified, objResult.serviceproviderData = spData
errors: verifErrors, resolve(objResult)
serviceproviderData: spData, })
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 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.be.a('function')
expect(doipjs.claims.verify).to.have.length(3) 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( return expect(doipjs.claims.verify('noURI')).to.eventually.be.rejectedWith(
'Invalid URI' undefined
) )
return expect( return expect(
doipjs.claims.verify('domain.org') doipjs.claims.verify('domain.org')
).to.eventually.be.rejectedWith('Invalid URI') ).to.eventually.be.rejectedWith(undefined)
}) })
doipjs.serviceproviders.list.forEach((spName, i) => { doipjs.serviceproviders.list.forEach((spName, i) => {