@fuzdev/fuz_app 0.152.0 → 0.153.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.
Files changed (159) hide show
  1. package/dist/actions/CLAUDE.md +14 -11
  2. package/dist/actions/action_rpc.js +3 -3
  3. package/dist/actions/action_types.d.ts +3 -3
  4. package/dist/actions/broadcast_api.d.ts +0 -6
  5. package/dist/actions/broadcast_api.d.ts.map +1 -1
  6. package/dist/actions/connection_closer.d.ts +14 -15
  7. package/dist/actions/connection_closer.d.ts.map +1 -1
  8. package/dist/actions/connection_closer.js +13 -13
  9. package/dist/actions/perform_action.d.ts +5 -1
  10. package/dist/actions/perform_action.d.ts.map +1 -1
  11. package/dist/actions/perform_action.js +57 -19
  12. package/dist/actions/register_action_ws.d.ts +6 -5
  13. package/dist/actions/register_action_ws.d.ts.map +1 -1
  14. package/dist/actions/register_action_ws.js +5 -6
  15. package/dist/auth/CLAUDE.md +44 -14
  16. package/dist/auth/account_actions.d.ts +2 -2
  17. package/dist/auth/account_actions.js +6 -6
  18. package/dist/auth/account_routes.js +9 -9
  19. package/dist/auth/admin_actions.d.ts +2 -1
  20. package/dist/auth/admin_actions.d.ts.map +1 -1
  21. package/dist/auth/admin_actions.js +21 -20
  22. package/dist/auth/audit_emitter.d.ts +111 -64
  23. package/dist/auth/audit_emitter.d.ts.map +1 -1
  24. package/dist/auth/audit_emitter.js +130 -55
  25. package/dist/auth/audit_log_queries.d.ts +9 -10
  26. package/dist/auth/audit_log_queries.d.ts.map +1 -1
  27. package/dist/auth/audit_log_queries.js +13 -10
  28. package/dist/auth/audit_log_routes.js +2 -2
  29. package/dist/auth/audit_log_schema.d.ts +6 -1
  30. package/dist/auth/audit_log_schema.d.ts.map +1 -1
  31. package/dist/auth/bootstrap_account.d.ts +4 -3
  32. package/dist/auth/bootstrap_account.d.ts.map +1 -1
  33. package/dist/auth/bootstrap_account.js +4 -3
  34. package/dist/auth/bootstrap_routes.js +2 -2
  35. package/dist/auth/cell_action_helpers.d.ts +3 -2
  36. package/dist/auth/cell_action_helpers.d.ts.map +1 -1
  37. package/dist/auth/cell_action_helpers.js +9 -10
  38. package/dist/auth/cell_actions.js +6 -6
  39. package/dist/auth/cell_data_schema.d.ts +5 -0
  40. package/dist/auth/cell_data_schema.d.ts.map +1 -1
  41. package/dist/auth/cell_data_schema.js +5 -0
  42. package/dist/auth/cell_field_actions.js +2 -2
  43. package/dist/auth/cell_grant_actions.js +2 -2
  44. package/dist/auth/cell_item_actions.js +3 -3
  45. package/dist/auth/deps.d.ts +12 -10
  46. package/dist/auth/deps.d.ts.map +1 -1
  47. package/dist/auth/middleware.js +1 -1
  48. package/dist/auth/request_context.d.ts +5 -10
  49. package/dist/auth/request_context.d.ts.map +1 -1
  50. package/dist/auth/request_context.js +10 -9
  51. package/dist/auth/role_grant_offer_actions.d.ts +11 -8
  52. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  53. package/dist/auth/role_grant_offer_actions.js +57 -41
  54. package/dist/auth/self_service_role_actions.d.ts +6 -5
  55. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  56. package/dist/auth/self_service_role_actions.js +17 -6
  57. package/dist/auth/session_cookie.d.ts +4 -4
  58. package/dist/auth/session_cookie.d.ts.map +1 -1
  59. package/dist/auth/session_cookie.js +0 -1
  60. package/dist/auth/session_middleware.d.ts +8 -5
  61. package/dist/auth/session_middleware.d.ts.map +1 -1
  62. package/dist/auth/session_middleware.js +10 -7
  63. package/dist/auth/signup_routes.js +6 -6
  64. package/dist/auth/standard_rpc_actions.d.ts +3 -2
  65. package/dist/auth/standard_rpc_actions.d.ts.map +1 -1
  66. package/dist/auth/standard_rpc_actions.js +3 -2
  67. package/dist/db/CLAUDE.md +10 -1
  68. package/dist/db/create_db.d.ts +18 -1
  69. package/dist/db/create_db.d.ts.map +1 -1
  70. package/dist/db/create_db.js +15 -4
  71. package/dist/db/db.d.ts +27 -0
  72. package/dist/db/db.d.ts.map +1 -1
  73. package/dist/db/db.js +26 -0
  74. package/dist/db/db_checkout_error.d.ts +36 -0
  75. package/dist/db/db_checkout_error.d.ts.map +1 -0
  76. package/dist/db/db_checkout_error.js +48 -0
  77. package/dist/db/db_pg.d.ts +83 -5
  78. package/dist/db/db_pg.d.ts.map +1 -1
  79. package/dist/db/db_pg.js +119 -9
  80. package/dist/db/pg_error.d.ts +4 -0
  81. package/dist/db/pg_error.d.ts.map +1 -1
  82. package/dist/db/pg_error.js +13 -3
  83. package/dist/dev/setup.d.ts +4 -2
  84. package/dist/dev/setup.d.ts.map +1 -1
  85. package/dist/dev/setup.js +12 -5
  86. package/dist/hono_context.d.ts +36 -6
  87. package/dist/hono_context.d.ts.map +1 -1
  88. package/dist/hono_context.js +29 -2
  89. package/dist/http/CLAUDE.md +11 -10
  90. package/dist/http/db_routes.d.ts +7 -12
  91. package/dist/http/db_routes.d.ts.map +1 -1
  92. package/dist/http/db_routes.js +32 -17
  93. package/dist/http/error_schemas.d.ts +12 -0
  94. package/dist/http/error_schemas.d.ts.map +1 -1
  95. package/dist/http/error_schemas.js +12 -0
  96. package/dist/http/jsonrpc_errors.d.ts +10 -0
  97. package/dist/http/jsonrpc_errors.d.ts.map +1 -1
  98. package/dist/http/jsonrpc_errors.js +14 -0
  99. package/dist/http/jsonrpc_helpers.d.ts +4 -0
  100. package/dist/http/jsonrpc_helpers.d.ts.map +1 -1
  101. package/dist/http/jsonrpc_helpers.js +8 -1
  102. package/dist/http/route_spec.d.ts +4 -4
  103. package/dist/http/route_spec.d.ts.map +1 -1
  104. package/dist/http/route_spec.js +20 -3
  105. package/dist/http/schema_helpers.d.ts +21 -0
  106. package/dist/http/schema_helpers.d.ts.map +1 -1
  107. package/dist/http/schema_helpers.js +38 -0
  108. package/dist/realtime/audit_log_sse.d.ts +2 -2
  109. package/dist/realtime/audit_log_sse.js +2 -2
  110. package/dist/server/app_backend.d.ts +25 -1
  111. package/dist/server/app_backend.d.ts.map +1 -1
  112. package/dist/server/app_backend.js +16 -3
  113. package/dist/server/app_error_handler.d.ts +30 -0
  114. package/dist/server/app_error_handler.d.ts.map +1 -0
  115. package/dist/server/app_error_handler.js +41 -0
  116. package/dist/server/app_server.d.ts +1 -1
  117. package/dist/server/app_server.d.ts.map +1 -1
  118. package/dist/server/app_server.js +6 -1
  119. package/dist/server/env.d.ts +8 -3
  120. package/dist/server/env.d.ts.map +1 -1
  121. package/dist/server/env.js +26 -4
  122. package/dist/testing/CLAUDE.md +22 -16
  123. package/dist/testing/audit_drift_guard.d.ts.map +1 -1
  124. package/dist/testing/audit_drift_guard.js +2 -0
  125. package/dist/testing/auth_apps.d.ts.map +1 -1
  126. package/dist/testing/auth_apps.js +2 -2
  127. package/dist/testing/cross_backend/conformance_case.d.ts +2 -0
  128. package/dist/testing/cross_backend/conformance_case.d.ts.map +1 -1
  129. package/dist/testing/cross_backend/conformance_case.js +8 -0
  130. package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
  131. package/dist/testing/cross_backend/conformance_table.js +24 -7
  132. package/dist/testing/cross_backend/default_backend_configs.d.ts +4 -0
  133. package/dist/testing/cross_backend/default_backend_configs.d.ts.map +1 -1
  134. package/dist/testing/cross_backend/default_backend_configs.js +7 -0
  135. package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts +11 -0
  136. package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts.map +1 -1
  137. package/dist/testing/cross_backend/testing_server_core.d.ts +1 -1
  138. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts +15 -11
  139. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts.map +1 -1
  140. package/dist/testing/cross_backend/ts_spine_backend_config.js +3 -12
  141. package/dist/testing/daemon_token_rotation.d.ts +4 -14
  142. package/dist/testing/daemon_token_rotation.d.ts.map +1 -1
  143. package/dist/testing/daemon_token_rotation.js +0 -12
  144. package/dist/testing/db.d.ts.map +1 -1
  145. package/dist/testing/db.js +7 -3
  146. package/dist/testing/middleware.d.ts.map +1 -1
  147. package/dist/testing/middleware.js +2 -2
  148. package/dist/testing/rpc_helpers.d.ts +19 -0
  149. package/dist/testing/rpc_helpers.d.ts.map +1 -1
  150. package/dist/testing/rpc_helpers.js +25 -2
  151. package/dist/testing/schema_parity.d.ts +1 -1
  152. package/dist/testing/stubs.js +2 -2
  153. package/dist/testing/surface_invariants.js +1 -1
  154. package/dist/testing/ws_round_trip.d.ts.map +1 -1
  155. package/dist/testing/ws_round_trip.js +3 -3
  156. package/dist/zod.d.ts +9 -0
  157. package/dist/zod.d.ts.map +1 -1
  158. package/dist/zod.js +1 -1
  159. package/package.json +3 -7
