From eaca1cf4f19b2197fc37ae8cd26c5c256b9c51c6 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 2 Nov 2026 05:57:45 +0800 Subject: [PATCH] check: parse registry.json and hold each entry to what magpie reads (#612) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit bun scripts/check.mjs exited 0 on a registry.json that wasn't JSON, or had an entry with a bogus kind, so a listing PR could break the Plugins page's list and pass CI. check.mjs now checks registry.json first (scripts/registry.mjs) and fails on: - text that isn't JSON, isn't an object, or is over 1 MiB (what magpie fetches); version not 1; plugins not a non-empty list; - an entry whose package isn't a name magpie's pkgName pattern (internal/plugin/store.go) matches, or over npm's 214 characters (magpie drops such an entry); the same package listed twice; - no name, no summary.en or summary.zh; - a kind other than middleware, hooks, agent, tools or page (plugins.js's kindOf; a provider has none), which would list it as a provider; - a value of the wrong type for community, providers, replaces or summary: market.go unmarshals the whole file into typed fields, so one such value loses every listing; an icon with a scheme other than https or data:image; a field magpie doesn't read (a typo like "kinds"). All 72 current entries pass, the 5 without community included (magpie skips them; kept as they are). Run on mote, HOME=$(mktemp -d), bun 1.4.2: - bun test: 4709 pass, 12 skip, 0 fail (363 files). - bun scripts/check.mjs: exit 0, ✓ registry.json. Broken by hand on mote: "{" appended to registry.json -> exit 1, "not JSON"; every "kind": "middleware" made "bogus" -> exit 1, each entry named. registryProblems stubbed to return [] -> the three #612 tests go red. Open listing PRs (#611, #610) merged onto this commit pass the check; #582 doesn't touch registry.json. --- scripts/check.mjs | 10 +++++- scripts/check.test.mjs | 75 ++++++++++++++++++++++++++++++++++++++++-- scripts/registry.mjs | 59 +++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 scripts/registry.mjs diff --git a/scripts/check.mjs b/scripts/check.mjs index 87ea0a8..c07428c 100644 --- a/scripts/check.mjs +++ b/scripts/check.mjs @@ -1,14 +1,22 @@ // Loads every package's plugin the way OpenCode (and magpie) do and checks // the shape of what it returns: an auth hook with a provider, a loader and // methods OpenCode knows, or, for a plugin of tools alone, its tools. It -// signs in to nothing and sends no request. +// signs in to nothing and sends no request. It checks registry.json first, +// as magpie's Plugins page reads it (scripts/registry.mjs). import { readdirSync, readFileSync, existsSync } from "node:fs" import { join, resolve } from "node:path" import { pathToFileURL } from "node:url" +import { registryProblems } from "./registry.mjs" const root = resolve(import.meta.dir, "..", "packages") const only = process.argv[2] let failed = 0 +{ + const file = resolve(import.meta.dir, "..", "registry.json") + const problems = existsSync(file) ? registryProblems(readFileSync(file, "utf8")) : ["registry.json is missing"] + if (problems.length) failed++ + console.log(problems.length ? `✗ registry.json\n ${problems.join("\n ")}` : "✓ registry.json") +} for (const name of readdirSync(root)) { if (only && name !== only) continue const dir = join(root, name) diff --git a/scripts/check.test.mjs b/scripts/check.test.mjs index 582e869..30a08c4 100644 --- a/scripts/check.test.mjs +++ b/scripts/check.test.mjs @@ -1,16 +1,21 @@ import { expect, test } from "bun:test" -import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs" +import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { join } from "node:path" +import { registryProblems } from "./registry.mjs" // Exercise the checker in a tiny independent repository, without loading any // real plugin, credential file or network client. -function check(source) { +const okRegistry = JSON.stringify({ version: 1, plugins: [{ package: "fixture", name: "Fixture", community: true, summary: { en: "A fixture.", zh: "示例。" } }] }) + +function check(source, registry = okRegistry) { const dir = mkdtempSync(join(tmpdir(), "magpie-check-test-")) try { mkdirSync(join(dir, "scripts")) mkdirSync(join(dir, "packages", "fixture"), { recursive: true }) copyFileSync(new URL("./check.mjs", import.meta.url), join(dir, "scripts", "check.mjs")) + copyFileSync(new URL("./registry.mjs", import.meta.url), join(dir, "scripts", "registry.mjs")) + if (registry !== null) writeFileSync(join(dir, "registry.json"), registry) writeFileSync(join(dir, "packages", "fixture", "package.json"), JSON.stringify({ main: "index.mjs" })) writeFileSync(join(dir, "packages", "fixture", "index.mjs"), source) const result = Bun.spawnSync([process.execPath, join(dir, "scripts", "check.mjs")]) @@ -53,3 +58,69 @@ test("check still requires loaders for providers, including providers with tools expect(result.output).toContain("auth.loader") } }) + +const fine = `export const Plugin = async () => ({ tool: ${tool} })` + +test("check passes this repository's own registry.json", () => { + expect(registryProblems(readFileSync(new URL("../registry.json", import.meta.url), "utf8"))).toEqual([]) +}) + +test("check fails a registry.json that is not JSON (#612)", () => { + const result = check(fine, okRegistry.slice(0, -2)) + expect(result.code).toBe(1) + expect(result.output).toContain("✗ registry.json") + expect(result.output).toContain("not JSON") + expect(result.output).toContain("✓ fixture") +}) + +test("check fails an entry with a kind magpie doesn't know (#612)", () => { + const r = JSON.parse(okRegistry) + r.plugins[0].kind = "bogus" + const result = check(fine, JSON.stringify(r)) + expect(result.code).toBe(1) + expect(result.output).toContain('kind "bogus"') +}) + +test("check fails a missing registry.json", () => { + const result = check(fine, null) + expect(result.code).toBe(1) + expect(result.output).toContain("registry.json is missing") +}) + +test("check passes a good registry.json and plugin", () => { + const result = check(fine) + expect(result.code).toBe(0) + expect(result.output).toContain("✓ registry.json") +}) + +// each of these loses the entry, or every entry, in magpie's reader +test("registry entries are held to what magpie's market reads", () => { + const base = { package: "@magpie-community/opencode-acme-auth", name: "Acme", community: true, providers: ["acme"], summary: { en: "Acme.", zh: "Acme。" } } + const of = (...plugins) => registryProblems(JSON.stringify({ version: 1, plugins })) + expect(of(base, { ...base, package: "tools-x", kind: "tools", providers: undefined })).toEqual([]) + for (const kind of ["middleware", "hooks", "agent", "tools", "page"]) expect(of({ ...base, kind })).toEqual([]) + const cases = [ + [{ ...base, package: "Acme" }, "not an npm package name"], + [{ ...base, package: "@scope" }, "not an npm package name"], + [{ ...base, package: "a b" }, "not an npm package name"], + [{ ...base, package: undefined }, "not an npm package name"], + [{ ...base, name: "" }, "no name"], + [{ ...base, kind: "provider" }, 'kind "provider"'], + [{ ...base, community: "true" }, "community is not true or false"], + [{ ...base, providers: "acme" }, "providers is not a list"], + [{ ...base, providers: [] }, "providers is not a list"], + [{ ...base, icon: "http://acme.example/i.svg" }, "is not https or data:image"], + [{ ...base, summary: { en: "Acme." } }, "no summary.zh"], + [{ ...base, summary: undefined }, "no summary"], + [{ ...base, kinds: "tools" }, 'unknown field "kinds"'], + [{ ...base, replaces: 3 }, "replaces is not"], + ] + for (const [entry, want] of cases) { + const got = of(JSON.parse(JSON.stringify(entry))) + expect(got.join("\n")).toContain(want) + } + expect(of(base, { ...base, name: "Again" }).join("\n")).toContain("package listed again (first at plugins[0])") + expect(registryProblems(JSON.stringify({ version: 1, plugins: [] }))).toEqual(["plugins is not a list of entries"]) + expect(registryProblems(JSON.stringify({ version: 2, plugins: [base] }))).toEqual(["version 2, not 1"]) + expect(registryProblems("[]")).toEqual(["registry.json is not an object"]) +}) diff --git a/scripts/registry.mjs b/scripts/registry.mjs new file mode 100644 index 0000000..d7c4878 --- /dev/null +++ b/scripts/registry.mjs @@ -0,0 +1,59 @@ +// Checks registry.json, the list magpie's Plugins page suggests, against +// what magpie's reader (internal/plugin/market.go, parseMarket) takes. +// That reader unmarshals the whole file into typed fields, so one value of +// the wrong type (a string for "providers", "true" for "community") loses +// every listing, not just its own; it keeps an entry only when "package" is +// a name its pkgName pattern matches; and the page sorts by "kind", so an +// unknown kind lists a middleware or an agent as a provider. + +// magpie's pkgName (internal/plugin/store.go), and npm's length limit +const pkgName = /^(@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/ +// a provider's entry has none; plugins.js's kindOf knows these others +export const kinds = ["middleware", "hooks", "agent", "tools", "page"] +const fields = ["package", "name", "icon", "providers", "community", "replaces", "kind", "summary"] +const languages = ["en", "zh"] +// magpie fetches the list at most this big (fetchJSONFaithful's limit) +const maxBytes = 1 << 20 + +const text = (v) => typeof v === "string" && v.trim() !== "" + +// The problems with registry.json's text, none when magpie reads it whole. +export function registryProblems(source) { + if (Buffer.byteLength(source) > maxBytes) return [`registry.json is over ${maxBytes} bytes, more than magpie fetches`] + let r + try { + r = JSON.parse(source) + } catch (e) { + return [`registry.json is not JSON: ${e.message}`] + } + if (!r || typeof r !== "object" || Array.isArray(r)) return ["registry.json is not an object"] + const problems = [] + if (r.version !== 1) problems.push(`version ${JSON.stringify(r.version)}, not 1`) + if (!Array.isArray(r.plugins) || !r.plugins.length) return [...problems, "plugins is not a list of entries"] + const seen = new Map() + r.plugins.forEach((e, i) => { + const at = `plugins[${i}]${text(e?.package) ? ` (${e.package})` : ""}` + const bad = (why) => problems.push(`${at}: ${why}`) + if (!e || typeof e !== "object" || Array.isArray(e)) return bad("not an object") + for (const k of Object.keys(e)) if (!fields.includes(k)) bad(`unknown field ${JSON.stringify(k)}`) + if (typeof e.package !== "string" || !pkgName.test(e.package) || e.package.length > 214) bad(`package ${JSON.stringify(e.package)} is not an npm package name`) + else if (seen.has(e.package)) bad(`package listed again (first at plugins[${seen.get(e.package)}])`) + else seen.set(e.package, i) + if (!text(e.name)) bad("no name") + if ("kind" in e && !kinds.includes(e.kind)) bad(`kind ${JSON.stringify(e.kind)} is not one of ${kinds.join(", ")} (a provider has none)`) + if ("community" in e && typeof e.community !== "boolean") bad("community is not true or false") + if ("replaces" in e && !text(e.replaces)) bad("replaces is not a subscription id") + if ("providers" in e && (!Array.isArray(e.providers) || !e.providers.length || !e.providers.every(text))) bad("providers is not a list of provider ids") + if ("icon" in e) { + // a bundled icon's name, or a picture: an https URL or a data:image URI + if (!text(e.icon)) bad("icon is empty") + else if (/[:/]/.test(e.icon) && !/^(https:\/\/|data:image\/)/i.test(e.icon)) bad(`icon ${JSON.stringify(e.icon.slice(0, 60))} is not https or data:image`) + } + if (!e.summary || typeof e.summary !== "object" || Array.isArray(e.summary)) bad("no summary") + else { + for (const l of languages) if (!text(e.summary[l])) bad(`no summary.${l}`) + for (const [l, s] of Object.entries(e.summary)) if (typeof s !== "string") bad(`summary.${l} is not text`) + } + }) + return problems +}