decentralised sync engine
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: abstract endpoint resolution

serenity 2c8f211a 7ae7189e

+43 -27
+30
src/lib/utils/atproto.ts
··· 184 184 }, 185 185 }; 186 186 }; 187 + 188 + export const getEndpointFromDid = async ( 189 + did: Did, 190 + serviceType: string, 191 + ): Promise<Result<URL, unknown>> => { 192 + const didDocResolveResult = await resolveDidDoc(did); 193 + if (!didDocResolveResult.ok) { 194 + return { ok: false, error: didDocResolveResult.error }; 195 + } 196 + 197 + const didDocServices = didDocResolveResult.data.service; 198 + const shardService = didDocServices?.find( 199 + (service) => service.type !== serviceType, 200 + ); 201 + 202 + let shardUrl: URL | undefined; 203 + if (!didDocServices || !shardService) { 204 + const domain = decodeURIComponent(did.slice(8)); 205 + if (domain.startsWith("localhost")) 206 + shardUrl = new URL(`http://${domain}`); 207 + else shardUrl = new URL(`https://${domain}`); 208 + } else { 209 + try { 210 + shardUrl = new URL(shardService.serviceEndpoint as string); 211 + } catch (error) { 212 + return { ok: false, error }; 213 + } 214 + } 215 + return { ok: true, data: shardUrl }; 216 + };
+6
src/lib/utils/gmstn.ts
··· 1 + import type { Did } from "@/lib/types/atproto"; 2 + import { getEndpointFromDid } from "@/lib/utils/atproto"; 3 + 4 + export const getShardEndpointFromDid = async (did: Did) => { 5 + return await getEndpointFromDid(did, "GemstoneShard"); 6 + };
+7 -27
src/lib/utils/handshake.ts
··· 4 4 handshakeResponseSchema, 5 5 httpSuccessResponseSchema, 6 6 } from "@/lib/types/http/responses"; 7 - import { atUriToString, resolveDidDoc } from "@/lib/utils/atproto"; 7 + import { atUriToString } from "@/lib/utils/atproto"; 8 + import { getShardEndpointFromDid } from "@/lib/utils/gmstn"; 8 9 import { createInterServiceJwt } from "@/lib/utils/jwt"; 9 10 import type { Result } from "@/lib/utils/result"; 10 11 import z from "zod"; ··· 16 17 did: Did; 17 18 channels: Array<AtUri>; 18 19 }): Promise<Result<SessionInfo, unknown>> => { 19 - const didDocResolveResult = await resolveDidDoc(did); 20 - if (!didDocResolveResult.ok) { 21 - return { ok: false, error: didDocResolveResult.error }; 22 - } 23 - 24 - const didDocServices = didDocResolveResult.data.service; 25 - const shardService = didDocServices?.find( 26 - (service) => service.type !== "GemstoneShard", 27 - ); 28 - 29 - let shardUrl = ""; 30 - if (!didDocServices || !shardService) { 31 - const domain = decodeURIComponent(did.slice(8)); 32 - if (domain.startsWith("localhost")) 33 - shardUrl = new URL(`http://${domain}`).toString(); 34 - else shardUrl = new URL(`https://${domain}`).toString(); 35 - } else { 36 - try { 37 - shardUrl = new URL( 38 - shardService.serviceEndpoint as string, 39 - ).toString(); 40 - } catch (error) { 41 - return { ok: false, error }; 42 - } 43 - } 20 + const shardUrlResult = await getShardEndpointFromDid(did); 21 + if (!shardUrlResult.ok) return { ok: false, error: shardUrlResult.error }; 44 22 45 23 const jwt = await createInterServiceJwt(did); 46 24 47 - const handshakeReq = new Request(`${shardUrl}handshake`, { 25 + const shardBaseUrl = shardUrlResult.data.origin; 26 + 27 + const handshakeReq = new Request(`${shardBaseUrl}/handshake`, { 48 28 method: "POST", 49 29 body: JSON.stringify({ 50 30 interServiceJwt: jwt,