Skip to content

Commit c91d391

Browse files
authored
Revert "Add support for static routing (i.e. \_routes.json) to Workers Assets (#9280)" (#9374)
This reverts commit b3f290a.
1 parent efdba4f commit c91d391

File tree

11 files changed

+57
-623
lines changed

11 files changed

+57
-623
lines changed

.changeset/tasty-hoops-own.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const normalizeConfiguration = (
2020
version: 2,
2121
rules: {},
2222
},
23-
has_static_routing: configuration?.has_static_routing ?? false,
2423
account_id: configuration?.account_id ?? -1,
2524
script_id: configuration?.script_id ?? -1,
2625
debug: configuration?.debug ?? false,

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,7 @@ export const canFetch = async (
203203
flagIsEnabled(
204204
configuration,
205205
SEC_FETCH_MODE_NAVIGATE_HEADER_PREFERS_ASSET_SERVING
206-
) &&
207-
request.headers.get("Sec-Fetch-Mode") === "navigate" &&
208-
!configuration.has_static_routing
206+
) && request.headers.get("Sec-Fetch-Mode") === "navigate"
209207
)
210208
) {
211209
configuration = {

packages/workers-shared/asset-worker/src/utils/rules-engine.ts

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,6 @@ export const replacer = (str: string, replacements: Replacements) => {
2828
return str;
2929
};
3030

31-
export const generateGlobOnlyRuleRegExp = (rule: string) => {
32-
// Escape all regex characters other than globs (the "*" character) since that's all that's supported.
33-
rule = rule.split("*").map(escapeRegex).join(".*");
34-
35-
// Wrap in line terminators to be safe.
36-
rule = "^" + rule + "$";
37-
38-
return RegExp(rule);
39-
};
40-
41-
export const generateRuleRegExp = (rule: string) => {
42-
// Create :splat capturer then escape.
43-
rule = rule.split("*").map(escapeRegex).join("(?<splat>.*)");
44-
45-
// Create :placeholder capturers (already escaped).
46-
// For placeholders in the host, we separate at forward slashes and periods.
47-
// For placeholders in the path, we separate at forward slashes.
48-
// This matches the behavior of URLPattern.
49-
// e.g. https://:subdomain.domain/ -> https://(here).domain/
50-
// e.g. /static/:file -> /static/(image.jpg)
51-
// e.g. /blog/:post -> /blog/(an-exciting-post)
52-
const host_matches = rule.matchAll(HOST_PLACEHOLDER_REGEX);
53-
for (const host_match of host_matches) {
54-
rule = rule.split(host_match[0]).join(`(?<${host_match[1]}>[^/.]+)`);
55-
}
56-
57-
const path_matches = rule.matchAll(PLACEHOLDER_REGEX);
58-
for (const path_match of path_matches) {
59-
rule = rule.split(path_match[0]).join(`(?<${path_match[1]}>[^/]+)`);
60-
}
61-
62-
// Wrap in line terminators to be safe.
63-
rule = "^" + rule + "$";
64-
65-
return RegExp(rule);
66-
};
67-
6831
export const generateRulesMatcher = <T>(
6932
rules?: Record<string, T>,
7033
replacerFn: (match: T, replacements: Replacements) => T = (match) => match
@@ -77,8 +40,31 @@ export const generateRulesMatcher = <T>(
7740
.map(([rule, match]) => {
7841
const crossHost = rule.startsWith("https://");
7942

43+
// Create :splat capturer then escape.
44+
rule = rule.split("*").map(escapeRegex).join("(?<splat>.*)");
45+
46+
// Create :placeholder capturers (already escaped).
47+
// For placeholders in the host, we separate at forward slashes and periods.
48+
// For placeholders in the path, we separate at forward slashes.
49+
// This matches the behavior of URLPattern.
50+
// e.g. https://:subdomain.domain/ -> https://(here).domain/
51+
// e.g. /static/:file -> /static/(image.jpg)
52+
// e.g. /blog/:post -> /blog/(an-exciting-post)
53+
const host_matches = rule.matchAll(HOST_PLACEHOLDER_REGEX);
54+
for (const host_match of host_matches) {
55+
rule = rule.split(host_match[0]).join(`(?<${host_match[1]}>[^/.]+)`);
56+
}
57+
58+
const path_matches = rule.matchAll(PLACEHOLDER_REGEX);
59+
for (const path_match of path_matches) {
60+
rule = rule.split(path_match[0]).join(`(?<${path_match[1]}>[^/]+)`);
61+
}
62+
63+
// Wrap in line terminators to be safe.
64+
rule = "^" + rule + "$";
65+
8066
try {
81-
const regExp = generateRuleRegExp(rule);
67+
const regExp = new RegExp(rule);
8268
return [{ crossHost, regExp }, match];
8369
} catch {}
8470
})
@@ -145,19 +131,3 @@ export const generateRedirectsMatcher = (
145131
to: replacer(to, replacements),
146132
})
147133
);
148-
149-
export const generateStaticRoutingRuleMatcher =
150-
(rules: string[]) =>
151-
({ request }: { request: Request }) => {
152-
const { pathname } = new URL(request.url);
153-
for (const rule of rules) {
154-
try {
155-
const regExp = generateGlobOnlyRuleRegExp(rule);
156-
if (regExp.test(pathname)) {
157-
return true;
158-
}
159-
} catch {}
160-
}
161-
162-
return false;
163-
};

packages/workers-shared/asset-worker/tests/handler.test.ts

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,56 +1325,41 @@ describe("[Asset Worker] `canFetch`", () => {
13251325
[{ "Sec-Fetch-Mode": "cors" }, false],
13261326
] as const;
13271327

1328-
const staticRoutingModes = [
1329-
[false, true],
1330-
[true, false],
1331-
] as const;
1332-
13331328
const matrix = [];
13341329
for (const compatibilityOptions of compatibilityOptionsModes) {
13351330
for (const notFoundHandling of notFoundHandlingModes) {
13361331
for (const headers of headersModes) {
1337-
for (const hasStaticRouting of staticRoutingModes) {
1338-
matrix.push({
1339-
compatibilityDate: compatibilityOptions[0].compatibilityDate,
1340-
compatibilityFlags: compatibilityOptions[0].compatibilityFlags,
1341-
notFoundHandling: notFoundHandling[0],
1342-
headers: headers[0],
1343-
hasStaticRouting: hasStaticRouting[0],
1344-
expected:
1345-
compatibilityOptions[1] &&
1346-
notFoundHandling[1] &&
1347-
headers[1] &&
1348-
hasStaticRouting[1],
1349-
});
1350-
}
1332+
matrix.push({
1333+
compatibilityDate: compatibilityOptions[0].compatibilityDate,
1334+
compatibilityFlags: compatibilityOptions[0].compatibilityFlags,
1335+
notFoundHandling: notFoundHandling[0],
1336+
headers: headers[0],
1337+
expected:
1338+
compatibilityOptions[1] && notFoundHandling[1] && headers[1],
1339+
});
13511340
}
13521341
}
13531342
}
13541343

13551344
it.each(matrix)(
1356-
"compatibility_date $compatibilityDate, compatibility_flags $compatibilityFlags, not_found_handling $notFoundHandling, headers: $headers, hasStaticRouting $hasStaticRouting -> $expected",
1345+
"compatibility_date $compatibilityDate, compatibility_flags $compatibilityFlags, not_found_handling $notFoundHandling, headers: $headers -> $expected",
13571346
async ({
13581347
compatibilityDate,
13591348
compatibilityFlags,
13601349
notFoundHandling,
13611350
headers,
1362-
hasStaticRouting,
13631351
expected,
13641352
}) => {
13651353
expect(
13661354
await canFetch(
13671355
new Request("https://example.com/foo", { headers }),
13681356
// @ts-expect-error Empty config default to using mocked jaeger
13691357
mockEnv,
1370-
{
1371-
...normalizeConfiguration({
1372-
compatibility_date: compatibilityDate,
1373-
compatibility_flags: compatibilityFlags,
1374-
not_found_handling: notFoundHandling,
1375-
has_static_routing: hasStaticRouting,
1376-
}),
1377-
},
1358+
normalizeConfiguration({
1359+
compatibility_date: compatibilityDate,
1360+
compatibility_flags: compatibilityFlags,
1361+
not_found_handling: notFoundHandling,
1362+
}),
13781363
exists
13791364
)
13801365
).toBeTruthy();
@@ -1384,14 +1369,11 @@ describe("[Asset Worker] `canFetch`", () => {
13841369
new Request("https://example.com/bar", { headers }),
13851370
// @ts-expect-error Empty config default to using mocked jaeger
13861371
mockEnv,
1387-
{
1388-
...normalizeConfiguration({
1389-
compatibility_date: compatibilityDate,
1390-
compatibility_flags: compatibilityFlags,
1391-
not_found_handling: notFoundHandling,
1392-
has_static_routing: hasStaticRouting,
1393-
}),
1394-
},
1372+
normalizeConfiguration({
1373+
compatibility_date: compatibilityDate,
1374+
compatibility_flags: compatibilityFlags,
1375+
not_found_handling: notFoundHandling,
1376+
}),
13951377
exists
13961378
)
13971379
).toBe(expected);
@@ -1401,14 +1383,11 @@ describe("[Asset Worker] `canFetch`", () => {
14011383
new Request("https://example.com/", { headers }),
14021384
// @ts-expect-error Empty config default to using mocked jaeger
14031385
mockEnv,
1404-
{
1405-
...normalizeConfiguration({
1406-
compatibility_date: compatibilityDate,
1407-
compatibility_flags: compatibilityFlags,
1408-
not_found_handling: notFoundHandling,
1409-
has_static_routing: hasStaticRouting,
1410-
}),
1411-
},
1386+
normalizeConfiguration({
1387+
compatibility_date: compatibilityDate,
1388+
compatibility_flags: compatibilityFlags,
1389+
not_found_handling: notFoundHandling,
1390+
}),
14121391
exists
14131392
)
14141393
).toBeTruthy();
@@ -1418,14 +1397,11 @@ describe("[Asset Worker] `canFetch`", () => {
14181397
new Request("https://example.com/404", { headers }),
14191398
// @ts-expect-error Empty config default to using mocked jaeger
14201399
mockEnv,
1421-
{
1422-
...normalizeConfiguration({
1423-
compatibility_date: compatibilityDate,
1424-
compatibility_flags: compatibilityFlags,
1425-
not_found_handling: notFoundHandling,
1426-
has_static_routing: hasStaticRouting,
1427-
}),
1428-
},
1400+
normalizeConfiguration({
1401+
compatibility_date: compatibilityDate,
1402+
compatibility_flags: compatibilityFlags,
1403+
not_found_handling: notFoundHandling,
1404+
}),
14291405
exists
14301406
)
14311407
).toBeTruthy();

0 commit comments

Comments
 (0)