@reqray/transport-http 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +59 -2
- package/dist/index.cjs +187 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +51 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.js +162 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ruslan Kryklyvyi
|
|
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
CHANGED
|
@@ -1,3 +1,60 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @reqray/transport-http
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ships Reqray's request records from an application to a collector
|
|
4
|
+
([`@reqray/server`](https://www.npmjs.com/package/@reqray/server)) in batches,
|
|
5
|
+
without ever blocking the application.
|
|
6
|
+
|
|
7
|
+
Part of [Reqray](https://github.com/reqray/reqray) — a dev panel and request inspector for Node.js. It
|
|
8
|
+
instruments HTTP, SQL, outgoing calls and cache on its own, shows every
|
|
9
|
+
request as a waterfall, and finds N+1 down to the file and the line.
|
|
10
|
+
|
|
11
|
+
## Use
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @reqray/transport-http
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { httpTransport } from '@reqray/transport-http';
|
|
19
|
+
|
|
20
|
+
await app.register(reqray, {
|
|
21
|
+
enabled: true,
|
|
22
|
+
mode: 'production',
|
|
23
|
+
panel: false, // the panel lives on the collector
|
|
24
|
+
transport: httpTransport({
|
|
25
|
+
url: 'http://reqray-collector:4300',
|
|
26
|
+
token: process.env.REQRAY_TOKEN,
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## What it promises
|
|
32
|
+
|
|
33
|
+
- **The queue is bounded**, by count and by size. On overflow the oldest
|
|
34
|
+
records are dropped — memory does not grow because a collector is down.
|
|
35
|
+
- **An unreachable collector is a warning in the log** and retries with
|
|
36
|
+
exponential backoff, up to 30 seconds apart.
|
|
37
|
+
- **`429` is honoured**: the transport waits as long as `Retry-After` says.
|
|
38
|
+
|
|
39
|
+
## Options
|
|
40
|
+
|
|
41
|
+
| Option | Default | |
|
|
42
|
+
|---|---|---|
|
|
43
|
+
| `url` | required | The collector's address |
|
|
44
|
+
| `token` | none | Sent with every batch |
|
|
45
|
+
| `batchSize` | `50` | Records per request |
|
|
46
|
+
| `flushIntervalMs` | `2000` | How often a partial batch is sent |
|
|
47
|
+
| `maxQueue` | `1000` | Records held while the collector is away |
|
|
48
|
+
| `maxQueueBytes` | 8 MB | …and their total size |
|
|
49
|
+
| `timeoutMs` | `10000` | Per request to the collector |
|
|
50
|
+
| `retryDelayMs` | `1000` | First retry delay; doubles from there |
|
|
51
|
+
|
|
52
|
+
## More
|
|
53
|
+
|
|
54
|
+
Everything else — the detectors, security defaults, measured overhead and a
|
|
55
|
+
demo that runs with one command — is in the
|
|
56
|
+
[main README](https://github.com/reqray/reqray#readme).
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
|
|
60
|
+
[MIT](https://github.com/reqray/reqray/blob/main/LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
httpTransport: () => httpTransport
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_core = require("@reqray/core");
|
|
27
|
+
var MAX_RETRY_DELAY_MS = 3e4;
|
|
28
|
+
function httpTransport(options) {
|
|
29
|
+
const url = new URL("/ingest", options.url).toString();
|
|
30
|
+
const batchSize = options.batchSize ?? 50;
|
|
31
|
+
const flushIntervalMs = options.flushIntervalMs ?? 2e3;
|
|
32
|
+
const maxQueue = options.maxQueue ?? 1e3;
|
|
33
|
+
const maxQueueBytes = options.maxQueueBytes ?? 8 * 1024 * 1024;
|
|
34
|
+
const timeoutMs = options.timeoutMs ?? 1e4;
|
|
35
|
+
const baseRetryDelayMs = options.retryDelayMs ?? 1e3;
|
|
36
|
+
const queue = [];
|
|
37
|
+
let queuedSize = 0;
|
|
38
|
+
let askedToWaitMs = 0;
|
|
39
|
+
let timer = null;
|
|
40
|
+
let inFlight = false;
|
|
41
|
+
let draining = Promise.resolve();
|
|
42
|
+
let retryDelayMs = 0;
|
|
43
|
+
let pausedUntil = 0;
|
|
44
|
+
let closed = false;
|
|
45
|
+
const headers = { "content-type": "application/json" };
|
|
46
|
+
if (options.token) headers["authorization"] = `Bearer ${options.token}`;
|
|
47
|
+
async function post(batch) {
|
|
48
|
+
try {
|
|
49
|
+
const response = await fetch(url, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
headers,
|
|
52
|
+
// Assembled from pre-serialized records; the shape is IngestPayload.
|
|
53
|
+
body: `{"requests":[${batch.join(",")}]}`,
|
|
54
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
55
|
+
});
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
const seconds = Number(response.headers.get("retry-after"));
|
|
58
|
+
askedToWaitMs = seconds > 0 ? seconds * 1e3 : 0;
|
|
59
|
+
(0, import_core.warnOnce)(
|
|
60
|
+
"transport.http.status",
|
|
61
|
+
`collector answered ${response.status} \u2014 records will be retried and displaced by newer ones`
|
|
62
|
+
);
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
68
|
+
(0, import_core.warnOnce)(
|
|
69
|
+
"transport.http.unreachable",
|
|
70
|
+
`collector unreachable (${reason}) \u2014 the application keeps running, records are displaced by newer ones`
|
|
71
|
+
);
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function take() {
|
|
76
|
+
const batch = queue.splice(0, batchSize);
|
|
77
|
+
for (const record of batch) queuedSize -= record.length;
|
|
78
|
+
return batch;
|
|
79
|
+
}
|
|
80
|
+
function trim() {
|
|
81
|
+
let dropped = false;
|
|
82
|
+
while (queue.length > maxQueue || queuedSize > maxQueueBytes && queue.length > 0) {
|
|
83
|
+
queuedSize -= queue.shift()?.length ?? 0;
|
|
84
|
+
dropped = true;
|
|
85
|
+
}
|
|
86
|
+
if (!dropped) return;
|
|
87
|
+
(0, import_core.warnOnce)(
|
|
88
|
+
"transport.http.overflow",
|
|
89
|
+
`transport queue overflowed, oldest records are being dropped (limits: ${maxQueue} records, ${maxQueueBytes} bytes)`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
function armTimer() {
|
|
93
|
+
if (timer || closed || queue.length === 0) return;
|
|
94
|
+
const delay = Math.max(flushIntervalMs, pausedUntil - performance.now());
|
|
95
|
+
timer = setTimeout(() => {
|
|
96
|
+
timer = null;
|
|
97
|
+
void drain();
|
|
98
|
+
}, delay);
|
|
99
|
+
timer.unref?.();
|
|
100
|
+
}
|
|
101
|
+
async function drain() {
|
|
102
|
+
if (inFlight || closed) return;
|
|
103
|
+
if (performance.now() < pausedUntil) {
|
|
104
|
+
armTimer();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
inFlight = true;
|
|
108
|
+
let finished = () => {
|
|
109
|
+
};
|
|
110
|
+
draining = new Promise((resolve) => {
|
|
111
|
+
finished = resolve;
|
|
112
|
+
});
|
|
113
|
+
try {
|
|
114
|
+
while (queue.length > 0) {
|
|
115
|
+
const batch = take();
|
|
116
|
+
const delivered = await post(batch);
|
|
117
|
+
if (!delivered) {
|
|
118
|
+
queue.unshift(...batch);
|
|
119
|
+
for (const record of batch) queuedSize += record.length;
|
|
120
|
+
trim();
|
|
121
|
+
retryDelayMs = Math.min(
|
|
122
|
+
retryDelayMs > 0 ? retryDelayMs * 2 : baseRetryDelayMs,
|
|
123
|
+
MAX_RETRY_DELAY_MS
|
|
124
|
+
);
|
|
125
|
+
pausedUntil = performance.now() + Math.max(retryDelayMs * (0.5 + Math.random() * 0.5), askedToWaitMs);
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
retryDelayMs = 0;
|
|
129
|
+
pausedUntil = 0;
|
|
130
|
+
if (queue.length < batchSize) break;
|
|
131
|
+
}
|
|
132
|
+
} finally {
|
|
133
|
+
inFlight = false;
|
|
134
|
+
finished();
|
|
135
|
+
}
|
|
136
|
+
armTimer();
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
name: "http",
|
|
140
|
+
send(request) {
|
|
141
|
+
if (closed) return;
|
|
142
|
+
try {
|
|
143
|
+
const record = JSON.stringify(request);
|
|
144
|
+
queue.push(record);
|
|
145
|
+
queuedSize += record.length;
|
|
146
|
+
} catch {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
trim();
|
|
150
|
+
if (queue.length >= batchSize && performance.now() >= pausedUntil) {
|
|
151
|
+
setImmediate(() => void drain());
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
armTimer();
|
|
155
|
+
},
|
|
156
|
+
/**
|
|
157
|
+
* Sends whatever is queued, one attempt per batch.
|
|
158
|
+
*
|
|
159
|
+
* Called on shutdown, so it must not hang on a dead collector: what
|
|
160
|
+
* cannot be delivered right now is dropped.
|
|
161
|
+
*/
|
|
162
|
+
async flush() {
|
|
163
|
+
while (inFlight) await draining;
|
|
164
|
+
if (timer) {
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
timer = null;
|
|
167
|
+
}
|
|
168
|
+
while (queue.length > 0) {
|
|
169
|
+
if (!await post(take())) return;
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
async close() {
|
|
173
|
+
if (closed) return;
|
|
174
|
+
await this.flush();
|
|
175
|
+
closed = true;
|
|
176
|
+
if (timer) {
|
|
177
|
+
clearTimeout(timer);
|
|
178
|
+
timer = null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
184
|
+
0 && (module.exports = {
|
|
185
|
+
httpTransport
|
|
186
|
+
});
|
|
187
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * `@reqray/transport-http` — shipping batches to an external collector.\n *\n * The agent inside the application only collects and sends; storage and the\n * panel live in `@reqray/server`. Two promises this transport keeps no matter\n * what the collector does:\n *\n * - the application is never blocked: `send()` is a queue push, everything\n * else happens on timers;\n * - memory is bounded: the queue has a cap, and beyond it the **oldest**\n * records are dropped — fresh data is worth more than stale data.\n */\n\nimport { type Transport, warnOnce } from '@reqray/core';\nimport type { RecordedRequest } from '@reqray/protocol';\n\n/** Options for {@link httpTransport}. */\nexport interface HttpTransportOptions {\n /** Collector address, e.g. `http://localhost:4318`. */\n readonly url: string;\n /** Shared token; sent as `authorization: Bearer <token>`. */\n readonly token?: string;\n /** How many records go into one batch. */\n readonly batchSize?: number;\n /** How long a record may wait before the batch is sent anyway, ms. */\n readonly flushIntervalMs?: number;\n /** Queue cap; beyond it the oldest records are dropped. */\n readonly maxQueue?: number;\n /**\n * Cap on the serialized size of the queue; beyond it the oldest records\n * are dropped. A count alone bounds nothing: a thousand records with\n * bodies is hundreds of megabytes held for a collector that is down.\n */\n readonly maxQueueBytes?: number;\n /** Timeout of one POST to the collector, ms. */\n readonly timeoutMs?: number;\n /** First retry delay after a failure, ms. Doubles up to 30 s, with jitter. */\n readonly retryDelayMs?: number;\n}\n\n/** The transport with the extra lifecycle methods made non-optional. */\nexport interface HttpTransport extends Transport {\n flush(): Promise<void>;\n close(): Promise<void>;\n}\n\n/** The batch as the collector receives it. */\nexport interface IngestPayload {\n readonly requests: readonly RecordedRequest[];\n}\n\nconst MAX_RETRY_DELAY_MS = 30_000;\n\nexport function httpTransport(options: HttpTransportOptions): HttpTransport {\n const url = new URL('/ingest', options.url).toString();\n const batchSize = options.batchSize ?? 50;\n const flushIntervalMs = options.flushIntervalMs ?? 2_000;\n const maxQueue = options.maxQueue ?? 1_000;\n // shortcut: counted in string length, not bytes — non-ASCII bodies are up\n // to 3x larger on the wire. 8 MB keeps the worst case under the\n // collector's 32 MB batch limit; count bytes if a 400 for size ever shows.\n const maxQueueBytes = options.maxQueueBytes ?? 8 * 1024 * 1024;\n const timeoutMs = options.timeoutMs ?? 10_000;\n const baseRetryDelayMs = options.retryDelayMs ?? 1_000;\n\n // The queue holds records already serialized one by one: serializing a\n // whole batch at flush time is a multi-megabyte synchronous stringify that\n // shows up as event-loop lag spikes. Per-record cost is paid in `send()`,\n // which already runs outside request handling.\n const queue: string[] = [];\n let queuedSize = 0;\n /** What the collector asked for in `Retry-After` on its last refusal, ms. */\n let askedToWaitMs = 0;\n let timer: NodeJS.Timeout | null = null;\n let inFlight = false;\n /** Settles when the drain in progress, if any, has finished its POST. */\n let draining: Promise<void> = Promise.resolve();\n let retryDelayMs = 0;\n /** `performance.now()` before which sending is paused after a failure. */\n let pausedUntil = 0;\n let closed = false;\n\n const headers: Record<string, string> = { 'content-type': 'application/json' };\n if (options.token) headers['authorization'] = `Bearer ${options.token}`;\n\n async function post(batch: readonly string[]): Promise<boolean> {\n try {\n const response = await fetch(url, {\n method: 'POST',\n headers,\n // Assembled from pre-serialized records; the shape is IngestPayload.\n body: `{\"requests\":[${batch.join(',')}]}`,\n signal: AbortSignal.timeout(timeoutMs),\n });\n if (!response.ok) {\n // A saturated collector says how long to stay away; an agent that\n // came back sooner would be part of what saturates it.\n const seconds = Number(response.headers.get('retry-after'));\n askedToWaitMs = seconds > 0 ? seconds * 1000 : 0;\n warnOnce(\n 'transport.http.status',\n `collector answered ${response.status} — records will be retried and displaced by newer ones`,\n );\n return false;\n }\n return true;\n } catch (error) {\n const reason = error instanceof Error ? error.message : String(error);\n warnOnce(\n 'transport.http.unreachable',\n `collector unreachable (${reason}) — the application keeps running, records are displaced by newer ones`,\n );\n return false;\n }\n }\n\n /** Takes the next batch off the front of the queue. */\n function take(): string[] {\n const batch = queue.splice(0, batchSize);\n for (const record of batch) queuedSize -= record.length;\n return batch;\n }\n\n /** Trims the queue to its caps, dropping the oldest records. */\n function trim(): void {\n let dropped = false;\n while (queue.length > maxQueue || (queuedSize > maxQueueBytes && queue.length > 0)) {\n queuedSize -= queue.shift()?.length ?? 0;\n dropped = true;\n }\n if (!dropped) return;\n warnOnce(\n 'transport.http.overflow',\n `transport queue overflowed, oldest records are being dropped (limits: ${maxQueue} records, ${maxQueueBytes} bytes)`,\n );\n }\n\n function armTimer(): void {\n if (timer || closed || queue.length === 0) return;\n const delay = Math.max(flushIntervalMs, pausedUntil - performance.now());\n timer = setTimeout(() => {\n timer = null;\n void drain();\n }, delay);\n // The timer must not keep somebody else's process alive.\n timer.unref?.();\n }\n\n /**\n * Sends full batches while there are any, one POST in flight at a time.\n * A failed batch goes back to the front of the queue: on overflow it is\n * the first to be dropped, which is exactly the drop-oldest promise.\n */\n async function drain(): Promise<void> {\n if (inFlight || closed) return;\n if (performance.now() < pausedUntil) {\n armTimer();\n return;\n }\n\n inFlight = true;\n let finished = (): void => {};\n draining = new Promise((resolve) => {\n finished = resolve;\n });\n try {\n while (queue.length > 0) {\n const batch = take();\n const delivered = await post(batch);\n\n if (!delivered) {\n queue.unshift(...batch);\n for (const record of batch) queuedSize += record.length;\n trim();\n retryDelayMs = Math.min(\n retryDelayMs > 0 ? retryDelayMs * 2 : baseRetryDelayMs,\n MAX_RETRY_DELAY_MS,\n );\n // Jitter keeps a fleet of agents from retrying in lockstep.\n pausedUntil =\n performance.now() + Math.max(retryDelayMs * (0.5 + Math.random() * 0.5), askedToWaitMs);\n break;\n }\n retryDelayMs = 0;\n pausedUntil = 0;\n\n // The tail below batch size waits for the timer, not for the loop.\n if (queue.length < batchSize) break;\n }\n } finally {\n inFlight = false;\n finished();\n }\n armTimer();\n }\n\n return {\n name: 'http',\n\n send(request) {\n if (closed) return;\n try {\n const record = JSON.stringify(request);\n queue.push(record);\n queuedSize += record.length;\n } catch {\n // An unserializable record is dropped; the transport must never throw.\n return;\n }\n trim();\n if (queue.length >= batchSize && performance.now() >= pausedUntil) {\n // Off the caller's stack: `send` stays a queue push.\n setImmediate(() => void drain());\n return;\n }\n armTimer();\n },\n\n /**\n * Sends whatever is queued, one attempt per batch.\n *\n * Called on shutdown, so it must not hang on a dead collector: what\n * cannot be delivered right now is dropped.\n */\n async flush() {\n // A POST already on the wire is part of what is being flushed: returning\n // past it let `close()` finish with that batch undelivered and the tail\n // behind it never sent. Looped, because a `send` may start another.\n while (inFlight) await draining;\n if (timer) {\n clearTimeout(timer);\n timer = null;\n }\n while (queue.length > 0) {\n if (!(await post(take()))) return;\n }\n },\n\n async close() {\n if (closed) return;\n await this.flush();\n closed = true;\n if (timer) {\n clearTimeout(timer);\n timer = null;\n }\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,kBAAyC;AAsCzC,IAAM,qBAAqB;AAEpB,SAAS,cAAc,SAA8C;AAC1E,QAAM,MAAM,IAAI,IAAI,WAAW,QAAQ,GAAG,EAAE,SAAS;AACrD,QAAM,YAAY,QAAQ,aAAa;AACvC,QAAM,kBAAkB,QAAQ,mBAAmB;AACnD,QAAM,WAAW,QAAQ,YAAY;AAIrC,QAAM,gBAAgB,QAAQ,iBAAiB,IAAI,OAAO;AAC1D,QAAM,YAAY,QAAQ,aAAa;AACvC,QAAM,mBAAmB,QAAQ,gBAAgB;AAMjD,QAAM,QAA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ACL,UAAI,MAAM,UAAU,aAAa,YAAY,IAAI,KAAK,aAAa;AAEjE,qBAAa,MAAM,KAAK,MAAM,CAAC;AAC/B;AAAA,MACF;AACA,eAAS;AAAA,IACX;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQA,MAAM,QAAQ;AAIZ,aAAO,SAAU,OAAM;AACvB,UAAI,OAAO;AACT,qBAAa,KAAK;AAClB,gBAAQ;AAAA,MACV;AACA,aAAO,MAAM,SAAS,GAAG;AACvB,YAAI,CAAE,MAAM,KAAK,KAAK,CAAC,EAAI;AAAA,MAC7B;AAAA,IACF;AAAA,IAEA,MAAM,QAAQ;AACZ,UAAI,OAAQ;AACZ,YAAM,KAAK,MAAM;AACjB,eAAS;AACT,UAAI,OAAO;AACT,qBAAa,KAAK;AAClB,gBAAQ;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Transport } from '@reqray/core';
|
|
2
|
+
import { RecordedRequest } from '@reqray/protocol';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `@reqray/transport-http` — shipping batches to an external collector.
|
|
6
|
+
*
|
|
7
|
+
* The agent inside the application only collects and sends; storage and the
|
|
8
|
+
* panel live in `@reqray/server`. Two promises this transport keeps no matter
|
|
9
|
+
* what the collector does:
|
|
10
|
+
*
|
|
11
|
+
* - the application is never blocked: `send()` is a queue push, everything
|
|
12
|
+
* else happens on timers;
|
|
13
|
+
* - memory is bounded: the queue has a cap, and beyond it the **oldest**
|
|
14
|
+
* records are dropped — fresh data is worth more than stale data.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Options for {@link httpTransport}. */
|
|
18
|
+
interface HttpTransportOptions {
|
|
19
|
+
/** Collector address, e.g. `http://localhost:4318`. */
|
|
20
|
+
readonly url: string;
|
|
21
|
+
/** Shared token; sent as `authorization: Bearer <token>`. */
|
|
22
|
+
readonly token?: string;
|
|
23
|
+
/** How many records go into one batch. */
|
|
24
|
+
readonly batchSize?: number;
|
|
25
|
+
/** How long a record may wait before the batch is sent anyway, ms. */
|
|
26
|
+
readonly flushIntervalMs?: number;
|
|
27
|
+
/** Queue cap; beyond it the oldest records are dropped. */
|
|
28
|
+
readonly maxQueue?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Cap on the serialized size of the queue; beyond it the oldest records
|
|
31
|
+
* are dropped. A count alone bounds nothing: a thousand records with
|
|
32
|
+
* bodies is hundreds of megabytes held for a collector that is down.
|
|
33
|
+
*/
|
|
34
|
+
readonly maxQueueBytes?: number;
|
|
35
|
+
/** Timeout of one POST to the collector, ms. */
|
|
36
|
+
readonly timeoutMs?: number;
|
|
37
|
+
/** First retry delay after a failure, ms. Doubles up to 30 s, with jitter. */
|
|
38
|
+
readonly retryDelayMs?: number;
|
|
39
|
+
}
|
|
40
|
+
/** The transport with the extra lifecycle methods made non-optional. */
|
|
41
|
+
interface HttpTransport extends Transport {
|
|
42
|
+
flush(): Promise<void>;
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
/** The batch as the collector receives it. */
|
|
46
|
+
interface IngestPayload {
|
|
47
|
+
readonly requests: readonly RecordedRequest[];
|
|
48
|
+
}
|
|
49
|
+
declare function httpTransport(options: HttpTransportOptions): HttpTransport;
|
|
50
|
+
|
|
51
|
+
export { type HttpTransport, type HttpTransportOptions, type IngestPayload, httpTransport };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Transport } from '@reqray/core';
|
|
2
|
+
import { RecordedRequest } from '@reqray/protocol';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `@reqray/transport-http` — shipping batches to an external collector.
|
|
6
|
+
*
|
|
7
|
+
* The agent inside the application only collects and sends; storage and the
|
|
8
|
+
* panel live in `@reqray/server`. Two promises this transport keeps no matter
|
|
9
|
+
* what the collector does:
|
|
10
|
+
*
|
|
11
|
+
* - the application is never blocked: `send()` is a queue push, everything
|
|
12
|
+
* else happens on timers;
|
|
13
|
+
* - memory is bounded: the queue has a cap, and beyond it the **oldest**
|
|
14
|
+
* records are dropped — fresh data is worth more than stale data.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Options for {@link httpTransport}. */
|
|
18
|
+
interface HttpTransportOptions {
|
|
19
|
+
/** Collector address, e.g. `http://localhost:4318`. */
|
|
20
|
+
readonly url: string;
|
|
21
|
+
/** Shared token; sent as `authorization: Bearer <token>`. */
|
|
22
|
+
readonly token?: string;
|
|
23
|
+
/** How many records go into one batch. */
|
|
24
|
+
readonly batchSize?: number;
|
|
25
|
+
/** How long a record may wait before the batch is sent anyway, ms. */
|
|
26
|
+
readonly flushIntervalMs?: number;
|
|
27
|
+
/** Queue cap; beyond it the oldest records are dropped. */
|
|
28
|
+
readonly maxQueue?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Cap on the serialized size of the queue; beyond it the oldest records
|
|
31
|
+
* are dropped. A count alone bounds nothing: a thousand records with
|
|
32
|
+
* bodies is hundreds of megabytes held for a collector that is down.
|
|
33
|
+
*/
|
|
34
|
+
readonly maxQueueBytes?: number;
|
|
35
|
+
/** Timeout of one POST to the collector, ms. */
|
|
36
|
+
readonly timeoutMs?: number;
|
|
37
|
+
/** First retry delay after a failure, ms. Doubles up to 30 s, with jitter. */
|
|
38
|
+
readonly retryDelayMs?: number;
|
|
39
|
+
}
|
|
40
|
+
/** The transport with the extra lifecycle methods made non-optional. */
|
|
41
|
+
interface HttpTransport extends Transport {
|
|
42
|
+
flush(): Promise<void>;
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
/** The batch as the collector receives it. */
|
|
46
|
+
interface IngestPayload {
|
|
47
|
+
readonly requests: readonly RecordedRequest[];
|
|
48
|
+
}
|
|
49
|
+
declare function httpTransport(options: HttpTransportOptions): HttpTransport;
|
|
50
|
+
|
|
51
|
+
export { type HttpTransport, type HttpTransportOptions, type IngestPayload, httpTransport };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { warnOnce } from "@reqray/core";
|
|
3
|
+
var MAX_RETRY_DELAY_MS = 3e4;
|
|
4
|
+
function httpTransport(options) {
|
|
5
|
+
const url = new URL("/ingest", options.url).toString();
|
|
6
|
+
const batchSize = options.batchSize ?? 50;
|
|
7
|
+
const flushIntervalMs = options.flushIntervalMs ?? 2e3;
|
|
8
|
+
const maxQueue = options.maxQueue ?? 1e3;
|
|
9
|
+
const maxQueueBytes = options.maxQueueBytes ?? 8 * 1024 * 1024;
|
|
10
|
+
const timeoutMs = options.timeoutMs ?? 1e4;
|
|
11
|
+
const baseRetryDelayMs = options.retryDelayMs ?? 1e3;
|
|
12
|
+
const queue = [];
|
|
13
|
+
let queuedSize = 0;
|
|
14
|
+
let askedToWaitMs = 0;
|
|
15
|
+
let timer = null;
|
|
16
|
+
let inFlight = false;
|
|
17
|
+
let draining = Promise.resolve();
|
|
18
|
+
let retryDelayMs = 0;
|
|
19
|
+
let pausedUntil = 0;
|
|
20
|
+
let closed = false;
|
|
21
|
+
const headers = { "content-type": "application/json" };
|
|
22
|
+
if (options.token) headers["authorization"] = `Bearer ${options.token}`;
|
|
23
|
+
async function post(batch) {
|
|
24
|
+
try {
|
|
25
|
+
const response = await fetch(url, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
headers,
|
|
28
|
+
// Assembled from pre-serialized records; the shape is IngestPayload.
|
|
29
|
+
body: `{"requests":[${batch.join(",")}]}`,
|
|
30
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
31
|
+
});
|
|
32
|
+
if (!response.ok) {
|
|
33
|
+
const seconds = Number(response.headers.get("retry-after"));
|
|
34
|
+
askedToWaitMs = seconds > 0 ? seconds * 1e3 : 0;
|
|
35
|
+
warnOnce(
|
|
36
|
+
"transport.http.status",
|
|
37
|
+
`collector answered ${response.status} \u2014 records will be retried and displaced by newer ones`
|
|
38
|
+
);
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
44
|
+
warnOnce(
|
|
45
|
+
"transport.http.unreachable",
|
|
46
|
+
`collector unreachable (${reason}) \u2014 the application keeps running, records are displaced by newer ones`
|
|
47
|
+
);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function take() {
|
|
52
|
+
const batch = queue.splice(0, batchSize);
|
|
53
|
+
for (const record of batch) queuedSize -= record.length;
|
|
54
|
+
return batch;
|
|
55
|
+
}
|
|
56
|
+
function trim() {
|
|
57
|
+
let dropped = false;
|
|
58
|
+
while (queue.length > maxQueue || queuedSize > maxQueueBytes && queue.length > 0) {
|
|
59
|
+
queuedSize -= queue.shift()?.length ?? 0;
|
|
60
|
+
dropped = true;
|
|
61
|
+
}
|
|
62
|
+
if (!dropped) return;
|
|
63
|
+
warnOnce(
|
|
64
|
+
"transport.http.overflow",
|
|
65
|
+
`transport queue overflowed, oldest records are being dropped (limits: ${maxQueue} records, ${maxQueueBytes} bytes)`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
function armTimer() {
|
|
69
|
+
if (timer || closed || queue.length === 0) return;
|
|
70
|
+
const delay = Math.max(flushIntervalMs, pausedUntil - performance.now());
|
|
71
|
+
timer = setTimeout(() => {
|
|
72
|
+
timer = null;
|
|
73
|
+
void drain();
|
|
74
|
+
}, delay);
|
|
75
|
+
timer.unref?.();
|
|
76
|
+
}
|
|
77
|
+
async function drain() {
|
|
78
|
+
if (inFlight || closed) return;
|
|
79
|
+
if (performance.now() < pausedUntil) {
|
|
80
|
+
armTimer();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
inFlight = true;
|
|
84
|
+
let finished = () => {
|
|
85
|
+
};
|
|
86
|
+
draining = new Promise((resolve) => {
|
|
87
|
+
finished = resolve;
|
|
88
|
+
});
|
|
89
|
+
try {
|
|
90
|
+
while (queue.length > 0) {
|
|
91
|
+
const batch = take();
|
|
92
|
+
const delivered = await post(batch);
|
|
93
|
+
if (!delivered) {
|
|
94
|
+
queue.unshift(...batch);
|
|
95
|
+
for (const record of batch) queuedSize += record.length;
|
|
96
|
+
trim();
|
|
97
|
+
retryDelayMs = Math.min(
|
|
98
|
+
retryDelayMs > 0 ? retryDelayMs * 2 : baseRetryDelayMs,
|
|
99
|
+
MAX_RETRY_DELAY_MS
|
|
100
|
+
);
|
|
101
|
+
pausedUntil = performance.now() + Math.max(retryDelayMs * (0.5 + Math.random() * 0.5), askedToWaitMs);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
retryDelayMs = 0;
|
|
105
|
+
pausedUntil = 0;
|
|
106
|
+
if (queue.length < batchSize) break;
|
|
107
|
+
}
|
|
108
|
+
} finally {
|
|
109
|
+
inFlight = false;
|
|
110
|
+
finished();
|
|
111
|
+
}
|
|
112
|
+
armTimer();
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
name: "http",
|
|
116
|
+
send(request) {
|
|
117
|
+
if (closed) return;
|
|
118
|
+
try {
|
|
119
|
+
const record = JSON.stringify(request);
|
|
120
|
+
queue.push(record);
|
|
121
|
+
queuedSize += record.length;
|
|
122
|
+
} catch {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
trim();
|
|
126
|
+
if (queue.length >= batchSize && performance.now() >= pausedUntil) {
|
|
127
|
+
setImmediate(() => void drain());
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
armTimer();
|
|
131
|
+
},
|
|
132
|
+
/**
|
|
133
|
+
* Sends whatever is queued, one attempt per batch.
|
|
134
|
+
*
|
|
135
|
+
* Called on shutdown, so it must not hang on a dead collector: what
|
|
136
|
+
* cannot be delivered right now is dropped.
|
|
137
|
+
*/
|
|
138
|
+
async flush() {
|
|
139
|
+
while (inFlight) await draining;
|
|
140
|
+
if (timer) {
|
|
141
|
+
clearTimeout(timer);
|
|
142
|
+
timer = null;
|
|
143
|
+
}
|
|
144
|
+
while (queue.length > 0) {
|
|
145
|
+
if (!await post(take())) return;
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
async close() {
|
|
149
|
+
if (closed) return;
|
|
150
|
+
await this.flush();
|
|
151
|
+
closed = true;
|
|
152
|
+
if (timer) {
|
|
153
|
+
clearTimeout(timer);
|
|
154
|
+
timer = null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
export {
|
|
160
|
+
httpTransport
|
|
161
|
+
};
|
|
162
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * `@reqray/transport-http` — shipping batches to an external collector.\n *\n * The agent inside the application only collects and sends; storage and the\n * panel live in `@reqray/server`. Two promises this transport keeps no matter\n * what the collector does:\n *\n * - the application is never blocked: `send()` is a queue push, everything\n * else happens on timers;\n * - memory is bounded: the queue has a cap, and beyond it the **oldest**\n * records are dropped — fresh data is worth more than stale data.\n */\n\nimport { type Transport, warnOnce } from '@reqray/core';\nimport type { RecordedRequest } from '@reqray/protocol';\n\n/** Options for {@link httpTransport}. */\nexport interface HttpTransportOptions {\n /** Collector address, e.g. `http://localhost:4318`. */\n readonly url: string;\n /** Shared token; sent as `authorization: Bearer <token>`. */\n readonly token?: string;\n /** How many records go into one batch. */\n readonly batchSize?: number;\n /** How long a record may wait before the batch is sent anyway, ms. */\n readonly flushIntervalMs?: number;\n /** Queue cap; beyond it the oldest records are dropped. */\n readonly maxQueue?: number;\n /**\n * Cap on the serialized size of the queue; beyond it the oldest records\n * are dropped. A count alone bounds nothing: a thousand records with\n * bodies is hundreds of megabytes held for a collector that is down.\n */\n readonly maxQueueBytes?: number;\n /** Timeout of one POST to the collector, ms. */\n readonly timeoutMs?: number;\n /** First retry delay after a failure, ms. Doubles up to 30 s, with jitter. */\n readonly retryDelayMs?: number;\n}\n\n/** The transport with the extra lifecycle methods made non-optional. */\nexport interface HttpTransport extends Transport {\n flush(): Promise<void>;\n close(): Promise<void>;\n}\n\n/** The batch as the collector receives it. */\nexport interface IngestPayload {\n readonly requests: readonly RecordedRequest[];\n}\n\nconst MAX_RETRY_DELAY_MS = 30_000;\n\nexport function httpTransport(options: HttpTransportOptions): HttpTransport {\n const url = new URL('/ingest', options.url).toString();\n const batchSize = options.batchSize ?? 50;\n const flushIntervalMs = options.flushIntervalMs ?? 2_000;\n const maxQueue = options.maxQueue ?? 1_000;\n // shortcut: counted in string length, not bytes — non-ASCII bodies are up\n // to 3x larger on the wire. 8 MB keeps the worst case under the\n // collector's 32 MB batch limit; count bytes if a 400 for size ever shows.\n const maxQueueBytes = options.maxQueueBytes ?? 8 * 1024 * 1024;\n const timeoutMs = options.timeoutMs ?? 10_000;\n const baseRetryDelayMs = options.retryDelayMs ?? 1_000;\n\n // The queue holds records already serialized one by one: serializing a\n // whole batch at flush time is a multi-megabyte synchronous stringify that\n // shows up as event-loop lag spikes. Per-record cost is paid in `send()`,\n // which already runs outside request handling.\n const queue: string[] = [];\n let queuedSize = 0;\n /** What the collector asked for in `Retry-After` on its last refusal, ms. */\n let askedToWaitMs = 0;\n let timer: NodeJS.Timeout | null = null;\n let inFlight = false;\n /** Settles when the drain in progress, if any, has finished its POST. */\n let draining: Promise<void> = Promise.resolve();\n let retryDelayMs = 0;\n /** `performance.now()` before which sending is paused after a failure. */\n let pausedUntil = 0;\n let closed = false;\n\n const headers: Record<string, string> = { 'content-type': 'application/json' };\n if (options.token) headers['authorization'] = `Bearer ${options.token}`;\n\n async function post(batch: readonly string[]): Promise<boolean> {\n try {\n const response = await fetch(url, {\n method: 'POST',\n headers,\n // Assembled from pre-serialized records; the shape is IngestPayload.\n body: `{\"requests\":[${batch.join(',')}]}`,\n signal: AbortSignal.timeout(timeoutMs),\n });\n if (!response.ok) {\n // A saturated collector says how long to stay away; an agent that\n // came back sooner would be part of what saturates it.\n const seconds = Number(response.headers.get('retry-after'));\n askedToWaitMs = seconds > 0 ? seconds * 1000 : 0;\n warnOnce(\n 'transport.http.status',\n `collector answered ${response.status} — records will be retried and displaced by newer ones`,\n );\n return false;\n }\n return true;\n } catch (error) {\n const reason = error instanceof Error ? error.message : String(error);\n warnOnce(\n 'transport.http.unreachable',\n `collector unreachable (${reason}) — the application keeps running, records are displaced by newer ones`,\n );\n return false;\n }\n }\n\n /** Takes the next batch off the front of the queue. */\n function take(): string[] {\n const batch = queue.splice(0, batchSize);\n for (const record of batch) queuedSize -= record.length;\n return batch;\n }\n\n /** Trims the queue to its caps, dropping the oldest records. */\n function trim(): void {\n let dropped = false;\n while (queue.length > maxQueue || (queuedSize > maxQueueBytes && queue.length > 0)) {\n queuedSize -= queue.shift()?.length ?? 0;\n dropped = true;\n }\n if (!dropped) return;\n warnOnce(\n 'transport.http.overflow',\n `transport queue overflowed, oldest records are being dropped (limits: ${maxQueue} records, ${maxQueueBytes} bytes)`,\n );\n }\n\n function armTimer(): void {\n if (timer || closed || queue.length === 0) return;\n const delay = Math.max(flushIntervalMs, pausedUntil - performance.now());\n timer = setTimeout(() => {\n timer = null;\n void drain();\n }, delay);\n // The timer must not keep somebody else's process alive.\n timer.unref?.();\n }\n\n /**\n * Sends full batches while there are any, one POST in flight at a time.\n * A failed batch goes back to the front of the queue: on overflow it is\n * the first to be dropped, which is exactly the drop-oldest promise.\n */\n async function drain(): Promise<void> {\n if (inFlight || closed) return;\n if (performance.now() < pausedUntil) {\n armTimer();\n return;\n }\n\n inFlight = true;\n let finished = (): void => {};\n draining = new Promise((resolve) => {\n finished = resolve;\n });\n try {\n while (queue.length > 0) {\n const batch = take();\n const delivered = await post(batch);\n\n if (!delivered) {\n queue.unshift(...batch);\n for (const record of batch) queuedSize += record.length;\n trim();\n retryDelayMs = Math.min(\n retryDelayMs > 0 ? retryDelayMs * 2 : baseRetryDelayMs,\n MAX_RETRY_DELAY_MS,\n );\n // Jitter keeps a fleet of agents from retrying in lockstep.\n pausedUntil =\n performance.now() + Math.max(retryDelayMs * (0.5 + Math.random() * 0.5), askedToWaitMs);\n break;\n }\n retryDelayMs = 0;\n pausedUntil = 0;\n\n // The tail below batch size waits for the timer, not for the loop.\n if (queue.length < batchSize) break;\n }\n } finally {\n inFlight = false;\n finished();\n }\n armTimer();\n }\n\n return {\n name: 'http',\n\n send(request) {\n if (closed) return;\n try {\n const record = JSON.stringify(request);\n queue.push(record);\n queuedSize += record.length;\n } catch {\n // An unserializable record is dropped; the transport must never throw.\n return;\n }\n trim();\n if (queue.length >= batchSize && performance.now() >= pausedUntil) {\n // Off the caller's stack: `send` stays a queue push.\n setImmediate(() => void drain());\n return;\n }\n armTimer();\n },\n\n /**\n * Sends whatever is queued, one attempt per batch.\n *\n * Called on shutdown, so it must not hang on a dead collector: what\n * cannot be delivered right now is dropped.\n */\n async flush() {\n // A POST already on the wire is part of what is being flushed: returning\n // past it let `close()` finish with that batch undelivered and the tail\n // behind it never sent. Looped, because a `send` may start another.\n while (inFlight) await draining;\n if (timer) {\n clearTimeout(timer);\n timer = null;\n }\n while (queue.length > 0) {\n if (!(await post(take()))) return;\n }\n },\n\n async close() {\n if (closed) return;\n await this.flush();\n closed = true;\n if (timer) {\n clearTimeout(timer);\n timer = null;\n }\n },\n };\n}\n"],"mappings":";AAaA,SAAyB,gBAAgB;AAsCzC,IAAM,qBAAqB;AAEpB,SAAS,cAAc,SAA8C;AAC1E,QAAM,MAAM,IAAI,IAAI,WAAW,QAAQ,GAAG,EAAE,SAAS;AACrD,QAAM,YAAY,QAAQ,aAAa;AACvC,QAAM,kBAAkB,QAAQ,mBAAmB;AACnD,QAAM,WAAW,QAAQ,YAAY;AAIrC,QAAM,gBAAgB,QAAQ,iBAAiB,IAAI,OAAO;AAC1D,QAAM,YAAY,QAAQ,aAAa;AACvC,QAAM,mBAAmB,QAAQ,gBAAgB;AAMjD,QAAM,QAAkB,CAAC;AACzB,MAAI,aAAa;AAEjB,M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AI,KAAK,aAAa;AAEjE,qBAAa,MAAM,KAAK,MAAM,CAAC;AAC/B;AAAA,MACF;AACA,eAAS;AAAA,IACX;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAQA,MAAM,QAAQ;AAIZ,aAAO,SAAU,OAAM;AACvB,UAAI,OAAO;AACT,qBAAa,KAAK;AAClB,gBAAQ;AAAA,MACV;AACA,aAAO,MAAM,SAAS,GAAG;AACvB,YAAI,CAAE,MAAM,KAAK,KAAK,CAAC,EAAI;AAAA,MAC7B;AAAA,IACF;AAAA,IAEA,MAAM,QAAQ;AACZ,UAAI,OAAQ;AACZ,YAAM,KAAK,MAAM;AACjB,eAAS;AACT,UAAI,OAAO;AACT,qBAAa,KAAK;AAClB,gBAAQ;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reqray/transport-http",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Reqray HTTP transport: batches with backpressure to an external collector.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"nodejs",
|
|
8
|
+
"debugging",
|
|
9
|
+
"profiling",
|
|
10
|
+
"n+1",
|
|
11
|
+
"sql",
|
|
12
|
+
"observability",
|
|
13
|
+
"devtools",
|
|
14
|
+
"fastify",
|
|
15
|
+
"express",
|
|
16
|
+
"nestjs"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://github.com/reqray/reqray#readme",
|
|
19
|
+
"bugs": "https://github.com/reqray/reqray/issues",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/reqray/reqray.git",
|
|
23
|
+
"directory": "packages/transport-http"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20"
|
|
28
|
+
},
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/index.d.cts",
|
|
37
|
+
"default": "./dist/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"main": "./dist/index.cjs",
|
|
42
|
+
"module": "./dist/index.js",
|
|
43
|
+
"types": "./dist/index.d.ts",
|
|
44
|
+
"files": [
|
|
45
|
+
"dist"
|
|
46
|
+
],
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@reqray/core": "^0.1.1",
|
|
52
|
+
"@reqray/protocol": "^0.1.1"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/node": "^26.2.0",
|
|
56
|
+
"tsup": "^8.5.1",
|
|
57
|
+
"typescript": "^6.0.3"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsup",
|
|
61
|
+
"typecheck": "tsc --noEmit"
|
|
62
|
+
}
|
|
6
63
|
}
|