library: the Pi market searches the npm registry in use
The Library's Pi market asked registry.npmjs.org for every search, even
when the user's setup names another registry (magpie's own setting, an
.npmrc or bunfig, NPM_CONFIG_REGISTRY) or the 「国内镜像」 switch puts
npmmirror in front: Plugins installs from that registry, and the Pi
market alone went around it. It now asks plugin.Registry() first and
npm's own search only when that registry can't search (a private
registry without /-/v1/search).
TestPiSearchAsksTheRegistryInUse: the registry in use answers and npm
isn't asked; it answers 404 and npm is. The old tests now fix the
registry to none, so none of them reaches a real registry.
Run (mica, at this tree):
- go vet ./internal/library: ok
- go test ./internal/library -run Pi -count=1: ok
Broken by hand: the registry never asked: TestPiSearchAsksTheRegistryInUse
red ("npm asked 1 times"), then put back.
Not tried: a real private registry or npmmirror's search.
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
||||
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/lazyre"
|
||||
"github.com/yetone/magpie/internal/plugin"
|
||||
"github.com/yetone/magpie/internal/proc"
|
||||
)
|
||||
|
||||
@@ -66,8 +67,13 @@ type piListed struct {
|
||||
Date string `json:"date,omitempty"`
|
||||
}
|
||||
|
||||
// piNPMURL is npm's search; a variable for tests.
|
||||
var piNPMURL = "https://registry.npmjs.org/-/v1/search"
|
||||
// piNPMURL is npm's own search, asked when the registry in use (the
|
||||
// user's own, or npmmirror with the 「国内镜像」 switch) can't search;
|
||||
// piRegistry is that registry. Variables for tests.
|
||||
var (
|
||||
piNPMURL = "https://registry.npmjs.org/-/v1/search"
|
||||
piRegistry = func() string { return plugin.Registry().URL }
|
||||
)
|
||||
|
||||
//go:embed market_pi.json
|
||||
var piSnapshot []byte
|
||||
@@ -151,9 +157,15 @@ func searchPiNPM(q string, size, from int) ([]piListed, int, error) {
|
||||
} `json:"package"`
|
||||
} `json:"objects"`
|
||||
}
|
||||
u := piNPMURL + "?" + url.Values{"text": {text}, "size": {strconv.Itoa(size)}, "from": {strconv.Itoa(from)}}.Encode()
|
||||
if err := getJSON(u, &body); err != nil {
|
||||
return nil, 0, &SearchError{Source: "npm", Err: err}
|
||||
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 nil, 0, &SearchError{Source: "npm", Err: err}
|
||||
}
|
||||
}
|
||||
var out []piListed
|
||||
for _, o := range body.Objects {
|
||||
|
||||
@@ -42,13 +42,43 @@ func piNPM(t *testing.T, popular, found []string) (asked *atomic.Int32, texts *[
|
||||
fmt.Fprintf(w, `{"total":%d,"objects":[%s]}`, len(list), strings.Join(list, ","))
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
was := piNPMURL
|
||||
piNPMURL = srv.URL
|
||||
t.Cleanup(func() { piNPMURL = was })
|
||||
was, wasReg := piNPMURL, piRegistry
|
||||
piNPMURL, piRegistry = srv.URL, func() string { return "" }
|
||||
t.Cleanup(func() { piNPMURL, piRegistry = was, wasReg })
|
||||
piForget(t)
|
||||
return asked, texts, down
|
||||
}
|
||||
|
||||
// The registry in use is searched first (a user behind a registry of their
|
||||
// own, or npmmirror with the 「国内镜像」 switch), and npm's own only when it
|
||||
// can't search.
|
||||
func TestPiSearchAsksTheRegistryInUse(t *testing.T) {
|
||||
piCacheHere(t)
|
||||
pkg := `{"package":{"name":"pi-own","version":"1.0.0","keywords":["pi-package"]}}`
|
||||
asked, _, _ := piNPM(t, []string{`{"package":{"name":"pi-npm","version":"1.0.0","keywords":["pi-package"]}}`}, nil)
|
||||
var regAsked atomic.Int32
|
||||
var regDown atomic.Bool
|
||||
reg := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
regAsked.Add(1)
|
||||
if regDown.Load() || r.URL.Path != "/-/v1/search" {
|
||||
http.Error(w, "no search here", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, `{"total":1,"objects":[%s]}`, pkg)
|
||||
}))
|
||||
t.Cleanup(reg.Close)
|
||||
piRegistry = func() string { return reg.URL + "/" }
|
||||
list, _, err := searchPiNPM("", 250, 0)
|
||||
if err != nil || len(list) != 1 || list[0].Name != "pi-own" || asked.Load() != 0 {
|
||||
t.Fatalf("registry in use: %v %+v, npm asked %d times", err, list, asked.Load())
|
||||
}
|
||||
regDown.Store(true)
|
||||
list, _, err = searchPiNPM("", 250, 0)
|
||||
if err != nil || len(list) != 1 || list[0].Name != "pi-npm" || asked.Load() != 1 {
|
||||
t.Fatalf("registry can't search: %v %+v, npm asked %d times", err, list, asked.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func piForget(t *testing.T) {
|
||||
piPopular.Lock()
|
||||
piPopular.list, piPopular.at = nil, time.Time{}
|
||||
|
||||
Reference in New Issue
Block a user