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 30, 2024
1 parent 9e63082 commit e695ec6
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 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, { resolve } 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";

Check failure on line 19 in packages/wrangler/src/dev/miniflare.ts

View workflow job for this annotation

GitHub Actions / Checks

All imports in the declaration are only used as types. Use `import type`
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,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}`
Expand Down Expand Up @@ -879,6 +910,9 @@ export async function buildMiniflareOptions(
proxy: true,
})),
},
...(config.experimentalAssets
? [assetServerWorker as WorkerOptions]
: []),
...externalWorkers,
],
};
Expand Down Expand Up @@ -947,9 +981,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 +1017,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 e695ec6

Please sign in to comment.