Files
yetone 860ed6618d plugins: a 「国内镜像」 switch on the Plugins page downloads the list, plugins and Bun from mirrors in China first (Discord, akic404: GitHub/npm are slow from the mainland; asked for ghproxy) — Plugins › Mirrors in China, off by default, kept in settings (chinaMirror). On, source asks a copy of the very file before its official address: npm's registry (packages, versions, search) at registry.npmmirror.com, Bun's release zips at registry.npmmirror.com/-/binary/bun, and the community registry.json at cdn.jsdelivr.net/gh/magpie-community/plugins@<branch>; bun add gets BUN_CONFIG_REGISTRY=npmmirror unless the environment names a registry. No proxy: gh-proxy (MAGPIE_MIRRORS' GitHub fallback) handles nothing the switch sends, since a proxy could hand back other code. A mirror asked first that can't answer (404/403/5xx, e.g. a version it hasn't synced) falls through to the official address; npm's weekly downloads stay api.npmjs.org's (npmmirror counts its own). Checked by hand: npmmirror's bun-v1.4.2 darwin-aarch64 zip has the SHA-256 pinned in bun.go, jsDelivr's registry.json is byte-identical to raw.githubusercontent's, and bun 1.4.2 add through npmmirror locks the same sha512 integrity npmjs gives. TestChinaFirst, TestChinaComesFirst, TestChinaMirrorAnswers, TestChinaMirrorsMissIsAskedOfficially, TestDoFaithful, TestBunInstallsFromChinaMirror and TestPluginsMirrorSwitch cover it. A live run with the switch on fetched the list from jsDelivr, npm info from npmmirror and Bun from npmmirror (checksum passed); a sandbox magpie web showed the switch in the Plugins head, saved it, kept it after a reload, and a click didn't move the page. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried from inside mainland China.
2026-10-03 15:14:45 +08:00

41 lines
1.1 KiB
Go

package plugin
import (
"context"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/source"
)
// with 「国内镜像」, bun installs from npmmirror, unless the environment
// names a registry of its own
func TestBunInstallsFromChinaMirror(t *testing.T) {
was := source.China
t.Cleanup(func() { source.China = was })
t.Setenv("BUN_CONFIG_REGISTRY", "")
t.Setenv("NPM_CONFIG_REGISTRY", "")
reg := func() []string {
var out []string
for _, kv := range bunCommand(context.Background(), "bun", t.TempDir(), "add", "x").Env {
if k, _, _ := strings.Cut(kv, "="); k == "BUN_CONFIG_REGISTRY" && kv != "BUN_CONFIG_REGISTRY=" {
out = append(out, kv)
}
}
return out
}
source.China = func() bool { return false }
if r := reg(); len(r) != 0 {
t.Fatalf("switch off: %q", r)
}
source.China = func() bool { return true }
if r := reg(); !slices.Equal(r, []string{"BUN_CONFIG_REGISTRY=https://registry.npmmirror.com/"}) {
t.Fatalf("switch on: %q", r)
}
t.Setenv("NPM_CONFIG_REGISTRY", "https://npm.example/")
if r := reg(); len(r) != 0 {
t.Fatalf("a registry of the user's own: %q", r)
}
}