diff --git a/packages/dids/src/resolver/resolver-cache-level.ts b/packages/dids/src/resolver/resolver-cache-level.ts index 17982abd9..063cf8114 100644 --- a/packages/dids/src/resolver/resolver-cache-level.ts +++ b/packages/dids/src/resolver/resolver-cache-level.ts @@ -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. @@ -53,7 +53,7 @@ type CacheWrapper = { * * This object contains the resolved DID document and associated metadata. */ - value: DidResolutionResult; + value: CachedDidResolutionResult; } /** @@ -95,7 +95,7 @@ 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 { + async get(did: string): Promise { try { const str = await this.cache.get(did); const cacheWrapper: CacheWrapper = JSON.parse(str); @@ -103,7 +103,7 @@ export class DidResolverCacheLevel implements DidResolverCache { 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; diff --git a/packages/dids/src/resolver/universal-resolver.ts b/packages/dids/src/resolver/universal-resolver.ts index 95b9c5e67..16db8d1f5 100644 --- a/packages/dids/src/resolver/universal-resolver.ts +++ b/packages/dids/src/resolver/universal-resolver.ts @@ -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)); } diff --git a/packages/dids/src/types/did-core.ts b/packages/dids/src/types/did-core.ts index b57c1466d..1e82db6c7 100644 --- a/packages/dids/src/types/did-core.ts +++ b/packages/dids/src/types/did-core.ts @@ -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; }; diff --git a/packages/dids/src/types/did-resolution.ts b/packages/dids/src/types/did-resolution.ts index 474a498b8..0894b10db 100644 --- a/packages/dids/src/types/did-resolution.ts +++ b/packages/dids/src/types/did-resolution.ts @@ -36,10 +36,17 @@ export interface DidResolver { resolve(didUrl: string, options?: DidResolutionOptions): Promise; } +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 {} +export interface DidResolverCache extends KeyValueStore {} /** * Represents the interface for dereferencing a DID URL to a specific resource within a DID diff --git a/packages/dids/tests/resolver/resolver-cache-level.spec.ts b/packages/dids/tests/resolver/resolver-cache-level.spec.ts index 348b9c22b..3dd29b183 100644 --- a/packages/dids/tests/resolver/resolver-cache-level.spec.ts +++ b/packages/dids/tests/resolver/resolver-cache-level.spec.ts @@ -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'; @@ -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 () => { @@ -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; }); }); diff --git a/packages/dids/tests/resolver/universal-resolver.spec.ts b/packages/dids/tests/resolver/universal-resolver.spec.ts index f47e0b11f..7dd7eb945 100644 --- a/packages/dids/tests/resolver/universal-resolver.spec.ts +++ b/packages/dids/tests/resolver/universal-resolver.spec.ts @@ -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' },