@tracelog/lib 2.2.1 → 2.3.0-rc.83.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.
@@ -557,7 +557,7 @@ var LONG_TASK_THROTTLE_MS = 1e3;
557
557
  var MAX_NAVIGATION_HISTORY = 50;
558
558
 
559
559
  // package.json
560
- var version = "2.2.0";
560
+ var version = "2.3.0";
561
561
 
562
562
  // src/constants/version.constants.ts
563
563
  var LIB_VERSION = version;
@@ -848,7 +848,6 @@ var sanitizeString = (value) => {
848
848
  }
849
849
  });
850
850
  }
851
- sanitized = sanitized.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&#x27;").replaceAll("/", "&#x2F;");
852
851
  const result = sanitized.trim();
853
852
  return result;
854
853
  };
@@ -2536,6 +2535,7 @@ var EventManager = class extends StateManager {
2536
2535
  eventsQueue = [];
2537
2536
  pendingEventsBuffer = [];
2538
2537
  sendIntervalId = null;
2538
+ sendInProgress = false;
2539
2539
  rateLimitCounter = 0;
2540
2540
  rateLimitWindowStart = 0;
2541
2541
  lastSessionId = null;
@@ -3184,42 +3184,47 @@ var EventManager = class extends StateManager {
3184
3184
  }
3185
3185
  }
