@trafficwar/node 1.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/LICENSE +21 -0
- package/README.md +146 -0
- package/dist/index.cjs +973 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +141 -0
- package/dist/index.d.ts +141 -0
- package/dist/index.js +959 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TrafficWar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# @trafficwar/node
|
|
2
|
+
|
|
3
|
+
Official, dependency-free TrafficWar server SDK for Node.js 22 and newer.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @trafficwar/node
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Capture an event
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { TrafficWar } from "@trafficwar/node";
|
|
15
|
+
|
|
16
|
+
const trafficwar = new TrafficWar({
|
|
17
|
+
apiKey: process.env.TRAFFICWAR_API_KEY!,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const result = await trafficwar.capture({
|
|
21
|
+
event: "checkout.completed",
|
|
22
|
+
event_id: "018f47a2-7b17-7b9f-8a9c-5b1fdbda7b31",
|
|
23
|
+
timestamp: new Date(),
|
|
24
|
+
latency_ms: 184.2,
|
|
25
|
+
status_code: 200,
|
|
26
|
+
span_kind: "server",
|
|
27
|
+
operation_type: "http",
|
|
28
|
+
properties: {
|
|
29
|
+
orderId: "ord_123",
|
|
30
|
+
total: 49.95,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
console.log(result.ingestId, result.accepted, result.idempotencyKey);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`Date` timestamps are serialized as RFC3339 without changing the caller's
|
|
38
|
+
object. String timestamps must be RFC3339; numeric timestamps are integer epoch
|
|
39
|
+
milliseconds.
|
|
40
|
+
|
|
41
|
+
The SDK does not generate `event_id`. You may provide a UUID, or omit it and
|
|
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.
|
|
45
|
+
|
|
46
|
+
## Capture a batch
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
await trafficwar.captureBatch([
|
|
50
|
+
{ event: "job.started", trace_id: "trace-123" },
|
|
51
|
+
{ event: "job.finished", trace_id: "trace-123", latency_ms: 42 },
|
|
52
|
+
]);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Batches must contain 1 to 10,000 events. The SDK sends the required bare JSON
|
|
56
|
+
array to `/v1/server/batch`; a single capture sends one JSON object to
|
|
57
|
+
`/v1/server/capture`.
|
|
58
|
+
|
|
59
|
+
## Request options
|
|
60
|
+
|
|
61
|
+
Every SDK call creates a fresh idempotency key. The exact JSON bytes, optional
|
|
62
|
+
gzip bytes, and key are created once and reused across that call's retries.
|
|
63
|
+
Supply your own key when retry coordination must survive a process boundary:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const controller = new AbortController();
|
|
67
|
+
|
|
68
|
+
await trafficwar.capture(
|
|
69
|
+
{ event: "invoice.created" },
|
|
70
|
+
{
|
|
71
|
+
idempotencyKey: "invoice-inv_123-v1",
|
|
72
|
+
signal: controller.signal,
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
An idempotency key must contain 1-256 visible ASCII characters. A key is scoped
|
|
78
|
+
to the service represented by the API key and is body- and route-sensitive.
|
|
79
|
+
Reusing a key for a different request produces HTTP 409 and is never retried.
|
|
80
|
+
|
|
81
|
+
## Client options
|
|
82
|
+
|
|
83
|
+
- `apiKey` (required): TrafficWar server API key.
|
|
84
|
+
- `baseUrl`: ingest origin. Defaults to `https://ingest.trafficwar.tech`.
|
|
85
|
+
- `timeoutMs`: timeout for each attempt. Defaults to 30,000 ms, safely above
|
|
86
|
+
TrafficWar's 10-second durable acknowledgement window.
|
|
87
|
+
- `maxRetries`: retries after the first attempt. Defaults to 3.
|
|
88
|
+
- `compression`: `"auto"`, `"gzip"`, or `"none"`. Defaults to `"auto"`.
|
|
89
|
+
- `compressionThresholdBytes`: gzip threshold in auto mode. Defaults to 1024.
|
|
90
|
+
- `fetch`: injected Fetch-compatible function, primarily for testing.
|
|
91
|
+
|
|
92
|
+
The SDK sends `Content-Type: application/json`, `Authorization: Bearer ...`, a
|
|
93
|
+
server SDK User-Agent, and `x-trafficwar-sdk`. It never sends `Origin`.
|
|
94
|
+
|
|
95
|
+
Decoded JSON is limited to 8 MiB. The identity or gzip wire body is limited to
|
|
96
|
+
2 MiB. Auto mode gzip-compresses bodies at or above the configured threshold.
|
|
97
|
+
|
|
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
|
+
```ts
|
|
106
|
+
import {
|
|
107
|
+
TrafficWarApiError,
|
|
108
|
+
TrafficWarConnectionError,
|
|
109
|
+
TrafficWarProtocolError,
|
|
110
|
+
TrafficWarRateLimitError,
|
|
111
|
+
TrafficWarValidationError,
|
|
112
|
+
} from "@trafficwar/node";
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
await trafficwar.capture({ event: "task.failed", status_code: 500 });
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (error instanceof TrafficWarRateLimitError) {
|
|
118
|
+
console.error(error.period, error.remainingEvents, error.retryAfterSeconds);
|
|
119
|
+
} else if (error instanceof TrafficWarApiError) {
|
|
120
|
+
console.error(error.status, error.ingestId, error.details);
|
|
121
|
+
} else if (error instanceof TrafficWarConnectionError) {
|
|
122
|
+
console.error(error.timedOut, error.attempts, error.idempotencyKey);
|
|
123
|
+
} else if (
|
|
124
|
+
error instanceof TrafficWarProtocolError ||
|
|
125
|
+
error instanceof TrafficWarValidationError
|
|
126
|
+
) {
|
|
127
|
+
console.error(error.message);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Request errors retain the idempotency key. HTTP and protocol errors also retain
|
|
133
|
+
the status, parsed response details, `Retry-After` as `retryAfterSeconds`, and
|
|
134
|
+
the ingest ID when supplied by TrafficWar. Rate-limit errors additionally map
|
|
135
|
+
`period`, `used`, `limit`, `remainingEvents`, `batchEvents`, and
|
|
136
|
+
`retryAfterSecs`.
|
|
137
|
+
|
|
138
|
+
CommonJS is also supported:
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
const { TrafficWar } = require("@trafficwar/node");
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
MIT
|