This commit is contained in:
2025-11-17 15:39:44 +08:00
parent abfa51f12e
commit 1ddda89499
46 changed files with 2185 additions and 751 deletions

View File

@@ -0,0 +1,26 @@
package npm
import (
"strings"
"github.com/any-hub/any-hub/internal/proxy/hooks"
)
func init() {
hooks.MustRegister("npm", hooks.Hooks{
CachePolicy: cachePolicy,
})
}
func cachePolicy(_ *hooks.RequestContext, locatorPath string, current hooks.CachePolicy) hooks.CachePolicy {
if strings.Contains(locatorPath, "/-/") && strings.HasSuffix(locatorPath, ".tgz") {
current.AllowCache = true
current.AllowStore = true
current.RequireRevalidate = false
return current
}
current.AllowCache = true
current.AllowStore = true
current.RequireRevalidate = true
return current
}

View File

@@ -0,0 +1,22 @@
package npm
import (
"testing"
"github.com/any-hub/any-hub/internal/proxy/hooks"
)
func TestCachePolicyForTarball(t *testing.T) {
policy := cachePolicy(nil, "/pkg/-/pkg-1.0.0.tgz", hooks.CachePolicy{})
if policy.RequireRevalidate {
t.Fatalf("tarball should not require revalidate")
}
if !policy.AllowCache {
t.Fatalf("tarball should allow cache")
}
policy = cachePolicy(nil, "/pkg", hooks.CachePolicy{})
if !policy.RequireRevalidate {
t.Fatalf("metadata should require revalidate")
}
}