Skip to content

Commit

Permalink
do not cache resolution requests with errors (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen authored Apr 25, 2024
1 parent 60ae8af commit 20d6466
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-toes-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web5/dids": patch
---

Do not cache results that contain a resolution error.
6 changes: 4 additions & 2 deletions packages/dids/src/resolver/universal-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ export class UniversalResolver implements DidResolver, DidUrlDereferencer {
return cachedResolutionResult;
} else {
const resolutionResult = await resolver.resolve(parsedDid.uri, options);

await this.cache.set(parsedDid.uri, resolutionResult);
if (!resolutionResult.didResolutionMetadata.error) {
// Cache the resolution result if it was successful.
await this.cache.set(parsedDid.uri, resolutionResult);
}

return resolutionResult;
}
Expand Down
54 changes: 54 additions & 0 deletions packages/dids/tests/resolver/universal-resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ describe('UniversalResolver', () => {
didResolver = new UniversalResolver({ didResolvers: didMethodApis });
});

afterEach(() => {
sinon.restore();
});

it('returns an invalidDid error if the DID cannot be parsed', async () => {
const didResolutionResult = await didResolver.resolve('unparseable:did');
expect(didResolutionResult).to.exist;
Expand All @@ -39,6 +43,56 @@ describe('UniversalResolver', () => {
expect(didResolutionResult.didResolutionMetadata).to.have.property('error', 'methodNotSupported');
});

it('should not attempt to cache a DID resolution result if the result is an error', async () => {
// Create a Sinon spy on the cache.set method
const cacheSetSpy = sinon.spy(didResolver['cache'], 'set');

// stub the underlying JWK Resolver to return an error
const resultWithError = {
didResolutionMetadata: {
error: 'anyError'
},
didDocument: {
id: 'did:jwk:123456789abcdefghi'
},
didDocumentMetadata: {}
};

const didMethodResolver = sinon.stub(DidJwk, 'resolve').resolves(resultWithError);

// Resolve a DID
const did = 'did:jwk:123456789abcdefghi';
await didResolver.resolve(did);

// expect that the cache.set method was not called
expect(cacheSetSpy.called).to.be.false;
expect(didMethodResolver.callCount).to.equal(1);
});

it('should set cache for a DID resolution result if the result is not an error', async () => {
// Create a Sinon spy on the cache.set method
const cacheSetSpy = sinon.spy(didResolver['cache'], 'set');

// stub the underlying JWK Resolver to not return an error
const result = {
didResolutionMetadata : {},
didDocument : {
id: 'did:jwk:123456789abcdefghi'
},
didDocumentMetadata: {}
};

const didMethodResolver = sinon.stub(DidJwk, 'resolve').resolves(result);

// Resolve a DID
const did = 'did:jwk:123456789abcdefghi';
await didResolver.resolve(did);

// expect that the cache.set was called once
expect(cacheSetSpy.callCount).to.equal(1);
expect(didMethodResolver.callCount).to.equal(1);
});

it('pass DID JWK resolve test vectors', async () => {
type TestVector = {
description: string;
Expand Down

0 comments on commit 20d6466

Please sign in to comment.