Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(plugins): install GitHub plugins without the REST release API

tianma-iff19dba17a995
diff --git a/apps/api/src/plugin-distribution-routes.test.mjs b/apps/api/src/plugin-distribution-routes.test.mjs
index d6673f7f..21807a79 100644
--- a/apps/api/src/plugin-distribution-routes.test.mjs
+++ b/apps/api/src/plugin-distribution-routes.test.mjs
@@ -108,6 +108,28 @@ describe("GitHub plugin release asset proxy", () => {
})).rejects.toThrow("Invalid GitHub release coordinates");
});
+ test("lists a release from the public download URL when the GitHub REST API rate-limits the instance", async () => {
+ const calls = [];
+ const release = await readGithubReleaseByTag({
+ owner: "example",
+ repository: "edgeever-plugin",
+ releaseTag: "v1.2.3",
+ request: async (input) => {
+ const url = String(input);
+ calls.push(url);
+ if (url.includes("api.github.com")) return new Response("rate limited", { status: 403 });
+ if (url === "https://github.com/example/edgeever-plugin/releases/download/v1.2.3/manifest.json") {
+ return new Response("{\"version\":\"1.2.3\"}");
+ }
+ return new Response("missing", { status: 404 });
+ },
+ });
+ expect(release.tag_name).toBe("v1.2.3");
+ expect(release.assets.map((asset) => asset.name)).toEqual(["manifest.json", "main.js", "styles.css"]);
+ expect(calls[0]).toContain("/releases/tags/v1.2.3");
+ expect(calls).toContain("https://github.com/example/edgeever-plugin/releases/download/v1.2.3/manifest.json");
+ });
+
test("falls back to raw.githubusercontent.com when the GitHub REST API rate-limits the instance", async () => {
const calls = [];
const text = await readGithubRepositoryManifestText({
diff --git a/apps/api/src/plugin-distribution-routes.ts b/apps/api/src/plugin-distribution-routes.ts
index b1891a02..703c85d2 100644
--- a/apps/api/src/plugin-distribution-routes.ts
+++ b/apps/api/src/plugin-distribution-routes.ts
@@ -194,6 +194,30 @@ export const readGithubReleaseByTag = async ({
},
);
if (response.status === 404) throw new GithubUpstreamError("GitHub release was not found.", 404);
+ if (response.status === 403 || response.status === 429) {
+ const publicUrl = `https://github.com/${owner}/${repository}/releases/download/${releaseTag}/manifest.json`;
+ try {
+ const publicManifest = await request(publicUrl, {
+ redirect: "follow",
+ headers: { "User-Agent": "EdgeEver" },
+ });
+ if (publicManifest.ok) {
+ return {
+ tag_name: releaseTag,
+ draft: false,
+ assets: (["manifest.json", "main.js", "styles.css"] as const).map((name) => ({
+ id: 0,
+ name,
+ size: 0,
+ url: "",
+ browser_download_url: `https://github.com/${owner}/${repository}/releases/download/${releaseTag}/${name}`,
+ })),
+ };
+ }
+ } catch {
+ // Keep the original GitHub API status when the public download URL is also blocked.
+ }
+ }
if (!response.ok) throw new Error(`GitHub release request failed with HTTP ${response.status}.`);
const release = await response.json() as {
tag_name?: unknown;
diff --git a/apps/web/src/lib/plugins/github-plugin-distribution.test.mjs b/apps/web/src/lib/plugins/github-plugin-distribution.test.mjs
index 5d202fe2..0946f20e 100644
--- a/apps/web/src/lib/plugins/github-plugin-distribution.test.mjs
+++ b/apps/web/src/lib/plugins/github-plugin-distribution.test.mjs
@@ -65,6 +65,30 @@ describe("GitHub plugin distribution", () => {
expect(calls).not.toContain("https://api.github.com/assets/1");
});
+ test("downloads public release assets when GitHub's REST API rate-limits the browser", async () => {
+ const request = async (input) => {
+ const url = String(input);
+ if (url.endsWith("/contents/manifest.json")) return Response.json(manifest);
+ if (url.includes("/releases/tags/")) return new Response("rate limited", { status: 403 });
+ throw new Error(`Unexpected request: ${url}`);
+ };
+ const calls = [];
+ const downloadAsset = async (_coordinates, releaseTag, asset) => {
+ calls.push([releaseTag, asset.name]);
+ if (releaseTag === "1.2.3") throw new Error(`GitHub asset ${asset.name} failed with HTTP 404.`);
+ return new TextEncoder().encode(
+ asset.name === "manifest.json" ? JSON.stringify(manifest) : "export default { activate() {} };",
+ ).buffer;
+ };
+
+ const downloaded = await downloadGithubExtension("https://github.com/example/edgeever-plugin", request, downloadAsset);
+
+ expect(downloaded.releaseTag).toBe("v1.2.3");
+ expect(downloaded.pluginPackage?.mainJs).toContain("activate");
+ expect(calls[0]).toEqual(["1.2.3", "manifest.json"]);
+ expect(calls).toContainEqual(["v1.2.3", "main.js"]);
+ });
+
test("rejects a release without a bundled main.js asset", async () => {
const request = async (input) => {
const url = String(input);
diff --git a/apps/web/src/lib/plugins/github-plugin-distribution.ts b/apps/web/src/lib/plugins/github-plugin-distribution.ts
index 249e7162..beb47f22 100644
--- a/apps/web/src/lib/plugins/github-plugin-distribution.ts
+++ b/apps/web/src/lib/plugins/github-plugin-distribution.ts
@@ -165,7 +165,13 @@ const namedReleaseAsset = (name: string): GithubReleaseAsset => ({
browser_download_url: "",
});
+const isGithubApiQuotaError = (error: unknown) => {
+ const message = error instanceof Error ? error.message : String(error);
+ return /HTTP (401|403|429)/.test(message) || message.includes(GITHUB_UNREACHABLE_MESSAGE);
+};
+
const findRelease = async (request: typeof fetch, coordinates: GithubRepositoryCoordinates, version: string) => {
+ let quotaStatus: number | null = null;
for (const tag of [version, `v${version}`]) {
const response = await readGithubApi(
request,
@@ -173,11 +179,16 @@ const findRelease = async (request: typeof fetch, coordinates: GithubRepositoryC
{ Accept: "application/vnd.github+json", "X-GitHub-Api-Version": GITHUB_API_VERSION },
);
if (response.status === 404) continue;
+ if (isGithubRateLimitedStatus(response.status) || response.status === 401) {
+ quotaStatus = response.status;
+ continue;
+ }
if (!response.ok) throw new Error(`GitHub release request failed with HTTP ${response.status}.`);
const release = await response.json() as GithubReleaseResponse;
if (release.draft) throw new Error("GitHub draft releases cannot be installed.");
return release;
}
+ if (quotaStatus) throw new Error(`GitHub release request failed with HTTP ${quotaStatus}.`);
throw new Error(`GitHub Release ${version} or v${version} was not found.`);
};
@@ -379,7 +390,22 @@ export const downloadGithubExtension = async (
}
assertBundledEntry(repositoryManifest);
- const release = await findRelease(request, coordinates, repositoryManifest.version);
+ const downloadFromPinnedRelease = async () => {
+ const pinned = await downloadPinnedGithubExtension(input, repositoryManifest.version, {
+ downloadAssetBytes,
+ requireStyles: false,
+ });
+ assertReleaseManifest(repositoryManifest, pinned.manifest);
+ return pinned;
+ };
+
+ let release: GithubReleaseResponse;
+ try {
+ release = await findRelease(request, coordinates, repositoryManifest.version);
+ } catch (error) {
+ if (!isGithubApiQuotaError(error)) throw error;
+ return downloadFromPinnedRelease();
+ }
const manifestAsset = requireAsset(release, "manifest.json");
const mainAsset = requireAsset(release, "main.js");
const stylesAsset = release.assets.find((asset) => asset.name === "styles.css") ?? null;