generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fea0535
commit 4fabf58
Showing
9 changed files
with
112 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { DidResolutionResult, DidResolverCache, DidResolverCacheLevel, DidResolverCacheLevelParams } from "@web5/dids"; | ||
import { Web5PlatformAgent } from "./types/agent.js"; | ||
|
||
|
||
export class AgentDidResolverCache extends DidResolverCacheLevel implements DidResolverCache { | ||
|
||
private _agent?: Web5PlatformAgent; | ||
|
||
private _resolving: Record<string, boolean> = {}; | ||
|
||
constructor({ agent, db, location, ttl }: DidResolverCacheLevelParams & { agent?: Web5PlatformAgent }) { | ||
super ({ db, location, ttl }); | ||
this._agent = agent; | ||
} | ||
|
||
get agent() { | ||
if (!this._agent) { | ||
throw new Error("Agent not initialized"); | ||
} | ||
return this._agent; | ||
} | ||
|
||
set agent(agent: Web5PlatformAgent) { | ||
this._agent = agent; | ||
} | ||
|
||
async get(did: string): Promise<DidResolutionResult | void> { | ||
try { | ||
const str = await this.cache.get(did); | ||
const cachedResult = JSON.parse(str); | ||
if (!this._resolving[did] && Date.now() >= cachedResult.ttlMillis) { | ||
this._resolving[did] = true; | ||
const list = await this.agent.identity.list(); | ||
if (this.agent.agentDid.uri === did || list.find(identity => identity.did.uri === did)) { | ||
this.agent.did.resolve(did).then(result => { | ||
if (!result.didResolutionMetadata.error) { | ||
this.set(did, result); | ||
} | ||
}).finally(() => delete this._resolving[did]) | ||
} | ||
else { | ||
delete this._resolving[did]; | ||
this.cache.nextTick(() => this.cache.del(did)); | ||
return; | ||
} | ||
} | ||
return cachedResult.value; | ||
} catch(error: any) { | ||
if (error.notFound) { | ||
return; | ||
} | ||
throw error; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.