fix go cache

This commit is contained in:
2025-11-15 22:05:39 +08:00
parent 319d0021b9
commit efd7737765

View File

@@ -1,25 +1,30 @@
package hubmodule package hubmodule
import "strings" import (
"path"
"strings"
)
// DefaultLocatorRewrite 针对内置 hub 类型提供缓存路径重写逻辑。 // DefaultLocatorRewrite 根据 Hub 类型返回通用的路径重写逻辑。
func DefaultLocatorRewrite(hubType string) LocatorRewrite { func DefaultLocatorRewrite(hubType string) LocatorRewrite {
switch hubType { switch hubType {
case "npm": case "npm":
return rewriteNPMLocator return rewriteNPMLocator
case "go":
return rewriteGoLocator
default: default:
return nil return nil
} }
} }
func rewriteNPMLocator(loc Locator) Locator { func rewriteNPMLocator(loc Locator) Locator {
path := loc.Path pathVal := loc.Path
if path == "" { if pathVal == "" {
return loc return loc
} }
var qsSuffix string var qsSuffix string
core := path core := pathVal
if idx := strings.Index(core, "/__qs/"); idx >= 0 { if idx := strings.Index(core, "/__qs/"); idx >= 0 {
qsSuffix = core[idx:] qsSuffix = core[idx:]
core = core[:idx] core = core[:idx]
@@ -43,3 +48,23 @@ func rewriteNPMLocator(loc Locator) Locator {
loc.Path = clean + "/package.json" + qsSuffix loc.Path = clean + "/package.json" + qsSuffix
return loc return loc
} }
func rewriteGoLocator(loc Locator) Locator {
if loc.Path == "" {
loc.Path = "/"
return loc
}
clean := path.Clean("/" + loc.Path)
if strings.HasPrefix(clean, "/sumdb/") {
loc.Path = clean
return loc
}
if strings.HasSuffix(clean, "/") {
clean = strings.TrimSuffix(clean, "/")
if clean == "" {
clean = "/"
}
}
loc.Path = clean
return loc
}