Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

view local pds from atproto-browser #176

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions packages/atproto-browser/app/at/[identifier]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ async function DidHistory({ identifier }: { identifier: string }) {
throw new Error(identity.error);
}
const did = identity.didDocument.id;

const response = await fetch(`https://plc.directory/${did}/log/audit`);
const url = process.env.PLC_DIRECTORY_URL ?? "https://plc.directory";
const response = await fetch(`${url}/${did}/log/audit`);
if (!response.ok) {
throw new Error(`Failed to fetch history: ${response.statusText}`);
}
Expand Down Expand Up @@ -114,16 +114,16 @@ async function DidHistory({ identifier }: { identifier: string }) {
type:
service.type !== previousService.type
? {
from: previousService.type,
to: service.type,
}
from: previousService.type,
to: service.type,
}
: null,
endpoint:
service.endpoint !== previousService.endpoint
? {
from: previousService.endpoint,
to: service.endpoint,
}
from: previousService.endpoint,
to: service.endpoint,
}
: null,
};
})
Expand Down Expand Up @@ -172,7 +172,7 @@ async function DidHistory({ identifier }: { identifier: string }) {
</p>
<ul>
{alsoKnownAsAdded.length === 1 &&
alsoKnownAsRemoved.length === 1 ? (
alsoKnownAsRemoved.length === 1 ? (
<li>
Alias changed from{" "}
<Link href={getAtUriPath(new AtUri(alsoKnownAsRemoved[0]!))}>
Expand Down
30 changes: 29 additions & 1 deletion packages/atproto-browser/lib/atproto-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { cache } from "react";
import { unstable_cache as nextCache } from "next/cache";
import { isValidHandle } from "@atproto/syntax";
import { isDid } from "@atproto/did";
import { z } from "zod";

function timeoutWith<T>(
timeout: number,
Expand All @@ -23,11 +24,38 @@ function timeoutWith<T>(
]);
}

const PlcDocument = z.object({
id: z.string(),
alsoKnownAs: z.array(z.string()),
service: z.array(
z.object({
id: z.string(),
type: z.string(),
serviceEndpoint: z.string(),
}),
),
});

export const getDidDoc = cache(async (did: string) => {
const url = process.env.PLC_DIRECTORY_URL ?? "https://plc.directory";
const response = await fetch(`${url}/${did}`, {
next: {
// TODO: Also revalidate this when we receive an identity change event
// That would allow us to extend the revalidation time to 1 day
revalidate: 60 * 60, // 1 hour
},
});

return PlcDocument.parse(await response.json());
});

const didResolver = new DidResolver({});
const resolveDid = cache(
nextCache(
cache((did: string) =>
timeoutWith(1000, didResolver.resolve(did), "DID timeout"),
// this uses atproto lib, lets use our own func
// timeoutWith(1000, didResolver.resolve(did), "DID timeout"),
timeoutWith(1000, getDidDoc(did), "DID timeout"),
),
["did-doc"],
{
Expand Down