Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit e5207db

Browse files
committed
fix(cf): remove asynclocalstorage dep (#1157)
1 parent 812453a commit e5207db

File tree

2 files changed

+44
-57
lines changed

2 files changed

+44
-57
lines changed

packages/drivers/cloudflare-workers/src/actor-handler-do.ts

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DurableObject } from "cloudflare:workers";
1+
import { DurableObject, env } from "cloudflare:workers";
22
import type {
33
ActorKey,
44
ActorRouter,
@@ -16,7 +16,7 @@ import {
1616
CloudflareDurableObjectGlobalState,
1717
createCloudflareActorsActorDriverBuilder,
1818
} from "./actor-driver";
19-
import { type Bindings, CF_AMBIENT_ENV } from "./handler";
19+
import type { Bindings } from "./handler";
2020
import { logger } from "./log";
2121

2222
export const KEYS = {
@@ -70,8 +70,6 @@ export function createActorDurableObject(
7070
#actor?: LoadedActor;
7171

7272
async #loadActor(): Promise<LoadedActor> {
73-
// This is always called from another context using CF_AMBIENT_ENV
74-
7573
// Wait for init
7674
if (!this.#initialized) {
7775
// Wait for init
@@ -112,7 +110,7 @@ export function createActorDurableObject(
112110
// of knowing when the DO shuts down. We're making a broad assumption
113111
// that DO will boot a new isolate frequenlty enough that this is not an issue.
114112
const actorId = this.ctx.id.toString();
115-
globalState.setDOState(actorId, { ctx: this.ctx, env: this.env });
113+
globalState.setDOState(actorId, { ctx: this.ctx, env: env });
116114

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

159-
return await CF_AMBIENT_ENV.run(this.env, async () => {
160-
await this.ctx.storage.put({
161-
[KEYS.NAME]: req.name,
162-
[KEYS.KEY]: req.key,
163-
[KEYS.PERSIST_DATA]: serializeEmptyPersistData(req.input),
164-
});
165-
this.#initialized = {
166-
name: req.name,
167-
key: req.key,
168-
};
169-
170-
logger().debug("initialized actor", { key: req.key });
171-
172-
// Preemptively actor so the lifecycle hooks are called
173-
await this.#loadActor();
157+
await this.ctx.storage.put({
158+
[KEYS.NAME]: req.name,
159+
[KEYS.KEY]: req.key,
160+
[KEYS.PERSIST_DATA]: serializeEmptyPersistData(req.input),
174161
});
162+
this.#initialized = {
163+
name: req.name,
164+
key: req.key,
165+
};
166+
167+
logger().debug("initialized actor", { key: req.key });
168+
169+
// Preemptively actor so the lifecycle hooks are called
170+
await this.#loadActor();
175171
}
176172

177173
async fetch(request: Request): Promise<Response> {
178-
return await CF_AMBIENT_ENV.run(this.env, async () => {
179-
const { actorRouter } = await this.#loadActor();
174+
const { actorRouter } = await this.#loadActor();
180175

181-
const actorId = this.ctx.id.toString();
182-
return await actorRouter.fetch(request, {
183-
actorId,
184-
});
176+
const actorId = this.ctx.id.toString();
177+
return await actorRouter.fetch(request, {
178+
actorId,
185179
});
186180
}
187181

188182
async alarm(): Promise<void> {
189-
return await CF_AMBIENT_ENV.run(this.env, async () => {
190-
await this.#loadActor();
191-
const actorId = this.ctx.id.toString();
192-
193-
// Get the actor driver
194-
const managerDriver = runConfig.driver.manager(
195-
registry.config,
196-
runConfig,
197-
);
198-
const inlineClient = createClientWithDriver(
199-
createInlineClientDriver(managerDriver),
200-
);
201-
const actorDriver = runConfig.driver.actor(
202-
registry.config,
203-
runConfig,
204-
managerDriver,
205-
inlineClient,
206-
);
207-
208-
// Load the actor instance and trigger alarm
209-
const actor = await actorDriver.loadActor(actorId);
210-
await actor.onAlarm();
211-
});
183+
await this.#loadActor();
184+
const actorId = this.ctx.id.toString();
185+
186+
// Get the actor driver
187+
const managerDriver = runConfig.driver.manager(
188+
registry.config,
189+
runConfig,
190+
);
191+
const inlineClient = createClientWithDriver(
192+
createInlineClientDriver(managerDriver),
193+
);
194+
const actorDriver = runConfig.driver.actor(
195+
registry.config,
196+
runConfig,
197+
managerDriver,
198+
inlineClient,
199+
);
200+
201+
// Load the actor instance and trigger alarm
202+
const actor = await actorDriver.loadActor(actorId);
203+
await actor.onAlarm();
212204
}
213205
};
214206
}

packages/drivers/cloudflare-workers/src/handler.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { AsyncLocalStorage } from "node:async_hooks";
1+
import { env } from "cloudflare:workers";
22
import type { Registry, RunConfig } from "@rivetkit/core";
33
import type { Client } from "@rivetkit/core/client";
44
import { Hono } from "hono";
5-
import invariant from "invariant";
65
import {
76
type ActorHandlerInterface,
87
createActorDurableObject,
@@ -23,12 +22,8 @@ export interface Bindings {
2322
*
2423
* Use getCloudflareAmbientEnv unless using CF_AMBIENT_ENV.run.
2524
*/
26-
export const CF_AMBIENT_ENV = new AsyncLocalStorage<Bindings>();
27-
2825
export function getCloudflareAmbientEnv(): Bindings {
29-
const env = CF_AMBIENT_ENV.getStore();
30-
invariant(env, "missing CF_AMBIENT_ENV");
31-
return env;
26+
return env as unknown as Bindings;
3227
}
3328

3429
interface Handler {
@@ -87,7 +82,7 @@ export function createServer<R extends Registry<any>>(
8782
// Create Cloudflare handler
8883
const handler = {
8984
fetch: (request, env, ctx) => {
90-
return CF_AMBIENT_ENV.run(env, () => app.fetch(request, env, ctx));
85+
return app.fetch(request, env, ctx);
9186
},
9287
} satisfies ExportedHandler<Bindings>;
9388

0 commit comments

Comments
 (0)