Skip to content

Commit 2d40989

Browse files
authored
Revert "Revert "Upload metafiles for Workers Assets (#8255)" (#8336)" (#8338)
This reverts commit 00cf67c.
1 parent 3f475e5 commit 2d40989

File tree

29 files changed

+382
-240
lines changed

29 files changed

+382
-240
lines changed

.changeset/chatty-seas-grin.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"wrangler": minor
3+
"@cloudflare/vitest-pool-workers": patch
4+
"@cloudflare/workers-shared": patch
5+
"miniflare": patch
6+
---
7+
8+
feat: Upload \_headers and \_redirects if present with Workers Assets as part of `wrangler deploy` and `wrangler versions upload`.

fixtures/asset-config/html-handling.test.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ describe.each(testSuites)("$title", ({ title, suite }) => {
5959
vi.mocked(getAssetWithMetadataFromKV).mockRestore();
6060
});
6161
describe.each(suite)(`$html_handling`, ({ html_handling, cases }) => {
62-
beforeEach(() => {
63-
vi.mocked(applyConfigurationDefaults).mockImplementation(() => {
64-
return {
65-
html_handling,
66-
not_found_handling: "none",
67-
run_worker_first: true,
68-
serve_directly: false,
69-
};
70-
});
62+
beforeEach(async () => {
63+
const originalApplyConfigurationDefaults = (
64+
await vi.importActual<
65+
typeof import("../../packages/workers-shared/asset-worker/src/configuration")
66+
>("../../packages/workers-shared/asset-worker/src/configuration")
67+
).applyConfigurationDefaults;
68+
vi.mocked(applyConfigurationDefaults).mockImplementation(() => ({
69+
...originalApplyConfigurationDefaults({}),
70+
html_handling,
71+
not_found_handling: "none",
72+
}));
7173
});
7274
it.each(cases)(
7375
"$title",

fixtures/asset-config/redirects.test.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("[Asset Worker] `test location rewrite`", () => {
2424
afterEach(() => {
2525
vi.mocked(getAssetWithMetadataFromKV).mockRestore();
2626
});
27-
beforeEach(() => {
27+
beforeEach(async () => {
2828
vi.mocked(getAssetWithMetadataFromKV).mockImplementation(
2929
() =>
3030
Promise.resolve({
@@ -37,14 +37,16 @@ describe("[Asset Worker] `test location rewrite`", () => {
3737
>
3838
);
3939

40-
vi.mocked(applyConfigurationDefaults).mockImplementation(() => {
41-
return {
42-
html_handling: "none",
43-
not_found_handling: "none",
44-
run_worker_first: true,
45-
serve_directly: false,
46-
};
47-
});
40+
const originalApplyConfigurationDefaults = (
41+
await vi.importActual<
42+
typeof import("../../packages/workers-shared/asset-worker/src/configuration")
43+
>("../../packages/workers-shared/asset-worker/src/configuration")
44+
).applyConfigurationDefaults;
45+
vi.mocked(applyConfigurationDefaults).mockImplementation(() => ({
46+
...originalApplyConfigurationDefaults({}),
47+
html_handling: "none",
48+
not_found_handling: "none",
49+
}));
4850
});
4951

5052
it("returns 404 for non matched encoded url", async () => {

fixtures/asset-config/url-normalization.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SELF } from "cloudflare:test";
2-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { afterEach, beforeEach, describe, expect, it, Mock, vi } from "vitest";
33
import { applyConfigurationDefaults } from "../../packages/workers-shared/asset-worker/src/configuration";
44
import Worker from "../../packages/workers-shared/asset-worker/src/index";
55
import { getAssetWithMetadataFromKV } from "../../packages/workers-shared/asset-worker/src/utils/kv";
@@ -24,7 +24,7 @@ describe("[Asset Worker] `test slash normalization`", () => {
2424
afterEach(() => {
2525
vi.mocked(getAssetWithMetadataFromKV).mockRestore();
2626
});
27-
beforeEach(() => {
27+
beforeEach(async () => {
2828
vi.mocked(getAssetWithMetadataFromKV).mockImplementation(
2929
() =>
3030
Promise.resolve({
@@ -37,14 +37,16 @@ describe("[Asset Worker] `test slash normalization`", () => {
3737
>
3838
);
3939

40-
vi.mocked(applyConfigurationDefaults).mockImplementation(() => {
41-
return {
42-
html_handling: "none",
43-
not_found_handling: "none",
44-
run_worker_first: true,
45-
serve_directly: false,
46-
};
47-
});
40+
const originalApplyConfigurationDefaults = (
41+
await vi.importActual<
42+
typeof import("../../packages/workers-shared/asset-worker/src/configuration")
43+
>("../../packages/workers-shared/asset-worker/src/configuration")
44+
).applyConfigurationDefaults;
45+
vi.mocked(applyConfigurationDefaults).mockImplementation(() => ({
46+
...originalApplyConfigurationDefaults({}),
47+
html_handling: "none",
48+
not_found_handling: "none",
49+
}));
4850
});
4951

5052
it("returns 200 leading encoded double slash", async () => {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
9999
const assetService: Service = {
100100
name: `${ASSETS_SERVICE_NAME}:${id}`,
101101
worker: {
102-
compatibilityDate: "2024-08-01",
102+
// TODO: read these from the wrangler.toml
103+
compatibilityDate: "2024-07-31",
104+
compatibilityFlags: ["nodejs_compat"],
103105
modules: [
104106
{
105107
name: "asset-worker.mjs",
@@ -128,7 +130,9 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
128130
const routerService: Service = {
129131
name: `${ROUTER_SERVICE_NAME}:${id}`,
130132
worker: {
131-
compatibilityDate: "2024-08-01",
133+
// TODO: read these from the wrangler.toml
134+
compatibilityDate: "2024-07-31",
135+
compatibilityFlags: ["nodejs_compat", "no_nodejs_compat_v2"],
132136
modules: [
133137
{
134138
name: "router-worker.mjs",
@@ -148,7 +152,7 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
148152
},
149153
{
150154
name: "CONFIG",
151-
json: JSON.stringify(options.assets.routingConfig),
155+
json: JSON.stringify(options.assets.routerConfig),
152156
},
153157
],
154158
},

packages/miniflare/src/plugins/assets/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
AssetConfigSchema,
3-
RoutingConfigSchema,
3+
RouterConfigSchema,
44
} from "@cloudflare/workers-shared";
55
import { z } from "zod";
66
import { PathSchema } from "../../shared";
@@ -13,7 +13,7 @@ export const AssetsOptionsSchema = z.object({
1313
workerName: z.string().optional(),
1414
directory: PathSchema,
1515
binding: z.string().optional(),
16-
routingConfig: RoutingConfigSchema.optional(),
16+
routerConfig: RouterConfigSchema.optional(),
1717
assetConfig: AssetConfigSchema.optional(),
1818
})
1919
.optional(),

packages/vitest-pool-workers/src/pool/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ async function parseCustomPoolOptions(
218218
if (options.miniflare?.assets) {
219219
// (Used to set the SELF binding to point to the router worker instead)
220220
options.miniflare.hasAssetsAndIsVitest = true;
221-
options.miniflare.assets.routingConfig ??= {};
222-
options.miniflare.assets.routingConfig.has_user_worker = Boolean(
221+
options.miniflare.assets.routerConfig ??= {};
222+
options.miniflare.assets.routerConfig.has_user_worker = Boolean(
223223
options.main
224224
);
225225
}

packages/workers-shared/asset-worker/src/configuration.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@ import type { AssetConfig } from "../../utils/types";
33
export const applyConfigurationDefaults = (
44
configuration?: AssetConfig
55
): Required<AssetConfig> => {
6-
let runWorkerFirst = undefined;
7-
if (configuration?.run_worker_first !== undefined) {
8-
runWorkerFirst = configuration?.run_worker_first;
9-
} else if (configuration?.serve_directly !== undefined) {
10-
runWorkerFirst = !configuration.serve_directly;
11-
} else {
12-
runWorkerFirst = false;
13-
}
14-
156
return {
7+
compatibility_date: configuration?.compatibility_date ?? "2021-11-02",
8+
compatibility_flags: configuration?.compatibility_flags ?? [],
169
html_handling: configuration?.html_handling ?? "auto-trailing-slash",
1710
not_found_handling: configuration?.not_found_handling ?? "none",
18-
run_worker_first: runWorkerFirst,
19-
serve_directly: !runWorkerFirst,
11+
account_id: configuration?.account_id ?? -1,
12+
script_id: configuration?.script_id ?? -1,
2013
};
2114
};

packages/workers-shared/asset-worker/src/handler.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ export const handleRequest = async (
3838
});
3939
}
4040

41-
// if there was a POST etc. to a route without an asset
42-
// this should be passed onto a user worker if one exists
43-
// so prioritise returning a 404 over 405?
4441
const method = request.method.toUpperCase();
4542
if (!["GET", "HEAD"].includes(method)) {
4643
return env.JAEGER.enterSpan("method_not_allowed", (span) => {
@@ -721,7 +718,7 @@ const safeRedirect = async (
721718
* +===========================================+===========+======================+
722719
* | unreserved ASCII e.g. a-z | unchanged | unchanged |
723720
* +-------------------------------------------+-----------+----------------------+
724-
* | reserved (sometimes encoded) | unchanged | encoded |
721+
* | reserved (sometimes encoded) | unchanged | encoded |
725722
* | e.g. [ ] @ $ ! ' ( ) * + , ; = : ? # & % | | |
726723
* +-------------------------------------------+-----------+----------------------+
727724
* | non-ASCII e.g. ü. and space | encoded | encoded |

packages/workers-shared/asset-worker/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ExperimentAnalytics } from "./experiment-analytics";
1010
import { decodePath, getIntent, handleRequest } from "./handler";
1111
import { getAssetWithMetadataFromKV } from "./utils/kv";
1212
import type {
13-
AssetWorkerConfig,
13+
AssetConfig,
1414
ColoMetadata,
1515
JaegerTracing,
1616
UnsafePerformanceTimer,
@@ -31,7 +31,7 @@ export type Env = {
3131
*/
3232
ASSETS_KV_NAMESPACE: KVNamespace;
3333

34-
CONFIG: AssetWorkerConfig;
34+
CONFIG: AssetConfig;
3535

3636
SENTRY_DSN: string;
3737
SENTRY_ACCESS_CLIENT_ID: string;

0 commit comments

Comments
 (0)