Files
magpie/internal/plugin/dist.go
T
yetone 118f4f28bf plugin: a community plugin npm doesn't have installs from the plugins repo's dist branch, so a reviewed plugin no longer waits unmerged for an owner's npm 2FA (magpie-community/plugins#84)
npm's Trusted Publishing can't create a package, so a new plugin's first
version needs an owner's 2FA, and reviewed PRs sat unmerged for it. A
trust made after 2026-09-03 also allows stage publish only, which is why
npm refused zed 0.1.7 and zen-free 0.1.7.

The plugins repo now packs every merged version onto its `dist` branch
(scripts/dist.mjs, magpie-community/plugins f5308cd): tgz/<file> and an
index.json with each version's sha512, manifest and README, with no
credentials. magpie still asks npm first. Only when `bun add` fails for a
@magpie-community package at latest or an exact version, and not because
of the user's minimumReleaseAge (that hold is kept), does installDist
fetch the tarball. It refuses the tarball unless its sha512 is the index's,
keeps it in plugins/dist and runs `bun add ./dist/<file>`. A later npm
install replaces it, and pruneDist removes tarballs package.json no longer
names. npmAsk (market version, Update, the hourly check) takes dist's
latest when npm errors or is behind, and Readme falls back to dist's. The
branch is read through jsDelivr when the China mirror switch is on, else
raw.githubusercontent.com. installMu is now held for the whole install,
fallback included.

Run at this head (on origin/main 8d2a4a7da):
- go test ./... -count=1: all ok but internal/gateway, which hit the
  600s default timeout inside a file rename. The disk was at 5.3 GB free
  and the load around 22. Rerun alone with -timeout 30m: ok in 559.7s.
- gofmt -l clean. GOOS=windows go vet ./... ok. GOOS=linux go vet ok on
  every magpie package that doesn't import Wails (its Linux files need
  cgo, which can't be cross-compiled from macOS).
- New tests, with real Bun 1.4.2 against a fake npm and a fake dist:
  TestDistInstallsAPackageNPMDoesntHave, TestDistServesAVersionNPMRefused,
  TestDistRefusesATarballNotItsIndexs and TestDistWanted.
  TestUpdateBunHoldsBackSaysWhy now has dist holding the held version too.
- Broken by hand, each went red: no fallback, no sha512 check, no prune,
  no dist version in npmAsk, a loose file regexp, and no minAge guard.
- The real dist branch serves index.json and zed's 0.1.7 tarball from
  raw (200). jsDelivr's @dist answered 200, cold in ~20s.

- Real use, sandbox HOME, live npm and live dist: Info said zed 0.1.7
  (npm has 0.1.6), and Add@0.1.7 installed it from dist, package.json
  naming ./dist/magpie-community-opencode-zed-auth-0.1.7.tgz.

Not tried: the GUI's Add on the live branch, or the China mirror path.
2026-10-15 05:18:56 +08:00

221 lines
6.7 KiB
Go

