Skip to content

Commit

Permalink
Cache https did resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-sherman committed Aug 31, 2024
1 parent e4db927 commit e99b0a9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
48 changes: 32 additions & 16 deletions packages/frontpage/lib/data/atproto/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,34 @@ const getAtprotoDidFromDns = unstable_cache(
});
// records is [ [ "did=xxx" ] ]
// We're assuming that you only have one txt record or that the first one is the one we want
return (records[0]?.join().split("did=")[1] ?? null) as DID | null;
const val = records[0]?.join().split("did=")[1];
return val ? parseDid(val) : null;
},
["dns", "resolveTxt"],
["did", "dns"],
{
revalidate: 60 * 60 * 24, // 24 hours
},
);

const getAtprotoFromHttps = unstable_cache(
async (handle: string) => {
let res;
const timeoutSignal = AbortSignal.timeout(1500);
try {
res = await fetch(`https://${handle}/.well-known/atproto-did`, {
signal: timeoutSignal,
});
} catch (_e) {
// We're caching failures here, we should at some point invalidate the cache by listening to handle changes on the network
return null;
}

if (!res.ok) {
return null;
}
return parseDid(await res.text());
},
["did", "https"],
{
revalidate: 60 * 60 * 24, // 24 hours
},
Expand All @@ -40,26 +65,17 @@ export const getVerifiedDid = cache(async (handle: string) => {
getAtprotoDidFromDns(handle).catch((_) => {
return null;
}),
fetch(`https://${handle}/.well-known/atproto-did`, {
next: {
revalidate: 60 * 60 * 24, // 24 hours
},
})
.then((res) => {
if (!res.ok) {
return null;
}
return res.text();
})
.catch((_) => {
return null;
}),
getAtprotoFromHttps(handle).catch((_) => {
return null;
}),
]);

if (dnsDid && httpDid && dnsDid !== httpDid) {
return null;
}

// TODO: Check did doc includes the handle

return dnsDid ?? (httpDid ? parseDid(httpDid) : null);
});

Expand Down
1 change: 0 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"outputs": [".next/**", "!.next/cache/**"],
"env": [
"DRAINPIPE_CONSUMER_SECRET",
"POSTGRES_URL",
"DISCORD_WEBHOOK_URL",
"TURSO_CONNECTION_URL",
"TURSO_AUTH_TOKEN"
Expand Down

0 comments on commit e99b0a9

Please sign in to comment.