-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
349 lines (315 loc) · 9.93 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { readCar as createCarIterator } from "@atcute/car";
import { decode, decodeFirst, fromBytes, toCIDLink } from "@atcute/cbor";
import type { At, ComAtprotoSyncSubscribeRepos } from "@atcute/client/lexicons";
import { EventEmitter } from "node:events";
import * as WS from "ws";
/**
* Options for the Firehose class.
*/
export interface FirehoseOptions {
/**
* The Relay to connect to.
*/
relay?: string;
/**
* The cursor to listen from. If not provided, the firehose will start from the latest event.
*/
cursor?: string;
/**
* Whether to automatically reconnect when no new messages are received for a period of time.
* This will not reconnect if the connection was closed intentionally.
* To do that, listen for the `"close"` event and call `start()` again.
* @default true
*/
autoReconnect?: boolean;
}
export class Firehose extends EventEmitter {
/** The relay to connect to. */
public relay: string;
/** WebSocket connection to the relay. */
public ws?: WS.WebSocket;
/** The current cursor. */
public cursor = "";
private autoReconnect: boolean;
private reconnectTimeout: NodeJS.Timeout | undefined;
/**
* Creates a new Firehose instance.
* @param options Optional configuration.
*/
constructor(options: FirehoseOptions = {}) {
super();
this.relay = options.relay ?? "wss://bsky.network";
this.cursor = options.cursor ?? "";
this.autoReconnect = options.autoReconnect ?? true;
}
/**
* Opens a WebSocket connection to the relay.
*/
start() {
const cursorQueryParameter = this.cursor ? `?cursor=${this.cursor}` : "";
this.ws = new WS.WebSocket(
`${this.relay}/xrpc/com.atproto.sync.subscribeRepos${cursorQueryParameter}`,
);
this.ws.on("open", () => {
this.emit("open");
});
this.ws.on("message", (data) => {
try {
const message = this.parseMessage(data);
if ("seq" in message && message.seq && !isNaN(message.seq)) {
this.cursor = `${message.seq}`;
}
switch (message.$type) {
case "com.atproto.sync.subscribeRepos#handle":
this.emit("handle", message);
break;
case "com.atproto.sync.subscribeRepos#tombstone":
this.emit("tombstone", message);
break;
case "com.atproto.sync.subscribeRepos#migrate":
this.emit("migrate", message);
break;
case "com.atproto.sync.subscribeRepos#identity":
this.emit("identity", message);
break;
case "com.atproto.sync.subscribeRepos#info":
this.emit("info", message);
break;
case "com.atproto.sync.subscribeRepos#commit":
this.emit("commit", message);
break;
default:
this.emit("unknown", message);
break;
}
} catch (error) {
this.emit("error", { cursor: this.cursor, error });
} finally {
if (this.autoReconnect) this.preventReconnect();
}
});
this.ws.on("close", () => {
this.emit("close", this.cursor);
});
this.ws.on("error", (error) => {
this.emit("websocketError", { cursor: this.cursor, error });
});
}
/**
* Closes the WebSocket connection.
*/
close() {
this.ws?.close();
}
/** Emitted when the connection is opened. */
override on(event: "open", listener: () => void): this;
/** Emitted when the connection is closed. */
override on(event: "close", listener: (cursor: string) => void): this;
/**
* Emitted when the websocket reconnects due to not receiving any messages for a period of time.
* This will only be emitted if the `autoReconnect` option is `true`.
*/
override on(event: "reconnect", listener: () => void): this;
/** Emitted when an error occurs while handling a message. */
override on(
event: "error",
listener: ({ cursor, error }: { cursor: string; error: Error }) => void,
): this;
/** Emitted when an error occurs within the websocket. */
override on(
event: "websocketError",
listener: ({ cursor, error }: { cursor: string; error: unknown }) => void,
): this;
/** Emitted when an unknown message is received. */
override on(event: "unknown", listener: (message: unknown) => void): this;
/**
* Represents an update of an account's handle, or transition to/from invalid state.
* @deprecated Use on("identity") instead.
*/
override on(
event: "handle",
listener: (
message: ComAtprotoSyncSubscribeRepos.Handle & {
$type: "com.atproto.sync.subscribeRepos#handle";
},
) => void,
): this;
/**
* Represents an account moving from one PDS instance to another.
* @deprecated Use on("account") instead.
*/
override on(
event: "migrate",
listener: (
message: ComAtprotoSyncSubscribeRepos.Migrate & {
$type: "com.atproto.sync.subscribeRepos#migrate";
},
) => void,
): this;
/**
* Indicates that an account has been deleted.
* @deprecated Use on("account") instead.
*/
override on(
event: "tombstone",
listener: (
message: ComAtprotoSyncSubscribeRepos.Tombstone & {
$type: "com.atproto.sync.subscribeRepos#tombstone";
},
) => void,
): this;
/**
* Represents a change to an account's identity.
* Could be an updated handle, signing key, or pds hosting endpoint.
*/
override on(
event: "identity",
listener: (
message: ComAtprotoSyncSubscribeRepos.Identity & {
$type: "com.atproto.sync.subscribeRepos#identity";
},
) => void,
): this;
/** Represents a commit to a user's repository. */
override on(event: "commit", listener: (message: ParsedCommit) => void): this;
/** An informational message from the relay. */
override on(
event: "info",
listener: (
message: ComAtprotoSyncSubscribeRepos.Info & {
$type: "com.atproto.sync.subscribeRepos#info";
},
) => void,
): this;
override on(event: string, listener: (...args: any[]) => void): this {
super.on(event, listener);
return this;
}
private parseMessage(data: WS.RawData): ParsedCommit | { $type: string; seq?: number } {
let buffer: Buffer;
if (data instanceof Buffer) {
buffer = data;
} else if (data instanceof ArrayBuffer) {
buffer = Buffer.from(data);
} else if (Array.isArray(data)) {
buffer = Buffer.concat(data);
} else {
throw new Error("Unknown message contents: " + data);
}
const [header, remainder] = decodeFirst(buffer);
const [body, remainder2] = decodeFirst(remainder);
if (remainder2.length > 0) {
throw new Error("Excess bytes in message");
}
const { t, op } = parseHeader(header);
if (op === -1) throw new Error(`Error: ${body.message}\nError code: ${body.error}`);
if (t === "#commit") {
const commit = body as ComAtprotoSyncSubscribeRepos.Commit;
// A commit can contain no changes
if (!("blocks" in commit) || !(commit.blocks.$bytes.length)) {
return {
$type: "com.atproto.sync.subscribeRepos#commit",
...commit,
blocks: new Uint8Array(),
ops: [],
} satisfies ParsedCommit;
}
const blocks = fromBytes(commit.blocks);
const car = readCar(blocks);
const ops: Array<RepoOp> = commit.ops.map((op) => {
const action: "create" | "update" | "delete" = op.action as any;
if (action === "create" || action === "update") {
if (!op.cid) return;
const record = car.get(op.cid.$link);
if (!record) return;
return { action, path: op.path, cid: op.cid.$link, record };
} else if (action === "delete") {
return { action, path: op.path };
} else {
throw new Error(`Unknown action: ${action}`);
}
}).filter((op): op is Exclude<typeof op, undefined> => !!op);
return {
$type: "com.atproto.sync.subscribeRepos#commit",
...commit,
blocks,
ops,
} satisfies ParsedCommit;
}
return { $type: `com.atproto.sync.subscribeRepos${t}`, ...body };
}
private preventReconnect() {
if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = setTimeout(() => {
this.reconnect();
}, 5_000);
}
private reconnect() {
this.ws?.removeAllListeners();
this.ws?.terminate();
this.start();
this.emit("reconnect");
}
}
/**
* Represents a `create` or `update` repository operation.
*/
export interface CreateOrUpdateOp {
action: "create" | "update";
/** The record's path in the repository. */
path: string;
/** The record's CID. */
cid: string;
/** The record itself. */
record: {};
}
/**
* Represents a `delete` repository operation.
*/
export interface DeleteOp {
action: "delete";
/** The record's path in the repository. */
path: string;
}
/** A repository operation. */
export type RepoOp = CreateOrUpdateOp | DeleteOp;
/**
* Represents an update of repository state.
*/
export interface ParsedCommit {
$type: "com.atproto.sync.subscribeRepos#commit";
/** The stream sequence number of this message. */
seq: number;
/** 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: At.CIDLink;
/** 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 (if any). */
since: string | null;
/** CAR file containing relevant blocks, as a diff since the previous repo state. */
blocks: Uint8Array;
/** List of repo mutation operations in this commit (eg, records created, updated, or deleted). */
ops: Array<RepoOp>;
/** List of new blobs (by CID) referenced by records in this commit. */
blobs: At.CIDLink[];
/** Timestamp of when this message was originally broadcast. */
time: string;
}
function parseHeader(header: any): { t: string; op: 1 | -1 } {
if (
!header || typeof header !== "object" || !header.t || typeof header.t !== "string"
|| !header.op || typeof header.op !== "number"
) throw new Error("Invalid header received");
return { t: header.t, op: header.op };
}
function readCar(buffer: Uint8Array): Map<At.CID, unknown> {
const records = new Map<At.CID, unknown>();
for (const { cid, bytes } of createCarIterator(buffer).iterate()) {
records.set(toCIDLink(cid).$link, decode(bytes));
}
return records;
}