package plugin
import (
"context"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
// The community repo's dist branch: every version of each
// @magpie-community package, packed by the repo's publish workflow
// (scripts/dist.mjs there) as soon as it is merged. npm can't take a new
// package's first version without an owner's 2FA (trusted publishing sets
// up only a package that exists), so a reviewed plugin used to wait,
// unmerged, for someone to publish it by hand. magpie asks npm first, as
// before, and turns here only for what npm doesn't give: a package or
// version npm doesn't have, or npm not answering. A tarball is installed
// only when its sha512 is the one index.json names.
// distBase is where the branch is read; moved by tests. Read through the
// 「国内镜像」 switch's jsDelivr copy first when it is on (source.DoFaithful).
var distBase = "https://raw.githubusercontent.com/magpie-community/plugins/dist"
// distScope is the scope of the packages the branch has.
const distScope = "@magpie-community/"
type distVersion struct {
File string `json:"file"` // tgz/<npm pack's file name>
Integrity string `json:"integrity"` // sha512-<base64>, as npm writes it
Manifest json.RawMessage `json:"manifest"` // the version's package.json
}
type distPackage struct {
Latest string `json:"latest"`
Readme string `json:"readme"`
Versions map[string]distVersion `json:"versions"`
}
type distIndex struct {
Packages map[string]distPackage `json:"packages"`
}
var (
distMu sync.Mutex
distIdx *distIndex
distAt time.Time // when distIdx was read, or last failed to be
distOK bool // whether the last read was distIdx's
distKeep = 10 * time.Minute
distWait = time.Minute // after a read failed, before asking again
)
// distFile is a file the index may name: one tarball under tgz/, nothing
// that leaves the branch or plugins/dist.
var distFile = regexp.MustCompile(`^tgz/[A-Za-z0-9._-]+\.tgz$`)
// distPkg is what the dist branch has of the package, nil when it has
// none, the package isn't one of its scope, or the branch can't be read.
func distPkg(ctx context.Context, name string) *distPackage {
if !strings.HasPrefix(name, distScope) || distBase == "" {
return nil
}
distMu.Lock()
defer distMu.Unlock()
stale := distIdx == nil || time.Since(distAt) > distKeep
if stale && (distOK || distAt.IsZero() || time.Since(distAt) > distWait) {
b, err := fetchJSONFaithful(ctx, distBase+"/index.json", 32<<20)
var idx distIndex
if err == nil && json.Unmarshal(b, &idx) == nil {
distIdx, distOK = &idx, true
} else {
// what was read last stays, until a read works
distOK = false
}
distAt = time.Now()
}
if distIdx == nil {
return nil
}
p, ok := distIdx.Packages[name]
if !ok {
return nil
}
return &p
}
// distInfo is the package's newest version on the dist branch, as npm's
// /latest would say it, and whether there is one.
func distInfo(ctx context.Context, name string) (NPM, bool) {
d := distPkg(ctx, name)
if d == nil {
return NPM{}, false
}
v, ok := d.Versions[d.Latest]
if !ok {
return NPM{}, false
}
var l npmLatest
if json.Unmarshal(v.Manifest, &l) != nil {
return NPM{}, false
}
info := npmOf(l)
info.Version = d.Latest
return info, true
}
// distWanted is the package and version an npm spec asks for, when the
// dist branch could serve it: a @magpie-community package, at a version
// or "latest" (not a range or another tag).
func distWanted(spec string) (name, version string, ok bool) {
if IsPath(spec) || IsGit(spec) {
return "", "", false
}
name = Name(spec)
if !strings.HasPrefix(name, distScope) || !pkgName.MatchString(name) {
return "", "", false
}
version = strings.TrimPrefix(strings.TrimPrefix(spec, name), "@")
if version == "" {
version = "latest"
}
if version != "latest" && !exactVersion.MatchString(version) {
return "", "", false
}
return name, version, true
}
var exactVersion = regexp.MustCompile(`^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$`)
// installDist installs the package's version from the dist branch: its
// tarball fetched, checked against the index's sha512, kept in
// plugins/dist (bun.lock names it there, and a later bun add installs it
// again from it), and added with bun. installMu held.
func installDist(ctx context.Context, bun, name, version string) error {
d := distPkg(ctx, name)
if d == nil {
return fmt.Errorf("the community repo's dist branch has no %s, or can't be read", name)
}
if version == "latest" {
version = d.Latest
}
v, ok := d.Versions[version]
if !ok {
return fmt.Errorf("the community repo's dist branch has no %s@%s", name, version)
}
if !distFile.MatchString(v.File) {
return fmt.Errorf("the community repo's dist branch names %q for %s@%s, not a tarball of its own", v.File, name, version)
}
b, err := fetchJSONFaithful(ctx, distBase+"/"+v.File, 64<<20)
if err != nil {
return err
}
sum := sha512.Sum512(b)
if got := "sha512-" + base64.StdEncoding.EncodeToString(sum[:]); got != v.Integrity {
return fmt.Errorf("%s from the community repo's dist branch isn't the tarball its index names (sha512 %s, not %s)", v.File, got, v.Integrity)
}
dir := filepath.Join(Dir(), "dist")
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
file := path.Base(v.File)
tmp, err := os.CreateTemp(dir, file+".*")
if err != nil {
return err
}
_, werr := tmp.Write(b)
if cerr := tmp.Close(); werr == nil {
werr = cerr
}
if werr == nil {
werr = os.Rename(tmp.Name(), filepath.Join(dir, file))
}
if werr != nil {
os.Remove(tmp.Name())
return werr
}
return bunAdd(ctx, bun, "./dist/"+file)
}
// pruneDist takes out the tarballs in plugins/dist that plugins/package.json
// no longer names: one an update from npm, or a newer one, took the place
// of. installMu held. One it can't tell about (package.json unreadable)
// stays.
func pruneDist() {
b, err := os.ReadFile(filepath.Join(Dir(), "package.json"))
if err != nil {
return
}
var pj struct {
Dependencies map[string]string `json:"dependencies"`
}
if json.Unmarshal(b, &pj) != nil {
return
}
named := map[string]bool{}
for _, s := range pj.Dependencies {
s = strings.TrimPrefix(s, "file:")
if rest, ok := strings.CutPrefix(filepath.ToSlash(s), "./dist/"); ok {
named[rest] = true
} else if rest, ok := strings.CutPrefix(filepath.ToSlash(s), "dist/"); ok {
named[rest] = true
}
}
ents, err := os.ReadDir(filepath.Join(Dir(), "dist"))
if err != nil {
return
}
for _, e := range ents {
if !e.IsDir() && strings.HasSuffix(e.Name(), ".tgz") && !named[e.Name()] {
os.Remove(filepath.Join(Dir(), "dist", e.Name()))
}
}
}