rivetkit 0.0.0-main.41efcce → 0.0.0-main.49693dc
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/dist/browser/client.d.ts +939 -104
- package/dist/browser/client.js +1867 -900
- package/dist/browser/client.js.map +1 -1
- package/dist/browser/inspector/client.d.ts +252 -129
- package/dist/browser/inspector/client.js +2079 -233
- package/dist/browser/inspector/client.js.map +1 -1
- package/dist/tsup/actor/errors.cjs +6 -2
- package/dist/tsup/actor/errors.cjs.map +1 -1
- package/dist/tsup/actor/errors.d.cts +1 -74
- package/dist/tsup/actor/errors.d.ts +1 -74
- package/dist/tsup/actor/errors.js +5 -1
- package/dist/tsup/agent-os/index.cjs +2408 -2029
- package/dist/tsup/agent-os/index.cjs.map +1 -1
- package/dist/tsup/agent-os/index.d.cts +932 -114
- package/dist/tsup/agent-os/index.d.ts +932 -114
- package/dist/tsup/agent-os/index.js +2412 -2033
- package/dist/tsup/agent-os/index.js.map +1 -1
- package/dist/tsup/{chunk-J27CEW6F.js → chunk-2OBVQZX6.js} +6 -6
- package/dist/tsup/chunk-2OBVQZX6.js.map +1 -0
- package/dist/tsup/chunk-3ZWHWMFJ.js +14 -0
- package/dist/tsup/chunk-3ZWHWMFJ.js.map +1 -0
- package/dist/tsup/{chunk-AK57AL7S.cjs → chunk-4KGXFC6M.cjs} +238 -22
- package/dist/tsup/chunk-4KGXFC6M.cjs.map +1 -0
- package/dist/tsup/{chunk-CVHUXINE.js → chunk-5IU3MI7X.js} +594 -72
- package/dist/tsup/chunk-5IU3MI7X.js.map +1 -0
- package/dist/tsup/{chunk-VD7AZIBR.js → chunk-6CXQMHZR.js} +1161 -361
- package/dist/tsup/chunk-6CXQMHZR.js.map +1 -0
- package/dist/tsup/{chunk-SBEFJ4IQ.js → chunk-6W2UIH3X.js} +70 -8
- package/dist/tsup/chunk-6W2UIH3X.js.map +1 -0
- package/dist/tsup/{chunk-7MRGNVSR.js → chunk-6W5VGLFT.js} +237 -21
- package/dist/tsup/chunk-6W5VGLFT.js.map +1 -0
- package/dist/tsup/{chunk-RF3MEJV6.cjs → chunk-B73TBMMG.cjs} +18 -11
- package/dist/tsup/chunk-B73TBMMG.cjs.map +1 -0
- package/dist/tsup/chunk-D6TORRA4.js +243 -0
- package/dist/tsup/chunk-D6TORRA4.js.map +1 -0
- package/dist/tsup/{chunk-PXLJOMJD.js → chunk-FXT3HN32.js} +2 -2
- package/dist/tsup/{chunk-ZGWSH7YV.js → chunk-IXBD7BXC.js} +48 -72
- package/dist/tsup/chunk-IXBD7BXC.js.map +1 -0
- package/dist/tsup/{chunk-3BFYSYRU.js → chunk-JI6GZ2C2.js} +4 -12
- package/dist/tsup/chunk-JI6GZ2C2.js.map +1 -0
- package/dist/tsup/{chunk-PCBNKI2J.js → chunk-JTHHCZCZ.js} +24 -1
- package/dist/tsup/chunk-JTHHCZCZ.js.map +1 -0
- package/dist/tsup/{chunk-2NDZ7JCR.cjs → chunk-KVFIONKQ.cjs} +25 -2
- package/dist/tsup/chunk-KVFIONKQ.cjs.map +1 -0
- package/dist/tsup/{chunk-SPMMF6TR.cjs → chunk-LGOWPZJ5.cjs} +3 -3
- package/dist/tsup/chunk-LGOWPZJ5.cjs.map +1 -0
- package/dist/tsup/chunk-LYNWTX6R.cjs +14 -0
- package/dist/tsup/chunk-LYNWTX6R.cjs.map +1 -0
- package/dist/tsup/{chunk-LLD2D5LO.cjs → chunk-MYWZCDML.cjs} +4 -12
- package/dist/tsup/chunk-MYWZCDML.cjs.map +1 -0
- package/dist/tsup/{chunk-XSC2WWXD.js → chunk-NYDZLWHZ.js} +16 -9
- package/dist/tsup/chunk-NYDZLWHZ.js.map +1 -0
- package/dist/tsup/{chunk-AV54CVAG.cjs → chunk-RB7WUDX6.cjs} +1290 -490
- package/dist/tsup/chunk-RB7WUDX6.cjs.map +1 -0
- package/dist/tsup/{chunk-JTL6Y2UN.cjs → chunk-SPJORZMM.cjs} +49 -73
- package/dist/tsup/chunk-SPJORZMM.cjs.map +1 -0
- package/dist/tsup/chunk-SSMKIYEX.cjs +243 -0
- package/dist/tsup/chunk-SSMKIYEX.cjs.map +1 -0
- package/dist/tsup/{chunk-TQSL4NJ3.cjs → chunk-TQ4HSX2U.cjs} +72 -10
- package/dist/tsup/chunk-TQ4HSX2U.cjs.map +1 -0
- package/dist/tsup/{chunk-EVADNK6Z.cjs → chunk-U6ZQELGJ.cjs} +10 -10
- package/dist/tsup/chunk-U6ZQELGJ.cjs.map +1 -0
- package/dist/tsup/chunk-XLBMKZD3.cjs +1257 -0
- package/dist/tsup/chunk-XLBMKZD3.cjs.map +1 -0
- package/dist/tsup/client/mod.cjs +8 -9
- package/dist/tsup/client/mod.cjs.map +1 -1
- package/dist/tsup/client/mod.d.cts +18 -7
- package/dist/tsup/client/mod.d.ts +18 -7
- package/dist/tsup/client/mod.js +7 -8
- package/dist/tsup/common/log.cjs +5 -3
- package/dist/tsup/common/log.cjs.map +1 -1
- package/dist/tsup/common/log.d.cts +5 -1
- package/dist/tsup/common/log.d.ts +5 -1
- package/dist/tsup/common/log.js +6 -4
- package/dist/tsup/common/websocket.cjs +4 -4
- package/dist/tsup/common/websocket.cjs.map +1 -1
- package/dist/tsup/common/websocket.js +3 -3
- package/dist/tsup/{config-B3RIidaJ.d.ts → config-BXSV17G7.d.cts} +987 -72
- package/dist/tsup/{config-D6WxBYjV.d.cts → config-Bi1r6gI7.d.ts} +987 -72
- package/dist/tsup/{config-B9qUIHX4.d.ts → config-ey2I2zz5.d.cts} +82 -15
- package/dist/tsup/{config-B9qUIHX4.d.cts → config-ey2I2zz5.d.ts} +82 -15
- package/dist/tsup/context-4fJemY0D.d.cts +129 -0
- package/dist/tsup/context-D4sNYboo.d.ts +129 -0
- package/dist/tsup/db/drizzle.cjs +168 -103
- package/dist/tsup/db/drizzle.cjs.map +1 -1
- package/dist/tsup/db/drizzle.d.cts +8 -4
- package/dist/tsup/db/drizzle.d.ts +8 -4
- package/dist/tsup/db/drizzle.js +170 -105
- package/dist/tsup/db/drizzle.js.map +1 -1
- package/dist/tsup/db/mod.cjs +6 -163
- package/dist/tsup/db/mod.cjs.map +1 -1
- package/dist/tsup/db/mod.d.cts +12 -4
- package/dist/tsup/db/mod.d.ts +12 -4
- package/dist/tsup/db/mod.js +5 -162
- package/dist/tsup/db/mod.js.map +1 -1
- package/dist/tsup/dynamic/mod.cjs +24 -0
- package/dist/tsup/dynamic/mod.cjs.map +1 -0
- package/dist/tsup/dynamic/mod.d.cts +38 -0
- package/dist/tsup/dynamic/mod.d.ts +38 -0
- package/dist/tsup/dynamic/mod.js +24 -0
- package/dist/tsup/dynamic/mod.js.map +1 -0
- package/dist/tsup/inspector/mod.cjs +47 -12
- package/dist/tsup/inspector/mod.cjs.map +1 -1
- package/dist/tsup/inspector/mod.d.cts +26 -136
- package/dist/tsup/inspector/mod.d.ts +26 -136
- package/dist/tsup/inspector/mod.js +46 -11
- package/dist/tsup/inspector/mod.js.map +1 -1
- package/dist/tsup/inspector/workflow.cjs +21 -0
- package/dist/tsup/inspector/workflow.cjs.map +1 -0
- package/dist/tsup/inspector/workflow.d.cts +144 -0
- package/dist/tsup/inspector/workflow.d.ts +144 -0
- package/dist/tsup/inspector/workflow.js +21 -0
- package/dist/tsup/inspector/workflow.js.map +1 -0
- package/dist/tsup/inspector-tab/mod.cjs +162 -0
- package/dist/tsup/inspector-tab/mod.cjs.map +1 -0
- package/dist/tsup/inspector-tab/mod.d.cts +241 -0
- package/dist/tsup/inspector-tab/mod.d.ts +241 -0
- package/dist/tsup/inspector-tab/mod.js +162 -0
- package/dist/tsup/inspector-tab/mod.js.map +1 -0
- package/dist/tsup/mod.cjs +4268 -944
- package/dist/tsup/mod.cjs.map +1 -1
- package/dist/tsup/mod.d.cts +9 -8
- package/dist/tsup/mod.d.ts +9 -8
- package/dist/tsup/mod.js +4229 -905
- package/dist/tsup/mod.js.map +1 -1
- package/dist/tsup/test/mod.cjs +26 -21
- package/dist/tsup/test/mod.cjs.map +1 -1
- package/dist/tsup/test/mod.d.cts +7 -6
- package/dist/tsup/test/mod.d.ts +7 -6
- package/dist/tsup/test/mod.js +25 -20
- package/dist/tsup/test/mod.js.map +1 -1
- package/dist/tsup/unstable/migrations.cjs +101 -0
- package/dist/tsup/unstable/migrations.cjs.map +1 -0
- package/dist/tsup/unstable/migrations.d.cts +29 -0
- package/dist/tsup/unstable/migrations.d.ts +29 -0
- package/dist/tsup/unstable/migrations.js +101 -0
- package/dist/tsup/unstable/migrations.js.map +1 -0
- package/dist/tsup/utils-Bbz-3411.d.cts +162 -0
- package/dist/tsup/utils-BqdWVlzm.d.ts +162 -0
- package/dist/tsup/utils-vvJQyygr.d.cts +109 -0
- package/dist/tsup/utils-vvJQyygr.d.ts +109 -0
- package/dist/tsup/utils.cjs +7 -3
- package/dist/tsup/utils.cjs.map +1 -1
- package/dist/tsup/utils.d.cts +4 -147
- package/dist/tsup/utils.d.ts +4 -147
- package/dist/tsup/utils.js +6 -2
- package/dist/tsup/workflow/mod.cjs +517 -412
- package/dist/tsup/workflow/mod.cjs.map +1 -1
- package/dist/tsup/workflow/mod.d.cts +11 -10
- package/dist/tsup/workflow/mod.d.ts +11 -10
- package/dist/tsup/workflow/mod.js +511 -406
- package/dist/tsup/workflow/mod.js.map +1 -1
- package/package.json +62 -14
- package/src/actor/actions.ts +119 -0
- package/src/actor/config.test.ts +78 -0
- package/src/actor/config.ts +518 -96
- package/src/actor/contexts/index.ts +7 -2
- package/src/actor/definition.ts +38 -26
- package/src/actor/errors.ts +101 -89
- package/src/actor/keys.ts +2 -1
- package/src/actor/mod.ts +32 -21
- package/src/actor/schema.ts +2 -2
- package/src/agent-os/actor/cron.ts +37 -2
- package/src/agent-os/actor/index.ts +39 -19
- package/src/agent-os/actor/preview.ts +1 -2
- package/src/agent-os/actor/session.ts +2 -2
- package/src/agent-os/config.ts +1 -1
- package/src/agent-os/fs/database-vfs.ts +1 -1
- package/src/agent-os/index.ts +16 -15
- package/src/agent-os/types.ts +11 -0
- package/src/client/actor-common.ts +106 -53
- package/src/client/actor-conn.ts +94 -45
- package/src/client/actor-handle.ts +198 -79
- package/src/client/actor-query.ts +13 -6
- package/src/client/client.ts +43 -15
- package/src/client/config.ts +8 -0
- package/src/client/errors.ts +4 -2
- package/src/client/lifecycle-errors.ts +28 -4
- package/src/client/mod.ts +9 -4
- package/src/client/query.ts +3 -1
- package/src/client/queue.ts +8 -3
- package/src/client/raw-utils.ts +24 -16
- package/src/client/resolve-gateway-target.ts +3 -1
- package/src/client/utils.ts +36 -10
- package/src/common/actor-router-consts.ts +7 -0
- package/src/common/actor-websocket.ts +3 -1
- package/src/common/bare/actor-persist/v1.ts +205 -163
- package/src/common/bare/actor-persist/v2.ts +265 -213
- package/src/common/bare/actor-persist/v3.ts +176 -172
- package/src/common/bare/actor-persist/v4.ts +254 -253
- package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +848 -0
- package/src/common/bare/generated/client-protocol/v4.ts +599 -0
- package/src/common/bare/generated/inspector/v5.ts +1016 -0
- package/src/common/bare/generated/inspector/v6.ts +1367 -0
- package/src/common/bare/transport/v1.ts +694 -542
- package/src/common/client-protocol-versioned.ts +176 -67
- package/src/common/client-protocol-zod.ts +7 -0
- package/src/common/client-protocol.ts +1 -1
- package/src/common/database/config.ts +103 -10
- package/src/common/database/mod.test.ts +364 -0
- package/src/common/database/mod.ts +278 -149
- package/src/common/database/native-database.test.ts +220 -0
- package/src/common/database/native-database.ts +394 -58
- package/src/common/database/shared.ts +25 -0
- package/src/common/encoding.ts +302 -38
- package/src/common/engine.ts +28 -1
- package/src/common/eventsource.ts +1 -1
- package/src/common/fetch-like.ts +49 -0
- package/src/common/inline-websocket-adapter.ts +14 -13
- package/src/common/inspector-transport.ts +12 -2
- package/src/common/log.ts +117 -14
- package/src/common/router.ts +49 -23
- package/src/common/utils.ts +23 -203
- package/src/common/websocket-interface.ts +1 -1
- package/src/db/drizzle.test.ts +164 -0
- package/src/db/drizzle.ts +207 -121
- package/src/db/mod.ts +6 -1
- package/src/devtools-loader/index.ts +4 -7
- package/src/devtools-loader/serve-devtools.ts +26 -0
- package/src/dynamic/instance.ts +32 -0
- package/src/dynamic/internal.ts +50 -0
- package/src/dynamic/isolate-runtime.ts +64 -0
- package/src/dynamic/mod.ts +32 -0
- package/src/engine-client/actor-http-client.ts +23 -4
- package/src/engine-client/actor-websocket-client.ts +32 -5
- package/src/engine-client/api-endpoints.ts +52 -2
- package/src/engine-client/api-utils.ts +2 -2
- package/src/engine-client/driver.ts +21 -17
- package/src/engine-client/mod.ts +116 -64
- package/src/engine-client/ws-proxy.ts +9 -4
- package/src/globals.d.ts +4 -0
- package/src/inspector/client.browser.ts +127 -15
- package/src/inspector/mod.ts +2 -4
- package/src/inspector/workflow.ts +61 -0
- package/src/inspector-tab/mod.ts +301 -0
- package/src/registry/config/envoy.ts +1 -2
- package/src/registry/config/index.ts +160 -53
- package/src/registry/config/serverless.ts +7 -1
- package/src/registry/index.ts +659 -96
- package/src/registry/napi-runtime.ts +1154 -0
- package/src/registry/native-validation.ts +10 -12
- package/src/registry/native.ts +2005 -955
- package/src/registry/process-metrics.ts +250 -0
- package/src/registry/run-handler-coordinator.test.ts +252 -0
- package/src/registry/run-handler-coordinator.ts +219 -0
- package/src/registry/runtime.test.ts +50 -0
- package/src/registry/runtime.ts +839 -0
- package/src/registry/wasm-runtime.ts +1283 -0
- package/src/registry/write-through-proxy.ts +141 -0
- package/src/serde.ts +8 -2
- package/src/serverless/configure.ts +102 -49
- package/src/test/mod.ts +2 -17
- package/src/test/ready.ts +22 -0
- package/src/unstable/migrations.test.ts +170 -0
- package/src/unstable/migrations.ts +141 -0
- package/src/utils/crypto.ts +14 -0
- package/src/utils/endpoint-parser.ts +1 -1
- package/src/utils/env-vars.ts +39 -0
- package/src/utils/node.ts +44 -28
- package/src/utils/router.ts +1 -1
- package/src/utils.ts +4 -2
- package/src/workflow/context.ts +683 -242
- package/src/workflow/driver.test.ts +62 -0
- package/src/workflow/driver.ts +246 -119
- package/src/workflow/inspector.ts +24 -24
- package/src/workflow/mod.ts +76 -84
- package/dist/tsup/chunk-2NDZ7JCR.cjs.map +0 -1
- package/dist/tsup/chunk-3BFYSYRU.js.map +0 -1
- package/dist/tsup/chunk-6AVUJ52Q.cjs +0 -253
- package/dist/tsup/chunk-6AVUJ52Q.cjs.map +0 -1
- package/dist/tsup/chunk-7MRGNVSR.js.map +0 -1
- package/dist/tsup/chunk-7X7RVTFF.cjs +0 -735
- package/dist/tsup/chunk-7X7RVTFF.cjs.map +0 -1
- package/dist/tsup/chunk-AK57AL7S.cjs.map +0 -1
- package/dist/tsup/chunk-AV54CVAG.cjs.map +0 -1
- package/dist/tsup/chunk-CVHUXINE.js.map +0 -1
- package/dist/tsup/chunk-EVADNK6Z.cjs.map +0 -1
- package/dist/tsup/chunk-J27CEW6F.js.map +0 -1
- package/dist/tsup/chunk-JTL6Y2UN.cjs.map +0 -1
- package/dist/tsup/chunk-LLD2D5LO.cjs.map +0 -1
- package/dist/tsup/chunk-PCBNKI2J.js.map +0 -1
- package/dist/tsup/chunk-RF3MEJV6.cjs.map +0 -1
- package/dist/tsup/chunk-SB4MXRN2.js +0 -253
- package/dist/tsup/chunk-SB4MXRN2.js.map +0 -1
- package/dist/tsup/chunk-SBEFJ4IQ.js.map +0 -1
- package/dist/tsup/chunk-SPMMF6TR.cjs.map +0 -1
- package/dist/tsup/chunk-TQSL4NJ3.cjs.map +0 -1
- package/dist/tsup/chunk-VD7AZIBR.js.map +0 -1
- package/dist/tsup/chunk-XSC2WWXD.js.map +0 -1
- package/dist/tsup/chunk-ZGWSH7YV.js.map +0 -1
- package/dist/tsup/context-BxvHh9wi.d.cts +0 -92
- package/dist/tsup/context-CNIZBIKl.d.ts +0 -92
- package/dist/tsup/utils-fwx3o3K9.d.cts +0 -18
- package/dist/tsup/utils-fwx3o3K9.d.ts +0 -18
- package/src/actor/driver.ts +0 -141
- package/src/actor/instance/mod.ts +0 -2501
- package/src/drivers/engine/actor-driver.ts +0 -2470
- package/src/utils/serve.ts +0 -217
- /package/dist/tsup/{chunk-PXLJOMJD.js.map → chunk-FXT3HN32.js.map} +0 -0
package/src/registry/native.ts
CHANGED
|
@@ -1,35 +1,36 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
JsActorConfig,
|
|
3
|
-
JsHttpResponse,
|
|
4
|
-
JsServeConfig,
|
|
5
|
-
ActorContext as NativeActorContext,
|
|
6
|
-
NapiActorFactory as NativeActorFactory,
|
|
7
|
-
CancellationToken as NativeCancellationToken,
|
|
8
|
-
ConnHandle as NativeConnHandle,
|
|
9
|
-
CoreRegistry as NativeCoreRegistry,
|
|
10
|
-
Queue as NativeQueue,
|
|
11
|
-
QueueMessage as NativeQueueMessage,
|
|
12
|
-
Schedule as NativeSchedule,
|
|
13
|
-
StateDeltaPayload as NativeStateDeltaPayload,
|
|
14
|
-
WebSocket as NativeWebSocket,
|
|
15
|
-
} from "@rivetkit/rivetkit-napi";
|
|
16
1
|
import { VirtualWebSocket } from "@rivetkit/virtual-websocket";
|
|
2
|
+
import {
|
|
3
|
+
flattenActionHandlers,
|
|
4
|
+
flattenActionInputSchemas,
|
|
5
|
+
} from "@/actor/actions";
|
|
17
6
|
import {
|
|
18
7
|
ACTOR_CONTEXT_INTERNAL_SYMBOL,
|
|
8
|
+
type ActorCron,
|
|
9
|
+
type ActorCronEveryOptions,
|
|
10
|
+
type ActorCronSetOptions,
|
|
11
|
+
type ActorSchedule,
|
|
19
12
|
CONN_STATE_MANAGER_SYMBOL,
|
|
13
|
+
type CronFire,
|
|
14
|
+
type CronJobInfo,
|
|
15
|
+
disposeRunInspector,
|
|
20
16
|
getRunFunction,
|
|
21
17
|
getRunInspectorConfig,
|
|
18
|
+
getRunInspectorKind,
|
|
19
|
+
hasRunInspectorConfig,
|
|
20
|
+
RAW_STATE_SYMBOL,
|
|
21
|
+
type ScheduledEventInfo,
|
|
22
22
|
type WorkflowInspectorConfig,
|
|
23
23
|
} from "@/actor/config";
|
|
24
24
|
import type { AnyActorDefinition } from "@/actor/definition";
|
|
25
25
|
import {
|
|
26
26
|
decodeBridgeRivetError,
|
|
27
27
|
encodeBridgeRivetError,
|
|
28
|
-
type RivetErrorLike,
|
|
29
28
|
forbiddenError,
|
|
30
29
|
INTERNAL_ERROR_CODE,
|
|
30
|
+
isActorAbortedError,
|
|
31
31
|
isRivetErrorLike,
|
|
32
32
|
RivetError,
|
|
33
|
+
type RivetErrorLike,
|
|
33
34
|
toRivetError,
|
|
34
35
|
} from "@/actor/errors";
|
|
35
36
|
import { makePrefixedKey, removePrefixFromKey } from "@/actor/keys";
|
|
@@ -44,33 +45,23 @@ import {
|
|
|
44
45
|
createClientWithDriver,
|
|
45
46
|
} from "@/client/client";
|
|
46
47
|
import { convertRegistryConfigToClientConfig } from "@/client/config";
|
|
48
|
+
import { HEADER_CONN_PARAMS } from "@/common/actor-router-consts";
|
|
49
|
+
import type {
|
|
50
|
+
AnyDatabaseProvider,
|
|
51
|
+
SqliteProfilingOptions,
|
|
52
|
+
} from "@/common/database/config";
|
|
47
53
|
import {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
import {
|
|
53
|
-
CURRENT_VERSION as CLIENT_PROTOCOL_CURRENT_VERSION,
|
|
54
|
-
HTTP_ACTION_REQUEST_VERSIONED,
|
|
55
|
-
HTTP_ACTION_RESPONSE_VERSIONED,
|
|
56
|
-
HTTP_QUEUE_SEND_REQUEST_VERSIONED,
|
|
57
|
-
HTTP_QUEUE_SEND_RESPONSE_VERSIONED,
|
|
58
|
-
HTTP_RESPONSE_ERROR_VERSIONED,
|
|
59
|
-
} from "@/common/client-protocol-versioned";
|
|
60
|
-
import {
|
|
61
|
-
HttpActionRequestSchema,
|
|
62
|
-
HttpActionResponseSchema,
|
|
63
|
-
type HttpQueueSendRequest as HttpQueueSendRequestJson,
|
|
64
|
-
HttpQueueSendRequestSchema,
|
|
65
|
-
type HttpQueueSendResponse as HttpQueueSendResponseJson,
|
|
66
|
-
HttpQueueSendResponseSchema,
|
|
67
|
-
type HttpResponseError as HttpResponseErrorJson,
|
|
68
|
-
HttpResponseErrorSchema,
|
|
69
|
-
} from "@/common/client-protocol-zod";
|
|
70
|
-
import type { AnyDatabaseProvider } from "@/common/database/config";
|
|
54
|
+
bindNativeStateTransactionContext,
|
|
55
|
+
db as defaultDb,
|
|
56
|
+
registerNativeStateTransactionOpener,
|
|
57
|
+
} from "@/common/database/mod";
|
|
71
58
|
import { wrapJsNativeDatabase } from "@/common/database/native-database";
|
|
72
|
-
import type
|
|
73
|
-
import {
|
|
59
|
+
import { assertJsonCompatValue, type JsonCompatValue } from "@/common/encoding";
|
|
60
|
+
import { isResponseLike, type ResponseLike } from "@/common/fetch-like";
|
|
61
|
+
import {
|
|
62
|
+
decodeWorkflowHistoryTransport,
|
|
63
|
+
type WorkflowHistoryBytes,
|
|
64
|
+
} from "@/common/inspector-transport";
|
|
74
65
|
import { deconstructError, stringifyError } from "@/common/utils";
|
|
75
66
|
import type {
|
|
76
67
|
RivetCloseEvent,
|
|
@@ -80,16 +71,20 @@ import type {
|
|
|
80
71
|
} from "@/common/websocket-interface";
|
|
81
72
|
import { RemoteEngineControlClient } from "@/engine-client/mod";
|
|
82
73
|
import type { Registry } from "@/registry";
|
|
83
|
-
import type {
|
|
74
|
+
import type {
|
|
75
|
+
RegistryConfig,
|
|
76
|
+
RuntimeKind,
|
|
77
|
+
SqliteBackend,
|
|
78
|
+
} from "@/registry/config";
|
|
79
|
+
import { decodeCborCompat, encodeCborCompat } from "@/serde";
|
|
80
|
+
import { getEnvUniversal, toUint8Array, VERSION } from "@/utils";
|
|
84
81
|
import {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
serializeWithEncoding,
|
|
90
|
-
} from "@/serde";
|
|
91
|
-
import { bufferToArrayBuffer, VERSION } from "@/utils";
|
|
82
|
+
getNodeFsSync,
|
|
83
|
+
getNodePath,
|
|
84
|
+
importNodeDependencies,
|
|
85
|
+
} from "@/utils/node";
|
|
92
86
|
import { logger } from "./log";
|
|
87
|
+
import { loadNapiRuntime } from "./napi-runtime";
|
|
93
88
|
import {
|
|
94
89
|
type NativeValidationConfig,
|
|
95
90
|
validateActionArgs,
|
|
@@ -98,38 +93,214 @@ import {
|
|
|
98
93
|
validateQueueBody,
|
|
99
94
|
validateQueueComplete,
|
|
100
95
|
} from "./native-validation";
|
|
96
|
+
import { RunHandlerCoordinator } from "./run-handler-coordinator";
|
|
97
|
+
import type {
|
|
98
|
+
ActorContextHandle,
|
|
99
|
+
ActorFactoryHandle,
|
|
100
|
+
CancellationTokenHandle,
|
|
101
|
+
ConnHandle,
|
|
102
|
+
CoreRuntime,
|
|
103
|
+
RegistryHandle,
|
|
104
|
+
RuntimeActorConfig,
|
|
105
|
+
RuntimeBytes,
|
|
106
|
+
RuntimeCronFire,
|
|
107
|
+
RuntimeCronJobInfo,
|
|
108
|
+
RuntimeHttpResponse,
|
|
109
|
+
RuntimeInspectorTabEntry,
|
|
110
|
+
RuntimeQueueMessage,
|
|
111
|
+
RuntimeScheduledEventInfo,
|
|
112
|
+
RuntimeScheduledFireInfo,
|
|
113
|
+
RuntimeServeConfig,
|
|
114
|
+
RuntimeStateDeltaPayload,
|
|
115
|
+
WebSocketHandle,
|
|
116
|
+
} from "./runtime";
|
|
117
|
+
import { loadWasmRuntime } from "./wasm-runtime";
|
|
118
|
+
import {
|
|
119
|
+
createWriteThroughProxy,
|
|
120
|
+
unwrapWriteThroughProxy,
|
|
121
|
+
} from "./write-through-proxy";
|
|
101
122
|
|
|
102
|
-
type NativeBindings = typeof import("@rivetkit/rivetkit-napi");
|
|
103
|
-
type NativeWebSocketEvent =
|
|
104
|
-
| {
|
|
105
|
-
kind: "message";
|
|
106
|
-
data: string | Buffer;
|
|
107
|
-
binary: boolean;
|
|
108
|
-
messageIndex?: number;
|
|
109
|
-
}
|
|
110
|
-
| {
|
|
111
|
-
kind: "close";
|
|
112
|
-
code: number;
|
|
113
|
-
reason: string;
|
|
114
|
-
wasClean: boolean;
|
|
115
|
-
};
|
|
116
|
-
type NativeWebSocketWithEvents = NativeWebSocket & {
|
|
117
|
-
setEventCallback: (callback: (event: NativeWebSocketEvent) => void) => void;
|
|
118
|
-
};
|
|
119
123
|
const textEncoder = new TextEncoder();
|
|
120
124
|
const textDecoder = new TextDecoder();
|
|
125
|
+
|
|
126
|
+
type ResolvedRuntimeKind = Exclude<RuntimeKind, "auto">;
|
|
127
|
+
type RuntimeHostKind = "node-like" | "edge-like";
|
|
128
|
+
export type RuntimeLoaders = {
|
|
129
|
+
loadNative: () => ReturnType<typeof loadNapiRuntime>;
|
|
130
|
+
loadWasm: (
|
|
131
|
+
config?: RegistryConfig["wasm"],
|
|
132
|
+
) => ReturnType<typeof loadWasmRuntime>;
|
|
133
|
+
detectHost: () => RuntimeHostKind;
|
|
134
|
+
};
|
|
135
|
+
|
|
121
136
|
type SerializeStateReason = "save" | "inspector";
|
|
122
137
|
type NativeOnStateChangeHandler = (
|
|
123
|
-
ctx:
|
|
138
|
+
ctx: ActorContextHandleAdapter,
|
|
124
139
|
state: unknown,
|
|
125
140
|
) => void | Promise<void>;
|
|
126
141
|
type NativePersistConnState = {
|
|
127
142
|
state: unknown;
|
|
128
143
|
};
|
|
144
|
+
|
|
145
|
+
type NativeStateTransactionScope = {
|
|
146
|
+
actorContext: ActorContextHandleAdapter;
|
|
147
|
+
actorStateBaseline: unknown;
|
|
148
|
+
connectionStateBaselines: Map<string, Uint8Array>;
|
|
149
|
+
committed: boolean;
|
|
150
|
+
release: () => void;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const defaultRuntimeLoaders: RuntimeLoaders = {
|
|
154
|
+
loadNative: loadNapiRuntime,
|
|
155
|
+
loadWasm: loadWasmRuntime,
|
|
156
|
+
detectHost: detectRuntimeHost,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
function trySetProcessEnv(key: string, value: string) {
|
|
160
|
+
if (typeof process === "undefined") return;
|
|
161
|
+
try {
|
|
162
|
+
process.env[key] = value;
|
|
163
|
+
} catch {
|
|
164
|
+
// Some edge runtimes expose a read-only Node-compatible process.env.
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function detectRuntimeHost(): RuntimeHostKind {
|
|
169
|
+
const globalScope = globalThis as typeof globalThis & {
|
|
170
|
+
Bun?: unknown;
|
|
171
|
+
Deno?: unknown;
|
|
172
|
+
process?: { versions?: { node?: string } };
|
|
173
|
+
self?: unknown;
|
|
174
|
+
window?: unknown;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
if (
|
|
178
|
+
globalScope.Deno !== undefined ||
|
|
179
|
+
globalScope.Bun !== undefined ||
|
|
180
|
+
typeof globalScope.process?.versions?.node === "string"
|
|
181
|
+
) {
|
|
182
|
+
return "node-like";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return "edge-like";
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export function resolveRuntimeKind(
|
|
189
|
+
runtime: RuntimeKind | undefined,
|
|
190
|
+
): RuntimeKind {
|
|
191
|
+
return runtime ?? "auto";
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function loadedRuntimeKind(runtime: CoreRuntime): ResolvedRuntimeKind {
|
|
195
|
+
switch (runtime.kind) {
|
|
196
|
+
case "napi":
|
|
197
|
+
return "native";
|
|
198
|
+
case "wasm":
|
|
199
|
+
return "wasm";
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
throw new RivetError(
|
|
203
|
+
"config",
|
|
204
|
+
"unknown_runtime",
|
|
205
|
+
"RivetKit runtime must be NAPI or wasm.",
|
|
206
|
+
{
|
|
207
|
+
public: true,
|
|
208
|
+
statusCode: 500,
|
|
209
|
+
},
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export async function loadAutoRuntime(
|
|
214
|
+
config: RegistryConfig,
|
|
215
|
+
loaders: RuntimeLoaders = defaultRuntimeLoaders,
|
|
216
|
+
): Promise<CoreRuntime> {
|
|
217
|
+
if (loaders.detectHost() === "edge-like") {
|
|
218
|
+
return (await loaders.loadWasm(config.wasm)).runtime;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
return (await loaders.loadNative()).runtime;
|
|
223
|
+
} catch {
|
|
224
|
+
return (await loaders.loadWasm(config.wasm)).runtime;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export async function loadConfiguredRuntime(
|
|
229
|
+
config: RegistryConfig,
|
|
230
|
+
loaders: RuntimeLoaders = defaultRuntimeLoaders,
|
|
231
|
+
): Promise<CoreRuntime> {
|
|
232
|
+
const requested = resolveRuntimeKind(config.runtime);
|
|
233
|
+
|
|
234
|
+
if (requested === "native") {
|
|
235
|
+
return (await loaders.loadNative()).runtime;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (requested === "wasm") {
|
|
239
|
+
return (await loaders.loadWasm(config.wasm)).runtime;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return loadAutoRuntime(config, loaders);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function sqliteBackendForConfig(
|
|
246
|
+
config: RegistryConfig,
|
|
247
|
+
): SqliteBackend | undefined {
|
|
248
|
+
return config.sqlite?.backend ?? config.test?.sqliteBackend;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function normalizeRuntimeConfigForKind(
|
|
252
|
+
config: RegistryConfig,
|
|
253
|
+
runtimeKind: ResolvedRuntimeKind,
|
|
254
|
+
): RegistryConfig {
|
|
255
|
+
if (runtimeKind === "native") {
|
|
256
|
+
return config;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (sqliteBackendForConfig(config) === "local") {
|
|
260
|
+
throw new RivetError(
|
|
261
|
+
"config",
|
|
262
|
+
"wasm_local_sqlite",
|
|
263
|
+
"WebAssembly runtime cannot use local SQLite. Use remote SQLite instead.",
|
|
264
|
+
{
|
|
265
|
+
public: true,
|
|
266
|
+
statusCode: 400,
|
|
267
|
+
metadata: { runtime: "wasm", sqliteBackend: "local" },
|
|
268
|
+
},
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
...config,
|
|
274
|
+
sqlite: {
|
|
275
|
+
...config.sqlite,
|
|
276
|
+
backend: "remote",
|
|
277
|
+
},
|
|
278
|
+
test: {
|
|
279
|
+
...config.test,
|
|
280
|
+
enabled: config.test?.enabled ?? false,
|
|
281
|
+
sqliteBackend: "remote",
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function normalizeRuntimeConfig(
|
|
287
|
+
config: RegistryConfig,
|
|
288
|
+
runtime: CoreRuntime,
|
|
289
|
+
): RegistryConfig {
|
|
290
|
+
return normalizeRuntimeConfigForKind(config, loadedRuntimeKind(runtime));
|
|
291
|
+
}
|
|
129
292
|
type NativePersistActorState = {
|
|
130
293
|
state: unknown;
|
|
131
294
|
isInOnStateChange: boolean;
|
|
132
295
|
connStates: Map<string, NativePersistConnState>;
|
|
296
|
+
// Set when a coalesced save and onStateChange flush is pending for the
|
|
297
|
+
// current event loop tick.
|
|
298
|
+
saveScheduled?: boolean;
|
|
299
|
+
pendingSaveHandle?: ReturnType<typeof setImmediate>;
|
|
300
|
+
activeStateTransactionOwner?: symbol;
|
|
301
|
+
pendingStateTransactionOwners?: Set<symbol>;
|
|
302
|
+
stateTransactionTail?: Promise<void>;
|
|
303
|
+
stateTransactionSaveDeferred?: boolean;
|
|
133
304
|
};
|
|
134
305
|
type NativeDestroyGate = {
|
|
135
306
|
destroyCompletion?: Promise<void>;
|
|
@@ -151,10 +322,11 @@ type NativeActorRuntimeState = {
|
|
|
151
322
|
// of actorId-keyed module globals so same-key recreates start from a fresh
|
|
152
323
|
// generation.
|
|
153
324
|
function getNativeRuntimeState(
|
|
154
|
-
|
|
325
|
+
runtime: CoreRuntime,
|
|
326
|
+
ctx: ActorContextHandle,
|
|
155
327
|
): NativeActorRuntimeState {
|
|
156
328
|
const runtimeState = callNativeSync(() =>
|
|
157
|
-
|
|
329
|
+
runtime.actorRuntimeState(ctx),
|
|
158
330
|
) as NativeActorRuntimeState;
|
|
159
331
|
if (!runtimeState.destroyGate) {
|
|
160
332
|
runtimeState.destroyGate = {};
|
|
@@ -170,9 +342,14 @@ function getNativeRuntimeState(
|
|
|
170
342
|
}
|
|
171
343
|
|
|
172
344
|
function getNativePersistState(
|
|
173
|
-
|
|
345
|
+
runtime: CoreRuntime,
|
|
346
|
+
ctx: ActorContextHandle,
|
|
174
347
|
): NativePersistActorState {
|
|
175
|
-
|
|
348
|
+
const persistState = getNativeRuntimeState(runtime, ctx).persistState;
|
|
349
|
+
if (!persistState) {
|
|
350
|
+
throw new Error("native persist state was not initialized");
|
|
351
|
+
}
|
|
352
|
+
return persistState;
|
|
176
353
|
}
|
|
177
354
|
|
|
178
355
|
function isPromiseLike(value: unknown): value is PromiseLike<void> {
|
|
@@ -185,11 +362,12 @@ function isPromiseLike(value: unknown): value is PromiseLike<void> {
|
|
|
185
362
|
}
|
|
186
363
|
|
|
187
364
|
function getNativeConnPersistState(
|
|
188
|
-
|
|
189
|
-
|
|
365
|
+
runtime: CoreRuntime,
|
|
366
|
+
ctx: ActorContextHandle,
|
|
367
|
+
conn: ConnHandle,
|
|
190
368
|
): NativePersistConnState {
|
|
191
|
-
const persistState = getNativePersistState(ctx);
|
|
192
|
-
const connId = callNativeSync(() =>
|
|
369
|
+
const persistState = getNativePersistState(runtime, ctx);
|
|
370
|
+
const connId = callNativeSync(() => runtime.connId(conn));
|
|
193
371
|
let connState = persistState.connStates.get(connId);
|
|
194
372
|
if (!connState) {
|
|
195
373
|
connState = {
|
|
@@ -208,6 +386,15 @@ function stateMutationReentrantError(): RivetError {
|
|
|
208
386
|
);
|
|
209
387
|
}
|
|
210
388
|
|
|
389
|
+
function stateTransactionConflictError(): RivetError {
|
|
390
|
+
return new RivetError(
|
|
391
|
+
"actor",
|
|
392
|
+
"state_transaction_conflict",
|
|
393
|
+
"State cannot be mutated by another action while an experimental state-aware database transaction is active.",
|
|
394
|
+
{ public: true, statusCode: 409 },
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
|
|
211
398
|
function databaseNotConfiguredError(): RivetError {
|
|
212
399
|
return new RivetError(
|
|
213
400
|
"actor",
|
|
@@ -217,6 +404,23 @@ function databaseNotConfiguredError(): RivetError {
|
|
|
217
404
|
);
|
|
218
405
|
}
|
|
219
406
|
|
|
407
|
+
function databaseClientNotReadyError(): RivetError {
|
|
408
|
+
return new RivetError(
|
|
409
|
+
"actor",
|
|
410
|
+
"database_client_not_ready",
|
|
411
|
+
"actor database client was not initialized before user code ran. this is an internal lifecycle error; the migration callback should have pre-warmed the client. file an issue if you can reproduce.",
|
|
412
|
+
{ public: true },
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
function runHandlerNotConfiguredError(): RivetError {
|
|
416
|
+
return new RivetError(
|
|
417
|
+
"actor",
|
|
418
|
+
"run_handler_unavailable",
|
|
419
|
+
"Cannot schedule a run wake because this actor does not define a run handler.",
|
|
420
|
+
{ public: true, statusCode: 409 },
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
|
|
220
424
|
function stateNotEnabledError(): RivetError {
|
|
221
425
|
return new RivetError(
|
|
222
426
|
"actor",
|
|
@@ -244,21 +448,28 @@ function nativeEndpointNotConfiguredError(): RivetError {
|
|
|
244
448
|
);
|
|
245
449
|
}
|
|
246
450
|
|
|
247
|
-
function getNativeDestroyGate(ctx:
|
|
248
|
-
|
|
451
|
+
function getNativeDestroyGate(runtime: CoreRuntime, ctx: ActorContextHandle) {
|
|
452
|
+
const destroyGate = getNativeRuntimeState(runtime, ctx).destroyGate;
|
|
453
|
+
if (!destroyGate) {
|
|
454
|
+
throw new Error("native destroy gate was not initialized");
|
|
455
|
+
}
|
|
456
|
+
return destroyGate;
|
|
249
457
|
}
|
|
250
458
|
|
|
251
|
-
function markNativeDestroyRequested(
|
|
252
|
-
|
|
459
|
+
function markNativeDestroyRequested(
|
|
460
|
+
runtime: CoreRuntime,
|
|
461
|
+
ctx: ActorContextHandle,
|
|
462
|
+
) {
|
|
463
|
+
const gate = getNativeDestroyGate(runtime, ctx);
|
|
253
464
|
if (!gate.destroyCompletion) {
|
|
254
465
|
gate.destroyCompletion = new Promise<void>((resolve) => {
|
|
255
|
-
gate
|
|
466
|
+
gate.resolveDestroy = resolve;
|
|
256
467
|
});
|
|
257
468
|
}
|
|
258
469
|
}
|
|
259
470
|
|
|
260
|
-
function resolveNativeDestroy(ctx:
|
|
261
|
-
const gate = getNativeRuntimeState(ctx).destroyGate;
|
|
471
|
+
function resolveNativeDestroy(runtime: CoreRuntime, ctx: ActorContextHandle) {
|
|
472
|
+
const gate = getNativeRuntimeState(runtime, ctx).destroyGate;
|
|
262
473
|
if (!gate?.resolveDestroy) {
|
|
263
474
|
return;
|
|
264
475
|
}
|
|
@@ -268,10 +479,53 @@ function resolveNativeDestroy(ctx: NativeActorContext) {
|
|
|
268
479
|
gate.destroyCompletion = undefined;
|
|
269
480
|
}
|
|
270
481
|
|
|
482
|
+
function clearNativeRuntimeState(
|
|
483
|
+
runtime: CoreRuntime,
|
|
484
|
+
ctx: ActorContextHandle,
|
|
485
|
+
) {
|
|
486
|
+
callNativeSync(() => runtime.actorClearRuntimeState(ctx));
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
async function cleanupNativeSleepRuntimeState(
|
|
490
|
+
runtime: CoreRuntime,
|
|
491
|
+
ctx: ActorContextHandle,
|
|
492
|
+
afterTrackedWorkDrained?: () => Promise<void>,
|
|
493
|
+
): Promise<void> {
|
|
494
|
+
// The bounded wait gives shutdown work one grace-period chance to finish.
|
|
495
|
+
// Drained means all tracked shutdown work completed before the deadline, so
|
|
496
|
+
// we can save final state and clear runtime state immediately. If it did not
|
|
497
|
+
// drain, close database handles now, then defer the final save and clear until
|
|
498
|
+
// the tracked work finishes without a deadline.
|
|
499
|
+
const drained = await runtime.actorWaitForTrackedShutdownWork(ctx);
|
|
500
|
+
if (!drained) {
|
|
501
|
+
await closeNativeDatabaseClient(runtime, ctx);
|
|
502
|
+
await closeNativeSqlDatabase(runtime, ctx);
|
|
503
|
+
void runtime
|
|
504
|
+
.actorWaitForTrackedShutdownWorkUnbounded(ctx)
|
|
505
|
+
.then(async () => {
|
|
506
|
+
await afterTrackedWorkDrained?.();
|
|
507
|
+
clearNativeRuntimeState(runtime, ctx);
|
|
508
|
+
})
|
|
509
|
+
.catch((error) => {
|
|
510
|
+
logger().warn({
|
|
511
|
+
msg: "deferred native sleep cleanup failed",
|
|
512
|
+
error: stringifyError(error),
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
await afterTrackedWorkDrained?.();
|
|
519
|
+
await closeNativeDatabaseClient(runtime, ctx);
|
|
520
|
+
await closeNativeSqlDatabase(runtime, ctx);
|
|
521
|
+
clearNativeRuntimeState(runtime, ctx);
|
|
522
|
+
}
|
|
523
|
+
|
|
271
524
|
function closeNativeSqlDatabase(
|
|
272
|
-
|
|
525
|
+
runtime: CoreRuntime,
|
|
526
|
+
ctx: ActorContextHandle,
|
|
273
527
|
): Promise<void> | undefined {
|
|
274
|
-
const runtimeState = getNativeRuntimeState(ctx);
|
|
528
|
+
const runtimeState = getNativeRuntimeState(runtime, ctx);
|
|
275
529
|
const database = runtimeState.sql;
|
|
276
530
|
if (!database) {
|
|
277
531
|
return;
|
|
@@ -282,9 +536,10 @@ function closeNativeSqlDatabase(
|
|
|
282
536
|
}
|
|
283
537
|
|
|
284
538
|
async function closeNativeDatabaseClient(
|
|
285
|
-
|
|
539
|
+
runtime: CoreRuntime,
|
|
540
|
+
ctx: ActorContextHandle,
|
|
286
541
|
): Promise<void> {
|
|
287
|
-
const runtimeState = getNativeRuntimeState(ctx);
|
|
542
|
+
const runtimeState = getNativeRuntimeState(runtime, ctx);
|
|
288
543
|
const entry = runtimeState.databaseClient;
|
|
289
544
|
if (!entry) {
|
|
290
545
|
return;
|
|
@@ -303,27 +558,105 @@ async function closeNativeDatabaseClient(
|
|
|
303
558
|
}
|
|
304
559
|
|
|
305
560
|
function getOrCreateNativeSqlDatabase(
|
|
306
|
-
|
|
561
|
+
runtime: CoreRuntime,
|
|
562
|
+
ctx: ActorContextHandle,
|
|
307
563
|
): ReturnType<typeof wrapJsNativeDatabase> {
|
|
308
|
-
const runtimeState = getNativeRuntimeState(ctx);
|
|
564
|
+
const runtimeState = getNativeRuntimeState(runtime, ctx);
|
|
309
565
|
const cachedDatabase = runtimeState.sql;
|
|
310
566
|
if (cachedDatabase) {
|
|
311
567
|
return cachedDatabase;
|
|
312
568
|
}
|
|
313
569
|
|
|
314
|
-
const database = wrapJsNativeDatabase(
|
|
570
|
+
const database = wrapJsNativeDatabase({
|
|
571
|
+
exec: (sql) => runtime.actorSqlExec(ctx, sql),
|
|
572
|
+
execute: (sql, params) => runtime.actorSqlExecute(ctx, sql, params),
|
|
573
|
+
executeBatch: (statements) =>
|
|
574
|
+
runtime.actorSqlExecuteBatch(ctx, statements),
|
|
575
|
+
beginTransaction: async (timeoutMs, name) => {
|
|
576
|
+
const transaction = await runtime.actorSqlBeginTransaction(
|
|
577
|
+
ctx,
|
|
578
|
+
timeoutMs,
|
|
579
|
+
name,
|
|
580
|
+
);
|
|
581
|
+
return {
|
|
582
|
+
exec: (sql) =>
|
|
583
|
+
runtime.actorSqlTransactionExec(transaction, sql),
|
|
584
|
+
execute: (sql, params) =>
|
|
585
|
+
runtime.actorSqlTransactionExecute(
|
|
586
|
+
transaction,
|
|
587
|
+
sql,
|
|
588
|
+
params,
|
|
589
|
+
),
|
|
590
|
+
commit: () => runtime.actorSqlTransactionCommit(transaction),
|
|
591
|
+
rollback: () =>
|
|
592
|
+
runtime.actorSqlTransactionRollback(transaction),
|
|
593
|
+
};
|
|
594
|
+
},
|
|
595
|
+
beginStateTransaction: async (timeoutMs, context) => {
|
|
596
|
+
const scope = context as NativeStateTransactionScope | undefined;
|
|
597
|
+
if (!scope) {
|
|
598
|
+
throw new Error(
|
|
599
|
+
"actor state transaction context is not configured",
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
const transaction = await runtime.actorBeginStateTransaction(
|
|
603
|
+
ctx,
|
|
604
|
+
timeoutMs,
|
|
605
|
+
);
|
|
606
|
+
return {
|
|
607
|
+
exec: async () => {
|
|
608
|
+
throw new Error(
|
|
609
|
+
"actor state transactions only support single-statement execute calls",
|
|
610
|
+
);
|
|
611
|
+
},
|
|
612
|
+
execute: (sql, params) =>
|
|
613
|
+
runtime.actorStateTransactionExecute(
|
|
614
|
+
transaction,
|
|
615
|
+
sql,
|
|
616
|
+
params,
|
|
617
|
+
),
|
|
618
|
+
commit: async () => {
|
|
619
|
+
await runtime.actorStateTransactionCommit(
|
|
620
|
+
transaction,
|
|
621
|
+
scope.actorContext.serializeForTick("save"),
|
|
622
|
+
);
|
|
623
|
+
scope.committed = true;
|
|
624
|
+
},
|
|
625
|
+
rollback: () =>
|
|
626
|
+
runtime.actorStateTransactionRollback(transaction),
|
|
627
|
+
};
|
|
628
|
+
},
|
|
629
|
+
query: (sql, params) => runtime.actorSqlQuery(ctx, sql, params),
|
|
630
|
+
run: (sql, params) => runtime.actorSqlRun(ctx, sql, params),
|
|
631
|
+
metrics: () => runtime.actorSqlMetrics(ctx),
|
|
632
|
+
takeLastKvError: () => runtime.actorSqlTakeLastKvError(ctx),
|
|
633
|
+
close: () => runtime.actorSqlClose(ctx),
|
|
634
|
+
});
|
|
315
635
|
runtimeState.sql = database;
|
|
316
636
|
return database;
|
|
317
637
|
}
|
|
318
638
|
|
|
319
|
-
function
|
|
639
|
+
function toRuntimeBytes(
|
|
640
|
+
value: string | Uint8Array | ArrayBuffer,
|
|
641
|
+
): RuntimeBytes {
|
|
320
642
|
if (typeof value === "string") {
|
|
321
|
-
return
|
|
643
|
+
return textEncoder.encode(value);
|
|
322
644
|
}
|
|
323
645
|
if (value instanceof Uint8Array) {
|
|
324
|
-
return
|
|
646
|
+
return value;
|
|
325
647
|
}
|
|
326
|
-
return
|
|
648
|
+
return new Uint8Array(value);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
function arrayBufferViewToRuntimeBytes(value: ArrayBufferView): RuntimeBytes {
|
|
652
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function runtimeBytesToArrayBuffer(value: RuntimeBytes): ArrayBuffer {
|
|
656
|
+
return value.buffer.slice(
|
|
657
|
+
value.byteOffset,
|
|
658
|
+
value.byteOffset + value.byteLength,
|
|
659
|
+
) as ArrayBuffer;
|
|
327
660
|
}
|
|
328
661
|
|
|
329
662
|
type NativeKvValueType = "text" | "arrayBuffer" | "binary";
|
|
@@ -402,24 +735,43 @@ function decodeNativeKvValue<T extends NativeKvValueType = "text">(
|
|
|
402
735
|
}
|
|
403
736
|
}
|
|
404
737
|
|
|
405
|
-
async function loadNativeBindings(): Promise<NativeBindings> {
|
|
406
|
-
return import(["@rivetkit", "rivetkit-napi"].join("/"));
|
|
407
|
-
}
|
|
408
|
-
|
|
409
738
|
async function loadEngineCli(): Promise<typeof import("@rivetkit/engine-cli")> {
|
|
739
|
+
// LOAD-BEARING: the specifier is computed (`.join("/")`) on purpose. Do NOT
|
|
740
|
+
// "simplify" it to a literal `import("@rivetkit/engine-cli")`. Edge adapters
|
|
741
|
+
// (e.g. @rivetkit/supabase) pre-bundle this module for Deno; a literal
|
|
742
|
+
// dynamic import is statically resolvable, so Deno's eszip bundler would
|
|
743
|
+
// snapshot the ~117 MB engine-cli binary into the deploy and 413. The
|
|
744
|
+
// computed specifier keeps it opaque to static analysis so it is never
|
|
745
|
+
// bundled. Enforced by scripts/ci/check-edge-native-closure.mjs.
|
|
410
746
|
return import(["@rivetkit", "engine-cli"].join("/"));
|
|
411
747
|
}
|
|
412
748
|
|
|
413
|
-
function decodeValue<T>(value?:
|
|
749
|
+
function decodeValue<T>(value?: RuntimeBytes | null): T {
|
|
414
750
|
if (!value || value.length === 0) {
|
|
415
751
|
return undefined as T;
|
|
416
752
|
}
|
|
417
753
|
|
|
418
|
-
return decodeCborCompat(
|
|
754
|
+
return decodeCborCompat(value);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// Rejects `WorkflowHistoryBytes`: the compat layer rewrites an ArrayBuffer as
|
|
758
|
+
// `["$ArrayBuffer", "<base64>"]`, and the inspector wire field carries raw BARE.
|
|
759
|
+
// The conditional distributes, so it catches the branded type on its own but
|
|
760
|
+
// only strips it from a union that also has other members. A non-distributive
|
|
761
|
+
// `[T] extends [...]` resolves to `never` for the `any` arguments this takes
|
|
762
|
+
// elsewhere, so the union case stays uncovered.
|
|
763
|
+
function encodeValue<T>(
|
|
764
|
+
value: T extends WorkflowHistoryBytes ? never : T,
|
|
765
|
+
): RuntimeBytes {
|
|
766
|
+
return encodeCborCompat(value as JsonCompatValue);
|
|
419
767
|
}
|
|
420
768
|
|
|
421
|
-
function
|
|
422
|
-
return
|
|
769
|
+
function normalizeArgs(value: unknown): unknown[] {
|
|
770
|
+
return Array.isArray(value)
|
|
771
|
+
? value
|
|
772
|
+
: value === undefined || value === null
|
|
773
|
+
? []
|
|
774
|
+
: [value];
|
|
423
775
|
}
|
|
424
776
|
|
|
425
777
|
function unwrapTsfnPayload<T>(error: unknown, payload: T): T {
|
|
@@ -431,111 +783,14 @@ function unwrapTsfnPayload<T>(error: unknown, payload: T): T {
|
|
|
431
783
|
}
|
|
432
784
|
|
|
433
785
|
function normalizeNativeBridgeError(error: unknown): unknown {
|
|
434
|
-
const promoteKnownBridgeError = (value: unknown): unknown => {
|
|
435
|
-
if (!isRivetErrorLike(value)) {
|
|
436
|
-
return value;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
if (
|
|
440
|
-
value.group === "auth" &&
|
|
441
|
-
value.code === "forbidden" &&
|
|
442
|
-
(!value.public || value.statusCode === 500)
|
|
443
|
-
) {
|
|
444
|
-
return new RivetError(value.group, value.code, value.message, {
|
|
445
|
-
public: true,
|
|
446
|
-
statusCode: 403,
|
|
447
|
-
metadata: value.metadata,
|
|
448
|
-
cause: value instanceof Error ? value.cause : undefined,
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
if (
|
|
453
|
-
value.group === "actor" &&
|
|
454
|
-
value.code === "action_not_found" &&
|
|
455
|
-
(!value.public || value.statusCode === 500)
|
|
456
|
-
) {
|
|
457
|
-
return new RivetError(value.group, value.code, value.message, {
|
|
458
|
-
public: true,
|
|
459
|
-
statusCode: 404,
|
|
460
|
-
metadata: value.metadata,
|
|
461
|
-
cause: value instanceof Error ? value.cause : undefined,
|
|
462
|
-
});
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
if (
|
|
466
|
-
value.group === "actor" &&
|
|
467
|
-
value.code === "action_timed_out" &&
|
|
468
|
-
(!value.public || value.statusCode === 500)
|
|
469
|
-
) {
|
|
470
|
-
return new RivetError(value.group, value.code, value.message, {
|
|
471
|
-
public: true,
|
|
472
|
-
statusCode: 408,
|
|
473
|
-
metadata: value.metadata,
|
|
474
|
-
cause: value instanceof Error ? value.cause : undefined,
|
|
475
|
-
});
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
if (
|
|
479
|
-
value.group === "actor" &&
|
|
480
|
-
value.code === "aborted" &&
|
|
481
|
-
(!value.public || value.statusCode === 500)
|
|
482
|
-
) {
|
|
483
|
-
return new RivetError(value.group, value.code, value.message, {
|
|
484
|
-
public: true,
|
|
485
|
-
statusCode: 400,
|
|
486
|
-
metadata: value.metadata,
|
|
487
|
-
cause: value instanceof Error ? value.cause : undefined,
|
|
488
|
-
});
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
if (
|
|
492
|
-
value.group === "message" &&
|
|
493
|
-
(value.code === "incoming_too_long" ||
|
|
494
|
-
value.code === "outgoing_too_long") &&
|
|
495
|
-
(!value.public || value.statusCode === 500)
|
|
496
|
-
) {
|
|
497
|
-
return new RivetError(value.group, value.code, value.message, {
|
|
498
|
-
public: true,
|
|
499
|
-
statusCode: 400,
|
|
500
|
-
metadata: value.metadata,
|
|
501
|
-
cause: value instanceof Error ? value.cause : undefined,
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
if (
|
|
506
|
-
value.group === "queue" &&
|
|
507
|
-
[
|
|
508
|
-
"full",
|
|
509
|
-
"message_too_large",
|
|
510
|
-
"message_invalid",
|
|
511
|
-
"invalid_payload",
|
|
512
|
-
"invalid_completion_payload",
|
|
513
|
-
"already_completed",
|
|
514
|
-
"previous_message_not_completed",
|
|
515
|
-
"complete_not_configured",
|
|
516
|
-
"timed_out",
|
|
517
|
-
].includes(value.code) &&
|
|
518
|
-
(!value.public || value.statusCode === 500)
|
|
519
|
-
) {
|
|
520
|
-
return new RivetError(value.group, value.code, value.message, {
|
|
521
|
-
public: true,
|
|
522
|
-
statusCode: 400,
|
|
523
|
-
metadata: value.metadata,
|
|
524
|
-
cause: value instanceof Error ? value.cause : undefined,
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
return value;
|
|
529
|
-
};
|
|
530
|
-
|
|
531
786
|
if (typeof error === "string") {
|
|
532
|
-
return
|
|
787
|
+
return decodeBridgeRivetError(error) ?? error;
|
|
533
788
|
}
|
|
534
789
|
|
|
535
790
|
if (error instanceof Error) {
|
|
536
791
|
const bridged = decodeBridgeRivetError(error.message);
|
|
537
792
|
if (bridged) {
|
|
538
|
-
return
|
|
793
|
+
return bridged;
|
|
539
794
|
}
|
|
540
795
|
}
|
|
541
796
|
|
|
@@ -547,11 +802,11 @@ function normalizeNativeBridgeError(error: unknown): unknown {
|
|
|
547
802
|
) {
|
|
548
803
|
const bridged = decodeBridgeRivetError(error.reason);
|
|
549
804
|
if (bridged) {
|
|
550
|
-
return
|
|
805
|
+
return bridged;
|
|
551
806
|
}
|
|
552
807
|
}
|
|
553
808
|
|
|
554
|
-
return
|
|
809
|
+
return error;
|
|
555
810
|
}
|
|
556
811
|
|
|
557
812
|
function isStructuredBridgeError(
|
|
@@ -571,9 +826,7 @@ function isStructuredBridgeError(
|
|
|
571
826
|
function encodeNativeCallbackError(error: unknown): Error {
|
|
572
827
|
const structuredError = isStructuredBridgeError(error)
|
|
573
828
|
? error
|
|
574
|
-
: deconstructError(error,
|
|
575
|
-
bridge: "native_callback",
|
|
576
|
-
});
|
|
829
|
+
: deconstructError(error, true);
|
|
577
830
|
|
|
578
831
|
const bridgeError = new Error(encodeBridgeRivetError(structuredError), {
|
|
579
832
|
cause: error instanceof Error ? error : undefined,
|
|
@@ -582,6 +835,7 @@ function encodeNativeCallbackError(error: unknown): Error {
|
|
|
582
835
|
group: structuredError.group,
|
|
583
836
|
code: structuredError.code,
|
|
584
837
|
metadata: structuredError.metadata,
|
|
838
|
+
rayId: structuredError.rayId,
|
|
585
839
|
});
|
|
586
840
|
}
|
|
587
841
|
|
|
@@ -601,16 +855,10 @@ function callNativeSync<T>(invoke: () => T): T {
|
|
|
601
855
|
}
|
|
602
856
|
}
|
|
603
857
|
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
});
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
type NativeWorkflowInspectorConfig = WorkflowInspectorConfig<ArrayBuffer> & {
|
|
612
|
-
getState?: () => Promise<unknown> | unknown;
|
|
613
|
-
};
|
|
858
|
+
type NativeWorkflowInspectorConfig =
|
|
859
|
+
WorkflowInspectorConfig<WorkflowHistoryBytes> & {
|
|
860
|
+
getState?: () => Promise<unknown> | unknown;
|
|
861
|
+
};
|
|
614
862
|
|
|
615
863
|
function isClosedTaskRegistrationError(error: unknown): boolean {
|
|
616
864
|
const metadata = error instanceof RivetError ? error.metadata : undefined;
|
|
@@ -627,23 +875,25 @@ function isClosedTaskRegistrationError(error: unknown): boolean {
|
|
|
627
875
|
);
|
|
628
876
|
}
|
|
629
877
|
|
|
630
|
-
async function
|
|
631
|
-
|
|
878
|
+
async function createCancellationTokenHandle(
|
|
879
|
+
runtime: CoreRuntime,
|
|
880
|
+
signal?: AbortSignal,
|
|
881
|
+
): Promise<{
|
|
882
|
+
token?: CancellationTokenHandle;
|
|
632
883
|
cleanup?: () => void;
|
|
633
884
|
}> {
|
|
634
885
|
if (!signal) {
|
|
635
886
|
return {};
|
|
636
887
|
}
|
|
637
888
|
|
|
638
|
-
const
|
|
639
|
-
const token = new bindings.CancellationToken();
|
|
889
|
+
const token = runtime.createCancellationToken();
|
|
640
890
|
|
|
641
891
|
if (signal.aborted) {
|
|
642
|
-
|
|
892
|
+
runtime.cancelCancellationToken(token);
|
|
643
893
|
return { token };
|
|
644
894
|
}
|
|
645
895
|
|
|
646
|
-
const abort = () =>
|
|
896
|
+
const abort = () => runtime.cancelCancellationToken(token);
|
|
647
897
|
signal.addEventListener("abort", abort, { once: true });
|
|
648
898
|
return {
|
|
649
899
|
token,
|
|
@@ -781,6 +1031,10 @@ function serializeWorkflowEntryKind(
|
|
|
781
1031
|
| {
|
|
782
1032
|
tag: "WorkflowRemovedEntry";
|
|
783
1033
|
val: { originalType: string; originalName: string | null };
|
|
1034
|
+
}
|
|
1035
|
+
| {
|
|
1036
|
+
tag: "WorkflowVersionCheckEntry";
|
|
1037
|
+
val: { resolved: number; latest: number };
|
|
784
1038
|
} {
|
|
785
1039
|
switch (kind.tag) {
|
|
786
1040
|
case "WorkflowStepEntry":
|
|
@@ -846,9 +1100,18 @@ function serializeWorkflowEntryKind(
|
|
|
846
1100
|
originalName: kind.val.originalName,
|
|
847
1101
|
},
|
|
848
1102
|
};
|
|
1103
|
+
case "WorkflowVersionCheckEntry":
|
|
1104
|
+
return {
|
|
1105
|
+
tag: kind.tag,
|
|
1106
|
+
val: {
|
|
1107
|
+
resolved: kind.val.resolved,
|
|
1108
|
+
latest: kind.val.latest,
|
|
1109
|
+
},
|
|
1110
|
+
};
|
|
849
1111
|
}
|
|
850
1112
|
}
|
|
851
1113
|
|
|
1114
|
+
// TODO: Switch inspector routes to CBOR encoding
|
|
852
1115
|
function serializeWorkflowHistoryForJson(data: ArrayBuffer | null): {
|
|
853
1116
|
nameRegistry: string[];
|
|
854
1117
|
entries: Array<{
|
|
@@ -882,7 +1145,7 @@ function serializeWorkflowHistoryForJson(data: ArrayBuffer | null): {
|
|
|
882
1145
|
|
|
883
1146
|
const history = decodeWorkflowHistoryTransport(data);
|
|
884
1147
|
|
|
885
|
-
return {
|
|
1148
|
+
return jsonSafe({
|
|
886
1149
|
nameRegistry: [...history.nameRegistry],
|
|
887
1150
|
entries: history.entries.map((entry) => ({
|
|
888
1151
|
id: entry.id,
|
|
@@ -912,7 +1175,7 @@ function serializeWorkflowHistoryForJson(data: ArrayBuffer | null): {
|
|
|
912
1175
|
],
|
|
913
1176
|
),
|
|
914
1177
|
),
|
|
915
|
-
};
|
|
1178
|
+
});
|
|
916
1179
|
}
|
|
917
1180
|
|
|
918
1181
|
function toHttpJsonCompatible<T>(value: T): T {
|
|
@@ -982,74 +1245,24 @@ function wrapNativeCallback<Args extends Array<unknown>, Result>(
|
|
|
982
1245
|
};
|
|
983
1246
|
}
|
|
984
1247
|
|
|
985
|
-
function decodeArgs(value?:
|
|
1248
|
+
function decodeArgs(value?: RuntimeBytes | null): unknown[] {
|
|
986
1249
|
const decoded = decodeValue<unknown>(value);
|
|
987
|
-
return
|
|
988
|
-
? decoded
|
|
989
|
-
: decoded === undefined
|
|
990
|
-
? []
|
|
991
|
-
: [decoded];
|
|
992
|
-
}
|
|
993
|
-
|
|
994
|
-
function createWriteThroughProxy<T>(
|
|
995
|
-
value: T,
|
|
996
|
-
commit: (next: T) => void,
|
|
997
|
-
beforeChange?: () => void,
|
|
998
|
-
): T {
|
|
999
|
-
if (!value || typeof value !== "object") {
|
|
1000
|
-
return value;
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
const proxies = new WeakMap<object, object>();
|
|
1004
|
-
const wrap = (target: object): object => {
|
|
1005
|
-
const cached = proxies.get(target);
|
|
1006
|
-
if (cached) {
|
|
1007
|
-
return cached;
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
const proxy = new Proxy(target, {
|
|
1011
|
-
get(innerTarget, property, receiver) {
|
|
1012
|
-
const result = Reflect.get(innerTarget, property, receiver);
|
|
1013
|
-
return result && typeof result === "object"
|
|
1014
|
-
? wrap(result as object)
|
|
1015
|
-
: result;
|
|
1016
|
-
},
|
|
1017
|
-
set(innerTarget, property, nextValue, receiver) {
|
|
1018
|
-
beforeChange?.();
|
|
1019
|
-
const updated = Reflect.set(
|
|
1020
|
-
innerTarget,
|
|
1021
|
-
property,
|
|
1022
|
-
nextValue,
|
|
1023
|
-
receiver,
|
|
1024
|
-
);
|
|
1025
|
-
commit(value);
|
|
1026
|
-
return updated;
|
|
1027
|
-
},
|
|
1028
|
-
deleteProperty(innerTarget, property) {
|
|
1029
|
-
beforeChange?.();
|
|
1030
|
-
const updated = Reflect.deleteProperty(innerTarget, property);
|
|
1031
|
-
commit(value);
|
|
1032
|
-
return updated;
|
|
1033
|
-
},
|
|
1034
|
-
});
|
|
1035
|
-
|
|
1036
|
-
proxies.set(target, proxy);
|
|
1037
|
-
return proxy;
|
|
1038
|
-
};
|
|
1039
|
-
|
|
1040
|
-
return wrap(value as object) as T;
|
|
1250
|
+
return normalizeArgs(decoded);
|
|
1041
1251
|
}
|
|
1042
1252
|
|
|
1043
1253
|
function buildRequest(init: {
|
|
1044
1254
|
method: string;
|
|
1045
1255
|
uri: string;
|
|
1046
1256
|
headers?: Record<string, string>;
|
|
1047
|
-
body?:
|
|
1257
|
+
body?: RuntimeBytes;
|
|
1048
1258
|
}): Request {
|
|
1049
1259
|
const url = init.uri.startsWith("http")
|
|
1050
1260
|
? init.uri
|
|
1051
1261
|
: new URL(init.uri, "http://127.0.0.1").toString();
|
|
1052
|
-
const body =
|
|
1262
|
+
const body =
|
|
1263
|
+
init.body && init.body.length > 0
|
|
1264
|
+
? runtimeBytesToArrayBuffer(init.body)
|
|
1265
|
+
: undefined;
|
|
1053
1266
|
return new Request(url, {
|
|
1054
1267
|
method: init.method,
|
|
1055
1268
|
headers: init.headers,
|
|
@@ -1057,9 +1270,11 @@ function buildRequest(init: {
|
|
|
1057
1270
|
});
|
|
1058
1271
|
}
|
|
1059
1272
|
|
|
1060
|
-
async function
|
|
1273
|
+
async function toRuntimeHttpResponse(
|
|
1274
|
+
response: ResponseLike,
|
|
1275
|
+
): Promise<RuntimeHttpResponse> {
|
|
1061
1276
|
const headers = Object.fromEntries(response.headers.entries());
|
|
1062
|
-
const body =
|
|
1277
|
+
const body = new Uint8Array(await response.arrayBuffer());
|
|
1063
1278
|
return {
|
|
1064
1279
|
status: response.status,
|
|
1065
1280
|
headers,
|
|
@@ -1082,21 +1297,29 @@ function toActorKey(
|
|
|
1082
1297
|
}
|
|
1083
1298
|
|
|
1084
1299
|
class NativeConnAdapter {
|
|
1085
|
-
#
|
|
1300
|
+
#runtime: CoreRuntime;
|
|
1301
|
+
#conn: ConnHandle;
|
|
1086
1302
|
#schemas: NativeValidationConfig;
|
|
1087
|
-
#ctx?:
|
|
1303
|
+
#ctx?: ActorContextHandle;
|
|
1088
1304
|
#queueHibernationRemoval?: (connId: string) => void;
|
|
1305
|
+
#assertCanMutateState?: () => void;
|
|
1306
|
+
#stateProxy?: unknown;
|
|
1307
|
+
#stateProxyTarget?: unknown;
|
|
1089
1308
|
|
|
1090
1309
|
constructor(
|
|
1091
|
-
|
|
1310
|
+
runtime: CoreRuntime,
|
|
1311
|
+
conn: ConnHandle,
|
|
1092
1312
|
schemas: NativeValidationConfig = {},
|
|
1093
|
-
ctx?:
|
|
1313
|
+
ctx?: ActorContextHandle,
|
|
1094
1314
|
queueHibernationRemoval?: (connId: string) => void,
|
|
1315
|
+
assertCanMutateState?: () => void,
|
|
1095
1316
|
) {
|
|
1317
|
+
this.#runtime = runtime;
|
|
1096
1318
|
this.#conn = conn;
|
|
1097
1319
|
this.#schemas = schemas;
|
|
1098
1320
|
this.#ctx = ctx;
|
|
1099
1321
|
this.#queueHibernationRemoval = queueHibernationRemoval;
|
|
1322
|
+
this.#assertCanMutateState = assertCanMutateState;
|
|
1100
1323
|
(
|
|
1101
1324
|
this as NativeConnAdapter & {
|
|
1102
1325
|
[CONN_STATE_MANAGER_SYMBOL]?: unknown;
|
|
@@ -1111,33 +1334,77 @@ class NativeConnAdapter {
|
|
|
1111
1334
|
}
|
|
1112
1335
|
|
|
1113
1336
|
get id(): string {
|
|
1114
|
-
return this.#
|
|
1337
|
+
return this.#runtime.connId(this.#conn);
|
|
1115
1338
|
}
|
|
1116
1339
|
|
|
1117
1340
|
get params(): unknown {
|
|
1118
1341
|
return validateConnParams(
|
|
1119
1342
|
this.#schemas.connParamsSchema,
|
|
1120
|
-
decodeValue(this.#
|
|
1343
|
+
decodeValue(this.#runtime.connParams(this.#conn)),
|
|
1121
1344
|
);
|
|
1122
1345
|
}
|
|
1123
1346
|
|
|
1347
|
+
[RAW_STATE_SYMBOL](): unknown {
|
|
1348
|
+
return this.#readState();
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1124
1351
|
get state(): unknown {
|
|
1125
1352
|
const nextState = this.#readState();
|
|
1126
|
-
|
|
1127
|
-
this.#
|
|
1128
|
-
}
|
|
1353
|
+
if (!this.#ctx) {
|
|
1354
|
+
return this.#createStateProxy(nextState);
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
// Reading `conn.state` rebuilds the deep write-through proxy, which
|
|
1358
|
+
// allocates fresh on-change caches and rewraps the whole tree. Memoize
|
|
1359
|
+
// the proxy keyed on the underlying state object so repeated reads and
|
|
1360
|
+
// deep read cascades reuse a single proxy.
|
|
1361
|
+
if (
|
|
1362
|
+
this.#stateProxy === undefined ||
|
|
1363
|
+
this.#stateProxyTarget !== nextState
|
|
1364
|
+
) {
|
|
1365
|
+
this.#stateProxyTarget = nextState;
|
|
1366
|
+
this.#stateProxy = this.#createStateProxy(nextState);
|
|
1367
|
+
}
|
|
1368
|
+
return this.#stateProxy;
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
#createStateProxy(state: unknown): unknown {
|
|
1372
|
+
const assertCurrentState = () => {
|
|
1373
|
+
this.#assertCanMutateState?.();
|
|
1374
|
+
if (this.#ctx && this.#readState() !== state) {
|
|
1375
|
+
throw stateTransactionConflictError();
|
|
1376
|
+
}
|
|
1377
|
+
};
|
|
1378
|
+
return createWriteThroughProxy(
|
|
1379
|
+
state,
|
|
1380
|
+
(nextValue) => {
|
|
1381
|
+
assertCurrentState();
|
|
1382
|
+
this.#writeState(nextValue, { writeNative: true });
|
|
1383
|
+
},
|
|
1384
|
+
(newValue) => {
|
|
1385
|
+
assertCurrentState();
|
|
1386
|
+
assertJsonCompatValue(newValue);
|
|
1387
|
+
},
|
|
1388
|
+
);
|
|
1129
1389
|
}
|
|
1130
1390
|
|
|
1131
1391
|
set state(value: unknown) {
|
|
1132
|
-
this.#
|
|
1392
|
+
this.#assertCanMutateState?.();
|
|
1393
|
+
const nextValue = unwrapWriteThroughProxy(value);
|
|
1394
|
+
assertJsonCompatValue(nextValue);
|
|
1395
|
+
this.#writeState(nextValue, { writeNative: true });
|
|
1133
1396
|
}
|
|
1134
1397
|
|
|
1135
1398
|
initializeState(value: unknown): void {
|
|
1136
|
-
this.#writeState(value, {
|
|
1399
|
+
this.#writeState(unwrapWriteThroughProxy(value), {
|
|
1400
|
+
writeNative: false,
|
|
1401
|
+
});
|
|
1137
1402
|
}
|
|
1138
1403
|
|
|
1139
1404
|
get isHibernatable(): boolean {
|
|
1140
|
-
return callNativeSync(() =>
|
|
1405
|
+
return callNativeSync(() =>
|
|
1406
|
+
this.#runtime.connIsHibernatable(this.#conn),
|
|
1407
|
+
);
|
|
1141
1408
|
}
|
|
1142
1409
|
|
|
1143
1410
|
send(name: string, ...args: unknown[]): void {
|
|
@@ -1146,12 +1413,20 @@ class NativeConnAdapter {
|
|
|
1146
1413
|
name,
|
|
1147
1414
|
args,
|
|
1148
1415
|
);
|
|
1149
|
-
callNativeSync(() =>
|
|
1416
|
+
callNativeSync(() =>
|
|
1417
|
+
this.#runtime.connSend(
|
|
1418
|
+
this.#conn,
|
|
1419
|
+
name,
|
|
1420
|
+
encodeValue(validatedArgs),
|
|
1421
|
+
),
|
|
1422
|
+
);
|
|
1150
1423
|
}
|
|
1151
1424
|
|
|
1152
1425
|
async disconnect(reason?: string): Promise<void> {
|
|
1153
1426
|
const connId = this.id;
|
|
1154
|
-
await callNative(() =>
|
|
1427
|
+
await callNative(() =>
|
|
1428
|
+
this.#runtime.connDisconnect(this.#conn, reason),
|
|
1429
|
+
);
|
|
1155
1430
|
if (this.isHibernatable) {
|
|
1156
1431
|
this.#queueHibernationRemoval?.(connId);
|
|
1157
1432
|
}
|
|
@@ -1159,12 +1434,16 @@ class NativeConnAdapter {
|
|
|
1159
1434
|
|
|
1160
1435
|
#readState(): unknown {
|
|
1161
1436
|
if (!this.#ctx) {
|
|
1162
|
-
return decodeValue(this.#
|
|
1437
|
+
return decodeValue(this.#runtime.connState(this.#conn));
|
|
1163
1438
|
}
|
|
1164
1439
|
|
|
1165
|
-
const connState = getNativeConnPersistState(
|
|
1440
|
+
const connState = getNativeConnPersistState(
|
|
1441
|
+
this.#runtime,
|
|
1442
|
+
this.#ctx,
|
|
1443
|
+
this.#conn,
|
|
1444
|
+
);
|
|
1166
1445
|
if (connState.state === undefined) {
|
|
1167
|
-
connState.state = decodeValue(this.#
|
|
1446
|
+
connState.state = decodeValue(this.#runtime.connState(this.#conn));
|
|
1168
1447
|
}
|
|
1169
1448
|
return connState.state;
|
|
1170
1449
|
}
|
|
@@ -1177,32 +1456,41 @@ class NativeConnAdapter {
|
|
|
1177
1456
|
): void {
|
|
1178
1457
|
const encoded = encodeValue(value);
|
|
1179
1458
|
if (!this.#ctx) {
|
|
1180
|
-
this.#
|
|
1459
|
+
this.#runtime.connSetState(this.#conn, encoded);
|
|
1181
1460
|
return;
|
|
1182
1461
|
}
|
|
1183
1462
|
|
|
1184
|
-
const connState = getNativeConnPersistState(
|
|
1463
|
+
const connState = getNativeConnPersistState(
|
|
1464
|
+
this.#runtime,
|
|
1465
|
+
this.#ctx,
|
|
1466
|
+
this.#conn,
|
|
1467
|
+
);
|
|
1185
1468
|
connState.state = value;
|
|
1186
1469
|
if (options.writeNative) {
|
|
1187
|
-
this.#
|
|
1470
|
+
this.#runtime.connSetState(this.#conn, encoded);
|
|
1188
1471
|
}
|
|
1189
1472
|
}
|
|
1190
1473
|
}
|
|
1191
1474
|
|
|
1192
|
-
class NativeScheduleAdapter {
|
|
1193
|
-
#
|
|
1475
|
+
class NativeScheduleAdapter implements ActorSchedule {
|
|
1476
|
+
#runtime: CoreRuntime;
|
|
1477
|
+
#ctx: ActorContextHandle;
|
|
1194
1478
|
|
|
1195
|
-
constructor(
|
|
1196
|
-
this.#
|
|
1479
|
+
constructor(runtime: CoreRuntime, ctx: ActorContextHandle) {
|
|
1480
|
+
this.#runtime = runtime;
|
|
1481
|
+
this.#ctx = ctx;
|
|
1197
1482
|
}
|
|
1198
1483
|
|
|
1199
1484
|
async after(
|
|
1200
1485
|
duration: number,
|
|
1201
1486
|
action: string,
|
|
1202
1487
|
...args: unknown[]
|
|
1203
|
-
): Promise<
|
|
1204
|
-
|
|
1205
|
-
this.#
|
|
1488
|
+
): Promise<string> {
|
|
1489
|
+
return await this.#runtime.actorScheduleAfter(
|
|
1490
|
+
this.#ctx,
|
|
1491
|
+
duration,
|
|
1492
|
+
action,
|
|
1493
|
+
encodeValue(args),
|
|
1206
1494
|
);
|
|
1207
1495
|
}
|
|
1208
1496
|
|
|
@@ -1210,18 +1498,138 @@ class NativeScheduleAdapter {
|
|
|
1210
1498
|
timestamp: number,
|
|
1211
1499
|
action: string,
|
|
1212
1500
|
...args: unknown[]
|
|
1213
|
-
): Promise<
|
|
1214
|
-
|
|
1215
|
-
this.#
|
|
1501
|
+
): Promise<string> {
|
|
1502
|
+
return await this.#runtime.actorScheduleAt(
|
|
1503
|
+
this.#ctx,
|
|
1504
|
+
timestamp,
|
|
1505
|
+
action,
|
|
1506
|
+
encodeValue(args),
|
|
1507
|
+
);
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
async cancel(id: string): Promise<boolean> {
|
|
1511
|
+
return await this.#runtime.actorScheduleCancel(this.#ctx, id);
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
async get(id: string): Promise<ScheduledEventInfo | undefined> {
|
|
1515
|
+
const event = await this.#runtime.actorScheduleGet(this.#ctx, id);
|
|
1516
|
+
return event ? decodeScheduledEventInfo(event) : undefined;
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
async list(): Promise<ScheduledEventInfo[]> {
|
|
1520
|
+
return (await this.#runtime.actorScheduleList(this.#ctx)).map(
|
|
1521
|
+
decodeScheduledEventInfo,
|
|
1216
1522
|
);
|
|
1217
1523
|
}
|
|
1218
1524
|
}
|
|
1219
1525
|
|
|
1526
|
+
class NativeCronAdapter implements ActorCron {
|
|
1527
|
+
#runtime: CoreRuntime;
|
|
1528
|
+
#ctx: ActorContextHandle;
|
|
1529
|
+
|
|
1530
|
+
constructor(runtime: CoreRuntime, ctx: ActorContextHandle) {
|
|
1531
|
+
this.#runtime = runtime;
|
|
1532
|
+
this.#ctx = ctx;
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
async set(options: ActorCronSetOptions): Promise<void> {
|
|
1536
|
+
await this.#runtime.actorCronSet(
|
|
1537
|
+
this.#ctx,
|
|
1538
|
+
options.name,
|
|
1539
|
+
options.expression,
|
|
1540
|
+
options.timezone,
|
|
1541
|
+
options.action,
|
|
1542
|
+
encodeValue(options.args ?? []),
|
|
1543
|
+
options.maxHistory,
|
|
1544
|
+
);
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
async every(options: ActorCronEveryOptions): Promise<void> {
|
|
1548
|
+
await this.#runtime.actorCronEvery(
|
|
1549
|
+
this.#ctx,
|
|
1550
|
+
options.name,
|
|
1551
|
+
options.interval,
|
|
1552
|
+
options.action,
|
|
1553
|
+
encodeValue(options.args ?? []),
|
|
1554
|
+
options.maxHistory,
|
|
1555
|
+
);
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
async get(name: string): Promise<CronJobInfo | undefined> {
|
|
1559
|
+
const job = await this.#runtime.actorCronGet(this.#ctx, name);
|
|
1560
|
+
return job ? decodeCronJobInfo(job) : undefined;
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
async list(): Promise<CronJobInfo[]> {
|
|
1564
|
+
return (await this.#runtime.actorCronList(this.#ctx)).map(
|
|
1565
|
+
decodeCronJobInfo,
|
|
1566
|
+
);
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
async delete(name: string): Promise<boolean> {
|
|
1570
|
+
return await this.#runtime.actorCronDelete(this.#ctx, name);
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
async history(
|
|
1574
|
+
name: string,
|
|
1575
|
+
options?: { limit?: number },
|
|
1576
|
+
): Promise<CronFire[]> {
|
|
1577
|
+
return (
|
|
1578
|
+
await this.#runtime.actorCronHistory(
|
|
1579
|
+
this.#ctx,
|
|
1580
|
+
name,
|
|
1581
|
+
options?.limit,
|
|
1582
|
+
)
|
|
1583
|
+
).map(decodeCronFire);
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
function decodeScheduledEventInfo(
|
|
1588
|
+
event: RuntimeScheduledEventInfo,
|
|
1589
|
+
): ScheduledEventInfo {
|
|
1590
|
+
return {
|
|
1591
|
+
id: event.id,
|
|
1592
|
+
action: event.action,
|
|
1593
|
+
args: decodeArgs(event.args),
|
|
1594
|
+
runAt: event.runAt,
|
|
1595
|
+
};
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
function decodeCronJobInfo(job: RuntimeCronJobInfo): CronJobInfo {
|
|
1599
|
+
const base = {
|
|
1600
|
+
name: job.name,
|
|
1601
|
+
action: job.action,
|
|
1602
|
+
args: decodeArgs(job.args),
|
|
1603
|
+
nextRunAt: job.nextRunAt,
|
|
1604
|
+
lastRunAt: job.lastRunAt,
|
|
1605
|
+
maxHistory: job.maxHistory,
|
|
1606
|
+
};
|
|
1607
|
+
if (job.kind === "cron") {
|
|
1608
|
+
return {
|
|
1609
|
+
...base,
|
|
1610
|
+
kind: "cron",
|
|
1611
|
+
expression: job.expression as string,
|
|
1612
|
+
timezone: job.timezone as string,
|
|
1613
|
+
};
|
|
1614
|
+
}
|
|
1615
|
+
return {
|
|
1616
|
+
...base,
|
|
1617
|
+
kind: "every",
|
|
1618
|
+
interval: job.intervalMs as number,
|
|
1619
|
+
};
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
function decodeCronFire(fire: RuntimeCronFire): CronFire {
|
|
1623
|
+
return { ...fire };
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1220
1626
|
class NativeKvAdapter {
|
|
1221
|
-
#
|
|
1627
|
+
#runtime: CoreRuntime;
|
|
1628
|
+
#ctx: ActorContextHandle;
|
|
1222
1629
|
|
|
1223
|
-
constructor(
|
|
1224
|
-
this.#
|
|
1630
|
+
constructor(runtime: CoreRuntime, ctx: ActorContextHandle) {
|
|
1631
|
+
this.#runtime = runtime;
|
|
1632
|
+
this.#ctx = ctx;
|
|
1225
1633
|
}
|
|
1226
1634
|
|
|
1227
1635
|
async get<T extends NativeKvValueType = "text">(
|
|
@@ -1229,8 +1637,9 @@ class NativeKvAdapter {
|
|
|
1229
1637
|
options?: NativeKvValueOptions<T>,
|
|
1230
1638
|
): Promise<NativeKvValueTypeMap[T] | null> {
|
|
1231
1639
|
const value = await callNative(() =>
|
|
1232
|
-
this.#
|
|
1233
|
-
|
|
1640
|
+
this.#runtime.actorKvGet(
|
|
1641
|
+
this.#ctx,
|
|
1642
|
+
makePrefixedKey(encodeNativeKvUserKey(key)),
|
|
1234
1643
|
),
|
|
1235
1644
|
);
|
|
1236
1645
|
return value
|
|
@@ -1244,17 +1653,19 @@ class NativeKvAdapter {
|
|
|
1244
1653
|
_options?: NativeKvValueOptions,
|
|
1245
1654
|
): Promise<void> {
|
|
1246
1655
|
await callNative(() =>
|
|
1247
|
-
this.#
|
|
1248
|
-
|
|
1249
|
-
|
|
1656
|
+
this.#runtime.actorKvPut(
|
|
1657
|
+
this.#ctx,
|
|
1658
|
+
makePrefixedKey(encodeNativeKvUserKey(key)),
|
|
1659
|
+
toRuntimeBytes(value),
|
|
1250
1660
|
),
|
|
1251
1661
|
);
|
|
1252
1662
|
}
|
|
1253
1663
|
|
|
1254
1664
|
async delete(key: string | Uint8Array): Promise<void> {
|
|
1255
1665
|
await callNative(() =>
|
|
1256
|
-
this.#
|
|
1257
|
-
|
|
1666
|
+
this.#runtime.actorKvDelete(
|
|
1667
|
+
this.#ctx,
|
|
1668
|
+
makePrefixedKey(encodeNativeKvUserKey(key)),
|
|
1258
1669
|
),
|
|
1259
1670
|
);
|
|
1260
1671
|
}
|
|
@@ -1264,16 +1675,17 @@ class NativeKvAdapter {
|
|
|
1264
1675
|
end: string | Uint8Array,
|
|
1265
1676
|
): Promise<void> {
|
|
1266
1677
|
await callNative(() =>
|
|
1267
|
-
this.#
|
|
1268
|
-
|
|
1269
|
-
|
|
1678
|
+
this.#runtime.actorKvDeleteRange(
|
|
1679
|
+
this.#ctx,
|
|
1680
|
+
makePrefixedKey(encodeNativeKvUserKey(start)),
|
|
1681
|
+
makePrefixedKey(encodeNativeKvUserKey(end)),
|
|
1270
1682
|
),
|
|
1271
1683
|
);
|
|
1272
1684
|
}
|
|
1273
1685
|
|
|
1274
1686
|
async rawDeleteRange(start: Uint8Array, end: Uint8Array): Promise<void> {
|
|
1275
1687
|
await callNative(() =>
|
|
1276
|
-
this.#
|
|
1688
|
+
this.#runtime.actorKvDeleteRange(this.#ctx, start, end),
|
|
1277
1689
|
);
|
|
1278
1690
|
}
|
|
1279
1691
|
|
|
@@ -1285,13 +1697,12 @@ class NativeKvAdapter {
|
|
|
1285
1697
|
options?: NativeKvListOptions<T, K>,
|
|
1286
1698
|
): Promise<Array<[NativeKvKeyTypeMap[K], NativeKvValueTypeMap[T]]>> {
|
|
1287
1699
|
const entries = await callNative(() =>
|
|
1288
|
-
this.#
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
),
|
|
1700
|
+
this.#runtime.actorKvListPrefix(
|
|
1701
|
+
this.#ctx,
|
|
1702
|
+
makePrefixedKey(
|
|
1703
|
+
encodeNativeKvUserKey(
|
|
1704
|
+
prefix as NativeKvKeyTypeMap[K],
|
|
1705
|
+
options?.keyType,
|
|
1295
1706
|
),
|
|
1296
1707
|
),
|
|
1297
1708
|
{
|
|
@@ -1313,7 +1724,7 @@ class NativeKvAdapter {
|
|
|
1313
1724
|
prefix: Uint8Array,
|
|
1314
1725
|
): Promise<Array<[Uint8Array, Uint8Array]>> {
|
|
1315
1726
|
const entries = await callNative(() =>
|
|
1316
|
-
this.#
|
|
1727
|
+
this.#runtime.actorKvListPrefix(this.#ctx, prefix, {}),
|
|
1317
1728
|
);
|
|
1318
1729
|
return entries.map((entry) => [
|
|
1319
1730
|
new Uint8Array(entry.key),
|
|
@@ -1330,21 +1741,18 @@ class NativeKvAdapter {
|
|
|
1330
1741
|
options?: NativeKvListOptions<T, K>,
|
|
1331
1742
|
): Promise<Array<[NativeKvKeyTypeMap[K], NativeKvValueTypeMap[T]]>> {
|
|
1332
1743
|
const entries = await callNative(() =>
|
|
1333
|
-
this.#
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
),
|
|
1744
|
+
this.#runtime.actorKvListRange(
|
|
1745
|
+
this.#ctx,
|
|
1746
|
+
makePrefixedKey(
|
|
1747
|
+
encodeNativeKvUserKey(
|
|
1748
|
+
start as NativeKvKeyTypeMap[K],
|
|
1749
|
+
options?.keyType,
|
|
1340
1750
|
),
|
|
1341
1751
|
),
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
options?.keyType,
|
|
1347
|
-
),
|
|
1752
|
+
makePrefixedKey(
|
|
1753
|
+
encodeNativeKvUserKey(
|
|
1754
|
+
end as NativeKvKeyTypeMap[K],
|
|
1755
|
+
options?.keyType,
|
|
1348
1756
|
),
|
|
1349
1757
|
),
|
|
1350
1758
|
{
|
|
@@ -1374,17 +1782,18 @@ class NativeKvAdapter {
|
|
|
1374
1782
|
|
|
1375
1783
|
async batchGet(keys: Uint8Array[]): Promise<Array<Uint8Array | null>> {
|
|
1376
1784
|
const values = await callNative(() =>
|
|
1377
|
-
this.#
|
|
1785
|
+
this.#runtime.actorKvBatchGet(this.#ctx, keys),
|
|
1378
1786
|
);
|
|
1379
1787
|
return values.map((value) => (value ? new Uint8Array(value) : null));
|
|
1380
1788
|
}
|
|
1381
1789
|
|
|
1382
1790
|
async batchPut(entries: [Uint8Array, Uint8Array][]): Promise<void> {
|
|
1383
1791
|
await callNative(() =>
|
|
1384
|
-
this.#
|
|
1792
|
+
this.#runtime.actorKvBatchPut(
|
|
1793
|
+
this.#ctx,
|
|
1385
1794
|
entries.map(([key, value]) => ({
|
|
1386
|
-
key
|
|
1387
|
-
value
|
|
1795
|
+
key,
|
|
1796
|
+
value,
|
|
1388
1797
|
})),
|
|
1389
1798
|
),
|
|
1390
1799
|
);
|
|
@@ -1392,18 +1801,19 @@ class NativeKvAdapter {
|
|
|
1392
1801
|
|
|
1393
1802
|
async batchDelete(keys: Uint8Array[]): Promise<void> {
|
|
1394
1803
|
await callNative(() =>
|
|
1395
|
-
this.#
|
|
1804
|
+
this.#runtime.actorKvBatchDelete(this.#ctx, keys),
|
|
1396
1805
|
);
|
|
1397
1806
|
}
|
|
1398
1807
|
}
|
|
1399
1808
|
|
|
1400
1809
|
function wrapQueueMessage(
|
|
1401
|
-
message:
|
|
1810
|
+
message: RuntimeQueueMessage,
|
|
1402
1811
|
schemas: NativeValidationConfig["queues"],
|
|
1403
1812
|
) {
|
|
1404
1813
|
const name = callNativeSync(() => message.name());
|
|
1814
|
+
const id = callNativeSync(() => message.id());
|
|
1405
1815
|
return {
|
|
1406
|
-
id: Number(
|
|
1816
|
+
id: id <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(id) : id,
|
|
1407
1817
|
name,
|
|
1408
1818
|
body: validateQueueBody(
|
|
1409
1819
|
schemas,
|
|
@@ -1431,15 +1841,18 @@ function wrapQueueMessage(
|
|
|
1431
1841
|
}
|
|
1432
1842
|
|
|
1433
1843
|
class NativeQueueAdapter {
|
|
1434
|
-
#
|
|
1844
|
+
#runtime: CoreRuntime;
|
|
1845
|
+
#ctx: ActorContextHandle;
|
|
1435
1846
|
#schemas: NativeValidationConfig["queues"];
|
|
1436
1847
|
#pendingCompletableMessageIds = new Set<string>();
|
|
1437
1848
|
|
|
1438
1849
|
constructor(
|
|
1439
|
-
|
|
1850
|
+
runtime: CoreRuntime,
|
|
1851
|
+
ctx: ActorContextHandle,
|
|
1440
1852
|
schemas: NativeValidationConfig["queues"] = undefined,
|
|
1441
1853
|
) {
|
|
1442
|
-
this.#
|
|
1854
|
+
this.#runtime = runtime;
|
|
1855
|
+
this.#ctx = ctx;
|
|
1443
1856
|
this.#schemas = schemas;
|
|
1444
1857
|
}
|
|
1445
1858
|
|
|
@@ -1447,7 +1860,11 @@ class NativeQueueAdapter {
|
|
|
1447
1860
|
const validatedBody = validateQueueBody(this.#schemas, name, body);
|
|
1448
1861
|
return wrapQueueMessage(
|
|
1449
1862
|
await callNative(() =>
|
|
1450
|
-
this.#
|
|
1863
|
+
this.#runtime.actorQueueSend(
|
|
1864
|
+
this.#ctx,
|
|
1865
|
+
name,
|
|
1866
|
+
encodeValue(validatedBody),
|
|
1867
|
+
),
|
|
1451
1868
|
),
|
|
1452
1869
|
this.#schemas,
|
|
1453
1870
|
);
|
|
@@ -1489,13 +1906,15 @@ class NativeQueueAdapter {
|
|
|
1489
1906
|
);
|
|
1490
1907
|
}
|
|
1491
1908
|
|
|
1492
|
-
const { token, cleanup } = await
|
|
1909
|
+
const { token, cleanup } = await createCancellationTokenHandle(
|
|
1910
|
+
this.#runtime,
|
|
1493
1911
|
options?.signal,
|
|
1494
1912
|
);
|
|
1495
1913
|
|
|
1496
1914
|
try {
|
|
1497
1915
|
const messages = await callNative(() =>
|
|
1498
|
-
this.#
|
|
1916
|
+
this.#runtime.actorQueueNextBatch(
|
|
1917
|
+
this.#ctx,
|
|
1499
1918
|
{
|
|
1500
1919
|
names: this.#normalizeNames(options?.names),
|
|
1501
1920
|
count: options?.count,
|
|
@@ -1526,14 +1945,16 @@ class NativeQueueAdapter {
|
|
|
1526
1945
|
completable?: boolean;
|
|
1527
1946
|
},
|
|
1528
1947
|
) {
|
|
1529
|
-
const { token, cleanup } = await
|
|
1948
|
+
const { token, cleanup } = await createCancellationTokenHandle(
|
|
1949
|
+
this.#runtime,
|
|
1530
1950
|
options?.signal,
|
|
1531
1951
|
);
|
|
1532
1952
|
|
|
1533
1953
|
try {
|
|
1534
1954
|
return wrapQueueMessage(
|
|
1535
1955
|
await callNative(() =>
|
|
1536
|
-
this.#
|
|
1956
|
+
this.#runtime.actorQueueWaitForNames(
|
|
1957
|
+
this.#ctx,
|
|
1537
1958
|
[...names],
|
|
1538
1959
|
{
|
|
1539
1960
|
timeoutMs: options?.timeout,
|
|
@@ -1556,58 +1977,51 @@ class NativeQueueAdapter {
|
|
|
1556
1977
|
signal?: AbortSignal;
|
|
1557
1978
|
},
|
|
1558
1979
|
) {
|
|
1559
|
-
|
|
1980
|
+
const { token, cleanup } = await createCancellationTokenHandle(
|
|
1981
|
+
this.#runtime,
|
|
1982
|
+
options?.signal,
|
|
1983
|
+
);
|
|
1984
|
+
|
|
1985
|
+
try {
|
|
1560
1986
|
await callNative(() =>
|
|
1561
|
-
this.#
|
|
1562
|
-
|
|
1563
|
-
|
|
1987
|
+
this.#runtime.actorQueueWaitForNamesAvailable(
|
|
1988
|
+
this.#ctx,
|
|
1989
|
+
[...names],
|
|
1990
|
+
{
|
|
1991
|
+
timeoutMs: options?.timeout,
|
|
1992
|
+
},
|
|
1993
|
+
token,
|
|
1994
|
+
),
|
|
1564
1995
|
);
|
|
1565
|
-
|
|
1996
|
+
} finally {
|
|
1997
|
+
cleanup?.();
|
|
1566
1998
|
}
|
|
1999
|
+
}
|
|
2000
|
+
async waitForAvailable(
|
|
2001
|
+
names?: readonly string[],
|
|
2002
|
+
options?: { timeout?: number; signal?: AbortSignal },
|
|
2003
|
+
): Promise<void> {
|
|
2004
|
+
await this.waitForNamesAvailable(names ?? [], options);
|
|
2005
|
+
}
|
|
1567
2006
|
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
try {
|
|
1588
|
-
await callNative(() =>
|
|
1589
|
-
this.#queue.waitForNamesAvailable([...names], {
|
|
1590
|
-
timeoutMs: sliceTimeout,
|
|
1591
|
-
}),
|
|
1592
|
-
);
|
|
1593
|
-
return;
|
|
1594
|
-
} catch (error) {
|
|
1595
|
-
if (
|
|
1596
|
-
(error as { group?: string; code?: string }).group ===
|
|
1597
|
-
"queue" &&
|
|
1598
|
-
(error as { group?: string; code?: string }).code ===
|
|
1599
|
-
"timed_out"
|
|
1600
|
-
) {
|
|
1601
|
-
if (
|
|
1602
|
-
remainingTimeout === undefined ||
|
|
1603
|
-
remainingTimeout > 100
|
|
1604
|
-
) {
|
|
1605
|
-
continue;
|
|
1606
|
-
}
|
|
1607
|
-
}
|
|
1608
|
-
throw error;
|
|
1609
|
-
}
|
|
1610
|
-
}
|
|
2007
|
+
async complete(
|
|
2008
|
+
message: { id: number | bigint; name: string },
|
|
2009
|
+
response?: unknown,
|
|
2010
|
+
): Promise<void> {
|
|
2011
|
+
const validatedResponse = validateQueueComplete(
|
|
2012
|
+
this.#schemas,
|
|
2013
|
+
message.name,
|
|
2014
|
+
response,
|
|
2015
|
+
);
|
|
2016
|
+
await callNative(() =>
|
|
2017
|
+
this.#runtime.actorQueueCompletePersisted(
|
|
2018
|
+
this.#ctx,
|
|
2019
|
+
BigInt(message.id),
|
|
2020
|
+
message.name,
|
|
2021
|
+
encodeValue(validatedResponse),
|
|
2022
|
+
),
|
|
2023
|
+
);
|
|
2024
|
+
this.#pendingCompletableMessageIds.delete(message.id.toString());
|
|
1611
2025
|
}
|
|
1612
2026
|
|
|
1613
2027
|
async enqueueAndWait(
|
|
@@ -1619,13 +2033,15 @@ class NativeQueueAdapter {
|
|
|
1619
2033
|
},
|
|
1620
2034
|
) {
|
|
1621
2035
|
const validatedBody = validateQueueBody(this.#schemas, name, body);
|
|
1622
|
-
const { token, cleanup } = await
|
|
2036
|
+
const { token, cleanup } = await createCancellationTokenHandle(
|
|
2037
|
+
this.#runtime,
|
|
1623
2038
|
options?.signal,
|
|
1624
2039
|
);
|
|
1625
2040
|
|
|
1626
2041
|
try {
|
|
1627
2042
|
const response = await callNative(() =>
|
|
1628
|
-
this.#
|
|
2043
|
+
this.#runtime.actorQueueEnqueueAndWait(
|
|
2044
|
+
this.#ctx,
|
|
1629
2045
|
name,
|
|
1630
2046
|
encodeValue(validatedBody),
|
|
1631
2047
|
{
|
|
@@ -1671,17 +2087,25 @@ class NativeQueueAdapter {
|
|
|
1671
2087
|
completable: true,
|
|
1672
2088
|
});
|
|
1673
2089
|
}
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
this
|
|
1677
|
-
names:
|
|
1678
|
-
count: options?.count,
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
2090
|
+
|
|
2091
|
+
try {
|
|
2092
|
+
return await this.nextBatch({
|
|
2093
|
+
names: options?.names,
|
|
2094
|
+
count: options?.count,
|
|
2095
|
+
timeout: 0,
|
|
2096
|
+
completable: false,
|
|
2097
|
+
});
|
|
2098
|
+
} catch (error) {
|
|
2099
|
+
if (
|
|
2100
|
+
(error as { group?: string; code?: string }).group ===
|
|
2101
|
+
"queue" &&
|
|
2102
|
+
(error as { group?: string; code?: string }).code ===
|
|
2103
|
+
"timed_out"
|
|
2104
|
+
) {
|
|
2105
|
+
return [];
|
|
2106
|
+
}
|
|
2107
|
+
throw error;
|
|
2108
|
+
}
|
|
1685
2109
|
}
|
|
1686
2110
|
|
|
1687
2111
|
async *iter(options?: {
|
|
@@ -1699,11 +2123,7 @@ class NativeQueueAdapter {
|
|
|
1699
2123
|
}
|
|
1700
2124
|
yield message;
|
|
1701
2125
|
} catch (error) {
|
|
1702
|
-
if (
|
|
1703
|
-
isRivetErrorLike(error) &&
|
|
1704
|
-
error.group === "actor" &&
|
|
1705
|
-
error.code === "aborted"
|
|
1706
|
-
) {
|
|
2126
|
+
if (isActorAbortedError(error)) {
|
|
1707
2127
|
return;
|
|
1708
2128
|
}
|
|
1709
2129
|
throw error;
|
|
@@ -1754,50 +2174,56 @@ class NativeQueueAdapter {
|
|
|
1754
2174
|
);
|
|
1755
2175
|
}
|
|
1756
2176
|
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
this.#pendingCompletableMessageIds.delete(messageId);
|
|
1761
|
-
} catch (error) {
|
|
1762
|
-
throw error;
|
|
1763
|
-
}
|
|
2177
|
+
await message.complete(response);
|
|
2178
|
+
completed = true;
|
|
2179
|
+
this.#pendingCompletableMessageIds.delete(messageId);
|
|
1764
2180
|
},
|
|
1765
2181
|
};
|
|
1766
2182
|
}
|
|
1767
2183
|
}
|
|
1768
2184
|
|
|
1769
2185
|
class NativeWebSocketAdapter {
|
|
1770
|
-
#
|
|
2186
|
+
#runtime: CoreRuntime;
|
|
2187
|
+
#ws: WebSocketHandle;
|
|
1771
2188
|
#virtual: VirtualWebSocket;
|
|
1772
2189
|
#readyState: 0 | 1 | 2 | 3 = VirtualWebSocket.OPEN;
|
|
1773
2190
|
|
|
1774
|
-
constructor(ws:
|
|
1775
|
-
this.#
|
|
2191
|
+
constructor(runtime: CoreRuntime, ws: WebSocketHandle) {
|
|
2192
|
+
this.#runtime = runtime;
|
|
2193
|
+
this.#ws = ws;
|
|
1776
2194
|
this.#virtual = new VirtualWebSocket({
|
|
1777
2195
|
getReadyState: () => this.#readyState,
|
|
1778
2196
|
onSend: (data) => {
|
|
1779
2197
|
if (typeof data === "string") {
|
|
1780
2198
|
callNativeSync(() =>
|
|
1781
|
-
this.#
|
|
2199
|
+
this.#runtime.webSocketSend(
|
|
2200
|
+
this.#ws,
|
|
2201
|
+
textEncoder.encode(data),
|
|
2202
|
+
false,
|
|
2203
|
+
),
|
|
1782
2204
|
);
|
|
1783
2205
|
return;
|
|
1784
2206
|
}
|
|
1785
2207
|
|
|
1786
|
-
const
|
|
1787
|
-
?
|
|
1788
|
-
:
|
|
1789
|
-
callNativeSync(() =>
|
|
2208
|
+
const bytes = ArrayBuffer.isView(data)
|
|
2209
|
+
? arrayBufferViewToRuntimeBytes(data)
|
|
2210
|
+
: new Uint8Array(data as ArrayBufferLike);
|
|
2211
|
+
callNativeSync(() =>
|
|
2212
|
+
this.#runtime.webSocketSend(this.#ws, bytes, true),
|
|
2213
|
+
);
|
|
1790
2214
|
},
|
|
1791
2215
|
onClose: (code, reason) => {
|
|
1792
2216
|
this.#readyState = VirtualWebSocket.CLOSING;
|
|
1793
|
-
void callNative(() =>
|
|
2217
|
+
void callNative(() =>
|
|
2218
|
+
this.#runtime.webSocketClose(this.#ws, code, reason),
|
|
2219
|
+
);
|
|
1794
2220
|
},
|
|
1795
2221
|
});
|
|
1796
|
-
this.#
|
|
2222
|
+
this.#runtime.webSocketSetEventCallback(this.#ws, (event) => {
|
|
1797
2223
|
if (event.kind === "message") {
|
|
1798
2224
|
this.#virtual.triggerMessage(
|
|
1799
2225
|
event.binary
|
|
1800
|
-
?
|
|
2226
|
+
? runtimeBytesToArrayBuffer(event.data as RuntimeBytes)
|
|
1801
2227
|
: event.data,
|
|
1802
2228
|
event.messageIndex,
|
|
1803
2229
|
);
|
|
@@ -1922,8 +2348,8 @@ class NativeWebSocketAdapter {
|
|
|
1922
2348
|
|
|
1923
2349
|
type TrackedWebSocketListener = (event: any) => void | Promise<void>;
|
|
1924
2350
|
|
|
1925
|
-
class
|
|
1926
|
-
#ctx:
|
|
2351
|
+
class TrackedWebSocketHandleAdapter implements UniversalWebSocket {
|
|
2352
|
+
#ctx: ActorContextHandleAdapter;
|
|
1927
2353
|
#inner: UniversalWebSocket;
|
|
1928
2354
|
#listeners = new Map<string, TrackedWebSocketListener[]>();
|
|
1929
2355
|
#onopen: ((event: RivetEvent) => void | Promise<void>) | null = null;
|
|
@@ -1932,7 +2358,7 @@ class TrackedNativeWebSocketAdapter implements UniversalWebSocket {
|
|
|
1932
2358
|
#onmessage: ((event: RivetMessageEvent) => void | Promise<void>) | null =
|
|
1933
2359
|
null;
|
|
1934
2360
|
|
|
1935
|
-
constructor(ctx:
|
|
2361
|
+
constructor(ctx: ActorContextHandleAdapter, inner: UniversalWebSocket) {
|
|
1936
2362
|
this.#ctx = ctx;
|
|
1937
2363
|
this.#inner = inner;
|
|
1938
2364
|
|
|
@@ -2003,10 +2429,12 @@ class TrackedNativeWebSocketAdapter implements UniversalWebSocket {
|
|
|
2003
2429
|
}
|
|
2004
2430
|
|
|
2005
2431
|
addEventListener(type: string, listener: TrackedWebSocketListener): void {
|
|
2006
|
-
|
|
2007
|
-
|
|
2432
|
+
let listeners = this.#listeners.get(type);
|
|
2433
|
+
if (!listeners) {
|
|
2434
|
+
listeners = [];
|
|
2435
|
+
this.#listeners.set(type, listeners);
|
|
2008
2436
|
}
|
|
2009
|
-
|
|
2437
|
+
listeners.push(listener);
|
|
2010
2438
|
}
|
|
2011
2439
|
|
|
2012
2440
|
removeEventListener(
|
|
@@ -2148,7 +2576,8 @@ class TrackedNativeWebSocketAdapter implements UniversalWebSocket {
|
|
|
2148
2576
|
})
|
|
2149
2577
|
.finally(() => {
|
|
2150
2578
|
this.#ctx.endWebSocketCallback(callbackRegionId);
|
|
2151
|
-
})
|
|
2579
|
+
})
|
|
2580
|
+
.then(() => null),
|
|
2152
2581
|
);
|
|
2153
2582
|
} catch (error) {
|
|
2154
2583
|
logger().error({
|
|
@@ -2169,45 +2598,151 @@ class TrackedNativeWebSocketAdapter implements UniversalWebSocket {
|
|
|
2169
2598
|
}
|
|
2170
2599
|
}
|
|
2171
2600
|
|
|
2172
|
-
|
|
2173
|
-
#
|
|
2174
|
-
#ctx:
|
|
2601
|
+
class NativeConnectionMap implements ReadonlyMap<string, NativeConnAdapter> {
|
|
2602
|
+
#runtime: CoreRuntime;
|
|
2603
|
+
#ctx: ActorContextHandle;
|
|
2604
|
+
#schemas: NativeValidationConfig;
|
|
2605
|
+
#assertCanMutateState: () => void;
|
|
2606
|
+
|
|
2607
|
+
constructor(
|
|
2608
|
+
runtime: CoreRuntime,
|
|
2609
|
+
ctx: ActorContextHandle,
|
|
2610
|
+
schemas: NativeValidationConfig,
|
|
2611
|
+
assertCanMutateState: () => void,
|
|
2612
|
+
) {
|
|
2613
|
+
this.#runtime = runtime;
|
|
2614
|
+
this.#ctx = ctx;
|
|
2615
|
+
this.#schemas = schemas;
|
|
2616
|
+
this.#assertCanMutateState = assertCanMutateState;
|
|
2617
|
+
}
|
|
2618
|
+
|
|
2619
|
+
#connToAdapter(conn: ConnHandle): NativeConnAdapter {
|
|
2620
|
+
return new NativeConnAdapter(
|
|
2621
|
+
this.#runtime,
|
|
2622
|
+
conn,
|
|
2623
|
+
this.#schemas,
|
|
2624
|
+
this.#ctx,
|
|
2625
|
+
(connId) =>
|
|
2626
|
+
callNativeSync(() =>
|
|
2627
|
+
this.#runtime.actorQueueHibernationRemoval(
|
|
2628
|
+
this.#ctx,
|
|
2629
|
+
connId,
|
|
2630
|
+
),
|
|
2631
|
+
),
|
|
2632
|
+
this.#assertCanMutateState,
|
|
2633
|
+
);
|
|
2634
|
+
}
|
|
2635
|
+
|
|
2636
|
+
get size(): number {
|
|
2637
|
+
return callNativeSync(() => this.#runtime.actorConns(this.#ctx)).length;
|
|
2638
|
+
}
|
|
2639
|
+
|
|
2640
|
+
get(key: string): NativeConnAdapter | undefined {
|
|
2641
|
+
const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx));
|
|
2642
|
+
const conn = conns.find((c) => this.#runtime.connId(c) === key);
|
|
2643
|
+
if (!conn) return undefined;
|
|
2644
|
+
return this.#connToAdapter(conn);
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
has(key: string): boolean {
|
|
2648
|
+
const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx));
|
|
2649
|
+
return conns.some((c) => this.#runtime.connId(c) === key);
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2652
|
+
keys(): MapIterator<string> {
|
|
2653
|
+
const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx));
|
|
2654
|
+
return conns
|
|
2655
|
+
.map((c) => this.#runtime.connId(c))
|
|
2656
|
+
[Symbol.iterator]() satisfies MapIterator<string>;
|
|
2657
|
+
}
|
|
2658
|
+
|
|
2659
|
+
values(): MapIterator<NativeConnAdapter> {
|
|
2660
|
+
const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx));
|
|
2661
|
+
return conns
|
|
2662
|
+
.map((c) => this.#connToAdapter(c))
|
|
2663
|
+
[Symbol.iterator]() satisfies MapIterator<NativeConnAdapter>;
|
|
2664
|
+
}
|
|
2665
|
+
|
|
2666
|
+
entries(): MapIterator<[string, NativeConnAdapter]> {
|
|
2667
|
+
const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx));
|
|
2668
|
+
return conns
|
|
2669
|
+
.map(
|
|
2670
|
+
(c) =>
|
|
2671
|
+
[this.#runtime.connId(c), this.#connToAdapter(c)] as [
|
|
2672
|
+
string,
|
|
2673
|
+
NativeConnAdapter,
|
|
2674
|
+
],
|
|
2675
|
+
)
|
|
2676
|
+
[Symbol.iterator]() satisfies MapIterator<
|
|
2677
|
+
[string, NativeConnAdapter]
|
|
2678
|
+
>;
|
|
2679
|
+
}
|
|
2680
|
+
|
|
2681
|
+
forEach(
|
|
2682
|
+
callback: (
|
|
2683
|
+
value: NativeConnAdapter,
|
|
2684
|
+
key: string,
|
|
2685
|
+
map: ReadonlyMap<string, NativeConnAdapter>,
|
|
2686
|
+
) => void,
|
|
2687
|
+
thisArg?: unknown,
|
|
2688
|
+
): void {
|
|
2689
|
+
const conns = callNativeSync(() => this.#runtime.actorConns(this.#ctx));
|
|
2690
|
+
for (const conn of conns) {
|
|
2691
|
+
const id = this.#runtime.connId(conn);
|
|
2692
|
+
callback.call(thisArg, this.#connToAdapter(conn), id, this);
|
|
2693
|
+
}
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2696
|
+
[Symbol.iterator](): MapIterator<[string, NativeConnAdapter]> {
|
|
2697
|
+
return this.entries();
|
|
2698
|
+
}
|
|
2699
|
+
|
|
2700
|
+
readonly [Symbol.toStringTag] = "NativeConnectionMap";
|
|
2701
|
+
}
|
|
2702
|
+
|
|
2703
|
+
export class ActorContextHandleAdapter {
|
|
2704
|
+
#runtime: CoreRuntime;
|
|
2705
|
+
#ctx: ActorContextHandle;
|
|
2175
2706
|
#schemas: NativeValidationConfig;
|
|
2176
2707
|
#abortSignal?: AbortSignal;
|
|
2177
2708
|
#abortSignalCleanup?: () => void;
|
|
2178
2709
|
#client?: AnyClient;
|
|
2179
2710
|
#clientFactory?: () => AnyClient;
|
|
2711
|
+
#connMap?: NativeConnectionMap;
|
|
2712
|
+
#cron?: NativeCronAdapter;
|
|
2180
2713
|
#databaseProvider?: Exclude<AnyDatabaseProvider, undefined>;
|
|
2181
2714
|
#db?: unknown;
|
|
2182
|
-
#
|
|
2183
|
-
#dispatchCancelToken?: NativeCancellationToken;
|
|
2715
|
+
#dispatchCancelToken?: CancellationTokenHandle;
|
|
2184
2716
|
#kv?: NativeKvAdapter;
|
|
2185
2717
|
#queue?: NativeQueueAdapter;
|
|
2186
2718
|
#request?: Request;
|
|
2187
2719
|
#schedule?: NativeScheduleAdapter;
|
|
2188
|
-
#
|
|
2189
|
-
#
|
|
2720
|
+
#run?: { setWakeAt(timestamp: number | null): Promise<void> };
|
|
2721
|
+
#runHandlerConfigured: boolean;
|
|
2190
2722
|
#onStateChange?: NativeOnStateChangeHandler;
|
|
2191
2723
|
#stateEnabled: boolean;
|
|
2724
|
+
#stateProxy?: unknown;
|
|
2725
|
+
#stateProxyTarget?: unknown;
|
|
2726
|
+
readonly #stateTransactionOwner = Symbol("actor-state-transaction-owner");
|
|
2192
2727
|
|
|
2193
2728
|
constructor(
|
|
2194
|
-
|
|
2195
|
-
ctx:
|
|
2729
|
+
runtime: CoreRuntime,
|
|
2730
|
+
ctx: ActorContextHandle,
|
|
2196
2731
|
clientFactory?: () => AnyClient,
|
|
2197
2732
|
schemas: NativeValidationConfig = {},
|
|
2198
2733
|
databaseProvider?: AnyDatabaseProvider,
|
|
2199
2734
|
request?: Request,
|
|
2200
2735
|
stateEnabled = true,
|
|
2201
|
-
runHandlerActiveProvider?: () => boolean,
|
|
2202
2736
|
onStateChange?: NativeOnStateChangeHandler,
|
|
2203
|
-
dispatchCancelToken?:
|
|
2737
|
+
dispatchCancelToken?: CancellationTokenHandle,
|
|
2738
|
+
runHandlerConfigured = false,
|
|
2204
2739
|
) {
|
|
2205
|
-
this.#
|
|
2740
|
+
this.#runtime = runtime;
|
|
2206
2741
|
this.#ctx = ctx;
|
|
2207
2742
|
this.#clientFactory = clientFactory;
|
|
2208
2743
|
this.#schemas = schemas;
|
|
2209
2744
|
this.#dispatchCancelToken = dispatchCancelToken;
|
|
2210
|
-
this.#
|
|
2745
|
+
this.#runHandlerConfigured = runHandlerConfigured;
|
|
2211
2746
|
this.#onStateChange = onStateChange;
|
|
2212
2747
|
this.#stateEnabled = stateEnabled;
|
|
2213
2748
|
if (databaseProvider) {
|
|
@@ -2215,7 +2750,7 @@ export class NativeActorContextAdapter {
|
|
|
2215
2750
|
}
|
|
2216
2751
|
this.#request = request;
|
|
2217
2752
|
(
|
|
2218
|
-
this as
|
|
2753
|
+
this as ActorContextHandleAdapter & {
|
|
2219
2754
|
[ACTOR_CONTEXT_INTERNAL_SYMBOL]?: unknown;
|
|
2220
2755
|
}
|
|
2221
2756
|
)[ACTOR_CONTEXT_INTERNAL_SYMBOL] = new NativeWorkflowRuntimeAdapter(
|
|
@@ -2225,16 +2760,15 @@ export class NativeActorContextAdapter {
|
|
|
2225
2760
|
|
|
2226
2761
|
get kv() {
|
|
2227
2762
|
if (!this.#kv) {
|
|
2228
|
-
this.#kv = new NativeKvAdapter(this.#ctx
|
|
2763
|
+
this.#kv = new NativeKvAdapter(this.#runtime, this.#ctx);
|
|
2229
2764
|
}
|
|
2230
2765
|
return this.#kv;
|
|
2231
2766
|
}
|
|
2232
2767
|
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
this.#
|
|
2236
|
-
|
|
2237
|
-
return this.#sql;
|
|
2768
|
+
async actorRuntimeSocket(): Promise<{ path: string }> {
|
|
2769
|
+
return await callNative(() =>
|
|
2770
|
+
this.#runtime.actorRuntimeSocketProvision(this.#ctx),
|
|
2771
|
+
);
|
|
2238
2772
|
}
|
|
2239
2773
|
|
|
2240
2774
|
get db() {
|
|
@@ -2242,32 +2776,25 @@ export class NativeActorContextAdapter {
|
|
|
2242
2776
|
throw databaseNotConfiguredError();
|
|
2243
2777
|
}
|
|
2244
2778
|
|
|
2245
|
-
if (
|
|
2246
|
-
this.#
|
|
2247
|
-
|
|
2248
|
-
{
|
|
2249
|
-
get: (_target, property) => {
|
|
2250
|
-
if (property === "then") {
|
|
2251
|
-
return undefined;
|
|
2252
|
-
}
|
|
2779
|
+
if (this.#db) {
|
|
2780
|
+
return this.#db;
|
|
2781
|
+
}
|
|
2253
2782
|
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
);
|
|
2260
|
-
if (typeof value !== "function") {
|
|
2261
|
-
return value;
|
|
2262
|
-
}
|
|
2263
|
-
return await value.apply(client, args);
|
|
2264
|
-
};
|
|
2265
|
-
},
|
|
2266
|
-
},
|
|
2267
|
-
);
|
|
2783
|
+
const runtimeState = getNativeRuntimeState(this.#runtime, this.#ctx);
|
|
2784
|
+
const cachedClient = runtimeState.databaseClient;
|
|
2785
|
+
if (cachedClient) {
|
|
2786
|
+
this.#db = this.#bindDatabaseClient(cachedClient.client);
|
|
2787
|
+
return this.#db;
|
|
2268
2788
|
}
|
|
2269
2789
|
|
|
2270
|
-
|
|
2790
|
+
throw databaseClientNotReadyError();
|
|
2791
|
+
}
|
|
2792
|
+
|
|
2793
|
+
[RAW_STATE_SYMBOL](): unknown {
|
|
2794
|
+
if (!this.#stateEnabled) {
|
|
2795
|
+
throw stateNotEnabledError();
|
|
2796
|
+
}
|
|
2797
|
+
return this.#readState();
|
|
2271
2798
|
}
|
|
2272
2799
|
|
|
2273
2800
|
get state(): unknown {
|
|
@@ -2275,15 +2802,34 @@ export class NativeActorContextAdapter {
|
|
|
2275
2802
|
throw stateNotEnabledError();
|
|
2276
2803
|
}
|
|
2277
2804
|
const nextState = this.#readState();
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2805
|
+
// Reading `c.state` rebuilds the deep write-through proxy, which
|
|
2806
|
+
// allocates fresh on-change caches and rewraps the whole tree. Memoize
|
|
2807
|
+
// the proxy keyed on the underlying state object so repeated reads and
|
|
2808
|
+
// deep read cascades reuse a single proxy.
|
|
2809
|
+
if (
|
|
2810
|
+
this.#stateProxy === undefined ||
|
|
2811
|
+
this.#stateProxyTarget !== nextState
|
|
2812
|
+
) {
|
|
2813
|
+
const assertCurrentState = () => {
|
|
2284
2814
|
this.#assertCanMutateState();
|
|
2285
|
-
|
|
2286
|
-
|
|
2815
|
+
if (this.#readState() !== nextState) {
|
|
2816
|
+
throw stateTransactionConflictError();
|
|
2817
|
+
}
|
|
2818
|
+
};
|
|
2819
|
+
this.#stateProxyTarget = nextState;
|
|
2820
|
+
this.#stateProxy = createWriteThroughProxy(
|
|
2821
|
+
nextState,
|
|
2822
|
+
(nextValue) => {
|
|
2823
|
+
assertCurrentState();
|
|
2824
|
+
this.#writeState(nextValue, { scheduleSave: true });
|
|
2825
|
+
},
|
|
2826
|
+
(newValue) => {
|
|
2827
|
+
assertCurrentState();
|
|
2828
|
+
assertJsonCompatValue(newValue);
|
|
2829
|
+
},
|
|
2830
|
+
);
|
|
2831
|
+
}
|
|
2832
|
+
return this.#stateProxy;
|
|
2287
2833
|
}
|
|
2288
2834
|
|
|
2289
2835
|
set state(value: unknown) {
|
|
@@ -2291,18 +2837,22 @@ export class NativeActorContextAdapter {
|
|
|
2291
2837
|
throw stateNotEnabledError();
|
|
2292
2838
|
}
|
|
2293
2839
|
this.#assertCanMutateState();
|
|
2294
|
-
|
|
2840
|
+
const nextValue = unwrapWriteThroughProxy(value);
|
|
2841
|
+
assertJsonCompatValue(nextValue);
|
|
2842
|
+
this.#writeState(nextValue, { scheduleSave: true });
|
|
2295
2843
|
}
|
|
2296
2844
|
|
|
2297
2845
|
initializeState(value: unknown): void {
|
|
2298
2846
|
if (!this.#stateEnabled) {
|
|
2299
2847
|
return;
|
|
2300
2848
|
}
|
|
2301
|
-
this.#writeState(value, {
|
|
2849
|
+
this.#writeState(unwrapWriteThroughProxy(value), {
|
|
2850
|
+
scheduleSave: false,
|
|
2851
|
+
});
|
|
2302
2852
|
}
|
|
2303
2853
|
|
|
2304
2854
|
get vars(): unknown {
|
|
2305
|
-
const runtimeState = getNativeRuntimeState(this.#ctx);
|
|
2855
|
+
const runtimeState = getNativeRuntimeState(this.#runtime, this.#ctx);
|
|
2306
2856
|
if (runtimeState.varsInitialized) {
|
|
2307
2857
|
return runtimeState.vars;
|
|
2308
2858
|
}
|
|
@@ -2313,7 +2863,7 @@ export class NativeActorContextAdapter {
|
|
|
2313
2863
|
}
|
|
2314
2864
|
|
|
2315
2865
|
set vars(value: unknown) {
|
|
2316
|
-
const runtimeState = getNativeRuntimeState(this.#ctx);
|
|
2866
|
+
const runtimeState = getNativeRuntimeState(this.#runtime, this.#ctx);
|
|
2317
2867
|
runtimeState.varsInitialized = true;
|
|
2318
2868
|
runtimeState.vars = value;
|
|
2319
2869
|
}
|
|
@@ -2321,53 +2871,83 @@ export class NativeActorContextAdapter {
|
|
|
2321
2871
|
get queue(): NativeQueueAdapter {
|
|
2322
2872
|
if (!this.#queue) {
|
|
2323
2873
|
this.#queue = new NativeQueueAdapter(
|
|
2324
|
-
|
|
2874
|
+
this.#runtime,
|
|
2875
|
+
this.#ctx,
|
|
2325
2876
|
this.#schemas.queues,
|
|
2326
2877
|
);
|
|
2327
2878
|
}
|
|
2328
2879
|
return this.#queue;
|
|
2329
2880
|
}
|
|
2330
2881
|
|
|
2882
|
+
get run() {
|
|
2883
|
+
if (!this.#run) {
|
|
2884
|
+
this.#run = {
|
|
2885
|
+
setWakeAt: async (timestamp: number | null) => {
|
|
2886
|
+
if (timestamp !== null && !this.#runHandlerConfigured) {
|
|
2887
|
+
throw runHandlerNotConfiguredError();
|
|
2888
|
+
}
|
|
2889
|
+
if (
|
|
2890
|
+
timestamp !== null &&
|
|
2891
|
+
(!Number.isSafeInteger(timestamp) || timestamp < 0)
|
|
2892
|
+
) {
|
|
2893
|
+
throw new TypeError(
|
|
2894
|
+
"Run wake timestamp must be a non-negative safe integer or null",
|
|
2895
|
+
);
|
|
2896
|
+
}
|
|
2897
|
+
await callNative(() =>
|
|
2898
|
+
this.#runtime.actorSetRunWakeAt(this.#ctx, timestamp),
|
|
2899
|
+
);
|
|
2900
|
+
},
|
|
2901
|
+
};
|
|
2902
|
+
}
|
|
2903
|
+
return this.#run;
|
|
2904
|
+
}
|
|
2905
|
+
|
|
2331
2906
|
get schedule(): NativeScheduleAdapter {
|
|
2332
2907
|
if (!this.#schedule) {
|
|
2333
2908
|
this.#schedule = new NativeScheduleAdapter(
|
|
2334
|
-
|
|
2909
|
+
this.#runtime,
|
|
2910
|
+
this.#ctx,
|
|
2335
2911
|
);
|
|
2336
2912
|
}
|
|
2337
2913
|
return this.#schedule;
|
|
2338
2914
|
}
|
|
2339
2915
|
|
|
2916
|
+
get cron(): NativeCronAdapter {
|
|
2917
|
+
if (!this.#cron) {
|
|
2918
|
+
this.#cron = new NativeCronAdapter(this.#runtime, this.#ctx);
|
|
2919
|
+
}
|
|
2920
|
+
return this.#cron;
|
|
2921
|
+
}
|
|
2922
|
+
|
|
2340
2923
|
get actorId(): string {
|
|
2341
|
-
return callNativeSync(() => this.#
|
|
2924
|
+
return callNativeSync(() => this.#runtime.actorId(this.#ctx));
|
|
2342
2925
|
}
|
|
2343
2926
|
|
|
2344
2927
|
get name(): string {
|
|
2345
|
-
return callNativeSync(() => this.#
|
|
2928
|
+
return callNativeSync(() => this.#runtime.actorName(this.#ctx));
|
|
2346
2929
|
}
|
|
2347
2930
|
|
|
2348
2931
|
get key(): string[] {
|
|
2349
|
-
return toActorKey(
|
|
2932
|
+
return toActorKey(
|
|
2933
|
+
callNativeSync(() => this.#runtime.actorKey(this.#ctx)),
|
|
2934
|
+
);
|
|
2350
2935
|
}
|
|
2351
2936
|
|
|
2352
2937
|
get region(): string {
|
|
2353
|
-
return callNativeSync(() => this.#
|
|
2938
|
+
return callNativeSync(() => this.#runtime.actorRegion(this.#ctx));
|
|
2354
2939
|
}
|
|
2355
2940
|
|
|
2356
|
-
get conns():
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
this.#ctx.queueHibernationRemoval(connId),
|
|
2367
|
-
),
|
|
2368
|
-
),
|
|
2369
|
-
]),
|
|
2370
|
-
);
|
|
2941
|
+
get conns(): ReadonlyMap<string, NativeConnAdapter> {
|
|
2942
|
+
if (!this.#connMap) {
|
|
2943
|
+
this.#connMap = new NativeConnectionMap(
|
|
2944
|
+
this.#runtime,
|
|
2945
|
+
this.#ctx,
|
|
2946
|
+
this.#schemas,
|
|
2947
|
+
() => this.#assertCanMutateState(),
|
|
2948
|
+
);
|
|
2949
|
+
}
|
|
2950
|
+
return this.#connMap;
|
|
2371
2951
|
}
|
|
2372
2952
|
|
|
2373
2953
|
get log() {
|
|
@@ -2397,7 +2977,9 @@ export class NativeActorContextAdapter {
|
|
|
2397
2977
|
|
|
2398
2978
|
if (
|
|
2399
2979
|
actorSignal.aborted ||
|
|
2400
|
-
this.#
|
|
2980
|
+
this.#runtime.cancellationTokenAborted(
|
|
2981
|
+
this.#dispatchCancelToken,
|
|
2982
|
+
)
|
|
2401
2983
|
) {
|
|
2402
2984
|
controller.abort();
|
|
2403
2985
|
} else {
|
|
@@ -2407,10 +2989,13 @@ export class NativeActorContextAdapter {
|
|
|
2407
2989
|
once: true,
|
|
2408
2990
|
});
|
|
2409
2991
|
callNativeSync(() =>
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2992
|
+
this.#runtime.onCancellationTokenCancelled(
|
|
2993
|
+
dispatchCancelToken,
|
|
2994
|
+
() => {
|
|
2995
|
+
cleanup();
|
|
2996
|
+
controller.abort();
|
|
2997
|
+
},
|
|
2998
|
+
),
|
|
2414
2999
|
);
|
|
2415
3000
|
}
|
|
2416
3001
|
|
|
@@ -2437,15 +3022,15 @@ export class NativeActorContextAdapter {
|
|
|
2437
3022
|
return this.#db;
|
|
2438
3023
|
}
|
|
2439
3024
|
|
|
2440
|
-
const runtimeState = getNativeRuntimeState(this.#ctx);
|
|
3025
|
+
const runtimeState = getNativeRuntimeState(this.#runtime, this.#ctx);
|
|
2441
3026
|
const cachedClient = runtimeState.databaseClient;
|
|
2442
3027
|
if (cachedClient) {
|
|
2443
|
-
this.#db = cachedClient.client;
|
|
3028
|
+
this.#db = this.#bindDatabaseClient(cachedClient.client);
|
|
2444
3029
|
return this.#db;
|
|
2445
3030
|
}
|
|
2446
3031
|
|
|
2447
3032
|
const actorId = this.actorId;
|
|
2448
|
-
const
|
|
3033
|
+
const createdClient = await this.#databaseProvider.createClient({
|
|
2449
3034
|
actorId,
|
|
2450
3035
|
kv: {
|
|
2451
3036
|
batchPut: async (entries) => {
|
|
@@ -2466,18 +3051,147 @@ export class NativeActorContextAdapter {
|
|
|
2466
3051
|
log: {
|
|
2467
3052
|
debug: (obj) => logger().debug(obj),
|
|
2468
3053
|
},
|
|
2469
|
-
nativeDatabaseProvider:
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
3054
|
+
nativeDatabaseProvider: registerNativeStateTransactionOpener(
|
|
3055
|
+
{
|
|
3056
|
+
open: async (requestedActorId) => {
|
|
3057
|
+
void requestedActorId;
|
|
3058
|
+
return getOrCreateNativeSqlDatabase(
|
|
3059
|
+
this.#runtime,
|
|
3060
|
+
this.#ctx,
|
|
3061
|
+
);
|
|
3062
|
+
},
|
|
2473
3063
|
},
|
|
2474
|
-
|
|
3064
|
+
async (timeoutMs, context) =>
|
|
3065
|
+
await getOrCreateNativeSqlDatabase(
|
|
3066
|
+
this.#runtime,
|
|
3067
|
+
this.#ctx,
|
|
3068
|
+
).beginStateTransaction(timeoutMs, context),
|
|
3069
|
+
),
|
|
2475
3070
|
});
|
|
2476
3071
|
runtimeState.databaseClient = {
|
|
2477
|
-
client,
|
|
3072
|
+
client: createdClient,
|
|
2478
3073
|
};
|
|
2479
|
-
this.#db =
|
|
2480
|
-
return
|
|
3074
|
+
this.#db = this.#bindDatabaseClient(createdClient);
|
|
3075
|
+
return this.#db;
|
|
3076
|
+
}
|
|
3077
|
+
|
|
3078
|
+
#bindDatabaseClient(client: unknown): unknown {
|
|
3079
|
+
return bindNativeStateTransactionContext(client, {
|
|
3080
|
+
enter: async () => await this.#enterStateTransaction(),
|
|
3081
|
+
exit: (scope) =>
|
|
3082
|
+
this.#exitStateTransaction(
|
|
3083
|
+
scope as NativeStateTransactionScope,
|
|
3084
|
+
),
|
|
3085
|
+
});
|
|
3086
|
+
}
|
|
3087
|
+
|
|
3088
|
+
async #enterStateTransaction(): Promise<NativeStateTransactionScope> {
|
|
3089
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
3090
|
+
let pendingOwners = actorState.pendingStateTransactionOwners;
|
|
3091
|
+
if (!pendingOwners) {
|
|
3092
|
+
pendingOwners = new Set();
|
|
3093
|
+
actorState.pendingStateTransactionOwners = pendingOwners;
|
|
3094
|
+
}
|
|
3095
|
+
if (
|
|
3096
|
+
actorState.activeStateTransactionOwner ===
|
|
3097
|
+
this.#stateTransactionOwner ||
|
|
3098
|
+
pendingOwners.has(this.#stateTransactionOwner)
|
|
3099
|
+
) {
|
|
3100
|
+
throw stateTransactionConflictError();
|
|
3101
|
+
}
|
|
3102
|
+
pendingOwners.add(this.#stateTransactionOwner);
|
|
3103
|
+
const predecessor =
|
|
3104
|
+
actorState.stateTransactionTail ?? Promise.resolve();
|
|
3105
|
+
let release!: () => void;
|
|
3106
|
+
const hold = new Promise<void>((resolve) => {
|
|
3107
|
+
release = resolve;
|
|
3108
|
+
});
|
|
3109
|
+
actorState.stateTransactionTail = predecessor.then(
|
|
3110
|
+
async () => await hold,
|
|
3111
|
+
);
|
|
3112
|
+
try {
|
|
3113
|
+
await predecessor;
|
|
3114
|
+
pendingOwners.delete(this.#stateTransactionOwner);
|
|
3115
|
+
actorState.activeStateTransactionOwner =
|
|
3116
|
+
this.#stateTransactionOwner;
|
|
3117
|
+
return {
|
|
3118
|
+
actorContext: this,
|
|
3119
|
+
actorStateBaseline: this.#stateEnabled
|
|
3120
|
+
? structuredClone(this.#readState())
|
|
3121
|
+
: undefined,
|
|
3122
|
+
connectionStateBaselines: new Map(
|
|
3123
|
+
callNativeSync(() =>
|
|
3124
|
+
this.#runtime.actorConns(this.#ctx),
|
|
3125
|
+
).map((conn) => [
|
|
3126
|
+
callNativeSync(() => this.#runtime.connId(conn)),
|
|
3127
|
+
new Uint8Array(
|
|
3128
|
+
callNativeSync(() => this.#runtime.connState(conn)),
|
|
3129
|
+
),
|
|
3130
|
+
]),
|
|
3131
|
+
),
|
|
3132
|
+
committed: false,
|
|
3133
|
+
release,
|
|
3134
|
+
};
|
|
3135
|
+
} catch (error) {
|
|
3136
|
+
pendingOwners.delete(this.#stateTransactionOwner);
|
|
3137
|
+
if (
|
|
3138
|
+
actorState.activeStateTransactionOwner ===
|
|
3139
|
+
this.#stateTransactionOwner
|
|
3140
|
+
) {
|
|
3141
|
+
actorState.activeStateTransactionOwner = undefined;
|
|
3142
|
+
}
|
|
3143
|
+
release();
|
|
3144
|
+
throw error;
|
|
3145
|
+
}
|
|
3146
|
+
}
|
|
3147
|
+
|
|
3148
|
+
#exitStateTransaction(scope: NativeStateTransactionScope): void {
|
|
3149
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
3150
|
+
try {
|
|
3151
|
+
if (!scope.committed) {
|
|
3152
|
+
this.#restoreStateTransactionBaseline(scope);
|
|
3153
|
+
}
|
|
3154
|
+
} finally {
|
|
3155
|
+
if (
|
|
3156
|
+
actorState.activeStateTransactionOwner ===
|
|
3157
|
+
this.#stateTransactionOwner
|
|
3158
|
+
) {
|
|
3159
|
+
actorState.activeStateTransactionOwner = undefined;
|
|
3160
|
+
}
|
|
3161
|
+
if (actorState.stateTransactionSaveDeferred) {
|
|
3162
|
+
actorState.stateTransactionSaveDeferred = false;
|
|
3163
|
+
this.#scheduleSave();
|
|
3164
|
+
}
|
|
3165
|
+
scope.release();
|
|
3166
|
+
}
|
|
3167
|
+
}
|
|
3168
|
+
|
|
3169
|
+
#restoreStateTransactionBaseline(scope: NativeStateTransactionScope): void {
|
|
3170
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
3171
|
+
if (actorState.pendingSaveHandle !== undefined) {
|
|
3172
|
+
clearImmediate(actorState.pendingSaveHandle);
|
|
3173
|
+
}
|
|
3174
|
+
actorState.pendingSaveHandle = undefined;
|
|
3175
|
+
actorState.saveScheduled = false;
|
|
3176
|
+
|
|
3177
|
+
if (this.#stateEnabled) {
|
|
3178
|
+
actorState.state = structuredClone(scope.actorStateBaseline);
|
|
3179
|
+
}
|
|
3180
|
+
for (const conn of callNativeSync(() =>
|
|
3181
|
+
this.#runtime.actorConns(this.#ctx),
|
|
3182
|
+
)) {
|
|
3183
|
+
const connId = callNativeSync(() => this.#runtime.connId(conn));
|
|
3184
|
+
const baseline = scope.connectionStateBaselines.get(connId);
|
|
3185
|
+
if (baseline === undefined) {
|
|
3186
|
+
continue;
|
|
3187
|
+
}
|
|
3188
|
+
callNativeSync(() => this.#runtime.connSetState(conn, baseline));
|
|
3189
|
+
const cached = actorState.connStates.get(connId);
|
|
3190
|
+
if (cached) {
|
|
3191
|
+
cached.state = decodeValue(baseline);
|
|
3192
|
+
}
|
|
3193
|
+
}
|
|
3194
|
+
this.#scheduleSave();
|
|
2481
3195
|
}
|
|
2482
3196
|
|
|
2483
3197
|
async prepare(): Promise<void> {
|
|
@@ -2500,9 +3214,8 @@ export class NativeActorContextAdapter {
|
|
|
2500
3214
|
|
|
2501
3215
|
async closeDatabase(): Promise<void> {
|
|
2502
3216
|
this.#db = undefined;
|
|
2503
|
-
this.#
|
|
2504
|
-
await
|
|
2505
|
-
await closeNativeSqlDatabase(this.#ctx);
|
|
3217
|
+
await closeNativeDatabaseClient(this.#runtime, this.#ctx);
|
|
3218
|
+
await closeNativeSqlDatabase(this.#runtime, this.#ctx);
|
|
2506
3219
|
}
|
|
2507
3220
|
|
|
2508
3221
|
broadcast(name: string, ...args: unknown[]): void {
|
|
@@ -2512,7 +3225,11 @@ export class NativeActorContextAdapter {
|
|
|
2512
3225
|
args,
|
|
2513
3226
|
);
|
|
2514
3227
|
callNativeSync(() =>
|
|
2515
|
-
this.#
|
|
3228
|
+
this.#runtime.actorBroadcast(
|
|
3229
|
+
this.#ctx,
|
|
3230
|
+
name,
|
|
3231
|
+
encodeValue(validatedArgs),
|
|
3232
|
+
),
|
|
2516
3233
|
);
|
|
2517
3234
|
}
|
|
2518
3235
|
|
|
@@ -2521,42 +3238,68 @@ export class NativeActorContextAdapter {
|
|
|
2521
3238
|
maxWait?: number;
|
|
2522
3239
|
}): Promise<void> {
|
|
2523
3240
|
if (opts?.immediate) {
|
|
3241
|
+
this.#assertCanAwaitStateSave();
|
|
2524
3242
|
await callNative(() =>
|
|
2525
|
-
this.#
|
|
3243
|
+
this.#runtime.actorRequestSaveAndWait(this.#ctx, {
|
|
3244
|
+
immediate: true,
|
|
3245
|
+
}),
|
|
2526
3246
|
);
|
|
2527
3247
|
return;
|
|
2528
3248
|
}
|
|
2529
3249
|
|
|
2530
3250
|
if (opts?.maxWait != null) {
|
|
2531
3251
|
callNativeSync(() =>
|
|
2532
|
-
this.#
|
|
3252
|
+
this.#runtime.actorRequestSave(this.#ctx, {
|
|
3253
|
+
maxWaitMs: opts.maxWait,
|
|
3254
|
+
}),
|
|
2533
3255
|
);
|
|
2534
3256
|
return;
|
|
2535
3257
|
}
|
|
2536
3258
|
|
|
2537
|
-
callNativeSync(() =>
|
|
3259
|
+
callNativeSync(() =>
|
|
3260
|
+
this.#runtime.actorRequestSave(this.#ctx, { immediate: false }),
|
|
3261
|
+
);
|
|
3262
|
+
}
|
|
3263
|
+
|
|
3264
|
+
async saveStateAndWorkflowBatch(
|
|
3265
|
+
writes: Array<{ key: Uint8Array; value: Uint8Array }>,
|
|
3266
|
+
): Promise<void> {
|
|
3267
|
+
this.#assertCanAwaitStateSave();
|
|
3268
|
+
await callNative(() =>
|
|
3269
|
+
this.#runtime.actorSaveStateAndWorkflowBatch(this.#ctx, writes),
|
|
3270
|
+
);
|
|
3271
|
+
}
|
|
3272
|
+
|
|
3273
|
+
#assertCanAwaitStateSave(): void {
|
|
3274
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
3275
|
+
if (
|
|
3276
|
+
actorState.activeStateTransactionOwner ===
|
|
3277
|
+
this.#stateTransactionOwner
|
|
3278
|
+
) {
|
|
3279
|
+
throw stateTransactionConflictError();
|
|
3280
|
+
}
|
|
2538
3281
|
}
|
|
2539
3282
|
|
|
2540
|
-
serializeForTick(reason: SerializeStateReason):
|
|
3283
|
+
serializeForTick(reason: SerializeStateReason): RuntimeStateDeltaPayload {
|
|
2541
3284
|
void reason;
|
|
2542
|
-
const actorState = getNativePersistState(this.#ctx);
|
|
3285
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
2543
3286
|
const connHibernationRemoved = callNativeSync(() =>
|
|
2544
|
-
this.#
|
|
3287
|
+
this.#runtime.actorTakePendingHibernationChanges(this.#ctx),
|
|
2545
3288
|
);
|
|
2546
3289
|
for (const connId of connHibernationRemoved) {
|
|
2547
3290
|
actorState.connStates.delete(connId);
|
|
2548
3291
|
}
|
|
2549
3292
|
const state =
|
|
2550
3293
|
this.#stateEnabled && this.#readState() !== undefined
|
|
2551
|
-
?
|
|
3294
|
+
? encodeValue(this.#readState())
|
|
2552
3295
|
: undefined;
|
|
2553
3296
|
const connHibernation = callNativeSync(() =>
|
|
2554
|
-
this.#
|
|
3297
|
+
this.#runtime.actorDirtyHibernatableConns(this.#ctx),
|
|
2555
3298
|
).map((conn) => {
|
|
2556
|
-
const connId = callNativeSync(() =>
|
|
3299
|
+
const connId = callNativeSync(() => this.#runtime.connId(conn));
|
|
2557
3300
|
return {
|
|
2558
3301
|
connId,
|
|
2559
|
-
bytes:
|
|
3302
|
+
bytes: callNativeSync(() => this.#runtime.connState(conn)),
|
|
2560
3303
|
};
|
|
2561
3304
|
});
|
|
2562
3305
|
|
|
@@ -2569,55 +3312,52 @@ export class NativeActorContextAdapter {
|
|
|
2569
3312
|
|
|
2570
3313
|
async restartRunHandler(): Promise<void> {
|
|
2571
3314
|
await callNative(async () => {
|
|
2572
|
-
this.#
|
|
3315
|
+
this.#runtime.actorRestartRunHandler(this.#ctx);
|
|
2573
3316
|
});
|
|
2574
3317
|
}
|
|
2575
3318
|
|
|
2576
3319
|
async setAlarm(timestampMs?: number): Promise<void> {
|
|
2577
3320
|
await callNative(async () => {
|
|
2578
|
-
this.#
|
|
3321
|
+
this.#runtime.actorSetAlarm(this.#ctx, timestampMs);
|
|
2579
3322
|
});
|
|
2580
3323
|
}
|
|
2581
3324
|
|
|
2582
3325
|
keepAwake<T>(promise: Promise<T>): Promise<T> {
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
// finalize). The promise value is returned unchanged; core only
|
|
2586
|
-
// observes the settle signal.
|
|
2587
|
-
//
|
|
2588
|
-
// Counter-arm race (acceptable): the NAPI `keep_awake` call is async,
|
|
2589
|
-
// so the Rust `keep_awake_guard()` increment happens on first poll of
|
|
2590
|
-
// the Rust future, not synchronously when JS calls this method. There
|
|
2591
|
-
// is a sub-millisecond window where idle-sleep evaluation could
|
|
2592
|
-
// observe `keep_awake_count == 0`. In practice the idle timer runs on
|
|
2593
|
-
// `sleep_timeout` (default 30s), so the next poll always observes the
|
|
2594
|
-
// counter before the timer fires. Same race exists for `waitUntil`.
|
|
2595
|
-
// We accept this trade-off in exchange for keeping the JS API
|
|
2596
|
-
// fire-and-forget; core stays the single source of truth for sleep
|
|
2597
|
-
// gating logic. Logging the rejection avoids unhandled-promise warnings
|
|
2598
|
-
// without blocking the caller.
|
|
2599
|
-
callNative(() =>
|
|
2600
|
-
this.#ctx.keepAwake(Promise.resolve(promise).then(() => null)),
|
|
2601
|
-
).catch((error) => {
|
|
2602
|
-
if (!isClosedTaskRegistrationError(error)) {
|
|
3326
|
+
const trackedPromise = Promise.resolve(promise)
|
|
3327
|
+
.catch((error) => {
|
|
2603
3328
|
logger().warn({
|
|
2604
|
-
msg: "keepAwake
|
|
3329
|
+
msg: "keepAwake promise rejected",
|
|
2605
3330
|
error: stringifyError(error),
|
|
2606
3331
|
});
|
|
3332
|
+
})
|
|
3333
|
+
.then(() => null);
|
|
3334
|
+
try {
|
|
3335
|
+
callNativeSync(() =>
|
|
3336
|
+
this.#runtime.actorKeepAwake(this.#ctx, trackedPromise),
|
|
3337
|
+
);
|
|
3338
|
+
} catch (error) {
|
|
3339
|
+
if (!isClosedTaskRegistrationError(error)) {
|
|
3340
|
+
throw error;
|
|
2607
3341
|
}
|
|
2608
|
-
}
|
|
3342
|
+
}
|
|
2609
3343
|
return promise;
|
|
2610
3344
|
}
|
|
2611
3345
|
|
|
2612
|
-
runHandlerActive(): boolean {
|
|
2613
|
-
return this.#runHandlerActiveProvider?.() ?? false;
|
|
2614
|
-
}
|
|
2615
|
-
|
|
2616
3346
|
internalKeepAwake<T>(run: Promise<T> | (() => Promise<T>)): Promise<T> {
|
|
2617
3347
|
const promise = typeof run === "function" ? run() : run;
|
|
2618
|
-
|
|
3348
|
+
// Track only completion, swallowing the outcome. The real result/error
|
|
3349
|
+
// is delivered through the returned `promise`; without a rejection
|
|
3350
|
+
// handler here every workflow yield (which rejects with SleepError)
|
|
3351
|
+
// would be funneled into the registered task and logged as a spurious
|
|
3352
|
+
// "keep_awake promise rejected" warning.
|
|
3353
|
+
const trackedPromise = promise.then(
|
|
3354
|
+
() => null,
|
|
3355
|
+
() => null,
|
|
3356
|
+
);
|
|
2619
3357
|
try {
|
|
2620
|
-
callNativeSync(() =>
|
|
3358
|
+
callNativeSync(() =>
|
|
3359
|
+
this.#runtime.actorRegisterTask(this.#ctx, trackedPromise),
|
|
3360
|
+
);
|
|
2621
3361
|
} catch (error) {
|
|
2622
3362
|
if (!isClosedTaskRegistrationError(error)) {
|
|
2623
3363
|
throw error;
|
|
@@ -2627,8 +3367,11 @@ export class NativeActorContextAdapter {
|
|
|
2627
3367
|
}
|
|
2628
3368
|
|
|
2629
3369
|
waitUntil(promise: Promise<unknown>): void {
|
|
3370
|
+
const trackedPromise = Promise.resolve(promise).then(() => null);
|
|
2630
3371
|
try {
|
|
2631
|
-
callNativeSync(() =>
|
|
3372
|
+
callNativeSync(() =>
|
|
3373
|
+
this.#runtime.actorWaitUntil(this.#ctx, trackedPromise),
|
|
3374
|
+
);
|
|
2632
3375
|
} catch (error) {
|
|
2633
3376
|
if (!isClosedTaskRegistrationError(error)) {
|
|
2634
3377
|
throw error;
|
|
@@ -2637,11 +3380,18 @@ export class NativeActorContextAdapter {
|
|
|
2637
3380
|
}
|
|
2638
3381
|
|
|
2639
3382
|
beginWebSocketCallback(): number {
|
|
2640
|
-
return callNativeSync(() =>
|
|
3383
|
+
return callNativeSync(() =>
|
|
3384
|
+
this.#runtime.actorBeginWebsocketCallback(this.#ctx),
|
|
3385
|
+
);
|
|
2641
3386
|
}
|
|
2642
3387
|
|
|
2643
3388
|
endWebSocketCallback(callbackRegionId: number): void {
|
|
2644
|
-
callNativeSync(() =>
|
|
3389
|
+
callNativeSync(() =>
|
|
3390
|
+
this.#runtime.actorEndWebsocketCallback(
|
|
3391
|
+
this.#ctx,
|
|
3392
|
+
callbackRegionId,
|
|
3393
|
+
),
|
|
3394
|
+
);
|
|
2645
3395
|
}
|
|
2646
3396
|
|
|
2647
3397
|
// Intentionally a no-op. `setPreventSleep` / `preventSleep` are kept on the
|
|
@@ -2665,15 +3415,16 @@ export class NativeActorContextAdapter {
|
|
|
2665
3415
|
}
|
|
2666
3416
|
|
|
2667
3417
|
sleep(): void {
|
|
2668
|
-
|
|
3418
|
+
this.#flushStateChange();
|
|
3419
|
+
callNativeSync(() => this.#runtime.actorSleep(this.#ctx));
|
|
2669
3420
|
}
|
|
2670
3421
|
|
|
2671
3422
|
destroy(): void {
|
|
2672
3423
|
// Call the native destroy first so it can throw `actor/starting` or
|
|
2673
3424
|
// `actor/stopping` without leaving an unresolved destroyCompletion
|
|
2674
3425
|
// promise behind in the native runtime state.
|
|
2675
|
-
callNativeSync(() => this.#
|
|
2676
|
-
markNativeDestroyRequested(this.#ctx);
|
|
3426
|
+
callNativeSync(() => this.#runtime.actorDestroy(this.#ctx));
|
|
3427
|
+
markNativeDestroyRequested(this.#runtime, this.#ctx);
|
|
2677
3428
|
}
|
|
2678
3429
|
|
|
2679
3430
|
client<T = AnyClient>(): T extends Registry<any> ? Client<T> : T {
|
|
@@ -2688,12 +3439,16 @@ export class NativeActorContextAdapter {
|
|
|
2688
3439
|
}
|
|
2689
3440
|
|
|
2690
3441
|
async dispose(): Promise<void> {
|
|
3442
|
+
// Flush any save coalesced for this tick before the context is torn
|
|
3443
|
+
// down so the request-save and onStateChange always run.
|
|
3444
|
+
this.#flushStateChange();
|
|
2691
3445
|
this.#abortSignalCleanup?.();
|
|
2692
|
-
this.#sql = undefined;
|
|
2693
3446
|
}
|
|
2694
3447
|
|
|
2695
3448
|
#createActorAbortSignal(): AbortSignal {
|
|
2696
|
-
const nativeSignal = callNativeSync(() =>
|
|
3449
|
+
const nativeSignal = callNativeSync(() =>
|
|
3450
|
+
this.#runtime.actorAbortSignal(this.#ctx),
|
|
3451
|
+
);
|
|
2697
3452
|
const controller = new AbortController();
|
|
2698
3453
|
if (nativeSignal.aborted) {
|
|
2699
3454
|
controller.abort();
|
|
@@ -2706,10 +3461,10 @@ export class NativeActorContextAdapter {
|
|
|
2706
3461
|
}
|
|
2707
3462
|
|
|
2708
3463
|
#readState(): unknown {
|
|
2709
|
-
const actorState = getNativePersistState(this.#ctx);
|
|
3464
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
2710
3465
|
if (actorState.state === undefined) {
|
|
2711
3466
|
actorState.state = decodeValue(
|
|
2712
|
-
callNativeSync(() => this.#
|
|
3467
|
+
callNativeSync(() => this.#runtime.actorState(this.#ctx)),
|
|
2713
3468
|
);
|
|
2714
3469
|
}
|
|
2715
3470
|
return actorState.state;
|
|
@@ -2721,33 +3476,80 @@ export class NativeActorContextAdapter {
|
|
|
2721
3476
|
scheduleSave: boolean;
|
|
2722
3477
|
},
|
|
2723
3478
|
): void {
|
|
2724
|
-
|
|
2725
|
-
const actorState = getNativePersistState(this.#ctx);
|
|
3479
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
2726
3480
|
actorState.state = value;
|
|
2727
3481
|
if (!options.scheduleSave) {
|
|
2728
3482
|
return;
|
|
2729
3483
|
}
|
|
2730
|
-
this.#
|
|
3484
|
+
this.#scheduleSave();
|
|
2731
3485
|
}
|
|
2732
3486
|
|
|
2733
3487
|
#assertCanMutateState(): void {
|
|
2734
|
-
const actorState = getNativePersistState(this.#ctx);
|
|
3488
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
2735
3489
|
if (actorState.isInOnStateChange) {
|
|
2736
3490
|
throw stateMutationReentrantError();
|
|
2737
3491
|
}
|
|
3492
|
+
if (
|
|
3493
|
+
actorState.activeStateTransactionOwner !==
|
|
3494
|
+
this.#stateTransactionOwner &&
|
|
3495
|
+
(actorState.activeStateTransactionOwner !== undefined ||
|
|
3496
|
+
(actorState.pendingStateTransactionOwners?.size ?? 0) > 0)
|
|
3497
|
+
) {
|
|
3498
|
+
throw stateTransactionConflictError();
|
|
3499
|
+
}
|
|
3500
|
+
}
|
|
3501
|
+
|
|
3502
|
+
/** @internal */
|
|
3503
|
+
assertCanMutateState(): void {
|
|
3504
|
+
this.#assertCanMutateState();
|
|
3505
|
+
}
|
|
3506
|
+
|
|
3507
|
+
// Coalesce the request-save and onStateChange work to once per event loop
|
|
3508
|
+
// tick. A synchronous burst of mutations (for example
|
|
3509
|
+
// `Object.assign(c.state, ...)`) would otherwise cross the NAPI boundary and
|
|
3510
|
+
// run onStateChange once per field, re-serializing the whole state each time
|
|
3511
|
+
// and pinning the event loop on large state.
|
|
3512
|
+
#scheduleSave(): void {
|
|
3513
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
3514
|
+
if (actorState.saveScheduled) {
|
|
3515
|
+
return;
|
|
3516
|
+
}
|
|
3517
|
+
actorState.saveScheduled = true;
|
|
3518
|
+
actorState.pendingSaveHandle = setImmediate(() => {
|
|
3519
|
+
this.#flushStateChange();
|
|
3520
|
+
});
|
|
2738
3521
|
}
|
|
2739
3522
|
|
|
2740
|
-
#
|
|
2741
|
-
const actorState = getNativePersistState(this.#ctx);
|
|
2742
|
-
|
|
2743
|
-
|
|
3523
|
+
#flushStateChange(): void {
|
|
3524
|
+
const actorState = getNativePersistState(this.#runtime, this.#ctx);
|
|
3525
|
+
if (!actorState.saveScheduled) {
|
|
3526
|
+
return;
|
|
3527
|
+
}
|
|
3528
|
+
if (actorState.activeStateTransactionOwner !== undefined) {
|
|
3529
|
+
actorState.saveScheduled = false;
|
|
3530
|
+
if (actorState.pendingSaveHandle !== undefined) {
|
|
3531
|
+
clearImmediate(actorState.pendingSaveHandle);
|
|
3532
|
+
actorState.pendingSaveHandle = undefined;
|
|
3533
|
+
}
|
|
3534
|
+
actorState.stateTransactionSaveDeferred = true;
|
|
3535
|
+
return;
|
|
3536
|
+
}
|
|
3537
|
+
actorState.saveScheduled = false;
|
|
3538
|
+
if (actorState.pendingSaveHandle !== undefined) {
|
|
3539
|
+
clearImmediate(actorState.pendingSaveHandle);
|
|
3540
|
+
actorState.pendingSaveHandle = undefined;
|
|
3541
|
+
}
|
|
3542
|
+
|
|
3543
|
+
callNativeSync(() =>
|
|
3544
|
+
this.#runtime.actorRequestSave(this.#ctx, { immediate: false }),
|
|
3545
|
+
);
|
|
2744
3546
|
|
|
2745
3547
|
if (!this.#onStateChange) {
|
|
2746
3548
|
return;
|
|
2747
3549
|
}
|
|
2748
3550
|
|
|
2749
3551
|
actorState.isInOnStateChange = true;
|
|
2750
|
-
callNativeSync(() => this.#
|
|
3552
|
+
callNativeSync(() => this.#runtime.actorBeginOnStateChange(this.#ctx));
|
|
2751
3553
|
let shouldFinish = true;
|
|
2752
3554
|
try {
|
|
2753
3555
|
const result = this.#onStateChange(
|
|
@@ -2765,13 +3567,17 @@ export class NativeActorContextAdapter {
|
|
|
2765
3567
|
})
|
|
2766
3568
|
.finally(() => {
|
|
2767
3569
|
actorState.isInOnStateChange = false;
|
|
2768
|
-
callNativeSync(() =>
|
|
3570
|
+
callNativeSync(() =>
|
|
3571
|
+
this.#runtime.actorEndOnStateChange(this.#ctx),
|
|
3572
|
+
);
|
|
2769
3573
|
});
|
|
2770
3574
|
}
|
|
2771
3575
|
} finally {
|
|
2772
3576
|
if (shouldFinish) {
|
|
2773
3577
|
actorState.isInOnStateChange = false;
|
|
2774
|
-
callNativeSync(() =>
|
|
3578
|
+
callNativeSync(() =>
|
|
3579
|
+
this.#runtime.actorEndOnStateChange(this.#ctx),
|
|
3580
|
+
);
|
|
2775
3581
|
}
|
|
2776
3582
|
}
|
|
2777
3583
|
}
|
|
@@ -2782,7 +3588,7 @@ type NativeWorkflowQueueMessage = Awaited<
|
|
|
2782
3588
|
>;
|
|
2783
3589
|
|
|
2784
3590
|
class NativeWorkflowRuntimeAdapter {
|
|
2785
|
-
#ctx:
|
|
3591
|
+
#ctx: ActorContextHandleAdapter;
|
|
2786
3592
|
#completions = new Map<string, (response?: unknown) => Promise<void>>();
|
|
2787
3593
|
|
|
2788
3594
|
readonly id: string;
|
|
@@ -2845,9 +3651,12 @@ class NativeWorkflowRuntimeAdapter {
|
|
|
2845
3651
|
immediate?: boolean;
|
|
2846
3652
|
maxWait?: number;
|
|
2847
3653
|
}) => Promise<void>;
|
|
3654
|
+
saveStateAndWorkflowBatch: (
|
|
3655
|
+
writes: Array<{ key: Uint8Array; value: Uint8Array }>,
|
|
3656
|
+
) => Promise<void>;
|
|
2848
3657
|
};
|
|
2849
3658
|
|
|
2850
|
-
constructor(ctx:
|
|
3659
|
+
constructor(ctx: ActorContextHandleAdapter) {
|
|
2851
3660
|
this.#ctx = ctx;
|
|
2852
3661
|
this.id = ctx.actorId;
|
|
2853
3662
|
this.driver = {
|
|
@@ -2920,13 +3729,12 @@ class NativeWorkflowRuntimeAdapter {
|
|
|
2920
3729
|
saveState: async (opts) => {
|
|
2921
3730
|
await this.#ctx.saveState(opts);
|
|
2922
3731
|
},
|
|
3732
|
+
saveStateAndWorkflowBatch: async (writes) => {
|
|
3733
|
+
await this.#ctx.saveStateAndWorkflowBatch(writes);
|
|
3734
|
+
},
|
|
2923
3735
|
};
|
|
2924
3736
|
}
|
|
2925
3737
|
|
|
2926
|
-
isRunHandlerActive(): boolean {
|
|
2927
|
-
return this.#ctx.runHandlerActive();
|
|
2928
|
-
}
|
|
2929
|
-
|
|
2930
3738
|
async restartRunHandler(): Promise<void> {
|
|
2931
3739
|
await this.#ctx.restartRunHandler();
|
|
2932
3740
|
}
|
|
@@ -2963,112 +3771,67 @@ class NativeWorkflowRuntimeAdapter {
|
|
|
2963
3771
|
}
|
|
2964
3772
|
}
|
|
2965
3773
|
|
|
2966
|
-
function
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
request?: Request,
|
|
2991
|
-
stateEnabled = true,
|
|
2992
|
-
onStateChange?: NativeOnStateChangeHandler,
|
|
2993
|
-
dispatchCancelToken?: NativeCancellationToken,
|
|
2994
|
-
) {
|
|
2995
|
-
return Object.assign(
|
|
2996
|
-
new NativeActorContextAdapter(
|
|
2997
|
-
bindings,
|
|
2998
|
-
ctx,
|
|
2999
|
-
clientFactory,
|
|
3000
|
-
schemas,
|
|
3001
|
-
databaseProvider,
|
|
3002
|
-
request,
|
|
3003
|
-
stateEnabled,
|
|
3004
|
-
undefined,
|
|
3005
|
-
onStateChange,
|
|
3006
|
-
dispatchCancelToken,
|
|
3007
|
-
),
|
|
3008
|
-
{
|
|
3009
|
-
conn: new NativeConnAdapter(conn, schemas, ctx, (connId) =>
|
|
3010
|
-
callNativeSync(() => ctx.queueHibernationRemoval(connId)),
|
|
3011
|
-
),
|
|
3012
|
-
},
|
|
3013
|
-
);
|
|
3014
|
-
}
|
|
3015
|
-
|
|
3016
|
-
function buildNativeRequestErrorResponse(
|
|
3017
|
-
encoding: Encoding,
|
|
3018
|
-
path: string,
|
|
3019
|
-
error: unknown,
|
|
3020
|
-
): Response {
|
|
3021
|
-
const { statusCode, group, code, message, metadata } = deconstructError(
|
|
3022
|
-
error,
|
|
3023
|
-
logger(),
|
|
3024
|
-
{
|
|
3025
|
-
path,
|
|
3026
|
-
runtime: "native",
|
|
3027
|
-
},
|
|
3028
|
-
false,
|
|
3029
|
-
);
|
|
3030
|
-
const body = serializeWithEncoding<
|
|
3031
|
-
protocol.HttpResponseError,
|
|
3032
|
-
HttpResponseErrorJson,
|
|
3033
|
-
{ group: string; code: string; message: string; metadata?: unknown }
|
|
3034
|
-
>(
|
|
3035
|
-
encoding,
|
|
3036
|
-
{ group, code, message, metadata },
|
|
3037
|
-
HTTP_RESPONSE_ERROR_VERSIONED,
|
|
3038
|
-
CLIENT_PROTOCOL_CURRENT_VERSION,
|
|
3039
|
-
HttpResponseErrorSchema,
|
|
3040
|
-
(value) => value,
|
|
3041
|
-
(value) => ({
|
|
3042
|
-
group: value.group,
|
|
3043
|
-
code: value.code,
|
|
3044
|
-
message: value.message,
|
|
3045
|
-
metadata:
|
|
3046
|
-
value.metadata === undefined
|
|
3047
|
-
? null
|
|
3048
|
-
: bufferToArrayBuffer(encodeCborCompat(value.metadata)),
|
|
3049
|
-
}),
|
|
3774
|
+
function withConnContext(
|
|
3775
|
+
runtime: CoreRuntime,
|
|
3776
|
+
ctx: ActorContextHandle,
|
|
3777
|
+
conn: ConnHandle,
|
|
3778
|
+
clientFactory?: () => AnyClient,
|
|
3779
|
+
schemas: NativeValidationConfig = {},
|
|
3780
|
+
databaseProvider?: AnyDatabaseProvider,
|
|
3781
|
+
request?: Request,
|
|
3782
|
+
stateEnabled = true,
|
|
3783
|
+
onStateChange?: NativeOnStateChangeHandler,
|
|
3784
|
+
dispatchCancelToken?: CancellationTokenHandle,
|
|
3785
|
+
runHandlerConfigured = false,
|
|
3786
|
+
) {
|
|
3787
|
+
const actorContext = new ActorContextHandleAdapter(
|
|
3788
|
+
runtime,
|
|
3789
|
+
ctx,
|
|
3790
|
+
clientFactory,
|
|
3791
|
+
schemas,
|
|
3792
|
+
databaseProvider,
|
|
3793
|
+
request,
|
|
3794
|
+
stateEnabled,
|
|
3795
|
+
onStateChange,
|
|
3796
|
+
dispatchCancelToken,
|
|
3797
|
+
runHandlerConfigured,
|
|
3050
3798
|
);
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
3799
|
+
return Object.assign(actorContext, {
|
|
3800
|
+
conn: new NativeConnAdapter(
|
|
3801
|
+
runtime,
|
|
3802
|
+
conn,
|
|
3803
|
+
schemas,
|
|
3804
|
+
ctx,
|
|
3805
|
+
(connId) =>
|
|
3806
|
+
callNativeSync(() =>
|
|
3807
|
+
runtime.actorQueueHibernationRemoval(ctx, connId),
|
|
3808
|
+
),
|
|
3809
|
+
() => actorContext.assertCanMutateState(),
|
|
3810
|
+
),
|
|
3057
3811
|
});
|
|
3058
3812
|
}
|
|
3059
3813
|
|
|
3060
3814
|
function buildActorConfig(
|
|
3061
3815
|
definition: AnyActorDefinition,
|
|
3062
3816
|
registryConfig: RegistryConfig,
|
|
3063
|
-
|
|
3817
|
+
runtimeKind: "napi" | "wasm",
|
|
3818
|
+
): RuntimeActorConfig {
|
|
3064
3819
|
const config = definition.config as unknown as Record<string, unknown>;
|
|
3065
3820
|
const options = (config.options ?? {}) as Record<string, unknown>;
|
|
3066
3821
|
const canHibernate = options.canHibernateWebSocket;
|
|
3822
|
+
const usesRemoteSqlite =
|
|
3823
|
+
sqliteBackendForConfig(registryConfig) === "remote";
|
|
3824
|
+
const sqliteProfiling = (
|
|
3825
|
+
config.db as { sqliteProfiling?: SqliteProfilingOptions } | undefined
|
|
3826
|
+
)?.sqliteProfiling;
|
|
3067
3827
|
|
|
3068
3828
|
return {
|
|
3069
3829
|
name: options.name as string | undefined,
|
|
3070
3830
|
icon: options.icon as string | undefined,
|
|
3071
|
-
hasDatabase:
|
|
3831
|
+
hasDatabase: true,
|
|
3832
|
+
remoteSqlite: usesRemoteSqlite,
|
|
3833
|
+
sqliteProfiling,
|
|
3834
|
+
enableActorRuntimeSocket: options.enableActorRuntimeSocket === true,
|
|
3072
3835
|
hasState:
|
|
3073
3836
|
config.state !== undefined ||
|
|
3074
3837
|
typeof config.createState === "function",
|
|
@@ -3095,6 +3858,7 @@ function buildActorConfig(
|
|
|
3095
3858
|
| number
|
|
3096
3859
|
| undefined,
|
|
3097
3860
|
maxQueueSize: options.maxQueueSize as number | undefined,
|
|
3861
|
+
maxSchedules: options.maxSchedules as number | undefined,
|
|
3098
3862
|
maxQueueMessageSize: options.maxQueueMessageSize as number | undefined,
|
|
3099
3863
|
maxIncomingMessageSize: registryConfig.maxIncomingMessageSize as
|
|
3100
3864
|
| number
|
|
@@ -3108,32 +3872,123 @@ function buildActorConfig(
|
|
|
3108
3872
|
preloadMaxConnectionsBytes: options.preloadMaxConnectionsBytes as
|
|
3109
3873
|
| number
|
|
3110
3874
|
| undefined,
|
|
3111
|
-
actions: Object.keys((config.actions
|
|
3875
|
+
actions: Object.keys(flattenActionHandlers(config.actions))
|
|
3112
3876
|
.sort()
|
|
3113
3877
|
.map((name) => ({ name })),
|
|
3878
|
+
inspectorTabs: buildInspectorTabs(config.inspector, runtimeKind),
|
|
3114
3879
|
};
|
|
3115
3880
|
}
|
|
3116
3881
|
|
|
3882
|
+
function buildInspectorTabs(
|
|
3883
|
+
inspector: unknown,
|
|
3884
|
+
runtimeKind: "napi" | "wasm",
|
|
3885
|
+
): Array<RuntimeInspectorTabEntry> | undefined {
|
|
3886
|
+
if (!inspector || typeof inspector !== "object") return undefined;
|
|
3887
|
+
const tabs = (inspector as { tabs?: unknown }).tabs;
|
|
3888
|
+
if (!Array.isArray(tabs) || tabs.length === 0) return undefined;
|
|
3889
|
+
return tabs.map((raw) => {
|
|
3890
|
+
const entry = raw as {
|
|
3891
|
+
id: string;
|
|
3892
|
+
label?: string;
|
|
3893
|
+
source?: string;
|
|
3894
|
+
icon?: string;
|
|
3895
|
+
hidden?: boolean;
|
|
3896
|
+
};
|
|
3897
|
+
if (entry.hidden === true) {
|
|
3898
|
+
return { id: entry.id, hidden: true };
|
|
3899
|
+
}
|
|
3900
|
+
|
|
3901
|
+
if (runtimeKind === "wasm") {
|
|
3902
|
+
if (entry.source !== undefined) {
|
|
3903
|
+
logger().warn(
|
|
3904
|
+
{
|
|
3905
|
+
tabId: entry.id,
|
|
3906
|
+
runtimeKind,
|
|
3907
|
+
},
|
|
3908
|
+
"inspector.tabs[].source is not supported on wasm runners (current host: wasm). Tab descriptors will still appear in the dashboard strip but the tab body will render a not-available placeholder.",
|
|
3909
|
+
);
|
|
3910
|
+
}
|
|
3911
|
+
return {
|
|
3912
|
+
id: entry.id,
|
|
3913
|
+
label: entry.label,
|
|
3914
|
+
icon: entry.icon,
|
|
3915
|
+
source: undefined,
|
|
3916
|
+
};
|
|
3917
|
+
}
|
|
3918
|
+
|
|
3919
|
+
const resolved =
|
|
3920
|
+
entry.source !== undefined
|
|
3921
|
+
? getNodePath().resolve(entry.source)
|
|
3922
|
+
: undefined;
|
|
3923
|
+
if (resolved !== undefined) {
|
|
3924
|
+
validateInspectorTabSource(entry.id, resolved);
|
|
3925
|
+
}
|
|
3926
|
+
return {
|
|
3927
|
+
id: entry.id,
|
|
3928
|
+
label: entry.label,
|
|
3929
|
+
icon: entry.icon,
|
|
3930
|
+
source: resolved,
|
|
3931
|
+
};
|
|
3932
|
+
});
|
|
3933
|
+
}
|
|
3934
|
+
|
|
3935
|
+
function validateInspectorTabSource(tabId: string, resolved: string): void {
|
|
3936
|
+
if (resolved === getNodePath().parse(resolved).root) {
|
|
3937
|
+
throw new Error(
|
|
3938
|
+
`inspector.tabs[id="${tabId}"].source resolves to the filesystem root (${resolved}). ` +
|
|
3939
|
+
"Point it at the tab's own static-asset directory instead.",
|
|
3940
|
+
);
|
|
3941
|
+
}
|
|
3942
|
+
let stat: import("node:fs").Stats;
|
|
3943
|
+
try {
|
|
3944
|
+
stat = getNodeFsSync().statSync(resolved);
|
|
3945
|
+
} catch (err) {
|
|
3946
|
+
const code = (err as NodeJS.ErrnoException)?.code;
|
|
3947
|
+
if (code === "ENOENT") {
|
|
3948
|
+
throw new Error(
|
|
3949
|
+
`inspector.tabs[id="${tabId}"].source (${resolved}) does not exist.`,
|
|
3950
|
+
);
|
|
3951
|
+
}
|
|
3952
|
+
if (code === "EACCES") {
|
|
3953
|
+
throw new Error(
|
|
3954
|
+
`inspector.tabs[id="${tabId}"].source (${resolved}) is not readable (EACCES).`,
|
|
3955
|
+
);
|
|
3956
|
+
}
|
|
3957
|
+
throw new Error(
|
|
3958
|
+
`inspector.tabs[id="${tabId}"].source (${resolved}) could not be stat'd: ${
|
|
3959
|
+
(err as Error)?.message ?? err
|
|
3960
|
+
}`,
|
|
3961
|
+
);
|
|
3962
|
+
}
|
|
3963
|
+
if (!stat.isDirectory()) {
|
|
3964
|
+
throw new Error(
|
|
3965
|
+
`inspector.tabs[id="${tabId}"].source (${resolved}) must be a directory, got ${
|
|
3966
|
+
stat.isFile() ? "file" : "non-directory"
|
|
3967
|
+
}.`,
|
|
3968
|
+
);
|
|
3969
|
+
}
|
|
3970
|
+
}
|
|
3971
|
+
|
|
3117
3972
|
export function buildNativeFactory(
|
|
3118
|
-
|
|
3973
|
+
runtime: CoreRuntime,
|
|
3119
3974
|
registryConfig: RegistryConfig,
|
|
3120
3975
|
definition: AnyActorDefinition,
|
|
3121
|
-
):
|
|
3976
|
+
): ActorFactoryHandle {
|
|
3122
3977
|
const config = definition.config as Record<string, any>;
|
|
3123
|
-
const databaseProvider = config.db as
|
|
3978
|
+
const databaseProvider = (config.db ?? defaultDb()) as Exclude<
|
|
3979
|
+
AnyDatabaseProvider,
|
|
3980
|
+
undefined
|
|
3981
|
+
>;
|
|
3982
|
+
const actionHandlers = flattenActionHandlers(config.actions);
|
|
3124
3983
|
const schemaConfig: NativeValidationConfig = {
|
|
3125
|
-
actionInputSchemas:
|
|
3984
|
+
actionInputSchemas: flattenActionInputSchemas(
|
|
3985
|
+
config.actions,
|
|
3986
|
+
config.actionInputSchemas,
|
|
3987
|
+
),
|
|
3126
3988
|
connParamsSchema: config.connParamsSchema,
|
|
3127
3989
|
events: config.events,
|
|
3128
3990
|
queues: config.queues,
|
|
3129
3991
|
};
|
|
3130
|
-
const actionHandlers = Object.fromEntries(
|
|
3131
|
-
(
|
|
3132
|
-
Object.entries(config.actions ?? {}) as Array<
|
|
3133
|
-
[string, (...args: Array<any>) => any]
|
|
3134
|
-
>
|
|
3135
|
-
).map(([name, handler]) => [name, handler]),
|
|
3136
|
-
);
|
|
3137
3992
|
const createClient = () =>
|
|
3138
3993
|
createClientWithDriver(
|
|
3139
3994
|
new RemoteEngineControlClient(
|
|
@@ -3141,19 +3996,24 @@ export function buildNativeFactory(
|
|
|
3141
3996
|
),
|
|
3142
3997
|
{ encoding: "bare" },
|
|
3143
3998
|
);
|
|
3144
|
-
const
|
|
3145
|
-
const
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
|
|
3149
|
-
const getNativeWorkflowInspector = (ctx:
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
callNativeSync(() =>
|
|
3153
|
-
|
|
3999
|
+
const run = getRunFunction(config.run);
|
|
4000
|
+
const runHandlerCoordinator =
|
|
4001
|
+
getRunInspectorKind(config.run) === "workflow"
|
|
4002
|
+
? new RunHandlerCoordinator(config.run)
|
|
4003
|
+
: undefined;
|
|
4004
|
+
const getNativeWorkflowInspector = (ctx: ActorContextHandle) => {
|
|
4005
|
+
const actorId = callNativeSync(() => runtime.actorId(ctx));
|
|
4006
|
+
const restart = () =>
|
|
4007
|
+
callNativeSync(() => runtime.actorRestartRunHandler(ctx));
|
|
4008
|
+
return (runHandlerCoordinator?.getInspector(actorId, restart)
|
|
4009
|
+
?.workflow ??
|
|
4010
|
+
getRunInspectorConfig(config.run, actorId)?.workflow) as
|
|
4011
|
+
| NativeWorkflowInspectorConfig
|
|
4012
|
+
| undefined;
|
|
4013
|
+
};
|
|
3154
4014
|
const onStateChange =
|
|
3155
4015
|
typeof config.onStateChange === "function"
|
|
3156
|
-
? (actorCtx:
|
|
4016
|
+
? (actorCtx: ActorContextHandleAdapter, nextState: unknown) => {
|
|
3157
4017
|
config.onStateChange(actorCtx, nextState);
|
|
3158
4018
|
}
|
|
3159
4019
|
: undefined;
|
|
@@ -3161,6 +4021,8 @@ export function buildNativeFactory(
|
|
|
3161
4021
|
const hasStaticVars = "vars" in config;
|
|
3162
4022
|
const hasStaticConnState = Object.hasOwn(config, "connState");
|
|
3163
4023
|
const hasDynamicConnState = typeof config.createConnState === "function";
|
|
4024
|
+
const onSleep =
|
|
4025
|
+
typeof config.onSleep === "function" ? config.onSleep : undefined;
|
|
3164
4026
|
const needsDisconnectCallback =
|
|
3165
4027
|
typeof config.onDisconnect === "function" ||
|
|
3166
4028
|
hasStaticConnState ||
|
|
@@ -3169,30 +4031,30 @@ export function buildNativeFactory(
|
|
|
3169
4031
|
const stateEnabled =
|
|
3170
4032
|
config.state !== undefined || typeof config.createState === "function";
|
|
3171
4033
|
const makeActorCtx = (
|
|
3172
|
-
ctx:
|
|
4034
|
+
ctx: ActorContextHandle,
|
|
3173
4035
|
request?: Request,
|
|
3174
|
-
cancelToken?:
|
|
4036
|
+
cancelToken?: CancellationTokenHandle,
|
|
3175
4037
|
) =>
|
|
3176
|
-
new
|
|
3177
|
-
|
|
4038
|
+
new ActorContextHandleAdapter(
|
|
4039
|
+
runtime,
|
|
3178
4040
|
ctx,
|
|
3179
4041
|
createClient,
|
|
3180
4042
|
schemaConfig,
|
|
3181
4043
|
databaseProvider,
|
|
3182
4044
|
request,
|
|
3183
4045
|
stateEnabled,
|
|
3184
|
-
() => isNativeRunHandlerActive(ctx),
|
|
3185
4046
|
onStateChange,
|
|
3186
4047
|
cancelToken,
|
|
4048
|
+
run !== undefined,
|
|
3187
4049
|
);
|
|
3188
4050
|
const makeConnCtx = (
|
|
3189
|
-
ctx:
|
|
3190
|
-
conn:
|
|
4051
|
+
ctx: ActorContextHandle,
|
|
4052
|
+
conn: ConnHandle,
|
|
3191
4053
|
request?: Request,
|
|
3192
|
-
cancelToken?:
|
|
4054
|
+
cancelToken?: CancellationTokenHandle,
|
|
3193
4055
|
) =>
|
|
3194
4056
|
withConnContext(
|
|
3195
|
-
|
|
4057
|
+
runtime,
|
|
3196
4058
|
ctx,
|
|
3197
4059
|
conn,
|
|
3198
4060
|
createClient,
|
|
@@ -3202,14 +4064,15 @@ export function buildNativeFactory(
|
|
|
3202
4064
|
stateEnabled,
|
|
3203
4065
|
onStateChange,
|
|
3204
4066
|
cancelToken,
|
|
4067
|
+
run !== undefined,
|
|
3205
4068
|
);
|
|
3206
4069
|
const maybeHandleNativeInspectorRequest = async (
|
|
3207
|
-
ctx:
|
|
3208
|
-
|
|
4070
|
+
ctx: ActorContextHandle,
|
|
4071
|
+
_rawRequest: {
|
|
3209
4072
|
method: string;
|
|
3210
4073
|
uri: string;
|
|
3211
4074
|
headers?: Record<string, string>;
|
|
3212
|
-
body?:
|
|
4075
|
+
body?: RuntimeBytes;
|
|
3213
4076
|
},
|
|
3214
4077
|
jsRequest: Request,
|
|
3215
4078
|
): Promise<Response | undefined> => {
|
|
@@ -3243,14 +4106,24 @@ export function buildNativeFactory(
|
|
|
3243
4106
|
},
|
|
3244
4107
|
);
|
|
3245
4108
|
};
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
);
|
|
3252
|
-
|
|
3253
|
-
|
|
4109
|
+
|
|
4110
|
+
// Tab config is delivered over the inspector WS `Init` message, not
|
|
4111
|
+
// HTTP. Only custom-tab assets remain a public per-actor path.
|
|
4112
|
+
const isPublicPerActorPath =
|
|
4113
|
+
jsRequest.method === "GET" &&
|
|
4114
|
+
url.pathname.startsWith("/inspector/custom-tabs/");
|
|
4115
|
+
|
|
4116
|
+
if (!isPublicPerActorPath) {
|
|
4117
|
+
try {
|
|
4118
|
+
await runtime.actorVerifyInspectorAuth(
|
|
4119
|
+
ctx,
|
|
4120
|
+
jsRequest.headers
|
|
4121
|
+
.get("authorization")
|
|
4122
|
+
?.replace(/^Bearer\s+/i, "") ?? null,
|
|
4123
|
+
);
|
|
4124
|
+
} catch (error) {
|
|
4125
|
+
return errorResponse(error, 401);
|
|
4126
|
+
}
|
|
3254
4127
|
}
|
|
3255
4128
|
|
|
3256
4129
|
const workflowHistory = () =>
|
|
@@ -3260,6 +4133,7 @@ export function buildNativeFactory(
|
|
|
3260
4133
|
const workflowState = async () =>
|
|
3261
4134
|
(await getNativeWorkflowInspector(ctx)?.getState?.()) ?? null;
|
|
3262
4135
|
const actorCtx = makeActorCtx(ctx, jsRequest);
|
|
4136
|
+
const sql = getOrCreateNativeSqlDatabase(runtime, ctx);
|
|
3263
4137
|
try {
|
|
3264
4138
|
if (
|
|
3265
4139
|
url.pathname === "/inspector/state" &&
|
|
@@ -3318,8 +4192,8 @@ export function buildNativeFactory(
|
|
|
3318
4192
|
Number.isFinite(parsedLimit) && parsedLimit > 0
|
|
3319
4193
|
? Math.floor(parsedLimit)
|
|
3320
4194
|
: 100;
|
|
3321
|
-
const
|
|
3322
|
-
|
|
4195
|
+
const allMessages =
|
|
4196
|
+
await runtime.actorQueueInspectMessages(ctx);
|
|
3323
4197
|
const truncated = allMessages.length > limit;
|
|
3324
4198
|
const messages = allMessages.slice(0, limit).map((m) => ({
|
|
3325
4199
|
id: m.id,
|
|
@@ -3328,11 +4202,61 @@ export function buildNativeFactory(
|
|
|
3328
4202
|
}));
|
|
3329
4203
|
return jsonResponse({
|
|
3330
4204
|
size: allMessages.length,
|
|
3331
|
-
maxSize:
|
|
4205
|
+
maxSize: runtime.actorQueueMaxSize(ctx),
|
|
3332
4206
|
truncated,
|
|
3333
4207
|
messages,
|
|
3334
4208
|
});
|
|
3335
4209
|
}
|
|
4210
|
+
if (
|
|
4211
|
+
url.pathname === "/inspector/queue" &&
|
|
4212
|
+
jsRequest.method === "DELETE"
|
|
4213
|
+
) {
|
|
4214
|
+
await runtime.actorQueueReset(ctx);
|
|
4215
|
+
return jsonResponse({});
|
|
4216
|
+
}
|
|
4217
|
+
if (
|
|
4218
|
+
url.pathname === "/inspector/queue" &&
|
|
4219
|
+
jsRequest.method === "POST"
|
|
4220
|
+
) {
|
|
4221
|
+
let body: { name?: string; body?: unknown };
|
|
4222
|
+
try {
|
|
4223
|
+
body = (await jsRequest.json()) as {
|
|
4224
|
+
name?: string;
|
|
4225
|
+
body?: unknown;
|
|
4226
|
+
};
|
|
4227
|
+
} catch {
|
|
4228
|
+
return errorResponse(
|
|
4229
|
+
new RivetError(
|
|
4230
|
+
"actor",
|
|
4231
|
+
"invalid_request",
|
|
4232
|
+
"Invalid inspector JSON body",
|
|
4233
|
+
{ public: true },
|
|
4234
|
+
),
|
|
4235
|
+
400,
|
|
4236
|
+
);
|
|
4237
|
+
}
|
|
4238
|
+
const name = body.name ?? "";
|
|
4239
|
+
if (name === "") {
|
|
4240
|
+
return errorResponse(
|
|
4241
|
+
new RivetError(
|
|
4242
|
+
"actor",
|
|
4243
|
+
"invalid_request",
|
|
4244
|
+
"Queue message name must not be empty",
|
|
4245
|
+
{ public: true },
|
|
4246
|
+
),
|
|
4247
|
+
400,
|
|
4248
|
+
);
|
|
4249
|
+
}
|
|
4250
|
+
const cbor = encodeCborCompat(
|
|
4251
|
+
(body.body ?? null) as JsonCompatValue,
|
|
4252
|
+
);
|
|
4253
|
+
const message = await runtime.actorQueueSend(ctx, name, cbor);
|
|
4254
|
+
return jsonResponse({
|
|
4255
|
+
id: message.id().toString(),
|
|
4256
|
+
name: message.name(),
|
|
4257
|
+
createdAtMs: message.createdAt(),
|
|
4258
|
+
});
|
|
4259
|
+
}
|
|
3336
4260
|
if (
|
|
3337
4261
|
url.pathname === "/inspector/traces" &&
|
|
3338
4262
|
jsRequest.method === "GET"
|
|
@@ -3370,6 +4294,10 @@ export function buildNativeFactory(
|
|
|
3370
4294
|
getNativeWorkflowInspector(ctx) !== undefined,
|
|
3371
4295
|
});
|
|
3372
4296
|
} catch (error) {
|
|
4297
|
+
logger().error({
|
|
4298
|
+
msg: "error replaying workflow history",
|
|
4299
|
+
error,
|
|
4300
|
+
});
|
|
3373
4301
|
return errorResponse(error);
|
|
3374
4302
|
}
|
|
3375
4303
|
}
|
|
@@ -3377,7 +4305,7 @@ export function buildNativeFactory(
|
|
|
3377
4305
|
url.pathname === "/inspector/database/schema" &&
|
|
3378
4306
|
jsRequest.method === "GET"
|
|
3379
4307
|
) {
|
|
3380
|
-
const db =
|
|
4308
|
+
const db = sql;
|
|
3381
4309
|
const tables = queryRows(
|
|
3382
4310
|
await db.query(
|
|
3383
4311
|
"SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '__drizzle_%' ORDER BY name",
|
|
@@ -3431,7 +4359,7 @@ export function buildNativeFactory(
|
|
|
3431
4359
|
);
|
|
3432
4360
|
const quoted = `"${table.replace(/"/g, '""')}"`;
|
|
3433
4361
|
const rows = queryRows(
|
|
3434
|
-
await
|
|
4362
|
+
await sql.query(
|
|
3435
4363
|
`SELECT * FROM ${quoted} LIMIT ? OFFSET ?`,
|
|
3436
4364
|
[
|
|
3437
4365
|
Math.max(0, Math.min(limit, 500)),
|
|
@@ -3474,15 +4402,11 @@ export function buildNativeFactory(
|
|
|
3474
4402
|
const bindings = normalizeSqlitePropertyBindings(
|
|
3475
4403
|
body.properties as Record<string, unknown>,
|
|
3476
4404
|
);
|
|
3477
|
-
const rows = queryRows(
|
|
3478
|
-
await actorCtx.sql.query(body.sql, bindings),
|
|
3479
|
-
);
|
|
4405
|
+
const rows = queryRows(await sql.query(body.sql, bindings));
|
|
3480
4406
|
return jsonResponse({ rows: jsonSafe(rows) });
|
|
3481
4407
|
}
|
|
3482
4408
|
const args = Array.isArray(body.args) ? body.args : [];
|
|
3483
|
-
const rows = queryRows(
|
|
3484
|
-
await actorCtx.sql.query(body.sql, args),
|
|
3485
|
-
);
|
|
4409
|
+
const rows = queryRows(await sql.query(body.sql, args));
|
|
3486
4410
|
return jsonResponse({ rows: jsonSafe(rows) });
|
|
3487
4411
|
}
|
|
3488
4412
|
if (
|
|
@@ -3490,7 +4414,7 @@ export function buildNativeFactory(
|
|
|
3490
4414
|
jsRequest.method === "GET"
|
|
3491
4415
|
) {
|
|
3492
4416
|
const inspectorSnapshot = callNativeSync(() =>
|
|
3493
|
-
|
|
4417
|
+
runtime.actorInspectorSnapshot(ctx),
|
|
3494
4418
|
);
|
|
3495
4419
|
return jsonResponse({
|
|
3496
4420
|
state: stateEnabled ? actorCtx.state : undefined,
|
|
@@ -3511,7 +4435,7 @@ export function buildNativeFactory(
|
|
|
3511
4435
|
rpcs: Object.keys(actionHandlers).sort(),
|
|
3512
4436
|
queueSize: inspectorSnapshot.queueSize,
|
|
3513
4437
|
isStateEnabled: stateEnabled,
|
|
3514
|
-
isDatabaseEnabled:
|
|
4438
|
+
isDatabaseEnabled: true,
|
|
3515
4439
|
isWorkflowEnabled:
|
|
3516
4440
|
getNativeWorkflowInspector(ctx) !== undefined,
|
|
3517
4441
|
workflowState: await workflowState(),
|
|
@@ -3537,18 +4461,46 @@ export function buildNativeFactory(
|
|
|
3537
4461
|
404,
|
|
3538
4462
|
);
|
|
3539
4463
|
}
|
|
3540
|
-
const body = (await jsRequest.json()) as {
|
|
4464
|
+
const body = (await jsRequest.json()) as {
|
|
4465
|
+
args?: unknown;
|
|
4466
|
+
properties?: unknown;
|
|
4467
|
+
};
|
|
4468
|
+
if (body.args !== undefined && body.properties !== undefined) {
|
|
4469
|
+
return jsonResponse(
|
|
4470
|
+
{ error: "use either args or properties, not both" },
|
|
4471
|
+
{ status: 400 },
|
|
4472
|
+
);
|
|
4473
|
+
}
|
|
4474
|
+
if (
|
|
4475
|
+
body.properties !== undefined &&
|
|
4476
|
+
(body.properties === null ||
|
|
4477
|
+
typeof body.properties !== "object" ||
|
|
4478
|
+
Array.isArray(body.properties))
|
|
4479
|
+
) {
|
|
4480
|
+
return jsonResponse(
|
|
4481
|
+
{ error: "properties must be an object" },
|
|
4482
|
+
{ status: 400 },
|
|
4483
|
+
);
|
|
4484
|
+
}
|
|
4485
|
+
const args =
|
|
4486
|
+
body.properties !== undefined
|
|
4487
|
+
? [body.properties]
|
|
4488
|
+
: normalizeArgs(body.args);
|
|
3541
4489
|
try {
|
|
3542
4490
|
const output = await action(
|
|
3543
4491
|
actorCtx,
|
|
3544
4492
|
...validateActionArgs(
|
|
3545
4493
|
schemaConfig.actionInputSchemas,
|
|
3546
4494
|
actionName,
|
|
3547
|
-
|
|
4495
|
+
args,
|
|
3548
4496
|
),
|
|
3549
4497
|
);
|
|
3550
4498
|
return jsonResponse({ output });
|
|
3551
4499
|
} catch (error) {
|
|
4500
|
+
logger().error({
|
|
4501
|
+
msg: "Error handling inspector action request",
|
|
4502
|
+
error,
|
|
4503
|
+
});
|
|
3552
4504
|
return errorResponse(error);
|
|
3553
4505
|
}
|
|
3554
4506
|
}
|
|
@@ -3563,6 +4515,10 @@ export function buildNativeFactory(
|
|
|
3563
4515
|
{ status: 404 },
|
|
3564
4516
|
);
|
|
3565
4517
|
} catch (error) {
|
|
4518
|
+
logger().error({
|
|
4519
|
+
msg: "Error handling inspector request",
|
|
4520
|
+
error,
|
|
4521
|
+
});
|
|
3566
4522
|
return errorResponse(error);
|
|
3567
4523
|
} finally {
|
|
3568
4524
|
await actorCtx.dispose();
|
|
@@ -3575,10 +4531,10 @@ export function buildNativeFactory(
|
|
|
3575
4531
|
async (
|
|
3576
4532
|
error: unknown,
|
|
3577
4533
|
payload: {
|
|
3578
|
-
ctx:
|
|
3579
|
-
input?:
|
|
4534
|
+
ctx: ActorContextHandle;
|
|
4535
|
+
input?: RuntimeBytes;
|
|
3580
4536
|
},
|
|
3581
|
-
): Promise<
|
|
4537
|
+
): Promise<RuntimeBytes> => {
|
|
3582
4538
|
const { ctx, input } = unwrapTsfnPayload(
|
|
3583
4539
|
error,
|
|
3584
4540
|
payload,
|
|
@@ -3611,8 +4567,8 @@ export function buildNativeFactory(
|
|
|
3611
4567
|
async (
|
|
3612
4568
|
error: unknown,
|
|
3613
4569
|
payload: {
|
|
3614
|
-
ctx:
|
|
3615
|
-
input?:
|
|
4570
|
+
ctx: ActorContextHandle;
|
|
4571
|
+
input?: RuntimeBytes;
|
|
3616
4572
|
},
|
|
3617
4573
|
): Promise<void> => {
|
|
3618
4574
|
const { ctx, input } = unwrapTsfnPayload(
|
|
@@ -3636,7 +4592,7 @@ export function buildNativeFactory(
|
|
|
3636
4592
|
? wrapNativeCallback(
|
|
3637
4593
|
async (
|
|
3638
4594
|
error: unknown,
|
|
3639
|
-
payload: { ctx:
|
|
4595
|
+
payload: { ctx: ActorContextHandle },
|
|
3640
4596
|
): Promise<void> => {
|
|
3641
4597
|
const { ctx } = unwrapTsfnPayload(error, payload);
|
|
3642
4598
|
const actorCtx = makeActorCtx(ctx);
|
|
@@ -3666,7 +4622,7 @@ export function buildNativeFactory(
|
|
|
3666
4622
|
async (
|
|
3667
4623
|
error: unknown,
|
|
3668
4624
|
payload: {
|
|
3669
|
-
ctx:
|
|
4625
|
+
ctx: ActorContextHandle;
|
|
3670
4626
|
isNew: boolean;
|
|
3671
4627
|
},
|
|
3672
4628
|
) => {
|
|
@@ -3697,7 +4653,7 @@ export function buildNativeFactory(
|
|
|
3697
4653
|
? wrapNativeCallback(
|
|
3698
4654
|
async (
|
|
3699
4655
|
error: unknown,
|
|
3700
|
-
payload: { ctx:
|
|
4656
|
+
payload: { ctx: ActorContextHandle },
|
|
3701
4657
|
) => {
|
|
3702
4658
|
const { ctx } = unwrapTsfnPayload(error, payload);
|
|
3703
4659
|
const actorCtx = makeActorCtx(ctx);
|
|
@@ -3719,7 +4675,7 @@ export function buildNativeFactory(
|
|
|
3719
4675
|
? wrapNativeCallback(
|
|
3720
4676
|
async (
|
|
3721
4677
|
error: unknown,
|
|
3722
|
-
payload: { ctx:
|
|
4678
|
+
payload: { ctx: ActorContextHandle },
|
|
3723
4679
|
) => {
|
|
3724
4680
|
const { ctx } = unwrapTsfnPayload(error, payload);
|
|
3725
4681
|
const actorCtx = makeActorCtx(ctx);
|
|
@@ -3731,59 +4687,82 @@ export function buildNativeFactory(
|
|
|
3731
4687
|
},
|
|
3732
4688
|
)
|
|
3733
4689
|
: undefined,
|
|
3734
|
-
onSleep:
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
|
|
3762
|
-
|
|
3763
|
-
|
|
3764
|
-
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
3773
|
-
|
|
4690
|
+
onSleep: wrapNativeCallback(
|
|
4691
|
+
async (error: unknown, payload: { ctx: ActorContextHandle }) => {
|
|
4692
|
+
const { ctx } = unwrapTsfnPayload(error, payload);
|
|
4693
|
+
const actorCtx = makeActorCtx(ctx);
|
|
4694
|
+
const actorId = callNativeSync(() => runtime.actorId(ctx));
|
|
4695
|
+
// TODO: Move this save hook into cleanupNativeSleepRuntimeState
|
|
4696
|
+
// so immediate and deferred sleep cleanup share one save-state
|
|
4697
|
+
// path instead of passing a callback through cleanup.
|
|
4698
|
+
const saveActorState = async () => {
|
|
4699
|
+
if (runtime.kind === "wasm") {
|
|
4700
|
+
// Wasm cannot use the native context save helper here because
|
|
4701
|
+
// the runtime owns the serialized state handoff.
|
|
4702
|
+
await runtime.actorSaveState(
|
|
4703
|
+
ctx,
|
|
4704
|
+
actorCtx.serializeForTick("save"),
|
|
4705
|
+
);
|
|
4706
|
+
} else {
|
|
4707
|
+
await actorCtx.saveState({
|
|
4708
|
+
immediate: true,
|
|
4709
|
+
});
|
|
4710
|
+
}
|
|
4711
|
+
};
|
|
4712
|
+
try {
|
|
4713
|
+
if (onSleep) {
|
|
4714
|
+
await onSleep(actorCtx);
|
|
4715
|
+
}
|
|
4716
|
+
await saveActorState();
|
|
4717
|
+
} finally {
|
|
4718
|
+
try {
|
|
4719
|
+
await cleanupNativeSleepRuntimeState(
|
|
4720
|
+
runtime,
|
|
4721
|
+
ctx,
|
|
4722
|
+
saveActorState,
|
|
4723
|
+
);
|
|
4724
|
+
} finally {
|
|
4725
|
+
runHandlerCoordinator?.destroy(actorId);
|
|
4726
|
+
disposeRunInspector(config.run, actorId);
|
|
4727
|
+
await actorCtx.dispose();
|
|
4728
|
+
}
|
|
4729
|
+
}
|
|
4730
|
+
},
|
|
4731
|
+
),
|
|
4732
|
+
onDestroy: wrapNativeCallback(
|
|
4733
|
+
async (error: unknown, payload: { ctx: ActorContextHandle }) => {
|
|
4734
|
+
const { ctx } = unwrapTsfnPayload(error, payload);
|
|
4735
|
+
const actorCtx = makeActorCtx(ctx);
|
|
4736
|
+
const actorId = callNativeSync(() => runtime.actorId(ctx));
|
|
4737
|
+
// Close run control before user cleanup so replay cannot race actor
|
|
4738
|
+
// destruction. Recreating this actor id receives a fresh controller.
|
|
4739
|
+
runHandlerCoordinator?.destroy(actorId);
|
|
4740
|
+
disposeRunInspector(config.run, actorId);
|
|
4741
|
+
try {
|
|
4742
|
+
if (typeof config.onDestroy === "function") {
|
|
4743
|
+
await config.onDestroy(actorCtx);
|
|
4744
|
+
}
|
|
4745
|
+
} finally {
|
|
4746
|
+
resolveNativeDestroy(runtime, ctx);
|
|
4747
|
+
await actorCtx.closeDatabase();
|
|
4748
|
+
clearNativeRuntimeState(runtime, ctx);
|
|
4749
|
+
await actorCtx.dispose();
|
|
4750
|
+
}
|
|
4751
|
+
},
|
|
4752
|
+
),
|
|
3774
4753
|
onBeforeConnect:
|
|
3775
4754
|
typeof config.onBeforeConnect === "function"
|
|
3776
4755
|
? wrapNativeCallback(
|
|
3777
4756
|
async (
|
|
3778
4757
|
error: unknown,
|
|
3779
4758
|
payload: {
|
|
3780
|
-
ctx:
|
|
3781
|
-
params:
|
|
4759
|
+
ctx: ActorContextHandle;
|
|
4760
|
+
params: RuntimeBytes;
|
|
3782
4761
|
request?: {
|
|
3783
4762
|
method: string;
|
|
3784
4763
|
uri: string;
|
|
3785
4764
|
headers?: Record<string, string>;
|
|
3786
|
-
body?:
|
|
4765
|
+
body?: RuntimeBytes;
|
|
3787
4766
|
};
|
|
3788
4767
|
},
|
|
3789
4768
|
) => {
|
|
@@ -3815,17 +4794,17 @@ export function buildNativeFactory(
|
|
|
3815
4794
|
async (
|
|
3816
4795
|
error: unknown,
|
|
3817
4796
|
payload: {
|
|
3818
|
-
ctx:
|
|
3819
|
-
conn:
|
|
3820
|
-
params:
|
|
4797
|
+
ctx: ActorContextHandle;
|
|
4798
|
+
conn: ConnHandle;
|
|
4799
|
+
params: RuntimeBytes;
|
|
3821
4800
|
request?: {
|
|
3822
4801
|
method: string;
|
|
3823
4802
|
uri: string;
|
|
3824
4803
|
headers?: Record<string, string>;
|
|
3825
|
-
body?:
|
|
4804
|
+
body?: RuntimeBytes;
|
|
3826
4805
|
};
|
|
3827
4806
|
},
|
|
3828
|
-
): Promise<
|
|
4807
|
+
): Promise<RuntimeBytes> => {
|
|
3829
4808
|
const { ctx, conn, params, request } =
|
|
3830
4809
|
unwrapTsfnPayload(error, payload);
|
|
3831
4810
|
const actorCtx = makeActorCtx(
|
|
@@ -3833,13 +4812,18 @@ export function buildNativeFactory(
|
|
|
3833
4812
|
request ? buildRequest(request) : undefined,
|
|
3834
4813
|
);
|
|
3835
4814
|
const connAdapter = new NativeConnAdapter(
|
|
4815
|
+
runtime,
|
|
3836
4816
|
conn,
|
|
3837
4817
|
schemaConfig,
|
|
3838
4818
|
ctx,
|
|
3839
4819
|
(connId) =>
|
|
3840
4820
|
callNativeSync(() =>
|
|
3841
|
-
|
|
4821
|
+
runtime.actorQueueHibernationRemoval(
|
|
4822
|
+
ctx,
|
|
4823
|
+
connId,
|
|
4824
|
+
),
|
|
3842
4825
|
),
|
|
4826
|
+
() => actorCtx.assertCanMutateState(),
|
|
3843
4827
|
);
|
|
3844
4828
|
try {
|
|
3845
4829
|
const nextConnState = hasStaticConnState
|
|
@@ -3865,13 +4849,13 @@ export function buildNativeFactory(
|
|
|
3865
4849
|
async (
|
|
3866
4850
|
error: unknown,
|
|
3867
4851
|
payload: {
|
|
3868
|
-
ctx:
|
|
3869
|
-
conn:
|
|
4852
|
+
ctx: ActorContextHandle;
|
|
4853
|
+
conn: ConnHandle;
|
|
3870
4854
|
request?: {
|
|
3871
4855
|
method: string;
|
|
3872
4856
|
uri: string;
|
|
3873
4857
|
headers?: Record<string, string>;
|
|
3874
|
-
body?:
|
|
4858
|
+
body?: RuntimeBytes;
|
|
3875
4859
|
};
|
|
3876
4860
|
},
|
|
3877
4861
|
) => {
|
|
@@ -3884,13 +4868,18 @@ export function buildNativeFactory(
|
|
|
3884
4868
|
request ? buildRequest(request) : undefined,
|
|
3885
4869
|
);
|
|
3886
4870
|
const connAdapter = new NativeConnAdapter(
|
|
4871
|
+
runtime,
|
|
3887
4872
|
conn,
|
|
3888
4873
|
schemaConfig,
|
|
3889
4874
|
ctx,
|
|
3890
4875
|
(connId) =>
|
|
3891
4876
|
callNativeSync(() =>
|
|
3892
|
-
|
|
4877
|
+
runtime.actorQueueHibernationRemoval(
|
|
4878
|
+
ctx,
|
|
4879
|
+
connId,
|
|
4880
|
+
),
|
|
3893
4881
|
),
|
|
4882
|
+
() => actorCtx.assertCanMutateState(),
|
|
3894
4883
|
);
|
|
3895
4884
|
try {
|
|
3896
4885
|
await config.onConnect(
|
|
@@ -3910,8 +4899,8 @@ export function buildNativeFactory(
|
|
|
3910
4899
|
async (
|
|
3911
4900
|
error: unknown,
|
|
3912
4901
|
payload: {
|
|
3913
|
-
ctx:
|
|
3914
|
-
conn:
|
|
4902
|
+
ctx: ActorContextHandle;
|
|
4903
|
+
conn: ConnHandle;
|
|
3915
4904
|
},
|
|
3916
4905
|
) => {
|
|
3917
4906
|
const { ctx, conn } = unwrapTsfnPayload(error, payload);
|
|
@@ -3923,15 +4912,18 @@ export function buildNativeFactory(
|
|
|
3923
4912
|
await config.onDisconnect(
|
|
3924
4913
|
actorCtx,
|
|
3925
4914
|
new NativeConnAdapter(
|
|
4915
|
+
runtime,
|
|
3926
4916
|
conn,
|
|
3927
4917
|
schemaConfig,
|
|
3928
4918
|
ctx,
|
|
3929
4919
|
(connId) =>
|
|
3930
4920
|
callNativeSync(() =>
|
|
3931
|
-
|
|
4921
|
+
runtime.actorQueueHibernationRemoval(
|
|
4922
|
+
ctx,
|
|
3932
4923
|
connId,
|
|
3933
4924
|
),
|
|
3934
4925
|
),
|
|
4926
|
+
() => actorCtx.assertCanMutateState(),
|
|
3935
4927
|
),
|
|
3936
4928
|
);
|
|
3937
4929
|
}
|
|
@@ -3952,8 +4944,8 @@ export function buildNativeFactory(
|
|
|
3952
4944
|
async (
|
|
3953
4945
|
error: unknown,
|
|
3954
4946
|
payload: {
|
|
3955
|
-
ctx:
|
|
3956
|
-
conn:
|
|
4947
|
+
ctx: ActorContextHandle;
|
|
4948
|
+
conn: ConnHandle;
|
|
3957
4949
|
eventName: string;
|
|
3958
4950
|
},
|
|
3959
4951
|
) => {
|
|
@@ -3991,10 +4983,10 @@ export function buildNativeFactory(
|
|
|
3991
4983
|
async (
|
|
3992
4984
|
error: unknown,
|
|
3993
4985
|
payload: {
|
|
3994
|
-
ctx:
|
|
4986
|
+
ctx: ActorContextHandle;
|
|
3995
4987
|
name: string;
|
|
3996
|
-
args:
|
|
3997
|
-
output:
|
|
4988
|
+
args: RuntimeBytes;
|
|
4989
|
+
output: RuntimeBytes;
|
|
3998
4990
|
},
|
|
3999
4991
|
) => {
|
|
4000
4992
|
const { ctx, name, args, output } =
|
|
@@ -4019,14 +5011,14 @@ export function buildNativeFactory(
|
|
|
4019
5011
|
async (
|
|
4020
5012
|
error: unknown,
|
|
4021
5013
|
payload: {
|
|
4022
|
-
ctx:
|
|
5014
|
+
ctx: ActorContextHandle;
|
|
4023
5015
|
request: {
|
|
4024
5016
|
method: string;
|
|
4025
5017
|
uri: string;
|
|
4026
5018
|
headers?: Record<string, string>;
|
|
4027
|
-
body?:
|
|
5019
|
+
body?: RuntimeBytes;
|
|
4028
5020
|
};
|
|
4029
|
-
cancelToken?:
|
|
5021
|
+
cancelToken?: CancellationTokenHandle;
|
|
4030
5022
|
},
|
|
4031
5023
|
) => {
|
|
4032
5024
|
try {
|
|
@@ -4042,11 +5034,11 @@ export function buildNativeFactory(
|
|
|
4042
5034
|
jsRequest,
|
|
4043
5035
|
);
|
|
4044
5036
|
if (inspectorResponse) {
|
|
4045
|
-
return await
|
|
5037
|
+
return await toRuntimeHttpResponse(inspectorResponse);
|
|
4046
5038
|
}
|
|
4047
5039
|
|
|
4048
5040
|
if (typeof config.onRequest !== "function") {
|
|
4049
|
-
return await
|
|
5041
|
+
return await toRuntimeHttpResponse(
|
|
4050
5042
|
new Response(null, { status: 404 }),
|
|
4051
5043
|
);
|
|
4052
5044
|
}
|
|
@@ -4056,7 +5048,7 @@ export function buildNativeFactory(
|
|
|
4056
5048
|
let requestCtx:
|
|
4057
5049
|
| ReturnType<typeof withConnContext>
|
|
4058
5050
|
| undefined;
|
|
4059
|
-
let conn:
|
|
5051
|
+
let conn: ConnHandle | undefined;
|
|
4060
5052
|
try {
|
|
4061
5053
|
const connParams = validateConnParams(
|
|
4062
5054
|
schemaConfig.connParamsSchema,
|
|
@@ -4065,7 +5057,11 @@ export function buildNativeFactory(
|
|
|
4065
5057
|
: undefined,
|
|
4066
5058
|
);
|
|
4067
5059
|
conn = await callNative(() =>
|
|
4068
|
-
|
|
5060
|
+
runtime.actorConnectConn(
|
|
5061
|
+
ctx,
|
|
5062
|
+
encodeValue(connParams),
|
|
5063
|
+
request,
|
|
5064
|
+
),
|
|
4069
5065
|
);
|
|
4070
5066
|
requestCtx = makeConnCtx(
|
|
4071
5067
|
ctx,
|
|
@@ -4077,32 +5073,16 @@ export function buildNativeFactory(
|
|
|
4077
5073
|
requestCtx,
|
|
4078
5074
|
jsRequest,
|
|
4079
5075
|
);
|
|
4080
|
-
if (!(response
|
|
5076
|
+
if (!isResponseLike(response)) {
|
|
4081
5077
|
throw new Error(
|
|
4082
5078
|
"onRequest handler must return a Response",
|
|
4083
5079
|
);
|
|
4084
5080
|
}
|
|
4085
|
-
return await
|
|
4086
|
-
} catch (error) {
|
|
4087
|
-
const encodingHeader =
|
|
4088
|
-
jsRequest.headers.get(HEADER_ENCODING);
|
|
4089
|
-
const encoding: Encoding =
|
|
4090
|
-
encodingHeader === "cbor" ||
|
|
4091
|
-
encodingHeader === "bare"
|
|
4092
|
-
? encodingHeader
|
|
4093
|
-
: "json";
|
|
4094
|
-
const path = new URL(jsRequest.url).pathname;
|
|
4095
|
-
return await toJsHttpResponse(
|
|
4096
|
-
buildNativeRequestErrorResponse(
|
|
4097
|
-
encoding,
|
|
4098
|
-
path,
|
|
4099
|
-
error,
|
|
4100
|
-
),
|
|
4101
|
-
);
|
|
5081
|
+
return await toRuntimeHttpResponse(response);
|
|
4102
5082
|
} finally {
|
|
4103
5083
|
await requestCtx?.dispose();
|
|
4104
5084
|
if (conn) {
|
|
4105
|
-
await
|
|
5085
|
+
await runtime.connDisconnect(conn);
|
|
4106
5086
|
}
|
|
4107
5087
|
}
|
|
4108
5088
|
} catch (error) {
|
|
@@ -4120,30 +5100,29 @@ export function buildNativeFactory(
|
|
|
4120
5100
|
async (
|
|
4121
5101
|
error: unknown,
|
|
4122
5102
|
payload: {
|
|
4123
|
-
ctx:
|
|
4124
|
-
|
|
5103
|
+
ctx: ActorContextHandle;
|
|
5104
|
+
conn: ConnHandle;
|
|
5105
|
+
ws: WebSocketHandle;
|
|
4125
5106
|
request?: {
|
|
4126
5107
|
method: string;
|
|
4127
5108
|
uri: string;
|
|
4128
5109
|
headers?: Record<string, string>;
|
|
4129
|
-
body?:
|
|
5110
|
+
body?: RuntimeBytes;
|
|
4130
5111
|
};
|
|
4131
5112
|
},
|
|
4132
5113
|
) => {
|
|
4133
|
-
const { ctx, ws, request } =
|
|
4134
|
-
error,
|
|
4135
|
-
payload,
|
|
4136
|
-
);
|
|
5114
|
+
const { ctx, conn, ws, request } =
|
|
5115
|
+
unwrapTsfnPayload(error, payload);
|
|
4137
5116
|
const jsRequest = request
|
|
4138
5117
|
? buildRequest(request)
|
|
4139
5118
|
: undefined;
|
|
4140
|
-
const actorCtx =
|
|
5119
|
+
const actorCtx = makeConnCtx(ctx, conn, jsRequest);
|
|
4141
5120
|
try {
|
|
4142
5121
|
await config.onWebSocket(
|
|
4143
5122
|
actorCtx,
|
|
4144
|
-
new
|
|
5123
|
+
new TrackedWebSocketHandleAdapter(
|
|
4145
5124
|
actorCtx,
|
|
4146
|
-
new NativeWebSocketAdapter(ws),
|
|
5125
|
+
new NativeWebSocketAdapter(runtime, ws),
|
|
4147
5126
|
),
|
|
4148
5127
|
);
|
|
4149
5128
|
} finally {
|
|
@@ -4153,7 +5132,6 @@ export function buildNativeFactory(
|
|
|
4153
5132
|
)
|
|
4154
5133
|
: undefined,
|
|
4155
5134
|
run: (() => {
|
|
4156
|
-
const run = getRunFunction(config.run);
|
|
4157
5135
|
if (!run) {
|
|
4158
5136
|
return undefined;
|
|
4159
5137
|
}
|
|
@@ -4161,67 +5139,79 @@ export function buildNativeFactory(
|
|
|
4161
5139
|
return wrapNativeCallback(
|
|
4162
5140
|
async (
|
|
4163
5141
|
error: unknown,
|
|
4164
|
-
payload: { ctx:
|
|
5142
|
+
payload: { ctx: ActorContextHandle },
|
|
4165
5143
|
) => {
|
|
4166
5144
|
const { ctx } = unwrapTsfnPayload(error, payload);
|
|
4167
|
-
const actorId = callNativeSync(() =>
|
|
4168
|
-
const
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4173
|
-
|
|
4174
|
-
|
|
5145
|
+
const actorId = callNativeSync(() => runtime.actorId(ctx));
|
|
5146
|
+
const executeRun = async () => {
|
|
5147
|
+
const actorCtx = makeActorCtx(ctx);
|
|
5148
|
+
try {
|
|
5149
|
+
await run(actorCtx);
|
|
5150
|
+
} finally {
|
|
5151
|
+
await actorCtx.dispose();
|
|
5152
|
+
}
|
|
5153
|
+
};
|
|
5154
|
+
if (runHandlerCoordinator) {
|
|
5155
|
+
const outcome = await runHandlerCoordinator.run(
|
|
5156
|
+
actorId,
|
|
5157
|
+
() =>
|
|
5158
|
+
callNativeSync(() =>
|
|
5159
|
+
runtime.actorRestartRunHandler(ctx),
|
|
5160
|
+
),
|
|
5161
|
+
executeRun,
|
|
5162
|
+
);
|
|
5163
|
+
// Only a start suppressed behind a failed replay must preserve a
|
|
5164
|
+
// consumed durable wake. Closed actors must remain closed.
|
|
5165
|
+
return outcome !== "suppressed";
|
|
4175
5166
|
}
|
|
5167
|
+
await executeRun();
|
|
5168
|
+
return true;
|
|
4176
5169
|
},
|
|
4177
5170
|
);
|
|
4178
5171
|
})(),
|
|
4179
|
-
getWorkflowHistory:
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
5172
|
+
getWorkflowHistory: hasRunInspectorConfig(config.run)
|
|
5173
|
+
? wrapNativeCallback(
|
|
5174
|
+
async (
|
|
5175
|
+
error: unknown,
|
|
5176
|
+
payload: { ctx: ActorContextHandle },
|
|
5177
|
+
) => {
|
|
5178
|
+
const { ctx } = unwrapTsfnPayload(error, payload);
|
|
5179
|
+
const history =
|
|
5180
|
+
getNativeWorkflowInspector(ctx)?.getHistory();
|
|
5181
|
+
return history == null
|
|
5182
|
+
? undefined
|
|
5183
|
+
: toUint8Array(history);
|
|
5184
|
+
},
|
|
5185
|
+
)
|
|
5186
|
+
: undefined,
|
|
5187
|
+
replayWorkflow: hasRunInspectorConfig(config.run)
|
|
5188
|
+
? wrapNativeCallback(
|
|
5189
|
+
async (
|
|
5190
|
+
error: unknown,
|
|
5191
|
+
payload: {
|
|
5192
|
+
ctx: ActorContextHandle;
|
|
5193
|
+
entryId?: string;
|
|
4192
5194
|
},
|
|
4193
|
-
)
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
},
|
|
4204
|
-
) => {
|
|
4205
|
-
const { ctx, entryId } = unwrapTsfnPayload(
|
|
4206
|
-
error,
|
|
4207
|
-
payload,
|
|
4208
|
-
);
|
|
4209
|
-
const workflowInspector =
|
|
4210
|
-
getNativeWorkflowInspector(ctx);
|
|
4211
|
-
if (!workflowInspector?.replayFromStep) {
|
|
4212
|
-
return undefined;
|
|
4213
|
-
}
|
|
5195
|
+
) => {
|
|
5196
|
+
const { ctx, entryId } = unwrapTsfnPayload(
|
|
5197
|
+
error,
|
|
5198
|
+
payload,
|
|
5199
|
+
);
|
|
5200
|
+
const workflowInspector =
|
|
5201
|
+
getNativeWorkflowInspector(ctx);
|
|
5202
|
+
if (!workflowInspector?.replayFromStep) {
|
|
5203
|
+
return undefined;
|
|
5204
|
+
}
|
|
4214
5205
|
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
: undefined,
|
|
5206
|
+
const history =
|
|
5207
|
+
(await workflowInspector.replayFromStep(entryId)) ??
|
|
5208
|
+
null;
|
|
5209
|
+
return history == null
|
|
5210
|
+
? undefined
|
|
5211
|
+
: toUint8Array(history);
|
|
5212
|
+
},
|
|
5213
|
+
)
|
|
5214
|
+
: undefined,
|
|
4225
5215
|
actions: Object.fromEntries(
|
|
4226
5216
|
Object.entries(actionHandlers).map(([name, handler]) => [
|
|
4227
5217
|
name,
|
|
@@ -4229,14 +5219,15 @@ export function buildNativeFactory(
|
|
|
4229
5219
|
async (
|
|
4230
5220
|
error: unknown,
|
|
4231
5221
|
payload: {
|
|
4232
|
-
ctx:
|
|
4233
|
-
conn:
|
|
5222
|
+
ctx: ActorContextHandle;
|
|
5223
|
+
conn: ConnHandle | null;
|
|
4234
5224
|
name: string;
|
|
4235
|
-
args:
|
|
4236
|
-
|
|
5225
|
+
args: RuntimeBytes;
|
|
5226
|
+
scheduledFire?: RuntimeScheduledFireInfo;
|
|
5227
|
+
cancelToken?: CancellationTokenHandle;
|
|
4237
5228
|
},
|
|
4238
5229
|
) => {
|
|
4239
|
-
const { ctx, conn, args, cancelToken } =
|
|
5230
|
+
const { ctx, conn, args, scheduledFire, cancelToken } =
|
|
4240
5231
|
unwrapTsfnPayload(error, payload);
|
|
4241
5232
|
const actorCtx =
|
|
4242
5233
|
conn != null
|
|
@@ -4251,6 +5242,7 @@ export function buildNativeFactory(
|
|
|
4251
5242
|
name,
|
|
4252
5243
|
decodeArgs(args),
|
|
4253
5244
|
),
|
|
5245
|
+
...(scheduledFire ? [scheduledFire] : []),
|
|
4254
5246
|
),
|
|
4255
5247
|
);
|
|
4256
5248
|
} finally {
|
|
@@ -4264,19 +5256,19 @@ export function buildNativeFactory(
|
|
|
4264
5256
|
async (
|
|
4265
5257
|
error: unknown,
|
|
4266
5258
|
payload: {
|
|
4267
|
-
ctx:
|
|
4268
|
-
conn:
|
|
5259
|
+
ctx: ActorContextHandle;
|
|
5260
|
+
conn: ConnHandle;
|
|
4269
5261
|
request: {
|
|
4270
5262
|
method: string;
|
|
4271
5263
|
uri: string;
|
|
4272
5264
|
headers?: Record<string, string>;
|
|
4273
|
-
body?:
|
|
5265
|
+
body?: RuntimeBytes;
|
|
4274
5266
|
};
|
|
4275
5267
|
name: string;
|
|
4276
|
-
body:
|
|
5268
|
+
body: RuntimeBytes;
|
|
4277
5269
|
wait: boolean;
|
|
4278
5270
|
timeoutMs?: bigint | number;
|
|
4279
|
-
cancelToken?:
|
|
5271
|
+
cancelToken?: CancellationTokenHandle;
|
|
4280
5272
|
},
|
|
4281
5273
|
) => {
|
|
4282
5274
|
const {
|
|
@@ -4291,7 +5283,7 @@ export function buildNativeFactory(
|
|
|
4291
5283
|
} = unwrapTsfnPayload(error, payload);
|
|
4292
5284
|
const jsRequest = buildRequest(request);
|
|
4293
5285
|
const actorCtx = withConnContext(
|
|
4294
|
-
|
|
5286
|
+
runtime,
|
|
4295
5287
|
ctx,
|
|
4296
5288
|
conn,
|
|
4297
5289
|
createClient,
|
|
@@ -4301,6 +5293,7 @@ export function buildNativeFactory(
|
|
|
4301
5293
|
stateEnabled,
|
|
4302
5294
|
onStateChange,
|
|
4303
5295
|
cancelToken,
|
|
5296
|
+
run !== undefined,
|
|
4304
5297
|
);
|
|
4305
5298
|
try {
|
|
4306
5299
|
if (
|
|
@@ -4364,7 +5357,7 @@ export function buildNativeFactory(
|
|
|
4364
5357
|
async (
|
|
4365
5358
|
error: unknown,
|
|
4366
5359
|
payload: {
|
|
4367
|
-
ctx:
|
|
5360
|
+
ctx: ActorContextHandle;
|
|
4368
5361
|
reason: SerializeStateReason;
|
|
4369
5362
|
},
|
|
4370
5363
|
) => {
|
|
@@ -4379,20 +5372,20 @@ export function buildNativeFactory(
|
|
|
4379
5372
|
),
|
|
4380
5373
|
};
|
|
4381
5374
|
|
|
4382
|
-
return
|
|
5375
|
+
return runtime.createActorFactory(
|
|
4383
5376
|
callbacks,
|
|
4384
|
-
buildActorConfig(definition, registryConfig),
|
|
5377
|
+
buildActorConfig(definition, registryConfig, runtime.kind),
|
|
4385
5378
|
);
|
|
4386
5379
|
}
|
|
4387
5380
|
|
|
4388
|
-
async function buildServeConfig(
|
|
5381
|
+
export async function buildServeConfig(
|
|
4389
5382
|
config: RegistryConfig,
|
|
4390
|
-
): Promise<
|
|
5383
|
+
): Promise<RuntimeServeConfig> {
|
|
4391
5384
|
if (!config.endpoint) {
|
|
4392
5385
|
throw nativeEndpointNotConfiguredError();
|
|
4393
5386
|
}
|
|
4394
5387
|
|
|
4395
|
-
const serveConfig:
|
|
5388
|
+
const serveConfig: RuntimeServeConfig = {
|
|
4396
5389
|
version: config.envoy.version,
|
|
4397
5390
|
endpoint: config.endpoint,
|
|
4398
5391
|
token: config.token,
|
|
@@ -4408,39 +5401,96 @@ async function buildServeConfig(
|
|
|
4408
5401
|
serverlessMaxStartPayloadBytes: config.serverless.maxStartPayloadBytes,
|
|
4409
5402
|
};
|
|
4410
5403
|
|
|
4411
|
-
|
|
5404
|
+
// Always best-effort resolve the npm-installed engine binary and hand its
|
|
5405
|
+
// path to the core. The core alone decides whether to actually spawn a local
|
|
5406
|
+
// engine (its `should_manage_engine`, based on the endpoint + spawn mode), so
|
|
5407
|
+
// JS must not duplicate that decision here. Only JS knows the npm
|
|
5408
|
+
// `node_modules` layout, so it resolves the path; if no binary is available
|
|
5409
|
+
// (remote-only install, unsupported platform, optional deps skipped), leave
|
|
5410
|
+
// it unset and let the core report `engine.binary_unavailable` if it actually
|
|
5411
|
+
// needs one.
|
|
5412
|
+
try {
|
|
4412
5413
|
const { getEnginePath } = await loadEngineCli();
|
|
4413
5414
|
serveConfig.engineBinaryPath = getEnginePath();
|
|
5415
|
+
} catch (error) {
|
|
5416
|
+
// The npm-installed engine binary could not be resolved. The core still
|
|
5417
|
+
// decides whether it needs to spawn a local engine; if it does, it will
|
|
5418
|
+
// fail with engine.binary_unavailable (auto-download is off in the napi
|
|
5419
|
+
// runtime). Warn so the cause is actionable.
|
|
5420
|
+
logger().warn({
|
|
5421
|
+
msg: "could not resolve a local engine binary; if a local engine must be spawned it will fail with engine.binary_unavailable — set RIVET_ENGINE_BINARY_PATH or install the @rivetkit/engine-cli platform package",
|
|
5422
|
+
error: stringifyError(error),
|
|
5423
|
+
});
|
|
5424
|
+
}
|
|
5425
|
+
serveConfig.engineHost = config.engineHost;
|
|
5426
|
+
serveConfig.enginePort = config.enginePort;
|
|
5427
|
+
if (config.test?.enabled) {
|
|
5428
|
+
serveConfig.inspectorTestToken =
|
|
5429
|
+
getEnvUniversal("_RIVET_TEST_INSPECTOR_TOKEN") ?? "token";
|
|
4414
5430
|
}
|
|
4415
5431
|
|
|
4416
5432
|
return serveConfig;
|
|
4417
5433
|
}
|
|
4418
5434
|
|
|
4419
|
-
export async function
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
5435
|
+
export async function buildRegistryWithRuntime(
|
|
5436
|
+
config: RegistryConfig,
|
|
5437
|
+
runtime: CoreRuntime,
|
|
5438
|
+
): Promise<{
|
|
5439
|
+
runtime: CoreRuntime;
|
|
5440
|
+
registry: RegistryHandle;
|
|
5441
|
+
serveConfig: RuntimeServeConfig;
|
|
4423
5442
|
}> {
|
|
4424
5443
|
if (
|
|
4425
5444
|
config.test?.enabled &&
|
|
4426
|
-
|
|
5445
|
+
getEnvUniversal("_RIVET_TEST_INSPECTOR_TOKEN") === undefined
|
|
4427
5446
|
) {
|
|
4428
|
-
|
|
5447
|
+
trySetProcessEnv("_RIVET_TEST_INSPECTOR_TOKEN", "token");
|
|
5448
|
+
}
|
|
5449
|
+
|
|
5450
|
+
// Custom inspector tab `source` paths are resolved with node:path while
|
|
5451
|
+
// building actor configs below, so the Node modules must be loaded first.
|
|
5452
|
+
// Native (napi) runtime only; wasm has no filesystem.
|
|
5453
|
+
if (runtime.kind === "napi") {
|
|
5454
|
+
importNodeDependencies();
|
|
4429
5455
|
}
|
|
4430
5456
|
|
|
4431
|
-
const
|
|
4432
|
-
const registry = new bindings.CoreRegistry();
|
|
5457
|
+
const registry = runtime.createRegistry();
|
|
4433
5458
|
|
|
4434
5459
|
for (const [name, definition] of Object.entries(config.use)) {
|
|
4435
|
-
|
|
5460
|
+
runtime.registerActor(
|
|
5461
|
+
registry,
|
|
4436
5462
|
name,
|
|
4437
|
-
buildNativeFactory(
|
|
5463
|
+
buildNativeFactory(runtime, config, definition),
|
|
4438
5464
|
);
|
|
4439
5465
|
}
|
|
4440
5466
|
|
|
4441
5467
|
return {
|
|
4442
|
-
|
|
5468
|
+
runtime,
|
|
4443
5469
|
registry,
|
|
4444
5470
|
serveConfig: await buildServeConfig(config),
|
|
4445
5471
|
};
|
|
4446
5472
|
}
|
|
5473
|
+
|
|
5474
|
+
export async function buildNativeRegistry(config: RegistryConfig): Promise<{
|
|
5475
|
+
runtime: CoreRuntime;
|
|
5476
|
+
registry: RegistryHandle;
|
|
5477
|
+
serveConfig: RuntimeServeConfig;
|
|
5478
|
+
}> {
|
|
5479
|
+
const { runtime } = await loadNapiRuntime();
|
|
5480
|
+
return buildRegistryWithRuntime(
|
|
5481
|
+
normalizeRuntimeConfigForKind(config, "native"),
|
|
5482
|
+
runtime,
|
|
5483
|
+
);
|
|
5484
|
+
}
|
|
5485
|
+
|
|
5486
|
+
export async function buildConfiguredRegistry(config: RegistryConfig): Promise<{
|
|
5487
|
+
runtime: CoreRuntime;
|
|
5488
|
+
registry: RegistryHandle;
|
|
5489
|
+
serveConfig: RuntimeServeConfig;
|
|
5490
|
+
}> {
|
|
5491
|
+
const runtime = await loadConfiguredRuntime(config);
|
|
5492
|
+
return buildRegistryWithRuntime(
|
|
5493
|
+
normalizeRuntimeConfig(config, runtime),
|
|
5494
|
+
runtime,
|
|
5495
|
+
);
|
|
5496
|
+
}
|