Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-seahorses-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/service-utils": patch
---

[service-utils] import LZ4 codec
1 change: 1 addition & 0 deletions packages/service-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"dependencies": {
"aws4fetch": "1.0.20",
"kafkajs": "2.2.4",
"kafkajs-lz4": "2.0.0-beta.0",
"zod": "3.24.1"
},
"devDependencies": {
Expand Down
34 changes: 27 additions & 7 deletions packages/service-utils/src/node/usageV2.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { randomUUID } from "node:crypto";
import { checkServerIdentity } from "node:tls";
import {
CompressionCodecs,
CompressionTypes,
Kafka,
type Producer,
type ProducerConfig,
} from "kafkajs";
import LZ4Codec from "kafkajs-lz4";
import type { ServiceName } from "../core/services.js";
import { type UsageV2Event, getTopicName } from "../core/usageV2.js";

Expand All @@ -26,6 +28,7 @@ export class UsageV2Producer {
private kafka: Kafka;
private producer: Producer | null = null;
private topic: string;
private compression: CompressionTypes;

constructor(config: {
/**
Expand All @@ -40,14 +43,27 @@ export class UsageV2Producer {
* The product "source" where usage is coming from.
*/
productName: ServiceName;
/**
* The compression algorithm to use.
*/
compression?: CompressionTypes;

username: string;
password: string;
}) {
const {
producerName,
environment,
productName,
compression = CompressionTypes.LZ4,
username,
password,
} = config;

this.kafka = new Kafka({
clientId: `${config.producerName}-${config.environment}`,
clientId: `${producerName}-${environment}`,
brokers:
config.environment === "production"
environment === "production"
? ["warpstream.thirdweb.xyz:9092"]
: ["warpstream-dev.thirdweb.xyz:9092"],
ssl: {
Expand All @@ -57,19 +73,24 @@ export class UsageV2Producer {
},
sasl: {
mechanism: "plain",
username: config.username,
password: config.password,
username,
password,
},
});

this.topic = getTopicName(config.productName);
this.topic = getTopicName(productName);
this.compression = compression;
}

/**
* Connect the producer.
* This must be called before calling `sendEvents()`.
*/
async init(configOverrides?: ProducerConfig) {
if (this.compression === CompressionTypes.LZ4) {
CompressionCodecs[CompressionTypes.LZ4] = new LZ4Codec().codec;
}

this.producer = this.kafka.producer({
allowAutoTopicCreation: false,
...configOverrides,
Expand All @@ -95,7 +116,6 @@ export class UsageV2Producer {
configOverrides?: {
acks?: number;
timeout?: number;
compression?: CompressionTypes;
},
): Promise<void> {
if (!this.producer) {
Expand All @@ -121,7 +141,7 @@ export class UsageV2Producer {
})),
acks: -1, // All brokers must acknowledge
timeout: 10_000, // 10 seconds
compression: CompressionTypes.LZ4,
compression: this.compression,
...configOverrides,
});
}
Expand Down
Loading
Loading