MengXiangxi (#3821): a GitHub repository tagged magpie-plugin whose package.json has magpie.hooks and no main, exports or index.js, a gateway request hook alone that installs and runs, never showed in the Plugins page's "Unofficial, on GitHub". readTagged read the magpie block's middleware, agent and page but not hooks, so such a package was taken for a provider: the file #1327's check looks for in the repository was its entry as Bun resolves it, index.js, which it hasn't, and the 404 left it out without a word. An inert index.js, as the reporter tried, lists it, as a provider. readTagged reads magpie.hooks. A package with hooks, no main or exports, and none of middleware, agent or page is of kind "hooks", and the file #1327's check looks for is its hooks file, so one whose hooks file isn't committed (a dist built only to publish) is still left out, as a middleware's is. The Plugins page picks "hooks" out with middleware, as both run in the gateway (kindOf), and its chip says Request hook, as an installed one's row does, through the word kindChip takes since the previous commit. docs/subsystems/gateway-request-hooks.md says so. Not changed: a raw 404 that GitHub's cache answers just after a push is still kept for that push in this process (the reporter's second point, #4067). plugins.js grows 51 bytes for it. With the previous commit the core's scripts are 24 bytes under main's, 1,527,948 of 1,528,076. What was run (tree 79884772, on origin/main56602426, go1.26.8, linux/amd64, Linux 5.15), outside graftcp, the Go tests at umask 077: - TestTaggedHooksAlone, new. IvanXxxxyyuffff/magpie-system-prompt-file's package.json as it is (a repository the report names) is kept, of kind hooks, with its spec, package and version 0.3.0, and its hooks file is the file asked for, never an index.js. someone/hooks-gone, whose hooks file is only in a dist it doesn't commit, is left out. With TestTaggedRepos*, -count=50: 350 of 350 pass; -race -count=20: 140 of 140, no race. - plugin-github-hooks.test.cjs, new. Seven repositories of /api/plugins/github's real answer of 2026-10-27 (two providers, three middleware, two of hooks alone): each of hooks alone has the gateway's chip saying Request hook and is picked out under Middleware, in English, Chinese, Traditional Chinese, Japanese and German at 980px and 420px. Chromium and WebKit, 3 runs: 22 of 22 each run. - Broken by hand, each red: - no kind made of magpie.hooks, as on main: TestTaggedHooksAlone, hooks alone not kept. - the kind made but the file looked for still the entry, index.js: the same. - the hooks file not looked for: TestTaggedHooksAlone, the hooks file not among the files asked for. - kindOf without hooks, so a Provider: plugin-github-hooks 0 of 22. - the card's chip not given its word: 0 of 22. - TestCoreScriptBudget: ok. - gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go vet for windows, go build -tags nogui for linux and darwin and go build for windows: exit 0 each. go vet and go build without the tag on linux: exit 1 (no libsoup-3.0 or gio-unix-2.0 here). - The Plugins page's GUI files near the change, 24 with the new one and i18n-keys, each run by itself in Chromium and in WebKit: 42 of 48 runs green. The other 6 are red the same at56602426, each file run the same way, and none is in what this changes: - i18n-keys, both engines: "a plugin's words are its own", for plugins/routing/i18n.js "masking…", which internal/masking/masking.go says too (#4053). - builtins, WebKit: Plugins › Built-in, the Observability switch's click times out, the stuck Plugins header (.lib-head.pm-head.stuck) over it taking the pointer. - plugin-github, WebKit: de 440, a click scrolls #view-plugins, 74 to 139 (65 to 119 at 56602426; #3218 has the same check red in Chromium). - plugin-github-wait, Chromium: de 440, Try again moves #view-plugins by 2. - plugin-release, WebKit: en and zh read "Serves Zed\n" for "Serves Zed". - go test -tags nogui -count=1 -timeout 45m ./... (Bun 1.4.2, log kept; 8m48s, load 32 at its start): 71 packages ok, internal/plugin among them, and 16 tests red in 6 packages, none of them this commit's tests or what they cover (internal/gui's are its gateway tests): agent TestCodexDriftReached (#3398); TestInstalledVersion, TestHowInstalled, TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir and TestZedAppPath (#3141); TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout (#3641); davsync TestSyncTeraCloudStillSeesOtherWrites (#3420); edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram (umask 077, #3640); gateway TestPluginToldWhereTheAgentWorks (the /tmp/.git of #3675); gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie (#3649). The rest is what was run for 31a99237, at tree 2dd21699 on origin/main508911e4, the same Go change (github.go has since taken e4ccf67b's search timeout, in askTagged, not readTagged; 31a99237's plugins.js named the card's chip by hand, not through kindChip, and had three lines of comment this leaves out; not this tree): - Real use: magpie web built at508911e4and at that change's code (529f07e5, the same but for the new GUI file), each in a sandbox HOME, asked real GitHub for /api/plugins/github.508911e4listed 163 and left IvanXxxxyyuffff/magpie-system-prompt-file out. That change listed 164, it and MengXiangxi/magpie-openviking (which now has the inert index.js) of kind hooks. cyberElar/magpie-x-search (#1327's, a command only) was left out by both. Not tried: macOS and Windows; the real app's window (no display here, the page in Playwright's browsers only). Landed by yetone in a batch of Coffemon's patches from the issues. What was run, go1.26.8, on origin/maincc81ad4ea, then again at70a0c387c(gofmt, the three vets, the changed tests, umask 077): - mote (macOS 27): gofmt -l clean; go vet, GOOS=linux go vet -tags nogui and GOOS=windows go vet ./...: ok. edit and testenv at umask 077; agent, provider, library and gateway with an empty .git above TMPDIR; davsync, imagemcp, plugin, library, edit, testenv whole; the changed tests in agent, provider, gateway and gui; the timing tests -count=10 (gateway, davsync, imagemcp) and -count=5 (agent): all ok. TestCoreScriptBudget ok: core 1,536,751 of 1,537,034, app.js 1,312,975 of 1,315,316, no budget raised. - Red on the old code: on origin/main, TestWriteAtomicReadOnlyHardLink at umask 077 and two Reasonix tests under an empty .git; on Linux (wsl-run) TestProgram at umask 077 with the old program.go. With each fix's code taken back to main's, the Gemini imageConfig tests, TestPluginToldWhereTheAgentWorks, TestSkillRepoTraced, TestTaggedHooksAlone, TestMainURL/TestMainView and update-auto, plugin-github-hooks, plugins-more-discover, plugin-namesake were red. Broken by hand: claudeKeyed always true (both OwnEndpointNeverAsked tests red), claudeAsk a no-op (TestClaudeFirstWriteAsksTheProxy red), GitAt taking any .git folder (the three TestGitOnDisk tests red). - GUI: 123 files (plugins*, update-*, panel/tray/start/context, i18n, gui-zh-tw/ja/de and every file that evals app.js parts) in WebKit on Linux (wsl-run). 11 files had reds (click-scroll, panel-fold, plugin-card-update, plugin-github, plugin-move*, segs-glide, tray-totals, update-*), and run again side by side they fail the same on origin/maind80d75f30(79 pass, 26 fail here; 78 and 27 there). They are click-scroll guards ("the click must not scroll the page"), varying by language from run to run (#4385). copilot-ghe's route-after-close red is #1949, seen on main244d80e23too. In Chromium on mote, Chromium segfaulted partway through files (#4125): context-map, context-scroll-end and context-tune fail the same on origin/main0911a65e7, so the batch's own files (plugin-namesake, update-auto, plugins-more-discover, plugin-github-hooks) ran in Chromium on Linux: 62 of 62. - Full suite (go test -tags nogui -count=1 -timeout 45m ./..., mote) at 472a0608f (on70a0c387c): every package ok but internal/gateway, whose five reds were #4362's (Antigravity turned-away and TestCodexSpentGrokFailingIsTold), fixed on main ind80d75f30. After the rebase on244d80e23the five pass, and gofmt, the three vets and the targeted runs above were run again: ok. In Linux the sign-in, copilot, plugin-github*, plugins-more-discover, plugin-namesake, plugin-gateway, update-auto, quota-refresh-order and i18n files ran in both engines: 475 pass, 4 fail (copilot-ghe #1949, plugin-github's click scroll, both red on main too). - Rebased again onc7171a256, over its budget fixe94bf7a08: gofmt, vet of gui, gateway, provider and plugin, TestCoreScriptBudget (core 1,536,265 of 1,536,586 with the batch's last commit, statusTErr and settingsErr), the Gemini, GitOnDisk and plugin tests, and in Linux both engines i18n-keys, update-mirror, plugin-namesake, plugins-more-discover, plugin-github-hooks and update-auto (138 of 138): ok. Not tried: the whole GUI suite (mote's WebKit wouldn't launch and its Chromium segfaulted, #4125); macOS WebKit; the real app's window.
567 lines
18 KiB
Go
567 lines
18 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Plugins on GitHub that nobody listed: a repository its author tagged
|
|
// with the magpie-plugin topic. The market shows them apart from the
|
|
// community's, as not reviewed by anyone: installed from npm when its
|
|
// author published it there from the repository, else from the repository
|
|
// itself (github:owner/repo).
|
|
|
|
// Topic is the GitHub topic that puts a repository on the Plugins page.
|
|
const Topic = "magpie-plugin"
|
|
|
|
// Tagged is a repository tagged with Topic.
|
|
type Tagged struct {
|
|
Spec string `json:"spec"` // what Install adds: its npm name when npm has it from this repository, else github:owner/repo
|
|
Repo string `json:"repo"` // owner/repo
|
|
URL string `json:"url"` // its page on GitHub
|
|
Owner string `json:"owner"` // its owner's login
|
|
OwnerAvatar string `json:"ownerAvatar,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
Stars int `json:"stars"`
|
|
// License is its SPDX id as GitHub reads its LICENSE file, else as
|
|
// its package.json says (PkgLicense): a LICENSE GitHub can't match to
|
|
// one (NOASSERTION: MIT's text with a line of credit added) or none at
|
|
// all left the card with no licence, while npm shows package.json's
|
|
// (plugins#468)
|
|
License string `json:"license,omitempty"`
|
|
PkgLicense string `json:"pkgLicense,omitempty"`
|
|
Pushed time.Time `json:"pushed"`
|
|
// from its package.json on the default branch: what bun will install
|
|
// it as, its version, and whether it is gateway middleware or an agent
|
|
Package string `json:"package,omitempty"`
|
|
Version string `json:"version,omitempty"`
|
|
Kind string `json:"kind,omitempty"` // "middleware", "agent", "page" or "hooks" when that is all it is
|
|
// its package.json's magpie.icon, as an installed plugin gives its
|
|
// provider one: an https URL or a data:image URI, as it is written
|
|
Icon string `json:"icon,omitempty"`
|
|
}
|
|
|
|
// Where GitHub is asked, moved by tests.
|
|
var (
|
|
githubAPI = "https://api.github.com"
|
|
githubRaw = "https://raw.githubusercontent.com"
|
|
)
|
|
|
|
// taggedTTL is how long what GitHub said stands: short, so a repository
|
|
// tagged a moment ago shows within minutes (lee04052822 on X tagged theirs
|
|
// and, asked six hours apart, didn't see it). A search is one request of
|
|
// the ten a minute; what each repository's package.json and npm said is
|
|
// kept while its last push stays the same, so asking again costs no more.
|
|
const taggedTTL = 10 * time.Minute
|
|
|
|
// taggedSearch bounds each page of GitHub's search: from mainland China
|
|
// a page straight from api.github.com takes about 30s (MYCF711, #3923),
|
|
// and with the search under taggedWait the list was empty there.
|
|
// taggedWait is how long the page then waits for the repositories' reads;
|
|
// taggedEach bounds a repository's reads (package.json, npm, its entry file), which go on past
|
|
// that wait and are kept for the next ask; taggedAtOnce repositories are
|
|
// read at once; and a list that left a repository out for want of an
|
|
// answer is asked again after taggedRetry. Through a proxy far away each
|
|
// read takes a few hundred milliseconds (larchsis on X: a list cut short
|
|
// at the wait was kept ten minutes).
|
|
var (
|
|
taggedSearch = 60 * time.Second
|
|
taggedWait = 8 * time.Second
|
|
taggedEach = 20 * time.Second
|
|
taggedAtOnce = 12
|
|
taggedRetry = 20 * time.Second
|
|
// taggedPage is how many repositories one page of the search holds
|
|
// (GitHub's most is 100)
|
|
taggedPage = 100
|
|
)
|
|
|
|
var (
|
|
taggedMu sync.Mutex
|
|
taggedL []Tagged
|
|
taggedAt time.Time
|
|
// taggedWhy is why taggedL isn't GitHub's whole answer: the search
|
|
// failed (taggedL is what it said before), or repositories it named
|
|
// couldn't be read. nil when it is.
|
|
taggedWhy error
|
|
// owner/repo@pushed → what its package.json and npm made of it, nil
|
|
// when it was left out
|
|
readMu sync.Mutex
|
|
readRepo = map[string]*Tagged{}
|
|
)
|
|
|
|
func taggedCache() string { return filepath.Join(filepath.Dir(marketCache()), "plugin-github.json") }
|
|
|
|
// taggedOnDisk is the list as GitHub said it last, kept on disk; nil
|
|
// when there is none.
|
|
func taggedOnDisk() []Tagged {
|
|
var l []Tagged
|
|
if b, err := os.ReadFile(taggedCache()); err == nil {
|
|
_ = json.Unmarshal(b, &l)
|
|
}
|
|
return l
|
|
}
|
|
|
|
// ErrNoAnswer is GitHub not answering a read before the page stopped
|
|
// waiting for it.
|
|
var ErrNoAnswer = errors.New("no answer")
|
|
|
|
// TaggedMissing is the error TaggedRepos gives when GitHub's search
|
|
// answered but repositories it named couldn't be read: Missing of them
|
|
// aren't in the list, for Why.
|
|
type TaggedMissing struct {
|
|
Missing int
|
|
Why error
|
|
}
|
|
|
|
func (e *TaggedMissing) Error() string {
|
|
return fmt.Sprintf("%d repositories tagged %s couldn't be read from GitHub: %v", e.Missing, Topic, e.Why)
|
|
}
|
|
|
|
func (e *TaggedMissing) Unwrap() error { return e.Why }
|
|
|
|
// TaggedRepos are the repositories tagged with Topic, the most starred
|
|
// first: GitHub asked at most every ten minutes (its search lets 10 a minute
|
|
// through without a token), else what it said last, kept on disk. Archived
|
|
// repositories, forks and magpie-community's own (the market lists those)
|
|
// are left out, as is one whose package.json doesn't parse, which bun
|
|
// couldn't install either. A repository whose package.json didn't come in
|
|
// time is as the list had it last, and the list is asked again in
|
|
// taggedRetry. MAGPIE_PLUGIN_MARKET=off keeps to none.
|
|
//
|
|
// The error says the list isn't GitHub's whole answer, and why: the search
|
|
// failed (the list is what GitHub said before, empty when it never
|
|
// answered), or, a *TaggedMissing, repositories it named couldn't be read.
|
|
// An empty list is never a failed read passed off as there being none
|
|
// (till on Discord: GitHub unreachable from their desktop, and the
|
|
// section just wasn't there).
|
|
func TaggedRepos(ctx context.Context) ([]Tagged, error) {
|
|
taggedMu.Lock()
|
|
defer taggedMu.Unlock()
|
|
if taggedL != nil && time.Since(taggedAt) < taggedTTL {
|
|
return taggedL, taggedWhy
|
|
}
|
|
if os.Getenv("MAGPIE_PLUGIN_MARKET") == "off" {
|
|
return []Tagged{}, nil
|
|
}
|
|
// a magpie started again within the ten minutes: what GitHub said then,
|
|
// without the wait of asking (a package.json read for each)
|
|
if taggedL == nil {
|
|
if fi, err := os.Stat(taggedCache()); err == nil && time.Since(fi.ModTime()) < taggedTTL {
|
|
if l := taggedOnDisk(); l != nil {
|
|
taggedL, taggedAt, taggedWhy = l, fi.ModTime(), nil
|
|
return l, nil
|
|
}
|
|
}
|
|
}
|
|
last := taggedL
|
|
if last == nil {
|
|
last = taggedOnDisk()
|
|
}
|
|
if l, whole, missing, err := askTagged(ctx, last); err == nil {
|
|
taggedL, taggedAt, taggedWhy = l, time.Now(), nil
|
|
if !whole {
|
|
taggedAt = time.Now().Add(-taggedTTL + taggedRetry)
|
|
}
|
|
if missing != nil {
|
|
taggedWhy = missing
|
|
}
|
|
if b, err := json.Marshal(l); err == nil {
|
|
_ = os.MkdirAll(filepath.Dir(taggedCache()), 0o755)
|
|
_ = os.WriteFile(taggedCache(), b, 0o644)
|
|
// a magpie started again meanwhile asks again too
|
|
_ = os.Chtimes(taggedCache(), taggedAt, taggedAt)
|
|
}
|
|
return l, taggedWhy
|
|
} else {
|
|
// GitHub didn't answer: what it said last, asked again in a
|
|
// minute, and why it is that
|
|
l := last
|
|
if l == nil {
|
|
l = []Tagged{}
|
|
}
|
|
taggedL, taggedAt, taggedWhy = l, time.Now().Add(-taggedTTL+time.Minute), err
|
|
return l, err
|
|
}
|
|
}
|
|
|
|
// TaggedAskAgain has the next TaggedRepos ask GitHub at once, when what
|
|
// stands now isn't its whole answer: the Plugins page's Try again. A list
|
|
// GitHub gave whole stands its ten minutes all the same.
|
|
func TaggedAskAgain() {
|
|
taggedMu.Lock()
|
|
defer taggedMu.Unlock()
|
|
if taggedWhy != nil {
|
|
taggedAt = time.Time{}
|
|
}
|
|
}
|
|
|
|
// askTagged asks GitHub's search, and reads each repository it names
|
|
// that wasn't read at its last push. whole is false when a repository was
|
|
// left out for want of an answer in taggedWait: it is then as last had
|
|
// it, if it had it, and missing counts those last didn't have.
|
|
func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, missing *TaggedMissing, err error) {
|
|
type item struct {
|
|
FullName string `json:"full_name"`
|
|
HTMLURL string `json:"html_url"`
|
|
Description string `json:"description"`
|
|
Stars int `json:"stargazers_count"`
|
|
DefaultBranch string `json:"default_branch"`
|
|
Pushed time.Time `json:"pushed_at"`
|
|
Archived bool `json:"archived"`
|
|
Fork bool `json:"fork"`
|
|
Owner struct {
|
|
Login string `json:"login"`
|
|
AvatarURL string `json:"avatar_url"`
|
|
} `json:"owner"`
|
|
License *struct {
|
|
SPDX string `json:"spdx_id"`
|
|
} `json:"license"`
|
|
}
|
|
var r struct{ Items []item }
|
|
// every page, not the first: one page left out every repository past
|
|
// it. GitHub's search answers at most 1000 results.
|
|
listed := map[string]bool{}
|
|
for page := 1; page*taggedPage <= 1000; page++ {
|
|
v := url.Values{"q": {"topic:" + Topic + " archived:false fork:false"}, "sort": {"stars"}, "order": {"desc"},
|
|
"per_page": {strconv.Itoa(taggedPage)}, "page": {strconv.Itoa(page)}}
|
|
sc, cancel := context.WithTimeout(ctx, taggedSearch)
|
|
b, err := fetchJSON(sc, githubAPI+"/search/repositories?"+v.Encode(), 4<<20)
|
|
cancel()
|
|
if err != nil {
|
|
return nil, false, nil, err
|
|
}
|
|
var p struct {
|
|
Total int `json:"total_count"`
|
|
Items []item `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(b, &p); err != nil {
|
|
return nil, false, nil, err
|
|
}
|
|
for _, it := range p.Items {
|
|
// a repository starred between two pages can show on both
|
|
if k := strings.ToLower(it.FullName); !listed[k] {
|
|
listed[k] = true
|
|
r.Items = append(r.Items, it)
|
|
}
|
|
}
|
|
if len(p.Items) < taggedPage || page*taggedPage >= p.Total {
|
|
break
|
|
}
|
|
}
|
|
c, cancel := context.WithTimeout(ctx, taggedWait)
|
|
defer cancel()
|
|
before := map[string]Tagged{}
|
|
for _, t := range last {
|
|
before[strings.ToLower(t.Repo)] = t
|
|
}
|
|
// what the search says now, over what is kept of a repository
|
|
fresh := func(r, t Tagged) Tagged {
|
|
r.URL, r.Owner, r.OwnerAvatar, r.Description, r.Stars, r.License, r.Pushed = t.URL, t.Owner, t.OwnerAvatar, t.Description, t.Stars, t.License, t.Pushed
|
|
if r.License == "" {
|
|
r.License = r.PkgLicense
|
|
}
|
|
return r
|
|
}
|
|
var mu sync.Mutex // out, keep, done, why; late once the page stops waiting
|
|
late := false
|
|
var why error // a read GitHub or npm didn't answer, the last one
|
|
out := make([]Tagged, len(r.Items))
|
|
keep := make([]bool, len(r.Items))
|
|
done := make([]bool, len(r.Items))
|
|
asked := make([]Tagged, len(r.Items))
|
|
var wg sync.WaitGroup
|
|
sem := make(chan struct{}, taggedAtOnce)
|
|
// a read goes on after the page stops waiting, and is kept for the
|
|
// next ask
|
|
bg := context.WithoutCancel(ctx)
|
|
seen := map[string]bool{}
|
|
for i, it := range r.Items {
|
|
if it.Archived || it.Fork || strings.EqualFold(it.Owner.Login, "magpie-community") || !strings.Contains(it.FullName, "/") {
|
|
done[i] = true
|
|
continue
|
|
}
|
|
t := Tagged{
|
|
Spec: "github:" + it.FullName, Repo: it.FullName, URL: it.HTMLURL, Owner: it.Owner.Login, OwnerAvatar: it.Owner.AvatarURL,
|
|
Description: it.Description, Stars: it.Stars, Pushed: it.Pushed,
|
|
}
|
|
if it.License != nil && it.License.SPDX != "NOASSERTION" {
|
|
t.License = it.License.SPDX
|
|
}
|
|
asked[i] = t
|
|
key := it.FullName + "@" + it.Pushed.Format(time.RFC3339)
|
|
seen[key] = true
|
|
readMu.Lock()
|
|
was, read := readRepo[key]
|
|
readMu.Unlock()
|
|
if read {
|
|
if was != nil {
|
|
out[i], keep[i] = fresh(*was, t), true
|
|
}
|
|
done[i] = true
|
|
continue
|
|
}
|
|
wg.Add(1)
|
|
go func(i int, t Tagged, branch string) {
|
|
defer wg.Done()
|
|
sem <- struct{}{}
|
|
defer func() { <-sem }()
|
|
pc, cancel := context.WithTimeout(bg, taggedEach)
|
|
defer cancel()
|
|
got, ok, read, rerr := readTagged(pc, t, branch)
|
|
if read {
|
|
readMu.Lock()
|
|
if ok {
|
|
readRepo[key] = &got
|
|
} else {
|
|
readRepo[key] = nil
|
|
}
|
|
readMu.Unlock()
|
|
}
|
|
mu.Lock()
|
|
if !late {
|
|
out[i], keep[i] = got, ok
|
|
// a package.json GitHub didn't answer for: not known
|
|
done[i] = ok || read
|
|
if !done[i] && rerr != nil {
|
|
why = rerr
|
|
}
|
|
}
|
|
mu.Unlock()
|
|
}(i, t, it.DefaultBranch)
|
|
}
|
|
waited := make(chan struct{})
|
|
go func() { wg.Wait(); close(waited) }()
|
|
select {
|
|
case <-waited:
|
|
case <-c.Done():
|
|
}
|
|
mu.Lock()
|
|
late = true
|
|
mu.Unlock()
|
|
// a repository pushed to again, or no longer tagged, is forgotten
|
|
readMu.Lock()
|
|
for k := range readRepo {
|
|
if !seen[k] {
|
|
delete(readRepo, k)
|
|
}
|
|
}
|
|
readMu.Unlock()
|
|
l, whole = []Tagged{}, true
|
|
gone := 0
|
|
for i := range out {
|
|
switch {
|
|
case keep[i]:
|
|
l = append(l, out[i])
|
|
case !done[i]:
|
|
whole = false
|
|
if was, ok := before[strings.ToLower(asked[i].Repo)]; ok {
|
|
l = append(l, fresh(was, asked[i]))
|
|
} else {
|
|
gone++
|
|
}
|
|
}
|
|
}
|
|
if gone > 0 {
|
|
mu.Lock()
|
|
w := why
|
|
mu.Unlock()
|
|
if w == nil {
|
|
w = fmt.Errorf("%w in %s", ErrNoAnswer, taggedWait)
|
|
}
|
|
missing = &TaggedMissing{Missing: gone, Why: w}
|
|
}
|
|
return l, whole, missing, nil
|
|
}
|
|
|
|
// readTagged reads a tagged repository's package.json (on branch), and
|
|
// npm's copy of the package it names: t as it is installed, whether it is
|
|
// kept, and whether that is known (GitHub or npm not answering is not,
|
|
// and it is read again next time), with the read that didn't answer.
|
|
func readTagged(pc context.Context, t Tagged, branch string) (Tagged, bool, bool, error) {
|
|
if branch == "" {
|
|
branch = "HEAD"
|
|
}
|
|
pb, err := fetchJSON(pc, githubRaw+"/"+t.Repo+"/"+url.PathEscape(branch)+"/package.json", 2<<20)
|
|
if err != nil {
|
|
if errors.Is(err, errNotFound) {
|
|
return t, false, true, nil
|
|
}
|
|
return t, false, false, err
|
|
}
|
|
var pj struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
Main string `json:"main"`
|
|
Exports json.RawMessage `json:"exports"`
|
|
License any `json:"license"`
|
|
Magpie struct {
|
|
Middleware string `json:"middleware"`
|
|
Agent string `json:"agent"`
|
|
Hooks string `json:"hooks"`
|
|
Icon string `json:"icon"`
|
|
Page struct {
|
|
File string `json:"file"`
|
|
} `json:"page"`
|
|
} `json:"magpie"`
|
|
}
|
|
if json.Unmarshal(pb, &pj) != nil || !pkgName.MatchString(pj.Name) {
|
|
return t, false, true, nil
|
|
}
|
|
t.Package, t.Version = pj.Name, pj.Version
|
|
// the licence it names, a string as npm takes it, for when GitHub
|
|
// couldn't tell one from its LICENSE file
|
|
if l, ok := pj.License.(string); ok {
|
|
t.PkgLicense = strings.TrimSpace(l)
|
|
if t.License == "" {
|
|
t.License = t.PkgLicense
|
|
}
|
|
}
|
|
// a picture, as host.js's iconOf takes one: magpie checks and
|
|
// keeps it before the page shows it (the GUI's /api/plugins/github)
|
|
t.Icon = ownIcon(pj.Magpie.Icon)
|
|
if pj.Main == "" && len(pj.Exports) == 0 {
|
|
switch {
|
|
case strings.TrimSpace(pj.Magpie.Middleware) != "":
|
|
t.Kind = "middleware"
|
|
case strings.TrimSpace(pj.Magpie.Agent) != "":
|
|
t.Kind = "agent"
|
|
case strings.TrimSpace(pj.Magpie.Page.File) != "":
|
|
t.Kind = "page"
|
|
// gateway hooks alone (magpie.hooks) need no entry of their
|
|
// own, as the host loads them: the hooks file is the one to
|
|
// find (#3821)
|
|
case strings.TrimSpace(pj.Magpie.Hooks) != "":
|
|
t.Kind = "hooks"
|
|
}
|
|
}
|
|
// published to npm from this repository: npm's copy is the one
|
|
// built to be installed (a repository often leaves its dist out,
|
|
// built only to publish), so it is installed from npm
|
|
b, err := fetchJSON(pc, npmRegistry+"/"+npmPath(pj.Name)+"/latest", 1<<20)
|
|
known := err == nil || errors.Is(err, errNotFound)
|
|
var unknown error
|
|
if !known {
|
|
unknown = err
|
|
}
|
|
// npmNames is whether npm's copy names what it loads (main,
|
|
// exports); one that names nothing loads an index file
|
|
npmNames := false
|
|
if err == nil {
|
|
var l npmLatest
|
|
if json.Unmarshal(b, &l) == nil && l.Version != "" && strings.EqualFold(githubRepo(repoURL(l.Repository)), t.Repo) {
|
|
t.Spec, t.Version = pj.Name, l.Version
|
|
npmNames = l.Main != "" || len(l.Exports) > 0
|
|
}
|
|
}
|
|
// installed from the repository, the file it loads must be in
|
|
// it: one whose dist is built only to publish couldn't load
|
|
// (GitHub not answering is no answer, and keeps it; so is a
|
|
// name Bun would add .js or /index.js to). Installed from npm,
|
|
// a copy that names no file loads index.js, which the
|
|
// repository has too when it is a plugin at all: a package
|
|
// that is only a command (bin), an MCP server, has none and
|
|
// would never load (#1327)
|
|
if IsGit(t.Spec) || !npmNames {
|
|
entry := pj.Magpie.Middleware
|
|
if t.Kind == "agent" {
|
|
entry = pj.Magpie.Agent
|
|
} else if t.Kind == "page" {
|
|
entry = pj.Magpie.Page.File
|
|
} else if t.Kind == "hooks" {
|
|
entry = pj.Magpie.Hooks
|
|
} else if t.Kind != "middleware" {
|
|
entry = pkgEntry(pj.Exports, pj.Main)
|
|
if !IsGit(t.Spec) {
|
|
entry = pkgEntry(nil, "")
|
|
}
|
|
}
|
|
if path.Ext(entry) != "" {
|
|
_, err := fetchJSON(pc, githubRaw+"/"+t.Repo+"/"+url.PathEscape(branch)+"/"+strings.TrimPrefix(path.Clean("/"+entry), "/"), 1)
|
|
if errors.Is(err, errNotFound) {
|
|
return t, false, known, unknown
|
|
}
|
|
if err != nil {
|
|
known, unknown = false, err
|
|
}
|
|
}
|
|
}
|
|
return t, true, known, unknown
|
|
}
|
|
|
|
// pkgEntry is the file importing a package loads, as Bun resolves it: its
|
|
// exports' "." (import, else default), else main, else index.js.
|
|
func pkgEntry(exports json.RawMessage, main string) string {
|
|
var pick func(v any) string
|
|
pick = func(v any) string {
|
|
switch v := v.(type) {
|
|
case string:
|
|
return v
|
|
case map[string]any:
|
|
if d, ok := v["."]; ok {
|
|
return pick(d)
|
|
}
|
|
for _, k := range []string{"bun", "import", "default", "node", "require"} {
|
|
if s := pick(v[k]); s != "" {
|
|
return s
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
var v any
|
|
if len(exports) > 0 && json.Unmarshal(exports, &v) == nil {
|
|
if s := pick(v); s != "" {
|
|
return s
|
|
}
|
|
}
|
|
if main != "" {
|
|
return main
|
|
}
|
|
return "index.js"
|
|
}
|
|
|
|
// githubReadme is the README of a GitHub repository not installed yet, as
|
|
// its default branch has it.
|
|
func githubReadme(ctx context.Context, repo string) (Page, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 12*time.Second)
|
|
defer cancel()
|
|
var last error
|
|
for _, f := range []string{"README.md", "readme.md", "Readme.md", "README"} {
|
|
b, err := fetchJSON(ctx, githubRaw+"/"+repo+"/HEAD/"+f, 200<<10)
|
|
if err == nil {
|
|
return Page{Readme: string(b)}, nil
|
|
}
|
|
last = err
|
|
}
|
|
return Page{}, last
|
|
}
|
|
|
|
// githubRepo is owner/repo of a github: spec or a GitHub page's URL, ""
|
|
// for any other.
|
|
func githubRepo(spec string) string {
|
|
s := strings.TrimSuffix(strings.SplitN(spec, "#", 2)[0], ".git")
|
|
switch {
|
|
case strings.HasPrefix(s, "github:"):
|
|
s = strings.TrimPrefix(s, "github:")
|
|
case strings.HasPrefix(s, "https://github.com/"), strings.HasPrefix(s, "https://www.github.com/"):
|
|
s = s[strings.Index(s, "github.com/")+len("github.com/"):]
|
|
default:
|
|
return ""
|
|
}
|
|
p := strings.Split(strings.Trim(s, "/"), "/")
|
|
if len(p) < 2 || p[0] == "" || p[1] == "" {
|
|
return ""
|
|
}
|
|
return p[0] + "/" + p[1]
|
|
}
|