@rivetkit/cloudflare-workers
Version:
_Lightweight Libraries for Backends_
1 lines • 41.7 kB
Source Map (JSON)
{"version":3,"sources":["/home/nathan/rivetkit/packages/drivers/cloudflare-workers/dist/mod.cjs","../src/handler.ts","../src/actor-handler-do.ts","../src/actor-driver.ts","../src/log.ts","../src/config.ts","../src/manager-driver.ts","../src/util.ts","../src/websocket.ts"],"names":["env","_a","ActorHandler"],"mappings":"AAAA;ACAA,uDAAoB;AAGpB,4BAAqB;ADArB;AACA;AEJA;AAOA;AACC;AACA;AACA;AAAA,sCACM;AACP,8DAA0C;AFA1C;AACA;AGRA;AACC;AACA;AACA;AAAA;AAQD,4FAAsB;AAaf,IAAM,mCAAA,EAAN,MAAyC;AAAA;AAAA,EAE/C,CAAA,IAAA,kBAA8C,IAAI,GAAA,CAAI,CAAA;AAAA,EAEtD,UAAA,CAAW,OAAA,EAA2C;AACrD,IAAA,MAAM,MAAA,EAAQ,IAAA,CAAK,CAAA,GAAA,CAAK,GAAA,CAAI,OAAO,CAAA;AACnC,IAAA,iCAAA,MAAU,IAAU,KAAA,CAAA,EAAW,0CAA0C,CAAA;AACzE,IAAA,OAAO,KAAA;AAAA,EACR;AAAA,EAEA,UAAA,CAAW,OAAA,EAAiB,KAAA,EAAiC;AAC5D,IAAA,IAAA,CAAK,CAAA,GAAA,CAAK,GAAA,CAAI,OAAA,EAAS,KAAK,CAAA;AAAA,EAC7B;AACD,CAAA;AAOA,IAAM,aAAA,YAAN,MAAmB;AAAA,EAClB;AAAA,iBACA,aAAA,EAA4C,OAAA,CAAQ,aAAA,CAAc,EAAA;AAAA,kBAClE,uBAAA,EAAyB,IAAI,iCAAA,CAAuB,EAAA;AACrD,UAAA;AAEO,IAAM,4BAAA,EAAN,MAAyD;AAAA,EAC/D,CAAA,cAAA;AAAA,EACA,CAAA,SAAA;AAAA,EACA,CAAA,aAAA;AAAA,EACA,CAAA,YAAA;AAAA,EACA,CAAA,WAAA;AAAA,EACA,CAAA,OAAA,kBAAqC,IAA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AACD;ARmfW;AACA;ACjiBI;AACRD,EAAAA;AACR;AAYgB;AAIP,EAAA;AACD,EAAA;AACR;AAEgB;AAIT,EAAA;AAGA,EAAA;AACG,IAAA;AACD,MAAA;AACN,MAAA;AAAiD;AAEjD,MAAA;AACD,IAAA;AACA,IAAA;AACG,IAAA;AACJ,EAAA;AAGME,EAAAA;AAGA,EAAA;AAEC,EAAA;AACE,IAAA;AACR,IAAA;AAEO,MAAA;AAGD,MAAA;AACA,QAAA;AACL,MAAA;AAGM,MAAA;AACL,QAAA;AACC,UAAA;AACD,QAAA;AACD,MAAA;AAEA,MAAA;AACD,IAAA;AACD,EAAA;AACD;ADogBY;AACA;AACA;AACA","file":"/home/nathan/rivetkit/packages/drivers/cloudflare-workers/dist/mod.cjs","sourcesContent":[null,"import { env } from \"cloudflare:workers\";\nimport type { Registry, RunConfig } from \"@rivetkit/core\";\nimport type { Client } from \"@rivetkit/core/client\";\nimport { Hono } from \"hono\";\nimport {\n\ttype ActorHandlerInterface,\n\tcreateActorDurableObject,\n\ttype DurableObjectConstructor,\n} from \"./actor-handler-do\";\nimport { ConfigSchema, type InputConfig } from \"./config\";\nimport { CloudflareActorsManagerDriver } from \"./manager-driver\";\nimport { upgradeWebSocket } from \"./websocket\";\n\n/** Cloudflare Workers env */\nexport interface Bindings {\n\tACTOR_KV: KVNamespace;\n\tACTOR_DO: DurableObjectNamespace<ActorHandlerInterface>;\n}\n\n/**\n * Stores the env for the current request. Required since some contexts like the inline client driver does not have access to the Hono context.\n *\n * Use getCloudflareAmbientEnv unless using CF_AMBIENT_ENV.run.\n */\nexport function getCloudflareAmbientEnv(): Bindings {\n\treturn env as unknown as Bindings;\n}\n\ninterface Handler {\n\thandler: ExportedHandler<Bindings>;\n\tActorHandler: DurableObjectConstructor;\n}\n\ninterface SetupOutput<A extends Registry<any>> {\n\tclient: Client<A>;\n\tcreateHandler: (hono?: Hono) => Handler;\n}\n\nexport function createServerHandler<R extends Registry<any>>(\n\tregistry: R,\n\tinputConfig?: InputConfig,\n): Handler {\n\tconst { createHandler } = createServer(registry, inputConfig);\n\treturn createHandler();\n}\n\nexport function createServer<R extends Registry<any>>(\n\tregistry: R,\n\tinputConfig?: InputConfig,\n): SetupOutput<R> {\n\tconst config = ConfigSchema.parse(inputConfig);\n\n\t// Create config\n\tconst runConfig = {\n\t\tdriver: {\n\t\t\tname: \"cloudflare-workers\",\n\t\t\tmanager: () => new CloudflareActorsManagerDriver(),\n\t\t\t// HACK: We can't build the actor driver until we're inside the Durable Object\n\t\t\tactor: undefined as any,\n\t\t},\n\t\tgetUpgradeWebSocket: () => upgradeWebSocket,\n\t\t...config,\n\t} satisfies RunConfig;\n\n\t// Create Durable Object\n\tconst ActorHandler = createActorDurableObject(registry, runConfig);\n\n\t// Create server\n\tconst serverOutput = registry.createServer(runConfig);\n\n\treturn {\n\t\tclient: serverOutput.client as Client<R>,\n\t\tcreateHandler: (hono) => {\n\t\t\t// Build base router\n\t\t\tconst app = hono ?? new Hono();\n\n\t\t\t// Mount registry router at /registry\n\t\t\tif (!hono) {\n\t\t\t\tapp.route(\"/registry\", serverOutput.hono);\n\t\t\t}\n\n\t\t\t// Create Cloudflare handler\n\t\t\tconst handler = {\n\t\t\t\tfetch: (request, env, ctx) => {\n\t\t\t\t\treturn app.fetch(request, env, ctx);\n\t\t\t\t},\n\t\t\t} satisfies ExportedHandler<Bindings>;\n\n\t\t\treturn { handler, ActorHandler };\n\t\t},\n\t};\n}\n","import { DurableObject, env } from \"cloudflare:workers\";\nimport type {\n\tActorKey,\n\tActorRouter,\n\tRegistry,\n\tRunConfig,\n} from \"@rivetkit/core\";\nimport {\n\tcreateActorRouter,\n\tcreateClientWithDriver,\n\tcreateInlineClientDriver,\n} from \"@rivetkit/core\";\nimport { serializeEmptyPersistData } from \"@rivetkit/core/driver-helpers\";\nimport type { ExecutionContext } from \"hono\";\nimport {\n\tCloudflareDurableObjectGlobalState,\n\tcreateCloudflareActorsActorDriverBuilder,\n} from \"./actor-driver\";\nimport type { Bindings } from \"./handler\";\nimport { logger } from \"./log\";\n\nexport const KEYS = {\n\tNAME: \"rivetkit:name\",\n\tKEY: \"rivetkit:key\",\n\tPERSIST_DATA: \"rivetkit:data\",\n};\n\nexport interface ActorHandlerInterface extends DurableObject {\n\tinitialize(req: ActorInitRequest): Promise<void>;\n}\n\nexport interface ActorInitRequest {\n\tname: string;\n\tkey: ActorKey;\n\tinput?: unknown;\n}\n\ninterface InitializedData {\n\tname: string;\n\tkey: ActorKey;\n}\n\nexport type DurableObjectConstructor = new (\n\t...args: ConstructorParameters<typeof DurableObject<Bindings>>\n) => DurableObject<Bindings>;\n\ninterface LoadedActor {\n\tactorRouter: ActorRouter;\n}\n\nexport function createActorDurableObject(\n\tregistry: Registry<any>,\n\trunConfig: RunConfig,\n): DurableObjectConstructor {\n\tconst globalState = new CloudflareDurableObjectGlobalState();\n\n\t/**\n\t * Startup steps:\n\t * 1. If not already created call `initialize`, otherwise check KV to ensure it's initialized\n\t * 2. Load actor\n\t * 3. Start service requests\n\t */\n\treturn class ActorHandler\n\t\textends DurableObject<Bindings>\n\t\timplements ActorHandlerInterface\n\t{\n\t\t#initialized?: InitializedData;\n\t\t#initializedPromise?: PromiseWithResolvers<void>;\n\n\t\t#actor?: LoadedActor;\n\n\t\tasync #loadActor(): Promise<LoadedActor> {\n\t\t\t// Wait for init\n\t\t\tif (!this.#initialized) {\n\t\t\t\t// Wait for init\n\t\t\t\tif (this.#initializedPromise) {\n\t\t\t\t\tawait this.#initializedPromise.promise;\n\t\t\t\t} else {\n\t\t\t\t\tthis.#initializedPromise = Promise.withResolvers();\n\t\t\t\t\tconst res = await this.ctx.storage.get([\n\t\t\t\t\t\tKEYS.NAME,\n\t\t\t\t\t\tKEYS.KEY,\n\t\t\t\t\t\tKEYS.PERSIST_DATA,\n\t\t\t\t\t]);\n\t\t\t\t\tif (res.get(KEYS.PERSIST_DATA)) {\n\t\t\t\t\t\tconst name = res.get(KEYS.NAME) as string;\n\t\t\t\t\t\tif (!name) throw new Error(\"missing actor name\");\n\t\t\t\t\t\tconst key = res.get(KEYS.KEY) as ActorKey;\n\t\t\t\t\t\tif (!key) throw new Error(\"missing actor key\");\n\n\t\t\t\t\t\tlogger().debug(\"already initialized\", { name, key });\n\n\t\t\t\t\t\tthis.#initialized = { name, key };\n\t\t\t\t\t\tthis.#initializedPromise.resolve();\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlogger().debug(\"waiting to initialize\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Check if already loaded\n\t\t\tif (this.#actor) {\n\t\t\t\treturn this.#actor;\n\t\t\t}\n\n\t\t\tif (!this.#initialized) throw new Error(\"Not initialized\");\n\n\t\t\t// Register DO with global state first\n\t\t\t// HACK: This leaks the DO context, but DO does not provide a native way\n\t\t\t// of knowing when the DO shuts down. We're making a broad assumption\n\t\t\t// that DO will boot a new isolate frequenlty enough that this is not an issue.\n\t\t\tconst actorId = this.ctx.id.toString();\n\t\t\tglobalState.setDOState(actorId, { ctx: this.ctx, env: env });\n\n\t\t\t// Configure actor driver\n\t\t\trunConfig.driver.actor =\n\t\t\t\tcreateCloudflareActorsActorDriverBuilder(globalState);\n\n\t\t\t// Create manager driver (we need this for the actor router)\n\t\t\tconst managerDriver = runConfig.driver.manager(\n\t\t\t\tregistry.config,\n\t\t\t\trunConfig,\n\t\t\t);\n\n\t\t\t// Create inline client\n\t\t\tconst inlineClient = createClientWithDriver(\n\t\t\t\tcreateInlineClientDriver(managerDriver),\n\t\t\t);\n\n\t\t\t// Create actor driver\n\t\t\tconst actorDriver = runConfig.driver.actor(\n\t\t\t\tregistry.config,\n\t\t\t\trunConfig,\n\t\t\t\tmanagerDriver,\n\t\t\t\tinlineClient,\n\t\t\t);\n\n\t\t\t// Create actor router\n\t\t\tconst actorRouter = createActorRouter(runConfig, actorDriver);\n\n\t\t\t// Save actor\n\t\t\tthis.#actor = {\n\t\t\t\tactorRouter,\n\t\t\t};\n\n\t\t\t// Initialize the actor instance with proper metadata\n\t\t\t// This ensures the actor driver knows about this actor\n\t\t\tawait actorDriver.loadActor(actorId);\n\n\t\t\treturn this.#actor;\n\t\t}\n\n\t\t/** RPC called by the service that creates the DO to initialize it. */\n\t\tasync initialize(req: ActorInitRequest) {\n\t\t\t// TODO: Need to add this to a core promise that needs to be resolved before start\n\n\t\t\tawait this.ctx.storage.put({\n\t\t\t\t[KEYS.NAME]: req.name,\n\t\t\t\t[KEYS.KEY]: req.key,\n\t\t\t\t[KEYS.PERSIST_DATA]: serializeEmptyPersistData(req.input),\n\t\t\t});\n\t\t\tthis.#initialized = {\n\t\t\t\tname: req.name,\n\t\t\t\tkey: req.key,\n\t\t\t};\n\n\t\t\tlogger().debug(\"initialized actor\", { key: req.key });\n\n\t\t\t// Preemptively actor so the lifecycle hooks are called\n\t\t\tawait this.#loadActor();\n\t\t}\n\n\t\tasync fetch(request: Request): Promise<Response> {\n\t\t\tconst { actorRouter } = await this.#loadActor();\n\n\t\t\tconst actorId = this.ctx.id.toString();\n\t\t\treturn await actorRouter.fetch(request, {\n\t\t\t\tactorId,\n\t\t\t});\n\t\t}\n\n\t\tasync alarm(): Promise<void> {\n\t\t\tawait this.#loadActor();\n\t\t\tconst actorId = this.ctx.id.toString();\n\n\t\t\t// Get the actor driver\n\t\t\tconst managerDriver = runConfig.driver.manager(\n\t\t\t\tregistry.config,\n\t\t\t\trunConfig,\n\t\t\t);\n\t\t\tconst inlineClient = createClientWithDriver(\n\t\t\t\tcreateInlineClientDriver(managerDriver),\n\t\t\t);\n\t\t\tconst actorDriver = runConfig.driver.actor(\n\t\t\t\tregistry.config,\n\t\t\t\trunConfig,\n\t\t\t\tmanagerDriver,\n\t\t\t\tinlineClient,\n\t\t\t);\n\n\t\t\t// Load the actor instance and trigger alarm\n\t\t\tconst actor = await actorDriver.loadActor(actorId);\n\t\t\tawait actor.onAlarm();\n\t\t}\n\t};\n}\n","import type {\n\tAnyActorInstance as CoreAnyActorInstance,\n\tRegistryConfig,\n\tRunConfig,\n} from \"@rivetkit/core\";\nimport {\n\tcreateGenericConnDrivers,\n\tGenericConnGlobalState,\n\tlookupInRegistry,\n} from \"@rivetkit/core\";\nimport type { Client } from \"@rivetkit/core/client\";\nimport type {\n\tActorDriver,\n\tAnyActorInstance,\n\tManagerDriver,\n} from \"@rivetkit/core/driver-helpers\";\nimport invariant from \"invariant\";\nimport { KEYS } from \"./actor-handler-do\";\n\ninterface DurableObjectGlobalState {\n\tctx: DurableObjectState;\n\tenv: unknown;\n}\n\n/**\n * Cloudflare DO can have multiple DO running within the same global scope.\n *\n * This allows for storing the actor context globally and looking it up by ID in `CloudflareActorsActorDriver`.\n */\nexport class CloudflareDurableObjectGlobalState {\n\t// Single map for all actor state\n\t#dos: Map<string, DurableObjectGlobalState> = new Map();\n\n\tgetDOState(actorId: string): DurableObjectGlobalState {\n\t\tconst state = this.#dos.get(actorId);\n\t\tinvariant(state !== undefined, \"durable object state not in global state\");\n\t\treturn state;\n\t}\n\n\tsetDOState(actorId: string, state: DurableObjectGlobalState) {\n\t\tthis.#dos.set(actorId, state);\n\t}\n}\n\nexport interface DriverContext {\n\tstate: DurableObjectState;\n}\n\n// Actor handler to track running instances\nclass ActorHandler {\n\tactor?: AnyActorInstance;\n\tactorPromise?: PromiseWithResolvers<void> = Promise.withResolvers();\n\tgenericConnGlobalState = new GenericConnGlobalState();\n}\n\nexport class CloudflareActorsActorDriver implements ActorDriver {\n\t#registryConfig: RegistryConfig;\n\t#runConfig: RunConfig;\n\t#managerDriver: ManagerDriver;\n\t#inlineClient: Client<any>;\n\t#globalState: CloudflareDurableObjectGlobalState;\n\t#actors: Map<string, ActorHandler> = new Map();\n\n\tconstructor(\n\t\tregistryConfig: RegistryConfig,\n\t\trunConfig: RunConfig,\n\t\tmanagerDriver: ManagerDriver,\n\t\tinlineClient: Client<any>,\n\t\tglobalState: CloudflareDurableObjectGlobalState,\n\t) {\n\t\tthis.#registryConfig = registryConfig;\n\t\tthis.#runConfig = runConfig;\n\t\tthis.#managerDriver = managerDriver;\n\t\tthis.#inlineClient = inlineClient;\n\t\tthis.#globalState = globalState;\n\t}\n\n\t#getDOCtx(actorId: string) {\n\t\treturn this.#globalState.getDOState(actorId).ctx;\n\t}\n\n\tasync loadActor(actorId: string): Promise<AnyActorInstance> {\n\t\t// Check if actor is already loaded\n\t\tlet handler = this.#actors.get(actorId);\n\t\tif (handler) {\n\t\t\tif (handler.actorPromise) await handler.actorPromise.promise;\n\t\t\tif (!handler.actor) throw new Error(\"Actor should be loaded\");\n\t\t\treturn handler.actor;\n\t\t}\n\n\t\t// Create new actor handler\n\t\thandler = new ActorHandler();\n\t\tthis.#actors.set(actorId, handler);\n\n\t\t// Get the actor metadata from Durable Object storage\n\t\tconst doState = this.#globalState.getDOState(actorId);\n\t\tconst storage = doState.ctx.storage;\n\n\t\t// Load actor metadata\n\t\tconst [name, key] = await Promise.all([\n\t\t\tstorage.get<string>(KEYS.NAME),\n\t\t\tstorage.get<string[]>(KEYS.KEY),\n\t\t]);\n\n\t\tif (!name) {\n\t\t\tthrow new Error(`Actor ${actorId} is not initialized - missing name`);\n\t\t}\n\t\tif (!key) {\n\t\t\tthrow new Error(`Actor ${actorId} is not initialized - missing key`);\n\t\t}\n\n\t\t// Create actor instance\n\t\tconst definition = lookupInRegistry(this.#registryConfig, name);\n\t\thandler.actor = definition.instantiate();\n\n\t\t// Start actor\n\t\tconst connDrivers = createGenericConnDrivers(\n\t\t\thandler.genericConnGlobalState,\n\t\t);\n\t\tawait handler.actor.start(\n\t\t\tconnDrivers,\n\t\t\tthis,\n\t\t\tthis.#inlineClient,\n\t\t\tactorId,\n\t\t\tname,\n\t\t\tkey,\n\t\t\t\"unknown\", // TODO: Support regions in Cloudflare\n\t\t);\n\n\t\t// Finish\n\t\thandler.actorPromise?.resolve();\n\t\thandler.actorPromise = undefined;\n\n\t\treturn handler.actor;\n\t}\n\n\tgetGenericConnGlobalState(actorId: string): GenericConnGlobalState {\n\t\tconst handler = this.#actors.get(actorId);\n\t\tif (!handler) {\n\t\t\tthrow new Error(`Actor ${actorId} not loaded`);\n\t\t}\n\t\treturn handler.genericConnGlobalState;\n\t}\n\n\tgetContext(actorId: string): DriverContext {\n\t\tconst state = this.#globalState.getDOState(actorId);\n\t\treturn { state: state.ctx };\n\t}\n\n\tasync readPersistedData(actorId: string): Promise<Uint8Array | undefined> {\n\t\treturn await this.#getDOCtx(actorId).storage.get(KEYS.PERSIST_DATA);\n\t}\n\n\tasync writePersistedData(actorId: string, data: Uint8Array): Promise<void> {\n\t\tawait this.#getDOCtx(actorId).storage.put(KEYS.PERSIST_DATA, data);\n\t}\n\n\tasync setAlarm(actor: AnyActorInstance, timestamp: number): Promise<void> {\n\t\tawait this.#getDOCtx(actor.id).storage.setAlarm(timestamp);\n\t}\n\n\tasync getDatabase(actorId: string): Promise<unknown | undefined> {\n\t\treturn this.#getDOCtx(actorId).storage.sql;\n\t}\n}\n\nexport function createCloudflareActorsActorDriverBuilder(\n\tglobalState: CloudflareDurableObjectGlobalState,\n) {\n\treturn (\n\t\tregistryConfig: RegistryConfig,\n\t\trunConfig: RunConfig,\n\t\tmanagerDriver: ManagerDriver,\n\t\tinlineClient: Client<any>,\n\t) => {\n\t\treturn new CloudflareActorsActorDriver(\n\t\t\tregistryConfig,\n\t\t\trunConfig,\n\t\t\tmanagerDriver,\n\t\t\tinlineClient,\n\t\t\tglobalState,\n\t\t);\n\t};\n}\n","import { getLogger } from \"@rivetkit/core/log\";\n\nexport const LOGGER_NAME = \"driver-cloudflare-workers\";\n\nexport function logger() {\n\treturn getLogger(LOGGER_NAME);\n}\n","import { RunConfigSchema } from \"@rivetkit/core/driver-helpers\";\nimport type { Hono } from \"hono\";\nimport { z } from \"zod\";\n\nexport const ConfigSchema = RunConfigSchema.removeDefault()\n\t.omit({ driver: true, getUpgradeWebSocket: true })\n\t.extend({\n\t\tapp: z.custom<Hono>().optional(),\n\t})\n\t.default({});\nexport type InputConfig = z.input<typeof ConfigSchema>;\nexport type Config = z.infer<typeof ConfigSchema>;\n","import type { Encoding } from \"@rivetkit/core\";\nimport {\n\ttype ActorOutput,\n\ttype CreateInput,\n\ttype GetForIdInput,\n\ttype GetOrCreateWithKeyInput,\n\ttype GetWithKeyInput,\n\tHEADER_AUTH_DATA,\n\tHEADER_CONN_PARAMS,\n\tHEADER_ENCODING,\n\tHEADER_EXPOSE_INTERNAL_ERROR,\n\ttype ManagerDriver,\n} from \"@rivetkit/core/driver-helpers\";\nimport { ActorAlreadyExists, InternalError } from \"@rivetkit/core/errors\";\nimport type { Context as HonoContext } from \"hono\";\nimport { getCloudflareAmbientEnv } from \"./handler\";\nimport { logger } from \"./log\";\nimport type { Bindings } from \"./mod\";\nimport { serializeKey, serializeNameAndKey } from \"./util\";\n\n// Actor metadata structure\ninterface ActorData {\n\tname: string;\n\tkey: string[];\n}\n\n// Key constants similar to Redis implementation\nconst KEYS = {\n\tACTOR: {\n\t\t// Combined key for actor metadata (name and key)\n\t\tmetadata: (actorId: string) => `actor:${actorId}:metadata`,\n\n\t\t// Key index function for actor lookup\n\t\tkeyIndex: (name: string, key: string[] = []) => {\n\t\t\t// Use serializeKey for consistent handling of all keys\n\t\t\treturn `actor_key:${serializeKey(key)}`;\n\t\t},\n\t},\n};\n\nconst STANDARD_WEBSOCKET_HEADERS = [\n\t\"connection\",\n\t\"upgrade\",\n\t\"sec-websocket-key\",\n\t\"sec-websocket-version\",\n\t\"sec-websocket-protocol\",\n\t\"sec-websocket-extensions\",\n];\n\nexport class CloudflareActorsManagerDriver implements ManagerDriver {\n\tasync sendRequest(actorId: string, actorRequest: Request): Promise<Response> {\n\t\tconst env = getCloudflareAmbientEnv();\n\n\t\tlogger().debug(\"sending request to durable object\", {\n\t\t\tactorId,\n\t\t\tmethod: actorRequest.method,\n\t\t\turl: actorRequest.url,\n\t\t});\n\n\t\tconst id = env.ACTOR_DO.idFromString(actorId);\n\t\tconst stub = env.ACTOR_DO.get(id);\n\n\t\treturn await stub.fetch(actorRequest);\n\t}\n\n\tasync openWebSocket(\n\t\tpath: string,\n\t\tactorId: string,\n\t\tencoding: Encoding,\n\t\tparams: unknown,\n\t): Promise<WebSocket> {\n\t\tconst env = getCloudflareAmbientEnv();\n\n\t\tlogger().debug(\"opening websocket to durable object\", { actorId, path });\n\n\t\t// Make a fetch request to the Durable Object with WebSocket upgrade\n\t\tconst id = env.ACTOR_DO.idFromString(actorId);\n\t\tconst stub = env.ACTOR_DO.get(id);\n\n\t\tconst headers: Record<string, string> = {\n\t\t\tUpgrade: \"websocket\",\n\t\t\tConnection: \"Upgrade\",\n\t\t\t[HEADER_EXPOSE_INTERNAL_ERROR]: \"true\",\n\t\t\t[HEADER_ENCODING]: encoding,\n\t\t};\n\t\tif (params) {\n\t\t\theaders[HEADER_CONN_PARAMS] = JSON.stringify(params);\n\t\t}\n\t\t// HACK: See packages/drivers/cloudflare-workers/src/websocket.ts\n\t\theaders[\"sec-websocket-protocol\"] = \"rivetkit\";\n\n\t\t// Use the path parameter to determine the URL\n\t\tconst url = `http://actor${path}`;\n\n\t\tlogger().debug(\"rewriting websocket url\", {\n\t\t\tfrom: path,\n\t\t\tto: url,\n\t\t});\n\n\t\tconst response = await stub.fetch(url, {\n\t\t\theaders,\n\t\t});\n\t\tconst webSocket = response.webSocket;\n\n\t\tif (!webSocket) {\n\t\t\tthrow new InternalError(\n\t\t\t\t\"missing websocket connection in response from DO\",\n\t\t\t);\n\t\t}\n\n\t\tlogger().debug(\"durable object websocket connection open\", {\n\t\t\tactorId,\n\t\t});\n\n\t\twebSocket.accept();\n\n\t\t// TODO: Is this still needed?\n\t\t// HACK: Cloudflare does not call onopen automatically, so we need\n\t\t// to call this on the next tick\n\t\tsetTimeout(() => {\n\t\t\tconst event = new Event(\"open\");\n\t\t\t(webSocket as any).onopen?.(event);\n\t\t\t(webSocket as any).dispatchEvent(event);\n\t\t}, 0);\n\n\t\treturn webSocket as unknown as WebSocket;\n\t}\n\n\tasync proxyRequest(\n\t\tc: HonoContext<{ Bindings: Bindings }>,\n\t\tactorRequest: Request,\n\t\tactorId: string,\n\t): Promise<Response> {\n\t\tlogger().debug(\"forwarding request to durable object\", {\n\t\t\tactorId,\n\t\t\tmethod: actorRequest.method,\n\t\t\turl: actorRequest.url,\n\t\t});\n\n\t\tconst id = c.env.ACTOR_DO.idFromString(actorId);\n\t\tconst stub = c.env.ACTOR_DO.get(id);\n\n\t\treturn await stub.fetch(actorRequest);\n\t}\n\n\tasync proxyWebSocket(\n\t\tc: HonoContext<{ Bindings: Bindings }>,\n\t\tpath: string,\n\t\tactorId: string,\n\t\tencoding: Encoding,\n\t\tparams: unknown,\n\t\tauthData: unknown,\n\t): Promise<Response> {\n\t\tlogger().debug(\"forwarding websocket to durable object\", {\n\t\t\tactorId,\n\t\t\tpath,\n\t\t});\n\n\t\t// Validate upgrade\n\t\tconst upgradeHeader = c.req.header(\"Upgrade\");\n\t\tif (!upgradeHeader || upgradeHeader !== \"websocket\") {\n\t\t\treturn new Response(\"Expected Upgrade: websocket\", {\n\t\t\t\tstatus: 426,\n\t\t\t});\n\t\t}\n\n\t\t// TODO: strip headers\n\t\tconst newUrl = new URL(`http://actor${path}`);\n\t\tconst actorRequest = new Request(newUrl, c.req.raw);\n\n\t\tlogger().debug(\"rewriting websocket url\", {\n\t\t\tfrom: c.req.url,\n\t\t\tto: actorRequest.url,\n\t\t});\n\n\t\t// Always build fresh request to prevent forwarding unwanted headers\n\t\t// HACK: Since we can't build a new request, we need to remove\n\t\t// non-standard headers manually\n\t\tconst headerKeys: string[] = [];\n\t\tactorRequest.headers.forEach((v, k) => headerKeys.push(k));\n\t\tfor (const k of headerKeys) {\n\t\t\tif (!STANDARD_WEBSOCKET_HEADERS.includes(k)) {\n\t\t\t\tactorRequest.headers.delete(k);\n\t\t\t}\n\t\t}\n\n\t\t// Add RivetKit headers\n\t\tactorRequest.headers.set(HEADER_EXPOSE_INTERNAL_ERROR, \"true\");\n\t\tactorRequest.headers.set(HEADER_ENCODING, encoding);\n\t\tif (params) {\n\t\t\tactorRequest.headers.set(HEADER_CONN_PARAMS, JSON.stringify(params));\n\t\t}\n\t\tif (authData) {\n\t\t\tactorRequest.headers.set(HEADER_AUTH_DATA, JSON.stringify(authData));\n\t\t}\n\n\t\tconst id = c.env.ACTOR_DO.idFromString(actorId);\n\t\tconst stub = c.env.ACTOR_DO.get(id);\n\n\t\treturn await stub.fetch(actorRequest);\n\t}\n\n\tasync getForId({\n\t\tc,\n\t\tactorId,\n\t}: GetForIdInput<{ Bindings: Bindings }>): Promise<ActorOutput | undefined> {\n\t\tconst env = getCloudflareAmbientEnv();\n\n\t\t// Get actor metadata from KV (combined name and key)\n\t\tconst actorData = (await env.ACTOR_KV.get(KEYS.ACTOR.metadata(actorId), {\n\t\t\ttype: \"json\",\n\t\t})) as ActorData | null;\n\n\t\t// If the actor doesn't exist, return undefined\n\t\tif (!actorData) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn {\n\t\t\tactorId,\n\t\t\tname: actorData.name,\n\t\t\tkey: actorData.key,\n\t\t};\n\t}\n\n\tasync getWithKey({\n\t\tc,\n\t\tname,\n\t\tkey,\n\t}: GetWithKeyInput<{ Bindings: Bindings }>): Promise<\n\t\tActorOutput | undefined\n\t> {\n\t\tconst env = getCloudflareAmbientEnv();\n\n\t\tlogger().debug(\"getWithKey: searching for actor\", { name, key });\n\n\t\t// Generate deterministic ID from the name and key\n\t\t// This is aligned with how createActor generates IDs\n\t\tconst nameKeyString = serializeNameAndKey(name, key);\n\t\tconst actorId = env.ACTOR_DO.idFromName(nameKeyString).toString();\n\n\t\t// Check if the actor metadata exists\n\t\tconst actorData = await env.ACTOR_KV.get(KEYS.ACTOR.metadata(actorId), {\n\t\t\ttype: \"json\",\n\t\t});\n\n\t\tif (!actorData) {\n\t\t\tlogger().debug(\"getWithKey: no actor found with matching name and key\", {\n\t\t\t\tname,\n\t\t\t\tkey,\n\t\t\t\tactorId,\n\t\t\t});\n\t\t\treturn undefined;\n\t\t}\n\n\t\tlogger().debug(\"getWithKey: found actor with matching name and key\", {\n\t\t\tactorId,\n\t\t\tname,\n\t\t\tkey,\n\t\t});\n\t\treturn this.#buildActorOutput(c, actorId);\n\t}\n\n\tasync getOrCreateWithKey(\n\t\tinput: GetOrCreateWithKeyInput,\n\t): Promise<ActorOutput> {\n\t\t// TODO: Prevent race condition here\n\t\tconst getOutput = await this.getWithKey(input);\n\t\tif (getOutput) {\n\t\t\treturn getOutput;\n\t\t} else {\n\t\t\treturn await this.createActor(input);\n\t\t}\n\t}\n\n\tasync createActor({\n\t\tc,\n\t\tname,\n\t\tkey,\n\t\tinput,\n\t}: CreateInput<{ Bindings: Bindings }>): Promise<ActorOutput> {\n\t\tconst env = getCloudflareAmbientEnv();\n\n\t\t// Check if actor with the same name and key already exists\n\t\tconst existingActor = await this.getWithKey({ c, name, key });\n\t\tif (existingActor) {\n\t\t\tthrow new ActorAlreadyExists(name, key);\n\t\t}\n\n\t\t// Create a deterministic ID from the actor name and key\n\t\t// This ensures that actors with the same name and key will have the same ID\n\t\tconst nameKeyString = serializeNameAndKey(name, key);\n\t\tconst doId = env.ACTOR_DO.idFromName(nameKeyString);\n\t\tconst actorId = doId.toString();\n\n\t\t// Init actor\n\t\tconst actor = env.ACTOR_DO.get(doId);\n\t\tawait actor.initialize({\n\t\t\tname,\n\t\t\tkey,\n\t\t\tinput,\n\t\t});\n\n\t\t// Store combined actor metadata (name and key)\n\t\tconst actorData: ActorData = { name, key };\n\t\tawait env.ACTOR_KV.put(\n\t\t\tKEYS.ACTOR.metadata(actorId),\n\t\t\tJSON.stringify(actorData),\n\t\t);\n\n\t\t// Add to key index for lookups by name and key\n\t\tawait env.ACTOR_KV.put(KEYS.ACTOR.keyIndex(name, key), actorId);\n\n\t\treturn {\n\t\t\tactorId,\n\t\t\tname,\n\t\t\tkey,\n\t\t};\n\t}\n\n\t// Helper method to build actor output from an ID\n\tasync #buildActorOutput(\n\t\tc: any,\n\t\tactorId: string,\n\t): Promise<ActorOutput | undefined> {\n\t\tconst env = getCloudflareAmbientEnv();\n\n\t\tconst actorData = (await env.ACTOR_KV.get(KEYS.ACTOR.metadata(actorId), {\n\t\t\ttype: \"json\",\n\t\t})) as ActorData | null;\n\n\t\tif (!actorData) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn {\n\t\t\tactorId,\n\t\t\tname: actorData.name,\n\t\t\tkey: actorData.key,\n\t\t};\n\t}\n}\n","// Constants for key handling\nexport const EMPTY_KEY = \"(none)\";\nexport const KEY_SEPARATOR = \",\";\n\n/**\n * Serializes an array of key strings into a single string for use with idFromName\n *\n * @param name The actor name\n * @param key Array of key strings to serialize\n * @returns A single string containing the serialized name and key\n */\nexport function serializeNameAndKey(name: string, key: string[]): string {\n\t// Escape colons in the name\n\tconst escapedName = name.replace(/:/g, \"\\\\:\");\n\n\t// For empty keys, just return the name and a marker\n\tif (key.length === 0) {\n\t\treturn `${escapedName}:${EMPTY_KEY}`;\n\t}\n\n\t// Serialize the key array\n\tconst serializedKey = serializeKey(key);\n\n\t// Combine name and serialized key\n\treturn `${escapedName}:${serializedKey}`;\n}\n\n/**\n * Serializes an array of key strings into a single string\n *\n * @param key Array of key strings to serialize\n * @returns A single string containing the serialized key\n */\nexport function serializeKey(key: string[]): string {\n\t// Use a special marker for empty key arrays\n\tif (key.length === 0) {\n\t\treturn EMPTY_KEY;\n\t}\n\n\t// Escape each key part to handle the separator and the empty key marker\n\tconst escapedParts = key.map((part) => {\n\t\t// First check if it matches our empty key marker\n\t\tif (part === EMPTY_KEY) {\n\t\t\treturn `\\\\${EMPTY_KEY}`;\n\t\t}\n\n\t\t// Escape backslashes first, then commas\n\t\tlet escaped = part.replace(/\\\\/g, \"\\\\\\\\\");\n\t\tescaped = escaped.replace(/,/g, \"\\\\,\");\n\t\treturn escaped;\n\t});\n\n\treturn escapedParts.join(KEY_SEPARATOR);\n}\n\n/**\n * Deserializes a key string back into an array of key strings\n *\n * @param keyString The serialized key string\n * @returns Array of key strings\n */\nexport function deserializeKey(keyString: string): string[] {\n\t// Handle empty values\n\tif (!keyString) {\n\t\treturn [];\n\t}\n\n\t// Check for special empty key marker\n\tif (keyString === EMPTY_KEY) {\n\t\treturn [];\n\t}\n\n\t// Split by unescaped commas and unescape the escaped characters\n\tconst parts: string[] = [];\n\tlet currentPart = \"\";\n\tlet escaping = false;\n\n\tfor (let i = 0; i < keyString.length; i++) {\n\t\tconst char = keyString[i];\n\n\t\tif (escaping) {\n\t\t\t// This is an escaped character, add it directly\n\t\t\tcurrentPart += char;\n\t\t\tescaping = false;\n\t\t} else if (char === \"\\\\\") {\n\t\t\t// Start of an escape sequence\n\t\t\tescaping = true;\n\t\t} else if (char === KEY_SEPARATOR) {\n\t\t\t// This is a separator\n\t\t\tparts.push(currentPart);\n\t\t\tcurrentPart = \"\";\n\t\t} else {\n\t\t\t// Regular character\n\t\t\tcurrentPart += char;\n\t\t}\n\t}\n\n\t// Add the last part if it exists\n\tif (currentPart || parts.length > 0) {\n\t\tparts.push(currentPart);\n\t}\n\n\treturn parts;\n}\n","// Modified from https://github.com/honojs/hono/blob/40ea0eee58e39b31053a0246c595434f1094ad31/src/adapter/cloudflare-workers/websocket.ts#L17\n//\n// This version calls the open event by default\n\nimport type { UpgradeWebSocket, WSEvents, WSReadyState } from \"hono/ws\";\nimport { defineWebSocketHelper, WSContext } from \"hono/ws\";\n\n// Based on https://github.com/honojs/hono/issues/1153#issuecomment-1767321332\nexport const upgradeWebSocket: UpgradeWebSocket<\n\tWebSocket,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\tWSEvents<WebSocket>\n> = defineWebSocketHelper(async (c, events) => {\n\tconst upgradeHeader = c.req.header(\"Upgrade\");\n\tif (upgradeHeader !== \"websocket\") {\n\t\treturn;\n\t}\n\n\tconst webSocketPair = new WebSocketPair();\n\tconst client: WebSocket = webSocketPair[0];\n\tconst server: WebSocket = webSocketPair[1];\n\n\tconst wsContext = new WSContext<WebSocket>({\n\t\tclose: (code, reason) => server.close(code, reason),\n\t\tget protocol() {\n\t\t\treturn server.protocol;\n\t\t},\n\t\traw: server,\n\t\tget readyState() {\n\t\t\treturn server.readyState as WSReadyState;\n\t\t},\n\t\turl: server.url ? new URL(server.url) : null,\n\t\tsend: (source) => server.send(source),\n\t});\n\n\tif (events.onClose) {\n\t\tserver.addEventListener(\"close\", (evt: CloseEvent) =>\n\t\t\tevents.onClose?.(evt, wsContext),\n\t\t);\n\t}\n\tif (events.onMessage) {\n\t\tserver.addEventListener(\"message\", (evt: MessageEvent) =>\n\t\t\tevents.onMessage?.(evt, wsContext),\n\t\t);\n\t}\n\tif (events.onError) {\n\t\tserver.addEventListener(\"error\", (evt: Event) =>\n\t\t\tevents.onError?.(evt, wsContext),\n\t\t);\n\t}\n\n\tserver.accept?.();\n\n\t// note: cloudflare actors doesn't support 'open' event, so we call it immediately with a fake event\n\t//\n\t// we have to do this after `server.accept() is called`\n\tevents.onOpen?.(new Event(\"open\"), wsContext);\n\n\treturn new Response(null, {\n\t\tstatus: 101,\n\t\theaders: {\n\t\t\t// HACK: Required in order for Cloudflare to not error with \"Network connection lost\"\n\t\t\t//\n\t\t\t// This bug undocumented. Cannot easily reproduce outside of RivetKit.\n\t\t\t\"Sec-WebSocket-Protocol\": \"rivetkit\",\n\t\t},\n\t\twebSocket: client,\n\t});\n});\n"]}