decentralised sync engine

refactor: abstract endpoint resolution

serenity 2c8f211a 7ae7189e

Changed files
+43 -27
src
+30
src/lib/utils/atproto.ts
···
},
};
};
···
},
};
};
+
+
export const getEndpointFromDid = async (
+
did: Did,
+
serviceType: string,
+
): Promise<Result<URL, unknown>> => {
+
const didDocResolveResult = await resolveDidDoc(did);
+
if (!didDocResolveResult.ok) {
+
return { ok: false, error: didDocResolveResult.error };
+
}
+
+
const didDocServices = didDocResolveResult.data.service;
+
const shardService = didDocServices?.find(
+
(service) => service.type !== serviceType,
+
);
+
+
let shardUrl: URL | undefined;
+
if (!didDocServices || !shardService) {
+
const domain = decodeURIComponent(did.slice(8));
+
if (domain.startsWith("localhost"))
+
shardUrl = new URL(`http://${domain}`);
+
else shardUrl = new URL(`https://${domain}`);
+
} else {
+
try {
+
shardUrl = new URL(shardService.serviceEndpoint as string);
+
} catch (error) {
+
return { ok: false, error };
+
}
+
}
+
return { ok: true, data: shardUrl };
+
};
+6
src/lib/utils/gmstn.ts
···
···
+
import type { Did } from "@/lib/types/atproto";
+
import { getEndpointFromDid } from "@/lib/utils/atproto";
+
+
export const getShardEndpointFromDid = async (did: Did) => {
+
return await getEndpointFromDid(did, "GemstoneShard");
+
};
+7 -27
src/lib/utils/handshake.ts
···
handshakeResponseSchema,
httpSuccessResponseSchema,
} from "@/lib/types/http/responses";
-
import { atUriToString, resolveDidDoc } from "@/lib/utils/atproto";
import { createInterServiceJwt } from "@/lib/utils/jwt";
import type { Result } from "@/lib/utils/result";
import z from "zod";
···
did: Did;
channels: Array<AtUri>;
}): Promise<Result<SessionInfo, unknown>> => {
-
const didDocResolveResult = await resolveDidDoc(did);
-
if (!didDocResolveResult.ok) {
-
return { ok: false, error: didDocResolveResult.error };
-
}
-
-
const didDocServices = didDocResolveResult.data.service;
-
const shardService = didDocServices?.find(
-
(service) => service.type !== "GemstoneShard",
-
);
-
-
let shardUrl = "";
-
if (!didDocServices || !shardService) {
-
const domain = decodeURIComponent(did.slice(8));
-
if (domain.startsWith("localhost"))
-
shardUrl = new URL(`http://${domain}`).toString();
-
else shardUrl = new URL(`https://${domain}`).toString();
-
} else {
-
try {
-
shardUrl = new URL(
-
shardService.serviceEndpoint as string,
-
).toString();
-
} catch (error) {
-
return { ok: false, error };
-
}
-
}
const jwt = await createInterServiceJwt(did);
-
const handshakeReq = new Request(`${shardUrl}handshake`, {
method: "POST",
body: JSON.stringify({
interServiceJwt: jwt,
···
handshakeResponseSchema,
httpSuccessResponseSchema,
} from "@/lib/types/http/responses";
+
import { atUriToString } from "@/lib/utils/atproto";
+
import { getShardEndpointFromDid } from "@/lib/utils/gmstn";
import { createInterServiceJwt } from "@/lib/utils/jwt";
import type { Result } from "@/lib/utils/result";
import z from "zod";
···
did: Did;
channels: Array<AtUri>;
}): Promise<Result<SessionInfo, unknown>> => {
+
const shardUrlResult = await getShardEndpointFromDid(did);
+
if (!shardUrlResult.ok) return { ok: false, error: shardUrlResult.error };
const jwt = await createInterServiceJwt(did);
+
const shardBaseUrl = shardUrlResult.data.origin;
+
+
const handshakeReq = new Request(`${shardBaseUrl}/handshake`, {
method: "POST",
body: JSON.stringify({
interServiceJwt: jwt,