Skip to content

Commit

Permalink
fix: type errors and lints in listManifests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabivlj committed Mar 4, 2024
1 parent 7e58b84 commit 27c00a8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
5 changes: 5 additions & 0 deletions src/registry/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
FinishedUploadObject,
GetLayerResponse,
GetManifestResponse,
ListRepositoriesResponse,
PutManifestResponse,
Registry,
RegistryConfiguration,
Expand Down Expand Up @@ -467,6 +468,10 @@ export class RegistryHTTPClient implements Registry {
): Promise<RegistryError | FinishedUploadObject> {
throw new Error("unimplemented");
}

async listRepositories(_limit?: number, _last?: string): Promise<RegistryError | ListRepositoriesResponse> {
throw new Error("unimplemented");
}
}

// AuthType defined the supported auth types
Expand Down
24 changes: 10 additions & 14 deletions src/registry/r2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,30 +132,26 @@ export class R2Registry implements Registry {
}

async listRepositories(limit?: number, last?: string): Promise<RegistryError | ListRepositoriesResponse> {
const env = this.env;
const options = {
limit: limit ? limit : 1000,
delimiter: "/",
startAfter: last,
}
const r2Objects = (await env.REGISTRY.list(options));

let truncated = r2Objects.truncated;
let cursor = truncated ? r2Objects.cursor : undefined;

while (truncated) {
const next = await env.REGISTRY.list({
};
let r2Objects = await this.env.REGISTRY.list(options);
const objects = r2Objects.objects;
let cursor = r2Objects.truncated ? r2Objects.cursor : undefined;
while (r2Objects.truncated && objects.length < options.limit) {
const next = await this.env.REGISTRY.list({
...options,
cursor: cursor,
});
r2Objects.objects.push(...next.objects);

truncated = next.truncated;
cursor = next.cursor
objects.push(...next.objects);
r2Objects = next;
}

return {
repositories: r2Objects.delimitedPrefixes.map((name)=> name.endsWith('/') ? name.slice(0, -1) : name)
repositories: r2Objects.delimitedPrefixes.map((name) => (name.endsWith("/") ? name.slice(0, -1) : name)),
cursor: r2Objects.truncated ? r2Objects.cursor : undefined,
};
}

Expand Down
9 changes: 4 additions & 5 deletions src/registry/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ export type CheckManifestResponse =
exists: false;
};

export type ListRepositoriesResponse =
{
repositories: string[];
}

export type ListRepositoriesResponse = {
repositories: string[];
cursor?: string;
};

// Response layerExists call
export type CheckLayerResponse =
Expand Down
23 changes: 13 additions & 10 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ v2Router.get("/", async (_req, _env: Env) => {

v2Router.get("/_catalog", async (req, env: Env) => {
const { n, last } = req.query;
const res = await env.REGISTRY_CLIENT.listRepositories(
n ? parseInt(n?.toString()) : undefined,
last?.toString()
);
const res = await env.REGISTRY_CLIENT.listRepositories(n ? parseInt(n?.toString()) : undefined, last?.toString());
if ("response" in res) {
return res.response;
}

return new Response(JSON.stringify(res));
return new Response(JSON.stringify(res), {
headers: {
Link: `${req.url}/?last=${res.cursor ?? ""}; rel=next`,
},
});
});


v2Router.delete("/:name+/manifests/:reference", async (req, env: Env) => {
// deleting a manifest works by retrieving the """main""" manifest that its key is a sha,
// and then going through every tag and removing it
Expand All @@ -49,17 +52,17 @@ v2Router.delete("/:name+/manifests/:reference", async (req, env: Env) => {
//
// If somehow we need to remove by paginating, we accept a last query param

const { last } = req.query;
const { last, limit } = req.query;
const { name, reference } = req.params;
// Reference is ALWAYS a sha256
const manifest = await env.REGISTRY.head(`${name}/manifests/${reference}`);
if (manifest === null) {
return new Response(JSON.stringify(ManifestUnknownError), { status: 404 });
}

const limitInt = parseInt(limit?.toString() ?? "1000", 10);
const tags = await env.REGISTRY.list({
prefix: `${name}/manifests`,
limit: 1000,
limit: isNaN(limitInt) ? 1000 : limitInt,
startAfter: last?.toString(),
});
for (const tag of tags.objects) {
Expand All @@ -76,7 +79,7 @@ v2Router.delete("/:name+/manifests/:reference", async (req, env: Env) => {
return new Response(JSON.stringify(ManifestTagsListTooBigError), {
status: 400,
headers: {
"Link": `${req.url}/last=${tags.objects[tags.objects.length - 1]}; rel=next`,
"Link": `${req.url}/?last=${tags.truncated ? tags.cursor : ""}; rel=next`,
"Content-Type": "application/json",
},
});
Expand Down

0 comments on commit 27c00a8

Please sign in to comment.