UNPKG

@atproto/ozone

Version:

Backend service for moderating the Bluesky network.

65 lines 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Jetstream = void 0; const xrpc_server_1 = require("@atproto/xrpc-server"); class Jetstream { constructor(opts) { Object.defineProperty(this, "ws", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "url", { enumerable: true, configurable: true, writable: true, value: void 0 }); /** The current cursor. */ Object.defineProperty(this, "cursor", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.url = new URL(opts.endpoint); opts.wantedCollections?.forEach((collection) => { this.url.searchParams.append('wantedCollections', collection); }); opts.wantedDids?.forEach((did) => { this.url.searchParams.append('wantedDids', did); }); if (opts.cursor) this.cursor = opts.cursor; } async start(options) { this.ws = new xrpc_server_1.WebSocketKeepAlive({ getUrl: async () => { if (this.cursor) this.url.searchParams.set('cursor', this.cursor.toString()); return this.url.toString(); }, }); for await (const message of this.ws) { const parsedMessage = JSON.parse(message.toString()); if (parsedMessage.kind === 'commit') { const { collection, operation, record } = parsedMessage.commit || {}; if (operation === 'create') { options.onCreate?.[collection]?.(parsedMessage); } else if (operation === 'delete') { options.onDelete?.[collection]?.(parsedMessage); } } } } /** * Closes the WebSocket connection. */ close() { this.ws?.ws?.close(); } } exports.Jetstream = Jetstream; //# sourceMappingURL=service.js.map