feat(npm): add the virtual tarball handler

What this MR does

This MR adds npm.VirtualTarballHandler, the GET and HEAD handler for the tarball route of an npm virtual repository. For each read, the handler does these steps:

  1. It validates the target before any resolution: 400 for an unsafe file name, 404 file_not_found for an unparseable file name, 422 for an invalid package name.
  2. It parses the version from the file name, so that a version rule matches against it.
  3. It resolves over the upstream list and serves the bytes of the priority winner, with no re-point. A stored winner is served by proxy or by a 307 redirect, and a cold remote winner streams its upstream body.
  4. A hosted winner sends npm_files.created_at as Last-Modified, and Cache-Control follows the virtual repository.
  5. A winner whose stored blob is gone answers by its kind: 404 for a hosted winner, 503 for a remote winner (ruling E18).
  6. Every no-winner verdict answers through writeVirtualReadError.

The handler arms the write deadline two times, the same as in !3111 (merged). A failed arm increments gitlab_artifact_registry_npm_virtual_response_deadline_arm_failures_total and logs a Warn line that names the tarball route. Nothing wires the handler yet.

Interim behavior that part 4 replaces

Two paths are simpler in this part than in the final handler:

  • A body copy that fails after the 200 writes one Warn line. Part 4 adds the outcome booking per body source, the rationed deadline-kill line, and the abort of a chunked relay that ends short.
  • A cold HEAD whose drain fails answers through writeVirtualReadError directly. Part 4 sends the failure to ResolveDrainFailure (part 2), which can serve the cached copy of the winner.

Tests

virtual_tarball_test.go holds the main suite: winner selection, rules, no-winner verdicts, target rejection, HEAD parity, conditionals, Cache-Control, the delivery-mode override, the close of a cold stream body, and wiring faults. virtual_tarball_faults_test.go starts with the redirect, storage-fault, vanished-blob, and unservable-resolution cases. It also holds the write-deadline arm cases, the arm-failure counter case, and the client-disconnect 499 cases. Part 4 adds the rest of that file. virtual_tarball_internal_test.go tests two unexported paths: the deadline re-arm, and the 500 fallback of writeReadError. The rows map to the spec as the coverage table of !3111 (merged) states.

Size

This part has about 590 lines of production code and about 2,420 lines of tests, with about 790 lines of shared fixture. A further split does not help review: every test needs the fixture, and the handler has no smaller unit that serves a read.

Plan and spec amendments

This MR implements no plan step. It changes one row of the spec:

  • docs/specs/S31-npm-virtual.md, metrics table: the gitlab_artifact_registry_npm_virtual_response_deadline_arm_failures_total row now covers the packument, dist-tags, and tarball routes. Before this change, the row covered only the two metadata routes.

internal/format/npm/metrics.md (## Response-deadline and proxy series) has the same change.

Split of !3111 (merged)

This MR is one part of a split of !3111 (merged) into six stacked MRs. Each part targets the branch of the part before it, so each diff shows only that part. Merge the parts in order: 1, 2, 3, 4, 5, then !3111 (merged) itself as part 6.

Part MR Contents
1 !3150 (merged) npm_files.created_at to the hosted tarball winner
2 !3147 (merged) Resolver re-entry on a virtual drain failure
3 !3148 (merged) VirtualTarballHandler, not wired
4 !3149 (merged) Copy-fault accounting, kill log, cold HEAD drain failure
5 !3151 (merged) Usage events
6 !3111 (merged) Slot wiring, run recipe, docs

At the split, the six parts together were byte-identical to the original !3111 (merged) diff (same git patch-id --stable). This part received review fixes after the split, so that statement is true only for the parts as they were at the split. The spec coverage table, the rulings (E1 to E22), and the spec gaps stay in the description of !3111 (merged).

E2E scenario catalog and user documentation

This part adds no e2e scenario and changes no file in docs/user/. Nothing wires the virtual tarball handler before part 6, so the route still answers 501 and no client-visible behavior changes.

Related to #898 (closed)

🤖 Generated with Claude Code

Edited by Pawel Rozlach

Merge request reports

Loading
Loading