Skip to content

Commit 1b34878

Browse files
authored
fix: remove __STATIC_CONTENT_MANIFEST from module worker env (#4505)
1 parent 04a2d0e commit 1b34878

File tree

5 files changed

+42
-22
lines changed

5 files changed

+42
-22
lines changed

.changeset/soft-chairs-share.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"miniflare": patch
3+
---
4+
5+
fix: remove `__STATIC_CONTENT_MANIFEST` from module worker `env`
6+
7+
When using Workers Sites with a module worker, the asset manifest must be imported from the `__STATIC_CONTENT_MANIFEST` virtual module. Miniflare provided this module, but also erroneously added `__STATIC_CONTENT_MANIFEST` to the `env` object too. Whilst this didn't break anything locally, it could cause users to develop Workers that ran locally, but not when deployed. This change ensures `env` doesn't contain `__STATIC_CONTENT_MANIFEST`.

packages/miniflare/src/index.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import {
6262
getDirectSocketName,
6363
getGlobalServices,
6464
kProxyNodeBinding,
65-
maybeGetSitesManifestModule,
6665
normaliseDurableObject,
6766
} from "./plugins";
6867
import {
@@ -104,6 +103,7 @@ import {
104103
LogLevel,
105104
Mutex,
106105
SharedHeaders,
106+
SiteBindings,
107107
} from "./workers";
108108
import { _formatZodError } from "./zod-format";
109109

@@ -996,6 +996,7 @@ export class Miniflare {
996996
for (let i = 0; i < allWorkerOpts.length; i++) {
997997
const workerOpts = allWorkerOpts[i];
998998
const workerName = workerOpts.core.name ?? "";
999+
const isModulesWorker = Boolean(workerOpts.core.modules);
9991000

10001001
// Collect all bindings from this worker
10011002
const workerBindings: Worker_Binding[] = [];
@@ -1007,7 +1008,23 @@ export class Miniflare {
10071008
const pluginBindings = await plugin.getBindings(workerOpts[key], i);
10081009
if (pluginBindings !== undefined) {
10091010
for (const binding of pluginBindings) {
1010-
workerBindings.push(binding);
1011+
// If this is the Workers Sites manifest, we need to add it as a
1012+
// module for modules workers. For all other bindings, and in
1013+
// service workers, just add to worker bindings.
1014+
if (
1015+
key === "kv" &&
1016+
binding.name === SiteBindings.JSON_SITE_MANIFEST &&
1017+
isModulesWorker
1018+
) {
1019+
assert("json" in binding && binding.json !== undefined);
1020+
additionalModules.push({
1021+
name: SiteBindings.JSON_SITE_MANIFEST,
1022+
text: binding.json,
1023+
});
1024+
} else {
1025+
workerBindings.push(binding);
1026+
}
1027+
10111028
// Only `workerd` native bindings need to be proxied, the rest are
10121029
// already supported by Node.js (e.g. json, text/data blob, wasm)
10131030
if (isNativeTargetBinding(binding)) {
@@ -1032,12 +1049,6 @@ export class Miniflare {
10321049
}
10331050
}
10341051
}
1035-
1036-
if (key === "kv") {
1037-
// Add "__STATIC_CONTENT_MANIFEST" module if sites enabled
1038-
const module = maybeGetSitesManifestModule(pluginBindings);
1039-
if (module !== undefined) additionalModules.push(module);
1040-
}
10411052
}
10421053
}
10431054

packages/miniflare/src/plugins/kv/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,4 @@ export const KV_PLUGIN: Plugin<
146146
},
147147
};
148148

149-
export { maybeGetSitesManifestModule } from "./sites";
150149
export { KV_PLUGIN_NAME };

packages/miniflare/src/plugins/kv/sites.ts

+1-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "assert";
22
import fs from "fs/promises";
33
import path from "path";
44
import SCRIPT_KV_SITES from "worker:kv/sites";
5-
import { Service, Worker_Binding, Worker_Module } from "../../runtime";
5+
import { Service, Worker_Binding } from "../../runtime";
66
import { globsToRegExps } from "../../shared";
77
import {
88
SharedBindings,
@@ -102,17 +102,6 @@ export async function getSitesNodeBindings(
102102
};
103103
}
104104

105-
export function maybeGetSitesManifestModule(
106-
bindings: Worker_Binding[]
107-
): Worker_Module | undefined {
108-
for (const binding of bindings) {
109-
if (binding.name === SiteBindings.JSON_SITE_MANIFEST) {
110-
assert("json" in binding && binding.json !== undefined);
111-
return { name: SiteBindings.JSON_SITE_MANIFEST, text: binding.json };
112-
}
113-
}
114-
}
115-
116105
export function getSitesServices(options: SitesOptions): Service[] {
117106
// `siteRegExps` should've been set in `getSitesBindings()`, and `options`
118107
// should be the same object reference as before.

packages/miniflare/test/fixtures/sites/modules.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,22 @@ import { getAssetFromKV } from "@cloudflare/kv-asset-handler";
22
import manifestJSON from "__STATIC_CONTENT_MANIFEST";
33
const manifest = JSON.parse(manifestJSON);
44

5-
export default <ExportedHandler<{ __STATIC_CONTENT: KVNamespace }>>{
5+
export default <
6+
ExportedHandler<{
7+
__STATIC_CONTENT: KVNamespace;
8+
__STATIC_CONTENT_MANIFEST?: undefined;
9+
}>
10+
>{
611
async fetch(request, env, ctx) {
12+
if (
13+
"__STATIC_CONTENT_MANIFEST" in env ||
14+
env.__STATIC_CONTENT_MANIFEST !== undefined
15+
) {
16+
return new Response(
17+
"Expected __STATIC_CONTENT_MANIFEST to be undefined",
18+
{ status: 500 }
19+
);
20+
}
721
return await getAssetFromKV(
822
{
923
request,

0 commit comments

Comments
 (0)