Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 41 additions & 49 deletions packages/drivers/cloudflare-workers/src/actor-handler-do.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DurableObject } from "cloudflare:workers";
import { DurableObject, env } from "cloudflare:workers";
import type {
ActorKey,
ActorRouter,
Expand All @@ -16,7 +16,7 @@ import {
CloudflareDurableObjectGlobalState,
createCloudflareActorsActorDriverBuilder,
} from "./actor-driver";
import { type Bindings, CF_AMBIENT_ENV } from "./handler";
import type { Bindings } from "./handler";
import { logger } from "./log";

export const KEYS = {
Expand Down Expand Up @@ -70,8 +70,6 @@ export function createActorDurableObject(
#actor?: LoadedActor;

async #loadActor(): Promise<LoadedActor> {
// This is always called from another context using CF_AMBIENT_ENV

// Wait for init
if (!this.#initialized) {
// Wait for init
Expand Down Expand Up @@ -112,7 +110,7 @@ export function createActorDurableObject(
// of knowing when the DO shuts down. We're making a broad assumption
// that DO will boot a new isolate frequenlty enough that this is not an issue.
const actorId = this.ctx.id.toString();
globalState.setDOState(actorId, { ctx: this.ctx, env: this.env });
globalState.setDOState(actorId, { ctx: this.ctx, env: env });

// Configure actor driver
runConfig.driver.actor =
Expand Down Expand Up @@ -156,59 +154,53 @@ export function createActorDurableObject(
async initialize(req: ActorInitRequest) {
// TODO: Need to add this to a core promise that needs to be resolved before start

return await CF_AMBIENT_ENV.run(this.env, async () => {
await this.ctx.storage.put({
[KEYS.NAME]: req.name,
[KEYS.KEY]: req.key,
[KEYS.PERSIST_DATA]: serializeEmptyPersistData(req.input),
});
this.#initialized = {
name: req.name,
key: req.key,
};

logger().debug("initialized actor", { key: req.key });

// Preemptively actor so the lifecycle hooks are called
await this.#loadActor();
await this.ctx.storage.put({
[KEYS.NAME]: req.name,
[KEYS.KEY]: req.key,
[KEYS.PERSIST_DATA]: serializeEmptyPersistData(req.input),
});
this.#initialized = {
name: req.name,
key: req.key,
};

logger().debug("initialized actor", { key: req.key });

// Preemptively actor so the lifecycle hooks are called
await this.#loadActor();
}

async fetch(request: Request): Promise<Response> {
return await CF_AMBIENT_ENV.run(this.env, async () => {
const { actorRouter } = await this.#loadActor();
const { actorRouter } = await this.#loadActor();

const actorId = this.ctx.id.toString();
return await actorRouter.fetch(request, {
actorId,
});
const actorId = this.ctx.id.toString();
return await actorRouter.fetch(request, {
actorId,
});
}

async alarm(): Promise<void> {
return await CF_AMBIENT_ENV.run(this.env, async () => {
await this.#loadActor();
const actorId = this.ctx.id.toString();

// Get the actor driver
const managerDriver = runConfig.driver.manager(
registry.config,
runConfig,
);
const inlineClient = createClientWithDriver(
createInlineClientDriver(managerDriver),
);
const actorDriver = runConfig.driver.actor(
registry.config,
runConfig,
managerDriver,
inlineClient,
);

// Load the actor instance and trigger alarm
const actor = await actorDriver.loadActor(actorId);
await actor.onAlarm();
});
await this.#loadActor();
const actorId = this.ctx.id.toString();

// Get the actor driver
const managerDriver = runConfig.driver.manager(
registry.config,
runConfig,
);
const inlineClient = createClientWithDriver(
createInlineClientDriver(managerDriver),
);
const actorDriver = runConfig.driver.actor(
registry.config,
runConfig,
managerDriver,
inlineClient,
);

// Load the actor instance and trigger alarm
const actor = await actorDriver.loadActor(actorId);
await actor.onAlarm();
}
};
}
11 changes: 3 additions & 8 deletions packages/drivers/cloudflare-workers/src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { AsyncLocalStorage } from "node:async_hooks";
import { env } from "cloudflare:workers";
import type { Registry, RunConfig } from "@rivetkit/core";
import type { Client } from "@rivetkit/core/client";
import { Hono } from "hono";
import invariant from "invariant";
import {
type ActorHandlerInterface,
createActorDurableObject,
Expand All @@ -23,12 +22,8 @@ export interface Bindings {
*
* Use getCloudflareAmbientEnv unless using CF_AMBIENT_ENV.run.
*/
export const CF_AMBIENT_ENV = new AsyncLocalStorage<Bindings>();

export function getCloudflareAmbientEnv(): Bindings {
const env = CF_AMBIENT_ENV.getStore();
invariant(env, "missing CF_AMBIENT_ENV");
return env;
return env as unknown as Bindings;
}

interface Handler {
Expand Down Expand Up @@ -87,7 +82,7 @@ export function createServer<R extends Registry<any>>(
// Create Cloudflare handler
const handler = {
fetch: (request, env, ctx) => {
return CF_AMBIENT_ENV.run(env, () => app.fetch(request, env, ctx));
return app.fetch(request, env, ctx);
},
} satisfies ExportedHandler<Bindings>;

Expand Down
Loading