@trafficwar/node 2.0.0 → 2.1.1

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.js CHANGED
@@ -307,6 +307,8 @@ function normalizeEvent(input, path, idempotencyKey) {
307
307
  `${path}.timestamp`,
308
308
  idempotencyKey
309
309
  );
310
+ } else {
311
+ normalized.timestamp = (/* @__PURE__ */ new Date()).toISOString();
310
312
  }
311
313
  if (Object.hasOwn(normalized, "latency_ms") && (typeof normalized.latency_ms !== "number" || !Number.isFinite(normalized.latency_ms))) {
312
314
  throw validationError(
@@ -446,7 +448,7 @@ function uuidv7() {
446
448
 
447
449
  // package.json
448
450
  var package_default = {
449
- version: "2.0.0"};
451
+ version: "2.1.1"};
450
452
 
451
453
  // src/client.ts
452
454
  var TRAFFICWAR_DEFAULT_BASE_URL = "https://ingest.trafficwar.tech";
@@ -535,6 +537,16 @@ function validateIntegerOption(name, value, fallback, minimum, maximum) {
535
537
  }
536
538
  return actual;
537
539
  }
540
+ function validateBooleanOption(name, value, fallback) {
541
+ const actual = value ?? fallback;
542
+ if (typeof actual !== "boolean") {
543
+ throw new TrafficWarValidationError(
544
+ `${name} must be a boolean`,
545
+ { path: `options.${name}` }
546
+ );
547
+ }
548
+ return actual;
549
+ }
538
550
  function validateCompression(value) {
539
551
  const actual = value ?? "auto";
540
552
  if (actual !== "auto" && actual !== "gzip" && actual !== "none") {
@@ -770,6 +782,7 @@ function parseSuccess(response, parsed, expectedCount, idempotencyKey) {
770
782
  }
771
783
  var TrafficWar = class {
772
784
  baseUrl;
785
+ debug;
773
786
  timeoutMs;
774
787
  maxRetries;
775
788
  compression;
@@ -799,6 +812,7 @@ var TrafficWar = class {
799
812
  }
800
813
  this.#apiKey = validateApiKey(options.apiKey);
801
814
  this.baseUrl = normalizeBaseUrl(options.baseUrl);
815
+ this.debug = validateBooleanOption("debug", options.debug, false);
802
816
  this.timeoutMs = validateIntegerOption(
803
817
  "timeoutMs",
804
818
  options.timeoutMs,
@@ -851,6 +865,23 @@ var TrafficWar = class {
851
865
  return globalThis.fetch(input, init);
852
866
  });
853
867
  this.#onError = options.onError;
868
+ this.#debugLog("client initialized", {
869
+ baseUrl: this.baseUrl,
870
+ compression: this.compression,
871
+ flushIntervalMs: this.flushIntervalMs,
872
+ maxQueueSize: this.maxQueueSize,
873
+ maxRetries: this.maxRetries,
874
+ timeoutMs: this.timeoutMs
875
+ });
876
+ }
877
+ #debugLog(message, details) {
878
+ if (!this.debug) {
879
+ return;
880
+ }
881
+ try {
882
+ console.debug(`[TrafficWar] ${message}`, details);
883
+ } catch {
884
+ }
854
885
  }
855
886
  capture(eventOrEvents) {
856
887
  if (this.#closed) {
@@ -917,8 +948,16 @@ var TrafficWar = class {
917
948
  this.#pendingEventIds.add(event.event_id);
918
949
  }
919
950
  this.#pendingCount += normalized.length;
951
+ this.#debugLog("events queued", {
952
+ added: normalized.length,
953
+ pending: this.#pendingCount
954
+ });
920
955
  if (this.#pendingCount >= TRAFFICWAR_MAX_BATCH_SIZE) {
921
956
  this.#clearTimer();
957
+ this.#debugLog("automatic flush triggered", {
958
+ pending: this.#pendingCount,
959
+ reason: "batch-size"
960
+ });
922
961
  this.#startBackgroundDrain();
923
962
  } else {
924
963
  this.#ensureTimer();
@@ -937,6 +976,9 @@ var TrafficWar = class {
937
976
  this.capture(events);
938
977
  }
939
978
  flush() {
979
+ this.#debugLog("manual flush requested", {
980
+ pending: this.#pendingCount
981
+ });
940
982
  if (this.#closePromise) {
941
983
  return this.#closePromise;
942
984
  }
@@ -950,6 +992,9 @@ var TrafficWar = class {
950
992
  if (this.#closePromise) {
951
993
  return this.#closePromise;
952
994
  }
995
+ this.#debugLog("close requested", {
996
+ pending: this.#pendingCount
997
+ });
953
998
  this.#clearTimer();
954
999
  this.#closing = true;
955
1000
  let closing;
@@ -966,6 +1011,10 @@ var TrafficWar = class {
966
1011
  } while (this.#pendingCount > 0);
967
1012
  this.#clearTimer();
968
1013
  this.#closed = true;
1014
+ this.#debugLog("client closed", {
1015
+ accepted,
1016
+ batches: batches.length
1017
+ });
969
1018
  return { accepted, batches };
970
1019
  } finally {
971
1020
  if (this.#closePromise === closing) {
@@ -988,10 +1037,18 @@ var TrafficWar = class {
988
1037
  if (this.#timer === timer) {
989
1038
  this.#timer = void 0;
990
1039
  }
1040
+ this.#debugLog("automatic flush triggered", {
1041
+ pending: this.#pendingCount,
1042
+ reason: "interval"
1043
+ });
991
1044
  this.#startBackgroundDrain();
992
1045
  }, this.flushIntervalMs);
