rivetkit 2.0.2 → 2.0.4-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (403) hide show
  1. package/README.md +3 -5
  2. package/dist/browser/client.d.ts +2485 -0
  3. package/dist/browser/client.js +5182 -0
  4. package/dist/browser/client.js.map +1 -0
  5. package/dist/browser/inspector/client.d.ts +130 -0
  6. package/dist/browser/inspector/client.js +2854 -0
  7. package/dist/browser/inspector/client.js.map +1 -0
  8. package/dist/browser/v3-DnYObHH3.d.ts +279 -0
  9. package/dist/inspector.tar.gz +0 -0
  10. package/dist/schemas/actor-inspector/v1.ts +784 -0
  11. package/dist/schemas/actor-inspector/v2.ts +796 -0
  12. package/dist/schemas/actor-inspector/v3.ts +899 -0
  13. package/dist/schemas/actor-persist/v1.ts +225 -0
  14. package/dist/schemas/actor-persist/v2.ts +268 -0
  15. package/dist/schemas/actor-persist/v3.ts +280 -0
  16. package/dist/schemas/actor-persist/v4.ts +406 -0
  17. package/dist/schemas/client-protocol/v1.ts +441 -0
  18. package/dist/schemas/client-protocol/v2.ts +438 -0
  19. package/dist/schemas/client-protocol/v3.ts +554 -0
  20. package/dist/schemas/file-system-driver/v1.ts +108 -0
  21. package/dist/schemas/file-system-driver/v2.ts +142 -0
  22. package/dist/schemas/file-system-driver/v3.ts +167 -0
  23. package/dist/schemas/persist/v1.ts +781 -0
  24. package/dist/schemas/transport/v1.ts +697 -0
  25. package/dist/tsup/actor/errors.cjs +106 -0
  26. package/dist/tsup/actor/errors.cjs.map +1 -0
  27. package/dist/tsup/actor/errors.d.cts +188 -0
  28. package/dist/tsup/actor/errors.d.ts +188 -0
  29. package/dist/tsup/actor/errors.js +106 -0
  30. package/dist/tsup/actor/errors.js.map +1 -0
  31. package/dist/tsup/actor-router-consts-D29T1Z-K.d.cts +24 -0
  32. package/dist/tsup/actor-router-consts-D29T1Z-K.d.ts +24 -0
  33. package/dist/tsup/chunk-325TLXJT.js +1060 -0
  34. package/dist/tsup/chunk-325TLXJT.js.map +1 -0
  35. package/dist/tsup/chunk-424PT5DM.js +23 -0
  36. package/dist/tsup/chunk-424PT5DM.js.map +1 -0
  37. package/dist/tsup/chunk-4JVIG3SS.cjs +6289 -0
  38. package/dist/tsup/chunk-4JVIG3SS.cjs.map +1 -0
  39. package/dist/tsup/chunk-6LJAZ5R4.cjs +96 -0
  40. package/dist/tsup/chunk-6LJAZ5R4.cjs.map +1 -0
  41. package/dist/tsup/chunk-6XU3FMCB.cjs +534 -0
  42. package/dist/tsup/chunk-6XU3FMCB.cjs.map +1 -0
  43. package/dist/tsup/chunk-7HTNH26M.js +509 -0
  44. package/dist/tsup/chunk-7HTNH26M.js.map +1 -0
  45. package/dist/tsup/chunk-AUVH72RE.cjs +5977 -0
  46. package/dist/tsup/chunk-AUVH72RE.cjs.map +1 -0
  47. package/dist/tsup/chunk-D4BYUPNQ.js +645 -0
  48. package/dist/tsup/chunk-D4BYUPNQ.js.map +1 -0
  49. package/dist/tsup/chunk-HDQ2JUQT.cjs +23 -0
  50. package/dist/tsup/chunk-HDQ2JUQT.cjs.map +1 -0
  51. package/dist/tsup/chunk-HHXX2VRM.js +6289 -0
  52. package/dist/tsup/chunk-HHXX2VRM.js.map +1 -0
  53. package/dist/tsup/chunk-JEAEA2PB.js +49 -0
  54. package/dist/tsup/chunk-JEAEA2PB.js.map +1 -0
  55. package/dist/tsup/chunk-JYSEG3VF.cjs +642 -0
  56. package/dist/tsup/chunk-JYSEG3VF.cjs.map +1 -0
  57. package/dist/tsup/chunk-K6DGYILQ.js +2657 -0
  58. package/dist/tsup/chunk-K6DGYILQ.js.map +1 -0
  59. package/dist/tsup/chunk-KJSYAUOM.js +96 -0
  60. package/dist/tsup/chunk-KJSYAUOM.js.map +1 -0
  61. package/dist/tsup/chunk-L47L3ZWJ.cjs +509 -0
  62. package/dist/tsup/chunk-L47L3ZWJ.cjs.map +1 -0
  63. package/dist/tsup/chunk-LXUQ667X.js +2006 -0
  64. package/dist/tsup/chunk-LXUQ667X.js.map +1 -0
  65. package/dist/tsup/chunk-MXNPAB5W.js +5977 -0
  66. package/dist/tsup/chunk-MXNPAB5W.js.map +1 -0
  67. package/dist/tsup/chunk-N4KRDJ56.js +72 -0
  68. package/dist/tsup/chunk-N4KRDJ56.js.map +1 -0
  69. package/dist/tsup/chunk-NIYZDWMW.cjs +2006 -0
  70. package/dist/tsup/chunk-NIYZDWMW.cjs.map +1 -0
  71. package/dist/tsup/chunk-PQZHDKRW.cjs +1060 -0
  72. package/dist/tsup/chunk-PQZHDKRW.cjs.map +1 -0
  73. package/dist/tsup/chunk-PVOE6BU7.cjs +1050 -0
  74. package/dist/tsup/chunk-PVOE6BU7.cjs.map +1 -0
  75. package/dist/tsup/chunk-Q4UD2GA4.cjs +1810 -0
  76. package/dist/tsup/chunk-Q4UD2GA4.cjs.map +1 -0
  77. package/dist/tsup/chunk-QUD664YZ.js +1810 -0
  78. package/dist/tsup/chunk-QUD664YZ.js.map +1 -0
  79. package/dist/tsup/chunk-RTOCTWME.js +1050 -0
  80. package/dist/tsup/chunk-RTOCTWME.js.map +1 -0
  81. package/dist/tsup/chunk-SAZZ4SB2.cjs +2657 -0
  82. package/dist/tsup/chunk-SAZZ4SB2.cjs.map +1 -0
  83. package/dist/tsup/chunk-SR3KQE7Q.cjs +72 -0
  84. package/dist/tsup/chunk-SR3KQE7Q.cjs.map +1 -0
  85. package/dist/tsup/chunk-V2GHLYC6.cjs +49 -0
  86. package/dist/tsup/chunk-V2GHLYC6.cjs.map +1 -0
  87. package/dist/tsup/chunk-V3WG7XTW.cjs +645 -0
  88. package/dist/tsup/chunk-V3WG7XTW.cjs.map +1 -0
  89. package/dist/tsup/chunk-VKVNIQRQ.js +257 -0
  90. package/dist/tsup/chunk-VKVNIQRQ.js.map +1 -0
  91. package/dist/tsup/chunk-WMPW7JYC.js +642 -0
  92. package/dist/tsup/chunk-WMPW7JYC.js.map +1 -0
  93. package/dist/tsup/chunk-Z7HNQ2WF.js +534 -0
  94. package/dist/tsup/chunk-Z7HNQ2WF.js.map +1 -0
  95. package/dist/tsup/chunk-ZFY5J2EP.cjs +257 -0
  96. package/dist/tsup/chunk-ZFY5J2EP.cjs.map +1 -0
  97. package/dist/tsup/client/mod.cjs +33 -0
  98. package/dist/tsup/client/mod.cjs.map +1 -0
  99. package/dist/tsup/client/mod.d.cts +64 -0
  100. package/dist/tsup/client/mod.d.ts +64 -0
  101. package/dist/tsup/client/mod.js +33 -0
  102. package/dist/tsup/client/mod.js.map +1 -0
  103. package/dist/tsup/common/log.cjs +21 -0
  104. package/dist/tsup/common/log.cjs.map +1 -0
  105. package/dist/tsup/common/log.d.cts +34 -0
  106. package/dist/tsup/common/log.d.ts +34 -0
  107. package/dist/tsup/common/log.js +21 -0
  108. package/dist/tsup/common/log.js.map +1 -0
  109. package/dist/tsup/common/websocket.cjs +10 -0
  110. package/dist/tsup/common/websocket.cjs.map +1 -0
  111. package/dist/tsup/common/websocket.d.cts +3 -0
  112. package/dist/tsup/common/websocket.d.ts +3 -0
  113. package/dist/tsup/common/websocket.js +10 -0
  114. package/dist/tsup/common/websocket.js.map +1 -0
  115. package/dist/tsup/config-BiNoIHRs.d.cts +80 -0
  116. package/dist/tsup/config-BiNoIHRs.d.ts +80 -0
  117. package/dist/tsup/config-P3XujgRr.d.ts +2594 -0
  118. package/dist/tsup/config-_gfywqqI.d.cts +2594 -0
  119. package/dist/tsup/context-Bxd8Cx4H.d.cts +75 -0
  120. package/dist/tsup/context-uNA4TRn3.d.ts +75 -0
  121. package/dist/tsup/db/drizzle/mod.cjs +49 -0
  122. package/dist/tsup/db/drizzle/mod.cjs.map +1 -0
  123. package/dist/tsup/db/drizzle/mod.d.cts +17 -0
  124. package/dist/tsup/db/drizzle/mod.d.ts +17 -0
  125. package/dist/tsup/db/drizzle/mod.js +49 -0
  126. package/dist/tsup/db/drizzle/mod.js.map +1 -0
  127. package/dist/tsup/db/mod.cjs +9 -0
  128. package/dist/tsup/db/mod.cjs.map +1 -0
  129. package/dist/tsup/db/mod.d.cts +9 -0
  130. package/dist/tsup/db/mod.d.ts +9 -0
  131. package/dist/tsup/db/mod.js +9 -0
  132. package/dist/tsup/db/mod.js.map +1 -0
  133. package/dist/tsup/driver-BcLvZcKl.d.cts +13 -0
  134. package/dist/tsup/driver-CPGHKXyh.d.ts +13 -0
  135. package/dist/tsup/driver-helpers/mod.cjs +53 -0
  136. package/dist/tsup/driver-helpers/mod.cjs.map +1 -0
  137. package/dist/tsup/driver-helpers/mod.d.cts +47 -0
  138. package/dist/tsup/driver-helpers/mod.d.ts +47 -0
  139. package/dist/tsup/driver-helpers/mod.js +53 -0
  140. package/dist/tsup/driver-helpers/mod.js.map +1 -0
  141. package/dist/tsup/driver-test-suite/mod.cjs +4974 -0
  142. package/dist/tsup/driver-test-suite/mod.cjs.map +1 -0
  143. package/dist/tsup/driver-test-suite/mod.d.cts +73 -0
  144. package/dist/tsup/driver-test-suite/mod.d.ts +73 -0
  145. package/dist/tsup/driver-test-suite/mod.js +4974 -0
  146. package/dist/tsup/driver-test-suite/mod.js.map +1 -0
  147. package/dist/tsup/inspector/mod.cjs +164 -0
  148. package/dist/tsup/inspector/mod.cjs.map +1 -0
  149. package/dist/tsup/inspector/mod.d.cts +130 -0
  150. package/dist/tsup/inspector/mod.d.ts +130 -0
  151. package/dist/tsup/inspector/mod.js +164 -0
  152. package/dist/tsup/inspector/mod.js.map +1 -0
  153. package/dist/tsup/keys-CydblqMh.d.cts +13 -0
  154. package/dist/tsup/keys-CydblqMh.d.ts +13 -0
  155. package/dist/tsup/mod.cjs +82 -0
  156. package/dist/tsup/mod.cjs.map +1 -0
  157. package/dist/tsup/mod.d.cts +126 -0
  158. package/dist/tsup/mod.d.ts +126 -0
  159. package/dist/tsup/mod.js +82 -0
  160. package/dist/tsup/mod.js.map +1 -0
  161. package/dist/tsup/serve-test-suite/mod.cjs +2601 -0
  162. package/dist/tsup/serve-test-suite/mod.cjs.map +1 -0
  163. package/dist/tsup/serve-test-suite/mod.d.cts +9 -0
  164. package/dist/tsup/serve-test-suite/mod.d.ts +9 -0
  165. package/dist/tsup/serve-test-suite/mod.js +2601 -0
  166. package/dist/tsup/serve-test-suite/mod.js.map +1 -0
  167. package/dist/tsup/test/mod.cjs +90 -0
  168. package/dist/tsup/test/mod.cjs.map +1 -0
  169. package/dist/tsup/test/mod.d.cts +26 -0
  170. package/dist/tsup/test/mod.d.ts +26 -0
  171. package/dist/tsup/test/mod.js +90 -0
  172. package/dist/tsup/test/mod.js.map +1 -0
  173. package/dist/tsup/utils-fwx3o3K9.d.cts +18 -0
  174. package/dist/tsup/utils-fwx3o3K9.d.ts +18 -0
  175. package/dist/tsup/utils.cjs +43 -0
  176. package/dist/tsup/utils.cjs.map +1 -0
  177. package/dist/tsup/utils.d.cts +148 -0
  178. package/dist/tsup/utils.d.ts +148 -0
  179. package/dist/tsup/utils.js +43 -0
  180. package/dist/tsup/utils.js.map +1 -0
  181. package/dist/tsup/v3-DnYObHH3.d.cts +279 -0
  182. package/dist/tsup/v3-DnYObHH3.d.ts +279 -0
  183. package/dist/tsup/workflow/mod.cjs +16 -0
  184. package/dist/tsup/workflow/mod.cjs.map +1 -0
  185. package/dist/tsup/workflow/mod.d.cts +25 -0
  186. package/dist/tsup/workflow/mod.d.ts +25 -0
  187. package/dist/tsup/workflow/mod.js +16 -0
  188. package/dist/tsup/workflow/mod.js.map +1 -0
  189. package/package.json +293 -5
  190. package/src/actor/config.ts +1221 -0
  191. package/src/actor/conn/driver.ts +61 -0
  192. package/src/actor/conn/drivers/http.ts +17 -0
  193. package/src/actor/conn/drivers/raw-request.ts +24 -0
  194. package/src/actor/conn/drivers/raw-websocket.ts +65 -0
  195. package/src/actor/conn/drivers/websocket.ts +144 -0
  196. package/src/actor/conn/mod.ts +288 -0
  197. package/src/actor/conn/persisted.ts +81 -0
  198. package/src/actor/conn/state-manager.ts +196 -0
  199. package/src/actor/contexts/action.ts +47 -0
  200. package/src/actor/contexts/base/actor.ts +347 -0
  201. package/src/actor/contexts/base/conn-init.ts +68 -0
  202. package/src/actor/contexts/base/conn.ts +73 -0
  203. package/src/actor/contexts/before-action-response.ts +42 -0
  204. package/src/actor/contexts/before-connect.ts +31 -0
  205. package/src/actor/contexts/connect.ts +42 -0
  206. package/src/actor/contexts/create-conn-state.ts +32 -0
  207. package/src/actor/contexts/create-vars.ts +39 -0
  208. package/src/actor/contexts/create.ts +39 -0
  209. package/src/actor/contexts/destroy.ts +42 -0
  210. package/src/actor/contexts/disconnect.ts +43 -0
  211. package/src/actor/contexts/index.ts +33 -0
  212. package/src/actor/contexts/request.ts +80 -0
  213. package/src/actor/contexts/run.ts +47 -0
  214. package/src/actor/contexts/sleep.ts +42 -0
  215. package/src/actor/contexts/state-change.ts +42 -0
  216. package/src/actor/contexts/wake.ts +42 -0
  217. package/src/actor/contexts/websocket.ts +80 -0
  218. package/src/actor/database.ts +13 -0
  219. package/src/actor/definition.ts +64 -0
  220. package/src/actor/driver.ts +114 -0
  221. package/src/actor/errors.ts +556 -0
  222. package/src/actor/instance/connection-manager.ts +574 -0
  223. package/src/actor/instance/event-manager.ts +314 -0
  224. package/src/actor/instance/keys.ts +146 -0
  225. package/src/actor/instance/kv.ts +241 -0
  226. package/src/actor/instance/mod.ts +1658 -0
  227. package/src/actor/instance/persisted.ts +67 -0
  228. package/src/actor/instance/queue-manager.ts +603 -0
  229. package/src/actor/instance/queue.ts +345 -0
  230. package/src/actor/instance/schedule-manager.ts +392 -0
  231. package/src/actor/instance/state-manager.ts +542 -0
  232. package/src/actor/instance/traces-driver.ts +128 -0
  233. package/src/actor/keys.test.ts +275 -0
  234. package/src/actor/keys.ts +89 -0
  235. package/src/actor/log.ts +6 -0
  236. package/src/actor/mod.ts +110 -0
  237. package/src/actor/protocol/old.ts +416 -0
  238. package/src/actor/protocol/serde.ts +222 -0
  239. package/src/actor/router-endpoints.ts +400 -0
  240. package/src/actor/router-websocket-endpoints.test.ts +54 -0
  241. package/src/actor/router-websocket-endpoints.ts +405 -0
  242. package/src/actor/router.ts +380 -0
  243. package/src/actor/schedule.ts +17 -0
  244. package/src/actor/schema.ts +291 -0
  245. package/src/actor/utils.test.ts +48 -0
  246. package/src/actor/utils.ts +158 -0
  247. package/src/client/actor-common.ts +32 -0
  248. package/src/client/actor-conn.ts +1262 -0
  249. package/src/client/actor-handle.ts +344 -0
  250. package/src/client/actor-query.ts +112 -0
  251. package/src/client/client.ts +558 -0
  252. package/src/client/config.ts +151 -0
  253. package/src/client/errors.ts +76 -0
  254. package/src/client/log.ts +5 -0
  255. package/src/client/mod.browser.ts +2 -0
  256. package/src/client/mod.ts +70 -0
  257. package/src/client/queue.ts +146 -0
  258. package/src/client/raw-utils.ts +149 -0
  259. package/src/client/test.ts +44 -0
  260. package/src/client/utils.ts +252 -0
  261. package/src/common/actor-router-consts.ts +59 -0
  262. package/src/common/cors.ts +57 -0
  263. package/src/common/eventsource-interface.ts +47 -0
  264. package/src/common/eventsource.ts +44 -0
  265. package/src/common/inline-websocket-adapter.ts +154 -0
  266. package/src/common/log-levels.ts +27 -0
  267. package/src/common/log.ts +229 -0
  268. package/src/common/logfmt.ts +221 -0
  269. package/src/common/network.ts +2 -0
  270. package/src/common/router.ts +174 -0
  271. package/src/common/utils.ts +339 -0
  272. package/src/common/websocket-interface.ts +7 -0
  273. package/src/common/websocket.ts +43 -0
  274. package/src/db/config.ts +100 -0
  275. package/src/db/drizzle/mod.ts +226 -0
  276. package/src/db/drizzle/sqlite-core.ts +22 -0
  277. package/src/db/mod.ts +125 -0
  278. package/src/db/shared.ts +92 -0
  279. package/src/db/sqlite-vfs.ts +12 -0
  280. package/src/devtools-loader/index.ts +33 -0
  281. package/src/devtools-loader/log.ts +5 -0
  282. package/src/driver-helpers/mod.ts +33 -0
  283. package/src/driver-helpers/utils.ts +54 -0
  284. package/src/driver-test-suite/log.ts +5 -0
  285. package/src/driver-test-suite/mod.ts +293 -0
  286. package/src/driver-test-suite/test-inline-client-driver.ts +307 -0
  287. package/src/driver-test-suite/tests/access-control.ts +218 -0
  288. package/src/driver-test-suite/tests/action-features.ts +203 -0
  289. package/src/driver-test-suite/tests/actor-conn-hibernation.ts +152 -0
  290. package/src/driver-test-suite/tests/actor-conn-state.ts +300 -0
  291. package/src/driver-test-suite/tests/actor-conn.ts +596 -0
  292. package/src/driver-test-suite/tests/actor-db-raw.ts +73 -0
  293. package/src/driver-test-suite/tests/actor-db.ts +477 -0
  294. package/src/driver-test-suite/tests/actor-destroy.ts +294 -0
  295. package/src/driver-test-suite/tests/actor-driver.ts +18 -0
  296. package/src/driver-test-suite/tests/actor-error-handling.ts +150 -0
  297. package/src/driver-test-suite/tests/actor-handle.ts +312 -0
  298. package/src/driver-test-suite/tests/actor-inline-client.ts +163 -0
  299. package/src/driver-test-suite/tests/actor-inspector.ts +264 -0
  300. package/src/driver-test-suite/tests/actor-kv.ts +65 -0
  301. package/src/driver-test-suite/tests/actor-metadata.ts +116 -0
  302. package/src/driver-test-suite/tests/actor-onstatechange.ts +95 -0
  303. package/src/driver-test-suite/tests/actor-queue.ts +325 -0
  304. package/src/driver-test-suite/tests/actor-run.ts +181 -0
  305. package/src/driver-test-suite/tests/actor-schedule.ts +97 -0
  306. package/src/driver-test-suite/tests/actor-sleep.ts +415 -0
  307. package/src/driver-test-suite/tests/actor-state.ts +54 -0
  308. package/src/driver-test-suite/tests/actor-stateless.ts +70 -0
  309. package/src/driver-test-suite/tests/actor-vars.ts +97 -0
  310. package/src/driver-test-suite/tests/actor-workflow.ts +118 -0
  311. package/src/driver-test-suite/tests/manager-driver.ts +388 -0
  312. package/src/driver-test-suite/tests/raw-http-direct-registry.ts +227 -0
  313. package/src/driver-test-suite/tests/raw-http-request-properties.ts +454 -0
  314. package/src/driver-test-suite/tests/raw-http.ts +359 -0
  315. package/src/driver-test-suite/tests/raw-websocket-direct-registry.ts +393 -0
  316. package/src/driver-test-suite/tests/raw-websocket.ts +513 -0
  317. package/src/driver-test-suite/tests/request-access.ts +240 -0
  318. package/src/driver-test-suite/utils.ts +80 -0
  319. package/src/drivers/default.ts +38 -0
  320. package/src/drivers/engine/actor-driver.ts +1027 -0
  321. package/src/drivers/engine/config.ts +43 -0
  322. package/src/drivers/engine/log.ts +5 -0
  323. package/src/drivers/engine/mod.ts +36 -0
  324. package/src/drivers/file-system/actor.ts +102 -0
  325. package/src/drivers/file-system/global-state.ts +1445 -0
  326. package/src/drivers/file-system/kv-limits.ts +70 -0
  327. package/src/drivers/file-system/log.ts +5 -0
  328. package/src/drivers/file-system/manager.ts +300 -0
  329. package/src/drivers/file-system/mod.ts +78 -0
  330. package/src/drivers/file-system/sqlite-runtime.ts +210 -0
  331. package/src/drivers/file-system/utils.ts +125 -0
  332. package/src/engine-process/constants.ts +2 -0
  333. package/src/engine-process/log.ts +5 -0
  334. package/src/engine-process/mod.ts +464 -0
  335. package/src/globals.d.ts +35 -0
  336. package/src/inspector/actor-inspector.ts +352 -0
  337. package/src/inspector/config.ts +49 -0
  338. package/src/inspector/handler.ts +273 -0
  339. package/src/inspector/log.ts +5 -0
  340. package/src/inspector/mod.browser.ts +8 -0
  341. package/src/inspector/mod.ts +4 -0
  342. package/src/inspector/serve-ui.ts +40 -0
  343. package/src/inspector/transport.ts +18 -0
  344. package/src/inspector/utils.ts +32 -0
  345. package/src/manager/driver.ts +106 -0
  346. package/src/manager/gateway.ts +668 -0
  347. package/src/manager/log.ts +5 -0
  348. package/src/manager/mod.ts +2 -0
  349. package/src/manager/protocol/mod.ts +22 -0
  350. package/src/manager/protocol/query.ts +85 -0
  351. package/src/manager/router-schema.ts +22 -0
  352. package/src/manager/router.ts +660 -0
  353. package/src/manager-api/actors.ts +83 -0
  354. package/src/manager-api/common.ts +4 -0
  355. package/src/mod.ts +24 -0
  356. package/src/registry/config/driver.ts +21 -0
  357. package/src/registry/config/index.ts +510 -0
  358. package/src/registry/config/legacy-runner.ts +157 -0
  359. package/src/registry/config/runner.ts +21 -0
  360. package/src/registry/config/serverless.ts +94 -0
  361. package/src/registry/index.ts +194 -0
  362. package/src/registry/log.ts +5 -0
  363. package/src/remote-manager-driver/actor-http-client.ts +84 -0
  364. package/src/remote-manager-driver/actor-websocket-client.ts +81 -0
  365. package/src/remote-manager-driver/api-endpoints.ts +159 -0
  366. package/src/remote-manager-driver/api-utils.ts +69 -0
  367. package/src/remote-manager-driver/log.ts +5 -0
  368. package/src/remote-manager-driver/metadata.ts +64 -0
  369. package/src/remote-manager-driver/mod.ts +414 -0
  370. package/src/remote-manager-driver/ws-proxy.ts +189 -0
  371. package/src/schemas/actor-inspector/mod.ts +1 -0
  372. package/src/schemas/actor-inspector/versioned.ts +233 -0
  373. package/src/schemas/actor-persist/mod.ts +1 -0
  374. package/src/schemas/actor-persist/versioned.ts +217 -0
  375. package/src/schemas/client-protocol/mod.ts +1 -0
  376. package/src/schemas/client-protocol/versioned.ts +330 -0
  377. package/src/schemas/client-protocol-zod/mod.ts +118 -0
  378. package/src/schemas/file-system-driver/mod.ts +1 -0
  379. package/src/schemas/file-system-driver/versioned.ts +135 -0
  380. package/src/schemas/persist/mod.ts +1 -0
  381. package/src/schemas/transport/mod.ts +1 -0
  382. package/src/serde.ts +138 -0
  383. package/src/serve-test-suite/mod.ts +148 -0
  384. package/src/serverless/configure.ts +82 -0
  385. package/src/serverless/log.ts +5 -0
  386. package/src/serverless/router.test.ts +299 -0
  387. package/src/serverless/router.ts +215 -0
  388. package/src/test/log.ts +5 -0
  389. package/src/test/mod.ts +99 -0
  390. package/src/utils/crypto.ts +24 -0
  391. package/src/utils/endpoint-parser.test.ts +202 -0
  392. package/src/utils/endpoint-parser.ts +124 -0
  393. package/src/utils/env-vars.ts +78 -0
  394. package/src/utils/node.ts +178 -0
  395. package/src/utils/router.ts +83 -0
  396. package/src/utils/serve.ts +212 -0
  397. package/src/utils.test.ts +34 -0
  398. package/src/utils.ts +437 -0
  399. package/src/workflow/constants.ts +2 -0
  400. package/src/workflow/context.ts +597 -0
  401. package/src/workflow/driver.ts +194 -0
  402. package/src/workflow/inspector.ts +268 -0
  403. package/src/workflow/mod.ts +128 -0
