From e695ec6455059299e17f182611e0827bd46bfa8c Mon Sep 17 00:00:00 2001 From: Carmen Popoviciu Date: Tue, 30 Jul 2024 14:54:08 +0200 Subject: [PATCH] feat(wrangler): plumb the Asset SErver Worker into miniflare --- packages/wrangler/src/dev/miniflare.ts | 41 ++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/packages/wrangler/src/dev/miniflare.ts b/packages/wrangler/src/dev/miniflare.ts index c3bb37498900..46bd328510d0 100644 --- a/packages/wrangler/src/dev/miniflare.ts +++ b/packages/wrangler/src/dev/miniflare.ts @@ -1,6 +1,6 @@ import assert from "node:assert"; import { randomUUID } from "node:crypto"; -import path from "node:path"; +import path, { resolve } from "node:path"; import * as esmLexer from "es-module-lexer"; import { CoreHeaders, @@ -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"; @@ -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"]; @@ -374,7 +376,7 @@ type MiniflareBindingsConfig = Pick< | "services" | "serviceBindings" > & - Partial>; + Partial>; // TODO(someday): would be nice to type these methods more, can we export types for // each plugin options schema and use those @@ -408,7 +410,9 @@ export function buildMiniflareBindingOptions(config: MiniflareBindingsConfig): { // Setup service bindings to external services const serviceBindings: NonNullable = { ...config.serviceBindings, + ...(config.experimentalAssets ? { ASSET_SERVER: "asset-server" } : {}), }; + const notFoundServices = new Set(); for (const service of config.services ?? []) { if (service.service === config.name) { @@ -837,6 +841,33 @@ export async function buildMiniflareOptions( } } + // TODO @Carmen can we read the toml config instead? + const assetServerWorker: WorkerOptions | undefined = config.experimentalAssets + ? { + name: "asset-server", + compatibilityDate: "2024-01-01", + compatibilityFlags: ["nodejs_compat"], + modulesRoot: resolve( + "../../packages/workers-shared/asset-server-worker/src/index.ts" + ), + modules: [ + { + type: "ESModule", + path: resolve( + // TODO @Carmen this should point to a bundled worker (/dist/index.mjs) + "../../packages/workers-shared/asset-server-worker/src/index.ts" + ), + }, + ], + unsafeDirectSockets: [ + { + host: "127.0.0.1", + port: 0, + }, + ], + } + : undefined; + const upstream = typeof config.localUpstream === "string" ? `${config.upstreamProtocol}://${config.localUpstream}` @@ -879,6 +910,9 @@ export async function buildMiniflareOptions( proxy: true, })), }, + ...(config.experimentalAssets + ? [assetServerWorker as WorkerOptions] + : []), ...externalWorkers, ], }; @@ -947,9 +981,11 @@ export class MiniflareServer extends TypedEventTarget { config, this.#proxyToUserWorkerAuthenticationSecret ); + if (opts?.signal?.aborted) { return; } + if (this.#mf === undefined) { this.#mf = new Miniflare(options); } else { @@ -981,6 +1017,7 @@ export class MiniflareServer extends TypedEventTarget { this.dispatchEvent(new ErrorEvent("error", { error })); } } + onBundleUpdate(config: ConfigBundle, opts?: Abortable): Promise { return this.#mutex.runWith(() => this.#onBundleUpdate(config, opts)); }