diff --git a/src/core/source/github-content-miss.ts b/src/core/source/github-content-miss.ts index 91574f2..bf65bf5 100644 --- a/src/core/source/github-content-miss.ts +++ b/src/core/source/github-content-miss.ts @@ -3,6 +3,7 @@ import AnonymousError from "../AnonymousError"; import { ExpiringMap } from "../expiring-map"; import { octokit, waitForTokenGate, RateLimitDelayError } from "../GitHubUtils"; import { GitHubBaseData } from "./GitHubBase"; +import { githubTokenContext } from "../github-token-context"; type Miss = "file_not_found" | "repo_not_found" | "commit_not_found"; const results = new ExpiringMap(2048); @@ -27,6 +28,10 @@ export async function classifyContentMiss(data: GitHubBaseData, token: string): if (active) return active; if (pending.size >= 64) throw new AnonymousError("cache_busy", { httpStatus: 503 }); const probe = (async (): Promise => { + // A CDN read is commit-pinned and the path comes from that commit's stored + // tree, so a CDN 404 means the repository is no longer public. The GitHub + // API cannot be asked without the owner's access. + if (githubTokenContext(token)?.cdnOnly) return "repo_not_found"; const oct = octokit(token); const options = { owner: data.organization, repo: data.repoName, request: { timeout: 15_000 } }; try { diff --git a/test/public-cdn-fallback.test.js b/test/public-cdn-fallback.test.js index abebfc0..938969c 100644 --- a/test/public-cdn-fallback.test.js +++ b/test/public-cdn-fallback.test.js @@ -56,6 +56,12 @@ describe("jsDelivr access without owner credentials", function () { await repo.refreshIfNeeded(); }); + it("reports a CDN 404 as an unavailable repository, not an expired token", async () => { + const { classifyContentMiss } = require("../src/core/source/github-content-miss"); + const data = { repoId: "r", organization: "owner", repoName: "gone", commit, getToken: () => PUBLIC_CDN_TOKEN }; + expect(await classifyContentMiss(data, PUBLIC_CDN_TOKEN)).to.equal("repo_not_found"); + }); + it("encodes owner, repository and path segments", () => { expect(jsDelivrFileUrl("o", "r", commit, "x/%2e")).to.equal(`https://cdn.jsdelivr.net/gh/o/r@${commit}/x/%252e`); for (const path of ["../x", "a/./b", "a//b", ""]) expect(() => jsDelivrFileUrl("o", "r", commit, path)).to.throw("invalid_path");