993
1046
  timer.unref();
994
1047
  this.#timer = timer;
1048
+ this.#debugLog("flush timer scheduled", {
1049
+ delayMs: this.flushIntervalMs,
1050
+ pending: this.#pendingCount
1051
+ });
995
1052
  }
996
1053
  #clearTimer() {
997
1054
  if (this.#timer !== void 0) {
@@ -1042,8 +1099,24 @@ var TrafficWar = class {
1042
1099
  }
1043
1100
  let drain;
1044
1101
  drain = (async () => {
1102
+ this.#debugLog("flush started", {
1103
+ pending: this.#pendingCount
1104
+ });
1045
1105
  try {
1046
- return await this.#drain();
1106
+ const result = await this.#drain();
1107
+ this.#debugLog("flush completed", {
1108
+ accepted: result.accepted,
1109
+ batches: result.batches.length,
1110
+ pending: this.#pendingCount
1111
+ });
1112
+ return result;
1113
+ } catch (error) {
1114
+ this.#debugLog("flush failed", {
1115
+ errorMessage: error instanceof Error ? error.message : String(error),
1116
+ errorName: error instanceof Error ? error.name : typeof error,
1117
+ pending: this.#pendingCount
1118
+ });
1119
+ throw error;
1047
1120
  } finally {
1048
1121
  if (this.#drainPromise === drain) {
1049
1122
  this.#drainPromise = void 0;
@@ -1091,7 +1164,14 @@ var TrafficWar = class {
1091
1164
  this.compressionThresholdBytes
1092
1165
  );
1093
1166
  this.#queue.splice(0, count);
1094
- return { ...prepared, events, idempotencyKey };
1167
+ const batch = { ...prepared, events, idempotencyKey };
1168
+ this.#debugLog("batch prepared", {
1169
+ bodyBytes: prepared.body.byteLength,
1170
+ contentEncoding: prepared.contentEncoding ?? "identity",
1171
+ events: events.length,
1172
+ idempotencyKey
1173
+ });
1174
+ return batch;
1095
1175
  } catch (error) {
1096
1176
  if (!(error instanceof PreparedRequestTooLarge)) {
1097
1177
  throw error;
@@ -1141,6 +1221,13 @@ var TrafficWar = class {
1141
1221
  }
1142
1222
  );
1143
1223
  }
1224
+ this.#debugLog("request attempt", {
1225
+ attempt: attempt + 1,
1226
+ bodyBytes: batch.body.byteLength,
1227
+ events: batch.events.length,
1228
+ idempotencyKey: batch.idempotencyKey,
1229
+ maxAttempts: this.maxRetries + 1
1230
+ });
1144
1231
  let result;
1145
1232
  try {
1146
1233
  result = await executeAttempt(
@@ -1176,8 +1263,15 @@ var TrafficWar = class {
1176
1263
  }
1177
1264
  );
1178
1265
  }
1266
+ const delayMs = retryDelayMs(attempt, void 0);
1267
+ this.#debugLog("request retry scheduled", {
1268
+ attempt: attempt + 1,
1269
+ delayMs,
1270
+ idempotencyKey: batch.idempotencyKey,
1271
+ reason: failure.timedOut ? "timeout" : "transport"
1272
+ });
1179
1273
  try {
1180
- await sleep(retryDelayMs(attempt, void 0), signal);
1274
+ await sleep(delayMs, signal);
1181
1275
  } catch (cause) {
1182
1276
  throw new TrafficWarConnectionError(
1183
1277
  "TrafficWar request was aborted",
@@ -1194,18 +1288,32 @@ var TrafficWar = class {
1194
1288
  }
1195
1289
  const parsed = parseBody(result.text);
1196
1290
  if (result.response.ok) {
1197
- return parseSuccess(
1291
+ const ingest = parseSuccess(
1198
1292
  result.response,
1199
1293
  parsed,
1200
1294
  batch.events.length,
1201
1295
  batch.idempotencyKey
1202
1296
  );
1297
+ this.#debugLog("request accepted", {
1298
+ accepted: ingest.accepted,
1299
+ attempt: attempt + 1,
1300
+ idempotencyKey: batch.idempotencyKey,
1301
+ ingestId: ingest.ingestId
1302
+ });
1303
+ return ingest;
1203
1304
  }
1204
1305
  if (RETRYABLE_STATUSES.has(result.response.status) && attempt < this.maxRetries) {
1205
1306
  const retryAfter = retryAfterSeconds(result.response, parsed);
1206
1307
  if (retryAfter === void 0 || retryAfter <= MAX_RETRY_AFTER_SECONDS) {
1308
+ const delayMs = retryDelayMs(attempt, retryAfter);
1309
+ this.#debugLog("request retry scheduled", {
1310
+ attempt: attempt + 1,
1311
+ delayMs,
1312
+ idempotencyKey: batch.idempotencyKey,
1313
+ reason: `http-${result.response.status}`
1314
+ });
1207
1315
  try {
1208
- await sleep(retryDelayMs(attempt, retryAfter), signal);
1316
+ await sleep(delayMs, signal);
1209
1317
  } catch (cause) {
1210
1318
  throw new TrafficWarConnectionError(
1211
1319
  "TrafficWar request was aborted",
@@ -1221,6 +1329,11 @@ var TrafficWar = class {
1221
1329
  continue;
1222
1330
  }
1223
1331
  }
1332
+ this.#debugLog("request rejected", {
1333
+ attempt: attempt + 1,
1334
+ idempotencyKey: batch.idempotencyKey,
1335
+ status: result.response.status
1336
+ });
1224
1337
  throw buildApiError(
1225
1338
  result.response,
1226
1339
  parsed,