Files
plugins/scripts/registry.mjs
T
yetone eaca1cf4f1 check: parse registry.json and hold each entry to what magpie reads (#612)
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.
2026-11-02 05:57:45 +08:00

60 lines
3.5 KiB
JavaScript

// 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
}