@fuzdev/fuz_app 0.181.0 → 0.183.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 +2 -0
- package/dist/actions/CLAUDE.md +28 -10
- package/dist/actions/action_rpc.d.ts +11 -1
- package/dist/actions/action_rpc.d.ts.map +1 -1
- package/dist/actions/action_rpc.js +3 -2
- package/dist/actions/action_types.d.ts +16 -1
- package/dist/actions/action_types.d.ts.map +1 -1
- package/dist/actions/action_types.js +1 -1
- package/dist/actions/notification_sender.d.ts +83 -0
- package/dist/actions/notification_sender.d.ts.map +1 -0
- package/dist/actions/notification_sender.js +85 -0
- package/dist/actions/perform_action.d.ts +9 -0
- package/dist/actions/perform_action.d.ts.map +1 -1
- package/dist/actions/perform_action.js +22 -13
- package/dist/actions/register_action_ws.d.ts +14 -0
- package/dist/actions/register_action_ws.d.ts.map +1 -1
- package/dist/actions/register_action_ws.js +3 -2
- package/dist/actions/ws_socket_session.d.ts +3 -0
- package/dist/actions/ws_socket_session.d.ts.map +1 -1
- package/dist/actions/ws_socket_session.js +2 -1
- package/dist/auth/CLAUDE.md +113 -70
- package/dist/auth/account_actions.d.ts.map +1 -1
- package/dist/auth/account_actions.js +13 -3
- package/dist/auth/account_queries.d.ts +158 -16
- package/dist/auth/account_queries.d.ts.map +1 -1
- package/dist/auth/account_queries.js +189 -18
- package/dist/auth/account_routes.d.ts +6 -4
- package/dist/auth/account_routes.d.ts.map +1 -1
- package/dist/auth/account_routes.js +98 -92
- package/dist/auth/admin_actions.d.ts.map +1 -1
- package/dist/auth/admin_actions.js +93 -45
- package/dist/auth/api_token_queries.d.ts +13 -5
- package/dist/auth/api_token_queries.d.ts.map +1 -1
- package/dist/auth/api_token_queries.js +24 -9
- package/dist/auth/audit_emitter.d.ts +75 -84
- package/dist/auth/audit_emitter.d.ts.map +1 -1
- package/dist/auth/audit_emitter.js +83 -71
- package/dist/auth/audit_log_schema.d.ts +10 -5
- package/dist/auth/audit_log_schema.d.ts.map +1 -1
- package/dist/auth/audit_log_schema.js +22 -6
- package/dist/auth/bearer_auth.d.ts.map +1 -1
- package/dist/auth/bearer_auth.js +3 -2
- package/dist/auth/bootstrap_account.d.ts +31 -13
- package/dist/auth/bootstrap_account.d.ts.map +1 -1
- package/dist/auth/bootstrap_account.js +34 -15
- package/dist/auth/bootstrap_routes.d.ts +1 -1
- package/dist/auth/bootstrap_routes.d.ts.map +1 -1
- package/dist/auth/bootstrap_routes.js +7 -14
- package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
- package/dist/auth/daemon_token_middleware.js +12 -1
- package/dist/auth/deps.d.ts +18 -5
- package/dist/auth/deps.d.ts.map +1 -1
- package/dist/auth/deps.js +3 -2
- package/dist/auth/request_context.d.ts +73 -73
- package/dist/auth/request_context.d.ts.map +1 -1
- package/dist/auth/request_context.js +78 -96
- package/dist/auth/resolved_auth.d.ts +16 -11
- package/dist/auth/resolved_auth.d.ts.map +1 -1
- package/dist/auth/resolved_auth.js +6 -6
- package/dist/auth/role_grant_offer_action_specs.d.ts +1 -1
- package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_action_specs.js +2 -1
- package/dist/auth/role_grant_offer_actions.d.ts +32 -12
- package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_actions.js +91 -116
- package/dist/auth/role_grant_offer_notifications.d.ts +87 -38
- package/dist/auth/role_grant_offer_notifications.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_notifications.js +75 -22
- package/dist/auth/role_grant_offer_queries.d.ts +155 -18
- package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_offer_queries.js +262 -73
- package/dist/auth/role_grant_offer_schema.js +1 -1
- package/dist/auth/role_grant_queries.d.ts +43 -52
- package/dist/auth/role_grant_queries.d.ts.map +1 -1
- package/dist/auth/role_grant_queries.js +50 -74
- package/dist/auth/role_grant_sql.d.ts +21 -0
- package/dist/auth/role_grant_sql.d.ts.map +1 -1
- package/dist/auth/role_grant_sql.js +21 -0
- package/dist/auth/self_service_role_actions.d.ts.map +1 -1
- package/dist/auth/self_service_role_actions.js +17 -21
- package/dist/auth/session_queries.d.ts +40 -16
- package/dist/auth/session_queries.d.ts.map +1 -1
- package/dist/auth/session_queries.js +68 -29
- package/dist/auth/signup_routes.d.ts.map +1 -1
- package/dist/auth/signup_routes.js +90 -38
- package/dist/auth/standard_rpc_actions.d.ts +6 -19
- package/dist/auth/standard_rpc_actions.d.ts.map +1 -1
- package/dist/auth/standard_rpc_actions.js +5 -6
- package/dist/auth/token_scope.d.ts +1 -1
- package/dist/auth/token_scope.js +1 -1
- package/dist/db/sql_columns.d.ts +1 -1
- package/dist/db/sql_columns.js +1 -1
- package/dist/http/CLAUDE.md +22 -16
- package/dist/http/auth_shape.d.ts +1 -1
- package/dist/http/db_routes.d.ts +7 -22
- package/dist/http/db_routes.d.ts.map +1 -1
- package/dist/http/db_routes.js +72 -72
- package/dist/http/error_schemas.d.ts +9 -21
- package/dist/http/error_schemas.d.ts.map +1 -1
- package/dist/http/error_schemas.js +14 -24
- package/dist/http/pending_effects.d.ts +42 -4
- package/dist/http/pending_effects.d.ts.map +1 -1
- package/dist/http/pending_effects.js +34 -4
- package/dist/http/route_spec.d.ts +2 -2
- package/dist/server/app_backend.d.ts.map +1 -1
- package/dist/server/app_backend.js +3 -1
- package/dist/server/app_server.d.ts +69 -18
- package/dist/server/app_server.d.ts.map +1 -1
- package/dist/server/app_server.js +74 -10
- package/dist/server/serve_fact_route.d.ts +3 -3
- package/dist/server/serve_fact_route.js +12 -12
- package/dist/testing/CLAUDE.md +259 -790
- package/dist/testing/adversarial_404.d.ts +6 -1
- package/dist/testing/adversarial_404.d.ts.map +1 -1
- package/dist/testing/adversarial_404.js +38 -17
- package/dist/testing/app_server.d.ts +20 -140
- package/dist/testing/app_server.d.ts.map +1 -1
- package/dist/testing/app_server.js +48 -125
- package/dist/testing/audit_drift_guard.d.ts +11 -6
- package/dist/testing/audit_drift_guard.d.ts.map +1 -1
- package/dist/testing/audit_drift_guard.js +15 -8
- package/dist/testing/auth_apps.js +1 -1
- package/dist/testing/cross_backend/account_lifecycle.d.ts.map +1 -1
- package/dist/testing/cross_backend/account_lifecycle.js +43 -0
- package/dist/testing/cross_backend/audit_cross_helpers.d.ts +12 -9
- package/dist/testing/cross_backend/audit_cross_helpers.d.ts.map +1 -1
- package/dist/testing/cross_backend/audit_cross_helpers.js +13 -14
- package/dist/testing/cross_backend/capabilities.d.ts +9 -6
- package/dist/testing/cross_backend/capabilities.d.ts.map +1 -1
- package/dist/testing/cross_backend/capabilities.js +11 -7
- package/dist/testing/cross_backend/capability_mounts.d.ts +103 -0
- package/dist/testing/cross_backend/capability_mounts.d.ts.map +1 -0
- package/dist/testing/cross_backend/capability_mounts.js +423 -0
- package/dist/testing/cross_backend/cell_moderate.js +2 -2
- package/dist/testing/cross_backend/cell_relations.d.ts.map +1 -1
- package/dist/testing/cross_backend/cell_relations.js +12 -11
- package/dist/testing/cross_backend/conformance_principal.d.ts +54 -0
- package/dist/testing/cross_backend/conformance_principal.d.ts.map +1 -0
- package/dist/testing/cross_backend/conformance_principal.js +283 -0
- package/dist/testing/cross_backend/conformance_table.d.ts +2 -50
- package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
- package/dist/testing/cross_backend/conformance_table.js +2 -270
- package/dist/testing/cross_backend/credential_deadline.d.ts.map +1 -1
- package/dist/testing/cross_backend/credential_deadline.js +2 -0
- package/dist/testing/cross_backend/credential_wire.d.ts +1 -1
- package/dist/testing/cross_backend/credential_wire.d.ts.map +1 -1
- package/dist/testing/cross_backend/db_state_diff.d.ts +72 -0
- package/dist/testing/cross_backend/db_state_diff.d.ts.map +1 -0
- package/dist/testing/cross_backend/db_state_diff.js +444 -0
- package/dist/testing/cross_backend/declared_skips.d.ts +68 -0
- package/dist/testing/cross_backend/declared_skips.d.ts.map +1 -0
- package/dist/testing/cross_backend/declared_skips.js +132 -0
- package/dist/testing/cross_backend/default_spine_surface.d.ts +1 -26
- package/dist/testing/cross_backend/default_spine_surface.d.ts.map +1 -1
- package/dist/testing/cross_backend/default_spine_surface.js +2 -6
- package/dist/testing/cross_backend/differential.d.ts +11 -2
- package/dist/testing/cross_backend/differential.d.ts.map +1 -1
- package/dist/testing/cross_backend/differential.js +35 -11
- package/dist/testing/cross_backend/differential_normalize.d.ts +12 -0
- package/dist/testing/cross_backend/differential_normalize.d.ts.map +1 -1
- package/dist/testing/cross_backend/differential_normalize.js +16 -0
- package/dist/testing/cross_backend/full_spine_mount.d.ts +3 -10
- package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
- package/dist/testing/cross_backend/full_spine_mount.js +4 -4
- package/dist/testing/cross_backend/in_process_setup.d.ts +22 -12
- package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/in_process_setup.js +151 -134
- package/dist/testing/cross_backend/login_security.d.ts +12 -0
- package/dist/testing/cross_backend/login_security.d.ts.map +1 -1
- package/dist/testing/cross_backend/login_security.js +21 -4
- package/dist/testing/cross_backend/request_target_conformance_wire.d.ts +0 -2
- package/dist/testing/cross_backend/request_target_conformance_wire.d.ts.map +1 -1
- package/dist/testing/cross_backend/request_target_conformance_wire.js +3 -1
- package/dist/testing/cross_backend/revocation_drivers.d.ts +18 -5
- package/dist/testing/cross_backend/revocation_drivers.d.ts.map +1 -1
- package/dist/testing/cross_backend/revocation_drivers.js +18 -10
- package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts +1 -1
- package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts.map +1 -1
- package/dist/testing/cross_backend/role_grant_offer_lifecycle.d.ts.map +1 -1
- package/dist/testing/cross_backend/role_grant_offer_lifecycle.js +33 -1
- package/dist/testing/cross_backend/role_grant_offer_notification_ws.d.ts +5 -4
- package/dist/testing/cross_backend/role_grant_offer_notification_ws.d.ts.map +1 -1
- package/dist/testing/cross_backend/role_grant_offer_notification_ws.js +183 -30
- package/dist/testing/cross_backend/role_grant_participation.js +4 -4
- package/dist/testing/cross_backend/rpc_adversarial.js +1 -1
- package/dist/testing/cross_backend/session_cap.d.ts +2 -2
- package/dist/testing/cross_backend/session_cap.d.ts.map +1 -1
- package/dist/testing/cross_backend/session_cap.js +48 -16
- package/dist/testing/cross_backend/setup.d.ts +31 -1
- package/dist/testing/cross_backend/setup.d.ts.map +1 -1
- package/dist/testing/cross_backend/setup.js +32 -1
- package/dist/testing/cross_backend/spine_suites.d.ts +123 -12
- package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_suites.js +454 -57
- package/dist/testing/cross_backend/spine_surface_constants.d.ts +1 -1
- package/dist/testing/cross_backend/spine_surface_constants.d.ts.map +1 -1
- package/dist/testing/cross_backend/spine_surface_constants.js +1 -0
- package/dist/testing/cross_backend/sse_round_trip.d.ts +9 -1
- package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
- package/dist/testing/cross_backend/sse_round_trip.js +123 -27
- package/dist/testing/cross_backend/testing_backdoor.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_backdoor.js +4 -2
- package/dist/testing/cross_backend/testing_reset_actions.d.ts +59 -27
- package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
- package/dist/testing/cross_backend/testing_reset_actions.js +66 -42
- package/dist/testing/cross_backend/transport_composition.d.ts +1 -1
- package/dist/testing/cross_backend/transport_composition.d.ts.map +1 -1
- package/dist/testing/cross_backend/transport_composition.js +7 -3
- package/dist/testing/cross_backend/ws_round_trip.d.ts +5 -0
- package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
- package/dist/testing/cross_backend/ws_round_trip.js +54 -8
- package/dist/testing/data_exposure.d.ts.map +1 -1
- package/dist/testing/data_exposure.js +3 -0
- package/dist/testing/db.d.ts +0 -32
- package/dist/testing/db.d.ts.map +1 -1
- package/dist/testing/db.js +0 -86
- package/dist/testing/db_entities.d.ts +14 -0
- package/dist/testing/db_entities.d.ts.map +1 -1
- package/dist/testing/db_entities.js +17 -0
- package/dist/testing/db_invariant_queries.d.ts +259 -0
- package/dist/testing/db_invariant_queries.d.ts.map +1 -0
- package/dist/testing/db_invariant_queries.js +400 -0
- package/dist/testing/db_invariants.d.ts +106 -0
- package/dist/testing/db_invariants.d.ts.map +1 -0
- package/dist/testing/db_invariants.gen.sql.d.ts +11 -0
- package/dist/testing/db_invariants.gen.sql.d.ts.map +1 -0
- package/dist/testing/db_invariants.gen.sql.js +13 -0
- package/dist/testing/db_invariants.js +141 -0
- package/dist/testing/db_invariants.sql +152 -0
- package/dist/testing/describe_db.d.ts +36 -0
- package/dist/testing/describe_db.d.ts.map +1 -0
- package/dist/testing/describe_db.js +71 -0
- package/dist/testing/integration.d.ts +6 -9
- package/dist/testing/integration.d.ts.map +1 -1
- package/dist/testing/integration.js +9 -13
- package/dist/testing/integration_helpers.d.ts +1 -1
- package/dist/testing/integration_helpers.d.ts.map +1 -1
- package/dist/testing/middleware.d.ts +1 -1
- package/dist/testing/middleware.js +5 -5
- package/dist/testing/rate_limiting.d.ts +1 -1
- package/dist/testing/rate_limiting.d.ts.map +1 -1
- package/dist/testing/rate_limiting.js +3 -2
- package/dist/testing/resolved_auth.d.ts +3 -1
- package/dist/testing/resolved_auth.d.ts.map +1 -1
- package/dist/testing/resolved_auth.js +4 -4
- package/dist/testing/role_grant_helpers.d.ts +1 -1
- package/dist/testing/role_grant_helpers.d.ts.map +1 -1
- package/dist/testing/round_trip.d.ts.map +1 -1
- package/dist/testing/round_trip.js +3 -0
- package/dist/testing/rpc_attack_surface.d.ts +8 -6
- package/dist/testing/rpc_attack_surface.d.ts.map +1 -1
- package/dist/testing/rpc_attack_surface.js +69 -6
- package/dist/testing/rpc_round_trip.d.ts +1 -1
- package/dist/testing/rpc_round_trip.d.ts.map +1 -1
- package/dist/testing/rpc_round_trip.js +3 -0
- package/dist/testing/sse_round_trip.d.ts +1 -1
- package/dist/testing/sse_round_trip.d.ts.map +1 -1
- package/dist/testing/sse_round_trip.js +13 -4
- package/dist/testing/stubs.d.ts +4 -2
- package/dist/testing/stubs.d.ts.map +1 -1
- package/dist/testing/stubs.js +15 -7
- package/dist/testing/test_app.d.ts +166 -0
- package/dist/testing/test_app.d.ts.map +1 -0
- package/dist/testing/test_app.js +124 -0
- package/dist/testing/ws_round_trip.d.ts.map +1 -1
- package/dist/testing/ws_round_trip.js +8 -4
- package/package.json +1 -1
|
@@ -144,13 +144,21 @@ export const query_api_token_live_account = async (deps, id) => {
|
|
|
144
144
|
/**
|
|
145
145
|
* Revoke all tokens for an account.
|
|
146
146
|
*
|
|
147
|
+
* Locks its rows in id order before it deletes them (`ORDER BY id FOR
|
|
148
|
+
* UPDATE`), as the cap's eviction (`query_api_token_enforce_limit`) does, so
|
|
149
|
+
* the two reaching the same rows wait one on the other instead of
|
|
150
|
+
* deadlocking in the opposite orders their plans would otherwise walk. Twin
|
|
151
|
+
* of the Rust `fuz_auth::query_revoke_all_api_tokens_for_account`.
|
|
152
|
+
*
|
|
147
153
|
* @param deps - query dependencies
|
|
148
154
|
* @param account_id - the account whose tokens to revoke
|
|
149
155
|
* @returns the number of tokens revoked
|
|
150
156
|
* @mutates `api_token` table - deletes every row for `account_id`
|
|
151
157
|
*/
|
|
152
158
|
export const query_revoke_all_api_tokens_for_account = async (deps, account_id) => {
|
|
153
|
-
const rows = await deps.db.query(`DELETE FROM api_token
|
|
159
|
+
const rows = await deps.db.query(`DELETE FROM api_token
|
|
160
|
+
WHERE id IN (SELECT id FROM api_token WHERE account_id = $1 ORDER BY id FOR UPDATE)
|
|
161
|
+
RETURNING id`, [account_id]);
|
|
154
162
|
return rows.length;
|
|
155
163
|
};
|
|
156
164
|
/**
|
|
@@ -185,14 +193,16 @@ export const query_api_token_list_for_account = async (deps, account_id) => {
|
|
|
185
193
|
* spec declares `side_effects: true`, making one creator's INSERT +
|
|
186
194
|
* enforce_limit pair atomic. That does **not** serialize concurrent creators:
|
|
187
195
|
* under Read Committed, two transactions can't see each other's uncommitted
|
|
188
|
-
* token row, so each
|
|
189
|
-
* `max_tokens`.
|
|
190
|
-
* `
|
|
191
|
-
*
|
|
196
|
+
* token row, so each would evict against a stale count and both commit above
|
|
197
|
+
* `max_tokens`. The handler serializes them first, taking the account row
|
|
198
|
+
* `FOR NO KEY UPDATE` (`query_account_lock_live`) — see the matching note on
|
|
199
|
+
* `query_session_enforce_limit`.
|
|
192
200
|
*
|
|
193
201
|
* Ordering carries the same `id DESC` stability tie-breaker, with the same
|
|
194
202
|
* caveat, as `query_session_enforce_limit` — deterministic survivors under a
|
|
195
|
-
* `created_at` tie, not a guarantee that the row just inserted survives.
|
|
203
|
+
* `created_at` tie, not a guarantee that the row just inserted survives. The
|
|
204
|
+
* victims are locked in id order before the delete, as the revoke-all
|
|
205
|
+
* (`query_revoke_all_api_tokens_for_account`) locks its rows.
|
|
196
206
|
*
|
|
197
207
|
* Deleting a token row does not end the connections it opened, so the
|
|
198
208
|
* returned ids are what the caller closes once the transaction commits
|
|
@@ -209,9 +219,14 @@ export const query_api_token_enforce_limit = async (deps, account_id, max_tokens
|
|
|
209
219
|
const rows = await deps.db.query(`DELETE FROM api_token
|
|
210
220
|
WHERE id IN (
|
|
211
221
|
SELECT id FROM api_token
|
|
212
|
-
WHERE
|
|
213
|
-
|
|
214
|
-
|
|
222
|
+
WHERE id IN (
|
|
223
|
+
SELECT id FROM api_token
|
|
224
|
+
WHERE account_id = $1
|
|
225
|
+
ORDER BY created_at DESC, id DESC
|
|
226
|
+
OFFSET $2
|
|
227
|
+
)
|
|
228
|
+
ORDER BY id
|
|
229
|
+
FOR UPDATE
|
|
215
230
|
) RETURNING id`, [account_id, max_tokens]);
|
|
216
231
|
return rows.map((row) => row.id);
|
|
217
232
|
};
|
|
@@ -12,46 +12,52 @@
|
|
|
12
12
|
* These methods cover every fan-out shape the auth domain needs
|
|
13
13
|
* (`add_listener` and `listener_count` manage the listener chain):
|
|
14
14
|
*
|
|
15
|
-
* - `emit(ctx, input)` — the request-scoped emit. A success row
|
|
16
|
-
* request transaction
|
|
17
|
-
*
|
|
18
|
-
* in-flight promise goes on
|
|
19
|
-
* resolves at once.
|
|
15
|
+
* - `emit(ctx, input)` — the request-scoped emit. A success row is written
|
|
16
|
+
* in the request's transaction and the promise rejects when the INSERT
|
|
17
|
+
* fails — or when no transaction is open; a failure row is a
|
|
18
|
+
* fire-and-forget pool write whose in-flight promise goes on
|
|
19
|
+
* `ctx.pending_effects`, and the returned promise resolves at once.
|
|
20
20
|
* - `emit_role_grant_target(ctx, auth, input)` — wrapper that lifts the
|
|
21
21
|
* `actor_id` / `account_id` / `ip` boilerplate every role-grant-shape audit
|
|
22
22
|
* site repeated. Delegates to `emit`.
|
|
23
23
|
* - `emit_pool(input)` — awaitable pool write for code paths without a
|
|
24
|
-
* request
|
|
25
|
-
* then notifies.
|
|
24
|
+
* request (maintenance scripts). Writes, then notifies.
|
|
26
25
|
* - `notify(event)` — fan out an already-written audit row (e.g. rows
|
|
27
26
|
* returned by `query_accept_offer` that were inserted in-transaction by
|
|
28
27
|
* the query layer, or the offer-expiry rows `auth/cleanup.ts` writes in
|
|
29
28
|
* its sweep transaction). Runs every registered listener; per-listener
|
|
30
29
|
* throws are isolated.
|
|
31
|
-
* - `drain_inflight()` —
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
30
|
+
* - `drain_inflight()` — await every pool write in flight: what
|
|
31
|
+
* `AppServer.close` waits on (bounded) before the database closes, and the
|
|
32
|
+
* barrier behind the `_testing_drain_effects` test action.
|
|
33
|
+
* - `inflight_count()` — how many pool writes are in flight now.
|
|
35
34
|
*
|
|
36
35
|
* ## Where a row is written
|
|
37
36
|
*
|
|
38
|
-
* The routing reads
|
|
39
|
-
* transaction (`Db.in_transaction`).
|
|
37
|
+
* The routing reads the row's outcome, and for a success row whether `ctx.db`
|
|
38
|
+
* is a transaction (`Db.in_transaction`).
|
|
40
39
|
*
|
|
41
|
-
* - A **success** row
|
|
42
|
-
* `side_effects`, a route with `transaction: true
|
|
43
|
-
* transaction
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
40
|
+
* - A **success** row is written **in the request's transaction** — an action
|
|
41
|
+
* with `side_effects`, a route with `transaction: true`, or the transaction a
|
|
42
|
+
* `transaction: false` route opens around just its writes (`/login`,
|
|
43
|
+
* `/password`, signup, bootstrap, through
|
|
44
|
+
* `http/pending_effects.ts`'s `transaction_with_post_commit_rollback`),
|
|
45
|
+
* last before its commit. The change and the row that reports it commit
|
|
46
|
+
* together or not at all. The INSERT is awaited, and a failed one rejects
|
|
47
|
+
* `emit`, so the handler throws and its transaction rolls back — a
|
|
48
|
+
* committed change is never left unaudited, and a response, once sent, has
|
|
49
|
+
* its row committed. With no transaction open, `emit` refuses a success row
|
|
50
|
+
* by throwing, writing nothing (counted for `install_audit_drift_guard`,
|
|
51
|
+
* `get_audit_success_outside_transaction`).
|
|
47
52
|
* - A **failure** row is written on the pool, outside the request's
|
|
48
53
|
* transaction, so it survives the rollback that usually follows it — the
|
|
49
|
-
* forensic record of a refused attempt.
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
54
|
+
* forensic record of a refused attempt. No response waits for it;
|
|
55
|
+
* `AppServer.close` does, bounded (`drain_inflight`).
|
|
56
|
+
*
|
|
57
|
+
* `seq` is a `BIGSERIAL`, taken at INSERT: rows are numbered in INSERT order,
|
|
58
|
+
* not commit order, so a reader paging by `seq` can see a gap fill in when a
|
|
59
|
+
* transaction that inserted earlier commits later. What holds is that a
|
|
60
|
+
* success row is visible exactly when its change is.
|
|
55
61
|
*
|
|
56
62
|
* A pool write is fail-open: a failed one is counted
|
|
57
63
|
* (`get_audit_write_failures`) and logged as one `error` line carrying the
|
|
@@ -62,24 +68,18 @@
|
|
|
62
68
|
* than a request's, so a pool saturated enough to shed requests with a 503
|
|
63
69
|
* does not also drop the failure rows recording them.
|
|
64
70
|
*
|
|
65
|
-
* This is the Rust spine's split: `fuz_auth`'s `route_audit_emit` writes
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* routing between the twins, visible on the wire only when the audit INSERT
|
|
70
|
-
* fails: the TS `/logout` runs in a route transaction, so its success row is
|
|
71
|
-
* written in it (Rust writes it on the pool), and `db_admin_row_delete` is
|
|
72
|
-
* written on the pool after the commit on both (`http/db_routes.ts`, through
|
|
73
|
-
* `emit_pool`).
|
|
71
|
+
* This is the Rust spine's split: `fuz_auth`'s `route_audit_emit` writes a
|
|
72
|
+
* success row in the request transaction (`emit_in_tx`) — its REST handlers'
|
|
73
|
+
* and the db-admin row delete's included — and every failure row on the pool
|
|
74
|
+
* (`emit`).
|
|
74
75
|
*
|
|
75
76
|
* ## When listeners hear about a row
|
|
76
77
|
*
|
|
77
78
|
* - A **failure** row is announced as soon as it is written. It records an
|
|
78
79
|
* attempt, which happened whether or not the request's transaction commits.
|
|
79
80
|
* - A **success** row is announced only after the request's transaction
|
|
80
|
-
* commits
|
|
81
|
-
*
|
|
82
|
-
* `ctx.post_commit_effects`. A success row reports a state change, and the
|
|
81
|
+
* commits, and never when it rolls back: the fan-out is queued on
|
|
82
|
+
* `ctx.post_commit_effects`, discarded on a rollback. A success row reports a state change, and the
|
|
83
83
|
* listeners act on it — the revocation listeners close connections, the
|
|
84
84
|
* audit stream broadcasts it. Announced before the commit, a close could run
|
|
85
85
|
* while the revoked credential still reads as valid, and a rolled-back
|
|
@@ -108,16 +108,17 @@ import { type AuditLogConfig, type AuditLogEvent, type AuditLogInput } from './a
|
|
|
108
108
|
* and its two side-effect queues. The bound emitter carries its own `log`
|
|
109
109
|
* reference inside the closure, so per-call contexts don't need one.
|
|
110
110
|
*
|
|
111
|
-
* `db`
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
* and pushes the in-flight `Promise<void>` onto
|
|
115
|
-
* row's listener fan-out goes on
|
|
116
|
-
* listeners hear about a row").
|
|
111
|
+
* `db` is where a success row is written — it must be a transaction
|
|
112
|
+
* (`db.in_transaction`), or `emit` refuses the row (module doc, "Where a row
|
|
113
|
+
* is written"). A failure row's pool write is eager — the emitter fires it
|
|
114
|
+
* immediately and pushes the in-flight `Promise<void>` onto
|
|
115
|
+
* `pending_effects`. A success row's listener fan-out goes on
|
|
116
|
+
* `post_commit_effects` (module doc, "When listeners hear about a row").
|
|
117
117
|
*
|
|
118
118
|
* Both `RouteContext` and `ActionContext` structurally satisfy this
|
|
119
|
-
* shape, so handlers pass `route` / `ctx` directly
|
|
120
|
-
*
|
|
119
|
+
* shape, so handlers pass `route` / `ctx` directly, and so does the context
|
|
120
|
+
* `transaction_with_post_commit_rollback` hands its callback. Never pass one
|
|
121
|
+
* from a post-commit thunk: its transaction has ended.
|
|
121
122
|
*/
|
|
122
123
|
export interface AuditEmitterContext extends EmitAfterCommitContext, QueryDeps {
|
|
123
124
|
pending_effects: Array<Promise<void>>;
|
|
@@ -154,10 +155,10 @@ export interface AuditEmitter {
|
|
|
154
155
|
* Write an audit row for a request, routed by outcome and `ctx.db` (module
|
|
155
156
|
* doc, "Where a row is written").
|
|
156
157
|
*
|
|
157
|
-
* A success row
|
|
158
|
-
*
|
|
159
|
-
* the handler throws and the change rolls
|
|
160
|
-
* is a fire-and-forget pool write: its in-flight promise goes on
|
|
158
|
+
* A success row is written in the request's transaction: the promise
|
|
159
|
+
* settles once the INSERT has run, and rejects when it fails — or when
|
|
160
|
+
* `ctx.db` is no transaction — so the handler throws and the change rolls
|
|
161
|
+
* back with its row. A failure row is a fire-and-forget pool write: its in-flight promise goes on
|
|
161
162
|
* `ctx.pending_effects` (so tests with `await_pending_effects: true` can
|
|
162
163
|
* assert it inline), its errors are logged and counted, never thrown, and
|
|
163
164
|
* the returned promise resolves at once.
|
|
@@ -172,7 +173,7 @@ export interface AuditEmitter {
|
|
|
172
173
|
* @mutates `audit_log` table - inserts the row in `ctx.db`'s transaction or via the captured pool
|
|
173
174
|
* @mutates `ctx.pending_effects` - appends a pool write's in-flight settled promise
|
|
174
175
|
* @mutates `ctx.post_commit_effects` - appends a success row's listener fan-out
|
|
175
|
-
* @throws Error when an in-transaction INSERT fails
|
|
176
|
+
* @throws Error when an in-transaction INSERT fails, or a success row is emitted with no transaction open
|
|
176
177
|
*/
|
|
177
178
|
emit<T extends string>(ctx: AuditEmitterContext, input: AuditLogInput<T>): Promise<void>;
|
|
178
179
|
/**
|
|
@@ -185,9 +186,8 @@ export interface AuditEmitter {
|
|
|
185
186
|
*/
|
|
186
187
|
emit_role_grant_target<T extends string>(ctx: AuditEmitRoleGrantContext, auth: RequestActorContext, input: AuditEmitRoleGrantInput<T>): Promise<void>;
|
|
187
188
|
/**
|
|
188
|
-
* Awaitable pool write for code paths without a request
|
|
189
|
-
*
|
|
190
|
-
* (`db_admin_row_delete`, `http/db_routes.ts`).
|
|
189
|
+
* Awaitable pool write for code paths without a request — a maintenance
|
|
190
|
+
* script.
|
|
191
191
|
*
|
|
192
192
|
* Writes, then notifies the listeners, whatever the outcome — there is no
|
|
193
193
|
* request transaction to order the fan-out against. Tracked by
|
|
@@ -197,9 +197,11 @@ export interface AuditEmitter {
|
|
|
197
197
|
*
|
|
198
198
|
* Not for a success audit paired with a state mutation: the pool write
|
|
199
199
|
* can't roll back with the state, and a swallowed error leaves the
|
|
200
|
-
* mutation unaudited. Write those in the mutation's transaction
|
|
201
|
-
*
|
|
202
|
-
* shape `auth/cleanup.ts`'s offer sweep and
|
|
200
|
+
* mutation unaudited. Write those in the mutation's transaction — `emit`
|
|
201
|
+
* with a transaction context, or `query_audit_log` against the tx and
|
|
202
|
+
* `notify` after the commit, the shape `auth/cleanup.ts`'s offer sweep and
|
|
203
|
+
* `query_accept_offer` use. A success row written here is counted for
|
|
204
|
+
* `install_audit_drift_guard` (`get_audit_success_outside_transaction`).
|
|
203
205
|
*
|
|
204
206
|
* @mutates `audit_log` table - inserts the row via the captured pool
|
|
205
207
|
*/
|
|
@@ -246,35 +248,31 @@ export interface AuditEmitter {
|
|
|
246
248
|
*/
|
|
247
249
|
listener_count(): number;
|
|
248
250
|
/**
|
|
249
|
-
* Await every
|
|
250
|
-
*
|
|
251
|
+
* Await every pool write in flight — `emit`'s failure rows and every
|
|
252
|
+
* `emit_pool` — which no handler awaits. `AppServer.close` waits on it,
|
|
253
|
+
* bounded, before the database closes, so a row a request just started is
|
|
254
|
+
* not lost to the shutdown; it is also the deterministic barrier behind
|
|
255
|
+
* the `_testing_drain_effects` test action.
|
|
251
256
|
*
|
|
252
|
-
* Waits until no
|
|
257
|
+
* Waits until no pool write is outstanding, so a write that starts
|
|
253
258
|
* while the drain is waiting (an emit from a post-commit thunk) is awaited
|
|
254
259
|
* too. Once it resolves, a following `audit_log_list` read sees every row
|
|
255
260
|
* whose `emit` ran before the drain was called. A write that starts after
|
|
256
261
|
* the drain resolves is not covered. A success row's listener fan-out runs
|
|
257
262
|
* as a post-commit effect and is not tracked, so an SSE broadcast or an
|
|
258
263
|
* auth-guard close driven by one isn't guaranteed done when the drain
|
|
259
|
-
* resolves — the row itself is.
|
|
264
|
+
* resolves — the row itself is. A success row is written in its request's
|
|
265
|
+
* transaction, which the request awaits, so it is never in flight here.
|
|
260
266
|
*
|
|
261
|
-
*
|
|
262
|
-
* `track_inflight: true` — production never tracks, so there is nothing
|
|
263
|
-
* to wait for. Twin of the Rust `fuz_auth` `AuditEmitter::drain_inflight`.
|
|
267
|
+
* Twin of the Rust `fuz_auth` `AuditEmitter::drain_inflight`.
|
|
264
268
|
*/
|
|
265
269
|
drain_inflight(): Promise<void>;
|
|
266
270
|
/**
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
* The `_testing_drain_effects` action reports it as `tracked`, and the
|
|
272
|
-
* `list_audit_events` reads refuse `false`: a test binary whose emitter was
|
|
273
|
-
* built without tracking would otherwise answer the drain at once and its
|
|
274
|
-
* audit reads would race the writes. Twin of the Rust `fuz_auth`
|
|
275
|
-
* `AuditEmitter::is_tracking_inflight`.
|
|
271
|
+
* How many pool writes are in flight now — what a shutdown that gives up
|
|
272
|
+
* on `drain_inflight` at its bound reports as lost. Twin of the Rust
|
|
273
|
+
* `fuz_auth` `AuditEmitter::inflight_count`.
|
|
276
274
|
*/
|
|
277
|
-
|
|
275
|
+
inflight_count(): number;
|
|
278
276
|
}
|
|
279
277
|
/**
|
|
280
278
|
* Signature of `AuditEmitter.emit` — captured by the inner closure so
|
|
@@ -321,17 +319,6 @@ export interface CreateAuditEmitterOptions {
|
|
|
321
319
|
* Leave unset in production — it is a test instrumentation seam.
|
|
322
320
|
*/
|
|
323
321
|
emit_decorator?: EmitDecorator;
|
|
324
|
-
/**
|
|
325
|
-
* Track every in-flight `emit` write so `drain_inflight` can await it.
|
|
326
|
-
* Defaults to `false`, which makes `drain_inflight` resolve at once.
|
|
327
|
-
*
|
|
328
|
-
* Test binaries only. A transport can reply before a request's writes
|
|
329
|
-
* settle — a WebSocket message is answered before its effects flush — so
|
|
330
|
-
* a test reading the audit log after a reply needs this barrier. In
|
|
331
|
-
* production the tracking is pure overhead and nothing drains it. Twin of
|
|
332
|
-
* the Rust `fuz_auth` `AuditEmitter::new_with_inflight_tracking`.
|
|
333
|
-
*/
|
|
334
|
-
track_inflight?: boolean;
|
|
335
322
|
}
|
|
336
323
|
/**
|
|
337
324
|
* The whole row a dropped pool write would have inserted, as the one JSON
|
|
@@ -356,6 +343,10 @@ export interface CreateAuditEmitterOptions {
|
|
|
356
343
|
* @returns the row, ready to `JSON.stringify`
|
|
357
344
|
*/
|
|
358
345
|
export declare const to_dropped_audit_row: <T extends string>(input: AuditLogInput<T>, config: AuditLogConfig) => Record<string, unknown>;
|
|
346
|
+
/** Number of success rows emitted outside a transaction since process start. */
|
|
347
|
+
export declare const get_audit_success_outside_transaction: () => number;
|
|
348
|
+
/** Reset the counter — for tests only. */
|
|
349
|
+
export declare const reset_audit_success_outside_transaction: () => void;
|
|
359
350
|
/** Number of audit rows an emitter failed to write since process start. */
|
|
360
351
|
export declare const get_audit_write_failures: () => number;
|
|
361
352
|
/** Reset the counter — for tests only. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audit_emitter.d.ts","sourceRoot":"","sources":["../../src/lib/auth/audit_emitter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiGG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAEpD,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAKrD,OAAO,EAAqB,KAAK,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AAC5F,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAEhE,OAAO,EAEN,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,MAAM,uBAAuB,CAAC;AAE/B
|
|
1
|
+
{"version":3,"file":"audit_emitter.d.ts","sourceRoot":"","sources":["../../src/lib/auth/audit_emitter.ts"],"names":[],"mappings":"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"}
|
|
@@ -12,46 +12,52 @@
|
|
|
12
12
|
* These methods cover every fan-out shape the auth domain needs
|
|
13
13
|
* (`add_listener` and `listener_count` manage the listener chain):
|
|
14
14
|
*
|
|
15
|
-
* - `emit(ctx, input)` — the request-scoped emit. A success row
|
|
16
|
-
* request transaction
|
|
17
|
-
*
|
|
18
|
-
* in-flight promise goes on
|
|
19
|
-
* resolves at once.
|
|
15
|
+
* - `emit(ctx, input)` — the request-scoped emit. A success row is written
|
|
16
|
+
* in the request's transaction and the promise rejects when the INSERT
|
|
17
|
+
* fails — or when no transaction is open; a failure row is a
|
|
18
|
+
* fire-and-forget pool write whose in-flight promise goes on
|
|
19
|
+
* `ctx.pending_effects`, and the returned promise resolves at once.
|
|
20
20
|
* - `emit_role_grant_target(ctx, auth, input)` — wrapper that lifts the
|
|
21
21
|
* `actor_id` / `account_id` / `ip` boilerplate every role-grant-shape audit
|
|
22
22
|
* site repeated. Delegates to `emit`.
|
|
23
23
|
* - `emit_pool(input)` — awaitable pool write for code paths without a
|
|
24
|
-
* request
|
|
25
|
-
* then notifies.
|
|
24
|
+
* request (maintenance scripts). Writes, then notifies.
|
|
26
25
|
* - `notify(event)` — fan out an already-written audit row (e.g. rows
|
|
27
26
|
* returned by `query_accept_offer` that were inserted in-transaction by
|
|
28
27
|
* the query layer, or the offer-expiry rows `auth/cleanup.ts` writes in
|
|
29
28
|
* its sweep transaction). Runs every registered listener; per-listener
|
|
30
29
|
* throws are isolated.
|
|
31
|
-
* - `drain_inflight()` —
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
30
|
+
* - `drain_inflight()` — await every pool write in flight: what
|
|
31
|
+
* `AppServer.close` waits on (bounded) before the database closes, and the
|
|
32
|
+
* barrier behind the `_testing_drain_effects` test action.
|
|
33
|
+
* - `inflight_count()` — how many pool writes are in flight now.
|
|
35
34
|
*
|
|
36
35
|
* ## Where a row is written
|
|
37
36
|
*
|
|
38
|
-
* The routing reads
|
|
39
|
-
* transaction (`Db.in_transaction`).
|
|
37
|
+
* The routing reads the row's outcome, and for a success row whether `ctx.db`
|
|
38
|
+
* is a transaction (`Db.in_transaction`).
|
|
40
39
|
*
|
|
41
|
-
* - A **success** row
|
|
42
|
-
* `side_effects`, a route with `transaction: true
|
|
43
|
-
* transaction
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
40
|
+
* - A **success** row is written **in the request's transaction** — an action
|
|
41
|
+
* with `side_effects`, a route with `transaction: true`, or the transaction a
|
|
42
|
+
* `transaction: false` route opens around just its writes (`/login`,
|
|
43
|
+
* `/password`, signup, bootstrap, through
|
|
44
|
+
* `http/pending_effects.ts`'s `transaction_with_post_commit_rollback`),
|
|
45
|
+
* last before its commit. The change and the row that reports it commit
|
|
46
|
+
* together or not at all. The INSERT is awaited, and a failed one rejects
|
|
47
|
+
* `emit`, so the handler throws and its transaction rolls back — a
|
|
48
|
+
* committed change is never left unaudited, and a response, once sent, has
|
|
49
|
+
* its row committed. With no transaction open, `emit` refuses a success row
|
|
50
|
+
* by throwing, writing nothing (counted for `install_audit_drift_guard`,
|
|
51
|
+
* `get_audit_success_outside_transaction`).
|
|
47
52
|
* - A **failure** row is written on the pool, outside the request's
|
|
48
53
|
* transaction, so it survives the rollback that usually follows it — the
|
|
49
|
-
* forensic record of a refused attempt.
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
54
|
+
* forensic record of a refused attempt. No response waits for it;
|
|
55
|
+
* `AppServer.close` does, bounded (`drain_inflight`).
|
|
56
|
+
*
|
|
57
|
+
* `seq` is a `BIGSERIAL`, taken at INSERT: rows are numbered in INSERT order,
|
|
58
|
+
* not commit order, so a reader paging by `seq` can see a gap fill in when a
|
|
59
|
+
* transaction that inserted earlier commits later. What holds is that a
|
|
60
|
+
* success row is visible exactly when its change is.
|
|
55
61
|
*
|
|
56
62
|
* A pool write is fail-open: a failed one is counted
|
|
57
63
|
* (`get_audit_write_failures`) and logged as one `error` line carrying the
|
|
@@ -62,24 +68,18 @@
|
|
|
62
68
|
* than a request's, so a pool saturated enough to shed requests with a 503
|
|
63
69
|
* does not also drop the failure rows recording them.
|
|
64
70
|
*
|
|
65
|
-
* This is the Rust spine's split: `fuz_auth`'s `route_audit_emit` writes
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* routing between the twins, visible on the wire only when the audit INSERT
|
|
70
|
-
* fails: the TS `/logout` runs in a route transaction, so its success row is
|
|
71
|
-
* written in it (Rust writes it on the pool), and `db_admin_row_delete` is
|
|
72
|
-
* written on the pool after the commit on both (`http/db_routes.ts`, through
|
|
73
|
-
* `emit_pool`).
|
|
71
|
+
* This is the Rust spine's split: `fuz_auth`'s `route_audit_emit` writes a
|
|
72
|
+
* success row in the request transaction (`emit_in_tx`) — its REST handlers'
|
|
73
|
+
* and the db-admin row delete's included — and every failure row on the pool
|
|
74
|
+
* (`emit`).
|
|
74
75
|
*
|
|
75
76
|
* ## When listeners hear about a row
|
|
76
77
|
*
|
|
77
78
|
* - A **failure** row is announced as soon as it is written. It records an
|
|
78
79
|
* attempt, which happened whether or not the request's transaction commits.
|
|
79
80
|
* - A **success** row is announced only after the request's transaction
|
|
80
|
-
* commits
|
|
81
|
-
*
|
|
82
|
-
* `ctx.post_commit_effects`. A success row reports a state change, and the
|
|
81
|
+
* commits, and never when it rolls back: the fan-out is queued on
|
|
82
|
+
* `ctx.post_commit_effects`, discarded on a rollback. A success row reports a state change, and the
|
|
83
83
|
* listeners act on it — the revocation listeners close connections, the
|
|
84
84
|
* audit stream broadcasts it. Announced before the commit, a close could run
|
|
85
85
|
* while the revoked credential still reads as valid, and a rolled-back
|
|
@@ -165,6 +165,21 @@ export const to_dropped_audit_row = (input, config) => {
|
|
|
165
165
|
metadata
|
|
166
166
|
};
|
|
167
167
|
};
|
|
168
|
+
/**
|
|
169
|
+
* Process-wide counter for success rows written outside a transaction: an
|
|
170
|
+
* `emit` of one with no transaction open (which throws as well), and every
|
|
171
|
+
* `emit_pool` of one. A success row reports a change and belongs in that
|
|
172
|
+
* change's transaction; `install_audit_drift_guard` asserts this zero per
|
|
173
|
+
* test, so a pool-written success row fails the test that wrote it even when
|
|
174
|
+
* its caller swallowed the throw. Same lifetime as `audit_write_failures`.
|
|
175
|
+
*/
|
|
176
|
+
let audit_success_outside_transaction = 0;
|
|
177
|
+
/** Number of success rows emitted outside a transaction since process start. */
|
|
178
|
+
export const get_audit_success_outside_transaction = () => audit_success_outside_transaction;
|
|
179
|
+
/** Reset the counter — for tests only. */
|
|
180
|
+
export const reset_audit_success_outside_transaction = () => {
|
|
181
|
+
audit_success_outside_transaction = 0;
|
|
182
|
+
};
|
|
168
183
|
/** Number of audit rows an emitter failed to write since process start. */
|
|
169
184
|
export const get_audit_write_failures = () => audit_write_failures;
|
|
170
185
|
/** Reset the counter — for tests only. */
|
|
@@ -181,7 +196,7 @@ export const reset_audit_write_failures = () => {
|
|
|
181
196
|
* @returns the bound emitter; closes over the pool + config + listener chain
|
|
182
197
|
*/
|
|
183
198
|
export const create_audit_emitter = (options) => {
|
|
184
|
-
const { db, log, audit_log_config = builtin_audit_log_config, emit_decorator
|
|
199
|
+
const { db, log, audit_log_config = builtin_audit_log_config, emit_decorator } = options;
|
|
185
200
|
// Closure-private listener list — no mutable array is exposed on the
|
|
186
201
|
// returned (frozen) emitter; registration goes through `add_listener`.
|
|
187
202
|
// One entry object per registration, so a remover takes out its own
|
|
@@ -221,23 +236,19 @@ export const create_audit_emitter = (options) => {
|
|
|
221
236
|
if (event)
|
|
222
237
|
notify(event);
|
|
223
238
|
};
|
|
224
|
-
//
|
|
225
|
-
//
|
|
226
|
-
//
|
|
227
|
-
const inflight =
|
|
239
|
+
// Pool writes in flight — each removed as it settles, so the set is empty
|
|
240
|
+
// exactly when nothing is outstanding. No tracked promise rejects (`write`
|
|
241
|
+
// and `notify` log their failures).
|
|
242
|
+
const inflight = new Set();
|
|
228
243
|
const track = (promise) => {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
void promise.then(remove, remove);
|
|
235
|
-
}
|
|
244
|
+
inflight.add(promise);
|
|
245
|
+
const remove = () => {
|
|
246
|
+
inflight.delete(promise);
|
|
247
|
+
};
|
|
248
|
+
void promise.then(remove, remove);
|
|
236
249
|
return promise;
|
|
237
250
|
};
|
|
238
251
|
const drain_inflight = async () => {
|
|
239
|
-
if (!inflight)
|
|
240
|
-
return;
|
|
241
252
|
// Wait for zero, not for a snapshot: a write started while waiting —
|
|
242
253
|
// an emit from a post-commit thunk — joins the set and is awaited on
|
|
243
254
|
// the next pass. The removal reactions were registered before these,
|
|
@@ -246,32 +257,33 @@ export const create_audit_emitter = (options) => {
|
|
|
246
257
|
await Promise.allSettled(inflight);
|
|
247
258
|
}
|
|
248
259
|
};
|
|
249
|
-
const
|
|
250
|
-
const emit_pool = (input) =>
|
|
260
|
+
const inflight_count = () => inflight.size;
|
|
261
|
+
const emit_pool = (input) => {
|
|
262
|
+
if (input.outcome !== 'failure')
|
|
263
|
+
audit_success_outside_transaction++;
|
|
264
|
+
return track(write_and_notify(input));
|
|
265
|
+
};
|
|
266
|
+
// the routed failure write, which is not counted
|
|
267
|
+
const emit_failure_pool = (input) => track(write_and_notify(input));
|
|
251
268
|
// The routing — see the module doc, "Where a row is written".
|
|
252
269
|
const base_emit = async (ctx, input) => {
|
|
253
270
|
if (input.outcome === 'failure') {
|
|
254
271
|
// an attempt happened whether or not the transaction commits
|
|
255
|
-
ctx.pending_effects.push(
|
|
272
|
+
ctx.pending_effects.push(emit_failure_pool(input));
|
|
256
273
|
return;
|
|
257
274
|
}
|
|
258
|
-
if (ctx.db.in_transaction) {
|
|
259
|
-
//
|
|
260
|
-
//
|
|
261
|
-
//
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
return;
|
|
275
|
+
if (!ctx.db.in_transaction) {
|
|
276
|
+
// a success row reports a change, and is written with it — a row on
|
|
277
|
+
// the pool could land for a change that rolls back, or be lost for
|
|
278
|
+
// one that commits
|
|
279
|
+
audit_success_outside_transaction++;
|
|
280
|
+
throw new Error(`audit: a '${input.event_type}' success row was emitted with no transaction open — write it in the transaction of the change it reports`);
|
|
265
281
|
}
|
|
266
|
-
//
|
|
267
|
-
//
|
|
268
|
-
|
|
269
|
-
ctx.
|
|
270
|
-
emit_after_commit(ctx,
|
|
271
|
-
const event = await written;
|
|
272
|
-
if (event)
|
|
273
|
-
notify(event);
|
|
274
|
-
});
|
|
282
|
+
// Written in the request's transaction, so it commits or rolls back
|
|
283
|
+
// with the change it reports; a throw here fails the handler. The
|
|
284
|
+
// fan-out waits for the commit and is discarded on rollback.
|
|
285
|
+
const event = await query_audit_log({ db: ctx.db }, input, audit_log_config);
|
|
286
|
+
emit_after_commit(ctx, () => notify(event));
|
|
275
287
|
};
|
|
276
288
|
// The decorated `emit` is what `emit_role_grant_target` captures below
|
|
277
289
|
// and what gets exposed on the returned object — both call shapes
|
|
@@ -315,6 +327,6 @@ export const create_audit_emitter = (options) => {
|
|
|
315
327
|
add_listener,
|
|
316
328
|
listener_count,
|
|
317
329
|
drain_inflight,
|
|
318
|
-
|
|
330
|
+
inflight_count
|
|
319
331
|
});
|
|
320
332
|
};
|
|
@@ -51,10 +51,11 @@ export type RevocationScope = 'none' | 'session' | 'token' | 'account' | 'role';
|
|
|
51
51
|
* until it declares its scope here.
|
|
52
52
|
*
|
|
53
53
|
* A scope other than `none` marks an event whose success invalidates a live
|
|
54
|
-
* connection's credential or access: `
|
|
54
|
+
* connection's credential or access: `password_change`,
|
|
55
55
|
* `session_revoke_all`, `token_revoke_all`, `account_delete`, and
|
|
56
|
-
* `account_purge` are account-wide, `session_revoke
|
|
57
|
-
* the one credential they ended, and `role_grant_revoke`
|
|
56
|
+
* `account_purge` are account-wide, `session_revoke`, `logout` and
|
|
57
|
+
* `token_revoke` name the one credential they ended, and `role_grant_revoke`
|
|
58
|
+
* names the lost role.
|
|
58
59
|
* Granting access revokes nothing, and neither does an event about a subject
|
|
59
60
|
* other than a connection's identity.
|
|
60
61
|
*
|
|
@@ -129,7 +130,9 @@ export declare const audit_metadata_schemas: Readonly<{
|
|
|
129
130
|
login: import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").LooseObjectSchema<{
|
|
130
131
|
username: import("@fuzdev/fuz_schema/schema.ts").StringSchema;
|
|
131
132
|
}>>;
|
|
132
|
-
logout: import("@fuzdev/fuz_schema/schema.ts").
|
|
133
|
+
logout: import("@fuzdev/fuz_schema/schema.ts").LooseObjectSchema<{
|
|
134
|
+
session_id: import("@fuzdev/fuz_schema/schema.ts").BrandSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>, "SessionId">;
|
|
135
|
+
}>;
|
|
133
136
|
bootstrap: import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").LooseObjectSchema<{
|
|
134
137
|
error: import("@fuzdev/fuz_schema/schema.ts").StringSchema;
|
|
135
138
|
}>>;
|
|
@@ -227,7 +230,9 @@ export declare const audit_metadata_schemas: Readonly<{
|
|
|
227
230
|
username: import("@fuzdev/fuz_schema/schema.ts").NullableSchema<import("@fuzdev/fuz_schema/schema.ts").StringSchema>;
|
|
228
231
|
}>;
|
|
229
232
|
invite_delete: import("@fuzdev/fuz_schema/schema.ts").LooseObjectSchema<{
|
|
230
|
-
invite_id: import("@fuzdev/fuz_schema/schema.ts").BrandSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>, "Uuid"
|
|
233
|
+
invite_id: import("@fuzdev/fuz_schema/schema.ts").OptionalSchema<import("@fuzdev/fuz_schema/schema.ts").BrandSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>, "Uuid">>;
|
|
234
|
+
reason: import("@fuzdev/fuz_schema/schema.ts").OptionalSchema<import("@fuzdev/fuz_schema/schema.ts").StringSchema>;
|
|
235
|
+
attempted_invite_id: import("@fuzdev/fuz_schema/schema.ts").OptionalSchema<import("@fuzdev/fuz_schema/schema.ts").BrandSchema<import("@fuzdev/fuz_schema/schema.ts").PipeSchema<string, import("@fuzdev/fuz_schema/schema.ts").StringSchema>, "Uuid">>;
|
|
231
236
|
}>;
|
|
232
237
|
account_delete: import("@fuzdev/fuz_schema/schema.ts").LooseObjectSchema<{
|
|
233
238
|
username: import("@fuzdev/fuz_schema/schema.ts").OptionalSchema<import("@fuzdev/fuz_schema/schema.ts").StringSchema>;
|