@basaltkit/audit-viewer 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -5
- package/dist/viewer.d.ts +14 -0
- package/dist/viewer.js +13 -6
- package/package.json +11 -7
package/README.md
CHANGED
|
@@ -65,21 +65,29 @@ auditViewerRoutes({ title: 'Audit — Acme', apiBase: '/admin' })
|
|
|
65
65
|
|
|
66
66
|
## API reference
|
|
67
67
|
|
|
68
|
-
### `auditViewerPlugin({ bucketMs?, topN? })`
|
|
68
|
+
### `auditViewerPlugin({ bucketMs?, topN?, maxScan? })`
|
|
69
69
|
|
|
70
|
-
Registers the `AUDIT_VIEWER` token.
|
|
70
|
+
Registers the `AUDIT_VIEWER` token.
|
|
71
|
+
|
|
72
|
+
| Option | Type | Default | Purpose |
|
|
73
|
+
|---|---|---|---|
|
|
74
|
+
| `bucketMs` | `number` | `86_400_000` (1 day) | Timeline bucket size. |
|
|
75
|
+
| `topN` | `number` | `20` | Rows returned by the per-event / per-actor breakdowns. |
|
|
76
|
+
| `maxScan` | `number` | `10_000` | Upper bound on rows read from the store per call. |
|
|
77
|
+
|
|
78
|
+
`maxScan` exists because the trail is unbounded and these routes forward client input: an unbounded read is an OOM vector. When a call hits the bound, the result carries `truncated: true` and `total` means "matches within the window", not a grand total.
|
|
71
79
|
|
|
72
80
|
### `class AuditViewer`
|
|
73
81
|
|
|
74
82
|
| Method | Description |
|
|
75
83
|
|---|---|
|
|
76
|
-
| `page(query)` | `{ entries, total, limit, offset }`. |
|
|
77
|
-
| `stats(query)` | `{ total, byEvent, byActor, bySource, timeline }`. |
|
|
84
|
+
| `page(query)` | `{ entries, total, limit, offset, truncated }`. |
|
|
85
|
+
| `stats(query)` | `{ total, truncated, byEvent, byActor, bySource, timeline }`. |
|
|
78
86
|
| `get(id, tenantId?)` | A single entry, or `null`. |
|
|
79
87
|
|
|
80
88
|
`ViewerQuery`: `event` (wildcard), `actorId`, `tenantId`, `source` (`hook`/`event`/`manual`), `since`, `until`, `limit`, `offset`. Without `tenantId`, it uses `ctx().tenant.id` (otherwise `AuditTenantRequiredError`).
|
|
81
89
|
|
|
82
|
-
> Note: the extra filtering (source/until) and the aggregation happen in memory over the result of `Audit.trail`.
|
|
90
|
+
> Note: the extra filtering (source/until) and the aggregation happen in memory over the result of `Audit.trail`, bounded by `maxScan`. `truncated: true` means the trail had more matches than the window — raise `maxScan`, narrow the query (`since`/`until`/`event`), or use an `AuditStore` with richer database querying.
|
|
83
91
|
|
|
84
92
|
## Content-Security-Policy
|
|
85
93
|
|
package/dist/viewer.d.ts
CHANGED
|
@@ -18,12 +18,18 @@ export interface ViewerQuery {
|
|
|
18
18
|
}
|
|
19
19
|
export interface AuditPage {
|
|
20
20
|
entries: AuditEntry[];
|
|
21
|
+
/** Matches found within the scan window — see `truncated`. */
|
|
21
22
|
total: number;
|
|
22
23
|
limit: number;
|
|
23
24
|
offset: number;
|
|
25
|
+
/** True when the scan hit `maxScan`: there are more matches than `total` reports. */
|
|
26
|
+
truncated: boolean;
|
|
24
27
|
}
|
|
25
28
|
export interface AuditStats {
|
|
29
|
+
/** Entries counted within the scan window — see `truncated`. */
|
|
26
30
|
total: number;
|
|
31
|
+
/** True when the scan hit `maxScan`: the aggregates cover only the newest rows. */
|
|
32
|
+
truncated: boolean;
|
|
27
33
|
byEvent: {
|
|
28
34
|
event: string;
|
|
29
35
|
count: number;
|
|
@@ -44,6 +50,12 @@ export interface AuditViewerOptions {
|
|
|
44
50
|
bucketMs?: number;
|
|
45
51
|
/** How many rows the byEvent/byActor breakdowns return. Default 20. */
|
|
46
52
|
topN?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Upper bound on rows read from the store per call. The trail is unbounded, so
|
|
55
|
+
* an unbounded read is an OOM vector on any endpoint that forwards client input.
|
|
56
|
+
* Results past the bound are reported via `truncated`. Default 10 000.
|
|
57
|
+
*/
|
|
58
|
+
maxScan?: number;
|
|
47
59
|
}
|
|
48
60
|
/**
|
|
49
61
|
* Read-only lens over the append-only audit trail: tenant-scoped, filterable,
|
|
@@ -54,11 +66,13 @@ export declare class AuditViewer {
|
|
|
54
66
|
private readonly audit;
|
|
55
67
|
private readonly bucketMs;
|
|
56
68
|
private readonly topN;
|
|
69
|
+
private readonly maxScan;
|
|
57
70
|
constructor(audit: Audit, options?: AuditViewerOptions);
|
|
58
71
|
page(query?: ViewerQuery): Promise<AuditPage>;
|
|
59
72
|
get(id: string, tenantId?: string): Promise<AuditEntry | null>;
|
|
60
73
|
stats(query?: ViewerQuery): Promise<AuditStats>;
|
|
61
74
|
/** Matching entries (newest first), after the extra source/until filters. */
|
|
75
|
+
/** Reads at most `maxScan` rows and reports whether the trail had more. */
|
|
62
76
|
private match;
|
|
63
77
|
private top;
|
|
64
78
|
private tenant;
|
package/dist/viewer.js
CHANGED
|
@@ -6,6 +6,7 @@ export class AuditTenantRequiredError extends BasaltError {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
const DAY = 86_400_000;
|
|
9
|
+
const DEFAULT_MAX_SCAN = 10_000;
|
|
9
10
|
/**
|
|
10
11
|
* Read-only lens over the append-only audit trail: tenant-scoped, filterable,
|
|
11
12
|
* paginated queries plus aggregate stats. Wraps {@link Audit}; the trail itself
|
|
@@ -15,23 +16,25 @@ export class AuditViewer {
|
|
|
15
16
|
audit;
|
|
16
17
|
bucketMs;
|
|
17
18
|
topN;
|
|
19
|
+
maxScan;
|
|
18
20
|
constructor(audit, options = {}) {
|
|
19
21
|
this.audit = audit;
|
|
20
22
|
this.bucketMs = options.bucketMs ?? DAY;
|
|
21
23
|
this.topN = options.topN ?? 20;
|
|
24
|
+
this.maxScan = options.maxScan ?? DEFAULT_MAX_SCAN;
|
|
22
25
|
}
|
|
23
26
|
async page(query = {}) {
|
|
24
|
-
const all = await this.match(query);
|
|
27
|
+
const { entries: all, truncated } = await this.match(query);
|
|
25
28
|
const limit = query.limit ?? 50;
|
|
26
29
|
const offset = query.offset ?? 0;
|
|
27
|
-
return { entries: all.slice(offset, offset + limit), total: all.length, limit, offset };
|
|
30
|
+
return { entries: all.slice(offset, offset + limit), total: all.length, limit, offset, truncated };
|
|
28
31
|
}
|
|
29
32
|
async get(id, tenantId) {
|
|
30
|
-
const
|
|
31
|
-
return
|
|
33
|
+
const { entries } = await this.match({ ...(tenantId !== undefined ? { tenantId } : {}) });
|
|
34
|
+
return entries.find((entry) => entry.id === id) ?? null;
|
|
32
35
|
}
|
|
33
36
|
async stats(query = {}) {
|
|
34
|
-
const all = await this.match(query);
|
|
37
|
+
const { entries: all, truncated } = await this.match(query);
|
|
35
38
|
const byEvent = new Map();
|
|
36
39
|
const byActor = new Map();
|
|
37
40
|
const bySource = {};
|
|
@@ -46,6 +49,7 @@ export class AuditViewer {
|
|
|
46
49
|
}
|
|
47
50
|
return {
|
|
48
51
|
total: all.length,
|
|
52
|
+
truncated,
|
|
49
53
|
byEvent: this.top(byEvent).map(([event, count]) => ({ event, count })),
|
|
50
54
|
byActor: this.top(byActor).map(([actorId, count]) => ({ actorId, count })),
|
|
51
55
|
bySource,
|
|
@@ -53,16 +57,19 @@ export class AuditViewer {
|
|
|
53
57
|
};
|
|
54
58
|
}
|
|
55
59
|
/** Matching entries (newest first), after the extra source/until filters. */
|
|
60
|
+
/** Reads at most `maxScan` rows and reports whether the trail had more. */
|
|
56
61
|
async match(query) {
|
|
57
62
|
const tenantId = this.tenant(query.tenantId);
|
|
58
63
|
const trail = await this.audit.trail({
|
|
59
64
|
tenantId,
|
|
65
|
+
limit: this.maxScan,
|
|
60
66
|
...(query.event !== undefined ? { event: query.event } : {}),
|
|
61
67
|
...(query.actorId !== undefined ? { actorId: query.actorId } : {}),
|
|
62
68
|
...(query.since !== undefined ? { since: query.since } : {}),
|
|
63
69
|
});
|
|
64
|
-
|
|
70
|
+
const entries = trail.filter((entry) => (query.source === undefined || entry.source === query.source) &&
|
|
65
71
|
(query.until === undefined || entry.at <= query.until));
|
|
72
|
+
return { entries, truncated: trail.length >= this.maxScan };
|
|
66
73
|
}
|
|
67
74
|
top(counts) {
|
|
68
75
|
return [...counts.entries()].sort((a, b) => b[1] - a[1]).slice(0, this.topN);
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/audit-viewer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=22.5.0"
|
|
6
|
+
},
|
|
4
7
|
"description": "Read-only viewer for the @basaltkit/audit trail: tenant-scoped, filterable, paginated queries with aggregate stats, plus a self-contained HTML browser.",
|
|
5
8
|
"license": "MIT",
|
|
6
9
|
"type": "module",
|
|
10
|
+
"sideEffects": false,
|
|
7
11
|
"exports": {
|
|
8
12
|
".": {
|
|
9
13
|
"types": "./dist/index.d.ts",
|
|
@@ -14,9 +18,9 @@
|
|
|
14
18
|
"dist"
|
|
15
19
|
],
|
|
16
20
|
"dependencies": {
|
|
17
|
-
"@basaltkit/audit": "^1.
|
|
18
|
-
"@basaltkit/core": "^1.3.
|
|
19
|
-
"@basaltkit/http": "^1.
|
|
21
|
+
"@basaltkit/audit": "^1.3.0",
|
|
22
|
+
"@basaltkit/core": "^1.3.1",
|
|
23
|
+
"@basaltkit/http": "^1.14.0"
|
|
20
24
|
},
|
|
21
25
|
"peerDependencies": {
|
|
22
26
|
"zod": "^3.24.0 || ^4.0.0"
|
|
@@ -26,9 +30,9 @@
|
|
|
26
30
|
"typescript": "^7.0.2",
|
|
27
31
|
"vitest": "^4.1.11",
|
|
28
32
|
"zod": "^3.24.0 || ^4.0.0",
|
|
29
|
-
"@basaltkit/auth": "^1.
|
|
30
|
-
"@basaltkit/fastify": "^1.
|
|
31
|
-
"@basaltkit/tenancy": "^1.4.
|
|
33
|
+
"@basaltkit/auth": "^1.8.0",
|
|
34
|
+
"@basaltkit/fastify": "^1.8.1",
|
|
35
|
+
"@basaltkit/tenancy": "^1.4.2",
|
|
32
36
|
"@basaltkit/tsconfig": "^0.24.0"
|
|
33
37
|
},
|
|
34
38
|
"publishConfig": {
|