Skip to content

Commit 13c79f2

Browse files
committed
Nicer 404 message when a snapshot version is invalid
Also respond 200 if visiting the root of a valid snapshot version.
1 parent b8398e2 commit 13c79f2

File tree

3 files changed

+78
-11
lines changed

3 files changed

+78
-11
lines changed

gen_caddy.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import snapshotVersions from "./snapshot_versions.json" with { type: "json" };
2222

23-
const snapVersionsRegexp = snapshotVersions.join("|");
23+
const snapshotVersionsRegexp = snapshotVersions.join("|");
2424

2525
const caddyfile = `
2626
{
@@ -30,14 +30,41 @@ const caddyfile = `
3030
{$HOST_ADDRESS:localhost} {
3131
redir / https://delpa.org permanent
3232
33-
respond /health-check "OK"
34-
35-
@snapshot path_regexp ^/snapshot/(${snapVersionsRegexp})/(.*)$
36-
redir @snapshot https://raw.githubusercontent.com/delpa-org/melpa-snapshot-{re.1}/refs/heads/master/packages/{re.2} permanent
33+
respond /health-check "OK" {
34+
close
35+
}
3736
3837
respond "404 Not Found" 404 {
3938
close
4039
}
40+
41+
route /snapshot/* {
42+
@valid-snapshot-root path_regexp ^/snapshot/(${snapshotVersionsRegexp})/*$
43+
respond @valid-snapshot-root 200 {
44+
body "{re.1} is a valid snapshot version."
45+
close
46+
}
47+
@valid-snapshot path_regexp ^/snapshot/(${snapshotVersionsRegexp})/(.*)$
48+
redir @valid-snapshot https://raw.githubusercontent.com/delpa-org/melpa-snapshot-{re.1}/refs/heads/master/packages/{re.2} permanent
49+
50+
@invalid-snapshot path_regexp ^/snapshot/([^/]+).*$
51+
respond @invalid-snapshot 404 {
52+
53+
body "404 Not Found. Invalid snapshot version: {re.1}
54+
55+
Currently this server thinks the following snapshots are valid:
56+
57+
${snapshotVersions
58+
.toSorted()
59+
.reverse()
60+
.map((v) => `- ${v}`)
61+
.join("\n")}
62+
63+
For more information, please visit the Delpa homepage: https://delpa.org"
64+
65+
close
66+
}
67+
}
4168
}
4269
`;
4370

index.test.ts

+32-6
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ test("/health-check returns 200 with OK", async () => {
3939
describe("/snapshot", () => {
4040
const snapshotFirstPathComp = "snapshot" as const;
4141
for (const [name, path] of [
42-
["valid with a one-level subdir", "2024-01-01/a"],
43-
["valid with a one-level subdir with a trailing slash", "2024-01-01/a/"],
44-
["valid with a two-level subdir", "2024-01-01/a/b"],
45-
["valid with a two-level subdir with a trailing slash", "2024-01-01/a/b/"],
42+
["valid with a one-level subdir", "2024-01-01/a/b"],
43+
["valid with a one-level subdir with a trailing slash", "2024-02-02/a/b/"],
44+
["valid with a two-level subdir", "2024-01-01/a/b/c"],
45+
[
46+
"valid with a two-level subdir with a trailing slash",
47+
"2024-03-03/a/b/c/",
48+
],
4649
] as const) {
4750
test(`Redirect with valid URL under /shapshot: ${name}`, async () => {
4851
const response = await fetch(
@@ -55,14 +58,33 @@ describe("/snapshot", () => {
5558
expect(response.status).toBe(301);
5659
expect(response.headers.get("location")).toBe(
5760
delpaGitHubRawBaseUrl +
58-
"/melpa-snapshot-2024-01-01/refs/heads/master/packages/" +
61+
`/melpa-snapshot-${path.split("/")[0]}/refs/heads/master/packages/` +
5962
path.slice(
6063
path.indexOf("/") + 1, // Remove the top-level folder in path
6164
),
6265
);
6366
});
6467
}
6568

69+
for (const [name, path] of [
70+
["without a trailing slash", "2024-01-01"],
71+
["with a trailing slash", "2024-02-02/"],
72+
] as const) {
73+
test(`Report OK with valid snapshot version at a root dir of /shapshot: ${name}`, async () => {
74+
const response = await fetch(
75+
`${hostAddress}/${snapshotFirstPathComp}/${path}`,
76+
{
77+
redirect: "manual",
78+
},
79+
);
80+
81+
expect(response.status).toBe(200);
82+
const responseText = await response.text();
83+
expect(responseText).toContain("valid snapshot version");
84+
expect(responseText).toContain(path.split("/")[0]);
85+
});
86+
}
87+
6688
for (const [name, path] of [
6789
["non-existing snapshot", "2025-01-01"],
6890
["non-existing partially-matched snapshot", "2024-01"],
@@ -89,7 +111,11 @@ describe("/snapshot", () => {
89111

90112
expect(response.status).toBe(404);
91113
expect(response.headers.get("content-type")).toContain("text/plain");
92-
expect(await response.text()).toBe("404 Not Found");
114+
const responseText = await response.text();
115+
expect(responseText).toContain("404 Not Found");
116+
expect(responseText).toContain(
117+
`Invalid snapshot version: ${path.split("/")[0]}`,
118+
);
93119
});
94120
}
95121
});

prod.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ describe("/snapshot", () => {
6060
);
6161
});
6262

63+
test("Report OK with valid snapshot version at a root dir of /shapshot", async () => {
64+
const response = await fetch(
65+
`${hostAddress}/${snapshotFirstPathComp}/2025-01-02`,
66+
{
67+
redirect: "manual",
68+
},
69+
);
70+
71+
expect(response.status).toBe(200);
72+
const responseText = await response.text();
73+
expect(responseText).toContain("valid snapshot version");
74+
expect(responseText).toContain("2025-01-02");
75+
});
76+
6377
test("Return 404 with invalid URL under /shapshot", async () => {
6478
const response = await fetch(
6579
`${hostAddress}/${snapshotFirstPathComp}/non-existing`,

0 commit comments

Comments
 (0)