|
| 1 | +import { randomUUID } from "node:crypto"; |
| 2 | +import { checkServerIdentity } from "node:tls"; |
| 3 | +import { Kafka, type Producer } from "kafkajs"; |
| 4 | +import type { UsageV2Event } from "src/core/usageV2.js"; |
| 5 | + |
| 6 | +const TOPIC_USAGE_V2 = "usage_v2.raw_events"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Creates a UsageV2Producer which opens a persistent TCP connection. |
| 10 | + * This class is thread-safe so your service should re-use one instance. |
| 11 | + * |
| 12 | + * Example: |
| 13 | + * ```ts |
| 14 | + * usageV2 = new UsageV2Producer(..) |
| 15 | + * await usageV2.init() |
| 16 | + * await usageV2.sendEvents(events) |
| 17 | + * // Non-blocking: |
| 18 | + * // void usageV2.sendEvents(events).catch(console.error) |
| 19 | + * ``` |
| 20 | + */ |
| 21 | +export class UsageV2Producer { |
| 22 | + private kafka: Kafka; |
| 23 | + private producer: Producer | null = null; |
| 24 | + |
| 25 | + constructor(config: { |
| 26 | + /** |
| 27 | + * A descriptive name for your service. Example: "storage-server" |
| 28 | + */ |
| 29 | + producerName: string; |
| 30 | + /** |
| 31 | + * The environment the service is running in. |
| 32 | + */ |
| 33 | + environment: "development" | "production"; |
| 34 | + |
| 35 | + username: string; |
| 36 | + password: string; |
| 37 | + }) { |
| 38 | + this.kafka = new Kafka({ |
| 39 | + clientId: `${config.producerName}-${config.environment}`, |
| 40 | + brokers: |
| 41 | + config.environment === "production" |
| 42 | + ? ["warpstream.thirdweb.xyz:9092"] |
| 43 | + : ["warpstream-dev.thirdweb.xyz:9092"], |
| 44 | + ssl: { |
| 45 | + checkServerIdentity(hostname, cert) { |
| 46 | + return checkServerIdentity(hostname.toLowerCase(), cert); |
| 47 | + }, |
| 48 | + }, |
| 49 | + sasl: { |
| 50 | + mechanism: "plain", |
| 51 | + username: config.username, |
| 52 | + password: config.password, |
| 53 | + }, |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Connect the producer. |
| 59 | + * This must be called before calling `sendEvents()`. |
| 60 | + */ |
| 61 | + async init() { |
| 62 | + this.producer = this.kafka.producer({ |
| 63 | + allowAutoTopicCreation: false, |
| 64 | + }); |
| 65 | + await this.producer.connect(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Send usageV2 events. |
| 70 | + * This method may throw. To call this non-blocking: |
| 71 | + * |
| 72 | + * ```ts |
| 73 | + * usageV2 = new UsageV2Producer(...) |
| 74 | + * void usageV2.sendEvents(events).catch(console.error) |
| 75 | + * |
| 76 | + * @param events - The events to send. |
| 77 | + */ |
| 78 | + async sendEvents(events: UsageV2Event[]): Promise<void> { |
| 79 | + if (!this.producer) { |
| 80 | + throw new Error("Producer not initialized. Call `init()` first."); |
| 81 | + } |
| 82 | + |
| 83 | + const parsedEvents = events.map(({ id, created_at, data, ...rest }) => { |
| 84 | + return { |
| 85 | + id: id ?? randomUUID(), |
| 86 | + created_at: created_at ?? new Date(), |
| 87 | + data: JSON.stringify(data), |
| 88 | + ...rest, |
| 89 | + }; |
| 90 | + }); |
| 91 | + |
| 92 | + await this.producer.send({ |
| 93 | + topic: TOPIC_USAGE_V2, |
| 94 | + messages: parsedEvents.map((event) => ({ |
| 95 | + value: JSON.stringify(event), |
| 96 | + })), |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Disconnects UsageV2Producer. |
| 102 | + * Useful when shutting down the service to flush in-flight events. |
| 103 | + */ |
| 104 | + async disconnect() { |
| 105 | + if (this.producer) { |
| 106 | + await this.producer.disconnect(); |
| 107 | + this.producer = null; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments