adonisjs-server-stats 1.15.0 → 1.16.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.
Files changed (38) hide show
  1. package/README.md +88 -2
  2. package/dist/core/debug/types.d.ts +14 -0
  3. package/dist/core/types.d.ts +109 -4
  4. package/dist/src/dashboard/dashboard_controller.d.ts +7 -0
  5. package/dist/src/dashboard/dashboard_controller.js +10 -3
  6. package/dist/src/dashboard/dashboard_store.d.ts +1 -2
  7. package/dist/src/dashboard/dashboard_store.js +0 -3
  8. package/dist/src/dashboard/dashboard_types.d.ts +2 -0
  9. package/dist/src/dashboard/flush_manager.d.ts +1 -6
  10. package/dist/src/dashboard/flush_manager.js +3 -10
  11. package/dist/src/dashboard/integrations/config_inspector.js +9 -49
  12. package/dist/src/dashboard/sensitive_patterns.d.ts +39 -0
  13. package/dist/src/dashboard/sensitive_patterns.js +118 -0
  14. package/dist/src/dashboard/write_queue.d.ts +25 -13
  15. package/dist/src/dashboard/write_queue.js +63 -37
  16. package/dist/src/debug/debug_store.d.ts +15 -1
  17. package/dist/src/debug/debug_store.js +32 -4
  18. package/dist/src/debug/event_collector.d.ts +8 -0
  19. package/dist/src/debug/event_collector.js +12 -0
  20. package/dist/src/debug/types.d.ts +14 -0
  21. package/dist/src/define_config.js +23 -0
  22. package/dist/src/middleware/request_tracking_middleware.d.ts +2 -1
  23. package/dist/src/middleware/request_tracking_middleware.js +16 -1
  24. package/dist/src/provider/boot_helpers.d.ts +9 -0
  25. package/dist/src/provider/boot_helpers.js +31 -0
  26. package/dist/src/provider/dashboard_init.js +14 -1
  27. package/dist/src/provider/dashboard_setup.d.ts +10 -1
  28. package/dist/src/provider/dashboard_setup.js +47 -2
  29. package/dist/src/provider/server_stats_provider.d.ts +7 -0
  30. package/dist/src/provider/server_stats_provider.js +23 -7
  31. package/dist/src/provider/toolbar_setup.js +5 -1
  32. package/dist/src/routes/access_middleware.d.ts +7 -1
  33. package/dist/src/routes/access_middleware.js +6 -1
  34. package/dist/src/routes/register_routes.d.ts +13 -3
  35. package/dist/src/routes/register_routes.js +15 -1
  36. package/dist/src/stubs/config.stub +8 -0
  37. package/dist/src/types.d.ts +109 -4
  38. package/package.json +1 -1
package/README.md CHANGED
@@ -182,6 +182,7 @@ All fields are optional. `defineConfig({})` works with zero configuration.
182
182
  | `authorize` | `(ctx) => boolean` | -- | Per-request visibility guard |
183
183
  | `unsafeAllowNoAuth` | `boolean` | `false` | Register the dashboard/debug/stats routes even with **no** `authorize` guard. Off by default (routes fail closed). Local dev only — exposes secrets, email bodies, and SQL. |
184
184
  | `domain` | `string` | -- | Restrict every route to one host, e.g. `'admin.example.com'`. See [Custom domain](#custom-domain) |
185
+ | `production` | `ProductionConfig` | -- | Opt in to running in production. Off by default -- see [Production](#production) |
185
186
  | `onStats` | `(stats) => void` | -- | Callback after each collection tick |
186
187
  | `toolbar` | `boolean \| ToolbarConfig` | -- | `true` to enable with defaults, or pass a `ToolbarConfig` object |
187
188
  | `dashboard` | `boolean \| DashboardConfig` | -- | `true` to enable at `/__stats`, or pass a `DashboardConfig` |
@@ -205,6 +206,16 @@ All fields are optional. `defineConfig({})` works with zero configuration.
205
206
  | `path` | `string` | `'/__stats'` | URL path for the dashboard page |
206
207
  | `retentionDays` | `number` | `7` | Days to keep historical data in SQLite |
207
208
 
209
+ ### `ProductionConfig`
210
+
211
+ | Option | Type | Default | Description |
212
+ | --------------- | --------------- | ------- | ---------------------------------------------------------------------- |
213
+ | `enabled` | `boolean` | `false` | Register routes and build the dashboard when `NODE_ENV=production` |
214
+ | `capture` | `CaptureConfig` | all off | Which capture subsystems to switch on -- opt in one at a time |
215
+ | `retentionDays` | `number` | `3` | SQLite history to keep in production (the usual default is 7) |
216
+
217
+ `CaptureConfig` fields, each `boolean` and each defaulting to `false` in production: `queries`, `events`, `emails`, `traces`, `logs`.
218
+
208
219
  ### `AdvancedConfig`
209
220
 
210
221
  | Option | Type | Default | Description |
@@ -384,6 +395,81 @@ export default defineConfig({
384
395
 
385
396
  ---
386
397
 
398
+ ## Production
399
+
400
+ By default this package does **nothing** when `NODE_ENV=production` -- no routes are registered and neither the debug nor the dashboard store is built. The metrics engine still runs, so `onStats`, Prometheus, and Transmit broadcasting keep working; there is simply no HTTP surface.
401
+
402
+ Set `production.enabled` to lift that:
403
+
404
+ ```ts
405
+ export default defineConfig({
406
+ authorize: async (ctx) => (await ctx.auth.check()) && ctx.auth.user?.isAdmin === true,
407
+ dashboard: true,
408
+
409
+ production: {
410
+ enabled: true,
411
+ capture: { queries: true }, // everything else stays off
412
+ retentionDays: 3,
413
+ },
414
+ })
415
+ ```
416
+
417
+ ### An `authorize` guard is mandatory
418
+
419
+ `unsafeAllowNoAuth` is **ignored** in production. Without a guard the routes are not registered and a warning explains why -- an unauthenticated dashboard on a production host is never correct.
420
+
421
+ When production mode is active you get an unmissable startup banner, so nobody enables this and forgets:
422
+
423
+ ```
424
+ [ server-stats ] DASHBOARD IS LIVE IN PRODUCTION
425
+ reachable at: /admin/api/server-stats, /admin/api/debug/*, /__stats/*
426
+ guard: authorize() configured
427
+ capturing: queries
428
+ data at: .adonisjs/server-stats/dashboard.sqlite3 (retention: 3 days)
429
+ ```
430
+
431
+ ### Capture is off unless you ask for it
432
+
433
+ Each capture subsystem hooks something global and stores something sensitive, so in production every one starts **off** and is enabled by name:
434
+
435
+ | `capture` flag | What it records | Why it's off by default |
436
+ | -------------- | ----------------------------------- | ------------------------------------------------ |
437
+ | `queries` | SQL text, bindings, timings | Bindings can hold values you would not log |
438
+ | `events` | Application events (memory only) | Patches the emitter's `emit()` process-wide |
439
+ | `emails` | Sent mail incl. subject and body | Highest-sensitivity payload in the package |
440
+ | `traces` | Per-request spans | Wraps every request in `AsyncLocalStorage` |
441
+ | `logs` | Log lines into SQLite | Highest write volume, and logs quote payloads |
442
+
443
+ **With no capture flags at all you still get the useful core:** the request list, the overview, and the charts. Those come from request rows and 1-minute metric buckets, at a small fraction of full dev-mode write volume. Turn capture on when you are actually debugging something.
444
+
445
+ A disabled subsystem is never subscribed, so it costs nothing -- its dashboard pane simply stays empty.
446
+
447
+ > [!NOTE]
448
+ > `toolbar: { tracing: false }` still overrides `capture.traces`. It is the older kill switch and keeps winning.
449
+
450
+ ### Disk
451
+
452
+ Retention prunes rows older than `retentionDays` on boot and hourly. It does **not** run `VACUUM`, so the `.sqlite3` file reuses freed pages rather than shrinking -- expect the file to plateau, not shrink. Watch real numbers in the dashboard's storage panel (`GET {dashboardPath}/api/storage`), which reports file size, WAL size, and per-table row counts.
453
+
454
+ There is no sampling and no hard size cap. On a high-traffic app, enable capture deliberately and keep `retentionDays` short.
455
+
456
+ ### Async guards and the toolbar
457
+
458
+ An `authorize` callback may be sync or async -- the route guard awaits it, so `async (ctx) => { await ctx.auth.authenticate(); ... }` is safe.
459
+
460
+ > [!WARNING]
461
+ > The `@serverStats()` Edge toolbar evaluates the guard **synchronously** while rendering, so it cannot await. With an async guard the stats bar hides itself rather than risk showing when it shouldn't, and logs a warning once. The HTTP routes are guarded correctly either way -- only the cosmetic bar is affected. Use a synchronous guard if you want the bar in production.
462
+
463
+ ### What this does not give you
464
+
465
+ Worth knowing before you expose it, even behind an admin guard:
466
+
467
+ - **One guard, no tiers.** Anyone who passes `authorize` can also `DELETE /api/cache/:key` and `POST /api/jobs/:id/retry`. Scope cache access with `SERVER_STATS_CACHE_KEY_PREFIX` -- unset means unrestricted.
468
+ - **No audit log and no rate limiting** on those mutating endpoints.
469
+ - **SQL bindings are truncated at 256 characters but not redacted by key name.** A short secret passed as a bound parameter is stored as-is. Config and env values *are* redacted; query bindings are not.
470
+
471
+ ---
472
+
387
473
  ## Auto-Registered Routes
388
474
 
389
475
  All API routes are registered automatically by the package during `boot()` -- no manual controllers or route definitions needed. Each route group is gated by the `authorize` callback if configured.
@@ -466,7 +552,7 @@ Pass a bare host. A protocol, path, or port (`https://admin.example.com`, `admin
466
552
 
467
553
  > **The toolbar follows the domain.** The `@serverStats()` Edge tag and the React/Vue components request **relative** URLs, so they only work on pages served from the configured domain. Render the toolbar on `example.com` while `domain` points at `admin.example.com` and the bar will sit empty -- its polls 404 against the wrong host. Either serve the toolbar from the same domain, or leave `domain` unset and rely on `authorize` alone.
468
554
 
469
- > **Reminder:** routes are never registered in production regardless of this setting (`app.inProduction` short-circuits registration), so `domain` applies to your dev and staging environments.
555
+ > **Reminder:** routes are not registered in production unless you opt in via [`production.enabled`](#production). Without that, `domain` applies to your dev and staging environments only.
470
556
 
471
557
  ### Global middleware note
472
558
 
@@ -653,7 +739,7 @@ Found a bug? Have feedback? [Open an issue](https://github.com/simulieren/adonis
653
739
 
654
740
  ## Dev Toolbar
655
741
 
656
- Adds a debug panel with SQL query inspection, event tracking, email capture with HTML preview, route table, live logs, and per-request tracing. Only active in non-production environments.
742
+ Adds a debug panel with SQL query inspection, event tracking, email capture with HTML preview, route table, live logs, and per-request tracing. Active in non-production environments by default; see [Production](#production) to opt in there.
657
743
 
658
744
  ```ts
659
745
  export default defineConfig({
@@ -288,6 +288,20 @@ export interface DevToolbarConfig {
288
288
  dbPath: string;
289
289
  /** Base path for the debug toolbar API endpoints. */
290
290
  debugEndpoint: string;
291
+ /**
292
+ * Which capture subsystems are subscribed. Fully resolved — every field is
293
+ * present. All true outside production; all false in production unless the
294
+ * user opted in via `production.capture`.
295
+ */
296
+ capture: ResolvedCapture;
297
+ }
298
+ /** Fully-resolved capture flags. See {@link CaptureConfig} for the user-facing shape. */
299
+ export interface ResolvedCapture {
300
+ queries: boolean;
301
+ events: boolean;
302
+ emails: boolean;
303
+ traces: boolean;
304
+ logs: boolean;
291
305
  }
292
306
  /**
293
307
  * Color names available for the `badge` column format.
@@ -435,6 +435,94 @@ export interface DashboardConfig {
435
435
  */
436
436
  retentionDays?: number;
437
437
  }
438
+ /**
439
+ * Access-control callback signature.
440
+ *
441
+ * May be sync or async — the route guard awaits it. An async guard is the usual
442
+ * shape once the check has to consult `ctx.auth` or the database.
443
+ *
444
+ * One caveat: the `@serverStats()` Edge tag evaluates the guard synchronously
445
+ * while rendering the template, so with an **async** guard the toolbar hides
446
+ * itself rather than risk showing when it shouldn't. The HTTP routes await it
447
+ * properly either way — only the cosmetic bar is affected.
448
+ */
449
+ export type AccessGuard = (ctx: import('@adonisjs/core/http').HttpContext) => boolean | Promise<boolean>;
450
+ /**
451
+ * Which capture subsystems are active.
452
+ *
453
+ * Each one hooks a global: queries and events subscribe to the Lucid/app
454
+ * emitter, emails subscribe to the mail events, traces wrap every request in
455
+ * `AsyncLocalStorage`. Turning one off means its collector is never subscribed,
456
+ * so it costs nothing — its dashboard pane simply stays empty.
457
+ *
458
+ * Outside production every field defaults to `true`. In production every field
459
+ * defaults to **`false`** and must be opted into individually.
460
+ */
461
+ export interface CaptureConfig {
462
+ /** SQL text, bindings, and timings for every query. */
463
+ queries?: boolean;
464
+ /** Application events (in-memory only — never persisted to SQLite). */
465
+ events?: boolean;
466
+ /** Sent mail, including subject and body. */
467
+ emails?: boolean;
468
+ /** Per-request spans and the request timeline. */
469
+ traces?: boolean;
470
+ /** Log lines written into the dashboard's SQLite store. */
471
+ logs?: boolean;
472
+ }
473
+ /**
474
+ * Opt in to running the dashboard in production.
475
+ *
476
+ * By default this package registers **no routes** when `NODE_ENV=production`
477
+ * and never builds the debug or dashboard stores. Setting `enabled: true` lifts
478
+ * that, but only with an {@link ServerStatsConfig.authorize} guard in place —
479
+ * `unsafeAllowNoAuth` is ignored in production.
480
+ *
481
+ * Data capture stays **off** unless you ask for it. Without any `capture`
482
+ * flags you still get the request list, the overview, and the charts (those
483
+ * come from request rows and 1-minute metric buckets), at a small fraction of
484
+ * the write volume of a full dev-mode capture.
485
+ *
486
+ * @example
487
+ * ```ts
488
+ * export default defineConfig({
489
+ * authorize: async (ctx) => (await ctx.auth.check()) && ctx.auth.user?.isAdmin === true,
490
+ * dashboard: true,
491
+ * production: {
492
+ * enabled: true,
493
+ * capture: { queries: true },
494
+ * retentionDays: 3,
495
+ * },
496
+ * })
497
+ * ```
498
+ */
499
+ export interface ProductionConfig {
500
+ /**
501
+ * Register routes and build the dashboard when `NODE_ENV=production`.
502
+ *
503
+ * Requires an `authorize` guard — without one the routes are still not
504
+ * registered, and a warning explains why.
505
+ *
506
+ * @default false
507
+ */
508
+ enabled?: boolean;
509
+ /**
510
+ * Which capture subsystems to switch on. Every field defaults to `false` in
511
+ * production, so capture is opt-in one subsystem at a time.
512
+ *
513
+ * @see {@link CaptureConfig}
514
+ */
515
+ capture?: CaptureConfig;
516
+ /**
517
+ * How many days of history to keep in SQLite, overriding the usual default
518
+ * of 7. Retention deletes rows hourly but never runs `VACUUM`, so the
519
+ * database file reuses pages rather than shrinking — watch actual size via
520
+ * the dashboard's storage panel.
521
+ *
522
+ * @default 3
523
+ */
524
+ retentionDays?: number;
525
+ }
438
526
  /**
439
527
  * Advanced options that most users never need to touch.
440
528
  *
@@ -699,7 +787,7 @@ export interface ServerStatsConfig {
699
787
  *
700
788
  * @deprecated Use {@link authorize} instead. Will be removed in the next major version.
701
789
  */
702
- shouldShow?: (ctx: import('@adonisjs/core/http').HttpContext) => boolean;
790
+ shouldShow?: AccessGuard;
703
791
  /**
704
792
  * How often (in **milliseconds**) to run all collectors and
705
793
  * broadcast updated stats.
@@ -750,7 +838,7 @@ export interface ServerStatsConfig {
750
838
  * authorize: () => process.env.NODE_ENV === 'development'
751
839
  * ```
752
840
  */
753
- authorize?: (ctx: import('@adonisjs/core/http').HttpContext) => boolean;
841
+ authorize?: AccessGuard;
754
842
  /**
755
843
  * Register the sensitive dashboard/debug/stats routes WITHOUT any access
756
844
  * guard when no {@link authorize} callback is provided.
@@ -833,6 +921,21 @@ export interface ServerStatsConfig {
833
921
  * ```
834
922
  */
835
923
  domain?: string;
924
+ /**
925
+ * Opt in to running in production, where this package otherwise registers
926
+ * nothing at all.
927
+ *
928
+ * Requires an {@link authorize} guard, and leaves data capture off unless you
929
+ * enable it per subsystem.
930
+ *
931
+ * @see {@link ProductionConfig}
932
+ *
933
+ * @example
934
+ * ```ts
935
+ * production: { enabled: true, capture: { queries: true } }
936
+ * ```
937
+ */
938
+ production?: ProductionConfig;
836
939
  /**
837
940
  * Advanced options for fine-tuning internal behavior.
838
941
  *
@@ -878,7 +981,7 @@ export interface ResolvedServerStatsConfig {
878
981
  /** Optional dev toolbar configuration. */
879
982
  devToolbar?: DevToolbarOptions;
880
983
  /** Optional access-control callback. */
881
- shouldShow?: (ctx: import('@adonisjs/core/http').HttpContext) => boolean;
984
+ shouldShow?: AccessGuard;
882
985
  /** Collection interval in milliseconds (new name for {@link intervalMs}). */
883
986
  pollInterval?: number;
884
987
  /** Whether real-time (SSE) broadcasting is enabled (new name for {@link transport}). */
@@ -886,7 +989,7 @@ export interface ResolvedServerStatsConfig {
886
989
  /** HTTP endpoint path or `false` to disable (new name for {@link endpoint}). */
887
990
  statsEndpoint?: string | false;
888
991
  /** Access-control callback (new name for {@link shouldShow}). */
889
- authorize?: (ctx: import('@adonisjs/core/http').HttpContext) => boolean;
992
+ authorize?: AccessGuard;
890
993
  /**
891
994
  * Escape hatch to register sensitive routes without an access guard.
892
995
  * Exposes the dashboard without auth — local development only.
@@ -902,4 +1005,6 @@ export interface ResolvedServerStatsConfig {
902
1005
  verbose: boolean;
903
1006
  /** Optional domain restriction for all routes. */
904
1007
  domain?: string;
1008
+ /** Opt-in production behavior. Absent means this package is inert in production. */
1009
+ production?: ProductionConfig;
905
1010
  }
@@ -26,6 +26,13 @@ export default class DashboardController {
26
26
  createSavedFilter(ctx: HttpContext): Promise<void>;
27
27
  deleteSavedFilter(ctx: HttpContext): Promise<void>;
28
28
  private withDb;
29
+ /**
30
+ * Re-check the access guard for the dashboard page itself.
31
+ *
32
+ * Awaited: an async guard would otherwise return a truthy promise and let
33
+ * everyone through. The route middleware already applies the same guard, so
34
+ * this is defense in depth for the HTML page.
35
+ */
29
36
  private checkAccess;
30
37
  private getDashboardPath;
31
38
  }
@@ -23,7 +23,7 @@ export default class DashboardController {
23
23
  this.pageAssets = new DashboardPageAssets();
24
24
  }
25
25
  async page(ctx) {
26
- if (!this.checkAccess(ctx))
26
+ if (!(await this.checkAccess(ctx)))
27
27
  return ctx.response.forbidden({ error: 'Access denied' });
28
28
  const config = this.app.config.get('server_stats');
29
29
  const tc = config?.devToolbar ?? {};
@@ -163,12 +163,19 @@ export default class DashboardController {
163
163
  return response.json(emptyValue);
164
164
  }
165
165
  }
166
- checkAccess(ctx) {
166
+ /**
167
+ * Re-check the access guard for the dashboard page itself.
168
+ *
169
+ * Awaited: an async guard would otherwise return a truthy promise and let
170
+ * everyone through. The route middleware already applies the same guard, so
171
+ * this is defense in depth for the HTML page.
172
+ */
173
+ async checkAccess(ctx) {
167
174
  const config = this.app.config.get('server_stats');
168
175
  if (!config?.shouldShow)
169
176
  return true;
170
177
  try {
171
- return config.shouldShow(ctx);
178
+ return await config.shouldShow(ctx);
172
179
  }
173
180
  catch {
174
181
  return false;
@@ -1,4 +1,4 @@
1
- import type { DevToolbarConfig, EventRecord, EmailRecord } from '../debug/types.js';
1
+ import type { DevToolbarConfig, EmailRecord } from '../debug/types.js';
2
2
  import type { StorageStatsResult } from './storage_stats.js';
3
3
  import type { Knex } from 'knex';
4
4
  export type { RequestInput, PersistRequestInput, RequestFilters, QueryFilters, EventFilters, EmailFilters, LogFilters, TraceFilters, PaginatedResult, PaginateOptions, } from './dashboard_types.js';
@@ -26,7 +26,6 @@ export declare class DashboardStore {
26
26
  isReady(): boolean;
27
27
  getStorageStats(): Promise<StorageStatsResult>;
28
28
  persistRequest(input: PersistRequestInput): Promise<number | null>;
29
- queueEvents(requestIndex: number, events: EventRecord[]): void;
30
29
  recordLog(entry: Record<string, unknown>): void;
31
30
  recordEmail(record: EmailRecord): void;
32
31
  flushWriteQueue(): Promise<void>;
@@ -139,9 +139,6 @@ export class DashboardStore {
139
139
  this.flushMgr.persistRequest(input, this.dashboardPath);
140
140
  return Promise.resolve(null);
141
141
  }
142
- queueEvents(requestIndex, events) {
143
- this.flushMgr.queueEvents(requestIndex, events);
144
- }
145
142
  recordLog(entry) {
146
143
  this.flushMgr.recordLog(entry);
147
144
  }
@@ -16,6 +16,8 @@ export interface RequestInput {
16
16
  }
17
17
  export interface PersistRequestInput extends RequestInput {
18
18
  queries: import('../debug/types.js').QueryRecord[];
19
+ /** Events emitted since the previous request completed. */
20
+ events?: import('../debug/types.js').EventRecord[];
19
21
  trace: import('../debug/types.js').TraceRecord | null;
20
22
  httpRequestId?: string | null;
21
23
  }
@@ -1,12 +1,8 @@
1
- import type { EventRecord, EmailRecord } from '../debug/types.js';
1
+ import type { EmailRecord } from '../debug/types.js';
2
2
  import type { PersistRequestInput } from './dashboard_types.js';
3
3
  import type { Knex } from 'knex';
4
4
  export declare class FlushManager {
5
5
  writeQueue: PersistRequestInput[];
6
- pendingEvents: {
7
- requestIndex: number;
8
- events: EventRecord[];
9
- }[];
10
6
  pendingLogs: Record<string, unknown>[];
11
7
  pendingEmails: EmailRecord[];
12
8
  private flushTimer;
@@ -15,7 +11,6 @@ export declare class FlushManager {
15
11
  private db;
16
12
  constructor(getDb: () => Knex | null);
17
13
  persistRequest(input: PersistRequestInput, dashboardPath: string): void;
18
- queueEvents(requestIndex: number, events: EventRecord[]): void;
19
14
  recordLog(entry: Record<string, unknown>): void;
20
15
  recordEmail(record: EmailRecord): void;
21
16
  stop(): Promise<void>;
@@ -1,10 +1,9 @@
1
1
  import { log } from '../utils/logger.js';
2
- import { prepareRequestRows, prepareLogRows, flushRequests, flushEvents, flushEmails, flushLogs, hasWarned, markWarned, } from './write_queue.js';
2
+ import { prepareRequestRows, prepareLogRows, flushRequests, flushEmails, flushLogs, hasWarned, markWarned, } from './write_queue.js';
3
3
  const FLUSH_MS = 500;
4
4
  const MAX_Q = 200;
5
5
  export class FlushManager {
6
6
  writeQueue = [];
7
- pendingEvents = [];
8
7
  pendingLogs = [];
9
8
  pendingEmails = [];
10
9
  flushTimer = null;
@@ -21,10 +20,6 @@ export class FlushManager {
21
20
  this.writeQueue.push(input);
22
21
  this.scheduleFlush();
23
22
  }
24
- queueEvents(requestIndex, events) {
25
- if (events.length > 0)
26
- this.pendingEvents.push({ requestIndex, events });
27
- }
28
23
  recordLog(entry) {
29
24
  if (!this.db())
30
25
  return;
@@ -94,7 +89,6 @@ export class FlushManager {
94
89
  const pl = prepareLogRows(snap.logs);
95
90
  await db.transaction(async (trx) => {
96
91
  await flushRequests(trx, pr);
97
- await flushEvents(trx, snap.events);
98
92
  await flushEmails(trx, snap.emails);
99
93
  await flushLogs(trx, pl);
100
94
  });
@@ -115,10 +109,9 @@ export class FlushManager {
115
109
  takeSnapshot() {
116
110
  const requests = this.writeQueue.splice(0);
117
111
  const logs = this.pendingLogs.splice(0);
118
- const events = this.pendingEvents.splice(0);
119
112
  const emails = this.pendingEmails.splice(0);
120
- if (requests.length === 0 && logs.length === 0 && events.length === 0 && emails.length === 0)
113
+ if (requests.length === 0 && logs.length === 0 && emails.length === 0)
121
114
  return null;
122
- return { requests, logs, events, emails };
115
+ return { requests, logs, emails };
123
116
  }
124
117
  }
@@ -1,48 +1,9 @@
1
+ import { isSensitiveConfigName, looksLikeCredentialValue } from '../sensitive_patterns.js';
1
2
  // ---------------------------------------------------------------------------
2
- // Sensitive key patterns
3
+ // Sensitive key detection
3
4
  // ---------------------------------------------------------------------------
4
- /**
5
- * Patterns matched against key names (case-insensitive) to detect secrets.
6
- *
7
- * Uses `(?:^|[_.-])` and `(?:$|[_.-])` as boundaries instead of `\b`
8
- * because env vars use `_` as separators and `_` is a word character
9
- * in regex, so `\b` won't match between `CLIENT` and `SECRET` in
10
- * `GOOGLE_CLIENT_SECRET`.
11
- */
12
- const B = '(?:^|[_.\\-])'; // boundary before
13
- const A = '(?:$|[_.\\-])'; // boundary after
14
- const SENSITIVE_PATTERNS = [
15
- new RegExp(`${B}password${A}`, 'i'),
16
- new RegExp(`${B}secret${A}`, 'i'),
17
- new RegExp(`${B}token${A}`, 'i'),
18
- new RegExp(`${B}credential${A}`, 'i'),
19
- new RegExp(`${B}private${A}`, 'i'),
20
- new RegExp(`${B}auth${A}`, 'i'),
21
- // API keys: `api_key`, `apiKey`, `API_KEY`
22
- /api[_-]?key/i,
23
- // `_KEY` at end or `_KEY_` in middle (AWS_ACCESS_KEY_ID, ENCRYPTION_KEY, etc.)
24
- /[_-]key([_-]|$)/i,
25
- // ACCESS_KEY pattern (AWS credentials)
26
- /access[_-]?key/i,
27
- // Exact match for just "key" (standalone)
28
- /^key$/i,
29
- // Connection strings and DSNs
30
- new RegExp(`${B}dsn${A}`, 'i'),
31
- /connection[_-]?string/i,
32
- // Email addresses in env var names
33
- new RegExp(`${B}email${A}`, 'i'),
34
- new RegExp(`${B}smtp${A}`, 'i'),
35
- // Database/service URLs (often contain embedded credentials)
36
- /database[_-]?url/i,
37
- /redis[_-]?url/i,
38
- // Webhook secrets
39
- /webhook[_-]?secret/i,
40
- // Signing / encryption
41
- new RegExp(`${B}signing${A}`, 'i'),
42
- new RegExp(`${B}encryption${A}`, 'i'),
43
- // App key / app secret
44
- /app[_-]key/i,
45
- ];
5
+ // The name patterns live in `../sensitive_patterns.js` so the config inspector
6
+ // and the SQL-binding writer share one definition of "looks like a secret".
46
7
  const REDACTED_DISPLAY = '••••••••';
47
8
  function redact(_value) {
48
9
  // Never include the plaintext value: the redacted object is serialized
@@ -121,20 +82,19 @@ export class ConfigInspector {
121
82
  * Check if a key name matches any sensitive pattern.
122
83
  */
123
84
  function isSensitiveKey(key) {
124
- return SENSITIVE_PATTERNS.some((pattern) => pattern.test(key));
85
+ return isSensitiveConfigName(key);
125
86
  }
126
87
  /**
127
88
  * Check if a value looks sensitive based on its content.
128
89
  * Catches email addresses and URLs with embedded credentials.
129
90
  */
130
91
  function isSensitiveValue(value) {
131
- // Email addresses
92
+ // Email addresses are config-sensitive (SMTP accounts) even though they are
93
+ // ordinary data as a query binding — hence the check lives here, not in the
94
+ // shared shape helper.
132
95
  if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value))
133
96
  return true;
134
- // URLs with userinfo (credentials embedded in URL)
135
- if (/^[a-z][a-z0-9+.-]*:\/\/[^/]*:[^/]*@/i.test(value))
136
- return true;
137
- return false;
97
+ return looksLikeCredentialValue(value);
138
98
  }
139
99
  /** Sanitize a single key-value pair, redacting sensitive strings. */
140
100
  function sanitizeValue(key, value, seen) {
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Shared server-side rules for recognising credentials.
3
+ *
4
+ * Both the config inspector and the SQL-binding writer need to answer "does
5
+ * this look like a secret?", and they used to answer it differently — config
6
+ * values were redacted against a real word list while query bindings were only
7
+ * truncated by length. One list, used by both, so the same secret cannot be
8
+ * masked in one view and printed in full in another.
9
+ */
10
+ /**
11
+ * Names that identify a credential — applies to env vars, config keys, and SQL
12
+ * identifiers alike.
13
+ */
14
+ export declare const SECRET_NAME_PATTERNS: RegExp[];
15
+ /**
16
+ * Names that matter for env vars and config keys but NOT for SQL identifiers.
17
+ *
18
+ * An `email` env var is usually an SMTP account; an `email` *column* is ordinary
19
+ * application data, and redacting every binding of every query that touches it
20
+ * would make the query pane useless for debugging auth. Same for the service
21
+ * URLs, which are env-shaped names rather than column names.
22
+ */
23
+ export declare const CONFIG_ONLY_NAME_PATTERNS: RegExp[];
24
+ /** Whether a name identifies a credential. */
25
+ export declare function isSecretName(name: string): boolean;
26
+ /** Whether a name is sensitive in a config/env context (credentials plus contact/service names). */
27
+ export declare function isSensitiveConfigName(name: string): boolean;
28
+ /**
29
+ * Whether a SQL statement mentions a credential-shaped identifier.
30
+ *
31
+ * Tokenised first: the name patterns above use `_`/`.`/`-` boundaries, so
32
+ * running them across raw SQL would miss `password` sitting between spaces.
33
+ *
34
+ * Positional bindings cannot be mapped back to specific columns reliably, so a
35
+ * hit means every binding for that statement is redacted. Coarse on purpose —
36
+ * over-redacting one statement's parameters beats storing a password.
37
+ */
38
+ export declare function sqlMentionsSecret(sql: string): boolean;
39
+ export declare function looksLikeCredentialValue(value: string): boolean;