From 319d0021b98263f109f0d8422cef2db577b01b88 Mon Sep 17 00:00:00 2001 From: Rogee Date: Sat, 15 Nov 2025 21:35:41 +0800 Subject: [PATCH] fix: npm pkg --- internal/hubmodule/interfaces.go | 15 ++++++++------ internal/hubmodule/npm/module.go | 2 +- .../hubmodule/{npm/locator.go => rewrite.go} | 20 +++++++++++-------- internal/proxy/handler.go | 18 +++++++++++++++-- 4 files changed, 38 insertions(+), 17 deletions(-) rename internal/hubmodule/{npm/locator.go => rewrite.go} (60%) diff --git a/internal/hubmodule/interfaces.go b/internal/hubmodule/interfaces.go index 8b97976..1d43b82 100644 --- a/internal/hubmodule/interfaces.go +++ b/internal/hubmodule/interfaces.go @@ -1,10 +1,6 @@ package hubmodule -import ( - "time" - - "github.com/any-hub/any-hub/internal/cache" -) +import "time" // MigrationState 描述模块上线阶段,方便观测端区分 legacy/beta/ga。 type MigrationState string @@ -48,5 +44,12 @@ func DefaultModuleKey() string { return defaultModuleKey } +// Locator 表示模块可用于重写缓存路径的轻量结构体,避免依赖 cache 包。 +type Locator struct { + HubName string + Path string + HubType string +} + // LocatorRewrite 允许模块根据自身协议调整缓存路径,例如将 npm metadata 写入独立文件。 -type LocatorRewrite func(cache.Locator) cache.Locator +type LocatorRewrite func(Locator) Locator diff --git a/internal/hubmodule/npm/module.go b/internal/hubmodule/npm/module.go index f5ef93f..7232b44 100644 --- a/internal/hubmodule/npm/module.go +++ b/internal/hubmodule/npm/module.go @@ -25,6 +25,6 @@ func init() { RequiresMetadataFile: false, SupportsStreamingWrite: true, }, - LocatorRewrite: rewriteLocator, + LocatorRewrite: hubmodule.DefaultLocatorRewrite("npm"), }) } diff --git a/internal/hubmodule/npm/locator.go b/internal/hubmodule/rewrite.go similarity index 60% rename from internal/hubmodule/npm/locator.go rename to internal/hubmodule/rewrite.go index cfc5e3e..9632f49 100644 --- a/internal/hubmodule/npm/locator.go +++ b/internal/hubmodule/rewrite.go @@ -1,14 +1,18 @@ -package npm +package hubmodule -import ( - "strings" +import "strings" - "github.com/any-hub/any-hub/internal/cache" -) +// DefaultLocatorRewrite 针对内置 hub 类型提供缓存路径重写逻辑。 +func DefaultLocatorRewrite(hubType string) LocatorRewrite { + switch hubType { + case "npm": + return rewriteNPMLocator + default: + return nil + } +} -// rewriteLocator 将 npm metadata JSON 落盘至 package.json,避免与 tarball -// 路径的 `/-/` 子目录冲突,同时保持 tarball 使用原始路径。 -func rewriteLocator(loc cache.Locator) cache.Locator { +func rewriteNPMLocator(loc Locator) Locator { path := loc.Path if path == "" { return loc diff --git a/internal/proxy/handler.go b/internal/proxy/handler.go index 6d5db93..0502242 100644 --- a/internal/proxy/handler.go +++ b/internal/proxy/handler.go @@ -20,6 +20,7 @@ import ( "github.com/sirupsen/logrus" "github.com/any-hub/any-hub/internal/cache" + "github.com/any-hub/any-hub/internal/hubmodule" "github.com/any-hub/any-hub/internal/logging" "github.com/any-hub/any-hub/internal/server" ) @@ -289,6 +290,7 @@ func (h *Handler) buildUpstreamRequest(c fiber.Ctx, upstream *url.URL, route *se requestHeaders := fiberHeadersAsHTTP(c) server.CopyHeaders(req.Header, requestHeaders) + req.Header.Del("Accept-Encoding") req.Host = upstream.Host req.Header.Set("Host", upstream.Host) req.Header.Set("X-Forwarded-Host", c.Hostname()) @@ -410,8 +412,20 @@ func buildLocator(route *server.HubRoute, c fiber.Ctx) cache.Locator { HubName: route.Config.Name, Path: clean, } - if route.Module.LocatorRewrite != nil { - loc = route.Module.LocatorRewrite(loc) + rewrite := route.Module.LocatorRewrite + if rewrite == nil { + rewrite = hubmodule.DefaultLocatorRewrite(route.Config.Type) + } + if rewrite != nil { + rewritten := rewrite(hubmodule.Locator{ + HubName: loc.HubName, + Path: loc.Path, + HubType: route.Config.Type, + }) + loc = cache.Locator{ + HubName: rewritten.HubName, + Path: rewritten.Path, + } } return loc }