decentralised message store

feat: import more code over

serenity d202e90d c64ca5ff

Changed files
+69 -9
src
+40 -1
src/index.ts
···
import { setupDbWithMigrations } from "@/db";
-
import { DB_URL, SERVER_PORT } from "@/lib/env";
+
import { DB_URL, OWNER_DID, SERVER_PORT, SERVICE_DID } from "@/lib/env";
+
import { setRegistrationState } from "@/lib/state";
+
import type { AtUri } from "@/lib/types/atproto";
+
import { getRecordFromAtUri } from "@/lib/utils/atproto";
import { newErrorResponse } from "@/lib/utils/http/responses";
+
import { connectToPrism } from "@/lib/utils/prism";
import {
+
attachShardRegistrationListener,
wrapHttpRegistrationCheck,
wrapWsRegistrationCheck,
} from "@/lib/utils/registration";
···
server.log.error(err);
process.exit(1);
});
+
+
let shardUrlOrigin = decodeURIComponent(
+
SERVICE_DID.startsWith("did:web:") ? SERVICE_DID.slice(8) : "",
+
);
+
if (shardUrlOrigin === "localhost")
+
shardUrlOrigin += `:${SERVER_PORT.toString()}`;
+
if (shardUrlOrigin === "") {
+
// TODO: resolve did:plc endpoint to get the origin of the shard endpoint described by the did:plc doc
+
// for now we just throw.
+
throw new Error(
+
"did:plc support not yet implemented. Provide a did:web for now. did:plc support will come in the future.",
+
);
+
}
+
+
const shardAtUri: Required<AtUri> = {
+
// @ts-expect-error alas, template literal weirdness continues uwu
+
authority: OWNER_DID,
+
collection: "systems.gmstn.development.shard",
+
rKey: shardUrlOrigin,
+
};
+
+
const shardRecord = await getRecordFromAtUri(shardAtUri);
+
+
if (shardRecord.ok) {
+
setRegistrationState(true);
+
}
+
+
const prismWebsocket = connectToPrism({
+
wantedCollections: ["systems.gmstn.development.*"],
+
});
+
+
// TODO: probably move this to an `attachListeners` hook that attaches the listeners we want.
+
// least tested. will probably have nuances we need to work on in the future
+
attachShardRegistrationListener(prismWebsocket);
};
main()
+24
src/lib/utils/prism.ts
···
+
// Prism is a jetstream/jetstream-compatible fork for receiving filtered events from the firehose
+
import { PRISM_URL } from "@/lib/env";
+
import WebSocket from "ws";
+
+
export const connectToPrism = (opts?: {
+
wantedCollections?: Array<string>;
+
wantedDids?: Array<string>;
+
cursor?: number;
+
}) => {
+
const endpoint = PRISM_URL;
+
if (opts) {
+
const { wantedCollections, wantedDids, cursor } = opts;
+
if (wantedCollections)
+
wantedCollections.forEach((collection) => {
+
endpoint.searchParams.append("wantedCollections", collection);
+
});
+
if (wantedDids)
+
wantedDids.forEach((did) => {
+
endpoint.searchParams.append("wantedDids", did);
+
});
+
if (cursor) endpoint.searchParams.append("cursor", cursor.toString());
+
}
+
return new WebSocket(endpoint);
+
};
+5 -8
src/lib/utils/registration.ts
···
if (!registrationState.registered) {
return newErrorResponse(503, {
message:
-
"Lattice has not been registered for use. Register it in the dashboard or make the record yourself using the bootstrapper if you're doing local development.",
+
"Shard has not been registered for use. Register it in the dashboard or make the record yourself using the bootstrapper if you're doing local development.",
});
}
···
const registrationState = getRegistrationState();
const wrappedFunction: WsRouteHandler = (socket, request) => {
if (!registrationState.registered) {
-
socket.close(
-
1013,
-
"Service unavailable: Lattice not yet registered",
-
);
+
socket.close(1013, "Service unavailable: Shard not yet registered");
return;
}
···
return wrappedFunction;
}
-
export const attachLatticeRegistrationListener = (socket: WebSocket) => {
+
export const attachShardRegistrationListener = (socket: WebSocket) => {
socket.on("message", (rawData: RawData) => {
const data = rawDataToString(rawData);
const jsonData: unknown = JSON.parse(data);
···
// TODO: replace empty string with call to resolve did doc and the endpoint and yadda yadda etc. etc. you get it.
// if you don't, then the tl;dr is you need to resolve the did:plc document to get the service endpoint describing this lattice and ensure
// that the domain/origin/whatever matches with the rkey (or record value if we decide to transition to that)
-
const latticeDomain = SERVICE_DID.startsWith("did:web:")
+
const shardDomain = SERVICE_DID.startsWith("did:web:")
? SERVICE_DID.slice(8)
: "";
-
if (rkey !== latticeDomain) return;
+
if (rkey !== shardDomain) return;
setRegistrationState(true);
});