···11import type pino from "pino";
22import type { Database } from "#/db";
33-import type { Firehose } from "#/firehose";
33+import type { Ingester } from "#/firehose/ingester";
4455export type AppContext = {
66 db: Database;
77- firehose: Firehose;
77+ ingester: Ingester;
88 logger: pino.Logger;
99};
-62
src/firehose.ts
···11-import { cborToLexRecord, readCar } from "@atproto/repo";
22-import { Subscription } from "@atproto/xrpc-server";
33-import type { Database } from "#/db";
44-55-export class Firehose {
66- public sub: Subscription<unknown>;
77-88- constructor(public service: string, public db: Database) {
99- this.sub = new Subscription({
1010- service: service,
1111- method: "com.atproto.sync.subscribeRepos",
1212- getParams: () => ({}),
1313- validate: (value: unknown) => value,
1414- });
1515- }
1616-1717- async handleEvent(evt: any): Promise<void> {
1818- if (evt.$type !== "com.atproto.sync.subscribeRepos#commit") {
1919- return;
2020- }
2121-2222- const car = await readCar(evt.blocks);
2323-2424- for (const op of evt.ops) {
2525- if (op.action !== "create") continue;
2626- const uri = `at://${evt.repo}/${op.path}`;
2727- const [collection] = op.path.split("/");
2828- if (collection !== "app.bsky.feed.post") continue;
2929-3030- if (!op.cid) continue;
3131- const recordBytes = car.blocks.get(op.cid);
3232- if (!recordBytes) continue;
3333- const record = cborToLexRecord(recordBytes);
3434- await this.db
3535- .insertInto("post")
3636- .values({
3737- uri,
3838- text: record.text as string,
3939- indexedAt: new Date().toISOString(),
4040- })
4141- .execute();
4242- }
4343- }
4444-4545- async run(subscriptionReconnectDelay: number) {
4646- try {
4747- for await (const evt of this.sub) {
4848- try {
4949- await this.handleEvent(evt);
5050- } catch (err) {
5151- console.error("repo subscription could not handle message", err);
5252- }
5353- }
5454- } catch (err) {
5555- console.error("repo subscription errored", err);
5656- setTimeout(
5757- () => this.run(subscriptionReconnectDelay),
5858- subscriptionReconnectDelay
5959- );
6060- }
6161- }
6262-}