@reqray/store-sqlite 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 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,59 @@
1
- # Temporary Holding Version
1
+ # @reqray/store-sqlite
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A request history for Reqray that outlives the process. By default Reqray keeps
4
+ the last 500 requests in memory — right for a dev panel, wrong for "what
5
+ happened an hour ago". This store keeps them in a SQLite file.
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/store-sqlite
15
+ ```
16
+
17
+ ```js
18
+ import { sqliteTransport } from '@reqray/store-sqlite';
19
+
20
+ await app.register(reqray, {
21
+ transport: sqliteTransport({ file: '.reqray/requests.db' }),
22
+ });
23
+ ```
24
+
25
+ The panel then pages by cursor instead of loading everything, and filters go
26
+ to the store — "only the errors" means every error there is, not every error
27
+ on screen.
28
+
29
+ Node 22.5 or newer, where SQLite is built in. Nothing is compiled at install
30
+ time.
31
+
32
+ ## Options
33
+
34
+ | Option | Default | |
35
+ |---|---|---|
36
+ | `file` | required | Path to the database file |
37
+ | `maxRequests` | `20000` | Oldest requests are evicted past this |
38
+ | `maxBytes` | 256 MB | …or past this, whichever runs out first |
39
+ | `pinLimit` | `50` | Pinned requests are exempt from eviction, so they get a bound of their own |
40
+ | `retention` | none | A shared `retentionPolicy({ days })`, so the store and a log on disk evict by one number |
41
+
42
+ ```js
43
+ import { retentionPolicy, sqliteTransport } from '@reqray/store-sqlite';
44
+
45
+ const retention = retentionPolicy({ days: 7, file: '.reqray/retention.json' });
46
+ sqliteTransport({ file: '.reqray/requests.db', retention });
47
+ ```
48
+
49
+ The collector takes the same store: `REQRAY_DB=/var/lib/reqray/requests.db`.
50
+
51
+ ## More
52
+
53
+ Everything else — the detectors, security defaults, measured overhead and a
54
+ demo that runs with one command — is in the
55
+ [main README](https://github.com/reqray/reqray#readme).
56
+
57
+ ## License
58
+
59
+ [MIT](https://github.com/reqray/reqray/blob/main/LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,542 @@
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
+ retentionPolicy: () => import_core2.retentionPolicy,
24
+ sqliteTransport: () => sqliteTransport
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_core = require("@reqray/core");
28
+ var import_panel = require("@reqray/core/panel");
29
+
30
+ // src/query.ts
31
+ function likeArgument(needle) {
32
+ return `%${needle.replace(/[\\%_]/g, (char) => `\\${char}`)}%`;
33
+ }
34
+ function whereFor(filter) {
35
+ const clauses = ["1 = 1"];
36
+ const params = [];
37
+ if (filter.from !== void 0) {
38
+ clauses.push("started_at >= ?");
39
+ params.push(filter.from);
40
+ }
41
+ if (filter.to !== void 0) {
42
+ clauses.push("started_at <= ?");
43
+ params.push(filter.to);
44
+ }
45
+ const needle = filter.query?.trim().toLowerCase();
46
+ if (needle) {
47
+ clauses.push(
48
+ "(lower(method) LIKE ? ESCAPE '\\' OR lower(path) LIKE ? ESCAPE '\\' OR CAST(status AS TEXT) LIKE ? ESCAPE '\\' OR (query_string IS NOT NULL AND lower(query_string) LIKE ? ESCAPE '\\'))"
49
+ );
50
+ const like = likeArgument(needle);
51
+ params.push(like, like, like, like);
52
+ }
53
+ const methods = filter.methods?.filter(Boolean) ?? [];
54
+ if (methods.length > 0) {
55
+ clauses.push(`upper(method) IN (${methods.map(() => "?").join(", ")})`);
56
+ params.push(...methods.map((method) => method.toUpperCase()));
57
+ }
58
+ const classes = filter.statusClasses?.filter((value) => Number.isFinite(value)) ?? [];
59
+ if (classes.length > 0) {
60
+ clauses.push(`(status / 100) IN (${classes.map(() => "?").join(", ")})`);
61
+ params.push(...classes.map((value) => Math.floor(value)));
62
+ }
63
+ if (filter.minDurationMs !== void 0) {
64
+ clauses.push("duration_ms >= ?");
65
+ params.push(filter.minDurationMs);
66
+ }
67
+ if (filter.path) {
68
+ clauses.push("lower(path) LIKE ? ESCAPE '\\'");
69
+ params.push(likeArgument(filter.path.toLowerCase()));
70
+ }
71
+ if (filter.status !== void 0) {
72
+ clauses.push("status = ?");
73
+ params.push(filter.status);
74
+ }
75
+ if (filter.minSql !== void 0) {
76
+ clauses.push("sql_count >= ?");
77
+ params.push(filter.minSql);
78
+ }
79
+ const flagged = filter.flagged?.filter(Boolean) ?? [];
80
+ if (flagged.length > 0) {
81
+ clauses.push(`flag IN (${flagged.map(() => "?").join(", ")})`);
82
+ params.push(...flagged);
83
+ }
84
+ if (filter.test !== void 0) {
85
+ clauses.push("test IS NOT NULL AND lower(test) LIKE ? ESCAPE '\\'");
86
+ params.push(likeArgument(filter.test.toLowerCase()));
87
+ }
88
+ if (filter.onlyFindings) clauses.push("finding_count > 0");
89
+ if (filter.onlyNPlusOne) clauses.push("n_plus_one = 1");
90
+ if (filter.onlyTests) clauses.push("test IS NOT NULL");
91
+ if (filter.onlyErrors) clauses.push("has_error = 1");
92
+ if (filter.route) {
93
+ clauses.push("route = ? AND upper(method) = ?");
94
+ params.push(filter.route.route, filter.route.method.toUpperCase());
95
+ }
96
+ return { sql: clauses.join(" AND "), params };
97
+ }
98
+ function orderColumn(sort) {
99
+ return sort === "slowest" ? "duration_ms" : "started_at";
100
+ }
101
+ function afterCursor(cursor, column = "started_at") {
102
+ return {
103
+ sql: `(${column} < ? OR (${column} = ? AND id < ?))`,
104
+ params: [cursor.key, cursor.key, cursor.id]
105
+ };
106
+ }
107
+
108
+ // src/index.ts
109
+ var import_core2 = require("@reqray/core");
110
+ var AGE_SWEEP_MS = 6e4;
111
+ var FINDINGS_LIMIT = 5e3;
112
+ var DEFAULT_MAX_REQUESTS = 2e4;
113
+ var DEFAULT_MAX_BYTES = 256 * 1024 * 1024;
114
+ var DEFAULT_PIN_LIMIT = 50;
115
+ var DEFAULT_LIST_LIMIT = 1e3;
116
+ var TIME_SERIES_LIMIT = 5e4;
117
+ var BASELINE_LIMIT = 1e4;
118
+ var HISTORY_LIMIT = 5e4;
119
+ var SLOW_MS = 500;
120
+ var SCHEMA = `
121
+ CREATE TABLE IF NOT EXISTS requests (
122
+ id TEXT PRIMARY KEY,
123
+ started_at INTEGER NOT NULL,
124
+ method TEXT NOT NULL,
125
+ path TEXT NOT NULL,
126
+ query_string TEXT,
127
+ route TEXT NOT NULL,
128
+ status INTEGER NOT NULL,
129
+ duration_ms REAL NOT NULL,
130
+ sql_count INTEGER NOT NULL,
131
+ http_count INTEGER NOT NULL,
132
+ n_plus_one INTEGER NOT NULL,
133
+ has_error INTEGER NOT NULL,
134
+ finding_count INTEGER NOT NULL,
135
+ flag TEXT,
136
+ test TEXT,
137
+ loop_delay_ms REAL,
138
+ sql_ms REAL,
139
+ self_ms REAL,
140
+ bytes_out INTEGER,
141
+ cpu_ms REAL,
142
+ cpu_share REAL,
143
+ pinned INTEGER NOT NULL DEFAULT 0,
144
+ bytes INTEGER NOT NULL,
145
+ payload TEXT NOT NULL
146
+ );
147
+ CREATE INDEX IF NOT EXISTS requests_by_time ON requests (started_at DESC, id DESC);
148
+ CREATE INDEX IF NOT EXISTS requests_pinned ON requests (pinned) WHERE pinned = 1;
149
+ CREATE INDEX IF NOT EXISTS requests_by_duration ON requests (duration_ms DESC, id DESC);
150
+ CREATE INDEX IF NOT EXISTS requests_by_route ON requests (route, method, started_at DESC);
151
+ CREATE TABLE IF NOT EXISTS finding_states (
152
+ id TEXT PRIMARY KEY,
153
+ state TEXT NOT NULL,
154
+ updated_at INTEGER NOT NULL
155
+ );
156
+ `;
157
+ function toRequestRow(stored) {
158
+ return {
159
+ id: stored.id,
160
+ method: stored.method,
161
+ path: stored.path,
162
+ query: stored.query_string,
163
+ route: stored.route,
164
+ status: stored.status,
165
+ durationMs: stored.duration_ms,
166
+ startedAt: stored.started_at,
167
+ sqlCount: stored.sql_count,
168
+ httpCount: stored.http_count,
169
+ nPlusOne: stored.n_plus_one === 1,
170
+ hasError: stored.has_error === 1,
171
+ findingCount: stored.finding_count,
172
+ flag: stored.flag,
173
+ test: stored.test,
174
+ cpuMs: stored.cpu_ms,
175
+ cpuShare: stored.cpu_share,
176
+ pinned: stored.pinned === 1
177
+ };
178
+ }
179
+ function addMissingColumns(db) {
180
+ const existing = new Set(
181
+ db.prepare("PRAGMA table_info(requests)").all().map((row) => row["name"])
182
+ );
183
+ const columns = [
184
+ ["loop_delay_ms", "REAL"],
185
+ // Where the time went and what came back. In the payload since stages 38
186
+ // and 40, but a history screen cannot read a week of payloads to find
187
+ // out, so they are columns from here on.
188
+ ["sql_ms", "REAL"],
189
+ ["self_ms", "REAL"],
190
+ // Bytes sent to the client. Distinct from `bytes`, which is what the
191
+ // stored record weighs and what the retention budget counts.
192
+ ["bytes_out", "INTEGER"],
193
+ // What the request cost to run. A column rather than a payload field for
194
+ // the same reason as the two above: the list shows it on every row, and
195
+ // reading a week of payloads to draw a column is not a list.
196
+ ["cpu_ms", "REAL"],
197
+ ["cpu_share", "REAL"]
198
+ ];
199
+ for (const [name, definition] of columns) {
200
+ if (existing.has(name)) continue;
201
+ db.exec(`ALTER TABLE requests ADD COLUMN ${name} ${definition}`);
202
+ }
203
+ }
204
+ function openDatabase(file) {
205
+ const lookup = process.getBuiltinModule;
206
+ const sqlite = lookup?.call(process, "node:sqlite");
207
+ if (!sqlite?.DatabaseSync) {
208
+ throw new Error(
209
+ `@reqray/store-sqlite needs Node 22.5 or newer for the built-in SQLite (this is ${process.version}). Keep the in-memory transport, or upgrade Node.`
210
+ );
211
+ }
212
+ return new sqlite.DatabaseSync(file);
213
+ }
214
+ function sqliteTransport(options) {
215
+ const own = {
216
+ maxRequests: options.maxRequests ?? DEFAULT_MAX_REQUESTS,
217
+ maxBytes: options.maxBytes ?? DEFAULT_MAX_BYTES
218
+ };
219
+ const limits = () => {
220
+ const shared = options.retention?.current();
221
+ return {
222
+ maxRequests: shared?.maxRequests ?? own.maxRequests,
223
+ maxBytes: shared?.maxBytes ?? own.maxBytes,
224
+ days: shared?.days ?? null
225
+ };
226
+ };
227
+ const pinLimit = options.pinLimit ?? DEFAULT_PIN_LIMIT;
228
+ const listLimit = options.listLimit ?? DEFAULT_LIST_LIMIT;
229
+ const db = openDatabase(options.file);
230
+ db.exec("PRAGMA journal_mode = WAL");
231
+ db.exec("PRAGMA synchronous = NORMAL");
232
+ db.exec(SCHEMA);
233
+ addMissingColumns(db);
234
+ const insert = db.prepare(`
235
+ INSERT INTO requests (
236
+ id, started_at, method, path, query_string, route, status, duration_ms,
237
+ sql_count, http_count, n_plus_one, has_error, finding_count, flag, test,
238
+ loop_delay_ms, sql_ms, self_ms, bytes_out, cpu_ms, cpu_share,
239
+ pinned, bytes, payload
240
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?)
241
+ ON CONFLICT(id) DO NOTHING
242
+ `);
243
+ const evict = db.prepare(`
244
+ DELETE FROM requests WHERE id IN (
245
+ SELECT id FROM (
246
+ SELECT id,
247
+ SUM(bytes) OVER (ORDER BY started_at DESC, id DESC) AS running,
248
+ ROW_NUMBER() OVER (ORDER BY started_at DESC, id DESC) AS position
249
+ FROM requests WHERE pinned = 0
250
+ ) WHERE running > ? OR position > ?
251
+ )
252
+ `);
253
+ const evictOld = db.prepare("DELETE FROM requests WHERE pinned = 0 AND started_at < ?");
254
+ const totals = db.prepare(
255
+ "SELECT COUNT(*) AS size, COALESCE(SUM(bytes), 0) AS bytes, MIN(started_at) AS oldest FROM requests"
256
+ );
257
+ const evictable = db.prepare(
258
+ "SELECT COUNT(*) AS size, COALESCE(SUM(bytes), 0) AS bytes FROM requests WHERE pinned = 0"
259
+ );
260
+ const byId = db.prepare("SELECT payload FROM requests WHERE id = ?");
261
+ const recent = db.prepare(
262
+ "SELECT payload FROM requests ORDER BY started_at DESC, id DESC LIMIT ?"
263
+ );
264
+ const routeDurations = db.prepare(
265
+ `SELECT duration_ms, started_at, has_error, loop_delay_ms FROM requests
266
+ WHERE route = ? AND method = ? AND id <> ? AND started_at >= ? AND started_at <= ?
267
+ ORDER BY started_at DESC LIMIT ?`
268
+ );
269
+ const routeCounts = db.prepare(
270
+ `SELECT method, route, COUNT(*) AS calls, AVG(duration_ms) AS avg_ms
271
+ FROM requests WHERE started_at >= ? AND started_at <= ?
272
+ GROUP BY method, route`
273
+ );
274
+ const series = db.prepare(
275
+ "SELECT started_at, duration_ms, has_error FROM requests ORDER BY started_at DESC LIMIT ?"
276
+ );
277
+ const pinnedIds = db.prepare("SELECT id FROM requests WHERE pinned = 1 ORDER BY started_at DESC");
278
+ const countPinned = db.prepare("SELECT COUNT(*) AS pinned FROM requests WHERE pinned = 1");
279
+ const setPinned = db.prepare("UPDATE requests SET pinned = ? WHERE id = ?");
280
+ const removeAll = db.prepare("DELETE FROM requests");
281
+ const prepared = /* @__PURE__ */ new Map();
282
+ const statement = (sql) => {
283
+ const existing = prepared.get(sql);
284
+ if (existing) return existing;
285
+ const fresh = db.prepare(sql);
286
+ prepared.set(sql, fresh);
287
+ return fresh;
288
+ };
289
+ const listeners = /* @__PURE__ */ new Set();
290
+ let evicted = 0;
291
+ let size = 0;
292
+ let bytes = 0;
293
+ function refreshTotals() {
294
+ const current = evictable.get();
295
+ size = current?.size ?? 0;
296
+ bytes = current?.bytes ?? 0;
297
+ }
298
+ refreshTotals();
299
+ let sweptAt = 0;
300
+ function enforceRetention(now = Date.now()) {
301
+ const { maxRequests, maxBytes, days } = limits();
302
+ if (days !== null && now - sweptAt > AGE_SWEEP_MS) {
303
+ sweptAt = now;
304
+ const removed2 = evictOld.run(now - days * import_core.DAY_MS).changes;
305
+ if (removed2 > 0) {
306
+ evicted += removed2;
307
+ refreshTotals();
308
+ }
309
+ }
310
+ const overCount = size > maxRequests + Math.floor(maxRequests / 50);
311
+ const overBytes = bytes > maxBytes + Math.floor(maxBytes / 50);
312
+ if (!overCount && !overBytes) return;
313
+ const removed = evict.run(maxBytes, maxRequests).changes;
314
+ evicted += removed;
315
+ refreshTotals();
316
+ }
317
+ options.retention?.subscribe(() => {
318
+ sweptAt = 0;
319
+ enforceRetention();
320
+ });
321
+ enforceRetention();
322
+ return {
323
+ name: "sqlite",
324
+ send(request) {
325
+ try {
326
+ const days = limits().days;
327
+ if (days !== null && request.startedAt < Date.now() - days * import_core.DAY_MS) return;
328
+ const payload = JSON.stringify(request);
329
+ const row = (0, import_panel.toRow)(request, false);
330
+ const stored = insert.run(
331
+ request.id,
332
+ request.startedAt,
333
+ request.method,
334
+ request.path,
335
+ request.query,
336
+ row.route,
337
+ request.status,
338
+ request.durationMs,
339
+ row.sqlCount,
340
+ row.httpCount,
341
+ row.nPlusOne ? 1 : 0,
342
+ row.hasError ? 1 : 0,
343
+ row.findingCount,
344
+ row.flag,
345
+ request.test,
346
+ request.host?.loopDelayMs ?? null,
347
+ request.timing?.sqlMs ?? null,
348
+ request.timing?.selfMs ?? null,
349
+ request.weight?.bytesOut ?? null,
350
+ request.cost?.cpuMs ?? null,
351
+ request.cost?.cpuShare ?? null,
352
+ payload.length,
353
+ payload
354
+ );
355
+ if (stored.changes === 0) return;
356
+ size++;
357
+ bytes += payload.length;
358
+ enforceRetention();
359
+ } catch (error) {
360
+ const reason = error instanceof Error ? error.message : String(error);
361
+ (0, import_core.warnOnce)("transport.sqlite.write", `request not stored: ${reason}`);
362
+ return;
363
+ }
364
+ for (const listener of listeners) {
365
+ try {
366
+ listener(request);
367
+ } catch {
368
+ }
369
+ }
370
+ },
371
+ list() {
372
+ return recent.all(listLimit).map((stored) => JSON.parse(stored["payload"]));
373
+ },
374
+ get(id) {
375
+ const found = byId.get(id);
376
+ return found ? JSON.parse(found["payload"]) : void 0;
377
+ },
378
+ page(query) {
379
+ const where = whereFor(query.filter);
380
+ const matching = `((${where.sql}) OR pinned = 1)`;
381
+ const order = orderColumn(query.sort);
382
+ const cursor = (0, import_panel.decodeCursor)(query.cursor);
383
+ const position = cursor ? afterCursor(cursor, order) : null;
384
+ const clause = position ? `${matching} AND ${position.sql}` : matching;
385
+ const params = [...where.params, ...position?.params ?? []];
386
+ const rows = statement(
387
+ `SELECT id, started_at, method, path, query_string, route, status, duration_ms,
388
+ sql_count, http_count, n_plus_one, has_error, finding_count, flag, test,
389
+ cpu_ms, cpu_share, pinned
390
+ FROM requests WHERE ${clause}
391
+ ORDER BY ${order} DESC, id DESC LIMIT ?`
392
+ ).all(...params, query.limit + 1);
393
+ const page = rows.slice(0, query.limit);
394
+ const last = page[page.length - 1];
395
+ const nextCursor = rows.length > query.limit && last ? (0, import_panel.encodeCursor)(order === "duration_ms" ? last.duration_ms : last.started_at, last.id) : null;
396
+ const counts = statement(
397
+ `SELECT COUNT(*) AS total,
398
+ SUM(CASE WHEN duration_ms >= ? THEN 1 ELSE 0 END) AS slow,
399
+ SUM(CASE WHEN has_error = 1 THEN 1 ELSE 0 END) AS errors,
400
+ SUM(CASE WHEN finding_count > 0 THEN 1 ELSE 0 END) AS with_findings
401
+ FROM requests`
402
+ ).get(SLOW_MS);
403
+ const shown = statement(`SELECT COUNT(*) AS shown FROM requests WHERE ${matching}`).get(
404
+ ...where.params
405
+ );
406
+ const summary = {
407
+ shown: shown?.shown ?? 0,
408
+ total: counts?.total ?? 0,
409
+ slow: counts?.slow ?? 0,
410
+ errors: counts?.errors ?? 0,
411
+ withFindings: counts?.with_findings ?? 0
412
+ };
413
+ return { rows: page.map(toRequestRow), nextCursor, summary };
414
+ },
415
+ durations(query) {
416
+ return routeDurations.all(
417
+ query.route,
418
+ query.method,
419
+ query.excludeId ?? "",
420
+ query.from ?? 0,
421
+ query.to ?? Number.MAX_SAFE_INTEGER,
422
+ BASELINE_LIMIT
423
+ ).map((row) => ({
424
+ durationMs: row["duration_ms"],
425
+ startedAt: row["started_at"],
426
+ hasError: row["has_error"] === 1,
427
+ loopDelayMs: row["loop_delay_ms"] ?? null
428
+ }));
429
+ },
430
+ counts(query) {
431
+ return routeCounts.all(query.from, query.to).map((row) => ({
432
+ method: row["method"],
433
+ route: row["route"],
434
+ calls: row["calls"],
435
+ avgMs: row["avg_ms"]
436
+ }));
437
+ },
438
+ findings(query) {
439
+ const rows = statement(
440
+ `SELECT payload FROM requests
441
+ WHERE finding_count > 0 AND started_at >= ? AND started_at <= ?
442
+ ORDER BY started_at DESC LIMIT ?`
443
+ ).all(query.from ?? 0, query.to ?? Number.MAX_SAFE_INTEGER, FINDINGS_LIMIT);
444
+ const samples = rows.flatMap(
445
+ (row) => (0, import_panel.findingSamplesOf)(JSON.parse(row["payload"]))
446
+ );
447
+ return { samples, truncated: rows.length >= FINDINGS_LIMIT };
448
+ },
449
+ findingStates() {
450
+ const states = /* @__PURE__ */ new Map();
451
+ for (const row of statement("SELECT id, state FROM finding_states").all()) {
452
+ const state = row["state"];
453
+ if (state === "ack" || state === "muted") states.set(row["id"], state);
454
+ }
455
+ return states;
456
+ },
457
+ setFindingState(id, state) {
458
+ if (state === "new") {
459
+ statement("DELETE FROM finding_states WHERE id = ?").run(id);
460
+ return;
461
+ }
462
+ statement(
463
+ `INSERT INTO finding_states (id, state, updated_at) VALUES (?, ?, ?)
464
+ ON CONFLICT(id) DO UPDATE SET state = excluded.state, updated_at = excluded.updated_at`
465
+ ).run(id, state, Date.now());
466
+ },
467
+ history(query) {
468
+ const where = whereFor(query.filter);
469
+ const rows = statement(
470
+ `SELECT started_at, duration_ms, has_error, sql_count, http_count, bytes_out,
471
+ loop_delay_ms, finding_count, sql_ms, self_ms, method, route, flag
472
+ FROM requests WHERE ${where.sql}
473
+ ORDER BY started_at DESC LIMIT ?`
474
+ ).all(...where.params, HISTORY_LIMIT);
475
+ const samples = rows.map((row) => ({
476
+ startedAt: row["started_at"],
477
+ durationMs: row["duration_ms"],
478
+ hasError: row["has_error"] === 1,
479
+ sqlCount: row["sql_count"] ?? 0,
480
+ httpCount: row["http_count"] ?? 0,
481
+ bytes: row["bytes_out"] ?? 0,
482
+ loopDelayMs: row["loop_delay_ms"] ?? null,
483
+ findingCount: row["finding_count"] ?? 0,
484
+ sqlMs: row["sql_ms"] ?? null,
485
+ selfMs: row["self_ms"] ?? null,
486
+ method: row["method"],
487
+ route: row["route"],
488
+ flag: row["flag"] ?? null
489
+ }));
490
+ return { samples, truncated: samples.length >= HISTORY_LIMIT };
491
+ },
492
+ timeSeries() {
493
+ return series.all(TIME_SERIES_LIMIT).map((row) => ({
494
+ startedAt: row["started_at"],
495
+ durationMs: row["duration_ms"],
496
+ hasError: row["has_error"] === 1
497
+ }));
498
+ },
499
+ pin(id, pinned) {
500
+ if (pinned) {
501
+ const current = countPinned.get()?.pinned ?? 0;
502
+ if (current >= pinLimit) return false;
503
+ }
504
+ const changed = setPinned.run(pinned ? 1 : 0, id).changes > 0;
505
+ if (changed) refreshTotals();
506
+ return changed;
507
+ },
508
+ pinned() {
509
+ return pinnedIds.all().map((row) => row["id"]);
510
+ },
511
+ stats() {
512
+ const current = totals.get();
513
+ return {
514
+ size: current?.size ?? 0,
515
+ capacity: limits().maxRequests,
516
+ evicted,
517
+ oldestAt: current?.oldest ?? null,
518
+ pinned: countPinned.get()?.pinned ?? 0,
519
+ bytes: current?.bytes ?? 0
520
+ };
521
+ },
522
+ clear() {
523
+ removeAll.run();
524
+ refreshTotals();
525
+ },
526
+ subscribe(listener) {
527
+ listeners.add(listener);
528
+ return () => listeners.delete(listener);
529
+ },
530
+ async close() {
531
+ listeners.clear();
532
+ prepared.clear();
533
+ db.close();
534
+ }
535
+ };
536
+ }
537
+ // Annotate the CommonJS export names for ESM import in node:
538
+ 0 && (module.exports = {
539
+ retentionPolicy,
540
+ sqliteTransport
541
+ });
542
+ //# sourceMappingURL=index.cjs.map