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.
- package/README.md +3 -5
- package/dist/browser/client.d.ts +2485 -0
- package/dist/browser/client.js +5182 -0
- package/dist/browser/client.js.map +1 -0
- package/dist/browser/inspector/client.d.ts +130 -0
- package/dist/browser/inspector/client.js +2854 -0
- package/dist/browser/inspector/client.js.map +1 -0
- package/dist/browser/v3-DnYObHH3.d.ts +279 -0
- package/dist/inspector.tar.gz +0 -0
- package/dist/schemas/actor-inspector/v1.ts +784 -0
- package/dist/schemas/actor-inspector/v2.ts +796 -0
- package/dist/schemas/actor-inspector/v3.ts +899 -0
- package/dist/schemas/actor-persist/v1.ts +225 -0
- package/dist/schemas/actor-persist/v2.ts +268 -0
- package/dist/schemas/actor-persist/v3.ts +280 -0
- package/dist/schemas/actor-persist/v4.ts +406 -0
- package/dist/schemas/client-protocol/v1.ts +441 -0
- package/dist/schemas/client-protocol/v2.ts +438 -0
- package/dist/schemas/client-protocol/v3.ts +554 -0
- package/dist/schemas/file-system-driver/v1.ts +108 -0
- package/dist/schemas/file-system-driver/v2.ts +142 -0
- package/dist/schemas/file-system-driver/v3.ts +167 -0
- package/dist/schemas/persist/v1.ts +781 -0
- package/dist/schemas/transport/v1.ts +697 -0
- package/dist/tsup/actor/errors.cjs +106 -0
- package/dist/tsup/actor/errors.cjs.map +1 -0
- package/dist/tsup/actor/errors.d.cts +188 -0
- package/dist/tsup/actor/errors.d.ts +188 -0
- package/dist/tsup/actor/errors.js +106 -0
- package/dist/tsup/actor/errors.js.map +1 -0
- package/dist/tsup/actor-router-consts-D29T1Z-K.d.cts +24 -0
- package/dist/tsup/actor-router-consts-D29T1Z-K.d.ts +24 -0
- package/dist/tsup/chunk-325TLXJT.js +1060 -0
- package/dist/tsup/chunk-325TLXJT.js.map +1 -0
- package/dist/tsup/chunk-424PT5DM.js +23 -0
- package/dist/tsup/chunk-424PT5DM.js.map +1 -0
- package/dist/tsup/chunk-4JVIG3SS.cjs +6289 -0
- package/dist/tsup/chunk-4JVIG3SS.cjs.map +1 -0
- package/dist/tsup/chunk-6LJAZ5R4.cjs +96 -0
- package/dist/tsup/chunk-6LJAZ5R4.cjs.map +1 -0
- package/dist/tsup/chunk-6XU3FMCB.cjs +534 -0
- package/dist/tsup/chunk-6XU3FMCB.cjs.map +1 -0
- package/dist/tsup/chunk-7HTNH26M.js +509 -0
- package/dist/tsup/chunk-7HTNH26M.js.map +1 -0
- package/dist/tsup/chunk-AUVH72RE.cjs +5977 -0
- package/dist/tsup/chunk-AUVH72RE.cjs.map +1 -0
- package/dist/tsup/chunk-D4BYUPNQ.js +645 -0
- package/dist/tsup/chunk-D4BYUPNQ.js.map +1 -0
- package/dist/tsup/chunk-HDQ2JUQT.cjs +23 -0
- package/dist/tsup/chunk-HDQ2JUQT.cjs.map +1 -0
- package/dist/tsup/chunk-HHXX2VRM.js +6289 -0
- package/dist/tsup/chunk-HHXX2VRM.js.map +1 -0
- package/dist/tsup/chunk-JEAEA2PB.js +49 -0
- package/dist/tsup/chunk-JEAEA2PB.js.map +1 -0
- package/dist/tsup/chunk-JYSEG3VF.cjs +642 -0
- package/dist/tsup/chunk-JYSEG3VF.cjs.map +1 -0
- package/dist/tsup/chunk-K6DGYILQ.js +2657 -0
- package/dist/tsup/chunk-K6DGYILQ.js.map +1 -0
- package/dist/tsup/chunk-KJSYAUOM.js +96 -0
- package/dist/tsup/chunk-KJSYAUOM.js.map +1 -0
- package/dist/tsup/chunk-L47L3ZWJ.cjs +509 -0
- package/dist/tsup/chunk-L47L3ZWJ.cjs.map +1 -0
- package/dist/tsup/chunk-LXUQ667X.js +2006 -0
- package/dist/tsup/chunk-LXUQ667X.js.map +1 -0
- package/dist/tsup/chunk-MXNPAB5W.js +5977 -0
- package/dist/tsup/chunk-MXNPAB5W.js.map +1 -0
- package/dist/tsup/chunk-N4KRDJ56.js +72 -0
- package/dist/tsup/chunk-N4KRDJ56.js.map +1 -0
- package/dist/tsup/chunk-NIYZDWMW.cjs +2006 -0
- package/dist/tsup/chunk-NIYZDWMW.cjs.map +1 -0
- package/dist/tsup/chunk-PQZHDKRW.cjs +1060 -0
- package/dist/tsup/chunk-PQZHDKRW.cjs.map +1 -0
- package/dist/tsup/chunk-PVOE6BU7.cjs +1050 -0
- package/dist/tsup/chunk-PVOE6BU7.cjs.map +1 -0
- package/dist/tsup/chunk-Q4UD2GA4.cjs +1810 -0
- package/dist/tsup/chunk-Q4UD2GA4.cjs.map +1 -0
- package/dist/tsup/chunk-QUD664YZ.js +1810 -0
- package/dist/tsup/chunk-QUD664YZ.js.map +1 -0
- package/dist/tsup/chunk-RTOCTWME.js +1050 -0
- package/dist/tsup/chunk-RTOCTWME.js.map +1 -0
- package/dist/tsup/chunk-SAZZ4SB2.cjs +2657 -0
- package/dist/tsup/chunk-SAZZ4SB2.cjs.map +1 -0
- package/dist/tsup/chunk-SR3KQE7Q.cjs +72 -0
- package/dist/tsup/chunk-SR3KQE7Q.cjs.map +1 -0
- package/dist/tsup/chunk-V2GHLYC6.cjs +49 -0
- package/dist/tsup/chunk-V2GHLYC6.cjs.map +1 -0
- package/dist/tsup/chunk-V3WG7XTW.cjs +645 -0
- package/dist/tsup/chunk-V3WG7XTW.cjs.map +1 -0
- package/dist/tsup/chunk-VKVNIQRQ.js +257 -0
- package/dist/tsup/chunk-VKVNIQRQ.js.map +1 -0
- package/dist/tsup/chunk-WMPW7JYC.js +642 -0
- package/dist/tsup/chunk-WMPW7JYC.js.map +1 -0
- package/dist/tsup/chunk-Z7HNQ2WF.js +534 -0
- package/dist/tsup/chunk-Z7HNQ2WF.js.map +1 -0
- package/dist/tsup/chunk-ZFY5J2EP.cjs +257 -0
- package/dist/tsup/chunk-ZFY5J2EP.cjs.map +1 -0
- package/dist/tsup/client/mod.cjs +33 -0
- package/dist/tsup/client/mod.cjs.map +1 -0
- package/dist/tsup/client/mod.d.cts +64 -0
- package/dist/tsup/client/mod.d.ts +64 -0
- package/dist/tsup/client/mod.js +33 -0
- package/dist/tsup/client/mod.js.map +1 -0
- package/dist/tsup/common/log.cjs +21 -0
- package/dist/tsup/common/log.cjs.map +1 -0
- package/dist/tsup/common/log.d.cts +34 -0
- package/dist/tsup/common/log.d.ts +34 -0
- package/dist/tsup/common/log.js +21 -0
- package/dist/tsup/common/log.js.map +1 -0
- package/dist/tsup/common/websocket.cjs +10 -0
- package/dist/tsup/common/websocket.cjs.map +1 -0
- package/dist/tsup/common/websocket.d.cts +3 -0
- package/dist/tsup/common/websocket.d.ts +3 -0
- package/dist/tsup/common/websocket.js +10 -0
- package/dist/tsup/common/websocket.js.map +1 -0
- package/dist/tsup/config-BiNoIHRs.d.cts +80 -0
- package/dist/tsup/config-BiNoIHRs.d.ts +80 -0
- package/dist/tsup/config-P3XujgRr.d.ts +2594 -0
- package/dist/tsup/config-_gfywqqI.d.cts +2594 -0
- package/dist/tsup/context-Bxd8Cx4H.d.cts +75 -0
- package/dist/tsup/context-uNA4TRn3.d.ts +75 -0
- package/dist/tsup/db/drizzle/mod.cjs +49 -0
- package/dist/tsup/db/drizzle/mod.cjs.map +1 -0
- package/dist/tsup/db/drizzle/mod.d.cts +17 -0
- package/dist/tsup/db/drizzle/mod.d.ts +17 -0
- package/dist/tsup/db/drizzle/mod.js +49 -0
- package/dist/tsup/db/drizzle/mod.js.map +1 -0
- package/dist/tsup/db/mod.cjs +9 -0
- package/dist/tsup/db/mod.cjs.map +1 -0
- package/dist/tsup/db/mod.d.cts +9 -0
- package/dist/tsup/db/mod.d.ts +9 -0
- package/dist/tsup/db/mod.js +9 -0
- package/dist/tsup/db/mod.js.map +1 -0
- package/dist/tsup/driver-BcLvZcKl.d.cts +13 -0
- package/dist/tsup/driver-CPGHKXyh.d.ts +13 -0
- package/dist/tsup/driver-helpers/mod.cjs +53 -0
- package/dist/tsup/driver-helpers/mod.cjs.map +1 -0
- package/dist/tsup/driver-helpers/mod.d.cts +47 -0
- package/dist/tsup/driver-helpers/mod.d.ts +47 -0
- package/dist/tsup/driver-helpers/mod.js +53 -0
- package/dist/tsup/driver-helpers/mod.js.map +1 -0
- package/dist/tsup/driver-test-suite/mod.cjs +4974 -0
- package/dist/tsup/driver-test-suite/mod.cjs.map +1 -0
- package/dist/tsup/driver-test-suite/mod.d.cts +73 -0
- package/dist/tsup/driver-test-suite/mod.d.ts +73 -0
- package/dist/tsup/driver-test-suite/mod.js +4974 -0
- package/dist/tsup/driver-test-suite/mod.js.map +1 -0
- package/dist/tsup/inspector/mod.cjs +164 -0
- package/dist/tsup/inspector/mod.cjs.map +1 -0
- package/dist/tsup/inspector/mod.d.cts +130 -0
- package/dist/tsup/inspector/mod.d.ts +130 -0
- package/dist/tsup/inspector/mod.js +164 -0
- package/dist/tsup/inspector/mod.js.map +1 -0
- package/dist/tsup/keys-CydblqMh.d.cts +13 -0
- package/dist/tsup/keys-CydblqMh.d.ts +13 -0
- package/dist/tsup/mod.cjs +82 -0
- package/dist/tsup/mod.cjs.map +1 -0
- package/dist/tsup/mod.d.cts +126 -0
- package/dist/tsup/mod.d.ts +126 -0
- package/dist/tsup/mod.js +82 -0
- package/dist/tsup/mod.js.map +1 -0
- package/dist/tsup/serve-test-suite/mod.cjs +2601 -0
- package/dist/tsup/serve-test-suite/mod.cjs.map +1 -0
- package/dist/tsup/serve-test-suite/mod.d.cts +9 -0
- package/dist/tsup/serve-test-suite/mod.d.ts +9 -0
- package/dist/tsup/serve-test-suite/mod.js +2601 -0
- package/dist/tsup/serve-test-suite/mod.js.map +1 -0
- package/dist/tsup/test/mod.cjs +90 -0
- package/dist/tsup/test/mod.cjs.map +1 -0
- package/dist/tsup/test/mod.d.cts +26 -0
- package/dist/tsup/test/mod.d.ts +26 -0
- package/dist/tsup/test/mod.js +90 -0
- package/dist/tsup/test/mod.js.map +1 -0
- package/dist/tsup/utils-fwx3o3K9.d.cts +18 -0
- package/dist/tsup/utils-fwx3o3K9.d.ts +18 -0
- package/dist/tsup/utils.cjs +43 -0
- package/dist/tsup/utils.cjs.map +1 -0
- package/dist/tsup/utils.d.cts +148 -0
- package/dist/tsup/utils.d.ts +148 -0
- package/dist/tsup/utils.js +43 -0
- package/dist/tsup/utils.js.map +1 -0
- package/dist/tsup/v3-DnYObHH3.d.cts +279 -0
- package/dist/tsup/v3-DnYObHH3.d.ts +279 -0
- package/dist/tsup/workflow/mod.cjs +16 -0
- package/dist/tsup/workflow/mod.cjs.map +1 -0
- package/dist/tsup/workflow/mod.d.cts +25 -0
- package/dist/tsup/workflow/mod.d.ts +25 -0
- package/dist/tsup/workflow/mod.js +16 -0
- package/dist/tsup/workflow/mod.js.map +1 -0
- package/package.json +293 -5
- package/src/actor/config.ts +1221 -0
- package/src/actor/conn/driver.ts +61 -0
- package/src/actor/conn/drivers/http.ts +17 -0
- package/src/actor/conn/drivers/raw-request.ts +24 -0
- package/src/actor/conn/drivers/raw-websocket.ts +65 -0
- package/src/actor/conn/drivers/websocket.ts +144 -0
- package/src/actor/conn/mod.ts +288 -0
- package/src/actor/conn/persisted.ts +81 -0
- package/src/actor/conn/state-manager.ts +196 -0
- package/src/actor/contexts/action.ts +47 -0
- package/src/actor/contexts/base/actor.ts +347 -0
- package/src/actor/contexts/base/conn-init.ts +68 -0
- package/src/actor/contexts/base/conn.ts +73 -0
- package/src/actor/contexts/before-action-response.ts +42 -0
- package/src/actor/contexts/before-connect.ts +31 -0
- package/src/actor/contexts/connect.ts +42 -0
- package/src/actor/contexts/create-conn-state.ts +32 -0
- package/src/actor/contexts/create-vars.ts +39 -0
- package/src/actor/contexts/create.ts +39 -0
- package/src/actor/contexts/destroy.ts +42 -0
- package/src/actor/contexts/disconnect.ts +43 -0
- package/src/actor/contexts/index.ts +33 -0
- package/src/actor/contexts/request.ts +80 -0
- package/src/actor/contexts/run.ts +47 -0
- package/src/actor/contexts/sleep.ts +42 -0
- package/src/actor/contexts/state-change.ts +42 -0
- package/src/actor/contexts/wake.ts +42 -0
- package/src/actor/contexts/websocket.ts +80 -0
- package/src/actor/database.ts +13 -0
- package/src/actor/definition.ts +64 -0
- package/src/actor/driver.ts +114 -0
- package/src/actor/errors.ts +556 -0
- package/src/actor/instance/connection-manager.ts +574 -0
- package/src/actor/instance/event-manager.ts +314 -0
- package/src/actor/instance/keys.ts +146 -0
- package/src/actor/instance/kv.ts +241 -0
- package/src/actor/instance/mod.ts +1658 -0
- package/src/actor/instance/persisted.ts +67 -0
- package/src/actor/instance/queue-manager.ts +603 -0
- package/src/actor/instance/queue.ts +345 -0
- package/src/actor/instance/schedule-manager.ts +392 -0
- package/src/actor/instance/state-manager.ts +542 -0
- package/src/actor/instance/traces-driver.ts +128 -0
- package/src/actor/keys.test.ts +275 -0
- package/src/actor/keys.ts +89 -0
- package/src/actor/log.ts +6 -0
- package/src/actor/mod.ts +110 -0
- package/src/actor/protocol/old.ts +416 -0
- package/src/actor/protocol/serde.ts +222 -0
- package/src/actor/router-endpoints.ts +400 -0
- package/src/actor/router-websocket-endpoints.test.ts +54 -0
- package/src/actor/router-websocket-endpoints.ts +405 -0
- package/src/actor/router.ts +380 -0
- package/src/actor/schedule.ts +17 -0
- package/src/actor/schema.ts +291 -0
- package/src/actor/utils.test.ts +48 -0
- package/src/actor/utils.ts +158 -0
- package/src/client/actor-common.ts +32 -0
- package/src/client/actor-conn.ts +1262 -0
- package/src/client/actor-handle.ts +344 -0
- package/src/client/actor-query.ts +112 -0
- package/src/client/client.ts +558 -0
- package/src/client/config.ts +151 -0
- package/src/client/errors.ts +76 -0
- package/src/client/log.ts +5 -0
- package/src/client/mod.browser.ts +2 -0
- package/src/client/mod.ts +70 -0
- package/src/client/queue.ts +146 -0
- package/src/client/raw-utils.ts +149 -0
- package/src/client/test.ts +44 -0
- package/src/client/utils.ts +252 -0
- package/src/common/actor-router-consts.ts +59 -0
- package/src/common/cors.ts +57 -0
- package/src/common/eventsource-interface.ts +47 -0
- package/src/common/eventsource.ts +44 -0
- package/src/common/inline-websocket-adapter.ts +154 -0
- package/src/common/log-levels.ts +27 -0
- package/src/common/log.ts +229 -0
- package/src/common/logfmt.ts +221 -0
- package/src/common/network.ts +2 -0
- package/src/common/router.ts +174 -0
- package/src/common/utils.ts +339 -0
- package/src/common/websocket-interface.ts +7 -0
- package/src/common/websocket.ts +43 -0
- package/src/db/config.ts +100 -0
- package/src/db/drizzle/mod.ts +226 -0
- package/src/db/drizzle/sqlite-core.ts +22 -0
- package/src/db/mod.ts +125 -0
- package/src/db/shared.ts +92 -0
- package/src/db/sqlite-vfs.ts +12 -0
- package/src/devtools-loader/index.ts +33 -0
- package/src/devtools-loader/log.ts +5 -0
- package/src/driver-helpers/mod.ts +33 -0
- package/src/driver-helpers/utils.ts +54 -0
- package/src/driver-test-suite/log.ts +5 -0
- package/src/driver-test-suite/mod.ts +293 -0
- package/src/driver-test-suite/test-inline-client-driver.ts +307 -0
- package/src/driver-test-suite/tests/access-control.ts +218 -0
- package/src/driver-test-suite/tests/action-features.ts +203 -0
- package/src/driver-test-suite/tests/actor-conn-hibernation.ts +152 -0
- package/src/driver-test-suite/tests/actor-conn-state.ts +300 -0
- package/src/driver-test-suite/tests/actor-conn.ts +596 -0
- package/src/driver-test-suite/tests/actor-db-raw.ts +73 -0
- package/src/driver-test-suite/tests/actor-db.ts +477 -0
- package/src/driver-test-suite/tests/actor-destroy.ts +294 -0
- package/src/driver-test-suite/tests/actor-driver.ts +18 -0
- package/src/driver-test-suite/tests/actor-error-handling.ts +150 -0
- package/src/driver-test-suite/tests/actor-handle.ts +312 -0
- package/src/driver-test-suite/tests/actor-inline-client.ts +163 -0
- package/src/driver-test-suite/tests/actor-inspector.ts +264 -0
- package/src/driver-test-suite/tests/actor-kv.ts +65 -0
- package/src/driver-test-suite/tests/actor-metadata.ts +116 -0
- package/src/driver-test-suite/tests/actor-onstatechange.ts +95 -0
- package/src/driver-test-suite/tests/actor-queue.ts +325 -0
- package/src/driver-test-suite/tests/actor-run.ts +181 -0
- package/src/driver-test-suite/tests/actor-schedule.ts +97 -0
- package/src/driver-test-suite/tests/actor-sleep.ts +415 -0
- package/src/driver-test-suite/tests/actor-state.ts +54 -0
- package/src/driver-test-suite/tests/actor-stateless.ts +70 -0
- package/src/driver-test-suite/tests/actor-vars.ts +97 -0
- package/src/driver-test-suite/tests/actor-workflow.ts +118 -0
- package/src/driver-test-suite/tests/manager-driver.ts +388 -0
- package/src/driver-test-suite/tests/raw-http-direct-registry.ts +227 -0
- package/src/driver-test-suite/tests/raw-http-request-properties.ts +454 -0
- package/src/driver-test-suite/tests/raw-http.ts +359 -0
- package/src/driver-test-suite/tests/raw-websocket-direct-registry.ts +393 -0
- package/src/driver-test-suite/tests/raw-websocket.ts +513 -0
- package/src/driver-test-suite/tests/request-access.ts +240 -0
- package/src/driver-test-suite/utils.ts +80 -0
- package/src/drivers/default.ts +38 -0
- package/src/drivers/engine/actor-driver.ts +1027 -0
- package/src/drivers/engine/config.ts +43 -0
- package/src/drivers/engine/log.ts +5 -0
- package/src/drivers/engine/mod.ts +36 -0
- package/src/drivers/file-system/actor.ts +102 -0
- package/src/drivers/file-system/global-state.ts +1445 -0
- package/src/drivers/file-system/kv-limits.ts +70 -0
- package/src/drivers/file-system/log.ts +5 -0
- package/src/drivers/file-system/manager.ts +300 -0
- package/src/drivers/file-system/mod.ts +78 -0
- package/src/drivers/file-system/sqlite-runtime.ts +210 -0
- package/src/drivers/file-system/utils.ts +125 -0
- package/src/engine-process/constants.ts +2 -0
- package/src/engine-process/log.ts +5 -0
- package/src/engine-process/mod.ts +464 -0
- package/src/globals.d.ts +35 -0
- package/src/inspector/actor-inspector.ts +352 -0
- package/src/inspector/config.ts +49 -0
- package/src/inspector/handler.ts +273 -0
- package/src/inspector/log.ts +5 -0
- package/src/inspector/mod.browser.ts +8 -0
- package/src/inspector/mod.ts +4 -0
- package/src/inspector/serve-ui.ts +40 -0
- package/src/inspector/transport.ts +18 -0
- package/src/inspector/utils.ts +32 -0
- package/src/manager/driver.ts +106 -0
- package/src/manager/gateway.ts +668 -0
- package/src/manager/log.ts +5 -0
- package/src/manager/mod.ts +2 -0
- package/src/manager/protocol/mod.ts +22 -0
- package/src/manager/protocol/query.ts +85 -0
- package/src/manager/router-schema.ts +22 -0
- package/src/manager/router.ts +660 -0
- package/src/manager-api/actors.ts +83 -0
- package/src/manager-api/common.ts +4 -0
- package/src/mod.ts +24 -0
- package/src/registry/config/driver.ts +21 -0
- package/src/registry/config/index.ts +510 -0
- package/src/registry/config/legacy-runner.ts +157 -0
- package/src/registry/config/runner.ts +21 -0
- package/src/registry/config/serverless.ts +94 -0
- package/src/registry/index.ts +194 -0
- package/src/registry/log.ts +5 -0
- package/src/remote-manager-driver/actor-http-client.ts +84 -0
- package/src/remote-manager-driver/actor-websocket-client.ts +81 -0
- package/src/remote-manager-driver/api-endpoints.ts +159 -0
- package/src/remote-manager-driver/api-utils.ts +69 -0
- package/src/remote-manager-driver/log.ts +5 -0
- package/src/remote-manager-driver/metadata.ts +64 -0
- package/src/remote-manager-driver/mod.ts +414 -0
- package/src/remote-manager-driver/ws-proxy.ts +189 -0
- package/src/schemas/actor-inspector/mod.ts +1 -0
- package/src/schemas/actor-inspector/versioned.ts +233 -0
- package/src/schemas/actor-persist/mod.ts +1 -0
- package/src/schemas/actor-persist/versioned.ts +217 -0
- package/src/schemas/client-protocol/mod.ts +1 -0
- package/src/schemas/client-protocol/versioned.ts +330 -0
- package/src/schemas/client-protocol-zod/mod.ts +118 -0
- package/src/schemas/file-system-driver/mod.ts +1 -0
- package/src/schemas/file-system-driver/versioned.ts +135 -0
- package/src/schemas/persist/mod.ts +1 -0
- package/src/schemas/transport/mod.ts +1 -0
- package/src/serde.ts +138 -0
- package/src/serve-test-suite/mod.ts +148 -0
- package/src/serverless/configure.ts +82 -0
- package/src/serverless/log.ts +5 -0
- package/src/serverless/router.test.ts +299 -0
- package/src/serverless/router.ts +215 -0
- package/src/test/log.ts +5 -0
- package/src/test/mod.ts +99 -0
- package/src/utils/crypto.ts +24 -0
- package/src/utils/endpoint-parser.test.ts +202 -0
- package/src/utils/endpoint-parser.ts +124 -0
- package/src/utils/env-vars.ts +78 -0
- package/src/utils/node.ts +178 -0
- package/src/utils/router.ts +83 -0
- package/src/utils/serve.ts +212 -0
- package/src/utils.test.ts +34 -0
- package/src/utils.ts +437 -0
- package/src/workflow/constants.ts +2 -0
- package/src/workflow/context.ts +597 -0
- package/src/workflow/driver.ts +194 -0
- package/src/workflow/inspector.ts +268 -0
- package/src/workflow/mod.ts +128 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import * as cbor from "cbor-x";
|
|
2
|
+
import type * as protocol from "@/schemas/client-protocol/mod";
|
|
3
|
+
import {
|
|
4
|
+
CURRENT_VERSION as CLIENT_PROTOCOL_CURRENT_VERSION,
|
|
5
|
+
TO_CLIENT_VERSIONED,
|
|
6
|
+
} from "@/schemas/client-protocol/versioned";
|
|
7
|
+
import {
|
|
8
|
+
type ToClient as ToClientJson,
|
|
9
|
+
ToClientSchema,
|
|
10
|
+
} from "@/schemas/client-protocol-zod/mod";
|
|
11
|
+
import { bufferToArrayBuffer } from "@/utils";
|
|
12
|
+
import {
|
|
13
|
+
CONN_SEND_MESSAGE_SYMBOL,
|
|
14
|
+
CONN_SPEAKS_RIVETKIT_SYMBOL,
|
|
15
|
+
CONN_STATE_MANAGER_SYMBOL,
|
|
16
|
+
type Conn,
|
|
17
|
+
} from "../conn/mod";
|
|
18
|
+
import type { AnyDatabaseProvider } from "../database";
|
|
19
|
+
import * as errors from "../errors";
|
|
20
|
+
import { CachedSerializer } from "../protocol/serde";
|
|
21
|
+
import type { EventSchemaConfig, QueueSchemaConfig } from "../schema";
|
|
22
|
+
import type { ActorInstance } from "./mod";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Manages event subscriptions and broadcasting for actor instances.
|
|
26
|
+
* Handles subscription tracking and efficient message distribution to connected clients.
|
|
27
|
+
*/
|
|
28
|
+
export class EventManager<
|
|
29
|
+
S,
|
|
30
|
+
CP,
|
|
31
|
+
CS,
|
|
32
|
+
V,
|
|
33
|
+
I,
|
|
34
|
+
DB extends AnyDatabaseProvider,
|
|
35
|
+
E extends EventSchemaConfig = Record<never, never>,
|
|
36
|
+
Q extends QueueSchemaConfig = Record<never, never>,
|
|
37
|
+
> {
|
|
38
|
+
#actor: ActorInstance<S, CP, CS, V, I, DB, E, Q>;
|
|
39
|
+
#subscriptionIndex = new Map<
|
|
40
|
+
string,
|
|
41
|
+
Set<Conn<S, CP, CS, V, I, DB, E, Q>>
|
|
42
|
+
>();
|
|
43
|
+
|
|
44
|
+
constructor(actor: ActorInstance<S, CP, CS, V, I, DB, E, Q>) {
|
|
45
|
+
this.#actor = actor;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// MARK: - Public API
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Adds a subscription for a connection to an event.
|
|
52
|
+
*
|
|
53
|
+
* @param eventName - The name of the event to subscribe to
|
|
54
|
+
* @param connection - The connection subscribing to the event
|
|
55
|
+
* @param fromPersist - Whether this subscription is being restored from persistence
|
|
56
|
+
*/
|
|
57
|
+
addSubscription(
|
|
58
|
+
eventName: string,
|
|
59
|
+
connection: Conn<S, CP, CS, V, I, DB, E, Q>,
|
|
60
|
+
fromPersist: boolean,
|
|
61
|
+
) {
|
|
62
|
+
// Check if already subscribed
|
|
63
|
+
if (connection.subscriptions.has(eventName)) {
|
|
64
|
+
this.#actor.rLog.debug({
|
|
65
|
+
msg: "connection already has subscription",
|
|
66
|
+
eventName,
|
|
67
|
+
connId: connection.id,
|
|
68
|
+
});
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Update connection's subscription list
|
|
73
|
+
connection.subscriptions.add(eventName);
|
|
74
|
+
|
|
75
|
+
// Update subscription index
|
|
76
|
+
let subscribers = this.#subscriptionIndex.get(eventName);
|
|
77
|
+
if (!subscribers) {
|
|
78
|
+
subscribers = new Set();
|
|
79
|
+
this.#subscriptionIndex.set(eventName, subscribers);
|
|
80
|
+
}
|
|
81
|
+
subscribers.add(connection);
|
|
82
|
+
|
|
83
|
+
// Persist subscription if not restoring from persistence
|
|
84
|
+
if (!fromPersist) {
|
|
85
|
+
connection[CONN_STATE_MANAGER_SYMBOL].addSubscription({
|
|
86
|
+
eventName,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Save state immediately
|
|
90
|
+
this.#actor.stateManager.saveState({ immediate: true });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
this.#actor.rLog.debug({
|
|
94
|
+
msg: "subscription added",
|
|
95
|
+
eventName,
|
|
96
|
+
connId: connection.id,
|
|
97
|
+
totalSubscribers: subscribers.size,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Removes a subscription for a connection from an event.
|
|
103
|
+
*
|
|
104
|
+
* @param eventName - The name of the event to unsubscribe from
|
|
105
|
+
* @param connection - The connection unsubscribing from the event
|
|
106
|
+
* @param fromRemoveConn - Whether this is being called as part of connection removal
|
|
107
|
+
*/
|
|
108
|
+
removeSubscription(
|
|
109
|
+
eventName: string,
|
|
110
|
+
connection: Conn<S, CP, CS, V, I, DB, E, Q>,
|
|
111
|
+
fromRemoveConn: boolean,
|
|
112
|
+
) {
|
|
113
|
+
// Check if subscription exists
|
|
114
|
+
if (!connection.subscriptions.has(eventName)) {
|
|
115
|
+
this.#actor.rLog.warn({
|
|
116
|
+
msg: "connection does not have subscription",
|
|
117
|
+
eventName,
|
|
118
|
+
connId: connection.id,
|
|
119
|
+
});
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Remove from connection's subscription list
|
|
124
|
+
connection.subscriptions.delete(eventName);
|
|
125
|
+
|
|
126
|
+
// Update subscription index
|
|
127
|
+
const subscribers = this.#subscriptionIndex.get(eventName);
|
|
128
|
+
if (subscribers) {
|
|
129
|
+
subscribers.delete(connection);
|
|
130
|
+
if (subscribers.size === 0) {
|
|
131
|
+
this.#subscriptionIndex.delete(eventName);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Update persistence if not part of connection removal
|
|
136
|
+
if (!fromRemoveConn) {
|
|
137
|
+
// Remove from persisted subscriptions
|
|
138
|
+
const removed = connection[
|
|
139
|
+
CONN_STATE_MANAGER_SYMBOL
|
|
140
|
+
].removeSubscription({ eventName });
|
|
141
|
+
if (!removed) {
|
|
142
|
+
this.#actor.rLog.warn({
|
|
143
|
+
msg: "subscription does not exist in persist",
|
|
144
|
+
eventName,
|
|
145
|
+
connId: connection.id,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Save state immediately
|
|
150
|
+
this.#actor.stateManager.saveState({ immediate: true });
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
this.#actor.rLog.debug({
|
|
154
|
+
msg: "subscription removed",
|
|
155
|
+
eventName,
|
|
156
|
+
connId: connection.id,
|
|
157
|
+
remainingSubscribers: subscribers?.size || 0,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Broadcasts an event to all subscribed connections.
|
|
163
|
+
*
|
|
164
|
+
* @param name - The name of the event to broadcast
|
|
165
|
+
* @param args - The arguments to send with the event
|
|
166
|
+
*/
|
|
167
|
+
broadcast<Args extends Array<unknown>>(name: string, ...args: Args) {
|
|
168
|
+
this.#actor.assertReady();
|
|
169
|
+
|
|
170
|
+
// Get subscribers for this event
|
|
171
|
+
const subscribers = this.#subscriptionIndex.get(name);
|
|
172
|
+
if (!subscribers || subscribers.size === 0) {
|
|
173
|
+
this.#actor.rLog.debug({
|
|
174
|
+
msg: "no subscribers for event",
|
|
175
|
+
eventName: name,
|
|
176
|
+
});
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
this.#actor.emitTraceEvent("message.broadcast", {
|
|
181
|
+
"rivet.event.name": name,
|
|
182
|
+
"rivet.broadcast.subscribers": subscribers.size,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// Create serialized message
|
|
186
|
+
const eventData = { name, args };
|
|
187
|
+
const toClientSerializer = new CachedSerializer(
|
|
188
|
+
eventData,
|
|
189
|
+
TO_CLIENT_VERSIONED,
|
|
190
|
+
CLIENT_PROTOCOL_CURRENT_VERSION,
|
|
191
|
+
ToClientSchema,
|
|
192
|
+
// JSON: args is the raw value (array of arguments)
|
|
193
|
+
(value): ToClientJson => ({
|
|
194
|
+
body: {
|
|
195
|
+
tag: "Event" as const,
|
|
196
|
+
val: {
|
|
197
|
+
name: value.name,
|
|
198
|
+
args: value.args,
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
}),
|
|
202
|
+
// BARE/CBOR: args needs to be CBOR-encoded to ArrayBuffer
|
|
203
|
+
(value): protocol.ToClient => ({
|
|
204
|
+
body: {
|
|
205
|
+
tag: "Event" as const,
|
|
206
|
+
val: {
|
|
207
|
+
name: value.name,
|
|
208
|
+
args: bufferToArrayBuffer(cbor.encode(value.args)),
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
// Send to all subscribers
|
|
215
|
+
let sentCount = 0;
|
|
216
|
+
for (const connection of subscribers) {
|
|
217
|
+
if (connection[CONN_SPEAKS_RIVETKIT_SYMBOL]) {
|
|
218
|
+
try {
|
|
219
|
+
connection[CONN_SEND_MESSAGE_SYMBOL](toClientSerializer);
|
|
220
|
+
sentCount++;
|
|
221
|
+
} catch (error) {
|
|
222
|
+
// Propagate message size errors to the call site so developers
|
|
223
|
+
// can handle them
|
|
224
|
+
if (error instanceof errors.OutgoingMessageTooLong) {
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
// Log other errors (e.g., closed connections) and continue
|
|
228
|
+
this.#actor.rLog.error({
|
|
229
|
+
msg: "failed to send event to connection",
|
|
230
|
+
eventName: name,
|
|
231
|
+
connId: connection.id,
|
|
232
|
+
error:
|
|
233
|
+
error instanceof Error
|
|
234
|
+
? error.message
|
|
235
|
+
: String(error),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
this.#actor.rLog.debug({
|
|
242
|
+
msg: "event broadcasted",
|
|
243
|
+
eventName: name,
|
|
244
|
+
subscriberCount: subscribers.size,
|
|
245
|
+
sentCount,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Gets all subscribers for a specific event.
|
|
251
|
+
*
|
|
252
|
+
* @param eventName - The name of the event
|
|
253
|
+
* @returns Set of connections subscribed to the event, or undefined if no subscribers
|
|
254
|
+
*/
|
|
255
|
+
getSubscribers(
|
|
256
|
+
eventName: string,
|
|
257
|
+
): Set<Conn<S, CP, CS, V, I, DB, E, Q>> | undefined {
|
|
258
|
+
return this.#subscriptionIndex.get(eventName);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Gets all events and their subscriber counts.
|
|
263
|
+
*
|
|
264
|
+
* @returns Map of event names to subscriber counts
|
|
265
|
+
*/
|
|
266
|
+
getEventStats(): Map<string, number> {
|
|
267
|
+
const stats = new Map<string, number>();
|
|
268
|
+
for (const [eventName, subscribers] of this.#subscriptionIndex) {
|
|
269
|
+
stats.set(eventName, subscribers.size);
|
|
270
|
+
}
|
|
271
|
+
return stats;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Clears all subscriptions for a connection.
|
|
276
|
+
* Used during connection cleanup.
|
|
277
|
+
*
|
|
278
|
+
* @param connection - The connection to clear subscriptions for
|
|
279
|
+
*/
|
|
280
|
+
clearConnectionSubscriptions(connection: Conn<S, CP, CS, V, I, DB, E, Q>) {
|
|
281
|
+
for (const eventName of [...connection.subscriptions.values()]) {
|
|
282
|
+
this.removeSubscription(eventName, connection, true);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Gets the total number of unique events being subscribed to.
|
|
288
|
+
*/
|
|
289
|
+
get eventCount(): number {
|
|
290
|
+
return this.#subscriptionIndex.size;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Gets the total number of subscriptions across all events.
|
|
295
|
+
*/
|
|
296
|
+
get totalSubscriptionCount(): number {
|
|
297
|
+
let total = 0;
|
|
298
|
+
for (const subscribers of this.#subscriptionIndex.values()) {
|
|
299
|
+
total += subscribers.size;
|
|
300
|
+
}
|
|
301
|
+
return total;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Checks if an event has any subscribers.
|
|
306
|
+
*
|
|
307
|
+
* @param eventName - The name of the event to check
|
|
308
|
+
* @returns True if the event has at least one subscriber
|
|
309
|
+
*/
|
|
310
|
+
hasSubscribers(eventName: string): boolean {
|
|
311
|
+
const subscribers = this.#subscriptionIndex.get(eventName);
|
|
312
|
+
return subscribers !== undefined && subscribers.size > 0;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
export const KEYS = {
|
|
2
|
+
PERSIST_DATA: Uint8Array.from([1]),
|
|
3
|
+
CONN_PREFIX: Uint8Array.from([2]), // Prefix for connection keys
|
|
4
|
+
INSPECTOR_TOKEN: Uint8Array.from([3]), // Inspector token key
|
|
5
|
+
KV: Uint8Array.from([4]), // Prefix for user-facing KV storage
|
|
6
|
+
QUEUE_PREFIX: Uint8Array.from([5]), // Prefix for queue storage
|
|
7
|
+
WORKFLOW_PREFIX: Uint8Array.from([6]), // Prefix for workflow storage
|
|
8
|
+
TRACES_PREFIX: Uint8Array.from([7]), // Prefix for traces storage
|
|
9
|
+
SQLITE_PREFIX: Uint8Array.from([8]), // Prefix for SQLite VFS data
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const STORAGE_VERSION = {
|
|
13
|
+
QUEUE: 1,
|
|
14
|
+
WORKFLOW: 1,
|
|
15
|
+
TRACES: 1,
|
|
16
|
+
SQLITE: 1,
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
const STORAGE_VERSION_BYTES = {
|
|
20
|
+
QUEUE: Uint8Array.from([STORAGE_VERSION.QUEUE]),
|
|
21
|
+
WORKFLOW: Uint8Array.from([STORAGE_VERSION.WORKFLOW]),
|
|
22
|
+
TRACES: Uint8Array.from([STORAGE_VERSION.TRACES]),
|
|
23
|
+
SQLITE: Uint8Array.from([STORAGE_VERSION.SQLITE]),
|
|
24
|
+
} as const;
|
|
25
|
+
|
|
26
|
+
const QUEUE_NAMESPACE = {
|
|
27
|
+
METADATA: Uint8Array.from([1]),
|
|
28
|
+
MESSAGES: Uint8Array.from([2]),
|
|
29
|
+
} as const;
|
|
30
|
+
|
|
31
|
+
const QUEUE_ID_BYTES = 8;
|
|
32
|
+
|
|
33
|
+
function concatPrefix(prefix: Uint8Array, suffix: Uint8Array): Uint8Array {
|
|
34
|
+
const merged = new Uint8Array(prefix.length + suffix.length);
|
|
35
|
+
merged.set(prefix, 0);
|
|
36
|
+
merged.set(suffix, prefix.length);
|
|
37
|
+
return merged;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const QUEUE_STORAGE_PREFIX = concatPrefix(
|
|
41
|
+
KEYS.QUEUE_PREFIX,
|
|
42
|
+
STORAGE_VERSION_BYTES.QUEUE,
|
|
43
|
+
);
|
|
44
|
+
const QUEUE_METADATA_KEY = concatPrefix(
|
|
45
|
+
QUEUE_STORAGE_PREFIX,
|
|
46
|
+
QUEUE_NAMESPACE.METADATA,
|
|
47
|
+
);
|
|
48
|
+
const QUEUE_MESSAGES_PREFIX = concatPrefix(
|
|
49
|
+
QUEUE_STORAGE_PREFIX,
|
|
50
|
+
QUEUE_NAMESPACE.MESSAGES,
|
|
51
|
+
);
|
|
52
|
+
const WORKFLOW_STORAGE_PREFIX = concatPrefix(
|
|
53
|
+
KEYS.WORKFLOW_PREFIX,
|
|
54
|
+
STORAGE_VERSION_BYTES.WORKFLOW,
|
|
55
|
+
);
|
|
56
|
+
const TRACES_STORAGE_PREFIX = concatPrefix(
|
|
57
|
+
KEYS.TRACES_PREFIX,
|
|
58
|
+
STORAGE_VERSION_BYTES.TRACES,
|
|
59
|
+
);
|
|
60
|
+
const SQLITE_STORAGE_PREFIX = concatPrefix(
|
|
61
|
+
KEYS.SQLITE_PREFIX,
|
|
62
|
+
STORAGE_VERSION_BYTES.SQLITE,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// Helper to create a prefixed key for user-facing KV storage
|
|
66
|
+
export function makePrefixedKey(key: Uint8Array): Uint8Array {
|
|
67
|
+
const prefixed = new Uint8Array(KEYS.KV.length + key.length);
|
|
68
|
+
prefixed.set(KEYS.KV, 0);
|
|
69
|
+
prefixed.set(key, KEYS.KV.length);
|
|
70
|
+
return prefixed;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Helper to remove the prefix from a key
|
|
74
|
+
export function removePrefixFromKey(prefixedKey: Uint8Array): Uint8Array {
|
|
75
|
+
return prefixedKey.slice(KEYS.KV.length);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function makeWorkflowKey(key: Uint8Array): Uint8Array {
|
|
79
|
+
return concatPrefix(WORKFLOW_STORAGE_PREFIX, key);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function makeTracesKey(key: Uint8Array): Uint8Array {
|
|
83
|
+
return concatPrefix(TRACES_STORAGE_PREFIX, key);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function workflowStoragePrefix(): Uint8Array {
|
|
87
|
+
return Uint8Array.from(WORKFLOW_STORAGE_PREFIX);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function tracesStoragePrefix(): Uint8Array {
|
|
91
|
+
return Uint8Array.from(TRACES_STORAGE_PREFIX);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function sqliteStoragePrefix(): Uint8Array {
|
|
95
|
+
return Uint8Array.from(SQLITE_STORAGE_PREFIX);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function queueStoragePrefix(): Uint8Array {
|
|
99
|
+
return Uint8Array.from(QUEUE_STORAGE_PREFIX);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function queueMetadataKey(): Uint8Array {
|
|
103
|
+
return Uint8Array.from(QUEUE_METADATA_KEY);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function queueMessagesPrefix(): Uint8Array {
|
|
107
|
+
return Uint8Array.from(QUEUE_MESSAGES_PREFIX);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Helper to create a connection key
|
|
111
|
+
export function makeConnKey(connId: string): Uint8Array {
|
|
112
|
+
const encoder = new TextEncoder();
|
|
113
|
+
const connIdBytes = encoder.encode(connId);
|
|
114
|
+
const key = new Uint8Array(KEYS.CONN_PREFIX.length + connIdBytes.length);
|
|
115
|
+
key.set(KEYS.CONN_PREFIX, 0);
|
|
116
|
+
key.set(connIdBytes, KEYS.CONN_PREFIX.length);
|
|
117
|
+
return key;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Helper to create a queue message key
|
|
121
|
+
export function makeQueueMessageKey(id: bigint): Uint8Array {
|
|
122
|
+
const key = new Uint8Array(QUEUE_MESSAGES_PREFIX.length + QUEUE_ID_BYTES);
|
|
123
|
+
key.set(QUEUE_MESSAGES_PREFIX, 0);
|
|
124
|
+
const view = new DataView(key.buffer, key.byteOffset, key.byteLength);
|
|
125
|
+
view.setBigUint64(QUEUE_MESSAGES_PREFIX.length, id, false);
|
|
126
|
+
return key;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Helper to decode a queue message key
|
|
130
|
+
export function decodeQueueMessageKey(key: Uint8Array): bigint {
|
|
131
|
+
const offset = QUEUE_MESSAGES_PREFIX.length;
|
|
132
|
+
if (key.length < offset + QUEUE_ID_BYTES) {
|
|
133
|
+
throw new Error("Queue key is too short");
|
|
134
|
+
}
|
|
135
|
+
for (let i = 0; i < QUEUE_MESSAGES_PREFIX.length; i++) {
|
|
136
|
+
if (key[i] !== QUEUE_MESSAGES_PREFIX[i]) {
|
|
137
|
+
throw new Error("Queue key has invalid prefix");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const view = new DataView(
|
|
141
|
+
key.buffer,
|
|
142
|
+
key.byteOffset + offset,
|
|
143
|
+
QUEUE_ID_BYTES,
|
|
144
|
+
);
|
|
145
|
+
return view.getBigUint64(0, false);
|
|
146
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import type { ActorDriver } from "../driver";
|
|
2
|
+
import { makePrefixedKey, removePrefixFromKey } from "./keys";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* User-facing KV storage interface exposed on ActorContext.
|
|
6
|
+
*/
|
|
7
|
+
type KvValueType = "text" | "arrayBuffer" | "binary";
|
|
8
|
+
type KvKeyType = "text" | "binary";
|
|
9
|
+
type KvKey = Uint8Array | string;
|
|
10
|
+
|
|
11
|
+
type KvValueTypeMap = {
|
|
12
|
+
text: string;
|
|
13
|
+
arrayBuffer: ArrayBuffer;
|
|
14
|
+
binary: Uint8Array;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type KvKeyTypeMap = {
|
|
18
|
+
text: string;
|
|
19
|
+
binary: Uint8Array;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type KvValueOptions<T extends KvValueType = "text"> = {
|
|
23
|
+
type?: T;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type KvListOptions<
|
|
27
|
+
T extends KvValueType = "text",
|
|
28
|
+
K extends KvKeyType = "text",
|
|
29
|
+
> = KvValueOptions<T> & {
|
|
30
|
+
keyType?: K;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const textEncoder = new TextEncoder();
|
|
34
|
+
const textDecoder = new TextDecoder();
|
|
35
|
+
|
|
36
|
+
function encodeKey<K extends KvKeyType = KvKeyType>(
|
|
37
|
+
key: KvKeyTypeMap[K],
|
|
38
|
+
keyType?: K,
|
|
39
|
+
): Uint8Array {
|
|
40
|
+
if (key instanceof Uint8Array) {
|
|
41
|
+
return key;
|
|
42
|
+
}
|
|
43
|
+
const resolvedKeyType = keyType ?? "text";
|
|
44
|
+
if (resolvedKeyType === "binary") {
|
|
45
|
+
throw new TypeError("Expected a Uint8Array when keyType is binary");
|
|
46
|
+
}
|
|
47
|
+
return textEncoder.encode(key);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function decodeKey<K extends KvKeyType = "text">(
|
|
51
|
+
key: Uint8Array,
|
|
52
|
+
keyType?: K,
|
|
53
|
+
): KvKeyTypeMap[K] {
|
|
54
|
+
const resolvedKeyType = keyType ?? "text";
|
|
55
|
+
switch (resolvedKeyType) {
|
|
56
|
+
case "text":
|
|
57
|
+
return textDecoder.decode(key) as KvKeyTypeMap[K];
|
|
58
|
+
case "binary":
|
|
59
|
+
return key as KvKeyTypeMap[K];
|
|
60
|
+
default:
|
|
61
|
+
throw new TypeError("Invalid kv key type");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function resolveValueType(
|
|
66
|
+
value: string | Uint8Array | ArrayBuffer,
|
|
67
|
+
): KvValueType {
|
|
68
|
+
if (typeof value === "string") {
|
|
69
|
+
return "text";
|
|
70
|
+
}
|
|
71
|
+
if (value instanceof Uint8Array) {
|
|
72
|
+
return "binary";
|
|
73
|
+
}
|
|
74
|
+
if (value instanceof ArrayBuffer) {
|
|
75
|
+
return "arrayBuffer";
|
|
76
|
+
}
|
|
77
|
+
throw new TypeError("Invalid kv value");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function encodeValue<T extends KvValueType = KvValueType>(
|
|
81
|
+
value: KvValueTypeMap[T],
|
|
82
|
+
options?: KvValueOptions<T>,
|
|
83
|
+
): Uint8Array {
|
|
84
|
+
const type =
|
|
85
|
+
options?.type ??
|
|
86
|
+
resolveValueType(value as string | Uint8Array | ArrayBuffer);
|
|
87
|
+
switch (type) {
|
|
88
|
+
case "text":
|
|
89
|
+
if (typeof value !== "string") {
|
|
90
|
+
throw new TypeError("Expected a string when type is text");
|
|
91
|
+
}
|
|
92
|
+
return textEncoder.encode(value);
|
|
93
|
+
case "arrayBuffer":
|
|
94
|
+
if (!(value instanceof ArrayBuffer)) {
|
|
95
|
+
throw new TypeError("Expected an ArrayBuffer when type is arrayBuffer");
|
|
96
|
+
}
|
|
97
|
+
return new Uint8Array(value);
|
|
98
|
+
case "binary":
|
|
99
|
+
if (!(value instanceof Uint8Array)) {
|
|
100
|
+
throw new TypeError("Expected a Uint8Array when type is binary");
|
|
101
|
+
}
|
|
102
|
+
return value;
|
|
103
|
+
default:
|
|
104
|
+
throw new TypeError("Invalid kv value type");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function decodeValue<T extends KvValueType = "text">(
|
|
109
|
+
value: Uint8Array,
|
|
110
|
+
options?: KvValueOptions<T>,
|
|
111
|
+
): KvValueTypeMap[T] {
|
|
112
|
+
const type = options?.type ?? "text";
|
|
113
|
+
switch (type) {
|
|
114
|
+
case "text":
|
|
115
|
+
return textDecoder.decode(value) as KvValueTypeMap[T];
|
|
116
|
+
case "arrayBuffer": {
|
|
117
|
+
const copy = new Uint8Array(value.byteLength);
|
|
118
|
+
copy.set(value);
|
|
119
|
+
return copy.buffer as KvValueTypeMap[T];
|
|
120
|
+
}
|
|
121
|
+
case "binary":
|
|
122
|
+
return value as KvValueTypeMap[T];
|
|
123
|
+
default:
|
|
124
|
+
throw new TypeError("Invalid kv value type");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export class ActorKv {
|
|
129
|
+
#driver: ActorDriver;
|
|
130
|
+
#actorId: string;
|
|
131
|
+
|
|
132
|
+
constructor(driver: ActorDriver, actorId: string) {
|
|
133
|
+
this.#driver = driver;
|
|
134
|
+
this.#actorId = actorId;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Get a single value by key.
|
|
139
|
+
*/
|
|
140
|
+
async get<T extends KvValueType = "text">(
|
|
141
|
+
key: KvKey,
|
|
142
|
+
options?: KvValueOptions<T>,
|
|
143
|
+
): Promise<KvValueTypeMap[T] | null> {
|
|
144
|
+
const results = await this.#driver.kvBatchGet(this.#actorId, [
|
|
145
|
+
makePrefixedKey(encodeKey(key)),
|
|
146
|
+
]);
|
|
147
|
+
const result = results[0];
|
|
148
|
+
if (!result) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
return decodeValue(result, options);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Get multiple values by keys.
|
|
156
|
+
*/
|
|
157
|
+
async getBatch<T extends KvValueType = "text">(
|
|
158
|
+
keys: KvKey[],
|
|
159
|
+
options?: KvValueOptions<T>,
|
|
160
|
+
): Promise<(KvValueTypeMap[T] | null)[]> {
|
|
161
|
+
const prefixedKeys = keys.map((key) =>
|
|
162
|
+
makePrefixedKey(encodeKey(key)),
|
|
163
|
+
);
|
|
164
|
+
const results = await this.#driver.kvBatchGet(
|
|
165
|
+
this.#actorId,
|
|
166
|
+
prefixedKeys,
|
|
167
|
+
);
|
|
168
|
+
return results.map((result) =>
|
|
169
|
+
result ? decodeValue(result, options) : null,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Put a single key-value pair.
|
|
175
|
+
*/
|
|
176
|
+
async put<T extends KvValueType = KvValueType>(
|
|
177
|
+
key: KvKey,
|
|
178
|
+
value: KvValueTypeMap[T],
|
|
179
|
+
options?: KvValueOptions<T>,
|
|
180
|
+
): Promise<void> {
|
|
181
|
+
await this.#driver.kvBatchPut(this.#actorId, [
|
|
182
|
+
[makePrefixedKey(encodeKey(key)), encodeValue(value, options)],
|
|
183
|
+
]);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Put multiple key-value pairs.
|
|
188
|
+
*/
|
|
189
|
+
async putBatch<T extends KvValueType = KvValueType>(
|
|
190
|
+
entries: [KvKey, KvValueTypeMap[T]][],
|
|
191
|
+
options?: KvValueOptions<T>,
|
|
192
|
+
): Promise<void> {
|
|
193
|
+
const prefixedEntries: [Uint8Array, Uint8Array][] = entries.map(
|
|
194
|
+
([key, value]) => [
|
|
195
|
+
makePrefixedKey(encodeKey(key)),
|
|
196
|
+
encodeValue(value, options),
|
|
197
|
+
],
|
|
198
|
+
);
|
|
199
|
+
await this.#driver.kvBatchPut(this.#actorId, prefixedEntries);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Delete a single key.
|
|
204
|
+
*/
|
|
205
|
+
async delete(key: KvKey): Promise<void> {
|
|
206
|
+
await this.#driver.kvBatchDelete(this.#actorId, [
|
|
207
|
+
makePrefixedKey(encodeKey(key)),
|
|
208
|
+
]);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Delete multiple keys.
|
|
213
|
+
*/
|
|
214
|
+
async deleteBatch(keys: KvKey[]): Promise<void> {
|
|
215
|
+
const prefixedKeys = keys.map((key) =>
|
|
216
|
+
makePrefixedKey(encodeKey(key)),
|
|
217
|
+
);
|
|
218
|
+
await this.#driver.kvBatchDelete(this.#actorId, prefixedKeys);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* List all keys with a given prefix.
|
|
223
|
+
* Returns key-value pairs where keys have the user prefix removed.
|
|
224
|
+
*/
|
|
225
|
+
async list<T extends KvValueType = "text", K extends KvKeyType = "text">(
|
|
226
|
+
prefix: KvKeyTypeMap[K],
|
|
227
|
+
options?: KvListOptions<T, K>,
|
|
228
|
+
): Promise<[KvKeyTypeMap[K], KvValueTypeMap[T]][]> {
|
|
229
|
+
const prefixedPrefix = makePrefixedKey(
|
|
230
|
+
encodeKey(prefix, options?.keyType),
|
|
231
|
+
);
|
|
232
|
+
const results = await this.#driver.kvListPrefix(
|
|
233
|
+
this.#actorId,
|
|
234
|
+
prefixedPrefix,
|
|
235
|
+
);
|
|
236
|
+
return results.map(([key, value]) => [
|
|
237
|
+
decodeKey<K>(removePrefixFromKey(key), options?.keyType),
|
|
238
|
+
decodeValue<T>(value, options),
|
|
239
|
+
]);
|
|
240
|
+
}
|
|
241
|
+
}
|