From bc56b97677d5e72928d2849dea6e4051c107a49d Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 24 Nov 2026 02:05:55 +0000 Subject: [PATCH] fix(api): answer 200 when analytics hold no record for a package Analytics live in memory and restart empty. A package with no record yet returned 404, which every browser logs as a failed request on the Downloads tab. The endpoint now returns a null package. --- pkg/app/handlers_analytics.go | 10 +++------- pkg/app/handlers_system_test.go | 6 ++++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/app/handlers_analytics.go b/pkg/app/handlers_analytics.go index e47e5b2..b3ed3df 100644 --- a/pkg/app/handlers_analytics.go +++ b/pkg/app/handlers_analytics.go @@ -97,13 +97,9 @@ func (a *App) handleAnalyticsPackageStats(c fiber.Ctx) error { }) } - stats, exists := a.analyticsEngine.GetPackageStats(registry, name) - if !exists { - return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ - "error": "package not found in analytics", - }) - } - + // Analytics live in memory and restart empty, so a package with no record yet is a normal + // answer, not a missing resource: a 404 here shows up as a failed request in every client. + stats, _ := a.analyticsEngine.GetPackageStats(registry, name) return c.Status(fiber.StatusOK).JSON(fiber.Map{ "package": stats, }) diff --git a/pkg/app/handlers_system_test.go b/pkg/app/handlers_system_test.go index ac5efac..1749910 100644 --- a/pkg/app/handlers_system_test.go +++ b/pkg/app/handlers_system_test.go @@ -265,7 +265,9 @@ func TestPackageAnalyticsAcceptsNamesWithSlashes(t *testing.T) { t.Errorf("analytics for %q = %s, want one download", name, body) } } - if code, _, _ := doRequest(t, a, "/api/analytics/package/npm/@types/missing", nil); code != http.StatusNotFound { - t.Errorf("unknown package = %d, want 404", code) + // A package with no record yet is a normal answer; a 404 would show as a failed request in the browser. + out, body := getJSON(t, a, "/api/analytics/package/npm/@types/missing") + if pkg, present := out["package"]; !present || pkg != nil { + t.Errorf("unknown package = %s, want 200 with a null package", body) } }