Skip to content

Commit

Permalink
Merge pull request cloudflare#32 from cloudflare/gv/correct-manifest-…
Browse files Browse the repository at this point in the history
…unknown

fix: return a response that docker understands better when it doesn't find a manifest
  • Loading branch information
skepticfx authored Jun 3, 2024
2 parents 5750965 + 2e69f47 commit 246b148
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
4 changes: 3 additions & 1 deletion index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ describe("v2 manifests", () => {
{
code: "MANIFEST_UNKNOWN",
message: "manifest unknown",
detail: "This error is returned when the manifest, identified by name and tag is unknown to the repository.",
detail: {
Tag: "reference",
},
},
],
});
Expand Down
10 changes: 6 additions & 4 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from "itty-router";
import { BlobUnknownError, ManifestUnknownError } from "./v2-errors";
import { InternalError, ServerError } from "./errors";
import { errorString, wrap } from "./utils";
import { errorString, jsonHeaders, wrap } from "./utils";
import { hexToDigest } from "./user";
import { ManifestTagsListTooBigError } from "./v2-responses";
import { Env } from "..";
Expand Down Expand Up @@ -66,7 +66,7 @@ v2Router.delete("/:name+/manifests/:reference", async (req, env: Env) => {
// 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 });
return new Response(JSON.stringify(ManifestUnknownError(reference)), { status: 404, headers: jsonHeaders() });
}
const limitInt = parseInt(limit?.toString() ?? "1000", 10);
const tags = await env.REGISTRY.list({
Expand Down Expand Up @@ -162,7 +162,7 @@ v2Router.head("/:name+/manifests/:reference", async (req, env: Env) => {
}

if (checkManifestResponse === null || !checkManifestResponse.exists)
return new Response(JSON.stringify(ManifestUnknownError), { status: 404 });
return new Response(JSON.stringify(ManifestUnknownError(reference)), { status: 404, headers: jsonHeaders() });

return new Response(null, {
headers: {
Expand Down Expand Up @@ -221,7 +221,9 @@ v2Router.get("/:name+/manifests/:reference", async (req, env: Env, context: Exec
break;
}

if (getManifestResponse === null) return new Response(JSON.stringify(ManifestUnknownError), { status: 404 });
if (getManifestResponse === null)
return new Response(JSON.stringify(ManifestUnknownError(reference)), { status: 404, headers: jsonHeaders() });

return new Response(getManifestResponse.stream, {
headers: {
"Content-Length": getManifestResponse.size.toString(),
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ export function errorString(err: unknown): string {
export async function wrap<T, E = unknown>(fn: Promise<T>): Promise<[T, null] | [null, E]> {
return fn.then((data) => [data, null] as [T, null]).catch((err) => [null, err as unknown as E] as [null, E]);
}

export function jsonHeaders(): { "content-type": "application/json" } {
return { "content-type": "application/json" };
}
25 changes: 15 additions & 10 deletions src/v2-errors.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
export const ManifestUnknownError = {
errors: [
{
code: "MANIFEST_UNKNOWN",
message: "manifest unknown",
detail: "This error is returned when the manifest, identified by name and tag is unknown to the repository.",
},
],
};
export const ManifestUnknownError = (tag: string) =>
({
errors: [
{
code: "MANIFEST_UNKNOWN",
message: "manifest unknown",
detail: {
Tag: tag,
},
},
],
} as const);

export const BlobUnknownError = {
errors: [
{
code: "BLOB_UNKNOWN",
message: "blob unknown to registry",
detail: "This error may be returned when a manifest blob is unknown to the registry.",
detail: {
message: "This error may be returned when a layer blob is unknown to the registry.",
},
},
],
};

0 comments on commit 246b148

Please sign in to comment.