@trafficwar/node 4.0.0 → 5.0.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.d.ts CHANGED
@@ -20,12 +20,13 @@ type S3Source = OpenString<KnownS3Provider | `${string}.${KnownS3Provider}`>;
20
20
  interface TrafficWarEvent {
21
21
  event: EventCategory;
22
22
  event_id?: string;
23
- /** RFC3339 string, epoch milliseconds, or Date. Defaults to capture time. */
24
- timestamp?: string | number | Date;
25
- latency_ms?: number;
23
+ /** Actual span start: RFC3339 string, integer epoch milliseconds, or Date. */
24
+ timestamp: string | number | Date;
25
+ /** Measured duration; finite and nonnegative. */
26
+ latency_ms: number;
26
27
  properties?: JsonValue;
27
28
  user_agent?: string;
28
- label?: string;
29
+ label: string;
29
30
  ip?: string;
30
31
  /**
31
32
  * Stable emitter or dependency alias. External-service stations are keyed by
@@ -33,20 +34,20 @@ interface TrafficWarEvent {
33
34
  * `<bucket>.<provider>` such as `assets.ovh-s3` when separate bucket stations
34
35
  * are useful. Put the concrete action in `operation_type`.
35
36
  */
36
- source?: string;
37
+ source: string;
37
38
  country?: string;
38
39
  city?: string;
39
40
  trace_id?: string;
40
41
  distinct_id?: string;
41
42
  path?: string;
42
- /** RFC HTTP token, normalized to uppercase after trimming. */
43
+ /** Required for HTTP events. RFC HTTP token, normalized to uppercase after trimming. */
43
44
  http_method?: string;
44
45
  error?: string;
45
46
  exception?: string;
46
47
  error_code?: string;
47
- span_kind?: SpanKind;
48
+ span_kind: SpanKind;
48
49
  /** Concrete work performed, such as `route.handler` or `google.routes.compute`. */
49
- operation_type?: OperationType;
50
+ operation_type: OperationType;
50
51
  status_code?: number;
51
52
  }
52
53
  type CompressionMode = "auto" | "gzip" | "none";
package/dist/index.js CHANGED
@@ -299,12 +299,14 @@ function normalizeEvent(input, path, idempotencyKey) {
299
299
  normalized[key] = value;
300
300
  }
301
301
  }
302
- if (typeof normalized.event !== "string" || normalized.event.trim().length === 0) {
303
- throw validationError(
304
- `${path}.event must be a non-empty string`,
305
- `${path}.event`,
306
- idempotencyKey
307
- );
302
+ for (const field of ["event", "label", "source", "operation_type"]) {
303
+ if (typeof normalized[field] !== "string" || normalized[field].trim().length === 0) {
304
+ throw validationError(
305
+ `${path}.${field} must be a non-empty string`,
306
+ `${path}.${field}`,
307
+ idempotencyKey
308
+ );
309
+ }
308
310
  }
309
311
  for (const field of FORBIDDEN_IDENTITY_FIELDS) {
310
312
  if (Object.hasOwn(normalized, field)) {
@@ -324,18 +326,14 @@ function normalizeEvent(input, path, idempotencyKey) {
324
326
  );
325
327
  }
326
328
  }
327
- if (Object.hasOwn(normalized, "timestamp")) {
328
- normalized.timestamp = normalizeTimestamp(
329
- normalized.timestamp,
330
- `${path}.timestamp`,
331
- idempotencyKey
332
- );
333
- } else {
334
- normalized.timestamp = (/* @__PURE__ */ new Date()).toISOString();
335
- }
336
- if (Object.hasOwn(normalized, "latency_ms") && (typeof normalized.latency_ms !== "number" || !Number.isFinite(normalized.latency_ms))) {
329
+ normalized.timestamp = normalizeTimestamp(
330
+ normalized.timestamp,
331
+ `${path}.timestamp`,
332
+ idempotencyKey
333
+ );
334
+ if (typeof normalized.latency_ms !== "number" || !Number.isFinite(normalized.latency_ms) || normalized.latency_ms < 0) {
337
335
  throw validationError(
338
- `${path}.latency_ms must be a finite number`,
336
+ `${path}.latency_ms must be a finite nonnegative number`,
339
337
  `${path}.latency_ms`,
340
338
  idempotencyKey
341
339
  );
@@ -347,7 +345,8 @@ function normalizeEvent(input, path, idempotencyKey) {
347
345
  idempotencyKey
348
346
  );
349
347
  }
350
- if (httpMethodProvided) {
348
+ const category = normalized.event.trim().toLowerCase();
349
+ if (httpMethodProvided || category === "http") {
351
350
  normalized.http_method = normalizeHttpMethod(
352
351
  normalized.http_method,
353
352
  `${path}.http_method`,
@@ -363,13 +362,20 @@ function normalizeEvent(input, path, idempotencyKey) {
363
362
  );
364
363
  }
365
364
  }
366
- if (Object.hasOwn(normalized, "span_kind") && (typeof normalized.span_kind !== "string" || !SPAN_KINDS.has(normalized.span_kind))) {
365
+ if (typeof normalized.span_kind !== "string" || !SPAN_KINDS.has(normalized.span_kind)) {
367
366
  throw validationError(
368
367
  `${path}.span_kind must be server, client, producer, consumer, or internal`,
369
368
  `${path}.span_kind`,
370
369
  idempotencyKey
371
370
  );
372
371
  }
372
+ if ((category === "http" || category === "external" && httpMethodProvided) && !Object.hasOwn(normalized, "status_code") && !(typeof normalized.error === "string" && normalized.error.trim()) && !(typeof normalized.error_code === "string" && normalized.error_code.trim())) {
373
+ throw validationError(
374
+ `${path}.status_code is required for HTTP responses; use error or error_code for a failure without a response`,
375
+ `${path}.status_code`,
376
+ idempotencyKey
377
+ );
378
+ }
373
379
  try {
374
380
  return snapshotJsonValue(
375
381
  normalized,
@@ -588,7 +594,7 @@ function uuidv7() {
588
594
 
589
595
  // package.json
590
596
  var package_default = {
591
- version: "4.0.0"};
597
+ version: "5.0.0"};
592
598
 
593
599
  // src/client.ts
594
600
  var TRAFFICWAR_DEFAULT_BASE_URL = "https://ingest.trafficwar.tech";