diff --git a/internal/library/pimarket.go b/internal/library/pimarket.go index 6c8faa6cd..bab827b16 100644 --- a/internal/library/pimarket.go +++ b/internal/library/pimarket.go @@ -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 { diff --git a/internal/library/pimarket_test.go b/internal/library/pimarket_test.go index 165e3aad0..b484d5f4b 100644 --- a/internal/library/pimarket_test.go +++ b/internal/library/pimarket_test.go @@ -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{}