Skip to content

Commit

Permalink
fix: do not add query params on top of the URL with query params
Browse files Browse the repository at this point in the history
  • Loading branch information
gabivlj committed Nov 20, 2024
1 parent 3e532ba commit c2941b6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ v2Router.delete("/:name+/manifests/:reference", async (req, env: Env) => {
}
}

const urlNoQueryParams = new URL(req.url);
deleteAllURLParams(urlNoQueryParams);
if (tags.truncated) {
return new Response(JSON.stringify(ManifestTagsListTooBigError), {
status: 400,
headers: {
"Link": `${req.url}/?last=${tags.truncated ? tags.cursor : ""}; rel=next`,
"Link": `${urlNoQueryParams.toString()}/?last=${tags.truncated ? tags.cursor : ""}; rel=next`,
"Content-Type": "application/json",
},
});
Expand Down Expand Up @@ -514,6 +516,12 @@ export type TagsList = {
tags: string[];
};

function deleteAllURLParams(url: URL) {
for (const key of url.searchParams.keys()) {
url.searchParams.delete(key);
}
}

v2Router.get("/:name+/tags/list", async (req, env: Env) => {
const { name } = req.params;
const { n: nStr = 50, last } = req.query;
Expand All @@ -529,6 +537,8 @@ v2Router.get("/:name+/tags/list", async (req, env: Env) => {
});

const keys = tags.objects.map((object) => object.key.split("/").pop()!);
const urlNoQueryParams = new URL(req.url);
deleteAllURLParams(urlNoQueryParams);
return new Response(
JSON.stringify({
name,
Expand All @@ -538,7 +548,7 @@ v2Router.get("/:name+/tags/list", async (req, env: Env) => {
status: 200,
headers: {
"Content-Type": "application/json",
"Link": `${req.url}?n=${n}&last=${keys.length ? keys[keys.length - 1] : ""}; rel=next`,
"Link": `${urlNoQueryParams.toString()}?n=${n}&last=${keys.length ? keys[keys.length - 1] : ""}; rel=next`,
},
},
);
Expand Down

0 comments on commit c2941b6

Please sign in to comment.