library: the Pi market finds packages by name on npmmirror (eachg0 on X)

eachg0 on X: searching the Pi market for "rename" (or "pi-rename") found
nothing, though npm's pi-rename 1.2.0 carries the pi-package keyword.
cf20df721 (11-18) made the market ask the registry in use first: magpie's
own, the user's .npmrc/bunfig/env, or npmmirror with the 「国内镜像」
switch. npmmirror knows no keywords: qualifier. It answers 200
{"objects":[],"total":0} to "keywords:pi-package rename", so npm's own
search, asked only when the registry fails, never was. Its plain search,
"pi-package rename", finds 52, every one with the keyword.

A search with words that comes back empty is now asked again with the
qualifier as a plain word (plugin.PlainKeywords), and the keyword is
still checked here. The list before a search is unchanged: npmmirror
answers it with nothing, and magpie's snapshot stands, as before.

Sibling: the Plugins page's search (plugin.Search) asks
"keywords:pi-package <q>" too, and with the switch on source.Do sends it
to npmmirror first, so it found no pi provider packages there either.
searchNPM retries the same way. No other search uses a qualifier.

Tests, from npmmirror's own bytes (testdata/npmmirror, 2026-11-19):
TestPiSearchFindsByNameOnNPMMirror ("rename" and "pi-rename" find
pi-rename with npm's own search down), TestSearchFindsPiPackagesOnNPMMirror,
TestPlainKeywords.

Run (mica, at this tree; origin/main 3869aee3b, unchanged at push):
- gofmt -l: clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok; go build, GOOS=linux go build -tags
  nogui, GOOS=windows go build: ok
- go test -tags nogui ./internal/library ./internal/plugin: ok
- go test -tags nogui ./internal/gui: two reds, both filed and both
  passing alone at this tree: TestAppReplacesItsOlderGatewayOnceIdle
  (#6297) and TestGatewayModeListsNoOwnSessions (#7064)
- GUI library-pi-market.test.cjs: Chromium 22/22, WebKit 11/11

Broken by hand: the old pimarket.go and market.go with the new tests:
TestPiSearchFindsByNameOnNPMMirror red ("rename": [] asked
["keywords:pi-package" "keywords:pi-package rename"]),
TestSearchFindsPiPackagesOnNPMMirror red (Search = []).
Real use (sandbox HOME, NPM_CONFIG_REGISTRY=https://registry.npmmirror.com,
live npmmirror): old code "rename" 0 results, "pi-rename" 0; new code
52 and 52 with pi-rename; Plugins search "antigravity" finds the pi
provider packages. npm's own registry: pi-rename found before and after.
Not run: the full go test ./... (mote and mica each had another
session's full suite running), the whole GUI suite (no GUI or API change).
Not tried: the real 「国内镜像」 switch in the app (same registry URL as
the env var path).
This commit is contained in:
yetone
2026-11-19 11:50:37 +08:00
parent 3869aee3b0
commit 2051dfc232
9 changed files with 144 additions and 7 deletions
+24 -7
View File
@@ -157,14 +157,31 @@ func searchPiNPM(q string, size, from int) ([]piListed, int, error) {
} `json:"package"`
} `json:"objects"`
}
query := "?" + url.Values{"text": {text}, "size": {strconv.Itoa(size)}, "from": {strconv.Itoa(from)}}.Encode()
err := errors.New("no registry")
if r := strings.TrimRight(piRegistry(), "/"); r != "" && r != strings.TrimSuffix(piNPMURL, "/-/v1/search") {
err = getJSON(r+"/-/v1/search"+query, &body)
ask := func(text string) error {
body.Total, body.Objects = 0, nil
query := "?" + url.Values{"text": {text}, "size": {strconv.Itoa(size)}, "from": {strconv.Itoa(from)}}.Encode()
err := errors.New("no registry")
if r := strings.TrimRight(piRegistry(), "/"); r != "" && r != strings.TrimSuffix(piNPMURL, "/-/v1/search") {
err = getJSON(r+"/-/v1/search"+query, &body)
}
if err != nil {
if err = getJSON(piNPMURL+query, &body); err != nil {
return &SearchError{Source: "npm", Err: err}
}
}
return nil
}
if err != nil {
if err = getJSON(piNPMURL+query, &body); err != nil {
return nil, 0, &SearchError{Source: "npm", Err: err}
if err := ask(text); err != nil {
return nil, 0, err
}
// npmmirror answers nothing to a keywords: qualifier, and finds a
// search's words with the keyword as a plain word (PlainKeywords:
// eachg0 on X, "rename" found no pi-rename). The list before a search
// stays the one magpie shipped with there: npmmirror's has no monthly
// downloads to put the most used first.
if q != "" && len(body.Objects) == 0 && from == 0 {
if ask(plugin.PlainKeywords(text)) != nil {
body.Total, body.Objects = 0, nil // the first answer stands
}
}
var out []piListed
+51
View File
@@ -11,6 +11,7 @@ import (
"runtime"
"slices"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -383,3 +384,53 @@ func TestPiRunGivesAgentDir(t *testing.T) {
t.Errorf("err = %v, want npm's own words", err)
}
}
// eachg0 on X: with npmmirror as the registry (the 「国内镜像」 switch),
// searching the Pi market for "rename" or "pi-rename" found nothing, though
// pi-rename has the pi-package keyword. npmmirror answers every search with
// a keywords: qualifier with {"objects":[],"total":0}; its plain search
// finds them. The answers are npmmirror's own bytes (testdata/npmmirror,
// 2026-11-19), and npm's own search is down, so the mirror has to find it.
func TestPiSearchFindsByNameOnNPMMirror(t *testing.T) {
piCacheHere(t)
_, _, down := piNPM(t, nil, nil)
down.Store(true)
read := func(f string) []byte {
b, err := os.ReadFile(filepath.Join("testdata", "npmmirror", f))
if err != nil {
t.Fatal(err)
}
return b
}
var texts []string
var mu sync.Mutex
mirror := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
text := r.URL.Query().Get("text")
mu.Lock()
texts = append(texts, text)
mu.Unlock()
switch {
case strings.Contains(text, "keywords:"):
w.Write(read("search-keywords.json"))
case text == "pi-package rename":
w.Write(read("search-pi-package-rename.json"))
case text == "pi-package pi-rename":
w.Write(read("search-pi-package-pi-rename.json"))
default:
w.Write(read("search-keywords.json"))
}
}))
t.Cleanup(mirror.Close)
piRegistry = func() string { return mirror.URL }
for _, q := range []string{"rename", "pi-rename"} {
list, err := MarketPiPackages(q)
if err != nil || !slices.Contains(piNames(list), "pi-rename") {
t.Errorf("%q: %v %v (asked %q)", q, piNames(list), err, texts)
}
for _, p := range list {
if p.Name == "pi-rename" && (p.Source != "npm:pi-rename" || p.Version != "1.2.0" || p.Publisher != "shanelevs") {
t.Errorf("pi-rename = %+v", p)
}
}
}
}
@@ -0,0 +1 @@
{"objects":[],"total":0}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+21
View File
@@ -640,6 +640,9 @@ func searchNPM(ctx context.Context, text string, keep func(string, []string) boo
if err := json.Unmarshal(b, &r); err != nil {
return nil, err
}
if plain := PlainKeywords(text); len(r.Objects) == 0 && plain != text {
return searchNPM(ctx, plain, keep)
}
var out []Hit
for _, o := range r.Objects {
p := o.Package
@@ -654,6 +657,24 @@ func searchNPM(ctx context.Context, text string, keep func(string, []string) boo
return out, nil
}
// PlainKeywords is a search's text with each keywords: qualifier as the
// plain word it names: "keywords:pi-package rename" is "pi-package
// rename". npmmirror (the 「国内镜像」 switch, or a user's own registry
// pointed at it) knows no qualifier and answers {"objects":[],"total":0}
// to any text that has one, while its plain search finds the packages
// with the keyword (eachg0 on X: pi-rename not found), so a search that
// came back empty is asked again in plain words and the keyword is
// checked here.
func PlainKeywords(text string) string {
f := strings.Fields(text)
for i, w := range f {
if k, ok := strings.CutPrefix(w, "keywords:"); ok && k != "" {
f[i] = strings.ReplaceAll(k, ",", " ")
}
}
return strings.Join(f, " ")
}
// Page is a package's page: its README and when it last changed.
type Page struct {
Readme string `json:"readme"`
+43
View File
@@ -409,6 +409,49 @@ func TestSearchFindsPiPackages(t *testing.T) {
}
}
// The Plugins page's search, with npmmirror answering (the 「国内镜像」
// switch puts it before npm's own): npmmirror answers a keywords:
// qualifier with nothing, so pi packages were never found there (eachg0
// on X, the Pi market's sibling). Asked again in plain words, they are.
// The answers are npmmirror's own bytes (testdata/npmmirror, 2026-11-19).
func TestSearchFindsPiPackagesOnNPMMirror(t *testing.T) {
read := func(f string) []byte {
b, err := os.ReadFile(filepath.Join("testdata", "npmmirror", f))
if err != nil {
t.Fatal(err)
}
return b
}
fakeNPM(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("text") == "pi-package antigravity" {
w.Write(read("search-pi-package-antigravity.json"))
return
}
w.Write(read("search-keywords.json"))
})
hits, err := Search(context.Background(), "antigravity")
var names []string
for _, h := range hits {
names = append(names, h.Package)
}
if err != nil || !slices.Contains(names, "pi-antigravity") {
t.Fatalf("Search = %v, %v", names, err)
}
}
func TestPlainKeywords(t *testing.T) {
for in, want := range map[string]string{
"keywords:pi-package rename": "pi-package rename",
"keywords:a,b x": "a b x",
"rename opencode": "rename opencode",
"keywords:": "keywords:",
} {
if got := PlainKeywords(in); got != want {
t.Errorf("PlainKeywords(%q) = %q, want %q", in, got, want)
}
}
}
// pinnedPi is the pi testdata/pi-lock installs. Its bun.lock names every
// package of pi at a version and its npm integrity, so the test runs on
// the same pi each time. Installed as ensurePi installs it, at npm's
@@ -0,0 +1 @@
{"objects":[],"total":0}
File diff suppressed because one or more lines are too long