@rivetkit/engine 1.0.0 → 2.2.1-pr.4600.b74ff3b
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/CLAUDE.md +38 -0
- package/artifacts/config-schema.json +1140 -0
- package/artifacts/errors/actor.kv_storage_quota_exceeded.json +5 -0
- package/artifacts/errors/actor.no_runner_config_configured.json +5 -0
- package/artifacts/errors/guard.actor_runner_failed.json +5 -0
- package/artifacts/errors/guard.invalid_request.json +5 -0
- package/artifacts/errors/guard.invalid_request_body.json +5 -0
- package/artifacts/errors/guard.invalid_response_body.json +5 -0
- package/artifacts/errors/guard.missing_query_parameter.json +5 -0
- package/artifacts/errors/guard.query_ambiguous_runner_configs.json +5 -0
- package/artifacts/errors/guard.query_duplicate_param.json +5 -0
- package/artifacts/errors/guard.query_empty_actor_name.json +5 -0
- package/artifacts/errors/guard.query_get_disallowed_params.json +5 -0
- package/artifacts/errors/guard.query_invalid_base64_input.json +5 -0
- package/artifacts/errors/guard.query_invalid_cbor_input.json +5 -0
- package/artifacts/errors/guard.query_invalid_params.json +5 -0
- package/artifacts/errors/guard.query_invalid_percent_encoding.json +5 -0
- package/artifacts/errors/guard.query_missing_runner_name.json +5 -0
- package/artifacts/errors/guard.query_no_runner_configs.json +5 -0
- package/artifacts/errors/guard.query_param_missing_equals.json +5 -0
- package/artifacts/errors/guard.query_path_token_syntax.json +5 -0
- package/artifacts/errors/guard.query_unknown_param.json +5 -0
- package/artifacts/errors/guard.request_body_too_large.json +5 -0
- package/artifacts/errors/guard.response_body_too_large.json +5 -0
- package/artifacts/errors/serverless_runner_pool.failed_to_fetch_metadata.json +5 -0
- package/artifacts/errors/serverless_runner_pool.not_found.json +5 -0
- package/artifacts/errors/{api.rate_limited.json → test.api_rate_limited.json} +2 -2
- package/artifacts/errors/{namespace.invalid_name.json → test.namespace_invalid_name.json} +2 -2
- package/artifacts/errors/ws.going_away.json +5 -0
- package/artifacts/openapi.json +458 -6
- package/docker/builder-base/linux-gnu.Dockerfile +21 -0
- package/docker/builder-base/linux-musl.Dockerfile +53 -0
- package/docker/builder-base/osxcross.Dockerfile +42 -0
- package/docker/builder-base/windows-mingw.Dockerfile +41 -0
- package/docker/builder-base/windows-msvc.Dockerfile +25 -0
- package/docker/dev/docker-compose.yml +43 -18
- package/docker/dev/grafana/dashboards/api.json +1077 -1239
- package/docker/dev/grafana/dashboards/cache.json +911 -1074
- package/docker/dev/grafana/dashboards/epoxy.json +1606 -0
- package/docker/dev/grafana/dashboards/futures.json +242 -229
- package/docker/dev/grafana/dashboards/gasoline.json +2663 -2476
- package/docker/dev/grafana/dashboards/guard.json +1433 -1273
- package/docker/dev/grafana/dashboards/operation.json +871 -0
- package/docker/dev/grafana/dashboards/pegboard.json +1274 -0
- package/docker/dev/grafana/dashboards/tokio.json +930 -1004
- package/docker/dev/grafana/dashboards/traces.json +35 -13
- package/docker/dev/grafana/provisioning/datasources/datasources.yaml +8 -0
- package/docker/{dev-multinode/otel-collector-server → dev/otel-collector}/config.yaml +18 -13
- package/docker/dev/prometheus/prometheus.yml +4 -0
- package/docker/dev/rivet-engine/config.jsonc +9 -16
- package/docker/dev-host/docker-compose.yml +38 -16
- package/docker/dev-host/grafana/dashboards/api.json +1077 -1239
- package/docker/dev-host/grafana/dashboards/cache.json +911 -1074
- package/docker/dev-host/grafana/dashboards/epoxy.json +1606 -0
- package/docker/dev-host/grafana/dashboards/futures.json +242 -229
- package/docker/dev-host/grafana/dashboards/gasoline.json +2663 -2476
- package/docker/dev-host/grafana/dashboards/guard.json +1433 -1273
- package/docker/dev-host/grafana/dashboards/operation.json +871 -0
- package/docker/dev-host/grafana/dashboards/pegboard.json +1274 -0
- package/docker/dev-host/grafana/dashboards/tokio.json +930 -1004
- package/docker/dev-host/grafana/dashboards/traces.json +35 -13
- package/docker/dev-host/grafana/provisioning/datasources/datasources.yaml +8 -0
- package/docker/dev-host/{otel-collector-server → otel-collector}/config.yaml +18 -13
- package/docker/dev-host/prometheus/prometheus.yml +4 -0
- package/docker/dev-host/rivet-engine/config.jsonc +9 -16
- package/docker/dev-multidc/core/grafana/dashboards/api.json +1077 -1239
- package/docker/dev-multidc/core/grafana/dashboards/cache.json +911 -1074
- package/docker/dev-multidc/core/grafana/dashboards/epoxy.json +1606 -0
- package/docker/dev-multidc/core/grafana/dashboards/futures.json +242 -229
- package/docker/dev-multidc/core/grafana/dashboards/gasoline.json +2663 -2476
- package/docker/dev-multidc/core/grafana/dashboards/guard.json +1433 -1273
- package/docker/dev-multidc/core/grafana/dashboards/operation.json +871 -0
- package/docker/dev-multidc/core/grafana/dashboards/pegboard.json +1274 -0
- package/docker/dev-multidc/core/grafana/dashboards/tokio.json +930 -1004
- package/docker/dev-multidc/core/grafana/dashboards/traces.json +35 -13
- package/docker/dev-multidc/core/grafana/provisioning/datasources/datasources.yaml +8 -0
- package/docker/dev-multidc/core/prometheus/prometheus.yml +4 -0
- package/docker/dev-multidc/datacenters/dc-a/{otel-collector-server → otel-collector}/config.yaml +18 -13
- package/docker/dev-multidc/datacenters/dc-a/rivet-engine/config.jsonc +23 -22
- package/docker/{dev-multidc-multinode/datacenters/dc-b/otel-collector-server → dev-multidc/datacenters/dc-b/otel-collector}/config.yaml +18 -13
- package/docker/dev-multidc/datacenters/dc-b/rivet-engine/config.jsonc +23 -22
- package/docker/dev-multidc/datacenters/dc-c/{otel-collector-server → otel-collector}/config.yaml +18 -13
- package/docker/dev-multidc/datacenters/dc-c/rivet-engine/config.jsonc +23 -22
- package/docker/dev-multidc/docker-compose.yml +71 -64
- package/docker/dev-multidc-multinode/core/grafana/dashboards/api.json +1077 -1239
- package/docker/dev-multidc-multinode/core/grafana/dashboards/cache.json +911 -1074
- package/docker/dev-multidc-multinode/core/grafana/dashboards/epoxy.json +1606 -0
- package/docker/dev-multidc-multinode/core/grafana/dashboards/futures.json +242 -229
- package/docker/dev-multidc-multinode/core/grafana/dashboards/gasoline.json +2663 -2476
- package/docker/dev-multidc-multinode/core/grafana/dashboards/guard.json +1433 -1273
- package/docker/dev-multidc-multinode/core/grafana/dashboards/operation.json +871 -0
- package/docker/dev-multidc-multinode/core/grafana/dashboards/pegboard.json +1274 -0
- package/docker/dev-multidc-multinode/core/grafana/dashboards/tokio.json +930 -1004
- package/docker/dev-multidc-multinode/core/grafana/dashboards/traces.json +35 -13
- package/docker/dev-multidc-multinode/core/grafana/provisioning/datasources/datasources.yaml +8 -0
- package/docker/dev-multidc-multinode/core/prometheus/prometheus.yml +4 -0
- package/docker/dev-multidc-multinode/datacenters/dc-a/{otel-collector-server → otel-collector}/config.yaml +28 -13
- package/docker/dev-multidc-multinode/datacenters/dc-a/rivet-engine/0/config.jsonc +23 -22
- package/docker/dev-multidc-multinode/datacenters/dc-a/rivet-engine/1/config.jsonc +23 -22
- package/docker/dev-multidc-multinode/datacenters/dc-a/rivet-engine/2/config.jsonc +23 -22
- package/docker/{dev-multidc/datacenters/dc-b/otel-collector-server → dev-multidc-multinode/datacenters/dc-b/otel-collector}/config.yaml +28 -13
- package/docker/dev-multidc-multinode/datacenters/dc-b/rivet-engine/0/config.jsonc +23 -22
- package/docker/dev-multidc-multinode/datacenters/dc-b/rivet-engine/1/config.jsonc +23 -22
- package/docker/dev-multidc-multinode/datacenters/dc-b/rivet-engine/2/config.jsonc +23 -22
- package/docker/dev-multidc-multinode/datacenters/dc-c/{otel-collector-server → otel-collector}/config.yaml +28 -13
- package/docker/dev-multidc-multinode/datacenters/dc-c/rivet-engine/0/config.jsonc +23 -22
- package/docker/dev-multidc-multinode/datacenters/dc-c/rivet-engine/1/config.jsonc +23 -22
- package/docker/dev-multidc-multinode/datacenters/dc-c/rivet-engine/2/config.jsonc +23 -22
- package/docker/dev-multidc-multinode/docker-compose.yml +113 -88
- package/docker/dev-multinode/docker-compose.yml +57 -26
- package/docker/dev-multinode/grafana/dashboards/api.json +1077 -1239
- package/docker/dev-multinode/grafana/dashboards/cache.json +911 -1074
- package/docker/dev-multinode/grafana/dashboards/epoxy.json +1606 -0
- package/docker/dev-multinode/grafana/dashboards/futures.json +242 -229
- package/docker/dev-multinode/grafana/dashboards/gasoline.json +2663 -2476
- package/docker/dev-multinode/grafana/dashboards/guard.json +1433 -1273
- package/docker/dev-multinode/grafana/dashboards/operation.json +871 -0
- package/docker/dev-multinode/grafana/dashboards/pegboard.json +1274 -0
- package/docker/dev-multinode/grafana/dashboards/tokio.json +930 -1004
- package/docker/dev-multinode/grafana/dashboards/traces.json +35 -13
- package/docker/dev-multinode/grafana/provisioning/datasources/datasources.yaml +8 -0
- package/docker/{dev/otel-collector-server → dev-multinode/otel-collector}/config.yaml +28 -13
- package/docker/dev-multinode/prometheus/prometheus.yml +4 -0
- package/docker/dev-multinode/rivet-engine/0/config.jsonc +9 -16
- package/docker/dev-multinode/rivet-engine/1/config.jsonc +9 -16
- package/docker/dev-multinode/rivet-engine/2/config.jsonc +9 -16
- package/docker/engine/linux-aarch64.Dockerfile +9 -49
- package/docker/engine/linux-x86_64.Dockerfile +7 -57
- package/docker/engine/macos-aarch64.Dockerfile +8 -54
- package/docker/engine/macos-x86_64.Dockerfile +9 -55
- package/docker/engine/windows.Dockerfile +5 -53
- package/docker/template/grafana-dashboards/api.json +1077 -1239
- package/docker/template/grafana-dashboards/cache.json +911 -1074
- package/docker/template/grafana-dashboards/epoxy.json +1606 -0
- package/docker/template/grafana-dashboards/futures.json +242 -229
- package/docker/template/grafana-dashboards/gasoline.json +2663 -2476
- package/docker/template/grafana-dashboards/guard.json +1433 -1273
- package/docker/template/grafana-dashboards/operation.json +871 -0
- package/docker/template/grafana-dashboards/pegboard.json +1274 -0
- package/docker/template/grafana-dashboards/tokio.json +930 -1004
- package/docker/template/grafana-dashboards/traces.json +35 -13
- package/docker/template/node_modules/.bin/js-yaml +4 -4
- package/docker/template/node_modules/.bin/tsc +4 -4
- package/docker/template/node_modules/.bin/tsserver +4 -4
- package/docker/template/node_modules/.bin/tsx +4 -4
- package/docker/template/src/docker-compose.ts +42 -29
- package/docker/template/src/main.ts +4 -4
- package/docker/template/src/services/core/grafana.ts +14 -1
- package/docker/template/src/services/core/prometheus.ts +20 -0
- package/docker/template/src/services/edge/{otel-collector-server.ts → otel-collector.ts} +55 -24
- package/docker/template/src/services/edge/rivet-engine.ts +4 -16
- package/docker/template/src/services/edge/runner.ts +2 -3
- package/docker/universal/Dockerfile +5 -3
- package/package.json +2 -5
- package/packages/api-builder/src/global_context.rs +1 -1
- package/packages/api-builder/src/metrics.rs +28 -24
- package/packages/api-builder/src/middleware.rs +30 -48
- package/packages/api-builder/src/router.rs +13 -1
- package/packages/api-peer/Cargo.toml +7 -9
- package/packages/api-peer/src/actors/delete.rs +56 -57
- package/packages/api-peer/src/actors/get_or_create.rs +139 -0
- package/packages/api-peer/src/actors/kv_get.rs +40 -28
- package/packages/api-peer/src/actors/list.rs +31 -14
- package/packages/api-peer/src/actors/list_names.rs +6 -6
- package/packages/api-peer/src/actors/mod.rs +3 -0
- package/packages/api-peer/src/actors/reschedule.rs +55 -0
- package/packages/api-peer/src/actors/sleep.rs +55 -0
- package/packages/api-peer/src/envoys.rs +57 -0
- package/packages/api-peer/src/internal.rs +441 -24
- package/packages/api-peer/src/lib.rs +2 -1
- package/packages/api-peer/src/namespaces.rs +24 -9
- package/packages/api-peer/src/router.rs +31 -7
- package/packages/api-peer/src/runner_configs.rs +66 -19
- package/packages/api-peer/src/runners.rs +30 -32
- package/packages/api-public/Cargo.toml +2 -0
- package/packages/api-public/src/actors/create.rs +8 -17
- package/packages/api-public/src/actors/delete.rs +11 -35
- package/packages/api-public/src/actors/get_or_create.rs +23 -95
- package/packages/api-public/src/actors/kv_get.rs +12 -29
- package/packages/api-public/src/actors/list.rs +56 -78
- package/packages/api-public/src/actors/list_names.rs +15 -14
- package/packages/api-public/src/actors/mod.rs +2 -0
- package/packages/api-public/src/actors/reschedule.rs +65 -0
- package/packages/api-public/src/actors/sleep.rs +64 -0
- package/packages/api-public/src/actors/utils.rs +12 -60
- package/packages/api-public/src/ctx.rs +14 -6
- package/packages/api-public/src/datacenters.rs +5 -5
- package/packages/api-public/src/envoys.rs +57 -0
- package/packages/api-public/src/errors.rs +0 -7
- package/packages/api-public/src/health.rs +51 -44
- package/packages/api-public/src/lib.rs +2 -1
- package/packages/api-public/src/metadata.rs +44 -14
- package/packages/api-public/src/namespaces.rs +11 -11
- package/packages/api-public/src/router.rs +22 -5
- package/packages/api-public/src/runner_configs/delete.rs +13 -10
- package/packages/api-public/src/runner_configs/list.rs +5 -2
- package/packages/api-public/src/runner_configs/refresh_metadata.rs +1 -1
- package/packages/api-public/src/runner_configs/serverless_health_check.rs +2 -2
- package/packages/api-public/src/runner_configs/upsert.rs +12 -9
- package/packages/api-public/src/runner_configs/utils.rs +35 -175
- package/packages/api-public/src/runners.rs +17 -45
- package/packages/{dump-openapi → api-public-openapi-gen}/Cargo.toml +1 -1
- package/packages/api-types/src/actors/create.rs +1 -0
- package/packages/api-types/src/actors/delete.rs +20 -0
- package/packages/api-types/src/actors/get_or_create.rs +30 -0
- package/packages/api-types/src/actors/kv_get.rs +25 -0
- package/packages/api-types/src/actors/list.rs +8 -1
- package/packages/api-types/src/actors/mod.rs +5 -0
- package/packages/api-types/src/actors/reschedule.rs +26 -0
- package/packages/api-types/src/actors/sleep.rs +26 -0
- package/packages/api-types/src/datacenters/list.rs +2 -2
- package/packages/api-types/src/envoys/list.rs +24 -0
- package/packages/api-types/src/envoys/mod.rs +1 -0
- package/packages/api-types/src/lib.rs +1 -0
- package/packages/api-types/src/namespaces/list.rs +4 -0
- package/packages/api-types/src/namespaces/runner_configs.rs +23 -2
- package/packages/api-types/src/runner_configs/list.rs +6 -1
- package/packages/api-types/src/runner_configs/mod.rs +12 -0
- package/packages/api-types/src/runners/list.rs +4 -0
- package/packages/api-types/src/runners/list_names.rs +21 -0
- package/packages/api-types/src/runners/mod.rs +1 -0
- package/packages/api-util/src/lib.rs +44 -21
- package/packages/bootstrap/Cargo.toml +7 -4
- package/packages/bootstrap/src/backfill.rs +53 -0
- package/packages/bootstrap/src/lib.rs +43 -7
- package/packages/cache/Cargo.toml +3 -1
- package/packages/cache/src/driver.rs +43 -151
- package/packages/cache/src/getter_ctx.rs +48 -70
- package/packages/cache/src/inner.rs +28 -18
- package/packages/cache/src/key.rs +17 -3
- package/packages/cache/src/lib.rs +0 -2
- package/packages/cache/src/metrics.rs +43 -31
- package/packages/cache/src/req_config.rs +219 -156
- package/packages/cache/tests/fetch.rs +91 -0
- package/packages/cache/tests/in_flight.rs +361 -0
- package/packages/cache/tests/ttl.rs +314 -0
- package/packages/cache-purge/src/lib.rs +1 -1
- package/packages/config/Cargo.toml +1 -0
- package/packages/config/src/config/cache.rs +10 -3
- package/packages/config/src/config/clickhouse.rs +0 -30
- package/packages/config/src/config/{db.rs → db/mod.rs} +3 -18
- package/packages/config/src/config/db/postgres.rs +59 -0
- package/packages/config/src/config/guard.rs +19 -0
- package/packages/config/src/config/metrics.rs +22 -0
- package/packages/config/src/config/mod.rs +44 -10
- package/packages/config/src/config/pegboard.rs +242 -16
- package/packages/config/src/config/pubsub.rs +11 -0
- package/packages/config/src/config/runtime.rs +58 -0
- package/packages/config/src/config/telemetry.rs +1 -0
- package/packages/config/src/config/topology.rs +78 -19
- package/packages/config/src/defaults.rs +3 -0
- package/packages/config/src/lib.rs +10 -1
- package/packages/config-schema-gen/Cargo.toml +11 -0
- package/packages/config-schema-gen/build.rs +26 -0
- package/packages/config-schema-gen/src/lib.rs +2 -0
- package/packages/engine/Cargo.toml +11 -2
- package/packages/engine/src/commands/db/mod.rs +0 -10
- package/packages/engine/src/commands/epoxy.rs +395 -0
- package/packages/engine/src/commands/mod.rs +1 -1
- package/packages/engine/src/commands/start.rs +43 -63
- package/packages/engine/src/commands/udb/cli.rs +148 -4
- package/packages/engine/src/commands/wf/mod.rs +83 -12
- package/packages/engine/src/commands/wf/signal.rs +38 -0
- package/packages/engine/src/lib.rs +6 -3
- package/packages/engine/src/main.rs +1 -1
- package/packages/engine/src/run_config.rs +6 -7
- package/packages/engine/src/util/db.rs +1 -25
- package/packages/engine/src/util/wf/mod.rs +39 -5
- package/packages/engine/tests/common/actors.rs +50 -332
- package/packages/engine/tests/common/api/mod.rs +7 -0
- package/packages/engine/tests/common/api/peer.rs +364 -0
- package/packages/engine/tests/common/api/public.rs +473 -0
- package/packages/engine/tests/common/ctx.rs +15 -3
- package/packages/engine/tests/common/mod.rs +8 -5
- package/packages/engine/tests/common/test_envoy.rs +87 -0
- package/packages/engine/tests/common/test_helpers.rs +218 -130
- package/packages/engine/tests/common/test_runner.rs +273 -0
- package/packages/engine/tests/envoy/actors_lifecycle.rs +1277 -0
- package/packages/engine/tests/envoy/mod.rs +1 -0
- package/packages/engine/tests/mod.rs +3 -0
- package/packages/engine/tests/runner/actors_alarm.rs +1453 -0
- package/packages/engine/tests/runner/actors_kv_crud.rs +996 -0
- package/packages/engine/tests/runner/actors_kv_delete_range.rs +126 -0
- package/packages/engine/tests/runner/actors_kv_drop.rs +255 -0
- package/packages/engine/tests/runner/actors_kv_list.rs +1061 -0
- package/packages/engine/tests/runner/actors_kv_misc.rs +882 -0
- package/packages/engine/tests/runner/actors_lifecycle.rs +1284 -0
- package/packages/engine/tests/runner/actors_scheduling_errors.rs +1005 -0
- package/packages/engine/tests/runner/api_actors_create.rs +422 -0
- package/packages/engine/tests/runner/api_actors_delete.rs +487 -0
- package/packages/engine/tests/runner/api_actors_get_or_create.rs +634 -0
- package/packages/engine/tests/runner/api_actors_list.rs +1771 -0
- package/packages/engine/tests/runner/api_actors_list_names.rs +691 -0
- package/packages/engine/tests/runner/api_namespaces_create.rs +428 -0
- package/packages/engine/tests/runner/api_namespaces_list.rs +760 -0
- package/packages/engine/tests/runner/api_runner_configs_list.rs +646 -0
- package/packages/engine/tests/runner/api_runner_configs_upsert.rs +651 -0
- package/packages/engine/tests/runner/api_runners_list.rs +166 -0
- package/packages/engine/tests/runner/api_runners_list_names.rs +386 -0
- package/packages/engine/tests/runner/mod.rs +20 -0
- package/packages/engine/tests/runner/runner_drain_on_version.rs +620 -0
- package/packages/env/Cargo.toml +0 -4
- package/packages/env/src/lib.rs +0 -18
- package/packages/epoxy/Cargo.toml +3 -2
- package/packages/epoxy/README.md +554 -93
- package/packages/epoxy/src/consts.rs +4 -36
- package/packages/epoxy/src/http_client.rs +59 -26
- package/packages/epoxy/src/http_routes.rs +73 -10
- package/packages/epoxy/src/keys/keys.rs +260 -11
- package/packages/epoxy/src/keys/mod.rs +11 -1
- package/packages/epoxy/src/keys/replica.rs +5 -260
- package/packages/epoxy/src/lib.rs +2 -1
- package/packages/epoxy/src/metrics.rs +118 -0
- package/packages/epoxy/src/ops/kv/get_local.rs +15 -24
- package/packages/epoxy/src/ops/kv/get_optimistic.rs +102 -64
- package/packages/epoxy/src/ops/kv/mod.rs +1 -0
- package/packages/epoxy/src/ops/kv/purge_local.rs +18 -9
- package/packages/epoxy/src/ops/kv/read_value.rs +92 -0
- package/packages/epoxy/src/ops/mod.rs +0 -1
- package/packages/epoxy/src/ops/propose.rs +1079 -194
- package/packages/epoxy/src/replica/ballot.rs +162 -102
- package/packages/epoxy/src/replica/changelog.rs +147 -0
- package/packages/epoxy/src/replica/commit_kv.rs +69 -66
- package/packages/epoxy/src/replica/message_request.rs +33 -48
- package/packages/epoxy/src/replica/messages/accept.rs +82 -41
- package/packages/epoxy/src/replica/messages/commit.rs +21 -33
- package/packages/epoxy/src/replica/messages/mod.rs +0 -8
- package/packages/epoxy/src/replica/messages/prepare.rs +68 -69
- package/packages/epoxy/src/replica/mod.rs +1 -6
- package/packages/epoxy/src/replica/update_config.rs +3 -1
- package/packages/epoxy/src/types.rs +30 -54
- package/packages/epoxy/src/utils.rs +149 -16
- package/packages/epoxy/src/workflows/backfill.rs +233 -0
- package/packages/epoxy/src/workflows/coordinator/mod.rs +33 -7
- package/packages/epoxy/src/workflows/coordinator/reconfigure.rs +44 -0
- package/packages/epoxy/src/workflows/coordinator/replica_status_change.rs +4 -3
- package/packages/epoxy/src/workflows/mod.rs +1 -1
- package/packages/epoxy/src/workflows/replica/mod.rs +4 -6
- package/packages/epoxy/src/workflows/replica/setup.rs +130 -771
- package/packages/epoxy/tests/backfill.rs +65 -0
- package/packages/epoxy/tests/backfill_snapshot.rs +233 -0
- package/packages/epoxy/tests/common/mod.rs +77 -21
- package/packages/epoxy/tests/common/utils.rs +366 -10
- package/packages/epoxy/tests/consensus_regressions.rs +285 -0
- package/packages/epoxy/tests/kv.rs +128 -167
- package/packages/epoxy/tests/kv_get_optimistic.rs +257 -157
- package/packages/epoxy/tests/migration.rs +75 -0
- package/packages/epoxy/tests/proposal.rs +133 -28
- package/packages/epoxy/tests/reconfigure.rs +92 -474
- package/packages/error/tests/basic.rs +8 -8
- package/packages/gasoline/Cargo.toml +1 -0
- package/packages/gasoline/src/builder/common/message.rs +19 -47
- package/packages/gasoline/src/builder/common/signal.rs +37 -21
- package/packages/gasoline/src/builder/common/workflow.rs +19 -15
- package/packages/gasoline/src/builder/workflow/lupe.rs +295 -0
- package/packages/gasoline/src/builder/workflow/message.rs +24 -47
- package/packages/gasoline/src/builder/workflow/mod.rs +1 -0
- package/packages/gasoline/src/builder/workflow/signal.rs +68 -22
- package/packages/gasoline/src/builder/workflow/sub_workflow.rs +6 -15
- package/packages/gasoline/src/ctx/activity.rs +46 -6
- package/packages/gasoline/src/ctx/common.rs +26 -23
- package/packages/gasoline/src/ctx/listen.rs +33 -50
- package/packages/gasoline/src/ctx/message.rs +76 -64
- package/packages/gasoline/src/ctx/operation.rs +15 -5
- package/packages/gasoline/src/ctx/standalone.rs +32 -4
- package/packages/gasoline/src/ctx/test.rs +31 -6
- package/packages/gasoline/src/ctx/versioned_workflow.rs +33 -7
- package/packages/gasoline/src/ctx/workflow.rs +194 -384
- package/packages/gasoline/src/db/debug.rs +49 -9
- package/packages/gasoline/src/db/kv/debug.rs +905 -15
- package/packages/gasoline/src/db/kv/keys/history.rs +434 -9
- package/packages/gasoline/src/db/kv/keys/metric.rs +70 -47
- package/packages/gasoline/src/db/kv/keys/signal.rs +19 -3
- package/packages/gasoline/src/db/kv/keys/workflow.rs +349 -3
- package/packages/gasoline/src/db/kv/mod.rs +975 -514
- package/packages/gasoline/src/db/kv/system.rs +155 -18
- package/packages/gasoline/src/db/mod.rs +29 -7
- package/packages/gasoline/src/error.rs +26 -21
- package/packages/gasoline/src/executable.rs +3 -1
- package/packages/gasoline/src/history/cursor.rs +436 -336
- package/packages/gasoline/src/history/event.rs +24 -15
- package/packages/gasoline/src/listen.rs +2 -14
- package/packages/gasoline/src/message.rs +1 -1
- package/packages/gasoline/src/metrics.rs +260 -143
- package/packages/gasoline/src/prelude.rs +1 -1
- package/packages/gasoline/src/registry.rs +6 -2
- package/packages/gasoline/src/signal.rs +34 -31
- package/packages/gasoline/src/utils/mod.rs +1 -18
- package/packages/gasoline/src/utils/topic.rs +35 -0
- package/packages/gasoline/src/worker.rs +71 -14
- package/packages/gasoline/src/workflow.rs +13 -0
- package/packages/gasoline/tests/workflows/eviction_test.rs +2 -2
- package/packages/gasoline-macros/src/lib.rs +74 -12
- package/packages/gasoline-runtime/Cargo.toml +18 -0
- package/packages/gasoline-runtime/src/lib.rs +12 -0
- package/packages/gasoline-runtime/src/workflows/mod.rs +1 -0
- package/packages/gasoline-runtime/src/workflows/pruner.rs +55 -0
- package/packages/guard/Cargo.toml +16 -8
- package/packages/guard/src/cache/mod.rs +63 -43
- package/packages/guard/src/cache/pegboard_gateway.rs +144 -0
- package/packages/guard/src/errors.rs +105 -0
- package/packages/guard/src/lib.rs +5 -15
- package/packages/guard/src/metrics.rs +12 -0
- package/packages/guard/src/routing/actor_path.rs +409 -0
- package/packages/guard/src/routing/api_public.rs +6 -14
- package/packages/guard/src/routing/envoy.rs +98 -0
- package/packages/guard/src/routing/mod.rs +152 -206
- package/packages/guard/src/routing/pegboard_gateway/mod.rs +572 -0
- package/packages/guard/src/routing/pegboard_gateway/resolve_actor_query.rs +236 -0
- package/packages/guard/src/routing/runner.rs +24 -54
- package/packages/guard/src/routing/ws_health.rs +61 -0
- package/packages/guard/src/shared_state.rs +11 -2
- package/packages/guard/tests/parse_actor_path.rs +418 -165
- package/packages/guard-core/Cargo.toml +3 -10
- package/packages/guard-core/src/custom_serve.rs +4 -10
- package/packages/guard-core/src/errors.rs +20 -4
- package/packages/guard-core/src/lib.rs +6 -4
- package/packages/guard-core/src/metrics.rs +66 -53
- package/packages/guard-core/src/proxy_service.rs +618 -1520
- package/packages/guard-core/src/request_context.rs +149 -169
- package/packages/guard-core/src/response_body.rs +65 -0
- package/packages/guard-core/src/route.rs +76 -0
- package/packages/guard-core/src/server.rs +60 -26
- package/packages/guard-core/src/task_group.rs +4 -0
- package/packages/guard-core/src/utils.rs +296 -0
- package/packages/guard-core/src/websocket_handle.rs +3 -3
- package/packages/guard-core/tests/common/mod.rs +0 -1
- package/packages/guard-core/tests/custom_serve.rs +4 -6
- package/packages/guard-core/tests/simple_websocket.rs +19 -19
- package/packages/guard-core/tests/streaming_response.rs +4 -9
- package/packages/metrics/Cargo.toml +3 -2
- package/packages/metrics/src/buckets.rs +5 -11
- package/packages/metrics/src/lib.rs +6 -3
- package/packages/metrics/src/providers.rs +2 -42
- package/packages/metrics/src/registry.rs +7 -0
- package/packages/metrics/src/server.rs +57 -0
- package/packages/namespace/Cargo.toml +0 -3
- package/packages/namespace/src/keys/metric.rs +301 -0
- package/packages/namespace/src/keys/mod.rs +1 -1
- package/packages/namespace/src/ops/get_global.rs +7 -4
- package/packages/namespace/src/ops/get_local.rs +32 -16
- package/packages/namespace/src/ops/mod.rs +0 -1
- package/packages/namespace/src/ops/resolve_for_name_global.rs +7 -4
- package/packages/namespace/src/ops/resolve_for_name_local.rs +39 -19
- package/packages/namespace/src/workflows/namespace.rs +3 -3
- package/packages/pegboard/Cargo.toml +22 -0
- package/packages/pegboard/src/actor_kv/entry.rs +47 -0
- package/packages/pegboard/src/actor_kv/metrics.rs +19 -0
- package/packages/pegboard/src/actor_kv/mod.rs +530 -0
- package/packages/pegboard/src/actor_kv/preload.rs +363 -0
- package/packages/{actor-kv/src → pegboard/src/actor_kv}/utils.rs +36 -35
- package/packages/pegboard/src/errors.rs +39 -5
- package/packages/pegboard/src/keys/actor.rs +285 -2
- package/packages/{actor-kv/src/entry.rs → pegboard/src/keys/actor_kv.rs} +73 -39
- package/packages/pegboard/src/keys/backfill.rs +49 -0
- package/packages/pegboard/src/keys/envoy.rs +1070 -0
- package/packages/pegboard/src/keys/epoxy/ns.rs +1 -1
- package/packages/pegboard/src/keys/mod.rs +4 -6
- package/packages/pegboard/src/keys/ns.rs +493 -14
- package/packages/pegboard/src/keys/runner.rs +281 -0
- package/packages/{namespace → pegboard}/src/keys/runner_config.rs +53 -0
- package/packages/pegboard/src/lib.rs +15 -2
- package/packages/pegboard/src/metrics.rs +57 -16
- package/packages/pegboard/src/ops/actor/create.rs +123 -53
- package/packages/pegboard/src/ops/actor/get.rs +14 -45
- package/packages/pegboard/src/ops/actor/get_for_gateway.rs +16 -0
- package/packages/pegboard/src/ops/actor/get_for_key.rs +3 -0
- package/packages/pegboard/src/ops/actor/get_for_kv.rs +43 -0
- package/packages/pegboard/src/ops/actor/get_for_runner.rs +99 -0
- package/packages/pegboard/src/ops/actor/get_reservation_for_key.rs +1 -0
- package/packages/pegboard/src/ops/actor/list_for_ns.rs +10 -38
- package/packages/pegboard/src/ops/actor/list_names.rs +3 -3
- package/packages/pegboard/src/ops/actor/mod.rs +3 -1
- package/packages/pegboard/src/ops/actor/util.rs +263 -0
- package/packages/pegboard/src/ops/envoy/drain.rs +101 -0
- package/packages/pegboard/src/ops/envoy/evict_actors.rs +54 -0
- package/packages/pegboard/src/ops/envoy/expire.rs +92 -0
- package/packages/pegboard/src/ops/envoy/get.rs +135 -0
- package/packages/pegboard/src/ops/envoy/list.rs +131 -0
- package/packages/pegboard/src/ops/envoy/mod.rs +6 -0
- package/packages/pegboard/src/ops/envoy/update_ping.rs +92 -0
- package/packages/pegboard/src/ops/mod.rs +3 -0
- package/packages/pegboard/src/ops/runner/drain.rs +110 -0
- package/packages/pegboard/src/ops/runner/list_names.rs +3 -3
- package/packages/pegboard/src/ops/runner/list_runner_config_enabled_dcs.rs +199 -0
- package/packages/pegboard/src/ops/runner/list_runner_config_epoxy_replica_ids.rs +51 -0
- package/packages/pegboard/src/ops/runner/mod.rs +3 -1
- package/packages/pegboard/src/ops/runner/update_alloc_idx.rs +17 -5
- package/packages/{namespace → pegboard}/src/ops/runner_config/delete.rs +18 -9
- package/packages/pegboard/src/ops/runner_config/ensure_normal_if_missing.rs +62 -0
- package/packages/{namespace → pegboard}/src/ops/runner_config/get.rs +15 -5
- package/packages/pegboard/src/ops/runner_config/get_error.rs +146 -0
- package/packages/{namespace → pegboard}/src/ops/runner_config/list.rs +13 -12
- package/packages/pegboard/src/ops/runner_config/mod.rs +7 -0
- package/packages/pegboard/src/ops/runner_config/refresh_metadata.rs +124 -0
- package/packages/pegboard/src/ops/runner_config/upsert.rs +206 -0
- package/packages/pegboard/src/ops/serverless_metadata/fetch.rs +223 -0
- package/packages/pegboard/src/ops/serverless_metadata/mod.rs +1 -0
- package/packages/pegboard/src/pubsub_subjects.rs +52 -0
- package/packages/pegboard/src/utils.rs +36 -2
- package/packages/pegboard/src/workflows/actor/destroy.rs +135 -99
- package/packages/pegboard/src/workflows/actor/keys.rs +59 -5
- package/packages/pegboard/src/workflows/actor/metrics.rs +345 -0
- package/packages/pegboard/src/workflows/actor/mod.rs +848 -204
- package/packages/pegboard/src/workflows/actor/runtime.rs +785 -212
- package/packages/pegboard/src/workflows/actor/setup.rs +61 -0
- package/packages/pegboard/src/workflows/actor2/keys.rs +337 -0
- package/packages/pegboard/src/workflows/actor2/metrics.rs +334 -0
- package/packages/pegboard/src/workflows/actor2/mod.rs +1251 -0
- package/packages/pegboard/src/workflows/actor2/runtime.rs +1005 -0
- package/packages/pegboard/src/workflows/actor_runner_name_selector_backfill.rs +266 -0
- package/packages/pegboard/src/workflows/metrics_aggregator.rs +282 -0
- package/packages/pegboard/src/workflows/mod.rs +8 -0
- package/packages/pegboard/src/workflows/runner.rs +62 -56
- package/packages/pegboard/src/workflows/runner2.rs +978 -0
- package/packages/pegboard/src/workflows/runner_pool.rs +298 -0
- package/packages/pegboard/src/workflows/runner_pool_error_tracker.rs +173 -0
- package/packages/pegboard/src/workflows/runner_pool_metadata_poller.rs +237 -0
- package/packages/pegboard/src/workflows/serverless/backfill.rs +120 -0
- package/packages/pegboard/src/workflows/serverless/conn.rs +702 -0
- package/packages/pegboard/src/workflows/serverless/mod.rs +3 -0
- package/packages/pegboard/src/workflows/serverless/receiver.rs +87 -0
- package/packages/pegboard/tests/actor_v1_pre_migration.rs +77 -0
- package/packages/{actor-kv/tests/list_edge_cases.rs → pegboard/tests/kv_list_edge_cases.rs} +74 -59
- package/packages/{actor-kv → pegboard}/tests/kv_operations.rs +77 -48
- package/packages/pegboard-envoy/Cargo.toml +43 -0
- package/packages/pegboard-envoy/src/actor_event_demuxer.rs +165 -0
- package/packages/pegboard-envoy/src/conn.rs +417 -0
- package/packages/pegboard-envoy/src/errors.rs +38 -0
- package/packages/pegboard-envoy/src/lib.rs +250 -0
- package/packages/pegboard-envoy/src/metrics.rs +44 -0
- package/packages/pegboard-envoy/src/ping_task.rs +61 -0
- package/packages/pegboard-envoy/src/tunnel_to_ws_task.rs +183 -0
- package/packages/pegboard-envoy/src/utils.rs +68 -0
- package/packages/pegboard-envoy/src/ws_to_tunnel_task.rs +536 -0
- package/packages/pegboard-envoy/tests/support/ws_to_tunnel_task.rs +82 -0
- package/packages/pegboard-gateway/Cargo.toml +2 -0
- package/packages/pegboard-gateway/src/keepalive_task.rs +1 -1
- package/packages/pegboard-gateway/src/lib.rs +506 -128
- package/packages/pegboard-gateway/src/metrics.rs +7 -11
- package/packages/pegboard-gateway/src/metrics_task.rs +80 -0
- package/packages/pegboard-gateway/src/ping_task.rs +9 -2
- package/packages/pegboard-gateway/src/shared_state.rs +110 -74
- package/packages/pegboard-gateway/src/tunnel_to_ws_task.rs +21 -7
- package/packages/pegboard-gateway/src/ws_to_tunnel_task.rs +12 -6
- package/packages/pegboard-gateway2/Cargo.toml +37 -0
- package/packages/pegboard-gateway2/src/keepalive_task.rs +61 -0
- package/packages/pegboard-gateway2/src/lib.rs +1044 -0
- package/packages/pegboard-gateway2/src/metrics.rs +10 -0
- package/packages/pegboard-gateway2/src/metrics_task.rs +80 -0
- package/packages/pegboard-gateway2/src/ping_task.rs +30 -0
- package/packages/pegboard-gateway2/src/shared_state.rs +601 -0
- package/packages/pegboard-gateway2/src/tunnel_to_ws_task.rs +99 -0
- package/packages/pegboard-gateway2/src/ws_to_tunnel_task.rs +71 -0
- package/packages/{pegboard-serverless → pegboard-outbound}/Cargo.toml +9 -9
- package/packages/pegboard-outbound/src/lib.rs +487 -0
- package/packages/pegboard-outbound/src/metrics.rs +17 -0
- package/packages/pegboard-runner/Cargo.toml +11 -5
- package/packages/pegboard-runner/src/actor_event_demuxer.rs +163 -0
- package/packages/pegboard-runner/src/conn.rs +358 -122
- package/packages/pegboard-runner/src/errors.rs +5 -0
- package/packages/pegboard-runner/src/lib.rs +62 -36
- package/packages/pegboard-runner/src/metrics.rs +44 -0
- package/packages/pegboard-runner/src/ping_task.rs +60 -13
- package/packages/pegboard-runner/src/tunnel_to_ws_task.rs +249 -110
- package/packages/pegboard-runner/src/ws_to_tunnel_task.rs +738 -113
- package/packages/pegboard-runner/tests/support/ws_to_tunnel_task.rs +150 -0
- package/packages/pools/Cargo.toml +1 -2
- package/packages/pools/src/db/clickhouse.rs +7 -6
- package/packages/pools/src/db/udb.rs +16 -3
- package/packages/pools/src/db/ups.rs +27 -5
- package/packages/pools/src/error.rs +0 -3
- package/packages/pools/src/lib.rs +0 -2
- package/packages/pools/src/metrics.rs +33 -28
- package/packages/pools/src/pools.rs +15 -39
- package/packages/pools/src/prelude.rs +1 -1
- package/packages/postgres-util/Cargo.toml +13 -0
- package/packages/postgres-util/src/lib.rs +84 -0
- package/packages/runner-protocol/build.rs +157 -0
- package/packages/runner-protocol/src/lib.rs +16 -0
- package/packages/runner-protocol/src/util.rs +14 -0
- package/packages/runner-protocol/src/versioned.rs +4345 -0
- package/packages/runtime/src/lib.rs +46 -46
- package/packages/runtime/src/metrics.rs +39 -30
- package/packages/runtime/src/term_signal.rs +25 -12
- package/packages/runtime/src/traces.rs +5 -8
- package/packages/service-manager/src/lib.rs +66 -15
- package/packages/test-deps/src/datacenter.rs +22 -8
- package/packages/test-deps/src/lib.rs +47 -25
- package/packages/test-deps-docker/src/database.rs +45 -36
- package/packages/test-snapshot-gen/Cargo.toml +39 -0
- package/packages/test-snapshot-gen/snapshots/.gitkeep +0 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/metadata.json +3 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-1/000008.log +0 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-1/000009.sst +3 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-1/CURRENT +3 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-1/MANIFEST-000005 +3 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-1/OPTIONS-000007 +3 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-2/000008.log +0 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-2/000009.sst +3 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-2/CURRENT +3 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-2/MANIFEST-000005 +3 -0
- package/packages/test-snapshot-gen/snapshots/epoxy-v1/replica-2/OPTIONS-000007 +3 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/metadata.json +3 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-1/000008.log +0 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-1/000009.sst +3 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-1/CURRENT +3 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-1/MANIFEST-000005 +3 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-1/OPTIONS-000007 +3 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-2/000008.log +0 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-2/000009.sst +3 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-2/CURRENT +3 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-2/MANIFEST-000005 +3 -0
- package/packages/test-snapshot-gen/snapshots/pb-actor-v1-pre-migration/replica-2/OPTIONS-000007 +3 -0
- package/packages/test-snapshot-gen/src/lib.rs +328 -0
- package/packages/test-snapshot-gen/src/main.rs +145 -0
- package/packages/test-snapshot-gen/src/scenarios/epoxy_keys.rs +60 -0
- package/packages/test-snapshot-gen/src/scenarios/mod.rs +27 -0
- package/packages/test-snapshot-gen/src/scenarios/pb_actor_v1_pre_migration.rs +56 -0
- package/packages/test-snapshot-gen/src/test_cluster.rs +234 -0
- package/packages/tracing-reconfigure/src/lib.rs +1 -1
- package/packages/tracing-utils/src/lib.rs +12 -20
- package/packages/types/src/actor/error.rs +59 -0
- package/packages/types/src/actor/mod.rs +2 -0
- package/packages/types/src/actors.rs +5 -0
- package/packages/types/src/envoys.rs +21 -0
- package/packages/types/src/keys/backfill.rs +5 -0
- package/packages/types/src/keys/mod.rs +1 -0
- package/packages/types/src/lib.rs +2 -1
- package/packages/types/src/runner_configs.rs +43 -14
- package/packages/universaldb/Cargo.toml +4 -0
- package/packages/universaldb/src/database.rs +50 -5
- package/packages/universaldb/src/driver/mod.rs +12 -2
- package/packages/universaldb/src/driver/postgres/database.rs +88 -27
- package/packages/universaldb/src/driver/postgres/mod.rs +1 -1
- package/packages/universaldb/src/driver/postgres/transaction.rs +4 -7
- package/packages/universaldb/src/driver/postgres/transaction_task.rs +30 -52
- package/packages/universaldb/src/driver/rocksdb/database.rs +13 -7
- package/packages/universaldb/src/driver/rocksdb/transaction_conflict_tracker.rs +5 -5
- package/packages/universaldb/src/driver/rocksdb/transaction_task.rs +2 -1
- package/packages/universaldb/src/metrics.rs +39 -23
- package/packages/universaldb/src/prelude.rs +1 -1
- package/packages/universaldb/src/transaction.rs +9 -2
- package/packages/universaldb/src/utils/cherry_pick.rs +46 -46
- package/packages/universaldb/src/utils/keys.rs +21 -2
- package/packages/universaldb/src/utils/mod.rs +8 -0
- package/packages/universaldb/src/utils/subspace.rs +9 -4
- package/packages/universaldb/tests/integration.rs +5 -3
- package/packages/universaldb/tests/integration_gas.rs +5 -3
- package/packages/universaldb/tests/rocksdb.rs +152 -19
- package/packages/universalpubsub/Cargo.toml +8 -2
- package/packages/universalpubsub/benches/simple.rs +28 -8
- package/packages/universalpubsub/src/chunking.rs +27 -5
- package/packages/universalpubsub/src/driver/memory/mod.rs +131 -20
- package/packages/universalpubsub/src/driver/mod.rs +5 -0
- package/packages/universalpubsub/src/driver/nats/mod.rs +8 -0
- package/packages/universalpubsub/src/driver/postgres/mod.rs +505 -96
- package/packages/universalpubsub/src/lib.rs +3 -0
- package/packages/universalpubsub/src/metrics.rs +60 -0
- package/packages/universalpubsub/src/pubsub.rs +227 -87
- package/packages/universalpubsub/src/subject.rs +32 -0
- package/packages/universalpubsub/tests/chunking.rs +298 -0
- package/packages/universalpubsub/tests/integration.rs +148 -7
- package/packages/universalpubsub/tests/reconnect.rs +8 -6
- package/packages/util/Cargo.toml +1 -3
- package/packages/util/build.rs +6 -0
- package/packages/util/src/lib.rs +7 -2
- package/packages/util/src/metric.rs +1 -0
- package/packages/util/src/serde.rs +1 -516
- package/packages/{internal → util-serde}/Cargo.toml +4 -5
- package/packages/util-serde/src/lib.rs +517 -0
- package/packages/workflow-worker/Cargo.toml +4 -4
- package/packages/workflow-worker/src/lib.rs +3 -2
- package/sdks/go/api-full/client/client.go +17 -4
- package/sdks/go/api-full/metadata/client.go +50 -0
- package/sdks/go/api-full/namespaces/client.go +3 -0
- package/sdks/go/api-full/namespaces.go +6 -4
- package/sdks/go/api-full/runners/client.go +3 -0
- package/sdks/go/api-full/runners.go +8 -6
- package/sdks/go/api-full/types.go +107 -23
- package/sdks/rust/api-full/rust/.openapi-generator/FILES +6 -0
- package/sdks/rust/api-full/rust/Cargo.toml +1 -1
- package/sdks/rust/api-full/rust/README.md +5 -2
- package/sdks/rust/api-full/rust/docs/Actor.md +1 -0
- package/sdks/rust/api-full/rust/docs/ActorsDeleteApi.md +1 -1
- package/sdks/rust/api-full/rust/docs/ActorsKvGetApi.md +2 -1
- package/sdks/rust/api-full/rust/docs/ActorsListApi.md +3 -2
- package/sdks/rust/api-full/rust/docs/MetadataApi.md +34 -0
- package/sdks/rust/api-full/rust/docs/MetadataGetResponse.md +18 -0
- package/sdks/rust/api-full/rust/docs/NamespacesApi.md +3 -2
- package/sdks/rust/api-full/rust/docs/RunnerConfig.md +1 -0
- package/sdks/rust/api-full/rust/docs/RunnerConfigKindOneOf1Serverless.md +1 -0
- package/sdks/rust/api-full/rust/docs/RunnerConfigResponse.md +15 -0
- package/sdks/rust/api-full/rust/docs/RunnerConfigsListApi.md +3 -2
- package/sdks/rust/api-full/rust/docs/RunnerConfigsListResponseRunnerConfigsValue.md +1 -1
- package/sdks/rust/api-full/rust/docs/RunnersApi.md +3 -2
- package/sdks/rust/api-full/rust/src/apis/actors_create_api.rs +1 -1
- package/sdks/rust/api-full/rust/src/apis/actors_delete_api.rs +3 -5
- package/sdks/rust/api-full/rust/src/apis/actors_get_or_create_api.rs +1 -1
- package/sdks/rust/api-full/rust/src/apis/actors_kv_get_api.rs +4 -2
- package/sdks/rust/api-full/rust/src/apis/actors_list_api.rs +9 -2
- package/sdks/rust/api-full/rust/src/apis/actors_list_names_api.rs +1 -1
- package/sdks/rust/api-full/rust/src/apis/configuration.rs +2 -2
- package/sdks/rust/api-full/rust/src/apis/datacenters_api.rs +1 -1
- package/sdks/rust/api-full/rust/src/apis/health_api.rs +1 -1
- package/sdks/rust/api-full/rust/src/apis/metadata_api.rs +62 -0
- package/sdks/rust/api-full/rust/src/apis/mod.rs +1 -0
- package/sdks/rust/api-full/rust/src/apis/namespaces_api.rs +9 -2
- package/sdks/rust/api-full/rust/src/apis/runner_configs_delete_api.rs +1 -1
- package/sdks/rust/api-full/rust/src/apis/runner_configs_list_api.rs +9 -2
- package/sdks/rust/api-full/rust/src/apis/runner_configs_refresh_metadata_api.rs +1 -1
- package/sdks/rust/api-full/rust/src/apis/runner_configs_serverless_health_check_api.rs +1 -1
- package/sdks/rust/api-full/rust/src/apis/runner_configs_upsert_api.rs +1 -1
- package/sdks/rust/api-full/rust/src/apis/runners_api.rs +9 -2
- package/sdks/rust/api-full/rust/src/models/actor.rs +5 -1
- package/sdks/rust/api-full/rust/src/models/actor_name.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/actors_create_request.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/actors_create_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/actors_get_or_create_request.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/actors_get_or_create_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/actors_kv_get_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/actors_list_names_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/actors_list_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/crash_policy.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/datacenter.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/datacenter_health.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/datacenters_list_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/health_fanout_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/health_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/health_status.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/metadata_get_response.rs +48 -0
- package/sdks/rust/api-full/rust/src/models/mod.rs +4 -0
- package/sdks/rust/api-full/rust/src/models/namespace.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/namespace_list_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/namespaces_create_request.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/namespaces_create_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/pagination.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_config.rs +4 -1
- package/sdks/rust/api-full/rust/src/models/runner_config_kind.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_config_kind_one_of.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_config_kind_one_of_1.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_config_kind_one_of_1_serverless.rs +5 -1
- package/sdks/rust/api-full/rust/src/models/runner_config_response.rs +39 -0
- package/sdks/rust/api-full/rust/src/models/runner_config_variant.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_list_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_list_response_runner_configs_value.rs +3 -3
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_health_check_request.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_health_check_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_health_check_response_one_of.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_health_check_response_one_of_1.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_health_check_response_one_of_1_failure.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_health_check_response_one_of_success.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error_one_of.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error_one_of_1.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error_one_of_2.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error_one_of_3.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error_one_of_3_non_success_status.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error_one_of_4.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error_one_of_4_invalid_response_json.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error_one_of_5.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_serverless_metadata_error_one_of_5_invalid_response_schema.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_upsert_request_body.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runner_configs_upsert_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runners_list_names_response.rs +1 -1
- package/sdks/rust/api-full/rust/src/models/runners_list_response.rs +1 -1
- package/sdks/rust/api-full/src/apis/actors_api.rs +8 -4
- package/sdks/rust/api-full/src/apis/ns_api.rs +8 -4
- package/sdks/rust/data/src/converted.rs +7 -4
- package/sdks/rust/data/src/lib.rs +2 -2
- package/sdks/rust/data/src/versioned/mod.rs +47 -4
- package/sdks/rust/data/src/versioned/namespace_runner_config.rs +256 -6
- package/sdks/rust/envoy-client/Cargo.toml +25 -0
- package/sdks/rust/envoy-client/src/actor.rs +992 -0
- package/sdks/rust/envoy-client/src/commands.rs +88 -0
- package/sdks/rust/envoy-client/src/config.rs +159 -0
- package/sdks/rust/envoy-client/src/connection.rs +288 -0
- package/sdks/rust/envoy-client/src/context.rs +24 -0
- package/sdks/rust/envoy-client/src/envoy.rs +432 -0
- package/sdks/rust/envoy-client/src/events.rs +62 -0
- package/sdks/rust/envoy-client/src/handle.rs +355 -0
- package/sdks/rust/envoy-client/src/kv.rs +132 -0
- package/sdks/rust/envoy-client/src/latency_channel.rs +27 -0
- package/sdks/rust/envoy-client/src/lib.rs +15 -0
- package/sdks/rust/envoy-client/src/stringify.rs +322 -0
- package/sdks/rust/envoy-client/src/tunnel.rs +265 -0
- package/sdks/rust/envoy-client/src/utils.rs +172 -0
- package/sdks/rust/envoy-protocol/Cargo.toml +22 -0
- package/sdks/rust/envoy-protocol/build.rs +200 -0
- package/sdks/rust/envoy-protocol/src/generated.rs +1 -0
- package/sdks/rust/envoy-protocol/src/lib.rs +8 -0
- package/sdks/rust/envoy-protocol/src/versioned.rs +208 -0
- package/sdks/rust/epoxy-protocol/Cargo.toml +0 -2
- package/sdks/rust/epoxy-protocol/build.rs +7 -0
- package/sdks/rust/epoxy-protocol/src/lib.rs +2 -5
- package/sdks/rust/epoxy-protocol/src/protocol.rs +128 -0
- package/sdks/rust/test-envoy/Cargo.toml +23 -0
- package/sdks/rust/test-envoy/Dockerfile +22 -0
- package/sdks/rust/test-envoy/src/behaviors.rs +141 -0
- package/sdks/rust/test-envoy/src/lib.rs +11 -0
- package/sdks/rust/test-envoy/src/main.rs +4 -0
- package/sdks/rust/test-envoy/src/server.rs +269 -0
- package/sdks/schemas/README.md +1 -2
- package/sdks/schemas/data/namespace.runner_config.v3.bare +24 -0
- package/sdks/schemas/data/namespace.runner_config.v4.bare +25 -0
- package/sdks/schemas/data/namespace.runner_config.v5.bare +26 -0
- package/sdks/schemas/data/pegboard.namespace.runner_alloc_idx.v2.bare +8 -0
- package/sdks/schemas/envoy-protocol/v1.bare +459 -0
- package/sdks/schemas/epoxy-protocol/v2.bare +220 -0
- package/sdks/schemas/runner-protocol/v4.bare +438 -0
- package/sdks/schemas/runner-protocol/v5.bare +430 -0
- package/sdks/schemas/runner-protocol/v6.bare +432 -0
- package/sdks/schemas/runner-protocol/v7.bare +438 -0
- package/sdks/typescript/api-full/.turbo/turbo-build.log +28 -27
- package/sdks/typescript/api-full/build.js +7 -1
- package/sdks/typescript/api-full/package.json +66 -57
- package/sdks/typescript/api-full/rivetkit-engine-api-full-25.5.3.tgz +0 -0
- package/sdks/typescript/api-full/src/Client.ts +41 -10
- package/sdks/typescript/api-full/src/api/client/requests/ActorsDeleteRequest.ts +4 -2
- package/sdks/typescript/api-full/src/api/client/requests/ActorsKvGetRequest.ts +13 -0
- package/sdks/typescript/api-full/src/api/client/requests/ActorsListRequest.ts +6 -0
- package/sdks/typescript/api-full/src/api/client/requests/RunnerConfigsListRequest.ts +4 -0
- package/sdks/typescript/api-full/src/api/client/requests/index.ts +1 -0
- package/sdks/typescript/api-full/src/api/resources/index.ts +1 -0
- package/sdks/typescript/api-full/src/api/resources/metadata/client/Client.ts +97 -0
- package/sdks/typescript/api-full/src/api/resources/metadata/client/index.ts +1 -0
- package/sdks/typescript/api-full/src/api/resources/metadata/index.ts +1 -0
- package/sdks/typescript/api-full/src/api/resources/namespaces/client/Client.ts +12 -2
- package/sdks/typescript/api-full/src/api/resources/namespaces/client/requests/NamespacesListRequest.ts +6 -0
- package/sdks/typescript/api-full/src/api/resources/runners/client/Client.ts +12 -2
- package/sdks/typescript/api-full/src/api/resources/runners/client/requests/RunnersListRequest.ts +6 -0
- package/sdks/typescript/api-full/src/api/types/Actor.ts +2 -0
- package/sdks/typescript/api-full/src/api/types/MetadataGetResponse.ts +14 -0
- package/sdks/typescript/api-full/src/api/types/RunnerConfig.ts +1 -0
- package/sdks/typescript/api-full/src/api/types/RunnerConfigKindServerlessServerless.ts +2 -0
- package/sdks/typescript/api-full/src/api/types/RunnerConfigResponse.ts +9 -0
- package/sdks/typescript/api-full/src/api/types/RunnerConfigServerless.ts +2 -0
- package/sdks/typescript/api-full/src/api/types/RunnerConfigsListResponseRunnerConfigsValue.ts +1 -1
- package/sdks/typescript/api-full/src/api/types/index.ts +2 -0
- package/sdks/typescript/api-full/src/core/fetcher/stream-wrappers/Node18UniversalStreamWrapper.ts +6 -4
- package/sdks/typescript/api-full/src/core/fetcher/stream-wrappers/UndiciStreamWrapper.ts +4 -3
- package/sdks/typescript/api-full/src/serialization/types/Actor.ts +2 -0
- package/sdks/typescript/api-full/src/serialization/types/MetadataGetResponse.ts +34 -0
- package/sdks/typescript/api-full/src/serialization/types/RunnerConfig.ts +5 -0
- package/sdks/typescript/api-full/src/serialization/types/RunnerConfigKindServerlessServerless.ts +2 -0
- package/sdks/typescript/api-full/src/serialization/types/RunnerConfigResponse.ts +26 -0
- package/sdks/typescript/api-full/src/serialization/types/RunnerConfigServerless.ts +2 -0
- package/sdks/typescript/api-full/src/serialization/types/RunnerConfigsListResponseRunnerConfigsValue.ts +3 -3
- package/sdks/typescript/api-full/src/serialization/types/index.ts +2 -0
- package/sdks/typescript/api-full/turbo.json +9 -0
- package/sdks/typescript/envoy-protocol/.turbo/turbo-build.log +23 -0
- package/sdks/typescript/{runner-protocol → envoy-protocol}/dist/index.cjs +466 -281
- package/sdks/typescript/envoy-protocol/dist/index.cjs.map +1 -0
- package/sdks/typescript/envoy-protocol/dist/index.d.cts +699 -0
- package/sdks/typescript/envoy-protocol/dist/index.d.ts +699 -0
- package/sdks/typescript/{runner-protocol → envoy-protocol}/dist/index.js +530 -345
- package/sdks/typescript/envoy-protocol/dist/index.js.map +1 -0
- package/sdks/typescript/{runner-protocol → envoy-protocol}/node_modules/.bin/tsc +4 -4
- package/{tests/load → sdks/typescript/envoy-protocol}/node_modules/.bin/tsserver +4 -4
- package/sdks/typescript/envoy-protocol/node_modules/.bin/tsup +21 -0
- package/sdks/typescript/envoy-protocol/node_modules/.bin/tsup-node +21 -0
- package/sdks/typescript/envoy-protocol/package.json +36 -0
- package/sdks/typescript/envoy-protocol/src/index.ts +2331 -0
- package/sdks/typescript/envoy-protocol/tsconfig.json +9 -0
- package/sdks/typescript/envoy-protocol/tsup.config.ts +4 -0
- package/sdks/typescript/runner/package.json +8 -2
- package/sdks/typescript/runner/src/actor.ts +38 -0
- package/sdks/typescript/runner/src/mod.ts +435 -229
- package/sdks/typescript/runner/src/stringify.ts +36 -33
- package/sdks/typescript/runner/src/tunnel.ts +52 -56
- package/sdks/typescript/runner/src/utils.ts +19 -0
- package/sdks/typescript/runner/src/websocket-tunnel-adapter.ts +98 -435
- package/sdks/typescript/runner-protocol/package.json +11 -9
- package/sdks/typescript/runner-protocol/src/index.ts +224 -156
- package/sdks/typescript/runner-protocol/tsconfig.json +1 -9
- package/sdks/typescript/test-runner/.turbo/turbo-build.log +5 -5
- package/sdks/typescript/test-runner/dist/index.js +53 -44
- package/sdks/typescript/test-runner/dist/index.js.map +1 -1
- package/sdks/typescript/test-runner/node_modules/.bin/pino +2 -2
- package/sdks/typescript/test-runner/node_modules/.bin/tsc +4 -4
- package/sdks/typescript/test-runner/node_modules/.bin/tsserver +4 -4
- package/sdks/typescript/test-runner/node_modules/.bin/tsup +4 -4
- package/sdks/typescript/test-runner/node_modules/.bin/tsup-node +4 -4
- package/sdks/typescript/test-runner/node_modules/.bin/tsx +4 -4
- package/sdks/typescript/test-runner/node_modules/.bin/vitest +4 -4
- package/sdks/typescript/test-runner/package.json +3 -3
- package/sdks/typescript/test-runner/src/index.ts +65 -42
- package/sdks/typescript/test-runner/src/log.ts +4 -18
- package/artifacts/errors/actor.no_runners_available.json +0 -5
- package/artifacts/errors/guard.actor_destroyed.json +0 -5
- package/artifacts/errors/guard.actor_not_found.json +0 -5
- package/contrib-docs/ACTOR_KEY_RESERVATION.md +0 -101
- package/contrib-docs/API.md +0 -11
- package/contrib-docs/DOCKER.md +0 -5
- package/contrib-docs/ERRORS.md +0 -13
- package/contrib-docs/GUARD.md +0 -76
- package/contrib-docs/PEGBOARD_TUNNEL_RETRIES.md +0 -83
- package/contrib-docs/RUNNER_LIFECYCLE.md +0 -172
- package/contrib-docs/SDKS.md +0 -9
- package/contrib-docs/TEST_DEPENDENCIES.md +0 -43
- package/contrib-docs/design-choicse/EMBEDDED_KV.md +0 -80
- package/contrib-docs/operate/TRACING_RECONFIGURE.md +0 -78
- package/docker/dev/otel-collector-client/config.yaml +0 -39
- package/docker/dev-host/otel-collector-client/config.yaml +0 -39
- package/docker/dev-multidc/datacenters/dc-a/otel-collector-client/config.yaml +0 -39
- package/docker/dev-multidc/datacenters/dc-b/otel-collector-client/config.yaml +0 -39
- package/docker/dev-multidc/datacenters/dc-c/otel-collector-client/config.yaml +0 -39
- package/docker/dev-multidc-multinode/datacenters/dc-a/otel-collector-client/config.yaml +0 -39
- package/docker/dev-multidc-multinode/datacenters/dc-b/otel-collector-client/config.yaml +0 -39
- package/docker/dev-multidc-multinode/datacenters/dc-c/otel-collector-client/config.yaml +0 -39
- package/docker/dev-multinode/otel-collector-client/config.yaml +0 -39
- package/docker/template/src/services/edge/otel-collector-client.ts +0 -64
- package/packages/actor-kv/Cargo.toml +0 -31
- package/packages/actor-kv/src/key.rs +0 -81
- package/packages/actor-kv/src/lib.rs +0 -357
- package/packages/cache/src/rate_limit.rs +0 -109
- package/packages/cache/tests/integration.rs +0 -582
- package/packages/clickhouse-inserter/Cargo.toml +0 -17
- package/packages/clickhouse-inserter/src/error.rs +0 -16
- package/packages/clickhouse-inserter/src/lib.rs +0 -179
- package/packages/clickhouse-user-query/Cargo.toml +0 -16
- package/packages/clickhouse-user-query/examples/case_sensitivity_demo.rs +0 -100
- package/packages/clickhouse-user-query/examples/group_by_example.rs +0 -53
- package/packages/clickhouse-user-query/examples/string_contains_demo.rs +0 -96
- package/packages/clickhouse-user-query/src/builder.rs +0 -445
- package/packages/clickhouse-user-query/src/error.rs +0 -37
- package/packages/clickhouse-user-query/src/lib.rs +0 -61
- package/packages/clickhouse-user-query/src/query.rs +0 -143
- package/packages/clickhouse-user-query/src/schema.rs +0 -78
- package/packages/clickhouse-user-query/tests/builder_tests.rs +0 -619
- package/packages/clickhouse-user-query/tests/case_sensitivity_tests.rs +0 -307
- package/packages/clickhouse-user-query/tests/integration_tests.rs +0 -540
- package/packages/clickhouse-user-query/tests/query_tests.rs +0 -263
- package/packages/clickhouse-user-query/tests/schema_tests.rs +0 -44
- package/packages/config/src/config/vector.rs +0 -18
- package/packages/engine/src/commands/udb_keys.rs +0 -200
- package/packages/engine/tests/actors_create.rs +0 -524
- package/packages/engine/tests/actors_delete.rs +0 -243
- package/packages/engine/tests/actors_general.rs +0 -191
- package/packages/engine/tests/actors_get.rs +0 -230
- package/packages/engine/tests/actors_get_by_id.rs +0 -170
- package/packages/engine/tests/actors_get_or_create.rs +0 -294
- package/packages/engine/tests/actors_get_or_create_by_id.rs +0 -147
- package/packages/engine/tests/actors_lifecycle.rs +0 -165
- package/packages/engine/tests/actors_list.rs +0 -798
- package/packages/engine/tests/actors_list_names.rs +0 -353
- package/packages/engine/tests/common/ns.rs +0 -36
- package/packages/engine/tests/common/runner.rs +0 -134
- package/packages/engine/tests/runners_dupe_key.rs +0 -27
- package/packages/engine/tests/runners_version.rs +0 -50
- package/packages/env/build.rs +0 -8
- package/packages/epoxy/spec/KEYS.md +0 -33
- package/packages/epoxy/spec/PROPOSAL.md +0 -125
- package/packages/epoxy/spec/RECONFIGURE.md +0 -40
- package/packages/epoxy/src/ops/explicit_prepare.rs +0 -342
- package/packages/epoxy/src/replica/decide_path.rs +0 -51
- package/packages/epoxy/src/replica/lead_consensus.rs +0 -65
- package/packages/epoxy/src/replica/log.rs +0 -84
- package/packages/epoxy/src/replica/messages/accepted.rs +0 -35
- package/packages/epoxy/src/replica/messages/committed.rs +0 -41
- package/packages/epoxy/src/replica/messages/download_instances.rs +0 -69
- package/packages/epoxy/src/replica/messages/pre_accept.rs +0 -69
- package/packages/epoxy/src/replica/utils.rs +0 -111
- package/packages/epoxy/src/workflows/purger.rs +0 -81
- package/packages/guard/src/cache/actor.rs +0 -43
- package/packages/guard/src/middleware.rs +0 -42
- package/packages/guard/src/routing/pegboard_gateway.rs +0 -260
- package/packages/guard-core/src/analytics.rs +0 -46
- package/packages/internal/README.md +0 -1
- package/packages/internal/src/lib.rs +0 -1
- package/packages/internal/src/ops/bump_serverless_autoscaler_global.rs +0 -64
- package/packages/internal/src/ops/cache/mod.rs +0 -1
- package/packages/internal/src/ops/cache/purge_global.rs +0 -81
- package/packages/internal/src/ops/mod.rs +0 -2
- package/packages/namespace/src/ops/runner_config/mod.rs +0 -4
- package/packages/namespace/src/ops/runner_config/upsert.rs +0 -148
- package/packages/pegboard/src/ops/actor/get_runner.rs +0 -64
- package/packages/pegboard/src/ops/runner/find_dc_with_runner.rs +0 -222
- package/packages/pegboard-serverless/src/lib.rs +0 -523
- package/packages/types/src/msgs/mod.rs +0 -1
- package/packages/types/src/msgs/pegboard.rs +0 -5
- package/sdks/rust/epoxy-protocol/src/versioned.rs +0 -206
- package/sdks/rust/runner-protocol/build.rs +0 -115
- package/sdks/rust/runner-protocol/src/lib.rs +0 -10
- package/sdks/rust/runner-protocol/src/versioned.rs +0 -1734
- package/sdks/schemas/epoxy-protocol/v1.bare +0 -260
- package/sdks/typescript/runner/.turbo/turbo-build.log +0 -22
- package/sdks/typescript/runner/dist/mod.cjs +0 -2951
- package/sdks/typescript/runner/dist/mod.cjs.map +0 -1
- package/sdks/typescript/runner/dist/mod.d.cts +0 -326
- package/sdks/typescript/runner/dist/mod.d.ts +0 -326
- package/sdks/typescript/runner/dist/mod.js +0 -2951
- package/sdks/typescript/runner/dist/mod.js.map +0 -1
- package/sdks/typescript/runner/node_modules/.bin/pino +0 -21
- package/sdks/typescript/runner/node_modules/.bin/tsc +0 -21
- package/sdks/typescript/runner/node_modules/.bin/tsserver +0 -21
- package/sdks/typescript/runner/node_modules/.bin/tsup +0 -21
- package/sdks/typescript/runner/node_modules/.bin/tsup-node +0 -21
- package/sdks/typescript/runner/node_modules/.bin/tsx +0 -21
- package/sdks/typescript/runner/node_modules/.bin/uuid +0 -21
- package/sdks/typescript/runner/node_modules/.bin/vitest +0 -21
- package/sdks/typescript/runner-protocol/.turbo/turbo-build.log +0 -22
- package/sdks/typescript/runner-protocol/dist/index.cjs.map +0 -1
- package/sdks/typescript/runner-protocol/dist/index.d.cts +0 -666
- package/sdks/typescript/runner-protocol/dist/index.d.ts +0 -666
- package/sdks/typescript/runner-protocol/dist/index.js.map +0 -1
- package/sdks/typescript/runner-protocol/node_modules/.bin/tsserver +0 -21
- package/sdks/typescript/runner-protocol/node_modules/.bin/tsup +0 -21
- package/sdks/typescript/runner-protocol/node_modules/.bin/tsup-node +0 -21
- package/sdks/typescript/test-runner/Dockerfile +0 -26
- package/tests/load/README.md +0 -28
- package/tests/load/actor-lifecycle/README.md +0 -26
- package/tests/load/actor-lifecycle/actor.ts +0 -41
- package/tests/load/actor-lifecycle/config.ts +0 -14
- package/tests/load/actor-lifecycle/index.ts +0 -62
- package/tests/load/actor-lifecycle/rivet_api.ts +0 -140
- package/tests/load/actor-lifecycle/types.ts +0 -17
- package/tests/load/node_modules/.bin/biome +0 -21
- package/tests/load/node_modules/.bin/tsc +0 -21
- package/tests/load/package.json +0 -15
- package/tests/load/tsconfig.json +0 -20
- package/tests/smoke/README.md +0 -32
- package/tests/smoke/package.json +0 -19
- package/tests/smoke/scripts/connect.ts +0 -41
- package/tests/smoke/src/server/registry.ts +0 -32
- package/tests/smoke/src/server/server.ts +0 -7
- package/tests/smoke/src/smoke-test/index.ts +0 -161
- package/tests/smoke/src/smoke-test/spawn-actor.ts +0 -109
- package/tests/smoke/tsconfig.json +0 -43
- /package/packages/{dump-openapi → api-public-openapi-gen}/build.rs +0 -0
- /package/packages/{dump-openapi → api-public-openapi-gen}/src/lib.rs +0 -0
- /package/{sdks/rust → packages}/runner-protocol/Cargo.toml +0 -0
- /package/{sdks/rust → packages}/runner-protocol/src/compat.rs +0 -0
- /package/{sdks/rust → packages}/runner-protocol/src/generated.rs +0 -0
- /package/{sdks/rust → packages}/runner-protocol/src/uuid_compat.rs +0 -0
- /package/sdks/rust/{runner-protocol → envoy-protocol}/src/util.rs +0 -0
- /package/{tests/smoke → sdks/typescript/envoy-protocol}/turbo.json +0 -0
|
@@ -1,2951 +0,0 @@
|
|
|
1
|
-
// src/mod.ts
|
|
2
|
-
import * as protocol from "@rivetkit/engine-runner-protocol";
|
|
3
|
-
|
|
4
|
-
// src/log.ts
|
|
5
|
-
var LOGGER;
|
|
6
|
-
function setLogger(logger2) {
|
|
7
|
-
LOGGER = logger2;
|
|
8
|
-
}
|
|
9
|
-
function logger() {
|
|
10
|
-
return LOGGER;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// src/utils.ts
|
|
14
|
-
function unreachable(x) {
|
|
15
|
-
throw `Unreachable: ${x}`;
|
|
16
|
-
}
|
|
17
|
-
function calculateBackoff(attempt, options = {}) {
|
|
18
|
-
const {
|
|
19
|
-
initialDelay = 1e3,
|
|
20
|
-
maxDelay = 3e4,
|
|
21
|
-
multiplier = 2,
|
|
22
|
-
jitter = true
|
|
23
|
-
} = options;
|
|
24
|
-
let delay = Math.min(initialDelay * multiplier ** attempt, maxDelay);
|
|
25
|
-
if (jitter) {
|
|
26
|
-
delay = delay * (1 + Math.random() * 0.25);
|
|
27
|
-
}
|
|
28
|
-
return Math.floor(delay);
|
|
29
|
-
}
|
|
30
|
-
function parseWebSocketCloseReason(reason) {
|
|
31
|
-
var _a;
|
|
32
|
-
const [mainPart, rayId] = reason.split("#");
|
|
33
|
-
const [group, error] = mainPart.split(".");
|
|
34
|
-
if (!group || !error) {
|
|
35
|
-
(_a = logger()) == null ? void 0 : _a.warn({ msg: "failed to parse close reason", reason });
|
|
36
|
-
return void 0;
|
|
37
|
-
}
|
|
38
|
-
return {
|
|
39
|
-
group,
|
|
40
|
-
error,
|
|
41
|
-
rayId
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
var U16_MAX = 65535;
|
|
45
|
-
function wrappingLtU16(a, b) {
|
|
46
|
-
return a !== b && wrappingSub(b, a, U16_MAX) < U16_MAX / 2;
|
|
47
|
-
}
|
|
48
|
-
function wrappingLteU16(a, b) {
|
|
49
|
-
return a === b || wrappingLtU16(a, b);
|
|
50
|
-
}
|
|
51
|
-
function wrappingAddU16(a, b) {
|
|
52
|
-
return (a + b) % (U16_MAX + 1);
|
|
53
|
-
}
|
|
54
|
-
function wrappingSubU16(a, b) {
|
|
55
|
-
return wrappingSub(a, b, U16_MAX);
|
|
56
|
-
}
|
|
57
|
-
function wrappingSub(a, b, max) {
|
|
58
|
-
const result = a - b;
|
|
59
|
-
if (result < 0) {
|
|
60
|
-
return result + max + 1;
|
|
61
|
-
}
|
|
62
|
-
return result;
|
|
63
|
-
}
|
|
64
|
-
function arraysEqual(a, b) {
|
|
65
|
-
const ua = new Uint8Array(a);
|
|
66
|
-
const ub = new Uint8Array(b);
|
|
67
|
-
if (ua.length !== ub.length) return false;
|
|
68
|
-
for (let i = 0; i < ua.length; i++) {
|
|
69
|
-
if (ua[i] !== ub[i]) return false;
|
|
70
|
-
}
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
function promiseWithResolvers() {
|
|
74
|
-
let resolve;
|
|
75
|
-
let reject;
|
|
76
|
-
const promise = new Promise((res, rej) => {
|
|
77
|
-
resolve = res;
|
|
78
|
-
reject = rej;
|
|
79
|
-
});
|
|
80
|
-
return { promise, resolve, reject };
|
|
81
|
-
}
|
|
82
|
-
function idToStr(id) {
|
|
83
|
-
const bytes = new Uint8Array(id);
|
|
84
|
-
return Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// src/actor.ts
|
|
88
|
-
var RunnerActor = class {
|
|
89
|
-
constructor(actorId, generation, config, hibernatingRequests) {
|
|
90
|
-
this.hibernatingRequests = hibernatingRequests;
|
|
91
|
-
this.actorId = actorId;
|
|
92
|
-
this.generation = generation;
|
|
93
|
-
this.config = config;
|
|
94
|
-
this.actorStartPromise = promiseWithResolvers();
|
|
95
|
-
}
|
|
96
|
-
actorId;
|
|
97
|
-
generation;
|
|
98
|
-
config;
|
|
99
|
-
pendingRequests = [];
|
|
100
|
-
webSockets = [];
|
|
101
|
-
actorStartPromise;
|
|
102
|
-
/**
|
|
103
|
-
* If restoreHibernatingRequests has been called. This is used to assert
|
|
104
|
-
* that the caller is implemented correctly.
|
|
105
|
-
**/
|
|
106
|
-
hibernationRestored = false;
|
|
107
|
-
// Pending request methods
|
|
108
|
-
getPendingRequest(gatewayId, requestId) {
|
|
109
|
-
var _a;
|
|
110
|
-
return (_a = this.pendingRequests.find(
|
|
111
|
-
(entry) => arraysEqual(entry.gatewayId, gatewayId) && arraysEqual(entry.requestId, requestId)
|
|
112
|
-
)) == null ? void 0 : _a.request;
|
|
113
|
-
}
|
|
114
|
-
createPendingRequest(gatewayId, requestId, clientMessageIndex) {
|
|
115
|
-
var _a, _b;
|
|
116
|
-
const exists = this.getPendingRequest(gatewayId, requestId) !== void 0;
|
|
117
|
-
if (exists) {
|
|
118
|
-
(_a = logger()) == null ? void 0 : _a.warn({
|
|
119
|
-
msg: "attempting to set pending request twice, replacing existing",
|
|
120
|
-
gatewayId: idToStr(gatewayId),
|
|
121
|
-
requestId: idToStr(requestId)
|
|
122
|
-
});
|
|
123
|
-
this.deletePendingRequest(gatewayId, requestId);
|
|
124
|
-
}
|
|
125
|
-
this.pendingRequests.push({
|
|
126
|
-
gatewayId,
|
|
127
|
-
requestId,
|
|
128
|
-
request: {
|
|
129
|
-
resolve: () => {
|
|
130
|
-
},
|
|
131
|
-
reject: () => {
|
|
132
|
-
},
|
|
133
|
-
actorId: this.actorId,
|
|
134
|
-
gatewayId,
|
|
135
|
-
requestId,
|
|
136
|
-
clientMessageIndex
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
(_b = logger()) == null ? void 0 : _b.debug({
|
|
140
|
-
msg: "added pending request",
|
|
141
|
-
gatewayId: idToStr(gatewayId),
|
|
142
|
-
requestId: idToStr(requestId),
|
|
143
|
-
length: this.pendingRequests.length
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
createPendingRequestWithStreamController(gatewayId, requestId, clientMessageIndex, streamController) {
|
|
147
|
-
var _a, _b;
|
|
148
|
-
const exists = this.getPendingRequest(gatewayId, requestId) !== void 0;
|
|
149
|
-
if (exists) {
|
|
150
|
-
(_a = logger()) == null ? void 0 : _a.warn({
|
|
151
|
-
msg: "attempting to set pending request twice, replacing existing",
|
|
152
|
-
gatewayId: idToStr(gatewayId),
|
|
153
|
-
requestId: idToStr(requestId)
|
|
154
|
-
});
|
|
155
|
-
this.deletePendingRequest(gatewayId, requestId);
|
|
156
|
-
}
|
|
157
|
-
this.pendingRequests.push({
|
|
158
|
-
gatewayId,
|
|
159
|
-
requestId,
|
|
160
|
-
request: {
|
|
161
|
-
resolve: () => {
|
|
162
|
-
},
|
|
163
|
-
reject: () => {
|
|
164
|
-
},
|
|
165
|
-
actorId: this.actorId,
|
|
166
|
-
gatewayId,
|
|
167
|
-
requestId,
|
|
168
|
-
clientMessageIndex,
|
|
169
|
-
streamController
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
(_b = logger()) == null ? void 0 : _b.debug({
|
|
173
|
-
msg: "added pending request with stream controller",
|
|
174
|
-
gatewayId: idToStr(gatewayId),
|
|
175
|
-
requestId: idToStr(requestId),
|
|
176
|
-
length: this.pendingRequests.length
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
deletePendingRequest(gatewayId, requestId) {
|
|
180
|
-
var _a;
|
|
181
|
-
const index = this.pendingRequests.findIndex(
|
|
182
|
-
(entry) => arraysEqual(entry.gatewayId, gatewayId) && arraysEqual(entry.requestId, requestId)
|
|
183
|
-
);
|
|
184
|
-
if (index !== -1) {
|
|
185
|
-
this.pendingRequests.splice(index, 1);
|
|
186
|
-
(_a = logger()) == null ? void 0 : _a.debug({
|
|
187
|
-
msg: "removed pending request",
|
|
188
|
-
gatewayId: idToStr(gatewayId),
|
|
189
|
-
requestId: idToStr(requestId),
|
|
190
|
-
length: this.pendingRequests.length
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
// WebSocket methods
|
|
195
|
-
getWebSocket(gatewayId, requestId) {
|
|
196
|
-
var _a;
|
|
197
|
-
return (_a = this.webSockets.find(
|
|
198
|
-
(entry) => arraysEqual(entry.gatewayId, gatewayId) && arraysEqual(entry.requestId, requestId)
|
|
199
|
-
)) == null ? void 0 : _a.ws;
|
|
200
|
-
}
|
|
201
|
-
setWebSocket(gatewayId, requestId, ws) {
|
|
202
|
-
var _a;
|
|
203
|
-
const exists = this.getWebSocket(gatewayId, requestId) !== void 0;
|
|
204
|
-
if (exists) {
|
|
205
|
-
(_a = logger()) == null ? void 0 : _a.warn({ msg: "attempting to set websocket twice" });
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
this.webSockets.push({ gatewayId, requestId, ws });
|
|
209
|
-
}
|
|
210
|
-
deleteWebSocket(gatewayId, requestId) {
|
|
211
|
-
const index = this.webSockets.findIndex(
|
|
212
|
-
(entry) => arraysEqual(entry.gatewayId, gatewayId) && arraysEqual(entry.requestId, requestId)
|
|
213
|
-
);
|
|
214
|
-
if (index !== -1) {
|
|
215
|
-
this.webSockets.splice(index, 1);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
// src/stringify.ts
|
|
221
|
-
function stringifyArrayBuffer(buffer) {
|
|
222
|
-
return `ArrayBuffer(${buffer.byteLength})`;
|
|
223
|
-
}
|
|
224
|
-
function stringifyBigInt(value) {
|
|
225
|
-
return `${value}n`;
|
|
226
|
-
}
|
|
227
|
-
function stringifyMap(map) {
|
|
228
|
-
const entries = Array.from(map.entries()).map(([k, v]) => `"${k}": "${v}"`).join(", ");
|
|
229
|
-
return `Map(${map.size}){${entries}}`;
|
|
230
|
-
}
|
|
231
|
-
function stringifyMessageId(messageId) {
|
|
232
|
-
return `MessageId{gatewayId: ${idToStr(messageId.gatewayId)}, requestId: ${idToStr(messageId.requestId)}, messageIndex: ${messageId.messageIndex}}`;
|
|
233
|
-
}
|
|
234
|
-
function stringifyToServerTunnelMessageKind(kind) {
|
|
235
|
-
switch (kind.tag) {
|
|
236
|
-
case "DeprecatedTunnelAck":
|
|
237
|
-
return "DeprecatedTunnelAck";
|
|
238
|
-
case "ToServerResponseStart": {
|
|
239
|
-
const { status, headers, body, stream } = kind.val;
|
|
240
|
-
const bodyStr = body === null ? "null" : stringifyArrayBuffer(body);
|
|
241
|
-
return `ToServerResponseStart{status: ${status}, headers: ${stringifyMap(headers)}, body: ${bodyStr}, stream: ${stream}}`;
|
|
242
|
-
}
|
|
243
|
-
case "ToServerResponseChunk": {
|
|
244
|
-
const { body, finish } = kind.val;
|
|
245
|
-
return `ToServerResponseChunk{body: ${stringifyArrayBuffer(body)}, finish: ${finish}}`;
|
|
246
|
-
}
|
|
247
|
-
case "ToServerResponseAbort":
|
|
248
|
-
return "ToServerResponseAbort";
|
|
249
|
-
case "ToServerWebSocketOpen": {
|
|
250
|
-
const { canHibernate } = kind.val;
|
|
251
|
-
return `ToServerWebSocketOpen{canHibernate: ${canHibernate}}`;
|
|
252
|
-
}
|
|
253
|
-
case "ToServerWebSocketMessage": {
|
|
254
|
-
const { data, binary } = kind.val;
|
|
255
|
-
return `ToServerWebSocketMessage{data: ${stringifyArrayBuffer(data)}, binary: ${binary}}`;
|
|
256
|
-
}
|
|
257
|
-
case "ToServerWebSocketMessageAck": {
|
|
258
|
-
const { index } = kind.val;
|
|
259
|
-
return `ToServerWebSocketMessageAck{index: ${index}}`;
|
|
260
|
-
}
|
|
261
|
-
case "ToServerWebSocketClose": {
|
|
262
|
-
const { code, reason, hibernate } = kind.val;
|
|
263
|
-
const codeStr = code === null ? "null" : code.toString();
|
|
264
|
-
const reasonStr = reason === null ? "null" : `"${reason}"`;
|
|
265
|
-
return `ToServerWebSocketClose{code: ${codeStr}, reason: ${reasonStr}, hibernate: ${hibernate}}`;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
function stringifyToClientTunnelMessageKind(kind) {
|
|
270
|
-
switch (kind.tag) {
|
|
271
|
-
case "DeprecatedTunnelAck":
|
|
272
|
-
return "DeprecatedTunnelAck";
|
|
273
|
-
case "ToClientRequestStart": {
|
|
274
|
-
const { actorId, method, path, headers, body, stream } = kind.val;
|
|
275
|
-
const bodyStr = body === null ? "null" : stringifyArrayBuffer(body);
|
|
276
|
-
return `ToClientRequestStart{actorId: "${actorId}", method: "${method}", path: "${path}", headers: ${stringifyMap(headers)}, body: ${bodyStr}, stream: ${stream}}`;
|
|
277
|
-
}
|
|
278
|
-
case "ToClientRequestChunk": {
|
|
279
|
-
const { body, finish } = kind.val;
|
|
280
|
-
return `ToClientRequestChunk{body: ${stringifyArrayBuffer(body)}, finish: ${finish}}`;
|
|
281
|
-
}
|
|
282
|
-
case "ToClientRequestAbort":
|
|
283
|
-
return "ToClientRequestAbort";
|
|
284
|
-
case "ToClientWebSocketOpen": {
|
|
285
|
-
const { actorId, path, headers } = kind.val;
|
|
286
|
-
return `ToClientWebSocketOpen{actorId: "${actorId}", path: "${path}", headers: ${stringifyMap(headers)}}`;
|
|
287
|
-
}
|
|
288
|
-
case "ToClientWebSocketMessage": {
|
|
289
|
-
const { data, binary } = kind.val;
|
|
290
|
-
return `ToClientWebSocketMessage{data: ${stringifyArrayBuffer(data)}, binary: ${binary}}`;
|
|
291
|
-
}
|
|
292
|
-
case "ToClientWebSocketClose": {
|
|
293
|
-
const { code, reason } = kind.val;
|
|
294
|
-
const codeStr = code === null ? "null" : code.toString();
|
|
295
|
-
const reasonStr = reason === null ? "null" : `"${reason}"`;
|
|
296
|
-
return `ToClientWebSocketClose{code: ${codeStr}, reason: ${reasonStr}}`;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
function stringifyCommand(command) {
|
|
301
|
-
switch (command.tag) {
|
|
302
|
-
case "CommandStartActor": {
|
|
303
|
-
const { actorId, generation, config, hibernatingRequests } = command.val;
|
|
304
|
-
const keyStr = config.key === null ? "null" : `"${config.key}"`;
|
|
305
|
-
const inputStr = config.input === null ? "null" : stringifyArrayBuffer(config.input);
|
|
306
|
-
const hibernatingRequestsStr = hibernatingRequests.length > 0 ? `[${hibernatingRequests.map((hr) => `{gatewayId: ${idToStr(hr.gatewayId)}, requestId: ${idToStr(hr.requestId)}}`).join(", ")}]` : "[]";
|
|
307
|
-
return `CommandStartActor{actorId: "${actorId}", generation: ${generation}, config: {name: "${config.name}", key: ${keyStr}, createTs: ${stringifyBigInt(config.createTs)}, input: ${inputStr}}, hibernatingRequests: ${hibernatingRequestsStr}}`;
|
|
308
|
-
}
|
|
309
|
-
case "CommandStopActor": {
|
|
310
|
-
const { actorId, generation } = command.val;
|
|
311
|
-
return `CommandStopActor{actorId: "${actorId}", generation: ${generation}}`;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
function stringifyCommandWrapper(wrapper) {
|
|
316
|
-
return `CommandWrapper{index: ${stringifyBigInt(wrapper.index)}, inner: ${stringifyCommand(wrapper.inner)}}`;
|
|
317
|
-
}
|
|
318
|
-
function stringifyEvent(event) {
|
|
319
|
-
switch (event.tag) {
|
|
320
|
-
case "EventActorIntent": {
|
|
321
|
-
const { actorId, generation, intent } = event.val;
|
|
322
|
-
const intentStr = intent.tag === "ActorIntentSleep" ? "Sleep" : intent.tag === "ActorIntentStop" ? "Stop" : "Unknown";
|
|
323
|
-
return `EventActorIntent{actorId: "${actorId}", generation: ${generation}, intent: ${intentStr}}`;
|
|
324
|
-
}
|
|
325
|
-
case "EventActorStateUpdate": {
|
|
326
|
-
const { actorId, generation, state } = event.val;
|
|
327
|
-
let stateStr;
|
|
328
|
-
if (state.tag === "ActorStateRunning") {
|
|
329
|
-
stateStr = "Running";
|
|
330
|
-
} else if (state.tag === "ActorStateStopped") {
|
|
331
|
-
const { code, message } = state.val;
|
|
332
|
-
const messageStr = message === null ? "null" : `"${message}"`;
|
|
333
|
-
stateStr = `Stopped{code: ${code}, message: ${messageStr}}`;
|
|
334
|
-
} else {
|
|
335
|
-
stateStr = "Unknown";
|
|
336
|
-
}
|
|
337
|
-
return `EventActorStateUpdate{actorId: "${actorId}", generation: ${generation}, state: ${stateStr}}`;
|
|
338
|
-
}
|
|
339
|
-
case "EventActorSetAlarm": {
|
|
340
|
-
const { actorId, generation, alarmTs } = event.val;
|
|
341
|
-
const alarmTsStr = alarmTs === null ? "null" : stringifyBigInt(alarmTs);
|
|
342
|
-
return `EventActorSetAlarm{actorId: "${actorId}", generation: ${generation}, alarmTs: ${alarmTsStr}}`;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
function stringifyEventWrapper(wrapper) {
|
|
347
|
-
return `EventWrapper{index: ${stringifyBigInt(wrapper.index)}, inner: ${stringifyEvent(wrapper.inner)}}`;
|
|
348
|
-
}
|
|
349
|
-
function stringifyToServer(message) {
|
|
350
|
-
switch (message.tag) {
|
|
351
|
-
case "ToServerInit": {
|
|
352
|
-
const {
|
|
353
|
-
name,
|
|
354
|
-
version,
|
|
355
|
-
totalSlots,
|
|
356
|
-
lastCommandIdx,
|
|
357
|
-
prepopulateActorNames,
|
|
358
|
-
metadata
|
|
359
|
-
} = message.val;
|
|
360
|
-
const lastCommandIdxStr = lastCommandIdx === null ? "null" : stringifyBigInt(lastCommandIdx);
|
|
361
|
-
const prepopulateActorNamesStr = prepopulateActorNames === null ? "null" : `Map(${prepopulateActorNames.size})`;
|
|
362
|
-
const metadataStr = metadata === null ? "null" : `"${metadata}"`;
|
|
363
|
-
return `ToServerInit{name: "${name}", version: ${version}, totalSlots: ${totalSlots}, lastCommandIdx: ${lastCommandIdxStr}, prepopulateActorNames: ${prepopulateActorNamesStr}, metadata: ${metadataStr}}`;
|
|
364
|
-
}
|
|
365
|
-
case "ToServerEvents": {
|
|
366
|
-
const events = message.val;
|
|
367
|
-
return `ToServerEvents{count: ${events.length}, events: [${events.map((e) => stringifyEventWrapper(e)).join(", ")}]}`;
|
|
368
|
-
}
|
|
369
|
-
case "ToServerAckCommands": {
|
|
370
|
-
const { lastCommandIdx } = message.val;
|
|
371
|
-
return `ToServerAckCommands{lastCommandIdx: ${stringifyBigInt(lastCommandIdx)}}`;
|
|
372
|
-
}
|
|
373
|
-
case "ToServerStopping":
|
|
374
|
-
return "ToServerStopping";
|
|
375
|
-
case "ToServerPing": {
|
|
376
|
-
const { ts } = message.val;
|
|
377
|
-
return `ToServerPing{ts: ${stringifyBigInt(ts)}}`;
|
|
378
|
-
}
|
|
379
|
-
case "ToServerKvRequest": {
|
|
380
|
-
const { actorId, requestId, data } = message.val;
|
|
381
|
-
const dataStr = stringifyKvRequestData(data);
|
|
382
|
-
return `ToServerKvRequest{actorId: "${actorId}", requestId: ${requestId}, data: ${dataStr}}`;
|
|
383
|
-
}
|
|
384
|
-
case "ToServerTunnelMessage": {
|
|
385
|
-
const { messageId, messageKind } = message.val;
|
|
386
|
-
return `ToServerTunnelMessage{messageId: ${stringifyMessageId(messageId)}, messageKind: ${stringifyToServerTunnelMessageKind(messageKind)}}`;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
function stringifyToClient(message) {
|
|
391
|
-
switch (message.tag) {
|
|
392
|
-
case "ToClientInit": {
|
|
393
|
-
const { runnerId, lastEventIdx, metadata } = message.val;
|
|
394
|
-
const metadataStr = `{runnerLostThreshold: ${stringifyBigInt(metadata.runnerLostThreshold)}}`;
|
|
395
|
-
return `ToClientInit{runnerId: "${runnerId}", lastEventIdx: ${stringifyBigInt(lastEventIdx)}, metadata: ${metadataStr}}`;
|
|
396
|
-
}
|
|
397
|
-
case "ToClientClose":
|
|
398
|
-
return "ToClientClose";
|
|
399
|
-
case "ToClientCommands": {
|
|
400
|
-
const commands = message.val;
|
|
401
|
-
return `ToClientCommands{count: ${commands.length}, commands: [${commands.map((c) => stringifyCommandWrapper(c)).join(", ")}]}`;
|
|
402
|
-
}
|
|
403
|
-
case "ToClientAckEvents": {
|
|
404
|
-
const { lastEventIdx } = message.val;
|
|
405
|
-
return `ToClientAckEvents{lastEventIdx: ${stringifyBigInt(lastEventIdx)}}`;
|
|
406
|
-
}
|
|
407
|
-
case "ToClientKvResponse": {
|
|
408
|
-
const { requestId, data } = message.val;
|
|
409
|
-
const dataStr = stringifyKvResponseData(data);
|
|
410
|
-
return `ToClientKvResponse{requestId: ${requestId}, data: ${dataStr}}`;
|
|
411
|
-
}
|
|
412
|
-
case "ToClientTunnelMessage": {
|
|
413
|
-
const { messageId, messageKind } = message.val;
|
|
414
|
-
return `ToClientTunnelMessage{messageId: ${stringifyMessageId(messageId)}, messageKind: ${stringifyToClientTunnelMessageKind(messageKind)}}`;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
function stringifyKvRequestData(data) {
|
|
419
|
-
switch (data.tag) {
|
|
420
|
-
case "KvGetRequest": {
|
|
421
|
-
const { keys } = data.val;
|
|
422
|
-
return `KvGetRequest{keys: ${keys.length}}`;
|
|
423
|
-
}
|
|
424
|
-
case "KvListRequest": {
|
|
425
|
-
const { query, reverse, limit } = data.val;
|
|
426
|
-
const reverseStr = reverse === null ? "null" : reverse.toString();
|
|
427
|
-
const limitStr = limit === null ? "null" : stringifyBigInt(limit);
|
|
428
|
-
return `KvListRequest{query: ${stringifyKvListQuery(query)}, reverse: ${reverseStr}, limit: ${limitStr}}`;
|
|
429
|
-
}
|
|
430
|
-
case "KvPutRequest": {
|
|
431
|
-
const { keys, values } = data.val;
|
|
432
|
-
return `KvPutRequest{keys: ${keys.length}, values: ${values.length}}`;
|
|
433
|
-
}
|
|
434
|
-
case "KvDeleteRequest": {
|
|
435
|
-
const { keys } = data.val;
|
|
436
|
-
return `KvDeleteRequest{keys: ${keys.length}}`;
|
|
437
|
-
}
|
|
438
|
-
case "KvDropRequest":
|
|
439
|
-
return "KvDropRequest";
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
function stringifyKvListQuery(query) {
|
|
443
|
-
switch (query.tag) {
|
|
444
|
-
case "KvListAllQuery":
|
|
445
|
-
return "KvListAllQuery";
|
|
446
|
-
case "KvListRangeQuery": {
|
|
447
|
-
const { start, end, exclusive } = query.val;
|
|
448
|
-
return `KvListRangeQuery{start: ${stringifyArrayBuffer(start)}, end: ${stringifyArrayBuffer(end)}, exclusive: ${exclusive}}`;
|
|
449
|
-
}
|
|
450
|
-
case "KvListPrefixQuery": {
|
|
451
|
-
const { key } = query.val;
|
|
452
|
-
return `KvListPrefixQuery{key: ${stringifyArrayBuffer(key)}}`;
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
function stringifyKvResponseData(data) {
|
|
457
|
-
switch (data.tag) {
|
|
458
|
-
case "KvErrorResponse": {
|
|
459
|
-
const { message } = data.val;
|
|
460
|
-
return `KvErrorResponse{message: "${message}"}`;
|
|
461
|
-
}
|
|
462
|
-
case "KvGetResponse": {
|
|
463
|
-
const { keys, values, metadata } = data.val;
|
|
464
|
-
return `KvGetResponse{keys: ${keys.length}, values: ${values.length}, metadata: ${metadata.length}}`;
|
|
465
|
-
}
|
|
466
|
-
case "KvListResponse": {
|
|
467
|
-
const { keys, values, metadata } = data.val;
|
|
468
|
-
return `KvListResponse{keys: ${keys.length}, values: ${values.length}, metadata: ${metadata.length}}`;
|
|
469
|
-
}
|
|
470
|
-
case "KvPutResponse":
|
|
471
|
-
return "KvPutResponse";
|
|
472
|
-
case "KvDeleteResponse":
|
|
473
|
-
return "KvDeleteResponse";
|
|
474
|
-
case "KvDropResponse":
|
|
475
|
-
return "KvDropResponse";
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// src/websocket-tunnel-adapter.ts
|
|
480
|
-
var HIBERNATABLE_SYMBOL = Symbol("hibernatable");
|
|
481
|
-
var WebSocketTunnelAdapter = class {
|
|
482
|
-
constructor(tunnel, actorId, requestId, serverMessageIndex, hibernatable, isRestoringHibernatable, request, sendCallback, closeCallback) {
|
|
483
|
-
this.request = request;
|
|
484
|
-
var _a;
|
|
485
|
-
this.#tunnel = tunnel;
|
|
486
|
-
this.#actorId = actorId;
|
|
487
|
-
this.#requestId = requestId;
|
|
488
|
-
this.#hibernatable = hibernatable;
|
|
489
|
-
this.#serverMessageIndex = serverMessageIndex;
|
|
490
|
-
this.#sendCallback = sendCallback;
|
|
491
|
-
this.#closeCallback = closeCallback;
|
|
492
|
-
if (isRestoringHibernatable) {
|
|
493
|
-
(_a = this.#log) == null ? void 0 : _a.debug({
|
|
494
|
-
msg: "setting WebSocket to OPEN state for restored connection",
|
|
495
|
-
actorId: this.#actorId,
|
|
496
|
-
requestId: this.#requestId,
|
|
497
|
-
hibernatable: this.#hibernatable
|
|
498
|
-
});
|
|
499
|
-
this.#readyState = 1;
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
// MARK: - WebSocket Compat Variables
|
|
503
|
-
#readyState = 0;
|
|
504
|
-
// CONNECTING
|
|
505
|
-
#eventListeners = /* @__PURE__ */ new Map();
|
|
506
|
-
#onopen = null;
|
|
507
|
-
#onclose = null;
|
|
508
|
-
#onerror = null;
|
|
509
|
-
#onmessage = null;
|
|
510
|
-
#bufferedAmount = 0;
|
|
511
|
-
#binaryType = "nodebuffer";
|
|
512
|
-
#extensions = "";
|
|
513
|
-
#protocol = "";
|
|
514
|
-
#url = "";
|
|
515
|
-
// mARK: - Internal State
|
|
516
|
-
#tunnel;
|
|
517
|
-
#actorId;
|
|
518
|
-
#requestId;
|
|
519
|
-
#hibernatable;
|
|
520
|
-
#serverMessageIndex;
|
|
521
|
-
get [HIBERNATABLE_SYMBOL]() {
|
|
522
|
-
return this.#hibernatable;
|
|
523
|
-
}
|
|
524
|
-
/**
|
|
525
|
-
* Called when sending a message from this WebSocket.
|
|
526
|
-
*
|
|
527
|
-
* Used to send a tunnel message from Tunnel.
|
|
528
|
-
*/
|
|
529
|
-
#sendCallback;
|
|
530
|
-
/**
|
|
531
|
-
* Called when closing this WebSocket.
|
|
532
|
-
*
|
|
533
|
-
* Used to send a tunnel message from Tunnel
|
|
534
|
-
*/
|
|
535
|
-
#closeCallback;
|
|
536
|
-
get #log() {
|
|
537
|
-
return this.#tunnel.log;
|
|
538
|
-
}
|
|
539
|
-
// MARK: - Lifecycle
|
|
540
|
-
get bufferedAmount() {
|
|
541
|
-
return this.#bufferedAmount;
|
|
542
|
-
}
|
|
543
|
-
_handleOpen(requestId) {
|
|
544
|
-
if (this.#readyState !== 0) {
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
this.#readyState = 1;
|
|
548
|
-
const event = {
|
|
549
|
-
type: "open",
|
|
550
|
-
rivetRequestId: requestId,
|
|
551
|
-
target: this
|
|
552
|
-
};
|
|
553
|
-
this.#fireEvent("open", event);
|
|
554
|
-
}
|
|
555
|
-
_handleMessage(requestId, data, serverMessageIndex, isBinary) {
|
|
556
|
-
var _a, _b, _c;
|
|
557
|
-
if (this.#readyState !== 1) {
|
|
558
|
-
(_a = this.#log) == null ? void 0 : _a.warn({
|
|
559
|
-
msg: "WebSocket message ignored - not in OPEN state",
|
|
560
|
-
requestId: this.#requestId,
|
|
561
|
-
actorId: this.#actorId,
|
|
562
|
-
currentReadyState: this.#readyState,
|
|
563
|
-
expectedReadyState: 1,
|
|
564
|
-
serverMessageIndex,
|
|
565
|
-
hibernatable: this.#hibernatable
|
|
566
|
-
});
|
|
567
|
-
return true;
|
|
568
|
-
}
|
|
569
|
-
if (this.#hibernatable) {
|
|
570
|
-
const previousIndex = this.#serverMessageIndex;
|
|
571
|
-
if (wrappingLteU16(serverMessageIndex, previousIndex)) {
|
|
572
|
-
(_b = this.#log) == null ? void 0 : _b.info({
|
|
573
|
-
msg: "received duplicate hibernating websocket message, this indicates the actor failed to ack the message index before restarting",
|
|
574
|
-
requestId,
|
|
575
|
-
actorId: this.#actorId,
|
|
576
|
-
previousIndex,
|
|
577
|
-
expectedIndex: wrappingAddU16(previousIndex, 1),
|
|
578
|
-
receivedIndex: serverMessageIndex
|
|
579
|
-
});
|
|
580
|
-
return true;
|
|
581
|
-
}
|
|
582
|
-
const expectedIndex = wrappingAddU16(previousIndex, 1);
|
|
583
|
-
if (serverMessageIndex !== expectedIndex) {
|
|
584
|
-
const closeReason = "ws.message_index_skip";
|
|
585
|
-
(_c = this.#log) == null ? void 0 : _c.warn({
|
|
586
|
-
msg: "hibernatable websocket message index out of sequence, closing connection",
|
|
587
|
-
requestId,
|
|
588
|
-
actorId: this.#actorId,
|
|
589
|
-
previousIndex,
|
|
590
|
-
expectedIndex,
|
|
591
|
-
receivedIndex: serverMessageIndex,
|
|
592
|
-
closeReason,
|
|
593
|
-
gap: wrappingSubU16(
|
|
594
|
-
wrappingSubU16(serverMessageIndex, previousIndex),
|
|
595
|
-
1
|
|
596
|
-
)
|
|
597
|
-
});
|
|
598
|
-
this.close(1008, closeReason);
|
|
599
|
-
return true;
|
|
600
|
-
}
|
|
601
|
-
this.#serverMessageIndex = serverMessageIndex;
|
|
602
|
-
}
|
|
603
|
-
let messageData;
|
|
604
|
-
if (isBinary) {
|
|
605
|
-
if (this.#binaryType === "nodebuffer") {
|
|
606
|
-
messageData = Buffer.from(data);
|
|
607
|
-
} else if (this.#binaryType === "arraybuffer") {
|
|
608
|
-
if (data instanceof Uint8Array) {
|
|
609
|
-
messageData = data.buffer.slice(
|
|
610
|
-
data.byteOffset,
|
|
611
|
-
data.byteOffset + data.byteLength
|
|
612
|
-
);
|
|
613
|
-
} else {
|
|
614
|
-
messageData = data;
|
|
615
|
-
}
|
|
616
|
-
} else {
|
|
617
|
-
throw new Error(
|
|
618
|
-
"Blob binaryType not supported in tunnel adapter"
|
|
619
|
-
);
|
|
620
|
-
}
|
|
621
|
-
} else {
|
|
622
|
-
messageData = data;
|
|
623
|
-
}
|
|
624
|
-
const event = {
|
|
625
|
-
type: "message",
|
|
626
|
-
data: messageData,
|
|
627
|
-
rivetRequestId: requestId,
|
|
628
|
-
rivetMessageIndex: serverMessageIndex,
|
|
629
|
-
target: this
|
|
630
|
-
};
|
|
631
|
-
this.#fireEvent("message", event);
|
|
632
|
-
return false;
|
|
633
|
-
}
|
|
634
|
-
_handleClose(_requestId, code, reason) {
|
|
635
|
-
this.#closeInner(code, reason, true);
|
|
636
|
-
}
|
|
637
|
-
_handleError(error) {
|
|
638
|
-
const event = {
|
|
639
|
-
type: "error",
|
|
640
|
-
target: this,
|
|
641
|
-
error
|
|
642
|
-
};
|
|
643
|
-
this.#fireEvent("error", event);
|
|
644
|
-
}
|
|
645
|
-
_closeWithoutCallback(code, reason) {
|
|
646
|
-
this.#closeInner(code, reason, false);
|
|
647
|
-
}
|
|
648
|
-
#fireEvent(type, event) {
|
|
649
|
-
var _a, _b, _c, _d, _e;
|
|
650
|
-
const listeners = this.#eventListeners.get(type);
|
|
651
|
-
if (listeners && listeners.size > 0) {
|
|
652
|
-
for (const listener of listeners) {
|
|
653
|
-
try {
|
|
654
|
-
listener.call(this, event);
|
|
655
|
-
} catch (error) {
|
|
656
|
-
(_a = logger()) == null ? void 0 : _a.error({
|
|
657
|
-
msg: "error in websocket event listener",
|
|
658
|
-
error,
|
|
659
|
-
type
|
|
660
|
-
});
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
switch (type) {
|
|
665
|
-
case "open":
|
|
666
|
-
if (this.#onopen) {
|
|
667
|
-
try {
|
|
668
|
-
this.#onopen.call(this, event);
|
|
669
|
-
} catch (error) {
|
|
670
|
-
(_b = logger()) == null ? void 0 : _b.error({
|
|
671
|
-
msg: "error in onopen handler",
|
|
672
|
-
error
|
|
673
|
-
});
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
break;
|
|
677
|
-
case "close":
|
|
678
|
-
if (this.#onclose) {
|
|
679
|
-
try {
|
|
680
|
-
this.#onclose.call(this, event);
|
|
681
|
-
} catch (error) {
|
|
682
|
-
(_c = logger()) == null ? void 0 : _c.error({
|
|
683
|
-
msg: "error in onclose handler",
|
|
684
|
-
error
|
|
685
|
-
});
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
break;
|
|
689
|
-
case "error":
|
|
690
|
-
if (this.#onerror) {
|
|
691
|
-
try {
|
|
692
|
-
this.#onerror.call(this, event);
|
|
693
|
-
} catch (error) {
|
|
694
|
-
(_d = logger()) == null ? void 0 : _d.error({
|
|
695
|
-
msg: "error in onerror handler",
|
|
696
|
-
error
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
break;
|
|
701
|
-
case "message":
|
|
702
|
-
if (this.#onmessage) {
|
|
703
|
-
try {
|
|
704
|
-
this.#onmessage.call(this, event);
|
|
705
|
-
} catch (error) {
|
|
706
|
-
(_e = logger()) == null ? void 0 : _e.error({
|
|
707
|
-
msg: "error in onmessage handler",
|
|
708
|
-
error
|
|
709
|
-
});
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
break;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
#closeInner(code, reason, callback) {
|
|
716
|
-
if (this.#readyState === 2 || // CLOSING
|
|
717
|
-
this.#readyState === 3) {
|
|
718
|
-
return;
|
|
719
|
-
}
|
|
720
|
-
this.#readyState = 2;
|
|
721
|
-
if (callback) {
|
|
722
|
-
this.#closeCallback(code, reason);
|
|
723
|
-
}
|
|
724
|
-
this.#readyState = 3;
|
|
725
|
-
const closeEvent = {
|
|
726
|
-
wasClean: true,
|
|
727
|
-
code: code || 1e3,
|
|
728
|
-
reason: reason || "",
|
|
729
|
-
type: "close",
|
|
730
|
-
target: this
|
|
731
|
-
};
|
|
732
|
-
this.#fireEvent("close", closeEvent);
|
|
733
|
-
}
|
|
734
|
-
// MARK: - WebSocket Compatible API
|
|
735
|
-
get readyState() {
|
|
736
|
-
return this.#readyState;
|
|
737
|
-
}
|
|
738
|
-
get binaryType() {
|
|
739
|
-
return this.#binaryType;
|
|
740
|
-
}
|
|
741
|
-
set binaryType(value) {
|
|
742
|
-
if (value === "nodebuffer" || value === "arraybuffer" || value === "blob") {
|
|
743
|
-
this.#binaryType = value;
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
get extensions() {
|
|
747
|
-
return this.#extensions;
|
|
748
|
-
}
|
|
749
|
-
get protocol() {
|
|
750
|
-
return this.#protocol;
|
|
751
|
-
}
|
|
752
|
-
get url() {
|
|
753
|
-
return this.#url;
|
|
754
|
-
}
|
|
755
|
-
get onopen() {
|
|
756
|
-
return this.#onopen;
|
|
757
|
-
}
|
|
758
|
-
set onopen(value) {
|
|
759
|
-
this.#onopen = value;
|
|
760
|
-
}
|
|
761
|
-
get onclose() {
|
|
762
|
-
return this.#onclose;
|
|
763
|
-
}
|
|
764
|
-
set onclose(value) {
|
|
765
|
-
this.#onclose = value;
|
|
766
|
-
}
|
|
767
|
-
get onerror() {
|
|
768
|
-
return this.#onerror;
|
|
769
|
-
}
|
|
770
|
-
set onerror(value) {
|
|
771
|
-
this.#onerror = value;
|
|
772
|
-
}
|
|
773
|
-
get onmessage() {
|
|
774
|
-
return this.#onmessage;
|
|
775
|
-
}
|
|
776
|
-
set onmessage(value) {
|
|
777
|
-
this.#onmessage = value;
|
|
778
|
-
}
|
|
779
|
-
send(data) {
|
|
780
|
-
if (this.#readyState === 0) {
|
|
781
|
-
throw new DOMException(
|
|
782
|
-
"WebSocket is still in CONNECTING state",
|
|
783
|
-
"InvalidStateError"
|
|
784
|
-
);
|
|
785
|
-
}
|
|
786
|
-
if (this.#readyState === 2 || this.#readyState === 3) {
|
|
787
|
-
return;
|
|
788
|
-
}
|
|
789
|
-
let isBinary = false;
|
|
790
|
-
let messageData;
|
|
791
|
-
if (typeof data === "string") {
|
|
792
|
-
messageData = data;
|
|
793
|
-
} else if (data instanceof ArrayBuffer) {
|
|
794
|
-
isBinary = true;
|
|
795
|
-
messageData = data;
|
|
796
|
-
} else if (ArrayBuffer.isView(data)) {
|
|
797
|
-
isBinary = true;
|
|
798
|
-
const view = data;
|
|
799
|
-
if (view.buffer instanceof SharedArrayBuffer) {
|
|
800
|
-
const bytes = new Uint8Array(
|
|
801
|
-
view.buffer,
|
|
802
|
-
view.byteOffset,
|
|
803
|
-
view.byteLength
|
|
804
|
-
);
|
|
805
|
-
messageData = bytes.buffer.slice(
|
|
806
|
-
bytes.byteOffset,
|
|
807
|
-
bytes.byteOffset + bytes.byteLength
|
|
808
|
-
);
|
|
809
|
-
} else {
|
|
810
|
-
messageData = view.buffer.slice(
|
|
811
|
-
view.byteOffset,
|
|
812
|
-
view.byteOffset + view.byteLength
|
|
813
|
-
);
|
|
814
|
-
}
|
|
815
|
-
} else if (data instanceof Blob) {
|
|
816
|
-
throw new Error("Blob sending not implemented in tunnel adapter");
|
|
817
|
-
} else if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
|
|
818
|
-
isBinary = true;
|
|
819
|
-
const buf = data;
|
|
820
|
-
if (buf.buffer instanceof SharedArrayBuffer) {
|
|
821
|
-
const bytes = new Uint8Array(
|
|
822
|
-
buf.buffer,
|
|
823
|
-
buf.byteOffset,
|
|
824
|
-
buf.byteLength
|
|
825
|
-
);
|
|
826
|
-
messageData = bytes.buffer.slice(
|
|
827
|
-
bytes.byteOffset,
|
|
828
|
-
bytes.byteOffset + bytes.byteLength
|
|
829
|
-
);
|
|
830
|
-
} else {
|
|
831
|
-
messageData = buf.buffer.slice(
|
|
832
|
-
buf.byteOffset,
|
|
833
|
-
buf.byteOffset + buf.byteLength
|
|
834
|
-
);
|
|
835
|
-
}
|
|
836
|
-
} else {
|
|
837
|
-
throw new Error("Invalid data type");
|
|
838
|
-
}
|
|
839
|
-
this.#sendCallback(messageData, isBinary);
|
|
840
|
-
}
|
|
841
|
-
close(code, reason) {
|
|
842
|
-
this.#closeInner(code, reason, true);
|
|
843
|
-
}
|
|
844
|
-
addEventListener(type, listener, options) {
|
|
845
|
-
if (typeof listener === "function") {
|
|
846
|
-
let listeners = this.#eventListeners.get(type);
|
|
847
|
-
if (!listeners) {
|
|
848
|
-
listeners = /* @__PURE__ */ new Set();
|
|
849
|
-
this.#eventListeners.set(type, listeners);
|
|
850
|
-
}
|
|
851
|
-
listeners.add(listener);
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
removeEventListener(type, listener, options) {
|
|
855
|
-
if (typeof listener === "function") {
|
|
856
|
-
const listeners = this.#eventListeners.get(type);
|
|
857
|
-
if (listeners) {
|
|
858
|
-
listeners.delete(listener);
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
dispatchEvent(event) {
|
|
863
|
-
return true;
|
|
864
|
-
}
|
|
865
|
-
static CONNECTING = 0;
|
|
866
|
-
static OPEN = 1;
|
|
867
|
-
static CLOSING = 2;
|
|
868
|
-
static CLOSED = 3;
|
|
869
|
-
CONNECTING = 0;
|
|
870
|
-
OPEN = 1;
|
|
871
|
-
CLOSING = 2;
|
|
872
|
-
CLOSED = 3;
|
|
873
|
-
// Additional methods for compatibility
|
|
874
|
-
ping(data, mask, cb) {
|
|
875
|
-
if (cb) cb(new Error("Ping not supported in tunnel adapter"));
|
|
876
|
-
}
|
|
877
|
-
pong(data, mask, cb) {
|
|
878
|
-
if (cb) cb(new Error("Pong not supported in tunnel adapter"));
|
|
879
|
-
}
|
|
880
|
-
/** @experimental */
|
|
881
|
-
terminate() {
|
|
882
|
-
this.#readyState = 3;
|
|
883
|
-
this.#closeCallback(1006, "Abnormal Closure");
|
|
884
|
-
const event = {
|
|
885
|
-
wasClean: false,
|
|
886
|
-
code: 1006,
|
|
887
|
-
reason: "Abnormal Closure",
|
|
888
|
-
type: "close",
|
|
889
|
-
target: this
|
|
890
|
-
};
|
|
891
|
-
this.#fireEvent("close", event);
|
|
892
|
-
}
|
|
893
|
-
};
|
|
894
|
-
|
|
895
|
-
// src/tunnel.ts
|
|
896
|
-
var RunnerShutdownError = class extends Error {
|
|
897
|
-
constructor() {
|
|
898
|
-
super("Runner shut down");
|
|
899
|
-
}
|
|
900
|
-
};
|
|
901
|
-
var Tunnel = class {
|
|
902
|
-
#runner;
|
|
903
|
-
/** Maps request IDs to actor IDs for lookup */
|
|
904
|
-
#requestToActor = [];
|
|
905
|
-
/** Buffer for messages when not connected */
|
|
906
|
-
#bufferedMessages = [];
|
|
907
|
-
get log() {
|
|
908
|
-
return this.#runner.log;
|
|
909
|
-
}
|
|
910
|
-
constructor(runner) {
|
|
911
|
-
this.#runner = runner;
|
|
912
|
-
}
|
|
913
|
-
start() {
|
|
914
|
-
}
|
|
915
|
-
resendBufferedEvents() {
|
|
916
|
-
var _a;
|
|
917
|
-
if (this.#bufferedMessages.length === 0) {
|
|
918
|
-
return;
|
|
919
|
-
}
|
|
920
|
-
(_a = this.log) == null ? void 0 : _a.info({
|
|
921
|
-
msg: "resending buffered tunnel messages",
|
|
922
|
-
count: this.#bufferedMessages.length
|
|
923
|
-
});
|
|
924
|
-
const messages = this.#bufferedMessages;
|
|
925
|
-
this.#bufferedMessages = [];
|
|
926
|
-
for (const { gatewayId, requestId, messageKind } of messages) {
|
|
927
|
-
this.#sendMessage(gatewayId, requestId, messageKind);
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
shutdown() {
|
|
931
|
-
for (const [_actorId, actor] of this.#runner.actors) {
|
|
932
|
-
for (const entry of actor.pendingRequests) {
|
|
933
|
-
entry.request.reject(new RunnerShutdownError());
|
|
934
|
-
}
|
|
935
|
-
actor.pendingRequests = [];
|
|
936
|
-
for (const entry of actor.webSockets) {
|
|
937
|
-
if (!entry.ws[HIBERNATABLE_SYMBOL]) {
|
|
938
|
-
entry.ws._closeWithoutCallback(1e3, "ws.tunnel_shutdown");
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
actor.webSockets = [];
|
|
942
|
-
}
|
|
943
|
-
this.#requestToActor = [];
|
|
944
|
-
}
|
|
945
|
-
async restoreHibernatingRequests(actorId, metaEntries) {
|
|
946
|
-
var _a, _b, _c, _d;
|
|
947
|
-
const actor = this.#runner.getActor(actorId);
|
|
948
|
-
if (!actor) {
|
|
949
|
-
throw new Error(
|
|
950
|
-
`Actor ${actorId} not found for restoring hibernating requests`
|
|
951
|
-
);
|
|
952
|
-
}
|
|
953
|
-
if (actor.hibernationRestored) {
|
|
954
|
-
throw new Error(
|
|
955
|
-
`Actor ${actorId} already restored hibernating requests`
|
|
956
|
-
);
|
|
957
|
-
}
|
|
958
|
-
(_a = this.log) == null ? void 0 : _a.debug({
|
|
959
|
-
msg: "restoring hibernating requests",
|
|
960
|
-
actorId,
|
|
961
|
-
requests: actor.hibernatingRequests.length
|
|
962
|
-
});
|
|
963
|
-
const backgroundOperations = [];
|
|
964
|
-
let connectedButNotLoadedCount = 0;
|
|
965
|
-
let restoredCount = 0;
|
|
966
|
-
for (const { gatewayId, requestId } of actor.hibernatingRequests) {
|
|
967
|
-
const requestIdStr = idToStr(requestId);
|
|
968
|
-
const meta = metaEntries.find(
|
|
969
|
-
(entry) => arraysEqual(entry.gatewayId, gatewayId) && arraysEqual(entry.requestId, requestId)
|
|
970
|
-
);
|
|
971
|
-
if (!meta) {
|
|
972
|
-
(_b = this.log) == null ? void 0 : _b.warn({
|
|
973
|
-
msg: "closing websocket that is not persisted",
|
|
974
|
-
requestId: requestIdStr
|
|
975
|
-
});
|
|
976
|
-
this.#sendMessage(gatewayId, requestId, {
|
|
977
|
-
tag: "ToServerWebSocketClose",
|
|
978
|
-
val: {
|
|
979
|
-
code: 1e3,
|
|
980
|
-
reason: "ws.meta_not_found_during_restore",
|
|
981
|
-
hibernate: false
|
|
982
|
-
}
|
|
983
|
-
});
|
|
984
|
-
connectedButNotLoadedCount++;
|
|
985
|
-
} else {
|
|
986
|
-
const request = buildRequestForWebSocket(
|
|
987
|
-
meta.path,
|
|
988
|
-
meta.headers
|
|
989
|
-
);
|
|
990
|
-
const restoreOperation = this.#createWebSocket(
|
|
991
|
-
actorId,
|
|
992
|
-
gatewayId,
|
|
993
|
-
requestId,
|
|
994
|
-
requestIdStr,
|
|
995
|
-
meta.serverMessageIndex,
|
|
996
|
-
true,
|
|
997
|
-
true,
|
|
998
|
-
request,
|
|
999
|
-
meta.path,
|
|
1000
|
-
meta.headers,
|
|
1001
|
-
false
|
|
1002
|
-
).then(() => {
|
|
1003
|
-
var _a2;
|
|
1004
|
-
const actor2 = this.#runner.getActor(actorId);
|
|
1005
|
-
if (actor2) {
|
|
1006
|
-
actor2.createPendingRequest(
|
|
1007
|
-
gatewayId,
|
|
1008
|
-
requestId,
|
|
1009
|
-
meta.clientMessageIndex
|
|
1010
|
-
);
|
|
1011
|
-
}
|
|
1012
|
-
(_a2 = this.log) == null ? void 0 : _a2.info({
|
|
1013
|
-
msg: "connection successfully restored",
|
|
1014
|
-
actorId,
|
|
1015
|
-
requestId: requestIdStr
|
|
1016
|
-
});
|
|
1017
|
-
}).catch((err) => {
|
|
1018
|
-
var _a2;
|
|
1019
|
-
(_a2 = this.log) == null ? void 0 : _a2.error({
|
|
1020
|
-
msg: "error creating websocket during restore",
|
|
1021
|
-
requestId: requestIdStr,
|
|
1022
|
-
err
|
|
1023
|
-
});
|
|
1024
|
-
this.#sendMessage(gatewayId, requestId, {
|
|
1025
|
-
tag: "ToServerWebSocketClose",
|
|
1026
|
-
val: {
|
|
1027
|
-
code: 1011,
|
|
1028
|
-
reason: "ws.restore_error",
|
|
1029
|
-
hibernate: false
|
|
1030
|
-
}
|
|
1031
|
-
});
|
|
1032
|
-
});
|
|
1033
|
-
backgroundOperations.push(restoreOperation);
|
|
1034
|
-
restoredCount++;
|
|
1035
|
-
}
|
|
1036
|
-
}
|
|
1037
|
-
let loadedButNotConnectedCount = 0;
|
|
1038
|
-
for (const meta of metaEntries) {
|
|
1039
|
-
const requestIdStr = idToStr(meta.requestId);
|
|
1040
|
-
const isConnected = actor.hibernatingRequests.some(
|
|
1041
|
-
(req) => arraysEqual(req.gatewayId, meta.gatewayId) && arraysEqual(req.requestId, meta.requestId)
|
|
1042
|
-
);
|
|
1043
|
-
if (!isConnected) {
|
|
1044
|
-
(_c = this.log) == null ? void 0 : _c.warn({
|
|
1045
|
-
msg: "removing stale persisted websocket",
|
|
1046
|
-
requestId: requestIdStr
|
|
1047
|
-
});
|
|
1048
|
-
const request = buildRequestForWebSocket(
|
|
1049
|
-
meta.path,
|
|
1050
|
-
meta.headers
|
|
1051
|
-
);
|
|
1052
|
-
const cleanupOperation = this.#createWebSocket(
|
|
1053
|
-
actorId,
|
|
1054
|
-
meta.gatewayId,
|
|
1055
|
-
meta.requestId,
|
|
1056
|
-
requestIdStr,
|
|
1057
|
-
meta.serverMessageIndex,
|
|
1058
|
-
true,
|
|
1059
|
-
true,
|
|
1060
|
-
request,
|
|
1061
|
-
meta.path,
|
|
1062
|
-
meta.headers,
|
|
1063
|
-
true
|
|
1064
|
-
).then((adapter) => {
|
|
1065
|
-
adapter.close(1e3, "ws.stale_metadata");
|
|
1066
|
-
}).catch((err) => {
|
|
1067
|
-
var _a2;
|
|
1068
|
-
(_a2 = this.log) == null ? void 0 : _a2.error({
|
|
1069
|
-
msg: "error creating stale websocket during restore",
|
|
1070
|
-
requestId: requestIdStr,
|
|
1071
|
-
err
|
|
1072
|
-
});
|
|
1073
|
-
});
|
|
1074
|
-
backgroundOperations.push(cleanupOperation);
|
|
1075
|
-
loadedButNotConnectedCount++;
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
await Promise.allSettled(backgroundOperations);
|
|
1079
|
-
actor.hibernationRestored = true;
|
|
1080
|
-
(_d = this.log) == null ? void 0 : _d.info({
|
|
1081
|
-
msg: "restored hibernatable websockets",
|
|
1082
|
-
actorId,
|
|
1083
|
-
restoredCount,
|
|
1084
|
-
connectedButNotLoadedCount,
|
|
1085
|
-
loadedButNotConnectedCount
|
|
1086
|
-
});
|
|
1087
|
-
}
|
|
1088
|
-
/**
|
|
1089
|
-
* Called from WebSocketOpen message and when restoring hibernatable WebSockets.
|
|
1090
|
-
*
|
|
1091
|
-
* engineAlreadyClosed will be true if this is only being called to trigger
|
|
1092
|
-
* the close callback and not to send a close message to the server. This
|
|
1093
|
-
* is used specifically to clean up zombie WebSocket connections.
|
|
1094
|
-
*/
|
|
1095
|
-
async #createWebSocket(actorId, gatewayId, requestId, requestIdStr, serverMessageIndex, isHibernatable, isRestoringHibernatable, request, path, headers, engineAlreadyClosed) {
|
|
1096
|
-
var _a;
|
|
1097
|
-
(_a = this.log) == null ? void 0 : _a.debug({
|
|
1098
|
-
msg: "createWebSocket creating adapter",
|
|
1099
|
-
actorId,
|
|
1100
|
-
requestIdStr,
|
|
1101
|
-
isHibernatable,
|
|
1102
|
-
path
|
|
1103
|
-
});
|
|
1104
|
-
const adapter = new WebSocketTunnelAdapter(
|
|
1105
|
-
this,
|
|
1106
|
-
actorId,
|
|
1107
|
-
requestIdStr,
|
|
1108
|
-
serverMessageIndex,
|
|
1109
|
-
isHibernatable,
|
|
1110
|
-
isRestoringHibernatable,
|
|
1111
|
-
request,
|
|
1112
|
-
(data, isBinary) => {
|
|
1113
|
-
const dataBuffer = typeof data === "string" ? new TextEncoder().encode(data).buffer : data;
|
|
1114
|
-
this.#sendMessage(gatewayId, requestId, {
|
|
1115
|
-
tag: "ToServerWebSocketMessage",
|
|
1116
|
-
val: {
|
|
1117
|
-
data: dataBuffer,
|
|
1118
|
-
binary: isBinary
|
|
1119
|
-
}
|
|
1120
|
-
});
|
|
1121
|
-
},
|
|
1122
|
-
(code, reason) => {
|
|
1123
|
-
if (!engineAlreadyClosed) {
|
|
1124
|
-
this.#sendMessage(gatewayId, requestId, {
|
|
1125
|
-
tag: "ToServerWebSocketClose",
|
|
1126
|
-
val: {
|
|
1127
|
-
code: code || null,
|
|
1128
|
-
reason: reason || null,
|
|
1129
|
-
hibernate: false
|
|
1130
|
-
}
|
|
1131
|
-
});
|
|
1132
|
-
}
|
|
1133
|
-
const actor2 = this.#runner.getActor(actorId);
|
|
1134
|
-
if (actor2) {
|
|
1135
|
-
actor2.deleteWebSocket(gatewayId, requestId);
|
|
1136
|
-
actor2.deletePendingRequest(gatewayId, requestId);
|
|
1137
|
-
}
|
|
1138
|
-
this.#removeRequestToActor(gatewayId, requestId);
|
|
1139
|
-
}
|
|
1140
|
-
);
|
|
1141
|
-
const actor = this.#runner.getActor(actorId);
|
|
1142
|
-
if (!actor) {
|
|
1143
|
-
throw new Error(`Actor ${actorId} not found`);
|
|
1144
|
-
}
|
|
1145
|
-
actor.setWebSocket(gatewayId, requestId, adapter);
|
|
1146
|
-
this.addRequestToActor(gatewayId, requestId, actorId);
|
|
1147
|
-
await this.#runner.config.websocket(
|
|
1148
|
-
this.#runner,
|
|
1149
|
-
actorId,
|
|
1150
|
-
adapter,
|
|
1151
|
-
gatewayId,
|
|
1152
|
-
requestId,
|
|
1153
|
-
request,
|
|
1154
|
-
path,
|
|
1155
|
-
headers,
|
|
1156
|
-
isHibernatable,
|
|
1157
|
-
isRestoringHibernatable
|
|
1158
|
-
);
|
|
1159
|
-
return adapter;
|
|
1160
|
-
}
|
|
1161
|
-
addRequestToActor(gatewayId, requestId, actorId) {
|
|
1162
|
-
this.#requestToActor.push({ gatewayId, requestId, actorId });
|
|
1163
|
-
}
|
|
1164
|
-
#removeRequestToActor(gatewayId, requestId) {
|
|
1165
|
-
const index = this.#requestToActor.findIndex(
|
|
1166
|
-
(entry) => arraysEqual(entry.gatewayId, gatewayId) && arraysEqual(entry.requestId, requestId)
|
|
1167
|
-
);
|
|
1168
|
-
if (index !== -1) {
|
|
1169
|
-
this.#requestToActor.splice(index, 1);
|
|
1170
|
-
}
|
|
1171
|
-
}
|
|
1172
|
-
getRequestActor(gatewayId, requestId) {
|
|
1173
|
-
var _a, _b;
|
|
1174
|
-
const entry = this.#requestToActor.find(
|
|
1175
|
-
(entry2) => arraysEqual(entry2.gatewayId, gatewayId) && arraysEqual(entry2.requestId, requestId)
|
|
1176
|
-
);
|
|
1177
|
-
if (!entry) {
|
|
1178
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
1179
|
-
msg: "missing requestToActor entry",
|
|
1180
|
-
requestId: idToStr(requestId)
|
|
1181
|
-
});
|
|
1182
|
-
return void 0;
|
|
1183
|
-
}
|
|
1184
|
-
const actor = this.#runner.getActor(entry.actorId);
|
|
1185
|
-
if (!actor) {
|
|
1186
|
-
(_b = this.log) == null ? void 0 : _b.warn({
|
|
1187
|
-
msg: "missing actor for requestToActor lookup",
|
|
1188
|
-
requestId: idToStr(requestId),
|
|
1189
|
-
actorId: entry.actorId
|
|
1190
|
-
});
|
|
1191
|
-
return void 0;
|
|
1192
|
-
}
|
|
1193
|
-
return actor;
|
|
1194
|
-
}
|
|
1195
|
-
async getAndWaitForRequestActor(gatewayId, requestId) {
|
|
1196
|
-
const actor = this.getRequestActor(gatewayId, requestId);
|
|
1197
|
-
if (!actor) return;
|
|
1198
|
-
await actor.actorStartPromise.promise;
|
|
1199
|
-
return actor;
|
|
1200
|
-
}
|
|
1201
|
-
#sendMessage(gatewayId, requestId, messageKind) {
|
|
1202
|
-
var _a, _b, _c, _d;
|
|
1203
|
-
if (!this.#runner.__webSocketReady()) {
|
|
1204
|
-
(_a = this.log) == null ? void 0 : _a.debug({
|
|
1205
|
-
msg: "buffering tunnel message, socket not connected to engine",
|
|
1206
|
-
requestId: idToStr(requestId),
|
|
1207
|
-
message: stringifyToServerTunnelMessageKind(messageKind)
|
|
1208
|
-
});
|
|
1209
|
-
this.#bufferedMessages.push({ gatewayId, requestId, messageKind });
|
|
1210
|
-
return;
|
|
1211
|
-
}
|
|
1212
|
-
const gatewayIdStr = idToStr(gatewayId);
|
|
1213
|
-
const requestIdStr = idToStr(requestId);
|
|
1214
|
-
const actor = this.getRequestActor(gatewayId, requestId);
|
|
1215
|
-
if (!actor) {
|
|
1216
|
-
(_b = this.log) == null ? void 0 : _b.warn({
|
|
1217
|
-
msg: "cannot send tunnel message, actor not found",
|
|
1218
|
-
gatewayId: gatewayIdStr,
|
|
1219
|
-
requestId: requestIdStr
|
|
1220
|
-
});
|
|
1221
|
-
return;
|
|
1222
|
-
}
|
|
1223
|
-
let clientMessageIndex;
|
|
1224
|
-
const pending = actor.getPendingRequest(gatewayId, requestId);
|
|
1225
|
-
if (pending) {
|
|
1226
|
-
clientMessageIndex = pending.clientMessageIndex;
|
|
1227
|
-
pending.clientMessageIndex++;
|
|
1228
|
-
} else {
|
|
1229
|
-
(_c = this.log) == null ? void 0 : _c.warn({
|
|
1230
|
-
msg: "missing pending request for send message, defaulting to message index 0",
|
|
1231
|
-
gatewayId: gatewayIdStr,
|
|
1232
|
-
requestId: requestIdStr
|
|
1233
|
-
});
|
|
1234
|
-
clientMessageIndex = 0;
|
|
1235
|
-
}
|
|
1236
|
-
const messageId = {
|
|
1237
|
-
gatewayId,
|
|
1238
|
-
requestId,
|
|
1239
|
-
messageIndex: clientMessageIndex
|
|
1240
|
-
};
|
|
1241
|
-
const messageIdStr = `${idToStr(messageId.gatewayId)}-${idToStr(messageId.requestId)}-${messageId.messageIndex}`;
|
|
1242
|
-
(_d = this.log) == null ? void 0 : _d.debug({
|
|
1243
|
-
msg: "sending tunnel msg",
|
|
1244
|
-
messageId: messageIdStr,
|
|
1245
|
-
gatewayId: gatewayIdStr,
|
|
1246
|
-
requestId: requestIdStr,
|
|
1247
|
-
messageIndex: clientMessageIndex,
|
|
1248
|
-
message: stringifyToServerTunnelMessageKind(messageKind)
|
|
1249
|
-
});
|
|
1250
|
-
const message = {
|
|
1251
|
-
tag: "ToServerTunnelMessage",
|
|
1252
|
-
val: {
|
|
1253
|
-
messageId,
|
|
1254
|
-
messageKind
|
|
1255
|
-
}
|
|
1256
|
-
};
|
|
1257
|
-
this.#runner.__sendToServer(message);
|
|
1258
|
-
}
|
|
1259
|
-
closeActiveRequests(actor) {
|
|
1260
|
-
const actorId = actor.actorId;
|
|
1261
|
-
for (const entry of actor.pendingRequests) {
|
|
1262
|
-
entry.request.reject(new Error(`Actor ${actorId} stopped`));
|
|
1263
|
-
if (entry.gatewayId && entry.requestId) {
|
|
1264
|
-
this.#removeRequestToActor(entry.gatewayId, entry.requestId);
|
|
1265
|
-
}
|
|
1266
|
-
}
|
|
1267
|
-
for (const entry of actor.webSockets) {
|
|
1268
|
-
const isHibernatable = entry.ws[HIBERNATABLE_SYMBOL];
|
|
1269
|
-
if (!isHibernatable) {
|
|
1270
|
-
entry.ws._closeWithoutCallback(1e3, "actor.stopped");
|
|
1271
|
-
}
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1274
|
-
async #fetch(actorId, gatewayId, requestId, request) {
|
|
1275
|
-
var _a;
|
|
1276
|
-
if (!this.#runner.hasActor(actorId)) {
|
|
1277
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
1278
|
-
msg: "ignoring request for unknown actor",
|
|
1279
|
-
actorId
|
|
1280
|
-
});
|
|
1281
|
-
return new Response("Actor not found", {
|
|
1282
|
-
status: 503,
|
|
1283
|
-
headers: { "x-rivet-error": "runner.actor_not_found" }
|
|
1284
|
-
});
|
|
1285
|
-
}
|
|
1286
|
-
const fetchHandler = this.#runner.config.fetch(
|
|
1287
|
-
this.#runner,
|
|
1288
|
-
actorId,
|
|
1289
|
-
gatewayId,
|
|
1290
|
-
requestId,
|
|
1291
|
-
request
|
|
1292
|
-
);
|
|
1293
|
-
if (!fetchHandler) {
|
|
1294
|
-
return new Response("Not Implemented", { status: 501 });
|
|
1295
|
-
}
|
|
1296
|
-
return fetchHandler;
|
|
1297
|
-
}
|
|
1298
|
-
async handleTunnelMessage(message) {
|
|
1299
|
-
var _a;
|
|
1300
|
-
const { gatewayId, requestId, messageIndex } = message.messageId;
|
|
1301
|
-
const gatewayIdStr = idToStr(gatewayId);
|
|
1302
|
-
const requestIdStr = idToStr(requestId);
|
|
1303
|
-
(_a = this.log) == null ? void 0 : _a.debug({
|
|
1304
|
-
msg: "receive tunnel msg",
|
|
1305
|
-
gatewayId: gatewayIdStr,
|
|
1306
|
-
requestId: requestIdStr,
|
|
1307
|
-
messageIndex: message.messageId.messageIndex,
|
|
1308
|
-
message: stringifyToClientTunnelMessageKind(message.messageKind)
|
|
1309
|
-
});
|
|
1310
|
-
switch (message.messageKind.tag) {
|
|
1311
|
-
case "ToClientRequestStart":
|
|
1312
|
-
await this.#handleRequestStart(
|
|
1313
|
-
gatewayId,
|
|
1314
|
-
requestId,
|
|
1315
|
-
message.messageKind.val
|
|
1316
|
-
);
|
|
1317
|
-
break;
|
|
1318
|
-
case "ToClientRequestChunk":
|
|
1319
|
-
await this.#handleRequestChunk(
|
|
1320
|
-
gatewayId,
|
|
1321
|
-
requestId,
|
|
1322
|
-
message.messageKind.val
|
|
1323
|
-
);
|
|
1324
|
-
break;
|
|
1325
|
-
case "ToClientRequestAbort":
|
|
1326
|
-
await this.#handleRequestAbort(gatewayId, requestId);
|
|
1327
|
-
break;
|
|
1328
|
-
case "ToClientWebSocketOpen":
|
|
1329
|
-
await this.#handleWebSocketOpen(
|
|
1330
|
-
gatewayId,
|
|
1331
|
-
requestId,
|
|
1332
|
-
message.messageKind.val
|
|
1333
|
-
);
|
|
1334
|
-
break;
|
|
1335
|
-
case "ToClientWebSocketMessage": {
|
|
1336
|
-
await this.#handleWebSocketMessage(
|
|
1337
|
-
gatewayId,
|
|
1338
|
-
requestId,
|
|
1339
|
-
messageIndex,
|
|
1340
|
-
message.messageKind.val
|
|
1341
|
-
);
|
|
1342
|
-
break;
|
|
1343
|
-
}
|
|
1344
|
-
case "ToClientWebSocketClose":
|
|
1345
|
-
await this.#handleWebSocketClose(
|
|
1346
|
-
gatewayId,
|
|
1347
|
-
requestId,
|
|
1348
|
-
message.messageKind.val
|
|
1349
|
-
);
|
|
1350
|
-
break;
|
|
1351
|
-
case "DeprecatedTunnelAck":
|
|
1352
|
-
break;
|
|
1353
|
-
default:
|
|
1354
|
-
unreachable(message.messageKind);
|
|
1355
|
-
}
|
|
1356
|
-
}
|
|
1357
|
-
async #handleRequestStart(gatewayId, requestId, req) {
|
|
1358
|
-
var _a, _b, _c;
|
|
1359
|
-
const requestIdStr = idToStr(requestId);
|
|
1360
|
-
const actor = await this.#runner.getAndWaitForActor(req.actorId);
|
|
1361
|
-
if (!actor) {
|
|
1362
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
1363
|
-
msg: "actor does not exist in handleRequestStart, request will leak",
|
|
1364
|
-
actorId: req.actorId,
|
|
1365
|
-
requestId: requestIdStr
|
|
1366
|
-
});
|
|
1367
|
-
return;
|
|
1368
|
-
}
|
|
1369
|
-
this.addRequestToActor(gatewayId, requestId, req.actorId);
|
|
1370
|
-
try {
|
|
1371
|
-
const headers = new Headers();
|
|
1372
|
-
for (const [key, value] of req.headers) {
|
|
1373
|
-
headers.append(key, value);
|
|
1374
|
-
}
|
|
1375
|
-
const request = new Request(`http://localhost${req.path}`, {
|
|
1376
|
-
method: req.method,
|
|
1377
|
-
headers,
|
|
1378
|
-
body: req.body ? new Uint8Array(req.body) : void 0
|
|
1379
|
-
});
|
|
1380
|
-
if (req.stream) {
|
|
1381
|
-
const stream = new ReadableStream({
|
|
1382
|
-
start: (controller) => {
|
|
1383
|
-
const existing = actor.getPendingRequest(
|
|
1384
|
-
gatewayId,
|
|
1385
|
-
requestId
|
|
1386
|
-
);
|
|
1387
|
-
if (existing) {
|
|
1388
|
-
existing.streamController = controller;
|
|
1389
|
-
existing.actorId = req.actorId;
|
|
1390
|
-
existing.gatewayId = gatewayId;
|
|
1391
|
-
existing.requestId = requestId;
|
|
1392
|
-
} else {
|
|
1393
|
-
actor.createPendingRequestWithStreamController(
|
|
1394
|
-
gatewayId,
|
|
1395
|
-
requestId,
|
|
1396
|
-
0,
|
|
1397
|
-
controller
|
|
1398
|
-
);
|
|
1399
|
-
}
|
|
1400
|
-
}
|
|
1401
|
-
});
|
|
1402
|
-
const streamingRequest = new Request(request, {
|
|
1403
|
-
body: stream,
|
|
1404
|
-
duplex: "half"
|
|
1405
|
-
});
|
|
1406
|
-
const response = await this.#fetch(
|
|
1407
|
-
req.actorId,
|
|
1408
|
-
gatewayId,
|
|
1409
|
-
requestId,
|
|
1410
|
-
streamingRequest
|
|
1411
|
-
);
|
|
1412
|
-
await this.#sendResponse(
|
|
1413
|
-
actor.actorId,
|
|
1414
|
-
actor.generation,
|
|
1415
|
-
gatewayId,
|
|
1416
|
-
requestId,
|
|
1417
|
-
response
|
|
1418
|
-
);
|
|
1419
|
-
} else {
|
|
1420
|
-
actor.createPendingRequest(gatewayId, requestId, 0);
|
|
1421
|
-
const response = await this.#fetch(
|
|
1422
|
-
req.actorId,
|
|
1423
|
-
gatewayId,
|
|
1424
|
-
requestId,
|
|
1425
|
-
request
|
|
1426
|
-
);
|
|
1427
|
-
await this.#sendResponse(
|
|
1428
|
-
actor.actorId,
|
|
1429
|
-
actor.generation,
|
|
1430
|
-
gatewayId,
|
|
1431
|
-
requestId,
|
|
1432
|
-
response
|
|
1433
|
-
);
|
|
1434
|
-
}
|
|
1435
|
-
} catch (error) {
|
|
1436
|
-
if (error instanceof RunnerShutdownError) {
|
|
1437
|
-
(_b = this.log) == null ? void 0 : _b.debug({ msg: "catught runner shutdown error" });
|
|
1438
|
-
} else {
|
|
1439
|
-
(_c = this.log) == null ? void 0 : _c.error({ msg: "error handling request", error });
|
|
1440
|
-
this.#sendResponseError(
|
|
1441
|
-
actor.actorId,
|
|
1442
|
-
actor.generation,
|
|
1443
|
-
gatewayId,
|
|
1444
|
-
requestId,
|
|
1445
|
-
500,
|
|
1446
|
-
"Internal Server Error"
|
|
1447
|
-
);
|
|
1448
|
-
}
|
|
1449
|
-
} finally {
|
|
1450
|
-
if (this.#runner.hasActor(req.actorId, actor.generation)) {
|
|
1451
|
-
actor.deletePendingRequest(gatewayId, requestId);
|
|
1452
|
-
this.#removeRequestToActor(gatewayId, requestId);
|
|
1453
|
-
}
|
|
1454
|
-
}
|
|
1455
|
-
}
|
|
1456
|
-
async #handleRequestChunk(gatewayId, requestId, chunk) {
|
|
1457
|
-
const actor = await this.getAndWaitForRequestActor(
|
|
1458
|
-
gatewayId,
|
|
1459
|
-
requestId
|
|
1460
|
-
);
|
|
1461
|
-
if (actor) {
|
|
1462
|
-
const pending = actor.getPendingRequest(gatewayId, requestId);
|
|
1463
|
-
if (pending == null ? void 0 : pending.streamController) {
|
|
1464
|
-
pending.streamController.enqueue(new Uint8Array(chunk.body));
|
|
1465
|
-
if (chunk.finish) {
|
|
1466
|
-
pending.streamController.close();
|
|
1467
|
-
actor.deletePendingRequest(gatewayId, requestId);
|
|
1468
|
-
this.#removeRequestToActor(gatewayId, requestId);
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
}
|
|
1472
|
-
}
|
|
1473
|
-
async #handleRequestAbort(gatewayId, requestId) {
|
|
1474
|
-
const actor = await this.getAndWaitForRequestActor(
|
|
1475
|
-
gatewayId,
|
|
1476
|
-
requestId
|
|
1477
|
-
);
|
|
1478
|
-
if (actor) {
|
|
1479
|
-
const pending = actor.getPendingRequest(gatewayId, requestId);
|
|
1480
|
-
if (pending == null ? void 0 : pending.streamController) {
|
|
1481
|
-
pending.streamController.error(new Error("Request aborted"));
|
|
1482
|
-
}
|
|
1483
|
-
actor.deletePendingRequest(gatewayId, requestId);
|
|
1484
|
-
this.#removeRequestToActor(gatewayId, requestId);
|
|
1485
|
-
}
|
|
1486
|
-
}
|
|
1487
|
-
async #sendResponse(actorId, generation, gatewayId, requestId, response) {
|
|
1488
|
-
var _a;
|
|
1489
|
-
if (!this.#runner.hasActor(actorId, generation)) {
|
|
1490
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
1491
|
-
msg: "actor not loaded to send response, assuming gateway has closed request",
|
|
1492
|
-
actorId,
|
|
1493
|
-
generation,
|
|
1494
|
-
requestId
|
|
1495
|
-
});
|
|
1496
|
-
return;
|
|
1497
|
-
}
|
|
1498
|
-
const body = response.body ? await response.arrayBuffer() : null;
|
|
1499
|
-
const headers = /* @__PURE__ */ new Map();
|
|
1500
|
-
response.headers.forEach((value, key) => {
|
|
1501
|
-
headers.set(key, value);
|
|
1502
|
-
});
|
|
1503
|
-
if (body && !headers.has("content-length")) {
|
|
1504
|
-
headers.set("content-length", String(body.byteLength));
|
|
1505
|
-
}
|
|
1506
|
-
this.#sendMessage(gatewayId, requestId, {
|
|
1507
|
-
tag: "ToServerResponseStart",
|
|
1508
|
-
val: {
|
|
1509
|
-
status: response.status,
|
|
1510
|
-
headers,
|
|
1511
|
-
body: body || null,
|
|
1512
|
-
stream: false
|
|
1513
|
-
}
|
|
1514
|
-
});
|
|
1515
|
-
}
|
|
1516
|
-
#sendResponseError(actorId, generation, gatewayId, requestId, status, message) {
|
|
1517
|
-
var _a;
|
|
1518
|
-
if (!this.#runner.hasActor(actorId, generation)) {
|
|
1519
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
1520
|
-
msg: "actor not loaded to send response, assuming gateway has closed request",
|
|
1521
|
-
actorId,
|
|
1522
|
-
generation,
|
|
1523
|
-
requestId
|
|
1524
|
-
});
|
|
1525
|
-
return;
|
|
1526
|
-
}
|
|
1527
|
-
const headers = /* @__PURE__ */ new Map();
|
|
1528
|
-
headers.set("content-type", "text/plain");
|
|
1529
|
-
this.#sendMessage(gatewayId, requestId, {
|
|
1530
|
-
tag: "ToServerResponseStart",
|
|
1531
|
-
val: {
|
|
1532
|
-
status,
|
|
1533
|
-
headers,
|
|
1534
|
-
body: new TextEncoder().encode(message).buffer,
|
|
1535
|
-
stream: false
|
|
1536
|
-
}
|
|
1537
|
-
});
|
|
1538
|
-
}
|
|
1539
|
-
async #handleWebSocketOpen(gatewayId, requestId, open) {
|
|
1540
|
-
var _a, _b, _c;
|
|
1541
|
-
const requestIdStr = idToStr(requestId);
|
|
1542
|
-
const actor = await this.#runner.getAndWaitForActor(open.actorId);
|
|
1543
|
-
if (!actor) {
|
|
1544
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
1545
|
-
msg: "ignoring websocket for unknown actor",
|
|
1546
|
-
actorId: open.actorId
|
|
1547
|
-
});
|
|
1548
|
-
this.#sendMessage(gatewayId, requestId, {
|
|
1549
|
-
tag: "ToServerWebSocketClose",
|
|
1550
|
-
val: {
|
|
1551
|
-
code: 1011,
|
|
1552
|
-
reason: "Actor not found",
|
|
1553
|
-
hibernate: false
|
|
1554
|
-
}
|
|
1555
|
-
});
|
|
1556
|
-
return;
|
|
1557
|
-
}
|
|
1558
|
-
const existingAdapter = actor.getWebSocket(gatewayId, requestId);
|
|
1559
|
-
if (existingAdapter) {
|
|
1560
|
-
(_b = this.log) == null ? void 0 : _b.warn({
|
|
1561
|
-
msg: "closing existing websocket for duplicate open event for the same request id",
|
|
1562
|
-
requestId: requestIdStr
|
|
1563
|
-
});
|
|
1564
|
-
existingAdapter._closeWithoutCallback(1e3, "ws.duplicate_open");
|
|
1565
|
-
}
|
|
1566
|
-
try {
|
|
1567
|
-
const request = buildRequestForWebSocket(
|
|
1568
|
-
open.path,
|
|
1569
|
-
Object.fromEntries(open.headers)
|
|
1570
|
-
);
|
|
1571
|
-
const canHibernate = this.#runner.config.hibernatableWebSocket.canHibernate(
|
|
1572
|
-
actor.actorId,
|
|
1573
|
-
gatewayId,
|
|
1574
|
-
requestId,
|
|
1575
|
-
request
|
|
1576
|
-
);
|
|
1577
|
-
const adapter = await this.#createWebSocket(
|
|
1578
|
-
actor.actorId,
|
|
1579
|
-
gatewayId,
|
|
1580
|
-
requestId,
|
|
1581
|
-
requestIdStr,
|
|
1582
|
-
0,
|
|
1583
|
-
canHibernate,
|
|
1584
|
-
false,
|
|
1585
|
-
request,
|
|
1586
|
-
open.path,
|
|
1587
|
-
Object.fromEntries(open.headers),
|
|
1588
|
-
false
|
|
1589
|
-
);
|
|
1590
|
-
actor.createPendingRequest(gatewayId, requestId, 0);
|
|
1591
|
-
this.#sendMessage(gatewayId, requestId, {
|
|
1592
|
-
tag: "ToServerWebSocketOpen",
|
|
1593
|
-
val: {
|
|
1594
|
-
canHibernate
|
|
1595
|
-
}
|
|
1596
|
-
});
|
|
1597
|
-
adapter._handleOpen(requestId);
|
|
1598
|
-
} catch (error) {
|
|
1599
|
-
(_c = this.log) == null ? void 0 : _c.error({ msg: "error handling websocket open", error });
|
|
1600
|
-
this.#sendMessage(gatewayId, requestId, {
|
|
1601
|
-
tag: "ToServerWebSocketClose",
|
|
1602
|
-
val: {
|
|
1603
|
-
code: 1011,
|
|
1604
|
-
reason: "Server Error",
|
|
1605
|
-
hibernate: false
|
|
1606
|
-
}
|
|
1607
|
-
});
|
|
1608
|
-
actor.deleteWebSocket(gatewayId, requestId);
|
|
1609
|
-
actor.deletePendingRequest(gatewayId, requestId);
|
|
1610
|
-
this.#removeRequestToActor(gatewayId, requestId);
|
|
1611
|
-
}
|
|
1612
|
-
}
|
|
1613
|
-
async #handleWebSocketMessage(gatewayId, requestId, serverMessageIndex, msg) {
|
|
1614
|
-
var _a;
|
|
1615
|
-
const actor = await this.getAndWaitForRequestActor(
|
|
1616
|
-
gatewayId,
|
|
1617
|
-
requestId
|
|
1618
|
-
);
|
|
1619
|
-
if (actor) {
|
|
1620
|
-
const adapter = actor.getWebSocket(gatewayId, requestId);
|
|
1621
|
-
if (adapter) {
|
|
1622
|
-
const data = msg.binary ? new Uint8Array(msg.data) : new TextDecoder().decode(new Uint8Array(msg.data));
|
|
1623
|
-
adapter._handleMessage(
|
|
1624
|
-
requestId,
|
|
1625
|
-
data,
|
|
1626
|
-
serverMessageIndex,
|
|
1627
|
-
msg.binary
|
|
1628
|
-
);
|
|
1629
|
-
return;
|
|
1630
|
-
}
|
|
1631
|
-
}
|
|
1632
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
1633
|
-
msg: "missing websocket for incoming websocket message, this may indicate the actor stopped before processing a message",
|
|
1634
|
-
requestId
|
|
1635
|
-
});
|
|
1636
|
-
}
|
|
1637
|
-
sendHibernatableWebSocketMessageAck(gatewayId, requestId, clientMessageIndex) {
|
|
1638
|
-
var _a, _b, _c;
|
|
1639
|
-
const requestIdStr = idToStr(requestId);
|
|
1640
|
-
(_a = this.log) == null ? void 0 : _a.debug({
|
|
1641
|
-
msg: "ack ws msg",
|
|
1642
|
-
requestId: requestIdStr,
|
|
1643
|
-
index: clientMessageIndex
|
|
1644
|
-
});
|
|
1645
|
-
if (clientMessageIndex < 0 || clientMessageIndex > 65535)
|
|
1646
|
-
throw new Error("invalid websocket ack index");
|
|
1647
|
-
const actor = this.getRequestActor(gatewayId, requestId);
|
|
1648
|
-
if (!actor) {
|
|
1649
|
-
(_b = this.log) == null ? void 0 : _b.warn({
|
|
1650
|
-
msg: "cannot send websocket ack, actor not found",
|
|
1651
|
-
requestId: requestIdStr
|
|
1652
|
-
});
|
|
1653
|
-
return;
|
|
1654
|
-
}
|
|
1655
|
-
const pending = actor.getPendingRequest(gatewayId, requestId);
|
|
1656
|
-
if (!(pending == null ? void 0 : pending.gatewayId)) {
|
|
1657
|
-
(_c = this.log) == null ? void 0 : _c.warn({
|
|
1658
|
-
msg: "cannot send websocket ack, gatewayId not found in pending request",
|
|
1659
|
-
requestId: requestIdStr
|
|
1660
|
-
});
|
|
1661
|
-
return;
|
|
1662
|
-
}
|
|
1663
|
-
this.#sendMessage(pending.gatewayId, requestId, {
|
|
1664
|
-
tag: "ToServerWebSocketMessageAck",
|
|
1665
|
-
val: {
|
|
1666
|
-
index: clientMessageIndex
|
|
1667
|
-
}
|
|
1668
|
-
});
|
|
1669
|
-
}
|
|
1670
|
-
async #handleWebSocketClose(gatewayId, requestId, close) {
|
|
1671
|
-
const actor = await this.getAndWaitForRequestActor(
|
|
1672
|
-
gatewayId,
|
|
1673
|
-
requestId
|
|
1674
|
-
);
|
|
1675
|
-
if (actor) {
|
|
1676
|
-
const adapter = actor.getWebSocket(gatewayId, requestId);
|
|
1677
|
-
if (adapter) {
|
|
1678
|
-
adapter._handleClose(
|
|
1679
|
-
requestId,
|
|
1680
|
-
close.code || void 0,
|
|
1681
|
-
close.reason || void 0
|
|
1682
|
-
);
|
|
1683
|
-
actor.deleteWebSocket(gatewayId, requestId);
|
|
1684
|
-
actor.deletePendingRequest(gatewayId, requestId);
|
|
1685
|
-
this.#removeRequestToActor(gatewayId, requestId);
|
|
1686
|
-
}
|
|
1687
|
-
}
|
|
1688
|
-
}
|
|
1689
|
-
};
|
|
1690
|
-
function buildRequestForWebSocket(path, headers) {
|
|
1691
|
-
const fullHeaders = {
|
|
1692
|
-
...headers,
|
|
1693
|
-
Upgrade: "websocket",
|
|
1694
|
-
Connection: "Upgrade"
|
|
1695
|
-
};
|
|
1696
|
-
if (!path.startsWith("/")) {
|
|
1697
|
-
throw new Error("path must start with leading slash");
|
|
1698
|
-
}
|
|
1699
|
-
const request = new Request(`http://actor${path}`, {
|
|
1700
|
-
method: "GET",
|
|
1701
|
-
headers: fullHeaders
|
|
1702
|
-
});
|
|
1703
|
-
return request;
|
|
1704
|
-
}
|
|
1705
|
-
|
|
1706
|
-
// src/websocket.ts
|
|
1707
|
-
var webSocketPromise = null;
|
|
1708
|
-
async function importWebSocket() {
|
|
1709
|
-
if (webSocketPromise !== null) {
|
|
1710
|
-
return webSocketPromise;
|
|
1711
|
-
}
|
|
1712
|
-
webSocketPromise = (async () => {
|
|
1713
|
-
var _a, _b, _c;
|
|
1714
|
-
let _WebSocket;
|
|
1715
|
-
if (typeof WebSocket !== "undefined") {
|
|
1716
|
-
_WebSocket = WebSocket;
|
|
1717
|
-
(_a = logger()) == null ? void 0 : _a.debug({ msg: "using native websocket" });
|
|
1718
|
-
} else {
|
|
1719
|
-
try {
|
|
1720
|
-
const ws = await import("ws");
|
|
1721
|
-
_WebSocket = ws.default;
|
|
1722
|
-
(_b = logger()) == null ? void 0 : _b.debug({ msg: "using websocket from npm" });
|
|
1723
|
-
} catch {
|
|
1724
|
-
_WebSocket = class MockWebSocket {
|
|
1725
|
-
constructor() {
|
|
1726
|
-
throw new Error(
|
|
1727
|
-
'WebSocket support requires installing the "ws" peer dependency.'
|
|
1728
|
-
);
|
|
1729
|
-
}
|
|
1730
|
-
};
|
|
1731
|
-
(_c = logger()) == null ? void 0 : _c.debug({ msg: "using mock websocket" });
|
|
1732
|
-
}
|
|
1733
|
-
}
|
|
1734
|
-
return _WebSocket;
|
|
1735
|
-
})();
|
|
1736
|
-
return webSocketPromise;
|
|
1737
|
-
}
|
|
1738
|
-
|
|
1739
|
-
// src/mod.ts
|
|
1740
|
-
var KV_EXPIRE = 3e4;
|
|
1741
|
-
var PROTOCOL_VERSION = 3;
|
|
1742
|
-
var RUNNER_PING_INTERVAL = 3e3;
|
|
1743
|
-
var EVENT_BACKLOG_WARN_THRESHOLD = 1e4;
|
|
1744
|
-
var SIGNAL_HANDLERS = [];
|
|
1745
|
-
var Runner = class {
|
|
1746
|
-
#config;
|
|
1747
|
-
get config() {
|
|
1748
|
-
return this.#config;
|
|
1749
|
-
}
|
|
1750
|
-
#actors = /* @__PURE__ */ new Map();
|
|
1751
|
-
// WebSocket
|
|
1752
|
-
__pegboardWebSocket;
|
|
1753
|
-
runnerId;
|
|
1754
|
-
#lastCommandIdx = -1;
|
|
1755
|
-
#pingLoop;
|
|
1756
|
-
#nextEventIdx = 0n;
|
|
1757
|
-
#started = false;
|
|
1758
|
-
#shutdown = false;
|
|
1759
|
-
#shuttingDown = false;
|
|
1760
|
-
#reconnectAttempt = 0;
|
|
1761
|
-
#reconnectTimeout;
|
|
1762
|
-
// Runner lost threshold management
|
|
1763
|
-
#runnerLostThreshold;
|
|
1764
|
-
#runnerLostTimeout;
|
|
1765
|
-
// Event storage for resending
|
|
1766
|
-
#eventHistory = [];
|
|
1767
|
-
#eventBacklogWarned = false;
|
|
1768
|
-
// Command acknowledgment
|
|
1769
|
-
#ackInterval;
|
|
1770
|
-
// KV operations
|
|
1771
|
-
#nextKvRequestId = 0;
|
|
1772
|
-
#kvRequests = /* @__PURE__ */ new Map();
|
|
1773
|
-
#kvCleanupInterval;
|
|
1774
|
-
// Tunnel for HTTP/WebSocket forwarding
|
|
1775
|
-
#tunnel;
|
|
1776
|
-
// Cached child logger with runner-specific attributes
|
|
1777
|
-
#logCached;
|
|
1778
|
-
get log() {
|
|
1779
|
-
if (this.#logCached) return this.#logCached;
|
|
1780
|
-
const l = logger();
|
|
1781
|
-
if (l) {
|
|
1782
|
-
if (this.runnerId) {
|
|
1783
|
-
this.#logCached = l.child({
|
|
1784
|
-
runnerId: this.runnerId
|
|
1785
|
-
});
|
|
1786
|
-
return this.#logCached;
|
|
1787
|
-
} else {
|
|
1788
|
-
return l;
|
|
1789
|
-
}
|
|
1790
|
-
}
|
|
1791
|
-
return void 0;
|
|
1792
|
-
}
|
|
1793
|
-
constructor(config) {
|
|
1794
|
-
this.#config = config;
|
|
1795
|
-
if (this.#config.logger) setLogger(this.#config.logger);
|
|
1796
|
-
this.#kvCleanupInterval = setInterval(() => {
|
|
1797
|
-
this.#cleanupOldKvRequests();
|
|
1798
|
-
}, 15e3);
|
|
1799
|
-
}
|
|
1800
|
-
// MARK: Manage actors
|
|
1801
|
-
sleepActor(actorId, generation) {
|
|
1802
|
-
const actor = this.getActor(actorId, generation);
|
|
1803
|
-
if (!actor) return;
|
|
1804
|
-
this.#sendActorIntent(actorId, actor.generation, "sleep");
|
|
1805
|
-
}
|
|
1806
|
-
async stopActor(actorId, generation) {
|
|
1807
|
-
const actor = this.getActor(actorId, generation);
|
|
1808
|
-
if (!actor) return;
|
|
1809
|
-
this.#sendActorIntent(actorId, actor.generation, "stop");
|
|
1810
|
-
}
|
|
1811
|
-
async forceStopActor(actorId, generation) {
|
|
1812
|
-
var _a;
|
|
1813
|
-
const actor = this.getActor(actorId, generation);
|
|
1814
|
-
if (!actor) return;
|
|
1815
|
-
try {
|
|
1816
|
-
await this.#config.onActorStop(actorId, actor.generation);
|
|
1817
|
-
} catch (err) {
|
|
1818
|
-
console.error(`Error in onActorStop for actor ${actorId}:`, err);
|
|
1819
|
-
}
|
|
1820
|
-
(_a = this.#tunnel) == null ? void 0 : _a.closeActiveRequests(actor);
|
|
1821
|
-
this.#removeActor(actorId, generation);
|
|
1822
|
-
this.#sendActorStateUpdate(actorId, actor.generation, "stopped");
|
|
1823
|
-
}
|
|
1824
|
-
#stopAllActors() {
|
|
1825
|
-
var _a;
|
|
1826
|
-
(_a = this.log) == null ? void 0 : _a.info({
|
|
1827
|
-
msg: "stopping all actors due to runner lost threshold exceeded"
|
|
1828
|
-
});
|
|
1829
|
-
const actorIds = Array.from(this.#actors.keys());
|
|
1830
|
-
for (const actorId of actorIds) {
|
|
1831
|
-
this.forceStopActor(actorId);
|
|
1832
|
-
}
|
|
1833
|
-
}
|
|
1834
|
-
getActor(actorId, generation) {
|
|
1835
|
-
var _a, _b;
|
|
1836
|
-
const actor = this.#actors.get(actorId);
|
|
1837
|
-
if (!actor) {
|
|
1838
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
1839
|
-
msg: "actor not found",
|
|
1840
|
-
actorId
|
|
1841
|
-
});
|
|
1842
|
-
return void 0;
|
|
1843
|
-
}
|
|
1844
|
-
if (generation !== void 0 && actor.generation !== generation) {
|
|
1845
|
-
(_b = this.log) == null ? void 0 : _b.warn({
|
|
1846
|
-
msg: "actor generation mismatch",
|
|
1847
|
-
actorId,
|
|
1848
|
-
generation
|
|
1849
|
-
});
|
|
1850
|
-
return void 0;
|
|
1851
|
-
}
|
|
1852
|
-
return actor;
|
|
1853
|
-
}
|
|
1854
|
-
async getAndWaitForActor(actorId, generation) {
|
|
1855
|
-
const actor = this.getActor(actorId, generation);
|
|
1856
|
-
if (!actor) return;
|
|
1857
|
-
await actor.actorStartPromise.promise;
|
|
1858
|
-
return actor;
|
|
1859
|
-
}
|
|
1860
|
-
hasActor(actorId, generation) {
|
|
1861
|
-
const actor = this.#actors.get(actorId);
|
|
1862
|
-
return !!actor && (generation === void 0 || actor.generation === generation);
|
|
1863
|
-
}
|
|
1864
|
-
get actors() {
|
|
1865
|
-
return this.#actors;
|
|
1866
|
-
}
|
|
1867
|
-
// IMPORTANT: Make sure to call stopActiveRequests if calling #removeActor
|
|
1868
|
-
#removeActor(actorId, generation) {
|
|
1869
|
-
var _a, _b, _c;
|
|
1870
|
-
const actor = this.#actors.get(actorId);
|
|
1871
|
-
if (!actor) {
|
|
1872
|
-
(_a = this.log) == null ? void 0 : _a.error({
|
|
1873
|
-
msg: "actor not found for removal",
|
|
1874
|
-
actorId
|
|
1875
|
-
});
|
|
1876
|
-
return void 0;
|
|
1877
|
-
}
|
|
1878
|
-
if (generation !== void 0 && actor.generation !== generation) {
|
|
1879
|
-
(_b = this.log) == null ? void 0 : _b.error({
|
|
1880
|
-
msg: "actor generation mismatch",
|
|
1881
|
-
actorId,
|
|
1882
|
-
generation
|
|
1883
|
-
});
|
|
1884
|
-
return void 0;
|
|
1885
|
-
}
|
|
1886
|
-
this.#actors.delete(actorId);
|
|
1887
|
-
(_c = this.log) == null ? void 0 : _c.info({
|
|
1888
|
-
msg: "removed actor",
|
|
1889
|
-
actorId,
|
|
1890
|
-
actors: this.#actors.size
|
|
1891
|
-
});
|
|
1892
|
-
return actor;
|
|
1893
|
-
}
|
|
1894
|
-
// MARK: Start
|
|
1895
|
-
async start() {
|
|
1896
|
-
var _a, _b;
|
|
1897
|
-
if (this.#started) throw new Error("Cannot call runner.start twice");
|
|
1898
|
-
this.#started = true;
|
|
1899
|
-
(_a = this.log) == null ? void 0 : _a.info({ msg: "starting runner" });
|
|
1900
|
-
this.#tunnel = new Tunnel(this);
|
|
1901
|
-
this.#tunnel.start();
|
|
1902
|
-
try {
|
|
1903
|
-
await this.#openPegboardWebSocket();
|
|
1904
|
-
} catch (error) {
|
|
1905
|
-
this.#started = false;
|
|
1906
|
-
throw error;
|
|
1907
|
-
}
|
|
1908
|
-
if (!this.#config.noAutoShutdown) {
|
|
1909
|
-
if (!SIGNAL_HANDLERS.length) {
|
|
1910
|
-
process.on("SIGTERM", async () => {
|
|
1911
|
-
var _a2;
|
|
1912
|
-
(_a2 = this.log) == null ? void 0 : _a2.debug("received SIGTERM");
|
|
1913
|
-
for (const handler of SIGNAL_HANDLERS) {
|
|
1914
|
-
await handler();
|
|
1915
|
-
}
|
|
1916
|
-
});
|
|
1917
|
-
process.on("SIGINT", async () => {
|
|
1918
|
-
var _a2;
|
|
1919
|
-
(_a2 = this.log) == null ? void 0 : _a2.debug("received SIGINT");
|
|
1920
|
-
for (const handler of SIGNAL_HANDLERS) {
|
|
1921
|
-
await handler();
|
|
1922
|
-
}
|
|
1923
|
-
});
|
|
1924
|
-
(_b = this.log) == null ? void 0 : _b.debug({
|
|
1925
|
-
msg: "added SIGTERM listeners"
|
|
1926
|
-
});
|
|
1927
|
-
}
|
|
1928
|
-
SIGNAL_HANDLERS.push(async () => {
|
|
1929
|
-
var _a2;
|
|
1930
|
-
const weak = new WeakRef(this);
|
|
1931
|
-
await ((_a2 = weak.deref()) == null ? void 0 : _a2.shutdown(false, false));
|
|
1932
|
-
});
|
|
1933
|
-
}
|
|
1934
|
-
}
|
|
1935
|
-
// MARK: Shutdown
|
|
1936
|
-
async shutdown(immediate, exit = false) {
|
|
1937
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1938
|
-
if (this.#shuttingDown) {
|
|
1939
|
-
(_a = this.log) == null ? void 0 : _a.debug({
|
|
1940
|
-
msg: "shutdown already in progress, ignoring"
|
|
1941
|
-
});
|
|
1942
|
-
return;
|
|
1943
|
-
}
|
|
1944
|
-
this.#shuttingDown = true;
|
|
1945
|
-
(_b = this.log) == null ? void 0 : _b.info({
|
|
1946
|
-
msg: "starting shutdown",
|
|
1947
|
-
immediate,
|
|
1948
|
-
exit
|
|
1949
|
-
});
|
|
1950
|
-
this.#shutdown = true;
|
|
1951
|
-
if (this.#reconnectTimeout) {
|
|
1952
|
-
clearTimeout(this.#reconnectTimeout);
|
|
1953
|
-
this.#reconnectTimeout = void 0;
|
|
1954
|
-
}
|
|
1955
|
-
if (this.#runnerLostTimeout) {
|
|
1956
|
-
clearTimeout(this.#runnerLostTimeout);
|
|
1957
|
-
this.#runnerLostTimeout = void 0;
|
|
1958
|
-
}
|
|
1959
|
-
if (this.#pingLoop) {
|
|
1960
|
-
clearInterval(this.#pingLoop);
|
|
1961
|
-
this.#pingLoop = void 0;
|
|
1962
|
-
}
|
|
1963
|
-
if (this.#ackInterval) {
|
|
1964
|
-
clearInterval(this.#ackInterval);
|
|
1965
|
-
this.#ackInterval = void 0;
|
|
1966
|
-
}
|
|
1967
|
-
if (this.#kvCleanupInterval) {
|
|
1968
|
-
clearInterval(this.#kvCleanupInterval);
|
|
1969
|
-
this.#kvCleanupInterval = void 0;
|
|
1970
|
-
}
|
|
1971
|
-
for (const request of this.#kvRequests.values()) {
|
|
1972
|
-
request.reject(
|
|
1973
|
-
new Error("WebSocket connection closed during shutdown")
|
|
1974
|
-
);
|
|
1975
|
-
}
|
|
1976
|
-
this.#kvRequests.clear();
|
|
1977
|
-
if (this.__webSocketReady()) {
|
|
1978
|
-
const pegboardWebSocket = this.__pegboardWebSocket;
|
|
1979
|
-
if (immediate) {
|
|
1980
|
-
pegboardWebSocket.close(1e3, "pegboard.runner_shutdown");
|
|
1981
|
-
} else {
|
|
1982
|
-
try {
|
|
1983
|
-
(_c = this.log) == null ? void 0 : _c.info({
|
|
1984
|
-
msg: "sending stopping message",
|
|
1985
|
-
readyState: pegboardWebSocket.readyState
|
|
1986
|
-
});
|
|
1987
|
-
this.__sendToServer({
|
|
1988
|
-
tag: "ToServerStopping",
|
|
1989
|
-
val: null
|
|
1990
|
-
});
|
|
1991
|
-
const closePromise = new Promise((resolve) => {
|
|
1992
|
-
if (!pegboardWebSocket)
|
|
1993
|
-
throw new Error("missing pegboardWebSocket");
|
|
1994
|
-
pegboardWebSocket.addEventListener("close", (ev) => {
|
|
1995
|
-
var _a2;
|
|
1996
|
-
(_a2 = this.log) == null ? void 0 : _a2.info({
|
|
1997
|
-
msg: "connection closed",
|
|
1998
|
-
code: ev.code,
|
|
1999
|
-
reason: ev.reason.toString()
|
|
2000
|
-
});
|
|
2001
|
-
resolve();
|
|
2002
|
-
});
|
|
2003
|
-
});
|
|
2004
|
-
await this.#waitForActorsToStop(pegboardWebSocket);
|
|
2005
|
-
(_d = this.log) == null ? void 0 : _d.info({
|
|
2006
|
-
msg: "closing WebSocket"
|
|
2007
|
-
});
|
|
2008
|
-
pegboardWebSocket.close(1e3, "pegboard.runner_shutdown");
|
|
2009
|
-
await closePromise;
|
|
2010
|
-
(_e = this.log) == null ? void 0 : _e.info({
|
|
2011
|
-
msg: "websocket shutdown completed"
|
|
2012
|
-
});
|
|
2013
|
-
} catch (error) {
|
|
2014
|
-
(_f = this.log) == null ? void 0 : _f.error({
|
|
2015
|
-
msg: "error during websocket shutdown:",
|
|
2016
|
-
error
|
|
2017
|
-
});
|
|
2018
|
-
pegboardWebSocket.close();
|
|
2019
|
-
}
|
|
2020
|
-
}
|
|
2021
|
-
} else {
|
|
2022
|
-
(_h = this.log) == null ? void 0 : _h.debug({
|
|
2023
|
-
msg: "no runner WebSocket to shutdown or already closed",
|
|
2024
|
-
readyState: (_g = this.__pegboardWebSocket) == null ? void 0 : _g.readyState
|
|
2025
|
-
});
|
|
2026
|
-
}
|
|
2027
|
-
if (this.#tunnel) {
|
|
2028
|
-
this.#tunnel.shutdown();
|
|
2029
|
-
this.#tunnel = void 0;
|
|
2030
|
-
}
|
|
2031
|
-
this.#config.onShutdown();
|
|
2032
|
-
if (exit) process.exit(0);
|
|
2033
|
-
}
|
|
2034
|
-
/**
|
|
2035
|
-
* Wait for all actors to stop before proceeding with shutdown.
|
|
2036
|
-
*
|
|
2037
|
-
* This method polls every 100ms to check if all actors have been stopped.
|
|
2038
|
-
*
|
|
2039
|
-
* It will resolve early if:
|
|
2040
|
-
* - All actors are stopped
|
|
2041
|
-
* - The WebSocket connection is closed
|
|
2042
|
-
* - The shutdown timeout is reached (120 seconds)
|
|
2043
|
-
*/
|
|
2044
|
-
async #waitForActorsToStop(ws) {
|
|
2045
|
-
const shutdownTimeout = 12e4;
|
|
2046
|
-
const shutdownCheckInterval = 100;
|
|
2047
|
-
const progressLogInterval = 5e3;
|
|
2048
|
-
const shutdownStartTs = Date.now();
|
|
2049
|
-
let lastProgressLogTs = 0;
|
|
2050
|
-
return new Promise((resolve) => {
|
|
2051
|
-
var _a, _b;
|
|
2052
|
-
const checkActors = () => {
|
|
2053
|
-
var _a2, _b2, _c, _d;
|
|
2054
|
-
const now = Date.now();
|
|
2055
|
-
const elapsed = now - shutdownStartTs;
|
|
2056
|
-
const wsIsClosed = ws.readyState === 2 || ws.readyState === 3;
|
|
2057
|
-
if (this.#actors.size === 0) {
|
|
2058
|
-
(_a2 = this.log) == null ? void 0 : _a2.info({
|
|
2059
|
-
msg: "all actors stopped",
|
|
2060
|
-
elapsed
|
|
2061
|
-
});
|
|
2062
|
-
return true;
|
|
2063
|
-
} else if (wsIsClosed) {
|
|
2064
|
-
(_b2 = this.log) == null ? void 0 : _b2.warn({
|
|
2065
|
-
msg: "websocket closed before all actors stopped",
|
|
2066
|
-
remainingActors: this.#actors.size,
|
|
2067
|
-
elapsed
|
|
2068
|
-
});
|
|
2069
|
-
return true;
|
|
2070
|
-
} else if (elapsed >= shutdownTimeout) {
|
|
2071
|
-
(_c = this.log) == null ? void 0 : _c.warn({
|
|
2072
|
-
msg: "shutdown timeout reached, forcing close",
|
|
2073
|
-
remainingActors: this.#actors.size,
|
|
2074
|
-
elapsed
|
|
2075
|
-
});
|
|
2076
|
-
return true;
|
|
2077
|
-
} else {
|
|
2078
|
-
if (now - lastProgressLogTs >= progressLogInterval) {
|
|
2079
|
-
(_d = this.log) == null ? void 0 : _d.info({
|
|
2080
|
-
msg: "waiting for actors to stop",
|
|
2081
|
-
remainingActors: this.#actors.size,
|
|
2082
|
-
elapsed
|
|
2083
|
-
});
|
|
2084
|
-
lastProgressLogTs = now;
|
|
2085
|
-
}
|
|
2086
|
-
return false;
|
|
2087
|
-
}
|
|
2088
|
-
};
|
|
2089
|
-
if (checkActors()) {
|
|
2090
|
-
(_a = this.log) == null ? void 0 : _a.debug({
|
|
2091
|
-
msg: "actors check completed immediately"
|
|
2092
|
-
});
|
|
2093
|
-
resolve();
|
|
2094
|
-
return;
|
|
2095
|
-
}
|
|
2096
|
-
(_b = this.log) == null ? void 0 : _b.debug({
|
|
2097
|
-
msg: "starting actor wait interval",
|
|
2098
|
-
checkInterval: shutdownCheckInterval
|
|
2099
|
-
});
|
|
2100
|
-
const interval = setInterval(() => {
|
|
2101
|
-
var _a2, _b2;
|
|
2102
|
-
(_a2 = this.log) == null ? void 0 : _a2.debug({
|
|
2103
|
-
msg: "actor wait interval tick",
|
|
2104
|
-
actorCount: this.#actors.size
|
|
2105
|
-
});
|
|
2106
|
-
if (checkActors()) {
|
|
2107
|
-
(_b2 = this.log) == null ? void 0 : _b2.debug({
|
|
2108
|
-
msg: "actors check completed, clearing interval"
|
|
2109
|
-
});
|
|
2110
|
-
clearInterval(interval);
|
|
2111
|
-
resolve();
|
|
2112
|
-
}
|
|
2113
|
-
}, shutdownCheckInterval);
|
|
2114
|
-
});
|
|
2115
|
-
}
|
|
2116
|
-
// MARK: Networking
|
|
2117
|
-
get pegboardEndpoint() {
|
|
2118
|
-
return this.#config.pegboardEndpoint || this.#config.endpoint;
|
|
2119
|
-
}
|
|
2120
|
-
get pegboardUrl() {
|
|
2121
|
-
const wsEndpoint = this.pegboardEndpoint.replace("http://", "ws://").replace("https://", "wss://");
|
|
2122
|
-
const baseUrl = wsEndpoint.endsWith("/") ? wsEndpoint.slice(0, -1) : wsEndpoint;
|
|
2123
|
-
return `${baseUrl}/runners/connect?protocol_version=${PROTOCOL_VERSION}&namespace=${encodeURIComponent(this.#config.namespace)}&runner_key=${encodeURIComponent(this.#config.runnerKey)}`;
|
|
2124
|
-
}
|
|
2125
|
-
// MARK: Runner protocol
|
|
2126
|
-
async #openPegboardWebSocket() {
|
|
2127
|
-
var _a;
|
|
2128
|
-
const protocols = ["rivet"];
|
|
2129
|
-
if (this.config.token)
|
|
2130
|
-
protocols.push(`rivet_token.${this.config.token}`);
|
|
2131
|
-
const WS = await importWebSocket();
|
|
2132
|
-
const ws = new WS(this.pegboardUrl, protocols);
|
|
2133
|
-
this.__pegboardWebSocket = ws;
|
|
2134
|
-
(_a = this.log) == null ? void 0 : _a.info({
|
|
2135
|
-
msg: "connecting",
|
|
2136
|
-
endpoint: this.pegboardEndpoint,
|
|
2137
|
-
namespace: this.#config.namespace,
|
|
2138
|
-
runnerKey: this.#config.runnerKey,
|
|
2139
|
-
hasToken: !!this.config.token
|
|
2140
|
-
});
|
|
2141
|
-
ws.addEventListener("open", () => {
|
|
2142
|
-
var _a2, _b;
|
|
2143
|
-
if (this.#reconnectAttempt > 0) {
|
|
2144
|
-
(_a2 = this.log) == null ? void 0 : _a2.info({
|
|
2145
|
-
msg: "runner reconnected",
|
|
2146
|
-
namespace: this.#config.namespace,
|
|
2147
|
-
runnerName: this.#config.runnerName,
|
|
2148
|
-
reconnectAttempt: this.#reconnectAttempt
|
|
2149
|
-
});
|
|
2150
|
-
} else {
|
|
2151
|
-
(_b = this.log) == null ? void 0 : _b.debug({
|
|
2152
|
-
msg: "runner connected",
|
|
2153
|
-
namespace: this.#config.namespace,
|
|
2154
|
-
runnerName: this.#config.runnerName
|
|
2155
|
-
});
|
|
2156
|
-
}
|
|
2157
|
-
this.#reconnectAttempt = 0;
|
|
2158
|
-
if (this.#reconnectTimeout) {
|
|
2159
|
-
clearTimeout(this.#reconnectTimeout);
|
|
2160
|
-
this.#reconnectTimeout = void 0;
|
|
2161
|
-
}
|
|
2162
|
-
if (this.#runnerLostTimeout) {
|
|
2163
|
-
clearTimeout(this.#runnerLostTimeout);
|
|
2164
|
-
this.#runnerLostTimeout = void 0;
|
|
2165
|
-
}
|
|
2166
|
-
const init = {
|
|
2167
|
-
name: this.#config.runnerName,
|
|
2168
|
-
version: this.#config.version,
|
|
2169
|
-
totalSlots: this.#config.totalSlots,
|
|
2170
|
-
lastCommandIdx: this.#lastCommandIdx >= 0 ? BigInt(this.#lastCommandIdx) : null,
|
|
2171
|
-
prepopulateActorNames: new Map(
|
|
2172
|
-
Object.entries(this.#config.prepopulateActorNames).map(
|
|
2173
|
-
([name, data]) => [
|
|
2174
|
-
name,
|
|
2175
|
-
{ metadata: JSON.stringify(data.metadata) }
|
|
2176
|
-
]
|
|
2177
|
-
)
|
|
2178
|
-
),
|
|
2179
|
-
metadata: JSON.stringify(this.#config.metadata)
|
|
2180
|
-
};
|
|
2181
|
-
this.__sendToServer({
|
|
2182
|
-
tag: "ToServerInit",
|
|
2183
|
-
val: init
|
|
2184
|
-
});
|
|
2185
|
-
const pingLoop = setInterval(() => {
|
|
2186
|
-
var _a3;
|
|
2187
|
-
if (ws.readyState === 1) {
|
|
2188
|
-
this.__sendToServer({
|
|
2189
|
-
tag: "ToServerPing",
|
|
2190
|
-
val: {
|
|
2191
|
-
ts: BigInt(Date.now())
|
|
2192
|
-
}
|
|
2193
|
-
});
|
|
2194
|
-
} else {
|
|
2195
|
-
clearInterval(pingLoop);
|
|
2196
|
-
(_a3 = this.log) == null ? void 0 : _a3.info({
|
|
2197
|
-
msg: "WebSocket not open, stopping ping loop"
|
|
2198
|
-
});
|
|
2199
|
-
}
|
|
2200
|
-
}, RUNNER_PING_INTERVAL);
|
|
2201
|
-
this.#pingLoop = pingLoop;
|
|
2202
|
-
const ackInterval = 5 * 60 * 1e3;
|
|
2203
|
-
const ackLoop = setInterval(() => {
|
|
2204
|
-
var _a3;
|
|
2205
|
-
if (ws.readyState === 1) {
|
|
2206
|
-
this.#sendCommandAcknowledgment();
|
|
2207
|
-
} else {
|
|
2208
|
-
clearInterval(ackLoop);
|
|
2209
|
-
(_a3 = this.log) == null ? void 0 : _a3.info({
|
|
2210
|
-
msg: "WebSocket not open, stopping ack loop"
|
|
2211
|
-
});
|
|
2212
|
-
}
|
|
2213
|
-
}, ackInterval);
|
|
2214
|
-
this.#ackInterval = ackLoop;
|
|
2215
|
-
});
|
|
2216
|
-
ws.addEventListener("message", async (ev) => {
|
|
2217
|
-
var _a2, _b, _c, _d, _e, _f;
|
|
2218
|
-
let buf;
|
|
2219
|
-
if (ev.data instanceof Blob) {
|
|
2220
|
-
buf = new Uint8Array(await ev.data.arrayBuffer());
|
|
2221
|
-
} else if (Buffer.isBuffer(ev.data)) {
|
|
2222
|
-
buf = new Uint8Array(ev.data);
|
|
2223
|
-
} else {
|
|
2224
|
-
throw new Error(`expected binary data, got ${typeof ev.data}`);
|
|
2225
|
-
}
|
|
2226
|
-
const message = protocol.decodeToClient(buf);
|
|
2227
|
-
(_a2 = this.log) == null ? void 0 : _a2.debug({
|
|
2228
|
-
msg: "received runner message",
|
|
2229
|
-
data: stringifyToClient(message)
|
|
2230
|
-
});
|
|
2231
|
-
if (message.tag === "ToClientInit") {
|
|
2232
|
-
const init = message.val;
|
|
2233
|
-
if (this.runnerId !== init.runnerId) {
|
|
2234
|
-
this.runnerId = init.runnerId;
|
|
2235
|
-
this.#eventHistory.length = 0;
|
|
2236
|
-
}
|
|
2237
|
-
this.#runnerLostThreshold = ((_b = init.metadata) == null ? void 0 : _b.runnerLostThreshold) ? Number(init.metadata.runnerLostThreshold) : void 0;
|
|
2238
|
-
(_c = this.log) == null ? void 0 : _c.info({
|
|
2239
|
-
msg: "received init",
|
|
2240
|
-
lastEventIdx: init.lastEventIdx,
|
|
2241
|
-
runnerLostThreshold: this.#runnerLostThreshold
|
|
2242
|
-
});
|
|
2243
|
-
this.#processUnsentKvRequests();
|
|
2244
|
-
this.#resendUnacknowledgedEvents(init.lastEventIdx);
|
|
2245
|
-
(_d = this.#tunnel) == null ? void 0 : _d.resendBufferedEvents();
|
|
2246
|
-
this.#config.onConnected();
|
|
2247
|
-
} else if (message.tag === "ToClientCommands") {
|
|
2248
|
-
const commands = message.val;
|
|
2249
|
-
this.#handleCommands(commands);
|
|
2250
|
-
} else if (message.tag === "ToClientAckEvents") {
|
|
2251
|
-
this.#handleAckEvents(message.val);
|
|
2252
|
-
} else if (message.tag === "ToClientKvResponse") {
|
|
2253
|
-
const kvResponse = message.val;
|
|
2254
|
-
this.#handleKvResponse(kvResponse);
|
|
2255
|
-
} else if (message.tag === "ToClientTunnelMessage") {
|
|
2256
|
-
(_e = this.#tunnel) == null ? void 0 : _e.handleTunnelMessage(message.val);
|
|
2257
|
-
} else if (message.tag === "ToClientClose") {
|
|
2258
|
-
(_f = this.#tunnel) == null ? void 0 : _f.shutdown();
|
|
2259
|
-
ws.close(1e3, "manual closure");
|
|
2260
|
-
} else {
|
|
2261
|
-
unreachable(message);
|
|
2262
|
-
}
|
|
2263
|
-
});
|
|
2264
|
-
ws.addEventListener("error", (ev) => {
|
|
2265
|
-
var _a2, _b;
|
|
2266
|
-
(_a2 = this.log) == null ? void 0 : _a2.error({
|
|
2267
|
-
msg: `WebSocket error: ${ev.error}`
|
|
2268
|
-
});
|
|
2269
|
-
if (!this.#shutdown) {
|
|
2270
|
-
if (!this.#runnerLostTimeout && this.#runnerLostThreshold && this.#runnerLostThreshold > 0) {
|
|
2271
|
-
(_b = this.log) == null ? void 0 : _b.info({
|
|
2272
|
-
msg: "starting runner lost timeout",
|
|
2273
|
-
seconds: this.#runnerLostThreshold / 1e3
|
|
2274
|
-
});
|
|
2275
|
-
this.#runnerLostTimeout = setTimeout(() => {
|
|
2276
|
-
this.#stopAllActors();
|
|
2277
|
-
}, this.#runnerLostThreshold);
|
|
2278
|
-
}
|
|
2279
|
-
this.#scheduleReconnect();
|
|
2280
|
-
}
|
|
2281
|
-
});
|
|
2282
|
-
ws.addEventListener("close", async (ev) => {
|
|
2283
|
-
var _a2, _b, _c, _d;
|
|
2284
|
-
const closeError = parseWebSocketCloseReason(ev.reason);
|
|
2285
|
-
if ((closeError == null ? void 0 : closeError.group) === "ws" && (closeError == null ? void 0 : closeError.error) === "eviction") {
|
|
2286
|
-
(_a2 = this.log) == null ? void 0 : _a2.info("runner websocket evicted");
|
|
2287
|
-
this.#config.onDisconnected(ev.code, ev.reason);
|
|
2288
|
-
await this.shutdown(true);
|
|
2289
|
-
} else {
|
|
2290
|
-
if ((closeError == null ? void 0 : closeError.group) === "pegboard" && (closeError == null ? void 0 : closeError.error) === "runner_shutdown") {
|
|
2291
|
-
(_b = this.log) == null ? void 0 : _b.info("runner shutdown");
|
|
2292
|
-
} else {
|
|
2293
|
-
(_c = this.log) == null ? void 0 : _c.warn({
|
|
2294
|
-
msg: "runner disconnected",
|
|
2295
|
-
code: ev.code,
|
|
2296
|
-
reason: ev.reason.toString(),
|
|
2297
|
-
closeError
|
|
2298
|
-
});
|
|
2299
|
-
}
|
|
2300
|
-
this.#config.onDisconnected(ev.code, ev.reason);
|
|
2301
|
-
}
|
|
2302
|
-
if (this.#pingLoop) {
|
|
2303
|
-
clearInterval(this.#pingLoop);
|
|
2304
|
-
this.#pingLoop = void 0;
|
|
2305
|
-
}
|
|
2306
|
-
if (this.#ackInterval) {
|
|
2307
|
-
clearInterval(this.#ackInterval);
|
|
2308
|
-
this.#ackInterval = void 0;
|
|
2309
|
-
}
|
|
2310
|
-
if (!this.#shutdown) {
|
|
2311
|
-
if (!this.#runnerLostTimeout && this.#runnerLostThreshold && this.#runnerLostThreshold > 0) {
|
|
2312
|
-
(_d = this.log) == null ? void 0 : _d.info({
|
|
2313
|
-
msg: "starting runner lost timeout",
|
|
2314
|
-
seconds: this.#runnerLostThreshold / 1e3
|
|
2315
|
-
});
|
|
2316
|
-
this.#runnerLostTimeout = setTimeout(() => {
|
|
2317
|
-
this.#stopAllActors();
|
|
2318
|
-
}, this.#runnerLostThreshold);
|
|
2319
|
-
}
|
|
2320
|
-
this.#scheduleReconnect();
|
|
2321
|
-
}
|
|
2322
|
-
});
|
|
2323
|
-
}
|
|
2324
|
-
#handleCommands(commands) {
|
|
2325
|
-
var _a;
|
|
2326
|
-
(_a = this.log) == null ? void 0 : _a.info({
|
|
2327
|
-
msg: "received commands",
|
|
2328
|
-
commandCount: commands.length
|
|
2329
|
-
});
|
|
2330
|
-
for (const commandWrapper of commands) {
|
|
2331
|
-
if (commandWrapper.inner.tag === "CommandStartActor") {
|
|
2332
|
-
this.#handleCommandStartActor(commandWrapper);
|
|
2333
|
-
} else if (commandWrapper.inner.tag === "CommandStopActor") {
|
|
2334
|
-
this.#handleCommandStopActor(commandWrapper);
|
|
2335
|
-
} else {
|
|
2336
|
-
unreachable(commandWrapper.inner);
|
|
2337
|
-
}
|
|
2338
|
-
this.#lastCommandIdx = Number(commandWrapper.index);
|
|
2339
|
-
}
|
|
2340
|
-
}
|
|
2341
|
-
#handleAckEvents(ack) {
|
|
2342
|
-
var _a;
|
|
2343
|
-
const lastAckedIdx = ack.lastEventIdx;
|
|
2344
|
-
const originalLength = this.#eventHistory.length;
|
|
2345
|
-
this.#eventHistory = this.#eventHistory.filter(
|
|
2346
|
-
(event) => event.index > lastAckedIdx
|
|
2347
|
-
);
|
|
2348
|
-
const prunedCount = originalLength - this.#eventHistory.length;
|
|
2349
|
-
if (prunedCount > 0) {
|
|
2350
|
-
(_a = this.log) == null ? void 0 : _a.info({
|
|
2351
|
-
msg: "pruned acknowledged events",
|
|
2352
|
-
lastAckedIdx: lastAckedIdx.toString(),
|
|
2353
|
-
prunedCount
|
|
2354
|
-
});
|
|
2355
|
-
}
|
|
2356
|
-
if (this.#eventHistory.length <= EVENT_BACKLOG_WARN_THRESHOLD) {
|
|
2357
|
-
this.#eventBacklogWarned = false;
|
|
2358
|
-
}
|
|
2359
|
-
}
|
|
2360
|
-
/** Track events to send to the server in case we need to resend it on disconnect. */
|
|
2361
|
-
#recordEvent(eventWrapper) {
|
|
2362
|
-
var _a;
|
|
2363
|
-
this.#eventHistory.push(eventWrapper);
|
|
2364
|
-
if (this.#eventHistory.length > EVENT_BACKLOG_WARN_THRESHOLD && !this.#eventBacklogWarned) {
|
|
2365
|
-
this.#eventBacklogWarned = true;
|
|
2366
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
2367
|
-
msg: "unacknowledged event backlog exceeds threshold",
|
|
2368
|
-
backlogSize: this.#eventHistory.length,
|
|
2369
|
-
threshold: EVENT_BACKLOG_WARN_THRESHOLD
|
|
2370
|
-
});
|
|
2371
|
-
}
|
|
2372
|
-
}
|
|
2373
|
-
async #handleCommandStartActor(commandWrapper) {
|
|
2374
|
-
var _a, _b, _c, _d;
|
|
2375
|
-
if (!this.#tunnel) throw new Error("missing tunnel on actor start");
|
|
2376
|
-
const startCommand = commandWrapper.inner.val;
|
|
2377
|
-
const actorId = startCommand.actorId;
|
|
2378
|
-
const generation = startCommand.generation;
|
|
2379
|
-
const config = startCommand.config;
|
|
2380
|
-
const actorConfig = {
|
|
2381
|
-
name: config.name,
|
|
2382
|
-
key: config.key,
|
|
2383
|
-
createTs: config.createTs,
|
|
2384
|
-
input: config.input ? new Uint8Array(config.input) : null
|
|
2385
|
-
};
|
|
2386
|
-
const instance = new RunnerActor(
|
|
2387
|
-
actorId,
|
|
2388
|
-
generation,
|
|
2389
|
-
actorConfig,
|
|
2390
|
-
startCommand.hibernatingRequests
|
|
2391
|
-
);
|
|
2392
|
-
const existingActor = this.#actors.get(actorId);
|
|
2393
|
-
if (existingActor) {
|
|
2394
|
-
(_a = this.log) == null ? void 0 : _a.warn({
|
|
2395
|
-
msg: "replacing existing actor in actors map",
|
|
2396
|
-
actorId,
|
|
2397
|
-
existingGeneration: existingActor.generation,
|
|
2398
|
-
newGeneration: generation,
|
|
2399
|
-
existingPendingRequests: existingActor.pendingRequests.length
|
|
2400
|
-
});
|
|
2401
|
-
}
|
|
2402
|
-
this.#actors.set(actorId, instance);
|
|
2403
|
-
for (const hr of startCommand.hibernatingRequests) {
|
|
2404
|
-
this.#tunnel.addRequestToActor(hr.gatewayId, hr.requestId, actorId);
|
|
2405
|
-
}
|
|
2406
|
-
(_b = this.log) == null ? void 0 : _b.info({
|
|
2407
|
-
msg: "created actor",
|
|
2408
|
-
actors: this.#actors.size,
|
|
2409
|
-
actorId,
|
|
2410
|
-
name: config.name,
|
|
2411
|
-
key: config.key,
|
|
2412
|
-
generation,
|
|
2413
|
-
hibernatingRequests: startCommand.hibernatingRequests.length
|
|
2414
|
-
});
|
|
2415
|
-
this.#sendActorStateUpdate(actorId, generation, "running");
|
|
2416
|
-
try {
|
|
2417
|
-
(_c = this.log) == null ? void 0 : _c.debug({
|
|
2418
|
-
msg: "calling onActorStart",
|
|
2419
|
-
actorId,
|
|
2420
|
-
generation
|
|
2421
|
-
});
|
|
2422
|
-
await this.#config.onActorStart(actorId, generation, actorConfig);
|
|
2423
|
-
instance.actorStartPromise.resolve();
|
|
2424
|
-
} catch (err) {
|
|
2425
|
-
(_d = this.log) == null ? void 0 : _d.error({
|
|
2426
|
-
msg: "error starting runner actor",
|
|
2427
|
-
actorId,
|
|
2428
|
-
err
|
|
2429
|
-
});
|
|
2430
|
-
instance.actorStartPromise.reject(err);
|
|
2431
|
-
await this.forceStopActor(actorId, generation);
|
|
2432
|
-
}
|
|
2433
|
-
}
|
|
2434
|
-
async #handleCommandStopActor(commandWrapper) {
|
|
2435
|
-
const stopCommand = commandWrapper.inner.val;
|
|
2436
|
-
const actorId = stopCommand.actorId;
|
|
2437
|
-
const generation = stopCommand.generation;
|
|
2438
|
-
await this.forceStopActor(actorId, generation);
|
|
2439
|
-
}
|
|
2440
|
-
#sendActorIntent(actorId, generation, intentType) {
|
|
2441
|
-
let actorIntent;
|
|
2442
|
-
if (intentType === "sleep") {
|
|
2443
|
-
actorIntent = { tag: "ActorIntentSleep", val: null };
|
|
2444
|
-
} else if (intentType === "stop") {
|
|
2445
|
-
actorIntent = {
|
|
2446
|
-
tag: "ActorIntentStop",
|
|
2447
|
-
val: null
|
|
2448
|
-
};
|
|
2449
|
-
} else {
|
|
2450
|
-
unreachable(intentType);
|
|
2451
|
-
}
|
|
2452
|
-
const intentEvent = {
|
|
2453
|
-
actorId,
|
|
2454
|
-
generation,
|
|
2455
|
-
intent: actorIntent
|
|
2456
|
-
};
|
|
2457
|
-
const eventIndex = this.#nextEventIdx++;
|
|
2458
|
-
const eventWrapper = {
|
|
2459
|
-
index: eventIndex,
|
|
2460
|
-
inner: {
|
|
2461
|
-
tag: "EventActorIntent",
|
|
2462
|
-
val: intentEvent
|
|
2463
|
-
}
|
|
2464
|
-
};
|
|
2465
|
-
this.#recordEvent(eventWrapper);
|
|
2466
|
-
this.__sendToServer({
|
|
2467
|
-
tag: "ToServerEvents",
|
|
2468
|
-
val: [eventWrapper]
|
|
2469
|
-
});
|
|
2470
|
-
}
|
|
2471
|
-
#sendActorStateUpdate(actorId, generation, stateType) {
|
|
2472
|
-
let actorState;
|
|
2473
|
-
if (stateType === "running") {
|
|
2474
|
-
actorState = { tag: "ActorStateRunning", val: null };
|
|
2475
|
-
} else if (stateType === "stopped") {
|
|
2476
|
-
actorState = {
|
|
2477
|
-
tag: "ActorStateStopped",
|
|
2478
|
-
val: {
|
|
2479
|
-
code: protocol.StopCode.Ok,
|
|
2480
|
-
message: null
|
|
2481
|
-
}
|
|
2482
|
-
};
|
|
2483
|
-
} else {
|
|
2484
|
-
unreachable(stateType);
|
|
2485
|
-
}
|
|
2486
|
-
const stateUpdateEvent = {
|
|
2487
|
-
actorId,
|
|
2488
|
-
generation,
|
|
2489
|
-
state: actorState
|
|
2490
|
-
};
|
|
2491
|
-
const eventIndex = this.#nextEventIdx++;
|
|
2492
|
-
const eventWrapper = {
|
|
2493
|
-
index: eventIndex,
|
|
2494
|
-
inner: {
|
|
2495
|
-
tag: "EventActorStateUpdate",
|
|
2496
|
-
val: stateUpdateEvent
|
|
2497
|
-
}
|
|
2498
|
-
};
|
|
2499
|
-
this.#recordEvent(eventWrapper);
|
|
2500
|
-
this.__sendToServer({
|
|
2501
|
-
tag: "ToServerEvents",
|
|
2502
|
-
val: [eventWrapper]
|
|
2503
|
-
});
|
|
2504
|
-
}
|
|
2505
|
-
#sendCommandAcknowledgment() {
|
|
2506
|
-
if (this.#lastCommandIdx < 0) {
|
|
2507
|
-
return;
|
|
2508
|
-
}
|
|
2509
|
-
this.__sendToServer({
|
|
2510
|
-
tag: "ToServerAckCommands",
|
|
2511
|
-
val: {
|
|
2512
|
-
lastCommandIdx: BigInt(this.#lastCommandIdx)
|
|
2513
|
-
}
|
|
2514
|
-
});
|
|
2515
|
-
}
|
|
2516
|
-
#handleKvResponse(response) {
|
|
2517
|
-
var _a;
|
|
2518
|
-
const requestId = response.requestId;
|
|
2519
|
-
const request = this.#kvRequests.get(requestId);
|
|
2520
|
-
if (!request) {
|
|
2521
|
-
(_a = this.log) == null ? void 0 : _a.error({
|
|
2522
|
-
msg: "received kv response for unknown request id",
|
|
2523
|
-
requestId
|
|
2524
|
-
});
|
|
2525
|
-
return;
|
|
2526
|
-
}
|
|
2527
|
-
this.#kvRequests.delete(requestId);
|
|
2528
|
-
if (response.data.tag === "KvErrorResponse") {
|
|
2529
|
-
request.reject(
|
|
2530
|
-
new Error(response.data.val.message || "Unknown KV error")
|
|
2531
|
-
);
|
|
2532
|
-
} else {
|
|
2533
|
-
request.resolve(response.data.val);
|
|
2534
|
-
}
|
|
2535
|
-
}
|
|
2536
|
-
#parseGetResponseSimple(response, requestedKeys) {
|
|
2537
|
-
const responseKeys = [];
|
|
2538
|
-
const responseValues = [];
|
|
2539
|
-
for (const key of response.keys) {
|
|
2540
|
-
responseKeys.push(new Uint8Array(key));
|
|
2541
|
-
}
|
|
2542
|
-
for (const value of response.values) {
|
|
2543
|
-
responseValues.push(new Uint8Array(value));
|
|
2544
|
-
}
|
|
2545
|
-
const result = [];
|
|
2546
|
-
for (const requestedKey of requestedKeys) {
|
|
2547
|
-
let found = false;
|
|
2548
|
-
for (let i = 0; i < responseKeys.length; i++) {
|
|
2549
|
-
if (this.#keysEqual(requestedKey, responseKeys[i])) {
|
|
2550
|
-
result.push(responseValues[i]);
|
|
2551
|
-
found = true;
|
|
2552
|
-
break;
|
|
2553
|
-
}
|
|
2554
|
-
}
|
|
2555
|
-
if (!found) {
|
|
2556
|
-
result.push(null);
|
|
2557
|
-
}
|
|
2558
|
-
}
|
|
2559
|
-
return result;
|
|
2560
|
-
}
|
|
2561
|
-
#keysEqual(key1, key2) {
|
|
2562
|
-
if (key1.length !== key2.length) return false;
|
|
2563
|
-
for (let i = 0; i < key1.length; i++) {
|
|
2564
|
-
if (key1[i] !== key2[i]) return false;
|
|
2565
|
-
}
|
|
2566
|
-
return true;
|
|
2567
|
-
}
|
|
2568
|
-
//#parseGetResponse(response: protocol.KvGetResponse) {
|
|
2569
|
-
// const keys: string[] = [];
|
|
2570
|
-
// const values: Uint8Array[] = [];
|
|
2571
|
-
// const metadata: { version: Uint8Array; createTs: bigint }[] = [];
|
|
2572
|
-
//
|
|
2573
|
-
// for (const key of response.keys) {
|
|
2574
|
-
// keys.push(new TextDecoder().decode(key));
|
|
2575
|
-
// }
|
|
2576
|
-
//
|
|
2577
|
-
// for (const value of response.values) {
|
|
2578
|
-
// values.push(new Uint8Array(value));
|
|
2579
|
-
// }
|
|
2580
|
-
//
|
|
2581
|
-
// for (const meta of response.metadata) {
|
|
2582
|
-
// metadata.push({
|
|
2583
|
-
// version: new Uint8Array(meta.version),
|
|
2584
|
-
// createTs: meta.createTs,
|
|
2585
|
-
// });
|
|
2586
|
-
// }
|
|
2587
|
-
//
|
|
2588
|
-
// return { keys, values, metadata };
|
|
2589
|
-
//}
|
|
2590
|
-
#parseListResponseSimple(response) {
|
|
2591
|
-
const result = [];
|
|
2592
|
-
for (let i = 0; i < response.keys.length; i++) {
|
|
2593
|
-
const key = response.keys[i];
|
|
2594
|
-
const value = response.values[i];
|
|
2595
|
-
if (key && value) {
|
|
2596
|
-
const keyBytes = new Uint8Array(key);
|
|
2597
|
-
const valueBytes = new Uint8Array(value);
|
|
2598
|
-
result.push([keyBytes, valueBytes]);
|
|
2599
|
-
}
|
|
2600
|
-
}
|
|
2601
|
-
return result;
|
|
2602
|
-
}
|
|
2603
|
-
//#parseListResponse(response: protocol.KvListResponse) {
|
|
2604
|
-
// const keys: string[] = [];
|
|
2605
|
-
// const values: Uint8Array[] = [];
|
|
2606
|
-
// const metadata: { version: Uint8Array; createTs: bigint }[] = [];
|
|
2607
|
-
//
|
|
2608
|
-
// for (const key of response.keys) {
|
|
2609
|
-
// keys.push(new TextDecoder().decode(key));
|
|
2610
|
-
// }
|
|
2611
|
-
//
|
|
2612
|
-
// for (const value of response.values) {
|
|
2613
|
-
// values.push(new Uint8Array(value));
|
|
2614
|
-
// }
|
|
2615
|
-
//
|
|
2616
|
-
// for (const meta of response.metadata) {
|
|
2617
|
-
// metadata.push({
|
|
2618
|
-
// version: new Uint8Array(meta.version),
|
|
2619
|
-
// createTs: meta.createTs,
|
|
2620
|
-
// });
|
|
2621
|
-
// }
|
|
2622
|
-
//
|
|
2623
|
-
// return { keys, values, metadata };
|
|
2624
|
-
//}
|
|
2625
|
-
// MARK: KV Operations
|
|
2626
|
-
async kvGet(actorId, keys) {
|
|
2627
|
-
const kvKeys = keys.map(
|
|
2628
|
-
(key) => key.buffer.slice(
|
|
2629
|
-
key.byteOffset,
|
|
2630
|
-
key.byteOffset + key.byteLength
|
|
2631
|
-
)
|
|
2632
|
-
);
|
|
2633
|
-
const requestData = {
|
|
2634
|
-
tag: "KvGetRequest",
|
|
2635
|
-
val: { keys: kvKeys }
|
|
2636
|
-
};
|
|
2637
|
-
const response = await this.#sendKvRequest(actorId, requestData);
|
|
2638
|
-
return this.#parseGetResponseSimple(response, keys);
|
|
2639
|
-
}
|
|
2640
|
-
async kvListAll(actorId, options) {
|
|
2641
|
-
const requestData = {
|
|
2642
|
-
tag: "KvListRequest",
|
|
2643
|
-
val: {
|
|
2644
|
-
query: { tag: "KvListAllQuery", val: null },
|
|
2645
|
-
reverse: (options == null ? void 0 : options.reverse) || null,
|
|
2646
|
-
limit: (options == null ? void 0 : options.limit) !== void 0 ? BigInt(options.limit) : null
|
|
2647
|
-
}
|
|
2648
|
-
};
|
|
2649
|
-
const response = await this.#sendKvRequest(actorId, requestData);
|
|
2650
|
-
return this.#parseListResponseSimple(response);
|
|
2651
|
-
}
|
|
2652
|
-
async kvListRange(actorId, start, end, exclusive, options) {
|
|
2653
|
-
const startKey = start.buffer.slice(
|
|
2654
|
-
start.byteOffset,
|
|
2655
|
-
start.byteOffset + start.byteLength
|
|
2656
|
-
);
|
|
2657
|
-
const endKey = end.buffer.slice(
|
|
2658
|
-
end.byteOffset,
|
|
2659
|
-
end.byteOffset + end.byteLength
|
|
2660
|
-
);
|
|
2661
|
-
const requestData = {
|
|
2662
|
-
tag: "KvListRequest",
|
|
2663
|
-
val: {
|
|
2664
|
-
query: {
|
|
2665
|
-
tag: "KvListRangeQuery",
|
|
2666
|
-
val: {
|
|
2667
|
-
start: startKey,
|
|
2668
|
-
end: endKey,
|
|
2669
|
-
exclusive: exclusive || false
|
|
2670
|
-
}
|
|
2671
|
-
},
|
|
2672
|
-
reverse: (options == null ? void 0 : options.reverse) || null,
|
|
2673
|
-
limit: (options == null ? void 0 : options.limit) !== void 0 ? BigInt(options.limit) : null
|
|
2674
|
-
}
|
|
2675
|
-
};
|
|
2676
|
-
const response = await this.#sendKvRequest(actorId, requestData);
|
|
2677
|
-
return this.#parseListResponseSimple(response);
|
|
2678
|
-
}
|
|
2679
|
-
async kvListPrefix(actorId, prefix, options) {
|
|
2680
|
-
const prefixKey = prefix.buffer.slice(
|
|
2681
|
-
prefix.byteOffset,
|
|
2682
|
-
prefix.byteOffset + prefix.byteLength
|
|
2683
|
-
);
|
|
2684
|
-
const requestData = {
|
|
2685
|
-
tag: "KvListRequest",
|
|
2686
|
-
val: {
|
|
2687
|
-
query: {
|
|
2688
|
-
tag: "KvListPrefixQuery",
|
|
2689
|
-
val: { key: prefixKey }
|
|
2690
|
-
},
|
|
2691
|
-
reverse: (options == null ? void 0 : options.reverse) || null,
|
|
2692
|
-
limit: (options == null ? void 0 : options.limit) !== void 0 ? BigInt(options.limit) : null
|
|
2693
|
-
}
|
|
2694
|
-
};
|
|
2695
|
-
const response = await this.#sendKvRequest(actorId, requestData);
|
|
2696
|
-
return this.#parseListResponseSimple(response);
|
|
2697
|
-
}
|
|
2698
|
-
async kvPut(actorId, entries) {
|
|
2699
|
-
const keys = entries.map(
|
|
2700
|
-
([key, _value]) => key.buffer.slice(
|
|
2701
|
-
key.byteOffset,
|
|
2702
|
-
key.byteOffset + key.byteLength
|
|
2703
|
-
)
|
|
2704
|
-
);
|
|
2705
|
-
const values = entries.map(
|
|
2706
|
-
([_key, value]) => value.buffer.slice(
|
|
2707
|
-
value.byteOffset,
|
|
2708
|
-
value.byteOffset + value.byteLength
|
|
2709
|
-
)
|
|
2710
|
-
);
|
|
2711
|
-
const requestData = {
|
|
2712
|
-
tag: "KvPutRequest",
|
|
2713
|
-
val: { keys, values }
|
|
2714
|
-
};
|
|
2715
|
-
await this.#sendKvRequest(actorId, requestData);
|
|
2716
|
-
}
|
|
2717
|
-
async kvDelete(actorId, keys) {
|
|
2718
|
-
const kvKeys = keys.map(
|
|
2719
|
-
(key) => key.buffer.slice(
|
|
2720
|
-
key.byteOffset,
|
|
2721
|
-
key.byteOffset + key.byteLength
|
|
2722
|
-
)
|
|
2723
|
-
);
|
|
2724
|
-
const requestData = {
|
|
2725
|
-
tag: "KvDeleteRequest",
|
|
2726
|
-
val: { keys: kvKeys }
|
|
2727
|
-
};
|
|
2728
|
-
await this.#sendKvRequest(actorId, requestData);
|
|
2729
|
-
}
|
|
2730
|
-
async kvDrop(actorId) {
|
|
2731
|
-
const requestData = {
|
|
2732
|
-
tag: "KvDropRequest",
|
|
2733
|
-
val: null
|
|
2734
|
-
};
|
|
2735
|
-
await this.#sendKvRequest(actorId, requestData);
|
|
2736
|
-
}
|
|
2737
|
-
// MARK: Alarm Operations
|
|
2738
|
-
setAlarm(actorId, alarmTs, generation) {
|
|
2739
|
-
const actor = this.getActor(actorId, generation);
|
|
2740
|
-
if (!actor) return;
|
|
2741
|
-
const alarmEvent = {
|
|
2742
|
-
actorId,
|
|
2743
|
-
generation: actor.generation,
|
|
2744
|
-
alarmTs: alarmTs !== null ? BigInt(alarmTs) : null
|
|
2745
|
-
};
|
|
2746
|
-
const eventIndex = this.#nextEventIdx++;
|
|
2747
|
-
const eventWrapper = {
|
|
2748
|
-
index: eventIndex,
|
|
2749
|
-
inner: {
|
|
2750
|
-
tag: "EventActorSetAlarm",
|
|
2751
|
-
val: alarmEvent
|
|
2752
|
-
}
|
|
2753
|
-
};
|
|
2754
|
-
this.#recordEvent(eventWrapper);
|
|
2755
|
-
this.__sendToServer({
|
|
2756
|
-
tag: "ToServerEvents",
|
|
2757
|
-
val: [eventWrapper]
|
|
2758
|
-
});
|
|
2759
|
-
}
|
|
2760
|
-
clearAlarm(actorId, generation) {
|
|
2761
|
-
this.setAlarm(actorId, null, generation);
|
|
2762
|
-
}
|
|
2763
|
-
#sendKvRequest(actorId, requestData) {
|
|
2764
|
-
return new Promise((resolve, reject) => {
|
|
2765
|
-
const requestId = this.#nextKvRequestId++;
|
|
2766
|
-
const requestEntry = {
|
|
2767
|
-
actorId,
|
|
2768
|
-
data: requestData,
|
|
2769
|
-
resolve,
|
|
2770
|
-
reject,
|
|
2771
|
-
sent: false,
|
|
2772
|
-
timestamp: Date.now()
|
|
2773
|
-
};
|
|
2774
|
-
this.#kvRequests.set(requestId, requestEntry);
|
|
2775
|
-
if (this.__webSocketReady()) {
|
|
2776
|
-
this.#sendSingleKvRequest(requestId);
|
|
2777
|
-
}
|
|
2778
|
-
});
|
|
2779
|
-
}
|
|
2780
|
-
#sendSingleKvRequest(requestId) {
|
|
2781
|
-
const request = this.#kvRequests.get(requestId);
|
|
2782
|
-
if (!request || request.sent) return;
|
|
2783
|
-
try {
|
|
2784
|
-
const kvRequest = {
|
|
2785
|
-
actorId: request.actorId,
|
|
2786
|
-
requestId,
|
|
2787
|
-
data: request.data
|
|
2788
|
-
};
|
|
2789
|
-
this.__sendToServer({
|
|
2790
|
-
tag: "ToServerKvRequest",
|
|
2791
|
-
val: kvRequest
|
|
2792
|
-
});
|
|
2793
|
-
request.sent = true;
|
|
2794
|
-
request.timestamp = Date.now();
|
|
2795
|
-
} catch (error) {
|
|
2796
|
-
this.#kvRequests.delete(requestId);
|
|
2797
|
-
request.reject(error);
|
|
2798
|
-
}
|
|
2799
|
-
}
|
|
2800
|
-
#processUnsentKvRequests() {
|
|
2801
|
-
if (!this.__webSocketReady()) {
|
|
2802
|
-
return;
|
|
2803
|
-
}
|
|
2804
|
-
let processedCount = 0;
|
|
2805
|
-
for (const [requestId, request] of this.#kvRequests.entries()) {
|
|
2806
|
-
if (!request.sent) {
|
|
2807
|
-
this.#sendSingleKvRequest(requestId);
|
|
2808
|
-
processedCount++;
|
|
2809
|
-
}
|
|
2810
|
-
}
|
|
2811
|
-
if (processedCount > 0) {
|
|
2812
|
-
}
|
|
2813
|
-
}
|
|
2814
|
-
/** Asserts WebSocket exists and is ready. */
|
|
2815
|
-
__webSocketReady() {
|
|
2816
|
-
return !!this.__pegboardWebSocket && this.__pegboardWebSocket.readyState === 1;
|
|
2817
|
-
}
|
|
2818
|
-
__sendToServer(message) {
|
|
2819
|
-
var _a, _b;
|
|
2820
|
-
(_a = this.log) == null ? void 0 : _a.debug({
|
|
2821
|
-
msg: "sending runner message",
|
|
2822
|
-
data: stringifyToServer(message)
|
|
2823
|
-
});
|
|
2824
|
-
const encoded = protocol.encodeToServer(message);
|
|
2825
|
-
if (this.__webSocketReady()) {
|
|
2826
|
-
this.__pegboardWebSocket.send(encoded);
|
|
2827
|
-
} else {
|
|
2828
|
-
(_b = this.log) == null ? void 0 : _b.error({
|
|
2829
|
-
msg: "WebSocket not available or not open for sending data"
|
|
2830
|
-
});
|
|
2831
|
-
}
|
|
2832
|
-
}
|
|
2833
|
-
sendHibernatableWebSocketMessageAck(gatewayId, requestId, index) {
|
|
2834
|
-
if (!this.#tunnel)
|
|
2835
|
-
throw new Error("missing tunnel to send message ack");
|
|
2836
|
-
this.#tunnel.sendHibernatableWebSocketMessageAck(
|
|
2837
|
-
gatewayId,
|
|
2838
|
-
requestId,
|
|
2839
|
-
index
|
|
2840
|
-
);
|
|
2841
|
-
}
|
|
2842
|
-
/**
|
|
2843
|
-
* Restores hibernatable WebSocket connections for an actor.
|
|
2844
|
-
*
|
|
2845
|
-
* This method should be called at the end of `onActorStart` after the
|
|
2846
|
-
* actor instance is fully initialized.
|
|
2847
|
-
*
|
|
2848
|
-
* This method will:
|
|
2849
|
-
* - Restore all provided hibernatable WebSocket connections
|
|
2850
|
-
* - Attach event listeners to the restored WebSockets
|
|
2851
|
-
* - Close any WebSocket connections that failed to restore
|
|
2852
|
-
*
|
|
2853
|
-
* The provided metadata list should include all hibernatable WebSockets
|
|
2854
|
-
* that were persisted for this actor. The gateway will automatically
|
|
2855
|
-
* close any connections that are not restored (i.e., not included in
|
|
2856
|
-
* this list).
|
|
2857
|
-
*
|
|
2858
|
-
* **Important:** This method must be called after `onActorStart` completes
|
|
2859
|
-
* and before marking the actor as "ready" to ensure all hibernatable
|
|
2860
|
-
* connections are fully restored.
|
|
2861
|
-
*
|
|
2862
|
-
* @param actorId - The ID of the actor to restore connections for
|
|
2863
|
-
* @param metaEntries - Array of hibernatable WebSocket metadata to restore
|
|
2864
|
-
*/
|
|
2865
|
-
async restoreHibernatingRequests(actorId, metaEntries) {
|
|
2866
|
-
if (!this.#tunnel)
|
|
2867
|
-
throw new Error("missing tunnel to restore hibernating requests");
|
|
2868
|
-
await this.#tunnel.restoreHibernatingRequests(actorId, metaEntries);
|
|
2869
|
-
}
|
|
2870
|
-
getServerlessInitPacket() {
|
|
2871
|
-
if (!this.runnerId) return void 0;
|
|
2872
|
-
const data = protocol.encodeToServerlessServer({
|
|
2873
|
-
tag: "ToServerlessServerInit",
|
|
2874
|
-
val: {
|
|
2875
|
-
runnerId: this.runnerId
|
|
2876
|
-
}
|
|
2877
|
-
});
|
|
2878
|
-
const buffer = Buffer.alloc(data.length + 2);
|
|
2879
|
-
buffer.writeUInt16LE(PROTOCOL_VERSION, 0);
|
|
2880
|
-
Buffer.from(data).copy(buffer, 2);
|
|
2881
|
-
return buffer.toString("base64");
|
|
2882
|
-
}
|
|
2883
|
-
#scheduleReconnect() {
|
|
2884
|
-
var _a, _b;
|
|
2885
|
-
if (this.#shutdown) {
|
|
2886
|
-
(_a = this.log) == null ? void 0 : _a.debug({
|
|
2887
|
-
msg: "Runner is shut down, not attempting reconnect"
|
|
2888
|
-
});
|
|
2889
|
-
return;
|
|
2890
|
-
}
|
|
2891
|
-
const delay = calculateBackoff(this.#reconnectAttempt, {
|
|
2892
|
-
initialDelay: 1e3,
|
|
2893
|
-
maxDelay: 3e4,
|
|
2894
|
-
multiplier: 2,
|
|
2895
|
-
jitter: true
|
|
2896
|
-
});
|
|
2897
|
-
(_b = this.log) == null ? void 0 : _b.debug({
|
|
2898
|
-
msg: `Scheduling reconnect attempt ${this.#reconnectAttempt + 1} in ${delay}ms`
|
|
2899
|
-
});
|
|
2900
|
-
this.#reconnectTimeout = setTimeout(async () => {
|
|
2901
|
-
var _a2;
|
|
2902
|
-
if (!this.#shutdown) {
|
|
2903
|
-
this.#reconnectAttempt++;
|
|
2904
|
-
(_a2 = this.log) == null ? void 0 : _a2.debug({
|
|
2905
|
-
msg: `Attempting to reconnect (attempt ${this.#reconnectAttempt})...`
|
|
2906
|
-
});
|
|
2907
|
-
await this.#openPegboardWebSocket();
|
|
2908
|
-
}
|
|
2909
|
-
}, delay);
|
|
2910
|
-
}
|
|
2911
|
-
#resendUnacknowledgedEvents(lastEventIdx) {
|
|
2912
|
-
var _a;
|
|
2913
|
-
const eventsToResend = this.#eventHistory.filter(
|
|
2914
|
-
(event) => event.index > lastEventIdx
|
|
2915
|
-
);
|
|
2916
|
-
if (eventsToResend.length === 0) return;
|
|
2917
|
-
(_a = this.log) == null ? void 0 : _a.info({
|
|
2918
|
-
msg: "resending unacknowledged events",
|
|
2919
|
-
fromIndex: lastEventIdx + 1n
|
|
2920
|
-
});
|
|
2921
|
-
this.__sendToServer({
|
|
2922
|
-
tag: "ToServerEvents",
|
|
2923
|
-
val: eventsToResend
|
|
2924
|
-
});
|
|
2925
|
-
}
|
|
2926
|
-
#cleanupOldKvRequests() {
|
|
2927
|
-
const thirtySecondsAgo = Date.now() - KV_EXPIRE;
|
|
2928
|
-
const toDelete = [];
|
|
2929
|
-
for (const [requestId, request] of this.#kvRequests.entries()) {
|
|
2930
|
-
if (request.timestamp < thirtySecondsAgo) {
|
|
2931
|
-
request.reject(
|
|
2932
|
-
new Error(
|
|
2933
|
-
"KV request timed out waiting for WebSocket connection"
|
|
2934
|
-
)
|
|
2935
|
-
);
|
|
2936
|
-
toDelete.push(requestId);
|
|
2937
|
-
}
|
|
2938
|
-
}
|
|
2939
|
-
for (const requestId of toDelete) {
|
|
2940
|
-
this.#kvRequests.delete(requestId);
|
|
2941
|
-
}
|
|
2942
|
-
if (toDelete.length > 0) {
|
|
2943
|
-
}
|
|
2944
|
-
}
|
|
2945
|
-
};
|
|
2946
|
-
export {
|
|
2947
|
-
Runner,
|
|
2948
|
-
RunnerActor,
|
|
2949
|
-
idToStr
|
|
2950
|
-
};
|
|
2951
|
-
//# sourceMappingURL=mod.js.map
|