Skip to content

Commit 01b4e72

Browse files
committed
Use abbreviated npm metadata when cooldown is disabled
Request application/vnd.npm.install-v1+json from the npm registry when cooldown filtering is not enabled. This format strips READMEs and other bulk data, reducing drizzle-orm metadata from 92MB to 4MB. Fall back to full metadata when cooldown is enabled since the abbreviated format lacks the time map needed for publish-date filtering.
1 parent 8b762ff commit 01b4e72

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

internal/handler/npm.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ func (h *NPMHandler) handlePackageMetadata(w http.ResponseWriter, r *http.Reques
7373
JSONError(w, http.StatusInternalServerError, "failed to create request")
7474
return
7575
}
76-
req.Header.Set("Accept", "application/json")
76+
// Use abbreviated metadata when cooldown is disabled — it's much smaller
77+
// (e.g. drizzle-orm: 4MB vs 92MB) but lacks the time map needed for cooldown.
78+
if h.proxy.Cooldown != nil && h.proxy.Cooldown.Enabled() {
79+
req.Header.Set("Accept", "application/json")
80+
} else {
81+
req.Header.Set("Accept", "application/vnd.npm.install-v1+json")
82+
}
7783

7884
resp, err := h.proxy.HTTPClient.Do(req)
7985
if err != nil {

internal/handler/npm_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,62 @@ func TestNPMRewriteMetadataCooldownExemptPackage(t *testing.T) {
293293
}
294294
}
295295

296+
func TestNPMHandlerUsesAbbreviatedMetadata(t *testing.T) {
297+
var gotAccept string
298+
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
299+
gotAccept = r.Header.Get("Accept")
300+
w.Header().Set("Content-Type", "application/json")
301+
_, _ = w.Write([]byte(`{
302+
"name": "testpkg",
303+
"versions": {
304+
"1.0.0": {
305+
"name": "testpkg",
306+
"version": "1.0.0",
307+
"dist": {
308+
"tarball": "https://registry.npmjs.org/testpkg/-/testpkg-1.0.0.tgz"
309+
}
310+
}
311+
}
312+
}`))
313+
}))
314+
defer upstream.Close()
315+
316+
t.Run("no cooldown uses abbreviated metadata", func(t *testing.T) {
317+
h := &NPMHandler{
318+
proxy: testProxy(),
319+
upstreamURL: upstream.URL,
320+
proxyURL: "http://proxy.local",
321+
}
322+
323+
req := httptest.NewRequest(http.MethodGet, "/testpkg", nil)
324+
w := httptest.NewRecorder()
325+
h.handlePackageMetadata(w, req)
326+
327+
if gotAccept != "application/vnd.npm.install-v1+json" {
328+
t.Errorf("Accept = %q, want abbreviated metadata header", gotAccept)
329+
}
330+
})
331+
332+
t.Run("cooldown enabled uses full metadata", func(t *testing.T) {
333+
proxy := testProxy()
334+
proxy.Cooldown = &cooldown.Config{Default: "3d"}
335+
336+
h := &NPMHandler{
337+
proxy: proxy,
338+
upstreamURL: upstream.URL,
339+
proxyURL: "http://proxy.local",
340+
}
341+
342+
req := httptest.NewRequest(http.MethodGet, "/testpkg", nil)
343+
w := httptest.NewRecorder()
344+
h.handlePackageMetadata(w, req)
345+
346+
if gotAccept == "application/vnd.npm.install-v1+json" {
347+
t.Error("cooldown enabled should use full metadata, not abbreviated")
348+
}
349+
})
350+
}
351+
296352
func TestNPMHandlerMetadataNotFound(t *testing.T) {
297353
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
298354
w.WriteHeader(http.StatusNotFound)

0 commit comments

Comments
 (0)