3186
3186
  async sendEventsQueue() {
3187
- if (!this.get("sessionId") || this.eventsQueue.length === 0) {
3187
+ if (!this.get("sessionId") || this.eventsQueue.length === 0 || this.sendInProgress) {
3188
3188
  return;
3189
3189
  }
3190
- const body = this.buildEventsPayload();
3191
- if (this.dataSenders.length === 0) {
3192
- this.emitEventsQueue(body);
3193
- return;
3194
- }
3195
- const eventsToSend = [...this.eventsQueue];
3196
- const eventIds = eventsToSend.map((e) => e.id);
3197
- const sendPromises = this.dataSenders.map(
3198
- async (sender) => sender.sendEventsQueue(body, {
3199
- onSuccess: () => {
3200
- },
3201
- onFailure: () => {
3190
+ this.sendInProgress = true;
3191
+ try {
3192
+ const body = this.buildEventsPayload();
3193
+ if (this.dataSenders.length === 0) {
3194
+ this.emitEventsQueue(body);
3195
+ return;
3196
+ }
3197
+ const eventsToSend = [...this.eventsQueue];
3198
+ const eventIds = eventsToSend.map((e) => e.id);
3199
+ const sendPromises = this.dataSenders.map(
3200
+ async (sender) => sender.sendEventsQueue(body, {
3201
+ onSuccess: () => {
3202
+ },
3203
+ onFailure: () => {
3204
+ }
3205
+ })
3206
+ );
3207
+ const results = await Promise.allSettled(sendPromises);
3208
+ const anySucceeded = results.some((result) => this.isSuccessfulResult(result));
3209
+ if (anySucceeded) {
3210
+ this.removeProcessedEvents(eventIds);
3211
+ this.emitEventsQueue(body);
3212
+ const failedCount = results.filter((result) => !this.isSuccessfulResult(result)).length;
3213
+ if (failedCount > 0) {
3214
+ log("debug", "Periodic send completed with some failures, removed from queue and persisted per-integration", {
3215
+ data: { eventCount: eventsToSend.length, failedCount }
3216
+ });
3202
3217
  }
3203
- })
3204
- );
3205
- const results = await Promise.allSettled(sendPromises);
3206
- const anySucceeded = results.some((result) => this.isSuccessfulResult(result));
3207
- if (anySucceeded) {
3208
- this.removeProcessedEvents(eventIds);
3209
- this.emitEventsQueue(body);
3210
- const failedCount = results.filter((result) => !this.isSuccessfulResult(result)).length;
3211
- if (failedCount > 0) {
3212
- log("debug", "Periodic send completed with some failures, removed from queue and persisted per-integration", {
3213
- data: { eventCount: eventsToSend.length, failedCount }
3218
+ } else {
3219
+ log("debug", "Periodic send complete failure, events kept in queue for retry", {
3220
+ data: { eventCount: eventsToSend.length }
3214
3221
  });
3215
3222
  }
3216
- } else {
3217
- log("debug", "Periodic send complete failure, events kept in queue for retry", {
3218
- data: { eventCount: eventsToSend.length }
3219
- });
3220
- }
3221
- if (this.eventsQueue.length === 0) {
3222
- this.clearSendInterval();
3223
+ if (this.eventsQueue.length === 0) {
3224
+ this.clearSendInterval();
3225
+ }
3226
+ } finally {
3227
+ this.sendInProgress = false;
3223
3228
  }
3224
3229
  }
3225
3230
  buildEventsPayload() {
@@ -6122,7 +6127,7 @@ var App = class extends StateManager {
6122
6127
  */
6123
6128
  async init(config = {}) {
6124
6129
  if (this.isInitialized) {
6125
- return;
6130
+ return { sessionId: this.get("sessionId") ?? "" };
6126
6131
  }
6127
6132
  this.managers.storage = new StorageManager();
6128
6133
  try {
@@ -6140,6 +6145,7 @@ var App = class extends StateManager {
6140
6145
  log("warn", "Failed to recover persisted events", { error });
6141
6146
  });
6142
6147
  this.isInitialized = true;
6148
+ return { sessionId: this.get("sessionId") ?? "" };
6143
6149
  } catch (error) {
6144
6150
  this.destroy(true);
6145
6151
  const errorMessage = error instanceof Error ? error.message : String(error);
@@ -6299,6 +6305,15 @@ var App = class extends StateManager {
6299
6305
  getEventManager() {
6300
6306
  return this.managers.event;
6301
6307
  }
6308
+ /**
6309
+ * Returns the current session ID.
6310
+ *
6311
+ * @returns The session ID string, or null if not yet initialized
6312
+ * @internal Used by api.getSessionId()
6313
+ */
6314
+ getSessionId() {
6315
+ return this.get("sessionId");
6316
+ }
6302
6317
  /**
6303
6318
  * Validates metadata object structure and values.
6304
6319
  *
@@ -6411,17 +6426,17 @@ var isInitializing = false;
6411
6426
  var isDestroying = false;
6412
6427
  var init = async (config) => {
6413
6428
  if (typeof window === "undefined" || typeof document === "undefined") {
6414
- return;
6429
+ return { sessionId: "" };
6415
6430
  }
6416
6431
  isDestroying = false;
6417
6432
  if (window.__traceLogDisabled === true) {
6418
- return;
6433
+ return { sessionId: "" };
6419
6434
  }
6420
6435
  if (app) {
6421
- return;
6436
+ return { sessionId: app.getSessionId() ?? "" };
6422
6437
  }
6423
6438
  if (isInitializing) {
6424
- return;
6439
+ return { sessionId: "" };
6425
6440
  }
6426
6441
  isInitializing = true;
6427
6442
  try {
@@ -6450,8 +6465,9 @@ var init = async (config) => {
6450
6465
  reject(new Error(`[TraceLog] Initialization timeout after ${INITIALIZATION_TIMEOUT_MS}ms`));
6451
6466
  }, INITIALIZATION_TIMEOUT_MS);
6452
6467
  });
6453
- await Promise.race([initPromise, timeoutPromise]);
6468
+ const result = await Promise.race([initPromise, timeoutPromise]);
6454
6469
  app = instance;
6470
+ return result;
6455
6471
  } catch (error) {
6456
6472
  try {
6457
6473
  instance.destroy(true);
@@ -6577,6 +6593,15 @@ var isInitialized = () => {
6577
6593
  }
6578
6594
  return app !== null;
6579
6595
  };
6596
+ var getSessionId = () => {
6597
+ if (typeof window === "undefined" || typeof document === "undefined") {
6598
+ return null;
6599
+ }
6600
+ if (!app) {
6601
+ return null;
6602
+ }
6603
+ return app.getSessionId();
6604
+ };
6580
6605
  var destroy = () => {
6581
6606
  if (typeof window === "undefined" || typeof document === "undefined") {
6582
6607
  return;
@@ -6650,6 +6675,7 @@ var tracelog = {
6650
6675
  setCustomHeaders,
6651
6676
  removeCustomHeaders,
6652
6677
  isInitialized,
6678
+ getSessionId,
6653
6679
  destroy,
6654
6680
  setQaMode: setQaMode2,
6655
6681
  updateGlobalMetadata,