Scratch space for learning atproto app development

more firehose improvements

dholms 5d629914 a4ee1607

Changed files
+129 -16
src
+67 -11
src/firehose/firehose.ts
···
import { Subscription } from "@atproto/xrpc-server";
import type { CID } from "multiformats/cid";
import {
+
type Account,
type Commit,
+
type Identity,
type RepoEvent,
+
isAccount,
isCommit,
+
isIdentity,
isValidRepoEvent,
} from "./lexicons";
···
getCursor?: () => Promise<number | undefined>;
setCursor?: (cursor: number) => Promise<void>;
subscriptionReconnectDelay?: number;
+
filterCollections?: string[];
+
excludeIdentity?: boolean;
+
excludeAccount?: boolean;
+
excludeCommit?: boolean;
};
export class Firehose {
···
try {
for await (const evt of this.sub) {
try {
-
const parsed = await parseEvent(evt);
-
for (const op of parsed) {
-
yield op;
+
if (isCommit(evt) && !this.opts.excludeCommit) {
+
const parsed = await parseCommit(evt);
+
for (const write of parsed) {
+
if (
+
!this.opts.filterCollections ||
+
this.opts.filterCollections.includes(write.uri.collection)
+
) {
+
yield write;
+
}
+
}
+
} else if (isAccount(evt) && !this.opts.excludeAccount) {
+
const parsed = parseAccount(evt);
+
if (parsed) {
+
yield parsed;
+
}
+
} else if (isIdentity(evt) && !this.opts.excludeIdentity) {
+
yield parseIdentity(evt);
}
} catch (err) {
console.error("repo subscription could not handle message", err);
···
}
}
-
export const parseEvent = async (evt: RepoEvent): Promise<Event[]> => {
-
if (!isCommit(evt)) return [];
-
return parseCommit(evt);
-
};
-
-
export const parseCommit = async (evt: Commit): Promise<Event[]> => {
+
export const parseCommit = async (evt: Commit): Promise<CommitEvt[]> => {
const car = await readCar(evt.blocks);
-
const evts: Event[] = [];
+
const evts: CommitEvt[] = [];
for (const op of evt.ops) {
const uri = new AtUri(`at://${evt.repo}/${op.path}`);
···
return evts;
};
-
type Event = Create | Update | Delete;
+
export const parseIdentity = (evt: Identity): IdentityEvt => {
+
return {
+
event: "identity",
+
did: evt.did,
+
handle: evt.handle,
+
};
+
};
+
+
export const parseAccount = (evt: Account): AccountEvt | undefined => {
+
if (evt.status && !isValidStatus(evt.status)) return;
+
return {
+
event: "account",
+
did: evt.did,
+
active: evt.active,
+
status: evt.status as AccountStatus,
+
};
+
};
+
+
const isValidStatus = (str: string): str is AccountStatus => {
+
return ["takendown", "suspended", "deleted", "deactivated"].includes(str);
+
};
+
+
type Event = CommitEvt | IdentityEvt | AccountEvt;
type CommitMeta = {
uri: AtUri;
···
collection: string;
rkey: string;
};
+
+
type CommitEvt = Create | Update | Delete;
type Create = CommitMeta & {
event: "create";
···
type Delete = CommitMeta & {
event: "delete";
};
+
+
type IdentityEvt = {
+
event: "identity";
+
did: string;
+
handle?: string;
+
};
+
+
type AccountEvt = {
+
event: "account";
+
did: string;
+
active: boolean;
+
status?: AccountStatus;
+
};
+
+
type AccountStatus = "takendown" | "suspended" | "deleted" | "deactivated";
+62 -5
src/firehose/lexicons.ts
···
}
export interface QueryParams {
-
/** The last known event to backfill from. */
+
/** The last known event seq number to backfill from. */
cursor?: number;
}
export type RepoEvent =
| Commit
+
| Identity
+
| Account
| Handle
| Migrate
| Tombstone
···
ctx: HandlerReqCtx<HA>
) => AsyncIterable<HandlerOutput>;
+
/** Represents an update of repository state. Note that empty commits are allowed, which include no repo data changes, but an update to rev and signature. */
export interface Commit {
+
/** The stream sequence number of this message. */
seq: number;
+
/** DEPRECATED -- unused */
rebase: boolean;
+
/** Indicates that this commit contained too many ops, or data size was too large. Consumers will need to make a separate request to get missing data. */
tooBig: boolean;
+
/** The repo this event comes from. */
repo: string;
+
/** Repo commit object CID. */
commit: CID;
+
/** DEPRECATED -- unused. WARNING -- nullable and optional; stick with optional to ensure golang interoperability. */
prev?: CID | null;
-
/** The rev of the emitted commit */
+
/** The rev of the emitted commit. Note that this information is also in the commit object included in blocks, unless this is a tooBig event. */
rev: string;
-
/** The rev of the last emitted commit from this repo */
+
/** The rev of the last emitted commit from this repo (if any). */
since: string | null;
-
/** CAR file containing relevant blocks */
+
/** CAR file containing relevant blocks, as a diff since the previous repo state. */
blocks: Uint8Array;
ops: RepoOp[];
blobs: CID[];
+
/** Timestamp of when this message was originally broadcast. */
time: string;
[k: string]: unknown;
}
···
);
}
+
/** Represents a change to an account's identity. Could be an updated handle, signing key, or pds hosting endpoint. Serves as a prod to all downstream services to refresh their identity cache. */
+
export interface Identity {
+
seq: number;
+
did: string;
+
time: string;
+
/** The current handle for the account, or 'handle.invalid' if validation fails. This field is optional, might have been validated or passed-through from an upstream source. Semantics and behaviors for PDS vs Relay may evolve in the future; see atproto specs for more details. */
+
handle?: string;
+
[k: string]: unknown;
+
}
+
+
export function isIdentity(v: unknown): v is Identity {
+
return (
+
isObj(v) &&
+
hasProp(v, "$type") &&
+
v.$type === "com.atproto.sync.subscribeRepos#identity"
+
);
+
}
+
+
/** Represents a change to an account's status on a host (eg, PDS or Relay). The semantics of this event are that the status is at the host which emitted the event, not necessarily that at the currently active PDS. Eg, a Relay takedown would emit a takedown with active=false, even if the PDS is still active. */
+
export interface Account {
+
seq: number;
+
did: string;
+
time: string;
+
/** Indicates that the account has a repository which can be fetched from the host that emitted this event. */
+
active: boolean;
+
/** If active=false, this optional field indicates a reason for why the account is not active. */
+
status?:
+
| "takendown"
+
| "suspended"
+
| "deleted"
+
| "deactivated"
+
| (string & {});
+
[k: string]: unknown;
+
}
+
+
export function isAccount(v: unknown): v is Account {
+
return (
+
isObj(v) &&
+
hasProp(v, "$type") &&
+
v.$type === "com.atproto.sync.subscribeRepos#account"
+
);
+
}
+
+
/** DEPRECATED -- Use #identity event instead */
export interface Handle {
seq: number;
did: string;
···
);
}
+
/** DEPRECATED -- Use #account event instead */
export interface Migrate {
seq: number;
did: string;
···
);
}
+
/** DEPRECATED -- Use #account event instead */
export interface Tombstone {
seq: number;
did: string;
···
);
}
-
/** A repo operation, ie a write of a single record. For creates and updates, cid is the record's CID as of this operation. For deletes, it's null. */
+
/** A repo operation, ie a mutation of a single record. */
export interface RepoOp {
action: "create" | "update" | "delete" | (string & {});
path: string;
+
/** For creates and updates, the new record CID. For deletions, null. */
cid: CID | null;
[k: string]: unknown;
}