Skip to content

Commit

Permalink
Support redirection from at-least-days-old (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuhdev authored Jan 12, 2025
1 parent 2cf8941 commit 09314ef
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
11 changes: 8 additions & 3 deletions gen_caddy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,18 @@ For more information, please visit the Delpa homepage: https://delpa.org"
route ${melpaAgeLeadingComp}/* {
${snapshotVersionWithAges
.map((snapshotVersionWithAge) => {
const matcherName = `@valid-age-root-${snapshotVersionWithAge.version}`;
const rootMatcherName =
`@valid-age-root-${snapshotVersionWithAge.version}` as const;
const pathMatcherName =
`@valid-age-${snapshotVersionWithAge.version}` as const;
return `
${matcherName} path_regexp ^${melpaAgeLeadingComp}/(${snapshotVersionWithAge.ageRegexp})/*$
respond ${matcherName} 200 {
${rootMatcherName} path_regexp ^${melpaAgeLeadingComp}/(${snapshotVersionWithAge.ageRegexp})/*$
respond ${rootMatcherName} 200 {
body "{re.1} days old points to snapshot version ${snapshotVersionWithAge.version}."
close
}
${pathMatcherName} path_regexp ^${melpaAgeLeadingComp}/(${snapshotVersionWithAge.ageRegexp})/(.*)$
rewrite ${pathMatcherName} ${melpaSnapshotLeadingComp}/${snapshotVersionWithAge.version}/{re.2}
`;
})
.join("\n")}
Expand Down
40 changes: 40 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,44 @@ describe("/melpa/at-least-days-old", () => {
);
});
}

for (const [name, path, snapshotVersion] of [
["without a trailing slash", "1/a/b", "2024-10-10"],
["with a trailing slash", "270/a/b/", "2024-03-03"],
] as const) {
test(`Redirect to the corresponding snapshot given a file under /melpa/at-least-days-old: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${ageLeadingPathComp}/${path}`,
{
redirect: "manual",
},
);

expect(response.status).toBe(301);
expect(response.headers.get("location")).toBe(
delpaGitHubRawBaseUrl +
`/melpa-snapshot-${snapshotVersion}/refs/heads/master/packages/` +
path.slice(
path.indexOf("/") + 1, // Remove the top-level folder in path
),
);
});
}

for (const [name, path] of [
["0 days", "0/a/b"],
["more than 270 days", "271/a/b/"],
] as const) {
test(`404 for out-of-range number of days under /melpa/at-least-days-old: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${ageLeadingPathComp}/${path}`,
{
redirect: "manual",
},
);

expect(response.status).toBe(404);
expect(await response.text()).toBe("404 Not Found");
});
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "podman build --build-arg snapshot_versions_type=test . -t delpa-redirection-test-server",
"build_prod": "podman build . -t delpa-redirection-server",
"start": "podman run -it --rm -p 3000:80 -p 3001:443 --env HOST_ADDRESS delpa-redirection-test-server",
"start_prod": "podman run -it --rm -p 3000:80 -p 3001:443 --env HOST_ADDRESS delpa-redirection-test-server",
"start_prod": "podman run -it --rm -p 3000:80 -p 3001:443 --env HOST_ADDRESS delpa-redirection-server",
"test": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest --run index.test.ts",
"test_prod": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest --run prod.test.ts",
"format": "eslint --fix *.ts *.mjs && prettier . --write",
Expand Down
25 changes: 25 additions & 0 deletions prod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,29 @@ describe("/melpa/at-least-days-old", () => {
/5 days old points to snapshot version \d\d\d\d-\d\d-\d\d\./,
);
});

test("Redirect to the corresponding snapshot given a file under /melpa/at-least-days-old", async () => {
const response = await fetch(
`${hostAddress}/${ageLeadingPathComp}/5/subpath/`,
{
redirect: "manual",
},
);

expect(response.status).toBe(301);
expect(response.headers.get("location")).toMatch(
new RegExp(
`${delpaGitHubRawBaseUrl}/melpa-snapshot-\\d\\d\\d\\d-\\d\\d-\\d\\d/refs/heads/master/packages/subpath/`,
),
);
});

test("404 for out-of-range number of days under /melpa/at-least-days-old", async () => {
const response = await fetch(`${hostAddress}/${ageLeadingPathComp}/271`, {
redirect: "manual",
});

expect(response.status).toBe(404);
expect(await response.text()).toBe("404 Not Found");
});
});

0 comments on commit 09314ef

Please sign in to comment.