@trafficwar/node 2.0.0 → 2.1.0

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