Skip to content

Commit

Permalink
ensure platform proxy is up-to-date
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Nov 19, 2024
1 parent a6f766b commit 77a3aaf
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
6 changes: 2 additions & 4 deletions packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,11 @@ export async function getPlatformProxy<
},
});

const mf = await worker.getLocalMiniflareInstance();
const bindings: Env = await mf.getBindings();
const cf = await mf.getCf();
const { env, cf } = await worker.getPlatformProxy();
deepFreeze(cf);

return {
env: bindings,
env: env as Env,
cf: cf as CfProperties,
ctx: new ExecutionContext(),
caches: new CacheStorage(),
Expand Down
9 changes: 7 additions & 2 deletions packages/wrangler/src/api/startDevWorker/DevEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function createWorkerObject(devEnv: DevEnv): Worker {
const w = await proxyWorker.getWorker(this.config.name);
return w.scheduled(...args);
},
async getLocalMiniflareInstance() {
async getPlatformProxy() {
const local = devEnv.runtimes.find(
(ctrl) => ctrl instanceof LocalRuntimeController
);
Expand All @@ -187,7 +187,12 @@ function createWorkerObject(devEnv: DevEnv): Worker {
throw new Error("local only");
}

return await local.getMiniflareInstance();
const [env, cf] = await Promise.all([local.getBindings(), local.getCf()]);

return {
env,
cf,
};
},
async dispose() {
await devEnv.teardown();
Expand Down
49 changes: 49 additions & 0 deletions packages/wrangler/src/api/startDevWorker/LocalRuntimeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ export class LocalRuntimeController extends RuntimeController {
#mutex = new Mutex();
#mf?: Miniflare;

#bindings?: Record<string, unknown>;
#cf?: Record<string, unknown>;

async getMiniflareInstance() {
if (this.#mf) {
return this.#mf;
Expand All @@ -157,6 +160,42 @@ export class LocalRuntimeController extends RuntimeController {
return this.#mf;
}

async getBindings(): Promise<Record<string, unknown>> {
const mf = await this.getMiniflareInstance();

this.#bindings = await mf.getBindings();

// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;

return new Proxy(
{},
{
get(_, prop, receiver) {
return Reflect.get(self.#bindings ?? {}, prop, receiver);
},
}
);
}

async getCf(): Promise<Record<string, unknown>> {
const mf = await this.getMiniflareInstance();

this.#cf = await mf.getCf();

// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;

return new Proxy(
{},
{
get(_, prop, receiver) {
return Reflect.get(self.#cf ?? {}, prop, receiver);
},
}
);
}

onBundleStart(_: BundleStartEvent) {
// Ignored in local runtime
}
Expand Down Expand Up @@ -190,6 +229,16 @@ export class LocalRuntimeController extends RuntimeController {
return;
}

if (this.#bindings) {
// Re-fetch bindings to ensure they're up-to-date
this.#bindings = await this.#mf.getBindings();
}

if (this.#cf) {
// Re-fetch `CfProperties` to ensure they're up-to-date
this.#cf = await this.#mf.getCf();
}

// Get entrypoint addresses
const entrypointAddresses: WorkerEntrypointsDefinition = {};
for (const name of entrypointNames) {
Expand Down
5 changes: 4 additions & 1 deletion packages/wrangler/src/api/startDevWorker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export interface Worker {
fetch: DispatchFetch;
scheduled: MiniflareWorker["scheduled"];
queue: MiniflareWorker["queue"];
getLocalMiniflareInstance(): Promise<Miniflare>;
getPlatformProxy(): Promise<{
env: Record<string, unknown>;
cf: Record<string, unknown>;
}>;
dispose(): Promise<void>;
}

Expand Down

0 comments on commit 77a3aaf

Please sign in to comment.