@fuzdev/fuz_app 0.150.0 → 0.151.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 (597) hide show
  1. package/README.md +1 -1
  2. package/dist/actions/CLAUDE.md +324 -299
  3. package/dist/actions/action_bridge.d.ts +5 -86
  4. package/dist/actions/action_bridge.d.ts.map +1 -1
  5. package/dist/actions/action_bridge.js +4 -84
  6. package/dist/actions/action_codegen.d.ts +8 -166
  7. package/dist/actions/action_codegen.d.ts.map +1 -1
  8. package/dist/actions/action_codegen.js +19 -95
  9. package/dist/actions/action_dispatcher.d.ts +10 -0
  10. package/dist/actions/action_dispatcher.d.ts.map +1 -1
  11. package/dist/actions/action_dispatcher.js +9 -6
  12. package/dist/actions/action_event.d.ts +1 -2
  13. package/dist/actions/action_event.d.ts.map +1 -1
  14. package/dist/actions/action_event.js +7 -13
  15. package/dist/actions/action_event_helpers.d.ts +1 -40
  16. package/dist/actions/action_event_helpers.d.ts.map +1 -1
  17. package/dist/actions/action_event_helpers.js +1 -25
  18. package/dist/actions/action_rpc.d.ts +10 -215
  19. package/dist/actions/action_rpc.d.ts.map +1 -1
  20. package/dist/actions/action_rpc.js +56 -111
  21. package/dist/actions/action_spec.d.ts +2 -2
  22. package/dist/actions/action_spec.js +2 -2
  23. package/dist/actions/action_types.d.ts +229 -8
  24. package/dist/actions/action_types.d.ts.map +1 -1
  25. package/dist/actions/action_types.js +56 -5
  26. package/dist/actions/broadcast_api.d.ts +22 -17
  27. package/dist/actions/broadcast_api.d.ts.map +1 -1
  28. package/dist/actions/broadcast_api.js +16 -27
  29. package/dist/actions/cancel.d.ts +4 -13
  30. package/dist/actions/cancel.d.ts.map +1 -1
  31. package/dist/actions/cancel.js +5 -17
  32. package/dist/actions/compile_action_registry.d.ts +1 -2
  33. package/dist/actions/compile_action_registry.d.ts.map +1 -1
  34. package/dist/actions/connection_closer.d.ts +120 -50
  35. package/dist/actions/connection_closer.d.ts.map +1 -1
  36. package/dist/actions/connection_closer.js +139 -31
  37. package/dist/actions/{transports_ws_backend.d.ts → connection_registry.d.ts} +120 -132
  38. package/dist/actions/connection_registry.d.ts.map +1 -0
  39. package/dist/actions/{transports_ws_backend.js → connection_registry.js} +149 -152
  40. package/dist/actions/frontend_rpc_client.d.ts +52 -2
  41. package/dist/actions/frontend_rpc_client.d.ts.map +1 -1
  42. package/dist/actions/frontend_rpc_client.js +42 -4
  43. package/dist/actions/peer_ping.d.ts +7 -9
  44. package/dist/actions/peer_ping.d.ts.map +1 -1
  45. package/dist/actions/peer_ping.js +6 -8
  46. package/dist/actions/peer_request.d.ts +6 -5
  47. package/dist/actions/peer_request.d.ts.map +1 -1
  48. package/dist/actions/peer_request.js +6 -5
  49. package/dist/actions/perform_action.d.ts +36 -28
  50. package/dist/actions/perform_action.d.ts.map +1 -1
  51. package/dist/actions/perform_action.js +60 -34
  52. package/dist/actions/protocol.d.ts +14 -0
  53. package/dist/actions/protocol.d.ts.map +1 -1
  54. package/dist/actions/protocol.js +18 -0
  55. package/dist/actions/register_action_ws.d.ts +137 -298
  56. package/dist/actions/register_action_ws.d.ts.map +1 -1
  57. package/dist/actions/register_action_ws.js +155 -923
  58. package/dist/actions/socket.svelte.d.ts +9 -0
  59. package/dist/actions/socket.svelte.d.ts.map +1 -1
  60. package/dist/actions/socket.svelte.js +16 -5
  61. package/dist/actions/transports.d.ts +88 -34
  62. package/dist/actions/transports.d.ts.map +1 -1
  63. package/dist/actions/transports.js +76 -76
  64. package/dist/actions/transports_http.d.ts +48 -12
  65. package/dist/actions/transports_http.d.ts.map +1 -1
  66. package/dist/actions/transports_http.js +75 -44
  67. package/dist/actions/transports_ws.d.ts +29 -0
  68. package/dist/actions/transports_ws.d.ts.map +1 -1
  69. package/dist/actions/transports_ws.js +34 -8
  70. package/dist/actions/ws_endpoint_spec.d.ts +52 -75
  71. package/dist/actions/ws_endpoint_spec.d.ts.map +1 -1
  72. package/dist/actions/ws_endpoint_spec.js +27 -10
  73. package/dist/actions/ws_outbound_writer.d.ts +3 -3
  74. package/dist/actions/ws_outbound_writer.js +3 -3
  75. package/dist/actions/ws_socket_session.d.ts +321 -0
  76. package/dist/actions/ws_socket_session.d.ts.map +1 -0
  77. package/dist/actions/ws_socket_session.js +783 -0
  78. package/dist/auth/CLAUDE.md +70 -44
  79. package/dist/auth/account_action_specs.d.ts +2 -0
  80. package/dist/auth/account_action_specs.d.ts.map +1 -1
  81. package/dist/auth/account_actions.d.ts +1 -1
  82. package/dist/auth/account_actions.d.ts.map +1 -1
  83. package/dist/auth/account_actions.js +9 -3
  84. package/dist/auth/account_queries.d.ts +10 -0
  85. package/dist/auth/account_queries.d.ts.map +1 -1
  86. package/dist/auth/account_queries.js +27 -17
  87. package/dist/auth/account_routes.d.ts +2 -1
  88. package/dist/auth/account_routes.d.ts.map +1 -1
  89. package/dist/auth/account_routes.js +14 -17
  90. package/dist/auth/account_schema.d.ts +5 -5
  91. package/dist/auth/account_schema.d.ts.map +1 -1
  92. package/dist/auth/account_schema.js +5 -2
  93. package/dist/auth/actor_lookup_actions.d.ts +1 -1
  94. package/dist/auth/actor_lookup_actions.d.ts.map +1 -1
  95. package/dist/auth/actor_lookup_actions.js +1 -1
  96. package/dist/auth/actor_search_action_specs.d.ts.map +1 -1
  97. package/dist/auth/actor_search_action_specs.js +2 -1
  98. package/dist/auth/actor_search_actions.d.ts +1 -1
  99. package/dist/auth/actor_search_actions.d.ts.map +1 -1
  100. package/dist/auth/actor_search_actions.js +1 -1
  101. package/dist/auth/actor_search_queries.d.ts +5 -5
  102. package/dist/auth/actor_search_queries.d.ts.map +1 -1
  103. package/dist/auth/actor_search_queries.js +7 -7
  104. package/dist/auth/admin_action_specs.d.ts.map +1 -1
  105. package/dist/auth/admin_action_specs.js +3 -1
  106. package/dist/auth/admin_actions.d.ts +1 -1
  107. package/dist/auth/admin_actions.d.ts.map +1 -1
  108. package/dist/auth/admin_actions.js +12 -42
  109. package/dist/auth/all_cell_actions.d.ts +6 -4
  110. package/dist/auth/all_cell_actions.d.ts.map +1 -1
  111. package/dist/auth/all_cell_actions.js +1 -1
  112. package/dist/auth/api_token_queries.d.ts +1 -0
  113. package/dist/auth/api_token_queries.d.ts.map +1 -1
  114. package/dist/auth/api_token_queries.js +1 -0
  115. package/dist/auth/audit_emitter.d.ts +3 -3
  116. package/dist/auth/audit_emitter.js +2 -2
  117. package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
  118. package/dist/auth/audit_log_route_schema.js +3 -2
  119. package/dist/auth/audit_log_routes.d.ts +20 -11
  120. package/dist/auth/audit_log_routes.d.ts.map +1 -1
  121. package/dist/auth/audit_log_routes.js +49 -29
  122. package/dist/auth/audit_log_schema.d.ts +13 -21
  123. package/dist/auth/audit_log_schema.d.ts.map +1 -1
  124. package/dist/auth/audit_log_schema.js +10 -18
  125. package/dist/auth/bearer_auth.d.ts +7 -4
  126. package/dist/auth/bearer_auth.d.ts.map +1 -1
  127. package/dist/auth/bearer_auth.js +21 -15
  128. package/dist/auth/cell_action_helpers.d.ts +89 -0
  129. package/dist/auth/cell_action_helpers.d.ts.map +1 -0
  130. package/dist/auth/cell_action_helpers.js +113 -0
  131. package/dist/auth/cell_actions.d.ts +22 -13
  132. package/dist/auth/cell_actions.d.ts.map +1 -1
  133. package/dist/auth/cell_actions.js +105 -167
  134. package/dist/auth/cell_audit_actions.d.ts +1 -1
  135. package/dist/auth/cell_audit_actions.d.ts.map +1 -1
  136. package/dist/auth/cell_audit_actions.js +3 -12
  137. package/dist/auth/cell_audit_metadata.d.ts +1 -1
  138. package/dist/auth/cell_audit_metadata.js +1 -1
  139. package/dist/auth/cell_field_actions.d.ts +1 -1
  140. package/dist/auth/cell_field_actions.d.ts.map +1 -1
  141. package/dist/auth/cell_field_actions.js +16 -66
  142. package/dist/auth/cell_grant_action_specs.d.ts +2 -2
  143. package/dist/auth/cell_grant_action_specs.js +3 -3
  144. package/dist/auth/cell_grant_actions.d.ts +73 -11
  145. package/dist/auth/cell_grant_actions.d.ts.map +1 -1
  146. package/dist/auth/cell_grant_actions.js +63 -55
  147. package/dist/auth/cell_grant_audit_metadata.d.ts +6 -3
  148. package/dist/auth/cell_grant_audit_metadata.d.ts.map +1 -1
  149. package/dist/auth/cell_grant_audit_metadata.js +7 -4
  150. package/dist/auth/cell_item_actions.d.ts +1 -1
  151. package/dist/auth/cell_item_actions.d.ts.map +1 -1
  152. package/dist/auth/cell_item_actions.js +21 -82
  153. package/dist/auth/cleanup.d.ts +1 -1
  154. package/dist/auth/cleanup.d.ts.map +1 -1
  155. package/dist/auth/cleanup.js +6 -3
  156. package/dist/auth/credential_deadline.d.ts +3 -3
  157. package/dist/auth/credential_deadline.js +3 -3
  158. package/dist/auth/credential_type_schema.d.ts +5 -5
  159. package/dist/auth/credential_type_schema.js +5 -5
  160. package/dist/auth/daemon_token_middleware.d.ts +12 -6
  161. package/dist/auth/daemon_token_middleware.d.ts.map +1 -1
  162. package/dist/auth/daemon_token_middleware.js +28 -17
  163. package/dist/auth/deps.d.ts +4 -4
  164. package/dist/auth/fail_floor.d.ts +55 -0
  165. package/dist/auth/fail_floor.d.ts.map +1 -0
  166. package/dist/auth/fail_floor.js +63 -0
  167. package/dist/auth/invite_queries.d.ts +3 -15
  168. package/dist/auth/invite_queries.d.ts.map +1 -1
  169. package/dist/auth/invite_queries.js +3 -21
  170. package/dist/auth/middleware.d.ts +12 -7
  171. package/dist/auth/middleware.d.ts.map +1 -1
  172. package/dist/auth/middleware.js +42 -29
  173. package/dist/auth/migrations.d.ts +5 -33
  174. package/dist/auth/migrations.d.ts.map +1 -1
  175. package/dist/auth/migrations.js +9 -16
  176. package/dist/auth/request_context.d.ts +101 -71
  177. package/dist/auth/request_context.d.ts.map +1 -1
  178. package/dist/auth/request_context.js +137 -140
  179. package/dist/auth/resolved_auth.d.ts +122 -36
  180. package/dist/auth/resolved_auth.d.ts.map +1 -1
  181. package/dist/auth/resolved_auth.js +89 -38
  182. package/dist/auth/role_grant_offer_action_specs.d.ts +99 -14
  183. package/dist/auth/role_grant_offer_action_specs.d.ts.map +1 -1
  184. package/dist/auth/role_grant_offer_action_specs.js +26 -13
  185. package/dist/auth/role_grant_offer_actions.d.ts +13 -7
  186. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  187. package/dist/auth/role_grant_offer_actions.js +14 -12
  188. package/dist/auth/role_grant_offer_notifications.d.ts +3 -3
  189. package/dist/auth/role_grant_offer_notifications.js +1 -1
  190. package/dist/auth/role_grant_offer_queries.d.ts +0 -5
  191. package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
  192. package/dist/auth/role_grant_offer_queries.js +9 -41
  193. package/dist/auth/role_grant_queries.d.ts +17 -67
  194. package/dist/auth/role_grant_queries.d.ts.map +1 -1
  195. package/dist/auth/role_grant_queries.js +34 -106
  196. package/dist/auth/role_grant_sql.d.ts +42 -0
  197. package/dist/auth/role_grant_sql.d.ts.map +1 -0
  198. package/dist/auth/role_grant_sql.js +54 -0
  199. package/dist/auth/scope_kind_schema.d.ts +1 -1
  200. package/dist/auth/scope_kind_schema.js +1 -1
  201. package/dist/auth/self_service_role_actions.d.ts +1 -1
  202. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  203. package/dist/auth/self_service_role_actions.js +1 -1
  204. package/dist/auth/session_queries.d.ts +2 -1
  205. package/dist/auth/session_queries.d.ts.map +1 -1
  206. package/dist/auth/session_queries.js +2 -1
  207. package/dist/auth/signup_routes.d.ts +2 -1
  208. package/dist/auth/signup_routes.d.ts.map +1 -1
  209. package/dist/auth/signup_routes.js +14 -13
  210. package/dist/auth/standard_rpc_actions.d.ts +1 -1
  211. package/dist/auth/standard_rpc_actions.d.ts.map +1 -1
  212. package/dist/auth/token_scope.d.ts +19 -46
  213. package/dist/auth/token_scope.d.ts.map +1 -1
  214. package/dist/auth/token_scope.js +23 -41
  215. package/dist/db/CLAUDE.md +9 -4
  216. package/dist/db/cell_ddl.d.ts +5 -142
  217. package/dist/db/cell_ddl.d.ts.map +1 -1
  218. package/dist/db/cell_ddl.js +17 -16
  219. package/dist/db/cell_field_queries.d.ts +0 -13
  220. package/dist/db/cell_field_queries.d.ts.map +1 -1
  221. package/dist/db/cell_field_queries.js +0 -16
  222. package/dist/db/cell_grant_queries.d.ts +10 -5
  223. package/dist/db/cell_grant_queries.d.ts.map +1 -1
  224. package/dist/db/cell_grant_queries.js +13 -7
  225. package/dist/db/cell_item_queries.d.ts +0 -7
  226. package/dist/db/cell_item_queries.d.ts.map +1 -1
  227. package/dist/db/cell_item_queries.js +0 -10
  228. package/dist/db/cell_queries.d.ts +12 -48
  229. package/dist/db/cell_queries.d.ts.map +1 -1
  230. package/dist/db/cell_queries.js +24 -55
  231. package/dist/db/cell_view_sql.d.ts +1 -6
  232. package/dist/db/cell_view_sql.d.ts.map +1 -1
  233. package/dist/db/cell_view_sql.js +1 -2
  234. package/dist/db/fact_ddl.d.ts +5 -30
  235. package/dist/db/fact_ddl.d.ts.map +1 -1
  236. package/dist/db/fact_ddl.js +14 -13
  237. package/dist/db/fact_store.d.ts +11 -0
  238. package/dist/db/fact_store.d.ts.map +1 -1
  239. package/dist/db/fact_store.js +4 -1
  240. package/dist/db/migrate.d.ts +44 -6
  241. package/dist/db/migrate.d.ts.map +1 -1
  242. package/dist/db/migrate.js +63 -4
  243. package/dist/dev/setup.d.ts +18 -14
  244. package/dist/dev/setup.d.ts.map +1 -1
  245. package/dist/dev/setup.js +42 -39
  246. package/dist/env/dotenv.d.ts +2 -2
  247. package/dist/env/dotenv.js +2 -2
  248. package/dist/env/update_env_variable.d.ts +33 -21
  249. package/dist/env/update_env_variable.d.ts.map +1 -1
  250. package/dist/env/update_env_variable.js +54 -35
  251. package/dist/hono_context.d.ts +15 -90
  252. package/dist/hono_context.d.ts.map +1 -1
  253. package/dist/hono_context.js +9 -56
  254. package/dist/http/CLAUDE.md +35 -20
  255. package/dist/http/auth_shape.d.ts +1 -8
  256. package/dist/http/auth_shape.d.ts.map +1 -1
  257. package/dist/http/auth_shape.js +6 -12
  258. package/dist/http/common_routes.d.ts.map +1 -1
  259. package/dist/http/common_routes.js +3 -2
  260. package/dist/http/db_routes.d.ts +3 -3
  261. package/dist/http/db_routes.js +3 -3
  262. package/dist/http/error_schemas.d.ts +1 -2
  263. package/dist/http/error_schemas.d.ts.map +1 -1
  264. package/dist/http/error_schemas.js +1 -2
  265. package/dist/http/health_schema.d.ts +16 -0
  266. package/dist/http/health_schema.d.ts.map +1 -0
  267. package/dist/http/health_schema.js +12 -0
  268. package/dist/http/jsonrpc_errors.d.ts +22 -2
  269. package/dist/http/jsonrpc_errors.d.ts.map +1 -1
  270. package/dist/http/jsonrpc_errors.js +41 -3
  271. package/dist/http/jsonrpc_helpers.d.ts +60 -17
  272. package/dist/http/jsonrpc_helpers.d.ts.map +1 -1
  273. package/dist/http/jsonrpc_helpers.js +67 -25
  274. package/dist/http/origin.d.ts +30 -1
  275. package/dist/http/origin.d.ts.map +1 -1
  276. package/dist/http/origin.js +46 -9
  277. package/dist/http/origin_wildcard.d.ts +97 -0
  278. package/dist/http/origin_wildcard.d.ts.map +1 -0
  279. package/dist/http/origin_wildcard.js +142 -0
  280. package/dist/http/pending_effects.d.ts +28 -0
  281. package/dist/http/pending_effects.d.ts.map +1 -1
  282. package/dist/http/pending_effects.js +32 -1
  283. package/dist/http/route_spec.d.ts +4 -3
  284. package/dist/http/route_spec.d.ts.map +1 -1
  285. package/dist/http/route_spec.js +54 -45
  286. package/dist/http/surface.d.ts +1 -1
  287. package/dist/http/surface.d.ts.map +1 -1
  288. package/dist/http/surface_query.d.ts +9 -50
  289. package/dist/http/surface_query.d.ts.map +1 -1
  290. package/dist/http/surface_query.js +48 -94
  291. package/dist/realtime/audit_log_sse.d.ts +180 -0
  292. package/dist/realtime/audit_log_sse.d.ts.map +1 -0
  293. package/dist/realtime/audit_log_sse.js +154 -0
  294. package/dist/realtime/sse.d.ts +21 -6
  295. package/dist/realtime/sse.d.ts.map +1 -1
  296. package/dist/realtime/sse.js +33 -10
  297. package/dist/realtime/sse_constants.d.ts +29 -2
  298. package/dist/realtime/sse_constants.d.ts.map +1 -1
  299. package/dist/realtime/sse_constants.js +35 -2
  300. package/dist/realtime/{subscriber_registry.d.ts → sse_registry.d.ts} +12 -12
  301. package/dist/realtime/sse_registry.d.ts.map +1 -0
  302. package/dist/realtime/{subscriber_registry.js → sse_registry.js} +4 -4
  303. package/dist/schema_meta.d.ts +30 -10
  304. package/dist/schema_meta.d.ts.map +1 -1
  305. package/dist/schema_meta.js +85 -21
  306. package/dist/sensitivity.d.ts +9 -3
  307. package/dist/sensitivity.d.ts.map +1 -1
  308. package/dist/sensitivity.js +3 -2
  309. package/dist/server/app_backend.d.ts +8 -5
  310. package/dist/server/app_backend.d.ts.map +1 -1
  311. package/dist/server/app_backend.js +6 -12
  312. package/dist/server/app_server.d.ts +62 -51
  313. package/dist/server/app_server.d.ts.map +1 -1
  314. package/dist/server/app_server.js +84 -71
  315. package/dist/server/app_server_context.d.ts +1 -1
  316. package/dist/server/app_server_context.d.ts.map +1 -1
  317. package/dist/server/env.d.ts +24 -20
  318. package/dist/server/env.d.ts.map +1 -1
  319. package/dist/server/env.js +24 -34
  320. package/dist/server/serve_fact_route.d.ts +12 -16
  321. package/dist/server/serve_fact_route.d.ts.map +1 -1
  322. package/dist/server/serve_fact_route.js +59 -34
  323. package/dist/server/static.d.ts.map +1 -1
  324. package/dist/server/static.js +2 -2
  325. package/dist/testing/CLAUDE.md +179 -158
  326. package/dist/testing/admin_integration.d.ts.map +1 -1
  327. package/dist/testing/admin_integration.js +10 -19
  328. package/dist/testing/adversarial_headers.d.ts.map +1 -1
  329. package/dist/testing/adversarial_headers.js +8 -0
  330. package/dist/testing/app_server.d.ts +9 -77
  331. package/dist/testing/app_server.d.ts.map +1 -1
  332. package/dist/testing/app_server.js +31 -112
  333. package/dist/testing/attack_surface.d.ts.map +1 -1
  334. package/dist/testing/attack_surface.js +3 -4
  335. package/dist/testing/audit_completeness.d.ts.map +1 -1
  336. package/dist/testing/audit_completeness.js +4 -5
  337. package/dist/testing/auth_apps.d.ts +3 -3
  338. package/dist/testing/auth_apps.d.ts.map +1 -1
  339. package/dist/testing/auth_apps.js +9 -6
  340. package/dist/testing/connection_closer_helpers.d.ts +8 -16
  341. package/dist/testing/connection_closer_helpers.d.ts.map +1 -1
  342. package/dist/testing/connection_closer_helpers.js +7 -20
  343. package/dist/testing/cross_backend/action_manifest.d.ts +15 -4
  344. package/dist/testing/cross_backend/action_manifest.d.ts.map +1 -1
  345. package/dist/testing/cross_backend/action_manifest.js +13 -3
  346. package/dist/testing/cross_backend/action_manifest_parity.d.ts +15 -7
  347. package/dist/testing/cross_backend/action_manifest_parity.d.ts.map +1 -1
  348. package/dist/testing/cross_backend/action_manifest_parity.js +8 -1
  349. package/dist/testing/cross_backend/body_size.d.ts.map +1 -1
  350. package/dist/testing/cross_backend/body_size.js +25 -22
  351. package/dist/testing/cross_backend/build_test_backend_paths.d.ts +22 -3
  352. package/dist/testing/cross_backend/build_test_backend_paths.d.ts.map +1 -1
  353. package/dist/testing/cross_backend/build_test_backend_paths.js +34 -11
  354. package/dist/testing/cross_backend/capabilities.d.ts +13 -7
  355. package/dist/testing/cross_backend/capabilities.d.ts.map +1 -1
  356. package/dist/testing/cross_backend/cell_grant_role.js +2 -2
  357. package/dist/testing/cross_backend/cell_grant_steward.d.ts +4 -0
  358. package/dist/testing/cross_backend/cell_grant_steward.d.ts.map +1 -0
  359. package/dist/testing/cross_backend/cell_grant_steward.js +120 -0
  360. package/dist/testing/cross_backend/cell_relations.d.ts.map +1 -1
  361. package/dist/testing/cross_backend/cell_relations.js +21 -0
  362. package/dist/testing/cross_backend/conformance_table.d.ts +1 -1
  363. package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
  364. package/dist/testing/cross_backend/default_backend_configs.d.ts +18 -4
  365. package/dist/testing/cross_backend/default_backend_configs.d.ts.map +1 -1
  366. package/dist/testing/cross_backend/default_backend_configs.js +29 -9
  367. package/dist/testing/cross_backend/default_spine_surface.d.ts +7 -7
  368. package/dist/testing/cross_backend/default_spine_surface.js +2 -2
  369. package/dist/testing/cross_backend/fact_serving.d.ts.map +1 -1
  370. package/dist/testing/cross_backend/fact_serving.js +48 -0
  371. package/dist/testing/cross_backend/full_spine_mount.d.ts +3 -4
  372. package/dist/testing/cross_backend/full_spine_mount.d.ts.map +1 -1
  373. package/dist/testing/cross_backend/full_spine_mount.js +9 -2
  374. package/dist/testing/cross_backend/origin_scope.d.ts.map +1 -1
  375. package/dist/testing/cross_backend/origin_scope.js +5 -13
  376. package/dist/testing/cross_backend/peer_ping_ws.d.ts.map +1 -1
  377. package/dist/testing/cross_backend/peer_ping_ws.js +29 -4
  378. package/dist/testing/cross_backend/postgres_database.d.ts +32 -0
  379. package/dist/testing/cross_backend/postgres_database.d.ts.map +1 -1
  380. package/dist/testing/cross_backend/postgres_database.js +56 -1
  381. package/dist/testing/cross_backend/query_shape.d.ts.map +1 -1
  382. package/dist/testing/cross_backend/query_shape.js +18 -1
  383. package/dist/testing/cross_backend/role_grant_offer_lifecycle.d.ts.map +1 -1
  384. package/dist/testing/cross_backend/role_grant_offer_lifecycle.js +23 -1
  385. package/dist/testing/cross_backend/rpc_success_fixtures.js +2 -2
  386. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts +1 -1
  387. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
  388. package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts +17 -5
  389. package/dist/testing/cross_backend/rust_spine_stub_backend_config.d.ts.map +1 -1
  390. package/dist/testing/cross_backend/rust_spine_stub_backend_config.js +5 -4
  391. package/dist/testing/cross_backend/session_cap.d.ts +8 -0
  392. package/dist/testing/cross_backend/session_cap.d.ts.map +1 -1
  393. package/dist/testing/cross_backend/session_cap.js +66 -2
  394. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  395. package/dist/testing/cross_backend/setup.js +5 -4
  396. package/dist/testing/cross_backend/spine_suites.d.ts +4 -1
  397. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  398. package/dist/testing/cross_backend/spine_suites.js +21 -0
  399. package/dist/testing/cross_backend/spine_surface_constants.d.ts +22 -7
  400. package/dist/testing/cross_backend/spine_surface_constants.d.ts.map +1 -1
  401. package/dist/testing/cross_backend/spine_surface_constants.js +22 -7
  402. package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
  403. package/dist/testing/cross_backend/sse_round_trip.js +4 -3
  404. package/dist/testing/cross_backend/standard.d.ts +6 -12
  405. package/dist/testing/cross_backend/standard.d.ts.map +1 -1
  406. package/dist/testing/cross_backend/test_cell_grant_steward_revoke_authorize.d.ts +8 -0
  407. package/dist/testing/cross_backend/test_cell_grant_steward_revoke_authorize.d.ts.map +1 -0
  408. package/dist/testing/cross_backend/test_cell_grant_steward_revoke_authorize.js +22 -0
  409. package/dist/testing/cross_backend/testing_backdoor.d.ts.map +1 -1
  410. package/dist/testing/cross_backend/testing_backdoor.js +4 -6
  411. package/dist/testing/cross_backend/testing_origin_check.d.ts +1 -1
  412. package/dist/testing/cross_backend/testing_origin_check.d.ts.map +1 -1
  413. package/dist/testing/cross_backend/testing_origin_check.js +1 -1
  414. package/dist/testing/cross_backend/testing_reset_actions.d.ts +10 -5
  415. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  416. package/dist/testing/cross_backend/testing_reset_actions.js +7 -6
  417. package/dist/testing/cross_backend/testing_server_bun.d.ts.map +1 -1
  418. package/dist/testing/cross_backend/testing_server_bun.js +5 -4
  419. package/dist/testing/cross_backend/testing_server_core.d.ts +6 -7
  420. package/dist/testing/cross_backend/testing_server_core.d.ts.map +1 -1
  421. package/dist/testing/cross_backend/testing_server_core.js +1 -1
  422. package/dist/testing/cross_backend/testing_server_node.d.ts.map +1 -1
  423. package/dist/testing/cross_backend/testing_server_node.js +13 -2
  424. package/dist/testing/cross_backend/token_lifetime.d.ts.map +1 -1
  425. package/dist/testing/cross_backend/token_lifetime.js +68 -0
  426. package/dist/testing/cross_backend/transport_composition.d.ts +1 -1
  427. package/dist/testing/cross_backend/transport_composition.d.ts.map +1 -1
  428. package/dist/testing/cross_backend/transport_composition.js +45 -3
  429. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts +16 -2
  430. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts.map +1 -1
  431. package/dist/testing/cross_backend/ts_spine_backend_config.js +11 -8
  432. package/dist/testing/cross_backend/ws_action_rate_limit.d.ts.map +1 -1
  433. package/dist/testing/cross_backend/ws_action_rate_limit.js +30 -12
  434. package/dist/testing/cross_backend/ws_connection_cap.d.ts.map +1 -1
  435. package/dist/testing/cross_backend/ws_connection_cap.js +5 -7
  436. package/dist/testing/cross_backend/ws_endpoint.d.ts +2 -3
  437. package/dist/testing/cross_backend/ws_endpoint.d.ts.map +1 -1
  438. package/dist/testing/cross_backend/ws_envelope.d.ts +17 -0
  439. package/dist/testing/cross_backend/ws_envelope.d.ts.map +1 -0
  440. package/dist/testing/cross_backend/ws_envelope.js +172 -0
  441. package/dist/testing/cross_backend/ws_inbound_dispatch.d.ts +16 -0
  442. package/dist/testing/cross_backend/ws_inbound_dispatch.d.ts.map +1 -1
  443. package/dist/testing/cross_backend/ws_inbound_dispatch.js +8 -4
  444. package/dist/testing/cross_backend/ws_outbound_overflow.d.ts.map +1 -1
  445. package/dist/testing/cross_backend/ws_outbound_overflow.js +5 -8
  446. package/dist/testing/cross_backend/ws_role_gated.js +2 -2
  447. package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
  448. package/dist/testing/cross_backend/ws_round_trip.js +6 -4
  449. package/dist/{cli/daemon.d.ts → testing/daemon_info.d.ts} +15 -47
  450. package/dist/testing/daemon_info.d.ts.map +1 -0
  451. package/dist/testing/daemon_info.js +108 -0
  452. package/dist/testing/daemon_token_rotation.js +1 -1
  453. package/dist/testing/data_exposure.d.ts.map +1 -1
  454. package/dist/testing/data_exposure.js +3 -4
  455. package/dist/testing/db.js +2 -2
  456. package/dist/testing/integration.d.ts.map +1 -1
  457. package/dist/testing/integration.js +35 -65
  458. package/dist/testing/integration_helpers.d.ts +0 -15
  459. package/dist/testing/integration_helpers.d.ts.map +1 -1
  460. package/dist/testing/integration_helpers.js +2 -42
  461. package/dist/testing/middleware.d.ts +20 -12
  462. package/dist/testing/middleware.d.ts.map +1 -1
  463. package/dist/testing/middleware.js +49 -40
  464. package/dist/testing/rate_limiting.d.ts.map +1 -1
  465. package/dist/testing/rate_limiting.js +13 -22
  466. package/dist/testing/resolved_auth.d.ts +30 -0
  467. package/dist/testing/resolved_auth.d.ts.map +1 -0
  468. package/dist/testing/resolved_auth.js +41 -0
  469. package/dist/testing/rpc_envelope_cases.d.ts.map +1 -1
  470. package/dist/testing/rpc_envelope_cases.js +3 -0
  471. package/dist/testing/rpc_helpers.d.ts +1 -1
  472. package/dist/testing/rpc_helpers.d.ts.map +1 -1
  473. package/dist/testing/rpc_helpers.js +2 -1
  474. package/dist/testing/sse_round_trip.d.ts +5 -4
  475. package/dist/testing/sse_round_trip.d.ts.map +1 -1
  476. package/dist/testing/sse_round_trip.js +2 -2
  477. package/dist/testing/stubs.d.ts +2 -2
  478. package/dist/testing/stubs.d.ts.map +1 -1
  479. package/dist/testing/stubs.js +24 -21
  480. package/dist/testing/test_origin.d.ts +25 -0
  481. package/dist/testing/test_origin.d.ts.map +1 -0
  482. package/dist/testing/test_origin.js +28 -0
  483. package/dist/testing/transports/raw_http.d.ts +54 -0
  484. package/dist/testing/transports/raw_http.d.ts.map +1 -1
  485. package/dist/testing/transports/raw_http.js +58 -0
  486. package/dist/testing/transports/ws_raw_client.d.ts +8 -0
  487. package/dist/testing/transports/ws_raw_client.d.ts.map +1 -1
  488. package/dist/testing/transports/ws_raw_client.js +13 -0
  489. package/dist/testing/transports/ws_transport.d.ts +15 -2
  490. package/dist/testing/transports/ws_transport.d.ts.map +1 -1
  491. package/dist/testing/transports/ws_transport.js +4 -2
  492. package/dist/testing/ws_round_trip.d.ts +27 -25
  493. package/dist/testing/ws_round_trip.d.ts.map +1 -1
  494. package/dist/testing/ws_round_trip.js +34 -26
  495. package/dist/ui/AccountSessions.svelte +48 -19
  496. package/dist/ui/AccountSessions.svelte.d.ts +2 -17
  497. package/dist/ui/AccountSessions.svelte.d.ts.map +1 -1
  498. package/dist/ui/AdminAccounts.svelte +6 -1
  499. package/dist/ui/AdminAuditLog.svelte +61 -22
  500. package/dist/ui/AdminAuditLog.svelte.d.ts.map +1 -1
  501. package/dist/ui/AdminInvites.svelte +1 -1
  502. package/dist/ui/AdminOverview.svelte +76 -37
  503. package/dist/ui/AdminOverview.svelte.d.ts.map +1 -1
  504. package/dist/ui/AdminRoleGrantHistory.svelte +6 -1
  505. package/dist/ui/AdminSessions.svelte +1 -1
  506. package/dist/ui/AppShell.svelte +3 -2
  507. package/dist/ui/AppShell.svelte.d.ts +3 -2
  508. package/dist/ui/AppShell.svelte.d.ts.map +1 -1
  509. package/dist/ui/CLAUDE.md +121 -35
  510. package/dist/ui/Datatable.svelte +33 -18
  511. package/dist/ui/Datatable.svelte.d.ts +2 -14
  512. package/dist/ui/Datatable.svelte.d.ts.map +1 -1
  513. package/dist/ui/OpenSignupToggle.svelte +10 -1
  514. package/dist/ui/OpenSignupToggle.svelte.d.ts.map +1 -1
  515. package/dist/ui/PopoverButton.svelte +13 -8
  516. package/dist/ui/PopoverButton.svelte.d.ts.map +1 -1
  517. package/dist/ui/RoleGrantOfferForm.svelte +3 -2
  518. package/dist/ui/RoleGrantOfferForm.svelte.d.ts +3 -2
  519. package/dist/ui/RoleGrantOfferForm.svelte.d.ts.map +1 -1
  520. package/dist/ui/RoleGrantOfferHistory.svelte +3 -3
  521. package/dist/ui/RoleGrantOfferInbox.svelte +19 -15
  522. package/dist/ui/RoleGrantOfferInbox.svelte.d.ts.map +1 -1
  523. package/dist/ui/SurfaceExplorer.svelte +66 -27
  524. package/dist/ui/SurfaceExplorer.svelte.d.ts.map +1 -1
  525. package/dist/ui/account_sessions_state.svelte.d.ts +7 -14
  526. package/dist/ui/account_sessions_state.svelte.d.ts.map +1 -1
  527. package/dist/ui/admin_accounts_state.svelte.d.ts +14 -9
  528. package/dist/ui/admin_accounts_state.svelte.d.ts.map +1 -1
  529. package/dist/ui/admin_accounts_state.svelte.js +6 -4
  530. package/dist/ui/admin_invites_state.svelte.d.ts +2 -2
  531. package/dist/ui/admin_invites_state.svelte.d.ts.map +1 -1
  532. package/dist/ui/admin_rpc_adapters.d.ts +9 -9
  533. package/dist/ui/admin_rpc_adapters.d.ts.map +1 -1
  534. package/dist/ui/admin_rpc_adapters.js +9 -9
  535. package/dist/ui/app_settings_state.svelte.d.ts +2 -2
  536. package/dist/ui/app_settings_state.svelte.d.ts.map +1 -1
  537. package/dist/ui/audit_log_state.svelte.d.ts +70 -13
  538. package/dist/ui/audit_log_state.svelte.d.ts.map +1 -1
  539. package/dist/ui/audit_log_state.svelte.js +239 -47
  540. package/dist/ui/backend_probe.svelte.d.ts +93 -0
  541. package/dist/ui/backend_probe.svelte.d.ts.map +1 -0
  542. package/dist/ui/backend_probe.svelte.js +140 -0
  543. package/dist/ui/format_scope.d.ts +7 -7
  544. package/dist/ui/format_scope.d.ts.map +1 -1
  545. package/dist/ui/format_scope.js +4 -4
  546. package/dist/ui/keyed_async_slot.svelte.d.ts +2 -2
  547. package/dist/ui/keyed_async_slot.svelte.js +2 -2
  548. package/dist/ui/popover.svelte.d.ts +18 -19
  549. package/dist/ui/popover.svelte.d.ts.map +1 -1
  550. package/dist/ui/popover.svelte.js +103 -16
  551. package/dist/ui/role_grant_offers_state.svelte.d.ts +41 -46
  552. package/dist/ui/role_grant_offers_state.svelte.d.ts.map +1 -1
  553. package/dist/ui/role_grant_offers_state.svelte.js +37 -19
  554. package/dist/ui/ui_format.d.ts +17 -0
  555. package/dist/ui/ui_format.d.ts.map +1 -1
  556. package/dist/ui/ui_format.js +20 -0
  557. package/package.json +2 -1
  558. package/dist/actions/register_ws_endpoint.d.ts +0 -79
  559. package/dist/actions/register_ws_endpoint.d.ts.map +0 -1
  560. package/dist/actions/register_ws_endpoint.js +0 -109
  561. package/dist/actions/transports_ws_auth_guard.d.ts +0 -96
  562. package/dist/actions/transports_ws_auth_guard.d.ts.map +0 -1
  563. package/dist/actions/transports_ws_auth_guard.js +0 -141
  564. package/dist/actions/transports_ws_backend.d.ts.map +0 -1
  565. package/dist/cli/args.d.ts +0 -55
  566. package/dist/cli/args.d.ts.map +0 -1
  567. package/dist/cli/args.js +0 -100
  568. package/dist/cli/config.d.ts +0 -47
  569. package/dist/cli/config.d.ts.map +0 -1
  570. package/dist/cli/config.js +0 -77
  571. package/dist/cli/daemon.d.ts.map +0 -1
  572. package/dist/cli/daemon.js +0 -181
  573. package/dist/cli/help.d.ts +0 -124
  574. package/dist/cli/help.d.ts.map +0 -1
  575. package/dist/cli/help.js +0 -215
  576. package/dist/cli/logger.d.ts +0 -46
  577. package/dist/cli/logger.d.ts.map +0 -1
  578. package/dist/cli/logger.js +0 -48
  579. package/dist/cli/util.d.ts +0 -36
  580. package/dist/cli/util.d.ts.map +0 -1
  581. package/dist/cli/util.js +0 -50
  582. package/dist/realtime/sse_auth_guard.d.ts +0 -197
  583. package/dist/realtime/sse_auth_guard.d.ts.map +0 -1
  584. package/dist/realtime/sse_auth_guard.js +0 -239
  585. package/dist/realtime/subscriber_registry.d.ts.map +0 -1
  586. package/dist/testing/bootstrap_success.d.ts +0 -29
  587. package/dist/testing/bootstrap_success.d.ts.map +0 -1
  588. package/dist/testing/bootstrap_success.js +0 -144
  589. package/dist/testing/mock_fs.d.ts +0 -20
  590. package/dist/testing/mock_fs.d.ts.map +0 -1
  591. package/dist/testing/mock_fs.js +0 -28
  592. package/dist/testing/standard.d.ts +0 -122
  593. package/dist/testing/standard.d.ts.map +0 -1
  594. package/dist/testing/standard.js +0 -73
  595. package/dist/testing/testing_rate_limiter.d.ts +0 -65
  596. package/dist/testing/testing_rate_limiter.d.ts.map +0 -1
  597. package/dist/testing/testing_rate_limiter.js +0 -80
