Skip to content

Commit 09314ef

Browse files
authored
Support redirection from at-least-days-old (#30)
1 parent 2cf8941 commit 09314ef

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

gen_caddy.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,18 @@ For more information, please visit the Delpa homepage: https://delpa.org"
108108
route ${melpaAgeLeadingComp}/* {
109109
${snapshotVersionWithAges
110110
.map((snapshotVersionWithAge) => {
111-
const matcherName = `@valid-age-root-${snapshotVersionWithAge.version}`;
111+
const rootMatcherName =
112+
`@valid-age-root-${snapshotVersionWithAge.version}` as const;
113+
const pathMatcherName =
114+
`@valid-age-${snapshotVersionWithAge.version}` as const;
112115
return `
113-
${matcherName} path_regexp ^${melpaAgeLeadingComp}/(${snapshotVersionWithAge.ageRegexp})/*$
114-
respond ${matcherName} 200 {
116+
${rootMatcherName} path_regexp ^${melpaAgeLeadingComp}/(${snapshotVersionWithAge.ageRegexp})/*$
117+
respond ${rootMatcherName} 200 {
115118
body "{re.1} days old points to snapshot version ${snapshotVersionWithAge.version}."
116119
close
117120
}
121+
${pathMatcherName} path_regexp ^${melpaAgeLeadingComp}/(${snapshotVersionWithAge.ageRegexp})/(.*)$
122+
rewrite ${pathMatcherName} ${melpaSnapshotLeadingComp}/${snapshotVersionWithAge.version}/{re.2}
118123
`;
119124
})
120125
.join("\n")}

index.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,44 @@ describe("/melpa/at-least-days-old", () => {
141141
);
142142
});
143143
}
144+
145+
for (const [name, path, snapshotVersion] of [
146+
["without a trailing slash", "1/a/b", "2024-10-10"],
147+
["with a trailing slash", "270/a/b/", "2024-03-03"],
148+
] as const) {
149+
test(`Redirect to the corresponding snapshot given a file under /melpa/at-least-days-old: ${name}`, async () => {
150+
const response = await fetch(
151+
`${hostAddress}/${ageLeadingPathComp}/${path}`,
152+
{
153+
redirect: "manual",
154+
},
155+
);
156+
157+
expect(response.status).toBe(301);
158+
expect(response.headers.get("location")).toBe(
159+
delpaGitHubRawBaseUrl +
160+
`/melpa-snapshot-${snapshotVersion}/refs/heads/master/packages/` +
161+
path.slice(
162+
path.indexOf("/") + 1, // Remove the top-level folder in path
163+
),
164+
);
165+
});
166+
}
167+
168+
for (const [name, path] of [
169+
["0 days", "0/a/b"],
170+
["more than 270 days", "271/a/b/"],
171+
] as const) {
172+
test(`404 for out-of-range number of days under /melpa/at-least-days-old: ${name}`, async () => {
173+
const response = await fetch(
174+
`${hostAddress}/${ageLeadingPathComp}/${path}`,
175+
{
176+
redirect: "manual",
177+
},
178+
);
179+
180+
expect(response.status).toBe(404);
181+
expect(await response.text()).toBe("404 Not Found");
182+
});
183+
}
144184
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"build": "podman build --build-arg snapshot_versions_type=test . -t delpa-redirection-test-server",
1212
"build_prod": "podman build . -t delpa-redirection-server",
1313
"start": "podman run -it --rm -p 3000:80 -p 3001:443 --env HOST_ADDRESS delpa-redirection-test-server",
14-
"start_prod": "podman run -it --rm -p 3000:80 -p 3001:443 --env HOST_ADDRESS delpa-redirection-test-server",
14+
"start_prod": "podman run -it --rm -p 3000:80 -p 3001:443 --env HOST_ADDRESS delpa-redirection-server",
1515
"test": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest --run index.test.ts",
1616
"test_prod": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest --run prod.test.ts",
1717
"format": "eslint --fix *.ts *.mjs && prettier . --write",

prod.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,29 @@ describe("/melpa/at-least-days-old", () => {
101101
/5 days old points to snapshot version \d\d\d\d-\d\d-\d\d\./,
102102
);
103103
});
104+
105+
test("Redirect to the corresponding snapshot given a file under /melpa/at-least-days-old", async () => {
106+
const response = await fetch(
107+
`${hostAddress}/${ageLeadingPathComp}/5/subpath/`,
108+
{
109+
redirect: "manual",
110+
},
111+
);
112+
113+
expect(response.status).toBe(301);
114+
expect(response.headers.get("location")).toMatch(
115+
new RegExp(
116+
`${delpaGitHubRawBaseUrl}/melpa-snapshot-\\d\\d\\d\\d-\\d\\d-\\d\\d/refs/heads/master/packages/subpath/`,
117+
),
118+
);
119+
});
120+
121+
test("404 for out-of-range number of days under /melpa/at-least-days-old", async () => {
122+
const response = await fetch(`${hostAddress}/${ageLeadingPathComp}/271`, {
123+
redirect: "manual",
124+
});
125+
126+
expect(response.status).toBe(404);
127+
expect(await response.text()).toBe("404 Not Found");
128+
});
104129
});

0 commit comments

Comments
 (0)