@trafficwar/node 1.0.0 → 2.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/README.md +97 -46
- package/dist/index.cjs +413 -130
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +26 -12
- package/dist/index.d.ts +26 -12
- package/dist/index.js +412 -131
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,81 +2,133 @@
|
|
|
2
2
|
|
|
3
3
|
Official, dependency-free TrafficWar server SDK for Node.js 22 and newer.
|
|
4
4
|
|
|
5
|
+
> **Breaking change in 2.0.0:** `capture` now enqueues events for automatic
|
|
6
|
+
> batching instead of waiting for an individual ingestion response.
|
|
7
|
+
|
|
5
8
|
## Install
|
|
6
9
|
|
|
7
10
|
```sh
|
|
8
11
|
npm install @trafficwar/node
|
|
9
12
|
```
|
|
10
13
|
|
|
11
|
-
## Capture
|
|
14
|
+
## Capture events
|
|
12
15
|
|
|
13
16
|
```ts
|
|
14
17
|
import { TrafficWar } from "@trafficwar/node";
|
|
15
18
|
|
|
16
19
|
const trafficwar = new TrafficWar({
|
|
17
20
|
apiKey: process.env.TRAFFICWAR_API_KEY!,
|
|
21
|
+
onError(error) {
|
|
22
|
+
console.error("TrafficWar automatic flush failed", error);
|
|
23
|
+
},
|
|
18
24
|
});
|
|
19
25
|
|
|
20
|
-
|
|
26
|
+
trafficwar.capture({
|
|
21
27
|
event: "checkout.completed",
|
|
22
|
-
event_id: "018f47a2-7b17-7b9f-8a9c-5b1fdbda7b31",
|
|
23
28
|
timestamp: new Date(),
|
|
24
29
|
latency_ms: 184.2,
|
|
25
30
|
status_code: 200,
|
|
26
|
-
|
|
27
|
-
operation_type: "http",
|
|
31
|
+
source: "checkout-api",
|
|
28
32
|
properties: {
|
|
29
33
|
orderId: "ord_123",
|
|
30
34
|
total: 49.95,
|
|
31
35
|
},
|
|
32
36
|
});
|
|
33
37
|
|
|
34
|
-
|
|
38
|
+
trafficwar.capture([
|
|
39
|
+
{ event: "job.started", source: "worker" },
|
|
40
|
+
{ event: "job.finished", source: "worker", latency_ms: 42 },
|
|
41
|
+
]);
|
|
35
42
|
```
|
|
36
43
|
|
|
44
|
+
`capture` accepts one event or a non-empty array. It validates, snapshots, and
|
|
45
|
+
enqueues the input synchronously, then returns `void`; it does not return an
|
|
46
|
+
`IngestResult` or wait for the network.
|
|
47
|
+
|
|
37
48
|
`Date` timestamps are serialized as RFC3339 without changing the caller's
|
|
38
49
|
object. String timestamps must be RFC3339; numeric timestamps are integer epoch
|
|
39
50
|
milliseconds.
|
|
40
51
|
|
|
41
|
-
|
|
42
|
-
let TrafficWar derive a stable ID from the request's idempotency receipt.
|
|
43
|
-
`user_id`, `service`, and `service_id` must not be sent: TrafficWar derives
|
|
44
|
-
identity from the API key.
|
|
52
|
+
## Automatic batching
|
|
45
53
|
|
|
46
|
-
|
|
54
|
+
The first queued event starts a one-second timer. The client flushes when that
|
|
55
|
+
timer expires or as soon as 10,000 events are pending. TrafficWar's fixed
|
|
56
|
+
server batch maximum is 10,000, so a larger queued array is split across
|
|
57
|
+
requests.
|
|
58
|
+
|
|
59
|
+
Every request is a bare JSON array sent to `/v1/server/batch`, including a
|
|
60
|
+
single-event capture. The queue holds at most 100,000 unacknowledged events by
|
|
61
|
+
default. Configure the timing and queue bound with `flushIntervalMs` and
|
|
62
|
+
`maxQueueSize`:
|
|
47
63
|
|
|
48
64
|
```ts
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
65
|
+
const trafficwar = new TrafficWar({
|
|
66
|
+
apiKey: process.env.TRAFFICWAR_API_KEY!,
|
|
67
|
+
flushIntervalMs: 1_000,
|
|
68
|
+
maxQueueSize: 100_000,
|
|
69
|
+
});
|
|
53
70
|
```
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
72
|
+
If enqueueing would exceed the configured queue limit, `capture` throws a
|
|
73
|
+
`TrafficWarValidationError` and enqueues none of that call.
|
|
74
|
+
|
|
75
|
+
The SDK generates a process-monotonic RFC 9562 UUIDv7 `event_id` for each event
|
|
76
|
+
that omits one. A caller may override it with any valid UUID. Caller-owned
|
|
77
|
+
objects are never modified.
|
|
58
78
|
|
|
59
|
-
|
|
79
|
+
Use `source` for caller-selected origin or runtime metadata such as
|
|
80
|
+
`checkout-api`, `node-worker`, or `production`. `span_kind` is separate,
|
|
81
|
+
optional tracing semantics (`server`, `client`, `producer`, `consumer`, or
|
|
82
|
+
`internal`); it does not replace `source`.
|
|
60
83
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
84
|
+
## Flush results and shutdown
|
|
85
|
+
|
|
86
|
+
Call `flush` when an application needs an acknowledgement before continuing:
|
|
64
87
|
|
|
65
88
|
```ts
|
|
66
|
-
|
|
89
|
+
trafficwar.capture({ event: "deployment.finished", source: "release-worker" });
|
|
67
90
|
|
|
68
|
-
await trafficwar.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
91
|
+
const result = await trafficwar.flush();
|
|
92
|
+
console.log(result.accepted);
|
|
93
|
+
for (const batch of result.batches) {
|
|
94
|
+
console.log(batch.ingestId, batch.accepted, batch.idempotencyKey);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`flush` returns an aggregate `FlushResult`: `accepted` is the number of events
|
|
99
|
+
drained by that operation, and `batches` contains one `IngestResult` per HTTP
|
|
100
|
+
request.
|
|
101
|
+
|
|
102
|
+
Always await `close` during graceful application shutdown:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
const result = await trafficwar.close();
|
|
106
|
+
console.log(result.accepted, result.batches.length);
|
|
75
107
|
```
|
|
76
108
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
109
|
+
`close` already stops automatic work and flushes every queued event; a separate
|
|
110
|
+
shutdown `flush` is unnecessary. The automatic timer is unref'ed and will not
|
|
111
|
+
keep Node running, so exiting without awaiting `close` can lose pending events.
|
|
112
|
+
|
|
113
|
+
`captureBatch(events)` remains as a deprecated compatibility alias. New code
|
|
114
|
+
passes the non-empty array directly to `capture(events)`.
|
|
115
|
+
|
|
116
|
+
## Delivery, retries, and errors
|
|
117
|
+
|
|
118
|
+
The SDK prepares each HTTP batch once. Its compact JSON bytes, optional gzip
|
|
119
|
+
bytes, generated event IDs, and internal idempotency key stay stable across
|
|
120
|
+
bounded retries. Idempotency is an internal delivery detail rather than part of
|
|
121
|
+
the `capture` API.
|
|
122
|
+
|
|
123
|
+
Transport failures, per-attempt timeouts, and HTTP 408, 425, 500, 502, 503, and
|
|
124
|
+
504 use exponential full jitter. `Retry-After` is honored as a minimum delay
|
|
125
|
+
up to 60 seconds; larger values are surfaced immediately as API errors. HTTP
|
|
126
|
+
409 and 429 are never retried.
|
|
127
|
+
|
|
128
|
+
An automatic delivery failure invokes optional `onError`. The handler is
|
|
129
|
+
observational: the failed prepared batch remains queued. A later automatic
|
|
130
|
+
attempt, `flush`, or `close` retries that same batch, and explicit `flush` or
|
|
131
|
+
`close` surfaces an error if delivery still fails.
|
|
80
132
|
|
|
81
133
|
## Client options
|
|
82
134
|
|
|
@@ -87,6 +139,10 @@ Reusing a key for a different request produces HTTP 409 and is never retried.
|
|
|
87
139
|
- `maxRetries`: retries after the first attempt. Defaults to 3.
|
|
88
140
|
- `compression`: `"auto"`, `"gzip"`, or `"none"`. Defaults to `"auto"`.
|
|
89
141
|
- `compressionThresholdBytes`: gzip threshold in auto mode. Defaults to 1024.
|
|
142
|
+
- `flushIntervalMs`: delay from the first pending event to automatic flush.
|
|
143
|
+
Defaults to 1,000 ms.
|
|
144
|
+
- `maxQueueSize`: maximum unacknowledged events. Defaults to 100,000.
|
|
145
|
+
- `onError`: optional callback for automatic background delivery errors.
|
|
90
146
|
- `fetch`: injected Fetch-compatible function, primarily for testing.
|
|
91
147
|
|
|
92
148
|
The SDK sends `Content-Type: application/json`, `Authorization: Bearer ...`, a
|
|
@@ -95,13 +151,6 @@ server SDK User-Agent, and `x-trafficwar-sdk`. It never sends `Origin`.
|
|
|
95
151
|
Decoded JSON is limited to 8 MiB. The identity or gzip wire body is limited to
|
|
96
152
|
2 MiB. Auto mode gzip-compresses bodies at or above the configured threshold.
|
|
97
153
|
|
|
98
|
-
## Retries and errors
|
|
99
|
-
|
|
100
|
-
Transport failures, per-attempt timeouts, and HTTP 408, 425, 500, 502, 503, and
|
|
101
|
-
504 use exponential full jitter. `Retry-After` is honored as a minimum delay
|
|
102
|
-
up to 60 seconds; larger values are surfaced immediately as API errors so calls
|
|
103
|
-
remain bounded. HTTP 409 and 429 are never retried.
|
|
104
|
-
|
|
105
154
|
```ts
|
|
106
155
|
import {
|
|
107
156
|
TrafficWarApiError,
|
|
@@ -111,8 +160,10 @@ import {
|
|
|
111
160
|
TrafficWarValidationError,
|
|
112
161
|
} from "@trafficwar/node";
|
|
113
162
|
|
|
163
|
+
trafficwar.capture({ event: "task.failed", status_code: 500 });
|
|
164
|
+
|
|
114
165
|
try {
|
|
115
|
-
await trafficwar.
|
|
166
|
+
await trafficwar.flush();
|
|
116
167
|
} catch (error) {
|
|
117
168
|
if (error instanceof TrafficWarRateLimitError) {
|
|
118
169
|
console.error(error.period, error.remainingEvents, error.retryAfterSeconds);
|
|
@@ -129,11 +180,11 @@ try {
|
|
|
129
180
|
}
|
|
130
181
|
```
|
|
131
182
|
|
|
132
|
-
|
|
133
|
-
the status, parsed response details, `Retry-After` as
|
|
134
|
-
the ingest ID when supplied by TrafficWar. Rate-limit
|
|
135
|
-
`period`, `used`, `limit`, `remainingEvents`,
|
|
136
|
-
`retryAfterSecs`.
|
|
183
|
+
Delivery errors retain the batch's internal idempotency key. HTTP and protocol
|
|
184
|
+
errors also retain the status, parsed response details, `Retry-After` as
|
|
185
|
+
`retryAfterSeconds`, and the ingest ID when supplied by TrafficWar. Rate-limit
|
|
186
|
+
errors additionally map `period`, `used`, `limit`, `remainingEvents`,
|
|
187
|
+
`batchEvents`, and `retryAfterSecs`.
|
|
137
188
|
|
|
138
189
|
CommonJS is also supported:
|
|
139
190
|
|