@@ -0,0 +1,783 @@
1
+ /**
2
+ * The per-socket half of the WebSocket endpoint — one upgraded socket, from
3
+ * admission to close.
4
+ *
5
+ * `register_action_ws` (`actions/register_action_ws.ts`) mounts the upgrade
6
+ * route; per upgrade it reads the credential off the Hono context into a
7
+ * `WsSocketUpgrade` and hands that, with the endpoint-level `WsSocketEndpoint`,
8
+ * to `create_ws_socket_events`, whose `WSEvents` the runtime adapter drives.
9
+ * Nothing here reads the Hono context or imports the mount, so a socket is
10
+ * reachable with a fake `WSContext` and no app.
11
+ *
12
+ * The WS-specific concerns — connection lifecycle, heartbeat,
13
+ * cancel-notification interception, socket-scoped notify — live here; the
14
+ * relation to `create_rpc_endpoint` and the shared `perform_action` core is in
15
+ * the `register_action_ws` module doc.
16
+ *
17
+ * ## Admission: the revocation race
18
+ *
19
+ * The auth middleware resolves the credential before the handshake, and the
20
+ * socket exists only once `onOpen` fires. Every revocation (session revoke,
21
+ * logout, password change, token revoke, account delete) closes the sockets
22
+ * *registered* under what it revoked, so one landing in that gap would close
23
+ * nothing, and the socket would then be admitted on the dead credential and
24
+ * keep it: per-message dispatch re-authorizes the *actor*, never the session
25
+ * or token — and its admission could evict a live socket of the same account
26
+ * past the cap.
27
+ *
28
+ * `onOpen` closes the gap in three steps: register the connection **pending**
29
+ * (`ConnectionRegistry.register_pending` — closeable by every
30
+ * revocation, inert otherwise), re-read the credential, the acting actor, and
31
+ * the role (`recheck_admission`, which the audit stream shares), then
32
+ * `ConnectionRegistry.admit`. A
33
+ * revocation whose close ran after the registration found the pending entry,
34
+ * so `admit` refuses; one that committed before the re-read is seen by it. The
35
+ * cap is enforced only by `admit`, so a refused upgrade evicts nothing, and
36
+ * pending registrations never count toward it.
37
+ *
38
+ * That argument needs every revocation to close only after its commit —
39
+ * otherwise a close could miss the registration while the re-read still sees
40
+ * the credential alive. Every one does: the revocation handlers queue their
41
+ * close behind the commit (`queue_connection_close`), and the audit emitter
42
+ * announces a success row to the listeners behind it too. So no interleaving
43
+ * admits a revoked credential; `docs/security.md` §Connection Admission says
44
+ * what that rests on.
45
+ *
46
+ * The acting actor the upgrade resolved is re-read beside the credential on
47
+ * every endpoint, gated or not: an actor soft-deleted under its live account
48
+ * keeps the account's sessions and tokens and its own grant rows, so neither
49
+ * the credential nor the role re-read would refuse it. A tombstoned acting
50
+ * actor is refused like a dead credential.
51
+ *
52
+ * An endpoint gated on `required_roles` re-reads them beside the credential:
53
+ * its upgrade passed the role gate before the socket registered, so a role
54
+ * revocation landing in between would close nothing (a `role` close target
55
+ * matches registrations), and the socket would be admitted without the role
56
+ * for its lifetime. The re-read reads the acting actor's grants afresh and
57
+ * admits on a global grant of any listed role, as the gate did.
58
+ *
59
+ * A refused upgrade is closed with `WS_CLOSE_SESSION_REVOKED` (4001, the
60
+ * reason a revoked socket gets) for a dead credential or a deleted acting actor,
61
+ * `WS_CLOSE_PERMISSION_REVOKED` (4006) for a lost role, or
62
+ * `WS_CLOSE_INTERNAL_ERROR` (1011) when a re-read itself failed — fail
63
+ * closed, never admitted unchecked. The handshake has already answered `101`,
64
+ * so a refusal can only be a close frame.
65
+ *
66
+ * Once the server is shutting down (`AppServer.close` ran the registry's
67
+ * `close_all_connections`) a refusal is `WS_CLOSE_GOING_AWAY` instead, so the
68
+ * client reconnects rather than treating its credential as revoked: an upgrade
69
+ * pending at the close-all is closed by it — and its abort makes the refusal
70
+ * that follows the re-read send nothing more, even when the database closed
71
+ * under that re-read — and one that registers after it is born closed and
72
+ * skips the re-read (`ConnectionRegistry` module doc, "Shutdown").
73
+ *
74
+ * Frames that arrive before admission are queued, not dispatched: nothing runs
75
+ * on a credential that has not been re-read. They dispatch in arrival order
76
+ * once the socket is admitted and `on_socket_open` has completed, and are
77
+ * dropped unread on a refusal. The queue holds at most
78
+ * `MAX_PRE_ADMISSION_FRAMES` frames and
79
+ * `PRE_ADMISSION_QUEUE_BYTES_FACTOR` × `max_message_bytes` bytes; past either
80
+ * the socket is closed.
81
+ *
82
+ * ## Credential deadline
83
+ *
84
+ * Expiry writes nothing a revocation listener could hear, so the socket
85
+ * watches its own credential's expiry (`ResolvedAuth.credential_deadline`,
86
+ * the earlier of a session's cookie and row, an API token's row, none for a
87
+ * daemon token or an eternal token). From admission, a timer closes it at the
88
+ * deadline with `WS_CLOSE_SESSION_REVOKED` (4001) and
89
+ * `WS_CLOSE_CREDENTIAL_EXPIRED_REASON`. The wait is a chain of timers of at
90
+ * most an hour (`schedule_credential_deadline`), so a session's 30 days — past
91
+ * `setTimeout`'s ceiling — does not fire at once, and each wake-up checks the
92
+ * wall clock too, so a host suspend does not extend the deadline.
93
+ *
94
+ * ## A server-closed socket dispatches nothing
95
+ *
96
+ * A runtime adapter can keep delivering inbound frames after the server calls
97
+ * `ws.close(…)`, until the close handshake completes (`@hono/node-ws` does —
98
+ * up to the `ws` close timeout when the client withholds its close frame).
99
+ * So every server-side close — revocation, cap eviction, heartbeat timeout,
100
+ * an oversized message, a refused admission — removes the connection from the
101
+ * registry and aborts the socket's signal *at close time*, and `onMessage`
102
+ * drops any frame on a socket that has ended. A connection the registry no
103
+ * longer holds though nothing closed its socket (a bare `remove_connection`)
104
+ * is ended on its next frame (`ConnectionRegistry.is_registered`), with
105
+ * `WS_CLOSE_INTERNAL_ERROR` — it must not linger as a socket that reads
106
+ * everything and answers nothing.
107
+ *
108
+ * @module
109
+ */
110
+ import { UnreachableError } from '@fuzdev/fuz_util/error.ts';
111
+ import { recheck_admission, resolved_auth_api_token_id, resolved_auth_session_hash } from '../auth/resolved_auth.js';
112
+ import { schedule_credential_deadline } from '../auth/credential_deadline.js';
113
+ import { flush_request_effects } from '../http/pending_effects.js';
114
+ import { jsonrpc_error_messages } from '../http/jsonrpc_errors.js';
115
+ import { classify_jsonrpc_message, create_jsonrpc_error_response, create_jsonrpc_notification, to_jsonrpc_params } from '../http/jsonrpc_helpers.js';
116
+ import { cancel_action_spec, CancelNotificationParams } from './cancel.js';
117
+ import { WS_CLOSE_CONNECTION_UNREGISTERED_REASON, WS_CLOSE_GOING_AWAY, WS_CLOSE_INTERNAL_ERROR, WS_CLOSE_INTERNAL_ERROR_REASON, WS_CLOSE_MESSAGE_TOO_BIG, WS_CLOSE_MESSAGE_TOO_BIG_REASON, WS_CLOSE_PERMISSION_REVOKED, WS_CLOSE_PERMISSION_REVOKED_REASON, WS_CLOSE_POLICY_VIOLATION, WS_CLOSE_PRE_ADMISSION_OVERFLOW_REASON, WS_CLOSE_CREDENTIAL_EXPIRED_REASON, WS_CLOSE_SERVER_HEARTBEAT_TIMEOUT, WS_CLOSE_SERVER_HEARTBEAT_TIMEOUT_REASON, WS_CLOSE_SESSION_REVOKED, WS_CLOSE_SOCKET_OPEN_FAILED_REASON, WS_CLOSE_SESSION_REVOKED_REASON, utf8_length, utf8_length_over } from './transports.js';
118
+ import { audit_unmatched_peer_response } from './peer_request.js';
119
+ import { perform_action, serialize_action_result } from './perform_action.js';
120
+ /**
121
+ * Max inbound frames one socket may have queued while it is still opening —
122
+ * between the handshake and the end of the credential re-check plus
123
+ * `on_socket_open`. One more closes the socket with
124
+ * `WS_CLOSE_POLICY_VIOLATION`.
125
+ *
126
+ * Frames sent before the socket is open wait rather than dispatch (nothing
127
+ * runs on a credential that has not been re-read), and a runtime adapter hands
128
+ * them over with no backpressure, so the wait needs a bound. This is the count
129
+ * half; `PRE_ADMISSION_QUEUE_BYTES_FACTOR` is the byte half.
130
+ *
131
+ * Sized so a client flushing its durable queue on reopen never meets it at the
132
+ * default `DEFAULT_QUEUE_MAX_SIZE` (`actions/socket.svelte.ts`). A consumer
133
+ * that raises the client's queue above this cap makes a full flush overflow
134
+ * it: the socket closes with `WS_CLOSE_POLICY_VIOLATION`, the flushed requests
135
+ * fail, and the client reconnects.
136
+ */
137
+ export const MAX_PRE_ADMISSION_FRAMES = 256;
138
+ /**
139
+ * The byte budget of one socket's pre-admission queue, as a multiple of the
140
+ * endpoint's `max_message_bytes`: the frames queued while the socket is still
141
+ * opening may total at most this many maximum-size messages. The frame that
142
+ * would pass it closes the socket with `WS_CLOSE_POLICY_VIOLATION`.
143
+ *
144
+ * The count cap alone (`MAX_PRE_ADMISSION_FRAMES`) would let each pending
145
+ * socket hold that many full-size messages, and pending sockets are not
146
+ * capped per account — a registration that is never admitted must not be able
147
+ * to pin that much memory. Small requests, which is what a client flushes on
148
+ * reopen, meet the count cap long before this one.
149
+ *
150
+ * A client whose queued requests total more than this budget and flushes
151
+ * them all on reopen overflows it: the socket closes, the flushed requests
152
+ * fail, and the client reconnects.
153
+ */
154
+ export const PRE_ADMISSION_QUEUE_BYTES_FACTOR = 4;
155
+ /**
156
+ * Max requests one socket may have dispatching at once. A request that
157
+ * arrives past it is answered `queue_overflow` with its id
158
+ * (`WS_INBOUND_DISPATCH_OVERFLOW_MESSAGE`) and not dispatched; the socket
159
+ * stays open, and the requests already running keep answering.
160
+ *
161
+ * A runtime adapter can hand frames over without awaiting the previous one's
162
+ * dispatch (`@hono/node-ws` does not await `onMessage`), so without a ceiling
163
+ * one socket could hold any number of handlers — and their pooled database
164
+ * work — at once. Shedding rather than queueing keeps the socket reading, so a
165
+ * reply to a server-initiated request still reaches the handler awaiting it.
166
+ * Generous, so ordinary pipelining never meets it. Same value as the Rust
167
+ * spine's `MAX_INBOUND_DISPATCHES_PER_CONNECTION`.
168
+ *
169
+ * Counted are the requests that passed envelope validation and are not yet
170
+ * answered — the entries of the socket's cancellation map. Notifications,
171
+ * replies to server-initiated requests, and refused frames never count.
172
+ */
173
+ export const MAX_INBOUND_DISPATCHES_PER_CONNECTION = 128;
174
+ /**
175
+ * The `queue_overflow` message a request gets past
176
+ * `MAX_INBOUND_DISPATCHES_PER_CONNECTION`. Same text as the Rust spine's.
177
+ */
178
+ export const WS_INBOUND_DISPATCH_OVERFLOW_MESSAGE = 'too many concurrent requests on this connection';
179
+ /**
180
+ * The `data.reason` of the `invalid_request` a WebSocket request gets when its
181
+ * id names a request still running on the same socket. The duplicate is not
182
+ * dispatched; the running request keeps its id, so its reply, and a `cancel`
183
+ * naming that id, stay its own. An id is free again once its request has been
184
+ * answered.
185
+ */
186
+ export const ERROR_DUPLICATE_REQUEST_ID = 'duplicate_request_id';
187
+ /**
188
+ * Send one JSON-RPC error response frame to `connection_id` through the
189
+ * registry's outbound queue.
190
+ */
191
+ const send_error_response = (registry, connection_id, id, error) => {
192
+ registry.send_to(connection_id, JSON.stringify(create_jsonrpc_error_response(id, error)));
193
+ };
194
+ /**
195
+ * Build the Hono `WSEvents` for one upgraded socket: admission, the receive
196
+ * watchdog, the credential deadline, frame dispatch, and close (module doc).
197
+ *
198
+ * @param endpoint - the endpoint-level values, shared by all of its sockets
199
+ * @param upgrade - what the upgrade request captured for this socket
200
+ * @returns the adapter's `onOpen` / `onMessage` / `onClose` for the socket
201
+ *
202
+ * @internal
203
+ */
204
+ export const create_ws_socket_events = (endpoint, upgrade) => {
205
+ const { path, db, log, registry, action_map, receive_timeout, receive_tick_interval, max_message_bytes, pre_admission_queue_max_bytes, on_socket_open, on_socket_close, action_ip_rate_limiter, action_account_rate_limiter, on_effect_error, required_roles } = endpoint;
206
+ const { client_ip, upgrade_preset, resolved, upgrade_context } = upgrade;
207
+ // the request context's account — `register_action_ws` holds it to the
208
+ // credential's before the upgrade
209
+ const account_id = upgrade_context.account.id;
210
+ // Session-based connections have a session hash for targeted
211
+ // revocation. Bearer/daemon connections pass null — still reachable
212
+ // by an `account` or `token` close target.
213
+ const session_hash = resolved_auth_session_hash(resolved);
214
+ // `api_token.id` — set only for bearer connections; enables a
215
+ // `token` close target to tear down just this socket on
216
+ // `token_revoke` without affecting the account's other sockets.
217
+ const api_token_id = resolved_auth_api_token_id(resolved);
218
+ // Per-socket abort controller — fires when the socket ends, chained
219
+ // into every in-flight handler's per-request controller via
220
+ // `AbortSignal.any`. Keeping both signals lets the client
221
+ // cancel-one-request-by-id (via the `cancel` notification) without
222
+ // tearing down the whole socket. Registered with the registry, which
223
+ // aborts it when it closes the connection (revocation, cap eviction),
224
+ // and aborted here on every other end (`end_socket`, `onClose`) — at
225
+ // close time, not when the close handshake lands.
226
+ const socket_abort_controller = new AbortController();
227
+ // Per-request controllers keyed by JSON-RPC request id — lets an
228
+ // incoming `cancel` notification abort just the matching handler.
229
+ // Populated on request dispatch, cleared in the handler's `finally`
230
+ // so a late-arriving cancel for a completed id (or a reused id)
231
+ // can't null-abort a freshly-arrived request. Idempotent: cancel
232
+ // for unknown ids no-ops. One entry per running request — a
233
+ // request reusing a live id is refused before it gets one — so its
234
+ // size is the in-flight count `MAX_INBOUND_DISPATCHES_PER_CONNECTION`
235
+ // bounds.
236
+ const pending_controllers = new Map();
237
+ // Identity is assembled at upgrade time so `on_socket_close` can
238
+ // read it — by the time the hook runs the registry record is gone,
239
+ // removed in `onClose` or earlier by a revocation, a connection-cap
240
+ // eviction, or another server-side close.
241
+ const identity = {
242
+ session_hash,
243
+ account_id,
244
+ api_token_id,
245
+ required_roles
246
+ };
247
+ // Captured on open, consumed on close. Undefined before onOpen
248
+ // fires or when a consumer never opens (e.g. immediate disconnect).
249
+ let captured_connection_id;
250
+ // Where the socket is in its life:
251
+ // - `opening` — from the handshake until admission and `on_socket_open`
252
+ // have both completed. Inbound frames are queued.
253
+ // - `open` — admitted; inbound frames dispatch.
254
+ // - `ended` — the client closed it, the server closed it, or it was
255
+ // refused at admission. Inbound frames are dropped: an adapter can
256
+ // keep delivering them until the close handshake lands.
257
+ let phase = 'opening';
258
+ // read through a function so a check after an `await` isn't narrowed
259
+ // to the value assigned before it
260
+ const phase_is = (expected) => phase === expected;
261
+ // Whether the connection was ever admitted — gates `on_socket_close`,
262
+ // the counterpart of an `on_socket_open` a refused socket never ran.
263
+ let was_admitted = false;
264
+ // The code the registry closed the pending registration with, taken
265
+ // before `onClose` removes it: the client's answer to that close can
266
+ // arrive before the re-read returns.
267
+ let pending_close_code = null;
268
+ // Frames received while `opening`, in arrival order — dispatched once
269
+ // the socket is open, dropped if it ends first. Each carries the
270
+ // settlers of the promise its `onMessage` call returned, so a caller
271
+ // awaiting that call (the test harnesses do) resumes when the frame
272
+ // has been dispatched or dropped.
273
+ const queued_frames = [];
274
+ // bytes queued so far, against `pre_admission_queue_max_bytes` — only
275
+ // read while the socket is opening, so never reset
276
+ let queued_bytes = 0;
277
+ const drop_queued_frames = () => {
278
+ for (const frame of queued_frames.splice(0))
279
+ frame.resolve();
280
+ };
281
+ // Receive-silence watchdog. Seeded at admission so the first window is
282
+ // exempt (cold-start grace — avoid killing mid-handshake sockets).
283
+ // Bumped by onMessage. Any incoming activity counts, not just
284
+ // heartbeats — chatty clients don't need to send extras.
285
+ let last_receive_time = 0;
286
+ let receive_timer = null;
287
+ const stop_receive_timer = () => {
288
+ if (receive_timer !== null) {
289
+ clearInterval(receive_timer);
290
+ receive_timer = null;
291
+ }
292
+ };
293
+ // The credential deadline: the socket closes when the credential it
294
+ // upgraded on expires (4001, `WS_CLOSE_CREDENTIAL_EXPIRED_REASON`) —
295
+ // expiry writes nothing a revocation listener hears. Started at
296
+ // admission, cancelled when the socket ends.
297
+ let cancel_credential_deadline = null;
298
+ const stop_credential_deadline = () => {
299
+ cancel_credential_deadline?.();
300
+ cancel_credential_deadline = null;
301
+ };
302
+ // The one place a socket's end is recorded, whoever ended it — the
303
+ // registry (revocation, cap eviction), `end_socket`, or `onClose` all
304
+ // abort this controller.
305
+ socket_abort_controller.signal.addEventListener('abort', () => {
306
+ phase = 'ended';
307
+ stop_receive_timer();
308
+ stop_credential_deadline();
309
+ drop_queued_frames();
310
+ }, { once: true });
311
+ // End the socket from the server side: unregister it and abort its
312
+ // signal *now* — so nothing more dispatches and in-flight handlers
313
+ // see `signal.aborted` while the close handshake is still in flight —
314
+ // then send the close frame. The registry does all three for a
315
+ // connection it still holds, sending the close behind the waiting
316
+ // frames the socket has room for and under the close deadline; a socket
317
+ // whose connection is already gone has nothing waiting and is closed
318
+ // directly.
319
+ const end_socket = (ws, code, reason) => {
320
+ const closed = captured_connection_id !== undefined &&
321
+ registry.close_connection(captured_connection_id, code, reason);
322
+ socket_abort_controller.abort();
323
+ if (closed)
324
+ return;
325
+ try {
326
+ ws.close(code, reason);
327
+ }
328
+ catch (error) {
329
+ log.error(`ws close ${code} failed:`, error);
330
+ }
331
+ };
332
+ // Refuse an upgrade at admission. A socket that already ended needs no
333
+ // close frame: a revocation closed its pending registration (the
334
+ // registry sent the 4001), the shutdown did (the 1001 — so a refusal
335
+ // during shutdown never sends a revocation's or a failure's code), or
336
+ // the client went away. Its registration is gone too — the registry
337
+ // removed it with its close, `onClose` with the client's — and it is
338
+ // not removed again here: `remove_connection` is for a socket that has
339
+ // closed, and would drop the close deadline of one that has not.
340
+ const refuse_upgrade = (ws, code, reason) => {
341
+ if (!socket_abort_controller.signal.aborted)
342
+ end_socket(ws, code, reason);
343
+ };
344
+ // Socket-scoped notification helper — routes to this socket only,
345
+ // matches the `ctx.notify` semantics exposed to per-message handlers.
346
+ // Through the registry's outbound queue, like every other frame: a
347
+ // handler that notifies past the connection's budget closes it.
348
+ const notify_socket = (connection_id) => (notify_method, notify_params) => {
349
+ try {
350
+ const notification = create_jsonrpc_notification(notify_method, to_jsonrpc_params(notify_params));
351
+ registry.send_to(connection_id, JSON.stringify(notification));
352
+ }
353
+ catch (error) {
354
+ log.error('notify send failed:', notify_method, error);
355
+ }
356
+ };
357
+ // Admit the upgraded socket: register it pending, re-check its
358
+ // credential, admit it (module doc, "Admission: the revocation
359
+ // race"). Returns the connection id once admitted, or `null` after
360
+ // refusing — the socket is then already closed.
361
+ const admit_upgrade = async (ws) => {
362
+ // Pending first, synchronously: from here every revocation finds it.
363
+ const connection_id = registry.register_pending(ws, identity, socket_abort_controller);
364
+ captured_connection_id = connection_id;
365
+ // Born closed — the server is shutting down, and the registry has
366
+ // already closed the socket with `WS_CLOSE_GOING_AWAY` — or ended
367
+ // otherwise before the re-read: nothing to re-read for. The removal
368
+ // is a no-op for a born-closed registration, which was never inserted.
369
+ if (socket_abort_controller.signal.aborted) {
370
+ registry.remove_connection(connection_id);
371
+ log.info(registry.is_closing()
372
+ ? 'ws upgrade admission: refused, the server is shutting down'
373
+ : 'ws upgrade admission: socket ended before the re-check', connection_id, account_id);
374
+ return null;
375
+ }
376
+ if (!upgrade_preset) {
377
+ let recheck;
378
+ try {
379
+ // pool-level `db`: the reads must see every revocation committed
380
+ // before they start
381
+ recheck = await recheck_admission({ db }, { resolved, request_context: upgrade_context, required_roles });
382
+ }
383
+ catch (error) {
384
+ // fail closed — never admitted unchecked. During shutdown the
385
+ // database may close under the re-read; that is the shutdown's
386
+ // refusal, not a failure.
387
+ if (registry.is_closing()) {
388
+ log.info('ws upgrade admission: re-check ended by the shutdown', error);
389
+ }
390
+ else {
391
+ log.error('ws upgrade admission: credential, actor, or role re-check failed', error);
392
+ }
393
+ refuse_upgrade(ws, WS_CLOSE_INTERNAL_ERROR, WS_CLOSE_INTERNAL_ERROR_REASON);
394
+ return null;
395
+ }
396
+ switch (recheck) {
397
+ case 'admitted':
398
+ break;
399
+ case 'credential_revoked':
400
+ log.info('ws upgrade admission: credential revoked during the upgrade', connection_id, account_id);
401
+ refuse_upgrade(ws, WS_CLOSE_SESSION_REVOKED, WS_CLOSE_SESSION_REVOKED_REASON);
402
+ return null;
403
+ case 'actor_inactive':
404
+ log.info('ws upgrade admission: acting actor deleted during the upgrade', connection_id, account_id);
405
+ refuse_upgrade(ws, WS_CLOSE_SESSION_REVOKED, WS_CLOSE_SESSION_REVOKED_REASON);
406
+ return null;
407
+ case 'role_revoked':
408
+ log.info('ws upgrade admission: role revoked during the upgrade', connection_id, account_id);
409
+ refuse_upgrade(ws, WS_CLOSE_PERMISSION_REVOKED, WS_CLOSE_PERMISSION_REVOKED_REASON);
410
+ return null;
411
+ default:
412
+ throw new UnreachableError(recheck);
413
+ }
414
+ }
415
+ // The cap's eviction happens here, after every gate and the re-read,
416
+ // so a refused upgrade never closes someone else's socket.
417
+ if (!registry.admit(connection_id)) {
418
+ // why the entry closed, from the code the registry closed it with
419
+ const code = pending_close_code ?? registry.take_pending_close_code(connection_id);
420
+ log.info(code === WS_CLOSE_PERMISSION_REVOKED
421
+ ? 'ws upgrade admission: closed by a permission revocation while pending'
422
+ : code === WS_CLOSE_SESSION_REVOKED
423
+ ? 'ws upgrade admission: closed by a credential revocation while pending'
424
+ : code === WS_CLOSE_GOING_AWAY || registry.is_closing()
425
+ ? 'ws upgrade admission: closed by the shutdown while pending'
426
+ : 'ws upgrade admission: ended while pending', connection_id, account_id);
427
+ refuse_upgrade(ws, WS_CLOSE_SESSION_REVOKED, WS_CLOSE_SESSION_REVOKED_REASON);
428
+ return null;
429
+ }
430
+ was_admitted = true;
431
+ return connection_id;
432
+ };
433
+ const open_socket = async (ws) => {
434
+ const connection_id = await admit_upgrade(ws);
435
+ if (connection_id === null)
436
+ return;
437
+ log.debug('ws opened', connection_id);
438
+ // Not on a socket that ended in the tick since `admit`, for the same
439
+ // reason as the heartbeat timer below.
440
+ if (!phase_is('ended')) {
441
+ cancel_credential_deadline = schedule_credential_deadline(resolved.credential_deadline, () => {
442
+ log.info('ws credential expired — closing', WS_CLOSE_SESSION_REVOKED, connection_id, identity.account_id);
443
+ end_socket(ws, WS_CLOSE_SESSION_REVOKED, WS_CLOSE_CREDENTIAL_EXPIRED_REASON);
444
+ });
445
+ }
446
+ // Not on a socket that ended in the tick since `admit` (a revocation
447
+ // can land there): its abort listener has already run, so nothing
448
+ // would ever stop the timer.
449
+ if (receive_timeout !== null && receive_tick_interval !== null && !phase_is('ended')) {
450
+ last_receive_time = Date.now();
451
+ receive_timer = setInterval(() => {
452
+ const now = Date.now();
453
+ const silence = now - last_receive_time;
454
+ if (silence >= receive_timeout) {
455
+ log.info(`receive timeout (${silence}ms) — closing ${WS_CLOSE_SERVER_HEARTBEAT_TIMEOUT}`, connection_id, identity.account_id);
456
+ stop_receive_timer();
457
+ end_socket(ws, WS_CLOSE_SERVER_HEARTBEAT_TIMEOUT, WS_CLOSE_SERVER_HEARTBEAT_TIMEOUT_REASON);
458
+ }
459
+ }, receive_tick_interval);
460
+ }
461
+ if (on_socket_open) {
462
+ try {
463
+ await on_socket_open({
464
+ ws,
465
+ connection_id,
466
+ identity,
467
+ notify: notify_socket(connection_id),
468
+ signal: socket_abort_controller.signal
469
+ });
470
+ }
471
+ catch (error) {
472
+ log.error('on_socket_open failed — closing socket:', error);
473
+ // an ended socket has no one to tell
474
+ if (phase_is('ended'))
475
+ return;
476
+ send_error_response(registry, connection_id, null, jsonrpc_error_messages.internal_error());
477
+ end_socket(ws, WS_CLOSE_INTERNAL_ERROR, WS_CLOSE_SOCKET_OPEN_FAILED_REASON);
478
+ return;
479
+ }
480
+ }
481
+ // Ended while the hook ran (a revocation, the client leaving) — the
482
+ // queue is already dropped.
483
+ if (!phase_is('opening'))
484
+ return;
485
+ // Open: dispatch what arrived meanwhile, in arrival order and back
486
+ // to back, the way the adapter would have delivered it.
487
+ phase = 'open';
488
+ for (const frame of queued_frames.splice(0)) {
489
+ if (registry.is_registered(connection_id)) {
490
+ handle_frame(frame.data, connection_id).then(frame.resolve, frame.reject);
491
+ }
492
+ else {
493
+ frame.resolve();
494
+ }
495
+ }
496
+ };
497
+ // Dispatch one inbound frame on an admitted, still-registered
498
+ // connection.
499
+ const handle_frame = async (data, connection_id) => {
500
+ const send_error = (id, error) => send_error_response(registry, connection_id, id, error);
501
+ let json;
502
+ try {
503
+ json = JSON.parse(String(data));
504
+ }
505
+ catch (error) {
506
+ log.error('JSON parse error:', error);
507
+ send_error(null, jsonrpc_error_messages.parse_error());
508
+ return;
509
+ }
510
+ // Batch JSON-RPC is not supported on the WebSocket path.
511
+ if (Array.isArray(json)) {
512
+ send_error(null, jsonrpc_error_messages.invalid_request('batch JSON-RPC requests are not supported on WebSocket'));
513
+ return;
514
+ }
515
+ // One classifier for both transports (`classify_jsonrpc_message`,
516
+ // the Rust twin's order: version → method → id → params); this
517
+ // path applies the WebSocket semantics to its arms.
518
+ const classified = classify_jsonrpc_message(json);
519
+ switch (classified.kind) {
520
+ case 'invalid':
521
+ send_error(classified.id, classified.error);
522
+ return;
523
+ case 'response': {
524
+ // A reply to a server-initiated request (`peer/ping`, etc.) —
525
+ // routed to the pending-request registry scoped to THIS
526
+ // socket. An unmatched id (unsolicited, cross-connection, or
527
+ // already settled) resolves nothing and is dropped, so the
528
+ // read loop survives a junk frame.
529
+ const { response } = classified;
530
+ const matched = registry.resolve_peer_response(connection_id, response);
531
+ if (!matched)
532
+ audit_unmatched_peer_response(log, connection_id, response.id);
533
+ return;
534
+ }
535
+ case 'notification': {
536
+ // `cancel` is intercepted for request-scoped cancellation;
537
+ // other notifications are silenced per JSON-RPC spec (consumer
538
+ // notification handlers are not a feature yet). Notifications
539
+ // stay unvalidated — they carry no id to answer an error on.
540
+ if (classified.method === cancel_action_spec.method) {
541
+ const parsed = CancelNotificationParams.safeParse(classified.params);
542
+ if (!parsed.success) {
543
+ log.debug('cancel: invalid params, ignoring', parsed.error.issues);
544
+ return;
545
+ }
546
+ const controller = pending_controllers.get(parsed.data.request_id);
547
+ if (controller) {
548
+ controller.abort();
549
+ }
550
+ else {
551
+ log.debug('cancel: no pending request for id', parsed.data.request_id);
552
+ }
553
+ }
554
+ return;
555
+ }
556
+ case 'request':
557
+ break;
558
+ default:
559
+ throw new UnreachableError(classified);
560
+ }
561
+ const { method, id, params } = classified.request;
562
+ // A request is live from here until its reply is sent. One reusing
563
+ // a live id is refused rather than dispatched: two requests under
564
+ // one id would share a cancel entry and a reply id, and the first to
565
+ // finish would clear the other's. Checked before the ceiling — a
566
+ // duplicate is the client's error whatever the load, and retrying it
567
+ // cannot succeed while the first runs. Neither refusal reaches
568
+ // `perform_action`, so neither charges the action rate limiters —
569
+ // the same on the Rust spine.
570
+ if (pending_controllers.has(id)) {
571
+ send_error(id, jsonrpc_error_messages.invalid_request({ reason: ERROR_DUPLICATE_REQUEST_ID }));
572
+ return;
573
+ }
574
+ // The in-flight ceiling: shed, don't queue — see
575
+ // `MAX_INBOUND_DISPATCHES_PER_CONNECTION`.
576
+ if (pending_controllers.size >= MAX_INBOUND_DISPATCHES_PER_CONNECTION) {
577
+ send_error(id, jsonrpc_error_messages.queue_overflow(WS_INBOUND_DISPATCH_OVERFLOW_MESSAGE));
578
+ return;
579
+ }
580
+ // Per-action method lookup — return method_not_found before
581
+ // we engage the dispatch machinery. Specs without a handler
582
+ // (client-only / dispatcher-handled) miss action_map and
583
+ // surface as method_not_found just like unknown methods.
584
+ const action = action_map.get(method);
585
+ if (!action) {
586
+ send_error(id, jsonrpc_error_messages.method_not_found(method));
587
+ return;
588
+ }
589
+ // Per-request controller — fires on explicit `cancel` or when the
590
+ // socket ends (via the socket_abort_controller chain below).
591
+ // Registered before dispatch, so the request is live (counted, and
592
+ // its id taken) from the moment it is accepted — so a cancel
593
+ // arriving mid-handler finds it;
594
+ // cleared in `finally` so late cancels for a completed id (or a
595
+ // future request that reuses the id) can't null-abort the wrong
596
+ // handler.
597
+ const request_controller = new AbortController();
598
+ pending_controllers.set(id, request_controller);
599
+ // Per-message side-effect queues. `pending_effects` collects
600
+ // eager fire-and-forget pool writes (audit emits, etc.);
601
+ // `post_commit_effects` collects deferred thunks pushed
602
+ // via `emit_after_commit` (WS notifications). Both flush
603
+ // in the `finally` so the next message sees a clean slate.
604
+ //
605
+ // Ordering invariant — the deferred queue starts first. Its
606
+ // thunks are invoked before the eager writes are awaited, so
607
+ // a revocation's close never waits on the audit INSERT: held
608
+ // behind a slow write, the socket would stay open on a
609
+ // credential already committed gone, and its next frame
610
+ // would be dispatched. The thunks that need a write (a
611
+ // success row's listener fan-out) await it themselves.
612
+ //
613
+ // Ordering invariant — reply-before-flush is load-bearing.
614
+ // A handler that revokes its own credential
615
+ // (`session_revoke_all`, `token_revoke` of the calling
616
+ // bearer) queues a close of this very socket on
617
+ // `post_commit_effects` (`queue_connection_close`), and
618
+ // the audit listeners repeat it from the same queue. The
619
+ // send on the success path runs before the `finally`
620
+ // flushes that queue, so the caller reads its reply and
621
+ // then the 4001 close. Inverting the order — flushing the
622
+ // queues before the send — would strand the caller without
623
+ // a reply.
624
+ const pending_effects = [];
625
+ const post_commit_effects = [];
626
+ const notify = notify_socket(connection_id);
627
+ // Server→client request seam (ActionPeer) — closes over this
628
+ // socket's connection_id so a handler can initiate a request to
629
+ // the originating client and await the reply.
630
+ const request_client = (request_method, request_params, request_options) => registry.request_connection(connection_id, request_method, request_params, request_options);
631
+ const signal = AbortSignal.any([socket_abort_controller.signal, request_controller.signal]);
632
+ try {
633
+ const result = await perform_action({
634
+ action,
635
+ raw_params: params,
636
+ request_id: id,
637
+ auth: resolved,
638
+ client_ip,
639
+ signal,
640
+ notify,
641
+ connection_id,
642
+ request_client,
643
+ preset: upgrade_preset
644
+ }, {
645
+ db,
646
+ pending_effects,
647
+ post_commit_effects,
648
+ log,
649
+ action_ip_rate_limiter,
650
+ action_account_rate_limiter
651
+ });
652
+ const { body: frame } = serialize_action_result(id, result, method, log);
653
+ // Refused when the connection is gone — closed while the handler
654
+ // ran, or closed by the handler's own notifications overflowing
655
+ // its outbound budget — so nothing follows a close frame.
656
+ registry.send_to(connection_id, frame);
657
+ }
658
+ finally {
659
+ pending_controllers.delete(id);
660
+ // deferred first — see the ordering invariant above
661
+ await flush_request_effects(pending_effects, post_commit_effects, log, on_effect_error
662
+ ? (reason) => on_effect_error(reason, { transport: 'ws', method, path })
663
+ : undefined);
664
+ }
665
+ };
666
+ return {
667
+ onOpen: async (_event, ws) => {
668
+ // An adapter may not await this (`@hono/node-ws` doesn't), so it
669
+ // must never reject.
670
+ try {
671
+ await open_socket(ws);
672
+ }
673
+ catch (error) {
674
+ log.error('ws open failed — closing socket:', error);
675
+ refuse_upgrade(ws, WS_CLOSE_INTERNAL_ERROR, WS_CLOSE_INTERNAL_ERROR_REASON);
676
+ }
677
+ },
678
+ onMessage: (event, ws) => {
679
+ // A socket the server closed — or refused — dispatches nothing
680
+ // more, however long its client withholds the close frame.
681
+ if (phase_is('ended'))
682
+ return;
683
+ const size = ws_message_size_over(event.data, max_message_bytes);
684
+ if (size !== null) {
685
+ log.warn(`closing socket: ${size}-byte message exceeds the ${max_message_bytes}-byte cap`);
686
+ end_socket(ws, WS_CLOSE_MESSAGE_TOO_BIG, WS_CLOSE_MESSAGE_TOO_BIG_REASON);
687
+ return;
688
+ }
689
+ if (phase_is('opening')) {
690
+ // Not open yet: queue, never dispatch — nothing runs on a
691
+ // credential that has not been re-read. Bounded by count and
692
+ // by bytes, since the adapter applies no backpressure.
693
+ const bytes = ws_message_size(event.data);
694
+ if (queued_frames.length >= MAX_PRE_ADMISSION_FRAMES ||
695
+ queued_bytes + bytes > pre_admission_queue_max_bytes) {
696
+ log.warn(`closing socket: pre-admission queue overflow (${queued_frames.length} frames, ${queued_bytes} + ${bytes} bytes)`);
697
+ end_socket(ws, WS_CLOSE_POLICY_VIOLATION, WS_CLOSE_PRE_ADMISSION_OVERFLOW_REASON);
698
+ return;
699
+ }
700
+ queued_bytes += bytes;
701
+ last_receive_time = Date.now();
702
+ return new Promise((resolve, reject) => {
703
+ queued_frames.push({ data: event.data, resolve, reject });
704
+ });
705
+ }
706
+ // The registry is the authority on whether the connection is
707
+ // still live. A close it made (revocation, cap eviction) ended the
708
+ // socket above; a connection it no longer holds though nothing
709
+ // closed the socket — a bare `remove_connection` — is ended here,
710
+ // rather than left reading every frame and answering none, where
711
+ // a chatty client would also keep the heartbeat from reaping it.
712
+ const connection_id = captured_connection_id;
713
+ if (connection_id === undefined || !registry.is_registered(connection_id)) {
714
+ log.warn('closing socket: its connection is no longer registered', connection_id);
715
+ end_socket(ws, WS_CLOSE_INTERNAL_ERROR, WS_CLOSE_CONNECTION_UNREGISTERED_REASON);
716
+ return;
717
+ }
718
+ // only a frame that is accepted counts as activity
719
+ last_receive_time = Date.now();
720
+ return handle_frame(event.data, connection_id);
721
+ },
722
+ onClose: async (event, ws) => {
723
+ // belt and braces: the abort listener stops the timer and the
724
+ // start is skipped on an ended socket, so this matters only if
725
+ // one of those is ever broken
726
+ stop_receive_timer();
727
+ socket_abort_controller.abort();
728
+ // Removed by the id captured on open — an adapter may hand each
729
+ // event its own `WSContext` (`@hono/bun` does), so the one this
730
+ // event carries can't identify the connection. And removed before
731
+ // the hook is awaited: the socket is gone, so a slow hook must not
732
+ // keep a dead entry that broadcasts still target and the
733
+ // per-account cap still counts.
734
+ if (captured_connection_id) {
735
+ if (!was_admitted) {
736
+ pending_close_code = registry.take_pending_close_code(captured_connection_id);
737
+ }
738
+ registry.remove_connection(captured_connection_id);
739
+ }
740
+ if (on_socket_close && captured_connection_id && was_admitted) {
741
+ try {
742
+ await on_socket_close({
743
+ ws,
744
+ connection_id: captured_connection_id,
745
+ identity
746
+ });
747
+ }
748
+ catch (error) {
749
+ log.error('on_socket_close failed:', error);
750
+ }
751
+ }
752
+ log.debug('ws closed', captured_connection_id, {
753
+ code: event.code,
754
+ reason: event.reason
755
+ });
756
+ }
757
+ };
758
+ };
759
+ /**
760
+ * The byte size of an inbound WebSocket message — UTF-8 bytes for a text
761
+ * frame, the buffer or blob size for a binary one.
762
+ */
763
+ const ws_message_size = (data) => {
764
+ if (typeof data === 'string')
765
+ return utf8_length(data);
766
+ if (data instanceof Blob)
767
+ return data.size;
768
+ if (data instanceof ArrayBuffer || ArrayBuffer.isView(data))
769
+ return data.byteLength;
770
+ return 0;
771
+ };
772
+ /**
773
+ * The byte size of an inbound WebSocket message when it exceeds `max_bytes`,
774
+ * else `null` — UTF-8 bytes for a text frame, the buffer or blob size for a
775
+ * binary one. A text frame that cannot exceed the cap is not encoded to be
776
+ * measured.
777
+ */
778
+ const ws_message_size_over = (data, max_bytes) => {
779
+ if (typeof data === 'string')
780
+ return utf8_length_over(data, max_bytes);
781
+ const size = ws_message_size(data);
782
+ return size > max_bytes ? size : null;
783
+ };