Skip to content

Commit

Permalink
feat(wrangler): plumb the Asset SErver Worker into miniflare
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmenPopoviciu committed Jul 31, 2024
1 parent 7783dbf commit e0f05f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/wrangler/scripts/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const EXTERNAL_DEPENDENCIES = [
// and read when we are bundling the worker application
"unenv",
"workerd/worker.mjs",
"@cloudflare/workers-shared/dist",
];

const pathToPackageJson = path.resolve(__dirname, "..", "package.json");
Expand Down
39 changes: 37 additions & 2 deletions packages/wrangler/src/dev/miniflare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "node:assert";
import { randomUUID } from "node:crypto";
import path from "node:path";
import path, { dirname } from "node:path";
import * as esmLexer from "es-module-lexer";
import {
CoreHeaders,
Expand All @@ -16,6 +16,7 @@ import {
EXTERNAL_AI_WORKER_NAME,
EXTERNAL_AI_WORKER_SCRIPT,
} from "../ai/fetcher";
import { ExperimentalAssets } from "../config/environment";
import { ModuleTypeToRuleType } from "../deployment-bundle/module-collection";
import { withSourceURLs } from "../deployment-bundle/source-url";
import { UserError } from "../errors";
Expand Down Expand Up @@ -172,6 +173,7 @@ export interface ConfigBundle {
bindings: CfWorkerInit["bindings"];
workerDefinitions: WorkerRegistry | undefined;
legacyAssetPaths: LegacyAssetPaths | undefined;
experimentalAssets: ExperimentalAssets | undefined;
initialPort: Port;
initialIp: string;
rules: Config["rules"];
Expand Down Expand Up @@ -374,7 +376,7 @@ type MiniflareBindingsConfig = Pick<
| "services"
| "serviceBindings"
> &
Partial<Pick<ConfigBundle, "format" | "bundle">>;
Partial<Pick<ConfigBundle, "format" | "bundle" | "experimentalAssets">>;

// TODO(someday): would be nice to type these methods more, can we export types for
// each plugin options schema and use those
Expand Down Expand Up @@ -408,7 +410,9 @@ export function buildMiniflareBindingOptions(config: MiniflareBindingsConfig): {
// Setup service bindings to external services
const serviceBindings: NonNullable<WorkerOptions["serviceBindings"]> = {
...config.serviceBindings,
...(config.experimentalAssets ? { ASSET_SERVER: "asset-server" } : {}),
};

const notFoundServices = new Set<string>();
for (const service of config.services ?? []) {
if (service.service === config.name) {
Expand Down Expand Up @@ -837,6 +841,31 @@ export async function buildMiniflareOptions(
}
}

// TODO @Carmen can we read the toml config instead?
const assetServerModulePath = require.resolve(
"@cloudflare/workers-shared/dist/asset-server-worker.mjs"
);
const assetServerWorker: WorkerOptions | undefined = config.experimentalAssets
? {
name: "asset-server",
compatibilityDate: "2024-01-01",
compatibilityFlags: ["nodejs_compat"],
modulesRoot: dirname(assetServerModulePath),
modules: [
{
type: "ESModule",
path: assetServerModulePath,
},
],
unsafeDirectSockets: [
{
host: "127.0.0.1",
port: 0,
},
],
}
: undefined;

const upstream =
typeof config.localUpstream === "string"
? `${config.upstreamProtocol}://${config.localUpstream}`
Expand Down Expand Up @@ -879,6 +908,9 @@ export async function buildMiniflareOptions(
proxy: true,
})),
},
...(config.experimentalAssets
? [assetServerWorker as WorkerOptions]
: []),
...externalWorkers,
],
};
Expand Down Expand Up @@ -947,9 +979,11 @@ export class MiniflareServer extends TypedEventTarget<MiniflareServerEventMap> {
config,
this.#proxyToUserWorkerAuthenticationSecret
);

if (opts?.signal?.aborted) {
return;
}

if (this.#mf === undefined) {
this.#mf = new Miniflare(options);
} else {
Expand Down Expand Up @@ -981,6 +1015,7 @@ export class MiniflareServer extends TypedEventTarget<MiniflareServerEventMap> {
this.dispatchEvent(new ErrorEvent("error", { error }));
}
}

onBundleUpdate(config: ConfigBundle, opts?: Abortable): Promise<void> {
return this.#mutex.runWith(() => this.#onBundleUpdate(config, opts));
}
Expand Down

0 comments on commit e0f05f4

Please sign in to comment.