Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Oct 29, 2024
1 parent a25986e commit 2c22d70
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type BindingsProxy<
* @returns An Object containing the generated proxies alongside other related utilities
*/
export async function getBindingsProxy<
Bindings = Record<string, unknown>,
Bindings extends Record<string, unknown> = Record<string, unknown>,
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
>(
options: GetBindingsProxyOptions = {}
Expand Down
65 changes: 38 additions & 27 deletions packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { processAssetsArg } from "../../../assets";
import { readConfig } from "../../../config";
import { DEFAULT_MODULE_RULES } from "../../../deployment-bundle/rules";
import { getBindings } from "../../../dev";
// import { getVarsForDev } from "../../../dev/dev-vars";
import {
buildAssetOptions,
buildMiniflareBindingOptions,
Expand All @@ -14,6 +13,7 @@ import { getLegacyAssetPaths, getSiteAssetPaths } from "../../../sites";
import { startWorker } from "../../startDevWorker";
import { CacheStorage } from "./caches";
import { ExecutionContext } from "./executionContext";
import type { Worker } from "../../startDevWorker";
import type { IncomingRequestCfProperties } from "@cloudflare/workers-types/experimental";
import type { ModuleRule, WorkerOptions } from "miniflare";

Expand Down Expand Up @@ -84,6 +84,8 @@ export type PlatformProxy<
dispose: () => Promise<void>;
};

let worker: Worker | undefined;

/**
* By reading from a `wrangler.toml` file this function generates proxy objects that can be
* used to simulate the interaction with the Cloudflare platform during local development
Expand All @@ -93,40 +95,40 @@ export type PlatformProxy<
* @returns An Object containing the generated proxies alongside other related utilities
*/
export async function getPlatformProxy<
Env = Record<string, unknown>,
Env extends Record<string, unknown> = Record<string, unknown>,
CfProperties extends Record<string, unknown> = IncomingRequestCfProperties,
>(
options: GetPlatformProxyOptions = {}
): Promise<PlatformProxy<Env, CfProperties>> {
// const env = options.environment;
// const rawConfig = readConfig(options.configPath, {
// experimentalJsonConfig: options.experimentalJsonConfig,
// env,
// });

const worker = await startWorker({
config: options.configPath,
env: options.environment,
dev: {
inspector: {
port: 0,
},
server: {
port: 0,
// TODO: Allow skipping custom build

if (!worker) {
worker = await startWorker({
config: options.configPath,
env: options.environment,
dev: {
inspector: {
port: 0,
},
server: {
port: 0,
},
logLevel: "none",
liveReload: false,
watch: false,
persist:
typeof options.persist === "object"
? options.persist.path
: options.persist
? ".wrangler/state/v3"
: undefined,
},
persist:
typeof options.persist === "object"
? options.persist.path
: options.persist
? ".wrangler/state/v3"
: undefined,
},
});
});
}

const mf = await worker.getLocalMiniflareInstance();
const bindings: Env = await mf.getBindings();
const cf = await mf.getCf();
// const vars = getVarsForDev(rawConfig, env);

deepFreeze(cf);

Expand All @@ -135,7 +137,16 @@ export async function getPlatformProxy<
cf: cf as CfProperties,
ctx: new ExecutionContext(),
caches: new CacheStorage(),
dispose: () => mf.dispose(),
dispose: async () => {
if (!worker) {
return;
}

const disposePromise = worker.dispose();
worker = undefined;

await disposePromise;
},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ export class LocalRuntimeController extends RuntimeController {
#mutex = new Mutex();
#mf?: Miniflare;
async getMiniflareInstance() {
if (this.#mf) {
return this.#mf;
}

await events.once(this, "reloadComplete");

if (!this.#mf) {
Expand Down

0 comments on commit 2c22d70

Please sign in to comment.