Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ v2Router.delete("/:name+/blobs/uploads/:id", async (req, env: Env) => {

// this is the first thing that the client asks for in an upload
v2Router.post("/:name+/blobs/uploads/", async (req, env: Env) => {
const { name } = req.params;
const { name } = req.params;
const [uploadObject, err] = await wrap<UploadObject | RegistryError, Error>(env.REGISTRY_CLIENT.startUpload(name));

if (err) {
Expand Down Expand Up @@ -524,31 +524,49 @@ v2Router.get("/:name+/tags/list", async (req, env: Env) => {

const { n: nStr = 50, last } = req.query;
const n = +nStr;
if (isNaN(n)) {
if (isNaN(n) || n <= 0) {
throw new ServerError("invalid 'n' parameter", 400);
}

const tags = await env.REGISTRY.list({
let tags = await env.REGISTRY.list({
prefix: `${name}/manifests`,
limit: n,
startAfter: last ? `${name}/manifests/${last}` : undefined,
});
// Filter out sha256 manifest
let manifest_tags = tags.objects.filter((tag) => !tag.key.startsWith(`${name}/manifests/sha256:`));
// If results are truncated and the manifest filter removed some result, extend the search to reach the n number of results expected by the client
while (tags.objects.length > 0 && tags.truncated && manifest_tags.length !== n) {
tags = await env.REGISTRY.list({
prefix: `${name}/manifests`,
limit: n - manifest_tags.length,
cursor: tags.cursor,
});
// Filter out sha256 manifest
manifest_tags = manifest_tags.concat(
tags.objects.filter((tag) => !tag.key.startsWith(`${name}/manifests/sha256:`)),
);
}

const keys = tags.objects.map((object) => object.key.split("/").pop()!);
const keys = manifest_tags.map((object) => object.key.split("/").pop()!);
const url = new URL(req.url);
url.searchParams.set("n", `${n}`);
url.searchParams.set("last", keys.length ? keys[keys.length - 1] : "");
const response_headers: { "Content-Type": string; "Link"?: string } = {
"Content-Type": "application/json",
};
// Only supply a next link if the previous result is truncated
if (tags.truncated) {
response_headers.Link = `${url.toString()}; rel=next`;
}
return new Response(
JSON.stringify({
name,
tags: keys,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
"Link": `${url.toString()}; rel=next`,
},
headers: response_headers,
},
);
});
Expand Down
7 changes: 4 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ describe("v2 manifests", () => {

test("PUT then list tags with GET /v2/:name/tags/list", async () => {
const { sha256 } = await createManifest("hello-world-list", await generateManifest("hello-world-list"), `hello`);
const expectedRes = ["hello", sha256];
const expectedRes = ["hello"];
for (let i = 0; i < 50; i++) {
expectedRes.push(`hello-${i}`);
}
Expand All @@ -257,6 +257,7 @@ describe("v2 manifests", () => {
const tags = (await tagsRes.json()) as TagsList;
expect(tags.name).toEqual("hello-world-list");
expect(tags.tags).toEqual(expectedRes);
expect(tags.tags).not.contain(sha256)

const res = await fetch(createRequest("DELETE", `/v2/hello-world-list/manifests/${sha256}`, null));
expect(res.ok).toBeTruthy();
Expand Down Expand Up @@ -514,8 +515,8 @@ describe("push and catalog", () => {
"hello",
"hello-2",
"latest",
"sha256:a8a29b609fa044cf3ee9a79b57a6fbfb59039c3e9c4f38a57ecb76238bf0dec6",
]);
expect(tags.tags).not.contain("sha256:a8a29b609fa044cf3ee9a79b57a6fbfb59039c3e9c4f38a57ecb76238bf0dec6");

const repositoryBuildUp: string[] = [];
let currentPath = "/v2/_catalog?n=1";
Expand Down Expand Up @@ -557,8 +558,8 @@ describe("push and catalog", () => {
"hello",
"hello-2",
"latest",
"sha256:a70525d2dd357c6ece8d9e0a5a232e34ca3bbceaa1584d8929cdbbfc81238210",
]);
expect(tags.tags).not.contain("sha256:a70525d2dd357c6ece8d9e0a5a232e34ca3bbceaa1584d8929cdbbfc81238210");

const repositoryBuildUp: string[] = [];
let currentPath = "/v2/_catalog?n=1";
Expand Down
Loading