const { expect } = require("chai"); const { Readable } = require("stream"); const got = require("got"); const { URL } = require("url"); require("ts-node/register/transpile-only"); const { PUBLIC_CDN_TOKEN, githubTokenForStreamer } = require("../src/core/github-token-context"); const { octokit } = require("../src/core/GitHubUtils"); const GitHubStream = require("../src/core/source/GitHubStream").default; const { jsDelivrFileUrl } = require("../src/core/source/GitHubStream"); describe("jsDelivr access without owner credentials", function () { const commit = "a".repeat(40); let original; beforeEach(() => { original = got.stream; }); afterEach(() => { got.stream = original; }); it("never reaches the GitHub API and reaches the streamer unchanged", async () => { let error; try { octokit(PUBLIC_CDN_TOKEN); } catch (e) { error = e; } expect(error.message).to.equal("token_expired"); expect(error.httpStatus).to.equal(401); expect(await githubTokenForStreamer(PUBLIC_CDN_TOKEN, "owner/repo")).to.equal(PUBLIC_CDN_TOKEN); }); it("downloads commit-pinned bytes from jsDelivr without any credential", async () => { const calls = []; got.stream = (url, options) => { calls.push({ url: String(url), options }); return Readable.from(["public bytes"]); }; const source = new GitHubStream({ repoId: "r", organization: "owner", repoName: "repo", commit, getToken: () => PUBLIC_CDN_TOKEN }); const chunks = []; for await (const chunk of await source.downloadWithFallback(PUBLIC_CDN_TOKEN, "blob-sha", "src/a b#.py")) chunks.push(Buffer.from(chunk)); expect(Buffer.concat(chunks).toString()).to.equal("public bytes"); expect(calls.map(c => c.url)).to.deep.equal([`https://cdn.jsdelivr.net/gh/owner/repo@${commit}/src/a%20b%23.py`]); expect(calls[0].options.headers).to.equal(undefined); expect(() => calls[0].options.hooks.beforeRedirect[0]({ url: new URL("https://evil.example/x") })).to.throw("upstream_error"); }); it("requires a pinned commit so a moving branch is never served", async () => { got.stream = () => { throw new Error("must not download"); }; const source = new GitHubStream({ repoId: "r", organization: "owner", repoName: "repo", commit: "main", getToken: () => PUBLIC_CDN_TOKEN }); const error = await source.downloadWithFallback(PUBLIC_CDN_TOKEN, "blob-sha", "a.py").catch(e => e); expect(error.message).to.equal("token_expired"); }); it("refuses Git LFS pointers, which jsDelivr serves instead of the file", async () => { const pointer = "version https://git-lfs.github.com/spec/v1\noid sha256:" + "b".repeat(64) + "\nsize 123\n"; got.stream = () => Readable.from([pointer]); const source = new GitHubStream({ repoId: "r", organization: "owner", repoName: "repo", commit, getToken: () => PUBLIC_CDN_TOKEN }); const error = await (async () => { for await (const _ of await source.downloadWithFallback(PUBLIC_CDN_TOKEN, "blob-sha", "big.bin")) void _; })().catch(e => e); expect(error.message).to.equal("upstream_error"); }); it("skips the automatic refresh, which needs the GitHub API", async () => { const Repository = require("../src/core/Repository").default; const GitHubBase = require("../src/core/source/GitHubBase").default; const repo = Object.create(Repository.prototype); repo._model = { options: { update: true, expirationMode: "never" }, lastView: new Date(0), source: {} }; Object.defineProperty(repo, "source", { value: Object.create(GitHubBase.prototype) }); repo.getToken = async () => PUBLIC_CDN_TOKEN; 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"); }); });