-
Notifications
You must be signed in to change notification settings - Fork 10
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
7fccf20
commit 4965900
Showing
4 changed files
with
101 additions
and
91 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,73 @@ | ||
import "server-only"; | ||
import { DidDocument, DidResolver, HandleResolver } from "@atproto/identity"; | ||
import { cache } from "react"; | ||
import { unstable_cache as nextCache } from "next/cache"; | ||
import { isValidHandle } from "@atproto/syntax"; | ||
import { isDid } from "@atproto/did"; | ||
|
||
function timeoutWith<T>( | ||
timeout: number, | ||
promise: Promise<T>, | ||
errorMessage: string, | ||
): Promise<T> { | ||
return Promise.race([ | ||
promise, | ||
new Promise<never>((_res, reject) => | ||
setTimeout(() => reject(new Error(errorMessage)), timeout), | ||
), | ||
]); | ||
} | ||
|
||
const didResolver = new DidResolver({}); | ||
const resolveDid = cache( | ||
nextCache( | ||
cache((did: string) => | ||
timeoutWith(1000, didResolver.resolve(did), "DID timeout"), | ||
), | ||
["did-doc"], | ||
{ | ||
revalidate: 10, | ||
}, | ||
), | ||
); | ||
const handleResolver = new HandleResolver({}); | ||
const resolveHandle = cache( | ||
nextCache( | ||
cache((handle: string) => | ||
timeoutWith(3000, handleResolver.resolve(handle), "Handle timeout"), | ||
), | ||
["handle-from-did"], | ||
{ | ||
revalidate: 10, | ||
}, | ||
), | ||
); | ||
|
||
export async function resolveIdentity( | ||
didOrHandle: string, | ||
): Promise< | ||
{ success: true; identity: DidDocument } | { success: false; error: string } | ||
> { | ||
let didStr; | ||
if (isValidHandle(didOrHandle)) { | ||
didStr = await resolveHandle(didOrHandle).catch(() => undefined); | ||
if (!didStr) { | ||
return { | ||
success: false, | ||
error: `Could not resolve did from handle: ${didOrHandle}`, | ||
}; | ||
} | ||
} else { | ||
if (!isDid(didOrHandle)) { | ||
return { success: false, error: `Invalid DID: ${didOrHandle}` }; | ||
} | ||
didStr = didOrHandle; | ||
} | ||
|
||
const didDocument = await resolveDid(didStr); | ||
if (!didDocument) { | ||
return { success: false, error: `Could not resolve DID: ${didStr}` }; | ||
} | ||
|
||
return { success: true, identity: didDocument }; | ||
} |
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.