@bentolabs/sdk 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -119,6 +119,7 @@ export declare class BentoLabsSDK {
119
119
  private isRecording;
120
120
  private batchTimer;
121
121
  private retryTimer;
122
+ private isSending;
122
123
  private stopRecordingFn;
123
124
  private distinctId;
124
125
  private identity;
package/dist/index.js CHANGED
@@ -13457,6 +13457,7 @@ var BentoLabsSDK = class {
13457
13457
  this.isRecording = false;
13458
13458
  this.batchTimer = null;
13459
13459
  this.retryTimer = null;
13460
+ this.isSending = false;
13460
13461
  this.stopRecordingFn = null;
13461
13462
  this.distinctId = "";
13462
13463
  this.identity = {
@@ -14126,7 +14127,7 @@ var BentoLabsSDK = class {
14126
14127
  if (this.config.debug) {
14127
14128
  console.log("[BentoLabsSDK] Event added:", event.type);
14128
14129
  }
14129
- if (this.events.length >= this.config.batchSize) {
14130
+ if (this.events.length >= this.config.batchSize && !this.isSending) {
14130
14131
  if (this.config.debug) {
14131
14132
  console.log(`[BentoLabsSDK] Batch size limit reached (${this.config.batchSize}), sending batch`);
14132
14133
  }
@@ -14134,6 +14135,12 @@ var BentoLabsSDK = class {
14134
14135
  }
14135
14136
  }
14136
14137
  async sendBatch() {
14138
+ if (this.isSending) {
14139
+ if (this.config.debug) {
14140
+ console.log("[BentoLabsSDK] Already sending, skipping batch");
14141
+ }
14142
+ return;
14143
+ }
14137
14144
  const now = Date.now();
14138
14145
  const readyEvents = this.events.filter((event) => {
14139
14146
  if ("nextRetryTime" in event) {
@@ -14144,6 +14151,7 @@ var BentoLabsSDK = class {
14144
14151
  if (readyEvents.length === 0) {
14145
14152
  return;
14146
14153
  }
14154
+ this.isSending = true;
14147
14155
  this.events = this.events.filter((event) => !readyEvents.includes(event));
14148
14156
  const payload = {
14149
14157
  sessionId: this.sessionId,
@@ -14157,14 +14165,53 @@ var BentoLabsSDK = class {
14157
14165
  console.log(`[BentoLabsSDK] Sending batch with ${readyEvents.length} events`);
14158
14166
  }
14159
14167
  try {
14168
+ const jsonPayload = JSON.stringify(payload);
14169
+ let body = jsonPayload;
14170
+ const headers = {
14171
+ "Content-Type": "application/json",
14172
+ Authorization: `Bearer ${this.config.apiKey}`,
14173
+ "X-Session-ID": this.sessionId
14174
+ };
14175
+ if (this.config.enableCompression && typeof CompressionStream !== "undefined") {
14176
+ try {
14177
+ const encoder = new TextEncoder();
14178
+ const data = encoder.encode(jsonPayload);
14179
+ const cs = new CompressionStream("gzip");
14180
+ const writer = cs.writable.getWriter();
14181
+ writer.write(data);
14182
+ writer.close();
14183
+ const compressedChunks = [];
14184
+ const reader = cs.readable.getReader();
14185
+ let result2 = await reader.read();
14186
+ while (!result2.done) {
14187
+ compressedChunks.push(result2.value);
14188
+ result2 = await reader.read();
14189
+ }
14190
+ const totalLength = compressedChunks.reduce((acc, chunk) => acc + chunk.length, 0);
14191
+ const compressedData = new Uint8Array(totalLength);
14192
+ let offset = 0;
14193
+ for (const chunk of compressedChunks) {
14194
+ compressedData.set(chunk, offset);
14195
+ offset += chunk.length;
14196
+ }
14197
+ body = new Blob([compressedData], { type: "application/json" });
14198
+ headers["Content-Encoding"] = "gzip";
14199
+ if (this.config.debug) {
14200
+ const ratio = ((1 - compressedData.length / data.length) * 100).toFixed(1);
14201
+ console.log(
14202
+ `[BentoLabsSDK] Compressed payload: ${data.length} -> ${compressedData.length} bytes (${ratio}% reduction)`
14203
+ );
14204
+ }
14205
+ } catch (compressionError) {
14206
+ if (this.config.debug) {
14207
+ console.warn("[BentoLabsSDK] Compression failed, sending uncompressed:", compressionError);
14208
+ }
14209
+ }
14210
+ }
14160
14211
  const response = await fetch(`${this.config.endpoint}/events/`, {
14161
14212
  method: "POST",
14162
- headers: {
14163
- "Content-Type": "application/json",
14164
- Authorization: `Bearer ${this.config.apiKey}`,
14165
- "X-Session-ID": this.sessionId
14166
- },
14167
- body: JSON.stringify(payload)
14213
+ headers,
14214
+ body
14168
14215
  });
14169
14216
  if (!response.ok) {
14170
14217
  throw new Error(`HTTP error! status: ${response.status}`);
@@ -14173,7 +14220,9 @@ var BentoLabsSDK = class {
14173
14220
  console.log("[BentoLabsSDK] Batch sent successfully");
14174
14221
  }
14175
14222
  } catch (error) {
14176
- console.error("[BentoLabsSDK] Failed to send batch:", error);
14223
+ if (this.config.debug) {
14224
+ console.error("[BentoLabsSDK] Failed to send batch:", error);
14225
+ }
14177
14226
  const retryableEvents = readyEvents.map((event) => {
14178
14227
  const retryCount = "retryCount" in event ? event.retryCount + 1 : 1;
14179
14228
  const delay = this.config.baseRetryDelay * Math.pow(2, retryCount - 1);
@@ -14187,12 +14236,15 @@ var BentoLabsSDK = class {
14187
14236
  if (droppedEvents > 0 && this.config.debug) {
14188
14237
  console.log(`[BentoLabsSDK] Dropped ${droppedEvents} events after max retries`);
14189
14238
  }
14190
- this.events.unshift(...eventsToRetry);
14191
- if (this.config.debug && eventsToRetry.length > 0) {
14192
- const nextRetryIn = Math.min(...eventsToRetry.map((e) => e.nextRetryTime)) - now;
14193
- console.log(`[BentoLabsSDK] ${eventsToRetry.length} events re-queued for retry in ${nextRetryIn}ms`);
14239
+ if (eventsToRetry.length > 0) {
14240
+ this.events.unshift(...eventsToRetry);
14241
+ if (this.config.debug) {
14242
+ const nextRetryIn = Math.min(...eventsToRetry.map((e) => e.nextRetryTime)) - now;
14243
+ console.log(`[BentoLabsSDK] ${eventsToRetry.length} events re-queued for retry in ${nextRetryIn}ms`);
14244
+ }
14194
14245
  }
14195
- this.scheduleRetry();
14246
+ } finally {
14247
+ this.isSending = false;
14196
14248
  }
14197
14249
  }
14198
14250
  scheduleRetry() {