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,416 @@
|
|
|
1
|
+
import * as cbor from "cbor-x";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import type { AnyDatabaseProvider } from "@/actor/database";
|
|
4
|
+
import * as errors from "@/actor/errors";
|
|
5
|
+
import {
|
|
6
|
+
CachedSerializer,
|
|
7
|
+
type Encoding,
|
|
8
|
+
type InputData,
|
|
9
|
+
} from "@/actor/protocol/serde";
|
|
10
|
+
import { deconstructError } from "@/common/utils";
|
|
11
|
+
import type * as protocol from "@/schemas/client-protocol/mod";
|
|
12
|
+
import {
|
|
13
|
+
CURRENT_VERSION as CLIENT_PROTOCOL_CURRENT_VERSION,
|
|
14
|
+
TO_CLIENT_VERSIONED,
|
|
15
|
+
TO_SERVER_VERSIONED,
|
|
16
|
+
} from "@/schemas/client-protocol/versioned";
|
|
17
|
+
import {
|
|
18
|
+
type ToClient as ToClientJson,
|
|
19
|
+
ToClientSchema,
|
|
20
|
+
type ToServer as ToServerJson,
|
|
21
|
+
ToServerSchema,
|
|
22
|
+
} from "@/schemas/client-protocol-zod/mod";
|
|
23
|
+
import { deserializeWithEncoding } from "@/serde";
|
|
24
|
+
import {
|
|
25
|
+
assertUnreachable,
|
|
26
|
+
bufferToArrayBuffer,
|
|
27
|
+
getEnvUniversal,
|
|
28
|
+
} from "../../utils";
|
|
29
|
+
import { CONN_SEND_MESSAGE_SYMBOL, type Conn } from "../conn/mod";
|
|
30
|
+
import { ActionContext } from "../contexts";
|
|
31
|
+
import type { ActorInstance } from "../instance/mod";
|
|
32
|
+
import type { EventSchemaConfig, QueueSchemaConfig } from "../schema";
|
|
33
|
+
|
|
34
|
+
interface MessageEventOpts {
|
|
35
|
+
encoding: Encoding;
|
|
36
|
+
maxIncomingMessageSize: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function getValueLength(value: InputData): number {
|
|
40
|
+
if (typeof value === "string") {
|
|
41
|
+
return value.length;
|
|
42
|
+
} else if (value instanceof Blob) {
|
|
43
|
+
return value.size;
|
|
44
|
+
} else if (
|
|
45
|
+
value instanceof ArrayBuffer ||
|
|
46
|
+
value instanceof SharedArrayBuffer ||
|
|
47
|
+
value instanceof Uint8Array
|
|
48
|
+
) {
|
|
49
|
+
return value.byteLength;
|
|
50
|
+
} else {
|
|
51
|
+
assertUnreachable(value);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function inputDataToBuffer(
|
|
56
|
+
data: InputData,
|
|
57
|
+
): Promise<Uint8Array | string> {
|
|
58
|
+
if (typeof data === "string") {
|
|
59
|
+
return data;
|
|
60
|
+
} else if (data instanceof Blob) {
|
|
61
|
+
const arrayBuffer = await data.arrayBuffer();
|
|
62
|
+
return new Uint8Array(arrayBuffer);
|
|
63
|
+
} else if (data instanceof Uint8Array) {
|
|
64
|
+
return data;
|
|
65
|
+
} else if (
|
|
66
|
+
data instanceof ArrayBuffer ||
|
|
67
|
+
data instanceof SharedArrayBuffer
|
|
68
|
+
) {
|
|
69
|
+
return new Uint8Array(data);
|
|
70
|
+
} else {
|
|
71
|
+
throw new errors.MalformedMessage();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function parseMessage(
|
|
76
|
+
value: InputData,
|
|
77
|
+
opts: MessageEventOpts,
|
|
78
|
+
): Promise<{
|
|
79
|
+
body:
|
|
80
|
+
| {
|
|
81
|
+
tag: "ActionRequest";
|
|
82
|
+
val: { id: bigint; name: string; args: unknown };
|
|
83
|
+
}
|
|
84
|
+
| {
|
|
85
|
+
tag: "SubscriptionRequest";
|
|
86
|
+
val: { eventName: string; subscribe: boolean };
|
|
87
|
+
};
|
|
88
|
+
}> {
|
|
89
|
+
// Validate value length
|
|
90
|
+
const length = getValueLength(value);
|
|
91
|
+
if (length > opts.maxIncomingMessageSize) {
|
|
92
|
+
throw new errors.IncomingMessageTooLong();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Convert value
|
|
96
|
+
let buffer = await inputDataToBuffer(value);
|
|
97
|
+
|
|
98
|
+
// HACK: For some reason, the output buffer needs to be cloned when using BARE encoding
|
|
99
|
+
//
|
|
100
|
+
// THis is likely because the input data is of type `Buffer` and there is an inconsistency in implementation that I am not aware of
|
|
101
|
+
if (buffer instanceof Buffer) {
|
|
102
|
+
buffer = new Uint8Array(buffer);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Deserialize message
|
|
106
|
+
return deserializeWithEncoding(
|
|
107
|
+
opts.encoding,
|
|
108
|
+
buffer,
|
|
109
|
+
TO_SERVER_VERSIONED,
|
|
110
|
+
ToServerSchema,
|
|
111
|
+
// JSON: values are already the correct type
|
|
112
|
+
(json: ToServerJson): any => json,
|
|
113
|
+
// BARE: need to decode ArrayBuffer fields back to unknown
|
|
114
|
+
(bare: protocol.ToServer): any => {
|
|
115
|
+
if (bare.body.tag === "ActionRequest") {
|
|
116
|
+
return {
|
|
117
|
+
body: {
|
|
118
|
+
tag: "ActionRequest",
|
|
119
|
+
val: {
|
|
120
|
+
id: bare.body.val.id,
|
|
121
|
+
name: bare.body.val.name,
|
|
122
|
+
args: cbor.decode(
|
|
123
|
+
new Uint8Array(bare.body.val.args),
|
|
124
|
+
),
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
} else {
|
|
129
|
+
// SubscriptionRequest has no ArrayBuffer fields
|
|
130
|
+
return bare;
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export interface ProcessMessageHandler<
|
|
137
|
+
S,
|
|
138
|
+
CP,
|
|
139
|
+
CS,
|
|
140
|
+
V,
|
|
141
|
+
I,
|
|
142
|
+
DB extends AnyDatabaseProvider,
|
|
143
|
+
E extends EventSchemaConfig,
|
|
144
|
+
Q extends QueueSchemaConfig,
|
|
145
|
+
> {
|
|
146
|
+
onExecuteAction?: (
|
|
147
|
+
ctx: ActionContext<S, CP, CS, V, I, DB, E, Q>,
|
|
148
|
+
name: string,
|
|
149
|
+
args: unknown[],
|
|
150
|
+
) => Promise<unknown>;
|
|
151
|
+
onSubscribe?: (
|
|
152
|
+
eventName: string,
|
|
153
|
+
conn: Conn<S, CP, CS, V, I, DB, E, Q>,
|
|
154
|
+
) => Promise<void>;
|
|
155
|
+
onUnsubscribe?: (
|
|
156
|
+
eventName: string,
|
|
157
|
+
conn: Conn<S, CP, CS, V, I, DB, E, Q>,
|
|
158
|
+
) => Promise<void>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export async function processMessage<
|
|
162
|
+
S,
|
|
163
|
+
CP,
|
|
164
|
+
CS,
|
|
165
|
+
V,
|
|
166
|
+
I,
|
|
167
|
+
DB extends AnyDatabaseProvider,
|
|
168
|
+
E extends EventSchemaConfig,
|
|
169
|
+
Q extends QueueSchemaConfig,
|
|
170
|
+
>(
|
|
171
|
+
message: {
|
|
172
|
+
body:
|
|
173
|
+
| {
|
|
174
|
+
tag: "ActionRequest";
|
|
175
|
+
val: { id: bigint; name: string; args: unknown };
|
|
176
|
+
}
|
|
177
|
+
| {
|
|
178
|
+
tag: "SubscriptionRequest";
|
|
179
|
+
val: { eventName: string; subscribe: boolean };
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
actor: ActorInstance<S, CP, CS, V, I, DB, E, Q>,
|
|
183
|
+
conn: Conn<S, CP, CS, V, I, DB, E, Q>,
|
|
184
|
+
handler: ProcessMessageHandler<S, CP, CS, V, I, DB, E, Q>,
|
|
185
|
+
) {
|
|
186
|
+
let actionId: bigint | undefined;
|
|
187
|
+
let actionName: string | undefined;
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
if (message.body.tag === "ActionRequest") {
|
|
191
|
+
// Action request
|
|
192
|
+
|
|
193
|
+
if (handler.onExecuteAction === undefined) {
|
|
194
|
+
throw new errors.Unsupported("Action");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const { id, name, args } = message.body.val;
|
|
198
|
+
actionId = id;
|
|
199
|
+
actionName = name;
|
|
200
|
+
|
|
201
|
+
actor.rLog.debug({
|
|
202
|
+
msg: "processing action request",
|
|
203
|
+
actionId: id,
|
|
204
|
+
actionName: name,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const ctx = new ActionContext<S, CP, CS, V, I, DB, E, Q>(
|
|
208
|
+
actor,
|
|
209
|
+
conn,
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
// Process the action request and wait for the result
|
|
213
|
+
// This will wait for async actions to complete
|
|
214
|
+
const output = await handler.onExecuteAction(
|
|
215
|
+
ctx,
|
|
216
|
+
name,
|
|
217
|
+
args as unknown[],
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
actor.rLog.debug({
|
|
221
|
+
msg: "sending action response",
|
|
222
|
+
actionId: id,
|
|
223
|
+
actionName: name,
|
|
224
|
+
outputType: typeof output,
|
|
225
|
+
isPromise: output instanceof Promise,
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// Send the response back to the client
|
|
229
|
+
conn[CONN_SEND_MESSAGE_SYMBOL](
|
|
230
|
+
new CachedSerializer(
|
|
231
|
+
output,
|
|
232
|
+
TO_CLIENT_VERSIONED,
|
|
233
|
+
CLIENT_PROTOCOL_CURRENT_VERSION,
|
|
234
|
+
ToClientSchema,
|
|
235
|
+
// JSON: output is the raw value
|
|
236
|
+
(value): ToClientJson => ({
|
|
237
|
+
body: {
|
|
238
|
+
tag: "ActionResponse" as const,
|
|
239
|
+
val: {
|
|
240
|
+
id: id,
|
|
241
|
+
output: value,
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
}),
|
|
245
|
+
// BARE/CBOR: output needs to be CBOR-encoded to ArrayBuffer
|
|
246
|
+
(value): protocol.ToClient => ({
|
|
247
|
+
body: {
|
|
248
|
+
tag: "ActionResponse" as const,
|
|
249
|
+
val: {
|
|
250
|
+
id: id,
|
|
251
|
+
output: bufferToArrayBuffer(cbor.encode(value)),
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
}),
|
|
255
|
+
),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
actor.rLog.debug({ msg: "action response sent", id, name: name });
|
|
259
|
+
} else if (message.body.tag === "SubscriptionRequest") {
|
|
260
|
+
// Subscription request
|
|
261
|
+
|
|
262
|
+
if (
|
|
263
|
+
handler.onSubscribe === undefined ||
|
|
264
|
+
handler.onUnsubscribe === undefined
|
|
265
|
+
) {
|
|
266
|
+
throw new errors.Unsupported("Subscriptions");
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const { eventName, subscribe } = message.body.val;
|
|
270
|
+
actor.rLog.debug({
|
|
271
|
+
msg: "processing subscription request",
|
|
272
|
+
eventName,
|
|
273
|
+
subscribe,
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
if (subscribe) {
|
|
277
|
+
await actor.assertCanSubscribe(
|
|
278
|
+
new ActionContext<S, CP, CS, V, I, DB, E, Q>(
|
|
279
|
+
actor,
|
|
280
|
+
conn,
|
|
281
|
+
),
|
|
282
|
+
eventName,
|
|
283
|
+
);
|
|
284
|
+
await handler.onSubscribe(eventName, conn);
|
|
285
|
+
} else {
|
|
286
|
+
await handler.onUnsubscribe(eventName, conn);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
actor.rLog.debug({
|
|
290
|
+
msg: "subscription request completed",
|
|
291
|
+
eventName,
|
|
292
|
+
subscribe,
|
|
293
|
+
});
|
|
294
|
+
} else {
|
|
295
|
+
assertUnreachable(message.body);
|
|
296
|
+
}
|
|
297
|
+
} catch (error) {
|
|
298
|
+
const { group, code, message, metadata } = deconstructError(
|
|
299
|
+
error,
|
|
300
|
+
actor.rLog,
|
|
301
|
+
{
|
|
302
|
+
connectionId: conn.id,
|
|
303
|
+
actionId,
|
|
304
|
+
actionName,
|
|
305
|
+
},
|
|
306
|
+
getEnvUniversal("RIVET_EXPOSE_ERRORS") === "1" ||
|
|
307
|
+
getEnvUniversal("NODE_ENV") === "development",
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
actor.rLog.debug({
|
|
311
|
+
msg: "sending error response",
|
|
312
|
+
actionId,
|
|
313
|
+
actionName,
|
|
314
|
+
code,
|
|
315
|
+
message,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
// Build response
|
|
319
|
+
const errorData = { group, code, message, metadata, actionId };
|
|
320
|
+
conn[CONN_SEND_MESSAGE_SYMBOL](
|
|
321
|
+
new CachedSerializer(
|
|
322
|
+
errorData,
|
|
323
|
+
TO_CLIENT_VERSIONED,
|
|
324
|
+
CLIENT_PROTOCOL_CURRENT_VERSION,
|
|
325
|
+
ToClientSchema,
|
|
326
|
+
// JSON: metadata is the raw value (keep as undefined if not present)
|
|
327
|
+
(value): ToClientJson => {
|
|
328
|
+
const val: any = {
|
|
329
|
+
group: value.group,
|
|
330
|
+
code: value.code,
|
|
331
|
+
message: value.message,
|
|
332
|
+
actionId:
|
|
333
|
+
value.actionId !== undefined
|
|
334
|
+
? value.actionId
|
|
335
|
+
: null,
|
|
336
|
+
};
|
|
337
|
+
if (value.metadata !== undefined) {
|
|
338
|
+
val.metadata = value.metadata;
|
|
339
|
+
}
|
|
340
|
+
return {
|
|
341
|
+
body: {
|
|
342
|
+
tag: "Error" as const,
|
|
343
|
+
val,
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
},
|
|
347
|
+
// BARE/CBOR: metadata needs to be CBOR-encoded to ArrayBuffer
|
|
348
|
+
// Note: protocol.Error expects `| null` for optional fields (BARE protocol)
|
|
349
|
+
(value): protocol.ToClient => ({
|
|
350
|
+
body: {
|
|
351
|
+
tag: "Error" as const,
|
|
352
|
+
val: {
|
|
353
|
+
group: value.group,
|
|
354
|
+
code: value.code,
|
|
355
|
+
message: value.message,
|
|
356
|
+
metadata: value.metadata
|
|
357
|
+
? bufferToArrayBuffer(
|
|
358
|
+
cbor.encode(value.metadata),
|
|
359
|
+
)
|
|
360
|
+
: null,
|
|
361
|
+
actionId:
|
|
362
|
+
value.actionId !== undefined
|
|
363
|
+
? value.actionId
|
|
364
|
+
: null,
|
|
365
|
+
},
|
|
366
|
+
},
|
|
367
|
+
}),
|
|
368
|
+
),
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
actor.rLog.debug({ msg: "error response sent", actionId, actionName });
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
///**
|
|
376
|
+
// * Use `CachedSerializer` if serializing the same data repeatedly.
|
|
377
|
+
// */
|
|
378
|
+
//export function serialize<T>(value: T, encoding: Encoding): OutputData {
|
|
379
|
+
// if (encoding === "json") {
|
|
380
|
+
// return JSON.stringify(value);
|
|
381
|
+
// } else if (encoding === "cbor") {
|
|
382
|
+
// // TODO: Remove this hack, but cbor-x can't handle anything extra in data structures
|
|
383
|
+
// const cleanValue = JSON.parse(JSON.stringify(value));
|
|
384
|
+
// return cbor.encode(cleanValue);
|
|
385
|
+
// } else {
|
|
386
|
+
// assertUnreachable(encoding);
|
|
387
|
+
// }
|
|
388
|
+
//}
|
|
389
|
+
//
|
|
390
|
+
//export async function deserialize(data: InputData, encoding: Encoding) {
|
|
391
|
+
// if (encoding === "json") {
|
|
392
|
+
// if (typeof data !== "string") {
|
|
393
|
+
// actor.rLog.warn("received non-string for json parse");
|
|
394
|
+
// throw new errors.MalformedMessage();
|
|
395
|
+
// } else {
|
|
396
|
+
// return JSON.parse(data);
|
|
397
|
+
// }
|
|
398
|
+
// } else if (encoding === "cbor") {
|
|
399
|
+
// if (data instanceof Blob) {
|
|
400
|
+
// const arrayBuffer = await data.arrayBuffer();
|
|
401
|
+
// return cbor.decode(new Uint8Array(arrayBuffer));
|
|
402
|
+
// } else if (data instanceof Uint8Array) {
|
|
403
|
+
// return cbor.decode(data);
|
|
404
|
+
// } else if (
|
|
405
|
+
// data instanceof ArrayBuffer ||
|
|
406
|
+
// data instanceof SharedArrayBuffer
|
|
407
|
+
// ) {
|
|
408
|
+
// return cbor.decode(new Uint8Array(data));
|
|
409
|
+
// } else {
|
|
410
|
+
// actor.rLog.warn("received non-binary type for cbor parse");
|
|
411
|
+
// throw new errors.MalformedMessage();
|
|
412
|
+
// }
|
|
413
|
+
// } else {
|
|
414
|
+
// assertUnreachable(encoding);
|
|
415
|
+
// }
|
|
416
|
+
//}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import * as cbor from "cbor-x";
|
|
2
|
+
import type { VersionedDataHandler } from "vbare";
|
|
3
|
+
import { z } from "zod/v4";
|
|
4
|
+
import * as errors from "@/actor/errors";
|
|
5
|
+
import { serializeWithEncoding } from "@/serde";
|
|
6
|
+
import { loggerWithoutContext } from "../log";
|
|
7
|
+
import { assertUnreachable } from "../utils";
|
|
8
|
+
|
|
9
|
+
/** Data that can be deserialized. */
|
|
10
|
+
export type InputData = string | Buffer | Blob | ArrayBufferLike | Uint8Array;
|
|
11
|
+
|
|
12
|
+
/** Data that's been serialized. */
|
|
13
|
+
export type OutputData = string | Uint8Array;
|
|
14
|
+
|
|
15
|
+
export const EncodingSchema = z.enum(["json", "cbor", "bare"]);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Encoding used to communicate between the client & actor.
|
|
19
|
+
*/
|
|
20
|
+
export type Encoding = z.infer<typeof EncodingSchema>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Helper class that helps serialize data without re-serializing for the same encoding.
|
|
24
|
+
*/
|
|
25
|
+
export class CachedSerializer<TBare, TJson, T = TBare> {
|
|
26
|
+
#data: T;
|
|
27
|
+
#cache = new Map<Encoding, OutputData>();
|
|
28
|
+
#versionedDataHandler: VersionedDataHandler<TBare>;
|
|
29
|
+
#version: number;
|
|
30
|
+
#zodSchema: z.ZodType<TJson>;
|
|
31
|
+
#toJson: (value: T) => TJson;
|
|
32
|
+
#toBare: (value: T) => TBare;
|
|
33
|
+
|
|
34
|
+
constructor(
|
|
35
|
+
data: T,
|
|
36
|
+
versionedDataHandler: VersionedDataHandler<TBare>,
|
|
37
|
+
version: number,
|
|
38
|
+
zodSchema: z.ZodType<TJson>,
|
|
39
|
+
toJson: (value: T) => TJson,
|
|
40
|
+
toBare: (value: T) => TBare,
|
|
41
|
+
) {
|
|
42
|
+
this.#data = data;
|
|
43
|
+
this.#versionedDataHandler = versionedDataHandler;
|
|
44
|
+
this.#version = version;
|
|
45
|
+
this.#zodSchema = zodSchema;
|
|
46
|
+
this.#toJson = toJson;
|
|
47
|
+
this.#toBare = toBare;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public get rawData(): T {
|
|
51
|
+
return this.#data;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public serialize(encoding: Encoding): OutputData {
|
|
55
|
+
const cached = this.#cache.get(encoding);
|
|
56
|
+
if (cached) {
|
|
57
|
+
return cached;
|
|
58
|
+
} else {
|
|
59
|
+
const serialized = serializeWithEncoding(
|
|
60
|
+
encoding,
|
|
61
|
+
this.#data,
|
|
62
|
+
this.#versionedDataHandler,
|
|
63
|
+
this.#version,
|
|
64
|
+
this.#zodSchema,
|
|
65
|
+
this.#toJson,
|
|
66
|
+
this.#toBare,
|
|
67
|
+
);
|
|
68
|
+
this.#cache.set(encoding, serialized);
|
|
69
|
+
return serialized;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
///**
|
|
75
|
+
// * Use `CachedSerializer` if serializing the same data repeatedly.
|
|
76
|
+
// */
|
|
77
|
+
//export function serialize<T>(value: T, encoding: Encoding): OutputData {
|
|
78
|
+
// if (encoding === "json") {
|
|
79
|
+
// return JSON.stringify(value);
|
|
80
|
+
// } else if (encoding === "cbor") {
|
|
81
|
+
// // TODO: Remove this hack, but cbor-x can't handle anything extra in data structures
|
|
82
|
+
// const cleanValue = JSON.parse(JSON.stringify(value));
|
|
83
|
+
// return cbor.encode(cleanValue);
|
|
84
|
+
// } else {
|
|
85
|
+
// assertUnreachable(encoding);
|
|
86
|
+
// }
|
|
87
|
+
//}
|
|
88
|
+
//
|
|
89
|
+
//export async function deserialize(data: InputData, encoding: Encoding) {
|
|
90
|
+
// if (encoding === "json") {
|
|
91
|
+
// if (typeof data !== "string") {
|
|
92
|
+
// logger().warn("received non-string for json parse");
|
|
93
|
+
// throw new errors.MalformedMessage();
|
|
94
|
+
// } else {
|
|
95
|
+
// return JSON.parse(data);
|
|
96
|
+
// }
|
|
97
|
+
// } else if (encoding === "cbor") {
|
|
98
|
+
// if (data instanceof Blob) {
|
|
99
|
+
// const arrayBuffer = await data.arrayBuffer();
|
|
100
|
+
// return cbor.decode(new Uint8Array(arrayBuffer));
|
|
101
|
+
// } else if (data instanceof Uint8Array) {
|
|
102
|
+
// return cbor.decode(data);
|
|
103
|
+
// } else if (
|
|
104
|
+
// data instanceof ArrayBuffer ||
|
|
105
|
+
// data instanceof SharedArrayBuffer
|
|
106
|
+
// ) {
|
|
107
|
+
// return cbor.decode(new Uint8Array(data));
|
|
108
|
+
// } else {
|
|
109
|
+
// logger().warn("received non-binary type for cbor parse");
|
|
110
|
+
// throw new errors.MalformedMessage();
|
|
111
|
+
// }
|
|
112
|
+
// } else {
|
|
113
|
+
// assertUnreachable(encoding);
|
|
114
|
+
// }
|
|
115
|
+
//}
|
|
116
|
+
|
|
117
|
+
// TODO: Encode base 128
|
|
118
|
+
function base64EncodeUint8Array(uint8Array: Uint8Array): string {
|
|
119
|
+
let binary = "";
|
|
120
|
+
const len = uint8Array.byteLength;
|
|
121
|
+
for (let i = 0; i < len; i++) {
|
|
122
|
+
binary += String.fromCharCode(uint8Array[i]);
|
|
123
|
+
}
|
|
124
|
+
return btoa(binary);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function base64EncodeArrayBuffer(arrayBuffer: ArrayBuffer): string {
|
|
128
|
+
const uint8Array = new Uint8Array(arrayBuffer);
|
|
129
|
+
return base64EncodeUint8Array(uint8Array);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Converts data that was encoded to a string. Some formats (like SSE) don't support raw binary data. */
|
|
133
|
+
export function encodeDataToString(message: OutputData): string {
|
|
134
|
+
if (typeof message === "string") {
|
|
135
|
+
return message;
|
|
136
|
+
} else if (message instanceof ArrayBuffer) {
|
|
137
|
+
return base64EncodeArrayBuffer(message);
|
|
138
|
+
} else if (message instanceof Uint8Array) {
|
|
139
|
+
return base64EncodeUint8Array(message);
|
|
140
|
+
} else {
|
|
141
|
+
assertUnreachable(message);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function base64DecodeToUint8Array(base64: string): Uint8Array {
|
|
146
|
+
// Check if Buffer is available (Node.js)
|
|
147
|
+
if (typeof Buffer !== "undefined") {
|
|
148
|
+
return new Uint8Array(Buffer.from(base64, "base64"));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Browser environment - use atob
|
|
152
|
+
const binary = atob(base64);
|
|
153
|
+
const len = binary.length;
|
|
154
|
+
const bytes = new Uint8Array(len);
|
|
155
|
+
for (let i = 0; i < len; i++) {
|
|
156
|
+
bytes[i] = binary.charCodeAt(i);
|
|
157
|
+
}
|
|
158
|
+
return bytes;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function base64DecodeToArrayBuffer(base64: string): ArrayBuffer {
|
|
162
|
+
return base64DecodeToUint8Array(base64).buffer as ArrayBuffer;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Stringifies with compat for values that BARE & CBOR supports. */
|
|
166
|
+
export function jsonStringifyCompat(input: any): string {
|
|
167
|
+
return JSON.stringify(input, (_key, value) => {
|
|
168
|
+
if (typeof value === "bigint") {
|
|
169
|
+
return ["$BigInt", value.toString()];
|
|
170
|
+
} else if (value instanceof ArrayBuffer) {
|
|
171
|
+
return ["$ArrayBuffer", base64EncodeArrayBuffer(value)];
|
|
172
|
+
} else if (value instanceof Uint8Array) {
|
|
173
|
+
return ["$Uint8Array", base64EncodeUint8Array(value)];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Escape user arrays that start with $ by prepending another $
|
|
177
|
+
if (
|
|
178
|
+
Array.isArray(value) &&
|
|
179
|
+
value.length === 2 &&
|
|
180
|
+
typeof value[0] === "string" &&
|
|
181
|
+
value[0].startsWith("$")
|
|
182
|
+
) {
|
|
183
|
+
return ["$" + value[0], value[1]];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return value;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Parses JSON with compat for values that BARE & CBOR supports. */
|
|
191
|
+
export function jsonParseCompat(input: string): any {
|
|
192
|
+
return JSON.parse(input, (_key, value) => {
|
|
193
|
+
// Handle arrays with $ prefix
|
|
194
|
+
if (
|
|
195
|
+
Array.isArray(value) &&
|
|
196
|
+
value.length === 2 &&
|
|
197
|
+
typeof value[0] === "string" &&
|
|
198
|
+
value[0].startsWith("$")
|
|
199
|
+
) {
|
|
200
|
+
// Known special types
|
|
201
|
+
if (value[0] === "$BigInt") {
|
|
202
|
+
return BigInt(value[1]);
|
|
203
|
+
} else if (value[0] === "$ArrayBuffer") {
|
|
204
|
+
return base64DecodeToArrayBuffer(value[1]);
|
|
205
|
+
} else if (value[0] === "$Uint8Array") {
|
|
206
|
+
return base64DecodeToUint8Array(value[1]);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Unescape user arrays that started with $ ($$foo -> $foo)
|
|
210
|
+
if (value[0].startsWith("$$")) {
|
|
211
|
+
return [value[0].substring(1), value[1]];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Unknown type starting with $ - this is an error
|
|
215
|
+
throw new Error(
|
|
216
|
+
`Unknown JSON encoding type: ${value[0]}. This may indicate corrupted data or a version mismatch.`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return value;
|
|
221
|
+
});
|
|
222
|
+
}
|