@@ -37,6 +37,7 @@
37
37
  * @module
38
38
  */
39
39
  import { rpc_action } from '../actions/action_types.js';
40
+ import { queue_connection_close } from '../actions/connection_closer.js';
40
41
  import { jsonrpc_errors } from '../http/jsonrpc_errors.js';
41
42
  import { builtin_role_specs_by_name, is_builtin_role, list_roles_with_grant_path, RoleName } from './role_schema.js';
42
43
  import { GRANT_PATH_SELF_SERVICE } from './grant_path_schema.js';
@@ -69,9 +70,10 @@ export const validate_self_service_eligible_roles = (roles) => {
69
70
  /**
70
71
  * Build the unified self-service role toggle RPC action.
71
72
  *
72
- * @param deps - `ActionFactoryDeps` (`log`, `audit`); `audit.emit` writes
73
- * audit rows via the captured pool. The bound emitter encapsulates
74
- * `on_audit_event` fan-out and the optional `AuditLogConfig`.
73
+ * @param deps - `RevokingActionFactoryDeps` (`log`, `audit`,
74
+ * `connection_closer`); `audit.emit` writes a success row in the request
75
+ * transaction and a failure row via the captured pool, and a disable closes the caller's connections gated on the role
76
+ * through `connection_closer`.
75
77
  * @param options - optional eligible-role override plus optional role schema for default-eligibility derivation
76
78
  * @returns the `RpcAction` array to spread into a `create_rpc_endpoint` call
77
79
  * @throws Error at factory time if any `eligible_roles` entry is missing from `options.roles.role_specs`
@@ -127,7 +129,7 @@ export const create_self_service_role_actions = (deps, options = {}) => {
127
129
  // grantor and grantee are the same identity; admin direct-grant
128
130
  // (separate code path) populates the same columns with the
129
131
  // grantee actor.
130
- deps.audit.emit(ctx, {
132
+ await deps.audit.emit(ctx, {
131
133
  event_type: 'role_grant_create',
132
134
  actor_id: auth.actor.id,
133
135
  account_id: auth.account.id,
@@ -158,11 +160,20 @@ export const create_self_service_role_actions = (deps, options = {}) => {
158
160
  // Raced with another revoker — treat as already revoked.
159
161
  return { ok: true, enabled: false, changed: false };
160
162
  }
163
+ // Close the caller's connections gated on the role once the revoke has
164
+ // committed, not through the audit row's announcement below — the
165
+ // admin revoke's rule (`role_grant_offer_actions.ts`). The grant is
166
+ // global, the only kind this surface toggles.
167
+ queue_connection_close(ctx, deps.connection_closer, {
168
+ kind: 'role',
169
+ account_id: auth.account.id,
170
+ role: result.role
171
+ });
161
172
  // Same actor-bound rule as the grant branch — `role_grant_revoke`
162
173
  // always populates both target columns even on self-service so
163
174
  // forensic queries that filter on `target_actor_id IS NOT NULL`
164
175
  // don't silently miss self-toggled role_grants.
165
- deps.audit.emit(ctx, {
176
+ await deps.audit.emit(ctx, {
166
177
  event_type: 'role_grant_revoke',
167
178
  actor_id: auth.actor.id,
168
179
  account_id: auth.account.id,
@@ -183,7 +194,7 @@ export const create_self_service_role_actions = (deps, options = {}) => {
183
194
  // both offerable and self-service-eligible has siblings). The WS supersede
184
195
  // notification to each grantor is not sent (twin of the Rust path).
185
196
  for (const offer of result.superseded_offers) {
186
- deps.audit.emit_role_grant_target(ctx, auth, {
197
+ await deps.audit.emit_role_grant_target(ctx, auth, {
187
198
  event_type: 'role_grant_offer_supersede',
188
199
  target_account_id: offer.to_account_id,
189
200
  target_actor_id: offer.to_actor_id,
@@ -52,12 +52,15 @@ export declare const session_cookie_options: SessionCookieOptions;
52
52
  * requiring no server-side session state. Simpler, but individual sessions
53
53
  * can only be invalidated by rotating the signing key (which invalidates all sessions).
54
54
  *
55
+ * The cookie helpers here take either. The spine's session middleware and
56
+ * auth chain take `SessionOptions<string>` — the identity is the session token
57
+ * the session leg hashes to find its `auth_session` row (`create_session_config`).
58
+ *
55
59
  * @example
56
60
  * ```ts
57
61
  * // zap: 3-part format (admin:session_id)
58
62
  * const zap_config: SessionOptions<string> = {
59
63
  * cookie_name: 'zap_session',
60
- * context_key: 'auth_session_id',
61
64
  * encode_identity: (session_id) => `admin:${session_id}`,
62
65
  * decode_identity: (payload) => {
63
66
  * const parts = payload.split(':');
@@ -69,7 +72,6 @@ export declare const session_cookie_options: SessionCookieOptions;
69
72
  * // visiones: 1-part format (account_id)
70
73
  * const visiones_config: SessionOptions<number> = {
71
74
  * cookie_name: 'session_id',
72
- * context_key: 'auth_session_id',
73
75
  * encode_identity: (id) => String(id),
74
76
  * decode_identity: (payload) => {
75
77
  * const n = parseInt(payload, 10);
@@ -80,8 +82,6 @@ export declare const session_cookie_options: SessionCookieOptions;
80
82
  */
81
83
  export interface SessionOptions<TIdentity> {
82
84
  cookie_name: string;
83
- /** Hono context variable name for the identity. */
84
- context_key: string;
85
85
  /**
86
86
  * Cookie lifetime in seconds. Single source of truth for both the embedded
87
87
  * `expires_at` (via `create_session_cookie_value`) and the cookie's HTTP
@@ -1 +1 @@
1
- {"version":3,"file":"session_cookie.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/auth/session_cookie.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,QAAoB,CAAC;AAWjD;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;IAClB,MAAM,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAC;IACpC,MAAM,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,EAAE,oBAMpC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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B,EAAE,cAAc,CAAC,MAAM,CAAwC,CAAC;AAE/F;;;;;;;;;;;;;;;;;GAiBG;AAEH,eAAO,MAAM,sBAAsB,GAAU,SAAS,EACrD,cAAc,MAAM,GAAG,SAAS,EAChC,SAAS,OAAO,EAChB,SAAS,cAAc,CAAC,SAAS,CAAC,EAClC,cAAc,MAAM,KAClB,OAAO,CAAC,oBAAoB,CAAC,SAAS,CAAC,CAgBzC,CAAC"}
@@ -116,7 +116,6 @@ export const create_session_cookie_value = async (keyring, identity, options, no
116
116
  */
117
117
  export const create_session_config = (cookie_name) => ({
118
118
  cookie_name,
119
- context_key: 'auth_session_id',
120
119
  encode_identity: (session_id) => session_id,
121
120
  decode_identity: (payload) => payload || null
122
121
  });
@@ -28,16 +28,19 @@ export declare const set_session_cookie: <T>(c: Context, value: string, options:
28
28
  */
29
29
  export declare const clear_session_cookie: <T>(c: Context, options: SessionOptions<T>) => void;
30
30
  /**
31
- * Create session middleware that parses cookies and sets identity on context.
31
+ * Create session middleware that parses cookies and sets the session token on
32
+ * context.
32
33
  *
33
- * Always sets the identity on context (null when invalid/missing) for type-safe reads.
34
- * Uses `options.context_key` as the Hono context variable name.
34
+ * Always sets `SESSION_TOKEN_KEY` (null when invalid/missing) for type-safe
35
+ * reads — the key the session leg (`create_request_context_middleware`)
36
+ * reads. The identity is the session token, so the options are a
37
+ * `SessionOptions<string>`.
35
38
  *
36
39
  * @param keyring - key ring for cookie verification
37
40
  * @param options - session configuration
38
- * @mutates Hono context - sets `options.context_key` and `SESSION_COOKIE_EXPIRES_AT_KEY`, and may clear the session cookie
41
+ * @mutates Hono context - sets `SESSION_TOKEN_KEY` and `SESSION_COOKIE_EXPIRES_AT_KEY`, and may clear the session cookie
39
42
  */
40
- export declare const create_session_middleware: <TIdentity>(keyring: Keyring, options: SessionOptions<TIdentity>) => MiddlewareHandler;
43
+ export declare const create_session_middleware: (keyring: Keyring, options: SessionOptions<string>) => MiddlewareHandler;
41
44
  /**
42
45
  * Options for `create_session_and_set_cookie`.
43
46
  */
@@ -1 +1 @@
1
- {"version":3,"file":"session_middleware.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/auth/session_middleware.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAGvD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EACN,KAAK,cAAc,EAKnB,MAAM,qBAAqB,CAAC;AAQ7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAGrD;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,EACnC,GAAG,OAAO,EACV,SAAS,cAAc,CAAC,CAAC,CAAC,KACxB,MAAM,GAAG,SAEX,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,EACnC,GAAG,OAAO,EACV,OAAO,MAAM,EACb,SAAS,cAAc,CAAC,CAAC,CAAC,KACxB,IAOF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,GAAI,CAAC,EAAE,GAAG,OAAO,EAAE,SAAS,cAAc,CAAC,CAAC,CAAC,KAAG,IAMhF,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,yBAAyB,GAAI,SAAS,EAClD,SAAS,OAAO,EAChB,SAAS,cAAc,CAAC,SAAS,CAAC,KAChC,iBAgBF,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,gCAAgC;IAChD,kCAAkC;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,kDAAkD;IAClD,IAAI,EAAE,SAAS,CAAC;IAChB,2CAA2C;IAC3C,CAAC,EAAE,OAAO,CAAC;IACX,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAC;IACnB,oCAAoC;IACpC,eAAe,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;IACxC,4DAA4D;IAC5D,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,6BAA6B,GACzC,SAAS,gCAAgC,KACvC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAavB,CAAC"}
1
+ {"version":3,"file":"session_middleware.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/auth/session_middleware.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAGvD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EACN,KAAK,cAAc,EAKnB,MAAM,qBAAqB,CAAC;AAQ7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAGrD;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,EACnC,GAAG,OAAO,EACV,SAAS,cAAc,CAAC,CAAC,CAAC,KACxB,MAAM,GAAG,SAEX,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,EACnC,GAAG,OAAO,EACV,OAAO,MAAM,EACb,SAAS,cAAc,CAAC,CAAC,CAAC,KACxB,IAOF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,GAAI,CAAC,EAAE,GAAG,OAAO,EAAE,SAAS,cAAc,CAAC,CAAC,CAAC,KAAG,IAMhF,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,yBAAyB,GACrC,SAAS,OAAO,EAChB,SAAS,cAAc,CAAC,MAAM,CAAC,KAC7B,iBAgBF,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,gCAAgC;IAChD,kCAAkC;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,kDAAkD;IAClD,IAAI,EAAE,SAAS,CAAC;IAChB,2CAA2C;IAC3C,CAAC,EAAE,OAAO,CAAC;IACX,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAC;IACnB,oCAAoC;IACpC,eAAe,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;IACxC,4DAA4D;IAC5D,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,6BAA6B,GACzC,SAAS,gCAAgC,KACvC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAavB,CAAC"}
@@ -7,7 +7,7 @@
7
7
  import { getCookie, setCookie, deleteCookie } from 'hono/cookie';
8
8
  import { session_cookie_options, process_session_cookie, create_session_cookie_value } from './session_cookie.js';
9
9
  import { generate_session_token, hash_session_token, AUTH_SESSION_LIFETIME_MS, query_create_session, query_session_enforce_limit } from './session_queries.js';
10
- import { SESSION_COOKIE_EXPIRES_AT_KEY } from '../hono_context.js';
10
+ import { SESSION_COOKIE_EXPIRES_AT_KEY, SESSION_TOKEN_KEY } from '../hono_context.js';
11
11
  /**
12
12
  * Read the session cookie value from a request.
13
13
  */
@@ -43,21 +43,24 @@ export const clear_session_cookie = (c, options) => {
43
43
  deleteCookie(c, options.cookie_name, cookie_options);
44
44
  };
45
45
  /**
46
- * Create session middleware that parses cookies and sets identity on context.
46
+ * Create session middleware that parses cookies and sets the session token on
47
+ * context.
47
48
  *
48
- * Always sets the identity on context (null when invalid/missing) for type-safe reads.
49
- * Uses `options.context_key` as the Hono context variable name.
49
+ * Always sets `SESSION_TOKEN_KEY` (null when invalid/missing) for type-safe
50
+ * reads — the key the session leg (`create_request_context_middleware`)
51
+ * reads. The identity is the session token, so the options are a
52
+ * `SessionOptions<string>`.
50
53
  *
51
54
  * @param keyring - key ring for cookie verification
52
55
  * @param options - session configuration
53
- * @mutates Hono context - sets `options.context_key` and `SESSION_COOKIE_EXPIRES_AT_KEY`, and may clear the session cookie
56
+ * @mutates Hono context - sets `SESSION_TOKEN_KEY` and `SESSION_COOKIE_EXPIRES_AT_KEY`, and may clear the session cookie
54
57
  */
55
58
  export const create_session_middleware = (keyring, options) => {
56
59
  return async (c, next) => {
57
60
  const signed_value = get_session_cookie(c, options);
58
61
  const result = await process_session_cookie(signed_value, keyring, options);
59
- // Always set identity (null when invalid/missing) for type-safe reads
60
- c.set(options.context_key, result.identity ?? null);
62
+ // Always set the token (null when invalid/missing) for type-safe reads
63
+ c.set(SESSION_TOKEN_KEY, result.identity ?? null);
61
64
  // the signed expiry rides beside it, for the credential deadline
62
65
  c.set(SESSION_COOKIE_EXPIRES_AT_KEY, result.expires_at ?? null);
63
66
  if (result.action === 'clear') {
@@ -110,8 +110,8 @@ export const create_signup_route_specs = (deps, options) => {
110
110
  // branch can still reference `invite.id` after the tx rolls
111
111
  // back on PG unique violation.
112
112
  let invite;
113
- const emit_failure_audit = (reason) => {
114
- deps.audit.emit(route, {
113
+ const emit_failure_audit = async (reason) => {
114
+ await deps.audit.emit(route, {
115
115
  event_type: 'signup',
116
116
  outcome: 'failure',
117
117
  ip: get_client_ip(c),
@@ -172,7 +172,7 @@ export const create_signup_route_specs = (deps, options) => {
172
172
  if (e instanceof NoMatchingInviteError) {
173
173
  ip_reserved.reservation.record();
174
174
  account_reservation?.record();
175
- emit_failure_audit('no_match');
175
+ await emit_failure_audit('no_match');
176
176
  await delay;
177
177
  return c.json({ error: ERROR_NO_MATCHING_INVITE }, 403);
178
178
  }
@@ -180,7 +180,7 @@ export const create_signup_route_specs = (deps, options) => {
180
180
  if (is_pg_unique_violation(e)) {
181
181
  ip_reserved.reservation.record();
182
182
  account_reservation?.record();
183
- emit_failure_audit('signup_conflict');
183
+ await emit_failure_audit('signup_conflict');
184
184
  await delay;
185
185
  return c.json({ error: ERROR_SIGNUP_CONFLICT }, 409);
186
186
  }
@@ -191,7 +191,7 @@ export const create_signup_route_specs = (deps, options) => {
191
191
  // `internal_error` before rethrowing. 5xx responses are
192
192
  // not floored: they aren't response-time-controlled
193
193
  // enumeration oracles.
194
- emit_failure_audit('internal_error');
194
+ await emit_failure_audit('internal_error');
195
195
  throw e;
196
196
  }
197
197
  // Clear the account-grain bucket only — the IP aggregate stands.
@@ -212,7 +212,7 @@ export const create_signup_route_specs = (deps, options) => {
212
212
  // dry); the `finally` releases it.
213
213
  if (open_signup)
214
214
  ip_reserved.reservation.record();
215
- deps.audit.emit(route, {
215
+ await deps.audit.emit(route, {
216
216
  event_type: 'signup',
217
217
  account_id: result.account.id,
218
218
  ip: get_client_ip(c),
@@ -11,8 +11,9 @@
11
11
  * `default_ttl_ms` and `authorize` go to role-grant-offer only; `max_tokens`
12
12
  * goes to account only;
13
13
  * `notification_sender` reaches role-grant-offer transparently (admin + account
14
- * ignore it). `deps.connection_closer` is read by admin + account, whose
15
- * revocation handlers close live connections through it.
14
+ * ignore it). `deps.connection_closer` is read by admin, account, and
15
+ * role-grant-offer, whose revocation handlers close live connections through
16
+ * it.
16
17
  *
17
18
  * Paired with `create_admin_rpc_adapters` on the UI side.
18
19
  *
@@ -1 +1 @@
1
- {"version":3,"file":"standard_rpc_actions.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/auth/standard_rpc_actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAwB,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACnF,OAAO,EAEN,KAAK,2BAA2B,EAChC,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAA0B,KAAK,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACzF,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAC9E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAE5D;;;;;;;;GAQG;AACH,MAAM,WAAW,yBAChB,SAAQ,kBAAkB,EAAE,2BAA2B,EAAE,oBAAoB;CAAG;AAEjF;;;;;;;GAOG;AACH,MAAM,WAAW,sBAAuB,SAAQ,yBAAyB;IACxE,mBAAmB,CAAC,EAAE,kBAAkB,GAAG,IAAI,CAAC;CAChD;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,2BAA2B,GACvC,MAAM,sBAAsB,EAC5B,UAAS,yBAA8B,KACrC,KAAK,CAAC,SAAS,CAIjB,CAAC"}
1
+ {"version":3,"file":"standard_rpc_actions.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/auth/standard_rpc_actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAwB,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACnF,OAAO,EAEN,KAAK,2BAA2B,EAChC,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAA0B,KAAK,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACzF,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAC9E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAE5D;;;;;;;;GAQG;AACH,MAAM,WAAW,yBAChB,SAAQ,kBAAkB,EAAE,2BAA2B,EAAE,oBAAoB;CAAG;AAEjF;;;;;;;GAOG;AACH,MAAM,WAAW,sBAAuB,SAAQ,yBAAyB;IACxE,mBAAmB,CAAC,EAAE,kBAAkB,GAAG,IAAI,CAAC;CAChD;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,2BAA2B,GACvC,MAAM,sBAAsB,EAC5B,UAAS,yBAA8B,KACrC,KAAK,CAAC,SAAS,CAIjB,CAAC"}
@@ -11,8 +11,9 @@
11
11
  * `default_ttl_ms` and `authorize` go to role-grant-offer only; `max_tokens`
12
12
  * goes to account only;
13
13
  * `notification_sender` reaches role-grant-offer transparently (admin + account
14
- * ignore it). `deps.connection_closer` is read by admin + account, whose
15
- * revocation handlers close live connections through it.
14
+ * ignore it). `deps.connection_closer` is read by admin, account, and
15
+ * role-grant-offer, whose revocation handlers close live connections through
16
+ * it.
16
17
  *
17
18
  * Paired with `create_admin_rpc_adapters` on the UI side.
18
19
  *
package/dist/db/CLAUDE.md CHANGED
@@ -14,10 +14,19 @@ the code.
14
14
 
15
15
  - `db.ts` — `Db` abstraction over pg, PGlite and pglet, `query` / `query_one` /
16
16
  `transaction`, `no_nested_transaction` guard.
17
+ - `db_checkout_error.ts` — `DbCheckoutError` (a failed pool checkout,
18
+ realm-global brand) + `is_db_checkout_error`; `to_thrown_jsonrpc_error` maps
19
+ it to `service_unavailable` (503), the route catch and the server's
20
+ `onError` to the flat `{error: 'service_unavailable'}`.
17
21
  - `create_db.ts` — URL-based driver auto-detection (`postgres` /
18
22
  `pglite-file` for `file://` / `pglet-memory` for `memory://`, over
19
23
  `@fuzdev/pglet-wasm`). An in-memory PGlite is `create_pglite_db(new PGlite())`.
20
- - `db_pg.ts` / `db_pglite.ts` / `db_pglet.ts` — driver adapters. `db_pg.ts` also
24
+ - `db_pg.ts` / `db_pglite.ts` / `db_pglet.ts` — driver adapters. `db_pg.ts`'s
25
+ `create_pg_pool` builds the pool with a bounded checkout
26
+ (`connectionTimeoutMillis`, `DEFAULT_DB_CHECKOUT_TIMEOUT_MS`) whose failure
27
+ throws `DbCheckoutError`, plus `query_with_checkout_timeout` behind
28
+ `Db.with_checkout_timeout` (the audit emitter's longer wait,
29
+ `DEFAULT_DB_AUDIT_CHECKOUT_TIMEOUT_MS`); PGlite and pglet have no pool. It also
21
30
  exports `register_pg_type_parsers`, the shared int8→number coercion (so
22
31
  `BIGSERIAL` columns read as JS numbers, matching PGlite) — called by both
23
32
  `create_db` and the test pg factory (`testing/db.ts`) so prod and tests
@@ -14,6 +14,15 @@
14
14
  * @module
15
15
  */
16
16
  import type { Db, DbType } from './db.js';
17
+ /** Options for `create_db`. */
18
+ export interface CreateDbOptions {
19
+ /**
20
+ * Bound on every pool checkout, in ms (`db/db_pg.ts`, "Checkout timeout").
21
+ * Default `DEFAULT_DB_CHECKOUT_TIMEOUT_MS`. Postgres only: PGlite and pglet
22
+ * have no pool, so it has no effect there.
23
+ */
24
+ checkout_timeout_ms?: number;
25
+ }
17
26
  /** Result of database initialization. */
18
27
  export interface CreateDbResult {
19
28
  db: Db;
@@ -33,10 +42,18 @@ export interface CreateDbResult {
33
42
  * or `create_pglet_db_driver` from `db/db_pglet.ts`.
34
43
  *
35
44
  * @param database_url - connection URL (`postgres://`, `postgresql://`, `file://`, or `memory://`)
45
+ * @param options - the pool's checkout bound (Postgres only)
36
46
  * @returns database instance, close callback, type, and display name
37
47
  * @throws Error if `database_url` uses an unsupported scheme. Driver
38
48
  * construction (`pg.Pool`, `PGlite` or pglet) may also throw on bad connection
39
49
  * parameters or missing peer-dependency packages.
40
50
  */
41
- export declare const create_db: (database_url: string) => Promise<CreateDbResult>;
51
+ export declare const create_db: (database_url: string, options?: CreateDbOptions) => Promise<CreateDbResult>;
52
+ /**
53
+ * Whether `database_url` names a Postgres server — the one driver with a pool.
54
+ *
55
+ * @param database_url - a database URL
56
+ * @returns `true` for `postgres://` and `postgresql://`
57
+ */
58
+ export declare const is_pg_database_url: (database_url: string) => boolean;
42
59
  //# sourceMappingURL=create_db.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create_db.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/db/create_db.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAK1C,yCAAyC;AACzC,MAAM,WAAW,cAAc;IAC9B,EAAE,EAAE,EAAE,CAAC;IACP,iFAAiF;IACjF,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,SAAS,GAAU,cAAc,MAAM,KAAG,OAAO,CAAC,cAAc,CAiC5E,CAAC"}
1
+ {"version":3,"file":"create_db.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/db/create_db.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAU1C,+BAA+B;AAC/B,MAAM,WAAW,eAAe;IAC/B;;;;OAIG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,yCAAyC;AACzC,MAAM,WAAW,cAAc;IAC9B,EAAE,EAAE,EAAE,CAAC;IACP,iFAAiF;IACjF,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,SAAS,GACrB,cAAc,MAAM,EACpB,UAAU,eAAe,KACvB,OAAO,CAAC,cAAc,CAoCxB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,cAAc,MAAM,KAAG,OACyB,CAAC"}
@@ -13,7 +13,7 @@
13
13
  *
14
14
  * @module
15
15
  */
16
- import { create_pg_db, register_pg_type_parsers } from './db_pg.js';
16
+ import { create_pg_db, create_pg_pool, DEFAULT_DB_CHECKOUT_TIMEOUT_MS, register_pg_type_parsers } from './db_pg.js';
17
17
  import { create_pglite_db } from './db_pglite.js';
18
18
  import { create_pglet_db_driver, create_pglet_instance } from './db_pglet.js';
19
19
  /**
@@ -27,16 +27,20 @@ import { create_pglet_db_driver, create_pglet_instance } from './db_pglet.js';
27
27
  * or `create_pglet_db_driver` from `db/db_pglet.ts`.
28
28
  *
29
29
  * @param database_url - connection URL (`postgres://`, `postgresql://`, `file://`, or `memory://`)
30
+ * @param options - the pool's checkout bound (Postgres only)
30
31
  * @returns database instance, close callback, type, and display name
31
32
  * @throws Error if `database_url` uses an unsupported scheme. Driver
32
33
  * construction (`pg.Pool`, `PGlite` or pglet) may also throw on bad connection
33
34
  * parameters or missing peer-dependency packages.
34
35
  */
35
- export const create_db = async (database_url) => {
36
- if (database_url.startsWith('postgres://') || database_url.startsWith('postgresql://')) {
36
+ export const create_db = async (database_url, options) => {
37
+ if (is_pg_database_url(database_url)) {
37
38
  const { default: pg } = await import('pg');
38
39
  await register_pg_type_parsers();
39
- const pool = new pg.Pool({ connectionString: database_url });
40
+ const pool = create_pg_pool(pg, {
41
+ connection_string: database_url,
42
+ checkout_timeout_ms: options?.checkout_timeout_ms ?? DEFAULT_DB_CHECKOUT_TIMEOUT_MS
43
+ });
40
44
  const { db, close } = create_pg_db(pool);
41
45
  return {
42
46
  db,
@@ -59,3 +63,10 @@ export const create_db = async (database_url) => {
59
63
  const scheme = database_url.split('://')[0] ?? database_url;
60
64
  throw new Error(`Unsupported database URL scheme: ${scheme}://. Expected postgres://, postgresql://, file://, or memory://`);
61
65
  };
66
+ /**
67
+ * Whether `database_url` names a Postgres server — the one driver with a pool.
68
+ *
69
+ * @param database_url - a database URL
70
+ * @returns `true` for `postgres://` and `postgresql://`
71
+ */
72
+ export const is_pg_database_url = (database_url) => database_url.startsWith('postgres://') || database_url.startsWith('postgresql://');
package/dist/db/db.d.ts CHANGED
@@ -30,6 +30,13 @@ export interface DbClient {
30
30
  export interface DbDeps {
31
31
  client: DbClient;
32
32
  transaction: <T>(fn: (tx_db: Db) => Promise<T>) => Promise<T>;
33
+ /**
34
+ * A `Db` over the same pool whose checkouts are bounded by `timeout_ms` —
35
+ * supplied by the pg adapter (`create_pg_db`). Omitted where there is no
36
+ * pool (PGlite, pglet), so `Db.with_checkout_timeout` returns the `Db`
37
+ * itself.
38
+ */
39
+ with_checkout_timeout?: (timeout_ms: number) => Db;
33
40
  }
34
41
  /**
35
42
  * Result of constructing a driver-specific `Db`.
@@ -77,6 +84,26 @@ export declare class Db {
77
84
  #private;
78
85
  readonly client: DbClient;
79
86
  constructor(options: DbDeps);
87
+ /**
88
+ * A `Db` over the same pool whose statements check out under `timeout_ms`
89
+ * instead of the pool's own bound — the audit emitter's longer wait
90
+ * (`db/db_pg.ts`, "Checkout timeout"). With no pool (PGlite, pglet, a
91
+ * transaction-scoped `Db`) there is no checkout to bound, and the `Db`
92
+ * itself is returned.
93
+ *
94
+ * @param timeout_ms - the checkout bound, in ms
95
+ * @returns a `Db` sharing this one's pool
96
+ */
97
+ with_checkout_timeout(timeout_ms: number): Db;
98
+ /**
99
+ * Whether this `Db` is scoped to an open transaction — the `Db` a driver
100
+ * adapter hands a `transaction` callback, which it builds with
101
+ * `no_nested_transaction`. A pool-level `Db` answers `false`.
102
+ *
103
+ * The audit emitter reads it to write a success row inside the request's
104
+ * transaction when one is open (`auth/audit_emitter.ts`).
105
+ */
106
+ get in_transaction(): boolean;
80
107
  /**
81
108
  * Execute a query and return all rows.
82
109
  *
@@ -1 +1 @@
1
- {"version":3,"file":"db.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/db/db.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH;;GAEG;AACH,MAAM,WAAW,QAAQ;IACxB,KAAK,EAAE,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC;QAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,CAAA;KAAE,CAAC,CAAC;CAC3F;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,MAAM;IACtB,MAAM,EAAE,QAAQ,CAAC;IACjB,WAAW,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;CAC9D;AAED;;;;;;GAMG;AACH,MAAM,WAAW,cAAc;IAC9B,EAAE,EAAE,EAAE,CAAC;IACP,iFAAiF;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AAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAG/D"}
package/dist/db/db.js CHANGED
@@ -44,9 +44,35 @@ export const no_nested_transaction = () => {
44
44
  export class Db {
45
45
  client;
46
46
  #transaction;
47
+ #with_checkout_timeout;
47
48
  constructor(options) {
48
49
  this.client = options.client;
49
50
  this.#transaction = options.transaction;
51
+ this.#with_checkout_timeout = options.with_checkout_timeout;
52
+ }
53
+ /**
54
+ * A `Db` over the same pool whose statements check out under `timeout_ms`
55
+ * instead of the pool's own bound — the audit emitter's longer wait
56
+ * (`db/db_pg.ts`, "Checkout timeout"). With no pool (PGlite, pglet, a
57
+ * transaction-scoped `Db`) there is no checkout to bound, and the `Db`
58
+ * itself is returned.
59
+ *
60
+ * @param timeout_ms - the checkout bound, in ms
61
+ * @returns a `Db` sharing this one's pool
62
+ */
63
+ with_checkout_timeout(timeout_ms) {
64
+ return this.#with_checkout_timeout?.(timeout_ms) ?? this;
65
+ }
66
+ /**
67
+ * Whether this `Db` is scoped to an open transaction — the `Db` a driver
68
+ * adapter hands a `transaction` callback, which it builds with
69
+ * `no_nested_transaction`. A pool-level `Db` answers `false`.
70
+ *
71
+ * The audit emitter reads it to write a success row inside the request's
72
+ * transaction when one is open (`auth/audit_emitter.ts`).
73
+ */
74
+ get in_transaction() {
75
+ return this.#transaction === no_nested_transaction;
50
76
  }
51
77
  /**
52
78
  * Execute a query and return all rows.
@@ -0,0 +1,36 @@
1
+ /**
2
+ * `DbCheckoutError` — a failed pool checkout, kept apart from a failed query.
3
+ *
4
+ * A request that cannot check a connection out of the pool (the wait timed
5
+ * out, a new connection's connect timed out or was refused, the pool is
6
+ * closed) finds the database unavailable; every request surface answers it
7
+ * with a 503 (`service_unavailable`), where a query that fails on a connection
8
+ * already checked out keeps its 500. The pool built by `create_pg_pool`
9
+ * (`db/db_pg.ts`) throws one from its `connect`, which per-statement
10
+ * `pool.query` and each transaction's `pool.connect()` both run through.
11
+ * `to_thrown_jsonrpc_error` (`http/jsonrpc_errors.ts`) maps it, so the
12
+ * dispatcher, the route catch, and the server's `onError` all answer it the
13
+ * same way. Twin of the Rust spine's `pool.get()` failures, which answer
14
+ * `fuz_http::db_unavailable` / `db_unavailable_response`.
15
+ *
16
+ * PGlite and pglet have no pool, so neither ever throws one.
17
+ *
18
+ * @module
19
+ */
20
+ /**
21
+ * A pool checkout failed; `cause` carries the driver's error, and the message
22
+ * repeats it for the server's log (`database checkout failed: <cause>`). The
23
+ * wire never carries it — the answer is the fixed `database unavailable`.
24
+ */
25
+ export declare class DbCheckoutError extends Error {
26
+ constructor(options: ErrorOptions);
27
+ }
28
+ /**
29
+ * Whether `err` is a `DbCheckoutError` from any copy of fuz_app, recognized by
30
+ * its realm-global brand.
31
+ *
32
+ * @param err - the thrown value
33
+ * @returns `true` for a failed pool checkout
34
+ */
35
+ export declare const is_db_checkout_error: (err: unknown) => boolean;
36
+ //# sourceMappingURL=db_checkout_error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db_checkout_error.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/db/db_checkout_error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAYH;;;;GAIG;AACH,qBAAa,eAAgB,SAAQ,KAAK;gBAC7B,OAAO,EAAE,YAAY;CAIjC;AAID;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,GAAI,KAAK,OAAO,KAAG,OAE+B,CAAC"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * `DbCheckoutError` — a failed pool checkout, kept apart from a failed query.
3
+ *
4
+ * A request that cannot check a connection out of the pool (the wait timed
5
+ * out, a new connection's connect timed out or was refused, the pool is
6
+ * closed) finds the database unavailable; every request surface answers it
7
+ * with a 503 (`service_unavailable`), where a query that fails on a connection
8
+ * already checked out keeps its 500. The pool built by `create_pg_pool`
9
+ * (`db/db_pg.ts`) throws one from its `connect`, which per-statement
10
+ * `pool.query` and each transaction's `pool.connect()` both run through.
11
+ * `to_thrown_jsonrpc_error` (`http/jsonrpc_errors.ts`) maps it, so the
12
+ * dispatcher, the route catch, and the server's `onError` all answer it the
13
+ * same way. Twin of the Rust spine's `pool.get()` failures, which answer
14
+ * `fuz_http::db_unavailable` / `db_unavailable_response`.
15
+ *
16
+ * PGlite and pglet have no pool, so neither ever throws one.
17
+ *
18
+ * @module
19
+ */
20
+ import { to_error_message } from '@fuzdev/fuz_util/error.ts';
21
+ /**
22
+ * Realm-global brand on `DbCheckoutError.prototype`. `Symbol.for` makes every
23
+ * copy of this module one process resolves mint the same key, so
24
+ * `is_db_checkout_error` recognizes an error from any copy of fuz_app, where
25
+ * `instanceof` sees only its own class.
26
+ */
27
+ const DB_CHECKOUT_ERROR_BRAND = Symbol.for('@fuzdev/fuz_app/DbCheckoutError');
28
+ /**
29
+ * A pool checkout failed; `cause` carries the driver's error, and the message
30
+ * repeats it for the server's log (`database checkout failed: <cause>`). The
31
+ * wire never carries it — the answer is the fixed `database unavailable`.
32
+ */
33
+ export class DbCheckoutError extends Error {
34
+ constructor(options) {
35
+ super(`database checkout failed: ${to_error_message(options.cause)}`, options);
36
+ this.name = 'DbCheckoutError';
37
+ }
38
+ }
39
+ Object.defineProperty(DbCheckoutError.prototype, DB_CHECKOUT_ERROR_BRAND, { value: true });
40
+ /**
41
+ * Whether `err` is a `DbCheckoutError` from any copy of fuz_app, recognized by
42
+ * its realm-global brand.
43
+ *
44
+ * @param err - the thrown value
45
+ * @returns `true` for a failed pool checkout
46
+ */
47
+ export const is_db_checkout_error = (err) => err instanceof Error &&
48
+ err[DB_CHECKOUT_ERROR_BRAND] === true;