···
1
-
import { OWNER_DID, SERVER_PORT, SERVICE_DID } from "@/lib/env";
1
+
import { __DEV__, OWNER_DID, SERVER_PORT, SERVICE_DID } from "@/lib/env";
2
+
import { performHandshakes } from "@/lib/setup";
import { setRegistrationState } from "@/lib/state";
import type { AtUri, Did } from "@/lib/types/atproto";
import type { SessionInfo } from "@/lib/types/handshake";
···
34
-
const latticeRecord = await getRecordFromAtUri({
35
+
const latticeAtUri: Required<AtUri> = {
// @ts-expect-error alas, template literal weirdness continues uwu
collection: "systems.gmstn.development.lattice",
41
-
if (latticeRecord.ok) setRegistrationState(true);
42
+
const latticeRecord = await getRecordFromAtUri(latticeAtUri);
44
+
if (latticeRecord.ok) {
45
+
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);
50
-
const constellationBacklinksResult = await getConstellationBacklink({
51
-
subject: SERVICE_DID,
53
-
nsid: "systems.gmstn.development.channel",
54
-
fieldName: "routeThrough.uri",
58
-
if (!constellationBacklinksResult.ok) {
60
-
"Something went wrong fetching constellation backlinks to do Shard handshakes",
64
-
const { records: channelBacklinks } = constellationBacklinksResult.data;
66
-
// TODO: For private lattices, do permission check on owner's PDS
67
-
// and filter out records from unauthorised pdses.
69
-
const channelRecordsPromises = channelBacklinks.map(
70
-
async ({ did, collection, rkey }) =>
71
-
await getRecordFromAtUri({
72
-
// @ts-expect-error seriously i gotta do something about the template literals not converting properly SIGH
79
-
const channelRecordResults = await Promise.all(channelRecordsPromises);
81
-
// mapping of shard -> list of channels (all AtUris)
82
-
const channelsByShard = new Map<AtUri, Array<AtUri>>();
84
-
channelRecordResults.forEach((result, idx) => {
85
-
if (!result.ok) return;
86
-
const { success, data: channelRecord } =
87
-
systemsGmstnDevelopmentChannelRecordSchema.safeParse(result.data);
88
-
if (!success) return;
89
-
const { storeAt } = channelRecord;
91
-
const storeAtAtUriResult = stringToAtUri(storeAt.uri);
92
-
if (!storeAtAtUriResult.ok) return;
93
-
const storeAtAtUri = storeAtAtUriResult.data;
95
-
// this is fine because Promise.all() preserves the order of the arrays
100
-
} = channelBacklinks[idx];
102
-
const existingMapValue = channelsByShard.get(storeAtAtUri);
104
-
const currentChannelUri: Required<AtUri> = {
105
-
// @ts-expect-error seriously i gotta do something about the template literals not converting properly SIGH
111
-
if (!existingMapValue) {
112
-
channelsByShard.set(storeAtAtUri, [currentChannelUri]);
114
-
const prevUris = existingMapValue;
115
-
channelsByShard.set(storeAtAtUri, [...prevUris, currentChannelUri]);
119
-
const channelSessions = new Map<AtUri, SessionInfo>();
121
-
const channelsByShardEntries = channelsByShard.entries();
123
-
for (const entry of channelsByShardEntries) {
124
-
const shardAtUri = entry[0];
126
-
let shardDid: Did | undefined;
127
-
// TODO: if the rkey of the shard URI is not a valid domain, then it must be a did:plc
128
-
// we need to find a better way to enforce this. we really should explore just resolving the
129
-
// record and then checking the record value for the actual domain instead.
130
-
// did resolution hard;;
132
-
isDomain(shardAtUri.rKey ?? "") ||
133
-
shardAtUri.rKey?.startsWith("localhost:")
135
-
// from the isDomain check, if we pass, we can conclude that
136
-
shardDid = `did:web:${encodeURIComponent(shardAtUri.rKey ?? "")}`;
138
-
shardDid = `did:plc:${encodeURIComponent(shardAtUri.rKey ?? "")}`;
141
-
const channelAtUris = entry[1];
143
-
const handshakeResult = await initiateHandshakeTo({
145
-
channels: channelAtUris,
147
-
if (!handshakeResult.ok) return;
148
-
const sessionInfo = handshakeResult.data;
149
-
console.log("Handshake to", shardAtUri.rKey, "complete!");
150
-
console.log("Session info:", sessionInfo);
151
-
channelSessions.set(shardAtUri, sessionInfo);
55
+
await performHandshakes(latticeAtUri);
const server = await setupServer();
for (const [url, route] of Object.entries(routes)) {