···
-
import { OWNER_DID, SERVER_PORT, SERVICE_DID } from "@/lib/env";
import { setRegistrationState } from "@/lib/state";
import type { AtUri, Did } from "@/lib/types/atproto";
import type { SessionInfo } from "@/lib/types/handshake";
···
-
const latticeRecord = await getRecordFromAtUri({
// @ts-expect-error alas, template literal weirdness continues uwu
collection: "systems.gmstn.development.lattice",
-
if (latticeRecord.ok) setRegistrationState(true);
const prismWebsocket = connectToPrism({
wantedCollections: ["systems.gmstn.development.*"],
···
// TODO: probably move this to an `attachListeners` hook that attaches the listeners we want.
attachLatticeRegistrationListener(prismWebsocket);
-
const constellationBacklinksResult = await getConstellationBacklink({
-
nsid: "systems.gmstn.development.channel",
-
fieldName: "routeThrough.uri",
-
if (!constellationBacklinksResult.ok) {
-
"Something went wrong fetching constellation backlinks to do Shard handshakes",
-
const { records: channelBacklinks } = constellationBacklinksResult.data;
-
// TODO: For private lattices, do permission check on owner's PDS
-
// and filter out records from unauthorised pdses.
-
const channelRecordsPromises = channelBacklinks.map(
-
async ({ did, collection, rkey }) =>
-
await getRecordFromAtUri({
-
// @ts-expect-error seriously i gotta do something about the template literals not converting properly SIGH
-
const channelRecordResults = await Promise.all(channelRecordsPromises);
-
// mapping of shard -> list of channels (all AtUris)
-
const channelsByShard = new Map<AtUri, Array<AtUri>>();
-
channelRecordResults.forEach((result, idx) => {
-
if (!result.ok) return;
-
const { success, data: channelRecord } =
-
systemsGmstnDevelopmentChannelRecordSchema.safeParse(result.data);
-
const { storeAt } = channelRecord;
-
const storeAtAtUriResult = stringToAtUri(storeAt.uri);
-
if (!storeAtAtUriResult.ok) return;
-
const storeAtAtUri = storeAtAtUriResult.data;
-
// this is fine because Promise.all() preserves the order of the arrays
-
} = channelBacklinks[idx];
-
const existingMapValue = channelsByShard.get(storeAtAtUri);
-
const currentChannelUri: Required<AtUri> = {
-
// @ts-expect-error seriously i gotta do something about the template literals not converting properly SIGH
-
if (!existingMapValue) {
-
channelsByShard.set(storeAtAtUri, [currentChannelUri]);
-
const prevUris = existingMapValue;
-
channelsByShard.set(storeAtAtUri, [...prevUris, currentChannelUri]);
-
const channelSessions = new Map<AtUri, SessionInfo>();
-
const channelsByShardEntries = channelsByShard.entries();
-
for (const entry of channelsByShardEntries) {
-
const shardAtUri = entry[0];
-
let shardDid: Did | undefined;
-
// TODO: if the rkey of the shard URI is not a valid domain, then it must be a did:plc
-
// we need to find a better way to enforce this. we really should explore just resolving the
-
// record and then checking the record value for the actual domain instead.
-
// did resolution hard;;
-
isDomain(shardAtUri.rKey ?? "") ||
-
shardAtUri.rKey?.startsWith("localhost:")
-
// from the isDomain check, if we pass, we can conclude that
-
shardDid = `did:web:${encodeURIComponent(shardAtUri.rKey ?? "")}`;
-
shardDid = `did:plc:${encodeURIComponent(shardAtUri.rKey ?? "")}`;
-
const channelAtUris = entry[1];
-
const handshakeResult = await initiateHandshakeTo({
-
channels: channelAtUris,
-
if (!handshakeResult.ok) return;
-
const sessionInfo = handshakeResult.data;
-
console.log("Handshake to", shardAtUri.rKey, "complete!");
-
console.log("Session info:", sessionInfo);
-
channelSessions.set(shardAtUri, sessionInfo);
const server = await setupServer();
for (const [url, route] of Object.entries(routes)) {