Skip to content

Commit

Permalink
remove the isStaleInCache property from the DidResolutionMetadata
Browse files Browse the repository at this point in the history
… as it is not specified in the spec
  • Loading branch information
LiranCohen committed Apr 24, 2024
1 parent fb921ec commit 15ebb58
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
8 changes: 4 additions & 4 deletions packages/dids/src/resolver/resolver-cache-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ms from 'ms';
import { Level } from 'level';

import type { DidResolutionResult } from '../types/did-core.js';
import type { DidResolverCache } from '../types/did-resolution.js';
import type { CachedDidResolutionResult, DidResolverCache } from '../types/did-resolution.js';

/**
* Configuration parameters for creating a LevelDB-based cache for DID resolution results.
Expand Down Expand Up @@ -53,7 +53,7 @@ type CacheWrapper = {
*
* This object contains the resolved DID document and associated metadata.
*/
value: DidResolutionResult;
value: CachedDidResolutionResult;
}

/**
Expand Down Expand Up @@ -95,15 +95,15 @@ export class DidResolverCacheLevel implements DidResolverCache {
* @param did - The DID string used as the key for retrieving the cached result.
* @returns The cached DID resolution result or undefined if not found or expired.
*/
async get(did: string): Promise<DidResolutionResult | void> {
async get(did: string): Promise<CachedDidResolutionResult | void> {
try {
const str = await this.cache.get(did);
const cacheWrapper: CacheWrapper = JSON.parse(str);

if (Date.now() >= cacheWrapper.ttlMillis) {
// if the cache entry has expired, mark it as stale so that the caller can determine whether to refresh it
// we do this to avoid clearing an existing cache entry that may not able to be resolved temporarily
cacheWrapper.value.didResolutionMetadata.isStaleInCache = true;
cacheWrapper.value.isStaleInCache = true;
}
return cacheWrapper.value;

Expand Down
2 changes: 1 addition & 1 deletion packages/dids/src/resolver/universal-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class UniversalResolver implements DidResolver, DidUrlDereferencer {

const cachedResolutionResult = await this.cache.get(parsedDid.uri);
if (cachedResolutionResult) {
if (cachedResolutionResult?.didResolutionMetadata.isStaleInCache === true) {
if (cachedResolutionResult?.isStaleInCache === true) {
// if the cache is stale, we would like to attempt refreshing it after returning the existing stale result
isomorphicNextTick(() => this.resolveAndCache(parsedDid.uri, resolver, options));
}
Expand Down
5 changes: 0 additions & 5 deletions packages/dids/src/types/did-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,6 @@ export type DidResolutionMetadata = {
*/
error?: string;

/**
* Signals whether or not the resolved DID document is stale in the cache.
*/
isStaleInCache?: boolean;

// Additional output metadata generated during DID Resolution.
[key: string]: any;
};
Expand Down
9 changes: 8 additions & 1 deletion packages/dids/src/types/did-resolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ export interface DidResolver {
resolve(didUrl: string, options?: DidResolutionOptions): Promise<DidResolutionResult>;
}

export type CachedDidResolutionResult = DidResolutionResult & {
/**
* Signals whether or not the resolved DID document is stale in the cache.
*/
isStaleInCache?: boolean;
}

/**
* Interface for cache implementations used by to store resolved DID documents.
*/
export interface DidResolverCache extends KeyValueStore<string, DidResolutionResult | void> {}
export interface DidResolverCache extends KeyValueStore<string, CachedDidResolutionResult | void> {}

/**
* Represents the interface for dereferencing a DID URL to a specific resource within a DID
Expand Down
6 changes: 3 additions & 3 deletions packages/dids/tests/resolver/resolver-cache-level.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import { Level } from 'level';

import type { DidResolutionResult } from '../../src/types/did-core.js';
import type { DidResolver, DidResolverCache } from '../../src/types/did-resolution.js';
import type { CachedDidResolutionResult, DidResolver, DidResolverCache } from '../../src/types/did-resolution.js';

import { DidJwk } from '../../src/methods/did-jwk.js';
import { UniversalResolver } from '../../src/resolver/universal-resolver.js';
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('DidResolverCacheLevel', () => {
// We expect the cache entry to exist be marked as stale.
valueInCache = await cache.get(testDid);
expect(valueInCache).to.not.be.undefined;
expect((valueInCache as DidResolutionResult).didResolutionMetadata.isStaleInCache).to.be.true;
expect((valueInCache as CachedDidResolutionResult).isStaleInCache).to.be.true;
});

it('uses a custom TTL, when specified', async () => {
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('DidResolverCacheLevel', () => {
// We expect the cache entry to exist be marked as stale.
valueInCache = await cache.get(testDid);
expect(valueInCache).to.not.be.undefined;
expect((valueInCache as DidResolutionResult).didResolutionMetadata.isStaleInCache).to.be.true;
expect((valueInCache as CachedDidResolutionResult).isStaleInCache).to.be.true;
});
});

Expand Down
5 changes: 2 additions & 3 deletions packages/dids/tests/resolver/universal-resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ describe('UniversalResolver', () => {
const resolveAndCacheSpy = sinon.spy(didResolver as any, 'resolveAndCache');

const staleResults = {
didResolutionMetadata: {
isStaleInCache: true
},
isStaleInCache: true,
didResolutionMetadata: {},
didDocument: {
id: 'did:jwk:123456789abcdefghi'
},
Expand Down

0 comments on commit 15ebb58

Please sign in to comment.