Files
yetone dd884058e7 plugins: a Discover plugin's README comes from the copy usemagpie.ai keeps, before npm or GitHub (River_WLan on X)
npm's human check answers 200 with a page that isn't the package's entry, so the
README showed "npm didn't answer: unexpected end of JSON input". The community
repo's tagged workflow now keeps each listed plugin's README at readmes/<key>.md
on dist (plugins repo, pushed), usemagpie.ai serves it at /api/plugins/readme
(edge 10 min, jsDelivr stands in for raw), and ReadmeIn asks it (5s an address,
MAGPIE_PLUGIN_CATALOG=off skips) before npm / GitHub; an installed plugin's folder
still comes first. A page that isn't npm's entry now says npm may be asking for
a human check. Only the plain README.md is kept: a language README is read from
npm/GitHub only when the copy is missing.

Run: go test ./internal/plugin ok (wsl); site/worker.test.mjs 40 pass (wsl).
Red on old code: TestReadmeFromTheKeptCopyBeforeNPM fails with "unexpected end of
JSON input" and npm asked 2 times; worker test red with the route removed.
tagged.mjs run on wsl against real GitHub: 369 plugins, 369 READMEs, a second run
re-read none. Not tried: the Actions run itself, the GUI (no GUI file changed).
2026-11-11 16:24:37 +08:00

76 lines
2.5 KiB
Go

package plugin
import (
"context"
"net/url"
"os"
"strings"
"time"
)
// A Discover page's README, from the copy the community repo's `tagged`
// workflow keeps (readmes/<key>.md on its dist branch, passed on by
// usemagpie.ai/api/plugins/readme) before npm or GitHub: npm puts a human
// check in front of some networks and answers a page that isn't the
// package's, so the README never came (River_WLan on X). Where the copy
// isn't there, or doesn't answer in readmeCacheWait, npm and GitHub are
// asked as they were.
// readmeBases is where a README's copy is asked, in order; moved by tests.
// A %s is the package's key (readmeKey), url-escaped as the site wants it.
var readmeBases = []string{
"https://usemagpie.ai/api/plugins/readme?name=%s",
"https://raw.githubusercontent.com/magpie-community/plugins/dist/readmes/%s.md",
}
// readmeCacheWait is how long each address may take: the copy is a small
// file, and a slow one is as good as none beside npm's own 12 seconds.
var readmeCacheWait = 5 * time.Second
// readmeKey is what a package is filed under in the copy: its npm name
// ("@scope/name" is the folder @scope's name.md) or "github/owner/repo" for
// one installed from a repository. "" when the spec is neither.
func readmeKey(spec string) string {
if repo := githubRepo(spec); repo != "" && strings.HasPrefix(spec, "github:") {
return "github/" + repo
}
if pkgName.MatchString(spec) {
return spec
}
return ""
}
// cachedReadme is the README the workflow kept for spec, false when it has
// none (or isn't asked: MAGPIE_PLUGIN_CATALOG=off keeps to the vendors).
func cachedReadme(ctx context.Context, spec string) (string, bool) {
key := readmeKey(spec)
if key == "" || os.Getenv("MAGPIE_PLUGIN_CATALOG") == "off" {
return "", false
}
for _, base := range readmeBases {
arg := key
if strings.Contains(base, "?") {
arg = url.QueryEscape(key)
}
c, cancel := context.WithTimeout(ctx, readmeCacheWait)
b, err := fetchJSON(c, strings.Replace(base, "%s", arg, 1), 256<<10)
cancel()
if ctx.Err() != nil {
return "", false
}
s := strings.TrimSpace(string(b))
if err != nil || s == "" || looksLikeHTML(s) {
continue
}
return string(b), true
}
return "", false
}
// looksLikeHTML is a page standing in for the file: a captive portal's or a
// human check's, never a README the workflow wrote.
func looksLikeHTML(s string) bool {
l := strings.ToLower(s[:min(len(s), 256)])
return strings.HasPrefix(l, "<!doctype") || strings.HasPrefix(l, "<html")
}