@@ -0,0 +1,380 @@
1
+ import { Hono } from "hono";
2
+ import {
3
+ type ActionOpts,
4
+ type ActionOutput,
5
+ type ConnsMessageOpts,
6
+ handleAction,
7
+ handleQueueSend,
8
+ handleRawRequest,
9
+ } from "@/actor/router-endpoints";
10
+
11
+ import {
12
+ PATH_CONNECT,
13
+ PATH_INSPECTOR_CONNECT,
14
+ PATH_WEBSOCKET_PREFIX,
15
+ } from "@/common/actor-router-consts";
16
+ import {
17
+ handleRouteError,
18
+ handleRouteNotFound,
19
+ loggerMiddleware,
20
+ } from "@/common/router";
21
+ import { noopNext } from "@/common/utils";
22
+ import { inspectorLogger } from "@/inspector/log";
23
+ import type { RegistryConfig } from "@/registry/config";
24
+ import { type GetUpgradeWebSocket, VERSION } from "@/utils";
25
+ import { timingSafeEqual } from "@/utils/crypto";
26
+ import { isDev } from "@/utils/env-vars";
27
+ import { CONN_DRIVER_SYMBOL } from "./conn/mod";
28
+ import type { ActorDriver } from "./driver";
29
+ import { loggerWithoutContext } from "./log";
30
+ import {
31
+ parseWebSocketProtocols,
32
+ routeWebSocket,
33
+ } from "./router-websocket-endpoints";
34
+
35
+ export type { ActionOpts, ActionOutput, ConnsMessageOpts };
36
+
37
+ interface ActorRouterBindings {
38
+ actorId: string;
39
+ }
40
+
41
+ export type ActorRouter = Hono<{ Bindings: ActorRouterBindings }>;
42
+
43
+ export interface MetadataResponse {
44
+ runtime: string;
45
+ version: string;
46
+ }
47
+
48
+ /**
49
+ * Creates a router that runs on the partitioned instance.
50
+ *
51
+ * You only need to pass `getUpgradeWebSocket` if this router is exposed
52
+ * directly publicly. Usually WebSockets are routed manually in the
53
+ * ManagerDriver instead of via Hono. The only platform that uses this
54
+ * currently is Cloudflare Workers.
55
+ */
56
+ export function createActorRouter(
57
+ config: RegistryConfig,
58
+ actorDriver: ActorDriver,
59
+ getUpgradeWebSocket: GetUpgradeWebSocket | undefined,
60
+ isTest: boolean,
61
+ ): ActorRouter {
62
+ const router = new Hono<{ Bindings: ActorRouterBindings }>({
63
+ strict: false,
64
+ });
65
+
66
+ router.use("*", loggerMiddleware(loggerWithoutContext()));
67
+
68
+ // Track all HTTP requests to prevent actor from sleeping during active requests
69
+ router.use("*", async (c, next) => {
70
+ const actor = await actorDriver.loadActor(c.env.actorId);
71
+ actor.beginHonoHttpRequest();
72
+ try {
73
+ await next();
74
+ } finally {
75
+ actor.endHonoHttpRequest();
76
+ }
77
+ });
78
+
79
+ router.get("/", (c) => {
80
+ return c.text(
81
+ "This is an RivetKit actor.\n\nLearn more at https://rivetkit.org",
82
+ );
83
+ });
84
+
85
+ router.get("/health", (c) => {
86
+ return c.text("ok");
87
+ });
88
+
89
+ router.get("/metadata", async (c) => {
90
+ return c.json({
91
+ runtime: "rivetkit",
92
+ version: VERSION,
93
+ } satisfies MetadataResponse);
94
+ });
95
+
96
+ if (isTest) {
97
+ // Test endpoint to force disconnect a connection non-cleanly
98
+ router.post("/.test/force-disconnect", async (c) => {
99
+ const connId = c.req.query("conn");
100
+
101
+ if (!connId) {
102
+ return c.text("Missing conn query parameter", 400);
103
+ }
104
+
105
+ const actor = await actorDriver.loadActor(c.env.actorId);
106
+ const conn = actor.connectionManager.getConnForId(connId);
107
+
108
+ if (!conn) {
109
+ return c.text(`Connection not found: ${connId}`, 404);
110
+ }
111
+
112
+ // Force close the connection without clean shutdown
113
+ if (conn[CONN_DRIVER_SYMBOL]?.terminate) {
114
+ conn[CONN_DRIVER_SYMBOL].terminate(actor, conn);
115
+ }
116
+
117
+ return c.json({ success: true });
118
+ });
119
+ }
120
+
121
+ // Route all WebSocket paths using the same handler
122
+ //
123
+ // All WebSockets use a separate underlying router in routeWebSocket since
124
+ // WebSockets also need to be routed from ManagerDriver.proxyWebSocket and
125
+ // ManagerDriver.openWebSocket.
126
+ if (getUpgradeWebSocket) {
127
+ router.on(
128
+ "GET",
129
+ [PATH_CONNECT, `${PATH_WEBSOCKET_PREFIX}*`, PATH_INSPECTOR_CONNECT],
130
+ async (c) => {
131
+ const upgradeWebSocket = getUpgradeWebSocket();
132
+ if (upgradeWebSocket) {
133
+ return upgradeWebSocket(async (c) => {
134
+ const protocols = c.req.header(
135
+ "sec-websocket-protocol",
136
+ );
137
+ const { encoding, connParams } =
138
+ parseWebSocketProtocols(protocols);
139
+
140
+ return await routeWebSocket(
141
+ c.req.raw,
142
+ c.req.path,
143
+ c.req.header(),
144
+ config,
145
+ actorDriver,
146
+ c.env.actorId,
147
+ encoding,
148
+ connParams,
149
+ undefined,
150
+ undefined,
151
+ false,
152
+ false,
153
+ );
154
+ })(c, noopNext());
155
+ } else {
156
+ return c.text(
157
+ "WebSockets are not enabled for this driver.",
158
+ 400,
159
+ );
160
+ }
161
+ },
162
+ );
163
+ }
164
+
165
+ // Inspector HTTP endpoints for agent-based debugging
166
+ if (config.inspector.enabled) {
167
+ // Auth middleware for inspector routes
168
+ const inspectorAuth = async (c: any): Promise<Response | undefined> => {
169
+ if (isDev() && !config.inspector.token()) {
170
+ inspectorLogger().warn({
171
+ msg: "RIVET_INSPECTOR_TOKEN is not set, skipping inspector auth in development mode",
172
+ });
173
+ return undefined;
174
+ }
175
+
176
+ const userToken = c.req
177
+ .header("Authorization")
178
+ ?.replace("Bearer ", "");
179
+ if (!userToken) {
180
+ return c.text("Unauthorized", 401);
181
+ }
182
+
183
+ const inspectorToken = config.inspector.token();
184
+ if (!inspectorToken) {
185
+ return c.text("Unauthorized", 401);
186
+ }
187
+
188
+ if (!timingSafeEqual(userToken, inspectorToken)) {
189
+ return c.text("Unauthorized", 401);
190
+ }
191
+
192
+ return undefined;
193
+ };
194
+
195
+ router.get("/inspector/state", async (c) => {
196
+ const authResponse = await inspectorAuth(c);
197
+ if (authResponse) return authResponse;
198
+
199
+ const actor = await actorDriver.loadActor(c.env.actorId);
200
+ const isStateEnabled = actor.inspector.isStateEnabled();
201
+ const state = isStateEnabled
202
+ ? actor.inspector.getStateJson()
203
+ : undefined;
204
+ return c.json({ state, isStateEnabled });
205
+ });
206
+
207
+ router.patch("/inspector/state", async (c) => {
208
+ const authResponse = await inspectorAuth(c);
209
+ if (authResponse) return authResponse;
210
+
211
+ const actor = await actorDriver.loadActor(c.env.actorId);
212
+ const body = await c.req.json<{ state: unknown }>();
213
+ await actor.inspector.setStateJson(body.state);
214
+ return c.json({ ok: true });
215
+ });
216
+
217
+ router.get("/inspector/connections", async (c) => {
218
+ const authResponse = await inspectorAuth(c);
219
+ if (authResponse) return authResponse;
220
+
221
+ const actor = await actorDriver.loadActor(c.env.actorId);
222
+ const connections = actor.inspector.getConnectionsJson();
223
+ return c.json({ connections });
224
+ });
225
+
226
+ router.get("/inspector/rpcs", async (c) => {
227
+ const authResponse = await inspectorAuth(c);
228
+ if (authResponse) return authResponse;
229
+
230
+ const actor = await actorDriver.loadActor(c.env.actorId);
231
+ const rpcs = actor.inspector.getRpcs();
232
+ return c.json({ rpcs });
233
+ });
234
+
235
+ router.post("/inspector/action/:name", async (c) => {
236
+ const authResponse = await inspectorAuth(c);
237
+ if (authResponse) return authResponse;
238
+
239
+ const actor = await actorDriver.loadActor(c.env.actorId);
240
+ const name = c.req.param("name");
241
+ const body = await c.req.json<{ args: unknown[] }>();
242
+ const output = await actor.inspector.executeActionJson(
243
+ name,
244
+ body.args ?? [],
245
+ );
246
+ return c.json({ output });
247
+ });
248
+
249
+ router.get("/inspector/queue", async (c) => {
250
+ const authResponse = await inspectorAuth(c);
251
+ if (authResponse) return authResponse;
252
+
253
+ const actor = await actorDriver.loadActor(c.env.actorId);
254
+ const limit = parseInt(c.req.query("limit") ?? "50", 10);
255
+ const status = await actor.inspector.getQueueStatusJson(limit);
256
+ return c.json(status);
257
+ });
258
+
259
+ router.get("/inspector/traces", async (c) => {
260
+ const authResponse = await inspectorAuth(c);
261
+ if (authResponse) return authResponse;
262
+
263
+ const actor = await actorDriver.loadActor(c.env.actorId);
264
+ const startMs = parseInt(c.req.query("startMs") ?? "0", 10);
265
+ const endMs = parseInt(
266
+ c.req.query("endMs") ?? String(Date.now()),
267
+ 10,
268
+ );
269
+ const limit = parseInt(c.req.query("limit") ?? "1000", 10);
270
+
271
+ await actor.traces.flush();
272
+ const result = await actor.inspector.getTracesJson({
273
+ startMs,
274
+ endMs,
275
+ limit,
276
+ });
277
+ return c.json(result);
278
+ });
279
+
280
+ router.get("/inspector/workflow-history", async (c) => {
281
+ const authResponse = await inspectorAuth(c);
282
+ if (authResponse) return authResponse;
283
+
284
+ const actor = await actorDriver.loadActor(c.env.actorId);
285
+ const result = actor.inspector.getWorkflowHistoryJson();
286
+ return c.json(result);
287
+ });
288
+
289
+ router.get("/inspector/summary", async (c) => {
290
+ const authResponse = await inspectorAuth(c);
291
+ if (authResponse) return authResponse;
292
+
293
+ const actor = await actorDriver.loadActor(c.env.actorId);
294
+
295
+ const isStateEnabled = actor.inspector.isStateEnabled();
296
+ const isDatabaseEnabled = actor.inspector.isDatabaseEnabled();
297
+ const isWorkflowEnabled = actor.inspector.isWorkflowEnabled();
298
+
299
+ const state = isStateEnabled
300
+ ? actor.inspector.getStateJson()
301
+ : undefined;
302
+ const connections = actor.inspector.getConnectionsJson();
303
+ const rpcs = actor.inspector.getRpcs();
304
+ const queueSize = actor.inspector.getQueueSize();
305
+ const workflowHistory = actor.inspector.getWorkflowHistory();
306
+
307
+ // Convert BigInt values in workflow history to numbers for JSON serialization.
308
+ const bigIntReplacer = (_key: string, value: unknown) =>
309
+ typeof value === "bigint" ? Number(value) : value;
310
+ const safeWorkflowHistory = workflowHistory
311
+ ? JSON.parse(JSON.stringify(workflowHistory, bigIntReplacer))
312
+ : null;
313
+
314
+ return c.json({
315
+ state,
316
+ connections,
317
+ rpcs,
318
+ queueSize,
319
+ isStateEnabled,
320
+ isDatabaseEnabled,
321
+ isWorkflowEnabled,
322
+ workflowHistory: safeWorkflowHistory,
323
+ });
324
+ });
325
+ }
326
+
327
+ router.post("/action/:action", async (c) => {
328
+ const actionName = c.req.param("action");
329
+
330
+ return handleAction(c, config, actorDriver, actionName, c.env.actorId);
331
+ });
332
+
333
+ router.post("/queue", async (c) => {
334
+ return handleQueueSend(c, config, actorDriver, c.env.actorId);
335
+ });
336
+
337
+ router.post("/queue/:name", async (c) => {
338
+ return handleQueueSend(
339
+ c,
340
+ config,
341
+ actorDriver,
342
+ c.env.actorId,
343
+ c.req.param("name"),
344
+ );
345
+ });
346
+
347
+ router.all("/request/*", async (c) => {
348
+ // TODO: This is not a clean way of doing this since `/http/` might exist mid-path
349
+ // Strip the /http prefix from the URL to get the original path
350
+ const url = new URL(c.req.url);
351
+ const originalPath = url.pathname.replace(/^\/request/, "") || "/";
352
+
353
+ // Create a new request with the corrected URL
354
+ const correctedUrl = new URL(originalPath + url.search, url.origin);
355
+ const correctedRequest = new Request(correctedUrl, {
356
+ method: c.req.method,
357
+ headers: c.req.raw.headers,
358
+ body: c.req.raw.body,
359
+ duplex: "half",
360
+ } as RequestInit);
361
+
362
+ loggerWithoutContext().debug({
363
+ msg: "rewriting http url",
364
+ from: c.req.url,
365
+ to: correctedRequest.url,
366
+ });
367
+
368
+ return await handleRawRequest(
369
+ c,
370
+ correctedRequest,
371
+ actorDriver,
372
+ c.env.actorId,
373
+ );
374
+ });
375
+
376
+ router.notFound(handleRouteNotFound);
377
+ router.onError(handleRouteError);
378
+
379
+ return router;
380
+ }
@@ -0,0 +1,17 @@
1
+ import type { AnyActorInstance } from "./instance/mod";
2
+
3
+ export class Schedule {
4
+ #actor: AnyActorInstance;
5
+
6
+ constructor(actor: AnyActorInstance) {
7
+ this.#actor = actor;
8
+ }
9
+
10
+ async after(duration: number, fn: string, ...args: unknown[]) {
11
+ await this.#actor.scheduleEvent(Date.now() + duration, fn, args);
12
+ }
13
+
14
+ async at(timestamp: number, fn: string, ...args: unknown[]) {
15
+ await this.#actor.scheduleEvent(timestamp, fn, args);
16
+ }
17
+ }
@@ -0,0 +1,291 @@
1
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
2
+ import { Unsupported } from "./errors";
3
+
4
+ export type SchemaHookResult = boolean | Promise<boolean>;
5
+
6
+ type SchemaHook<TContext = any> = (ctx: TContext) => SchemaHookResult;
7
+
8
+ export interface EventTypeToken<T, TContext = any> {
9
+ readonly _eventType?: T;
10
+ readonly canSubscribe?: SchemaHook<TContext>;
11
+ readonly schema?: PrimitiveSchema;
12
+ }
13
+
14
+ export interface QueueTypeToken<
15
+ TMessage,
16
+ TComplete = never,
17
+ TContext = any,
18
+ > {
19
+ readonly _queueMessage?: TMessage;
20
+ readonly _queueComplete?: TComplete;
21
+ readonly canPublish?: SchemaHook<TContext>;
22
+ readonly message?: PrimitiveSchema;
23
+ readonly complete?: PrimitiveSchema;
24
+ }
25
+
26
+ /** @deprecated Use `event<T>()`. */
27
+ export type Type<T> = EventTypeToken<T, any>;
28
+
29
+ interface EventOptions<TContext = any> {
30
+ canSubscribe?: SchemaHook<TContext>;
31
+ schema?: PrimitiveSchema;
32
+ }
33
+
34
+ interface QueueOptions<TContext = any> {
35
+ canPublish?: SchemaHook<TContext>;
36
+ message?: PrimitiveSchema;
37
+ complete?: PrimitiveSchema;
38
+ }
39
+
40
+ export function event<T, TContext = any>(
41
+ options?: EventOptions<TContext>,
42
+ ): EventTypeToken<T, TContext> {
43
+ return (options ?? {}) as EventTypeToken<T, TContext>;
44
+ }
45
+
46
+ export function queue<TMessage, TComplete = never, TContext = any>(
47
+ options?: QueueOptions<TContext>,
48
+ ): QueueTypeToken<
49
+ TMessage,
50
+ TComplete,
51
+ TContext
52
+ > {
53
+ return (options ?? {}) as QueueTypeToken<TMessage, TComplete, TContext>;
54
+ }
55
+
56
+ export type PrimitiveSchema = StandardSchemaV1 | EventTypeToken<unknown, any>;
57
+
58
+ export interface EventSchemaDefinition<TContext = any> {
59
+ schema: PrimitiveSchema;
60
+ canSubscribe?: SchemaHook<TContext>;
61
+ }
62
+
63
+ export interface QueueSchemaDefinition<TContext = any> {
64
+ message: PrimitiveSchema;
65
+ complete?: PrimitiveSchema;
66
+ canPublish?: SchemaHook<TContext>;
67
+ }
68
+
69
+ export type EventSchema<TContext = any> =
70
+ | PrimitiveSchema
71
+ | EventSchemaDefinition<TContext>;
72
+ export type QueueSchema =
73
+ | PrimitiveSchema
74
+ | QueueSchemaDefinition<any>
75
+ | QueueTypeToken<unknown, unknown, any>;
76
+ export type EventSchemaConfig<TContext = any> = Record<
77
+ string,
78
+ EventSchema<TContext>
79
+ >;
80
+ export type QueueSchemaConfig<TContext = any> = Record<string, QueueSchema>;
81
+ export type AnySchemaConfig = EventSchemaConfig | QueueSchemaConfig;
82
+
83
+ /** @deprecated Use `EventSchema` or `QueueSchema`. */
84
+ export type Schema = QueueSchema;
85
+ /** @deprecated Use `EventSchemaConfig` or `QueueSchemaConfig`. */
86
+ export type SchemaConfig = QueueSchemaConfig;
87
+
88
+ export type InferSchema<T> =
89
+ T extends QueueSchemaDefinition<any>
90
+ ? InferSchema<T["message"]>
91
+ : T extends QueueTypeToken<infer M, unknown, any>
92
+ ? M
93
+ : T extends EventSchemaDefinition<any>
94
+ ? InferSchema<T["schema"]>
95
+ : T extends StandardSchemaV1<any, infer O>
96
+ ? O
97
+ : T extends EventTypeToken<infer R, any>
98
+ ? R
99
+ : never;
100
+
101
+ export type InferSchemaMap<T extends Record<string, unknown>> = {
102
+ [K in keyof T]: InferSchema<T[K]>;
103
+ };
104
+
105
+ export type InferQueueComplete<T> =
106
+ T extends QueueTypeToken<unknown, infer C, any>
107
+ ? [C] extends [never]
108
+ ? never
109
+ : C
110
+ : T extends QueueSchemaDefinition<any>
111
+ ? T["complete"] extends PrimitiveSchema
112
+ ? InferSchema<T["complete"]>
113
+ : never
114
+ : never;
115
+
116
+ export type InferQueueCompleteMap<T extends QueueSchemaConfig> = {
117
+ [K in keyof T]: InferQueueComplete<T[K]>;
118
+ };
119
+
120
+ export type InferEventArgs<T> = T extends readonly unknown[]
121
+ ? number extends T["length"]
122
+ ? [T]
123
+ : T
124
+ : [T];
125
+
126
+ export type ValidationResult<T> =
127
+ | { success: true; data: T }
128
+ | { success: false; issues: unknown[] };
129
+
130
+ export function isStandardSchema(value: unknown): value is StandardSchemaV1 {
131
+ return typeof value === "object" && value !== null && "~standard" in value;
132
+ }
133
+
134
+ export function isQueueSchemaDefinition(
135
+ value: unknown,
136
+ ): value is QueueSchemaDefinition<any> {
137
+ if (isEventSchemaDefinition(value)) {
138
+ return false;
139
+ }
140
+ return (
141
+ typeof value === "object" &&
142
+ value !== null &&
143
+ "message" in value &&
144
+ (value as { message?: unknown }).message !== undefined
145
+ );
146
+ }
147
+
148
+ export function isEventSchemaDefinition(
149
+ value: unknown,
150
+ ): value is EventSchemaDefinition<any> {
151
+ return (
152
+ typeof value === "object" &&
153
+ value !== null &&
154
+ "schema" in value &&
155
+ (value as { schema?: unknown }).schema !== undefined
156
+ );
157
+ }
158
+
159
+ export function hasSchemaConfigKey<T extends AnySchemaConfig>(
160
+ schemas: T | undefined,
161
+ key: string,
162
+ ): boolean {
163
+ if (!schemas) {
164
+ return false;
165
+ }
166
+ return Object.prototype.hasOwnProperty.call(schemas, key);
167
+ }
168
+
169
+ export function getEventCanSubscribe<TContext = any>(
170
+ schemas: EventSchemaConfig<TContext> | undefined,
171
+ key: string,
172
+ ): SchemaHook<TContext> | undefined {
173
+ const schema = schemas?.[key];
174
+ if (!schema || isStandardSchema(schema)) {
175
+ return undefined;
176
+ }
177
+
178
+ const maybeCanSubscribe = (schema as { canSubscribe?: unknown })
179
+ .canSubscribe;
180
+ return typeof maybeCanSubscribe === "function"
181
+ ? (maybeCanSubscribe as SchemaHook<TContext>)
182
+ : undefined;
183
+ }
184
+
185
+ export function getQueueCanPublish<TContext = any>(
186
+ schemas: QueueSchemaConfig | undefined,
187
+ key: string,
188
+ ): SchemaHook<TContext> | undefined {
189
+ const schema = schemas?.[key];
190
+ if (!schema || isStandardSchema(schema)) {
191
+ return undefined;
192
+ }
193
+
194
+ const maybeCanPublish = (schema as { canPublish?: unknown }).canPublish;
195
+ return typeof maybeCanPublish === "function"
196
+ ? (maybeCanPublish as SchemaHook<TContext>)
197
+ : undefined;
198
+ }
199
+
200
+ function getValidationSchema(
201
+ schema: QueueSchema | EventSchema | undefined,
202
+ ): QueueSchema | EventSchema | undefined {
203
+ if (!schema) {
204
+ return undefined;
205
+ }
206
+ if (isEventSchemaDefinition(schema)) {
207
+ return schema.schema;
208
+ }
209
+ if (isQueueSchemaDefinition(schema)) {
210
+ return schema.message;
211
+ }
212
+ if (
213
+ typeof schema === "object" &&
214
+ schema !== null &&
215
+ "schema" in schema &&
216
+ (schema as { schema?: unknown }).schema !== undefined
217
+ ) {
218
+ return (schema as { schema: QueueSchema | EventSchema }).schema;
219
+ }
220
+ if (
221
+ typeof schema === "object" &&
222
+ schema !== null &&
223
+ "message" in schema &&
224
+ (schema as { message?: unknown }).message !== undefined
225
+ ) {
226
+ return (schema as { message: QueueSchema | EventSchema }).message;
227
+ }
228
+ return schema;
229
+ }
230
+
231
+ function isPromiseLike(value: unknown): value is PromiseLike<unknown> {
232
+ return (
233
+ typeof value === "object" &&
234
+ value !== null &&
235
+ "then" in value &&
236
+ typeof (value as { then?: unknown }).then === "function"
237
+ );
238
+ }
239
+
240
+ export async function validateSchema<T extends AnySchemaConfig>(
241
+ schemas: T | undefined,
242
+ key: keyof T & string,
243
+ data: unknown,
244
+ ): Promise<ValidationResult<InferSchemaMap<T>[typeof key]>> {
245
+ const schema = getValidationSchema(schemas?.[key]);
246
+
247
+ if (!schema) {
248
+ return { success: true, data: data as InferSchemaMap<T>[typeof key] };
249
+ }
250
+
251
+ if (isStandardSchema(schema)) {
252
+ const result = await schema["~standard"].validate(data);
253
+ if (result.issues) {
254
+ return { success: false, issues: [...result.issues] };
255
+ }
256
+ return {
257
+ success: true,
258
+ data: result.value as InferSchemaMap<T>[typeof key],
259
+ };
260
+ }
261
+
262
+ return { success: true, data: data as InferSchemaMap<T>[typeof key] };
263
+ }
264
+
265
+ export function validateSchemaSync<T extends AnySchemaConfig>(
266
+ schemas: T | undefined,
267
+ key: keyof T & string,
268
+ data: unknown,
269
+ ): ValidationResult<InferSchemaMap<T>[typeof key]> {
270
+ const schema = getValidationSchema(schemas?.[key]);
271
+
272
+ if (!schema) {
273
+ return { success: true, data: data as InferSchemaMap<T>[typeof key] };
274
+ }
275
+
276
+ if (isStandardSchema(schema)) {
277
+ const result = schema["~standard"].validate(data);
278
+ if (isPromiseLike(result)) {
279
+ throw new Unsupported("async schema validation");
280
+ }
281
+ if (result.issues) {
282
+ return { success: false, issues: [...result.issues] };
283
+ }
284
+ return {
285
+ success: true,
286
+ data: result.value as InferSchemaMap<T>[typeof key],
287
+ };
288
+ }
289
+
290
+ return { success: true, data: data as InferSchemaMap<T>[typeof key] };
291
+ }