Repository navigation
Extract portable PyPI metadata transformations - #732
Conversation
Move the simple API rewrites (HTML and JSON), the JSON API rewrite, the release-time parser and the filename parsers out of internal/handler into internal/metadata/pypi. The package takes its inputs explicitly (proxy and download URLs, cooldown policy, evaluation time, a denylist check) and reports what the JSON API rewrite holds back through callbacks the handler logs and records. It imports no handler, database, HTTP or logging code, and builds for wasip1. Cooldown is checked with Evaluate at one EvaluatedAt per rewrite, as in internal/metadata/npm.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Part of #501, the PyPI step you asked for there. Builds on #722, which changes the same PyPI filters; until that merges this PR shows its commits too, and the PyPI change is the last commit.
As in #612 for npm, the PyPI metadata transformations move out of
internal/handlerintointernal/metadata/pypi:RewriteSimpleHTML,RewriteDownloadHrefsandFilterSimpleHTMLLinksfor the simple API's HTML, andRewriteSimpleJSONfor PEP 691 JSONRewriteJSONfor the JSON API, package and version documents, with the denylist and cooldown it applies itselfReleaseTimes, which reads each release's newest upload timeFilenameIdentityand the wheel, sdist, egg and Windows installer parsers, withMetadataSuffixThe package takes its inputs explicitly in
Options: proxy and download URLs, the cooldown policy, the time cooldown is evaluated at, and a denylist check. It reports what the JSON API rewrite leaves out throughOnFilteredandOnWithheld, which the handler logs and records as before. It doesn't depend onhandler.Proxy.What stays in the handler: fetching, the rewrite cache, choosing which versions the simple index filters (the JSON API lookup and
versionInCooldown), downloads, and the streaming href rewrite for the root index (replacingReader). The handler's method names stay as thin wrappers, so the handler tests run unchanged against the real routes.Other changes:
handler.PyPIFilenameIdentityis gone;internal/mirrorcallspypi.FilenameIdentity.handler.PyPIMetadataSuffixstays, set frompypi.MetadataSuffix, forinternal/server.TestPyPIHandlerRewritesConfiguredDownloadUpstreamdoes, still works.Evaluateat oneEvaluatedAtper rewrite instead ofIsAllowedper release, as ininternal/metadata/npm. The handler passestime.Now(), so results are the same unless a rewrite runs across a cooldown boundary.Portability:
GOOS=wasip1 GOARCH=wasm go build ./internal/metadata/pypiworks.imports_test.gofails on any non-standard import outside an allowlist (cooldown, purl, vers, packageurl-go,internal/packageurl, andgolang.org/x/net/htmlfor the link filter) and onnet,net/http,log/slogordatabase/sql.Benchmarks (
internal/handler, #722's head vs this branch, test binaries run alternately, n=8, medians):Bytes and allocations per op are unchanged within a few allocations.
New tests in
internal/metadata/pypicover the JSON API rewrite with cooldown evaluated atEvaluatedAtby a release's newest file, the denylist, both callbacks, a disabled policy and no policy, the simple index in HTML and JSON, release times and filename identity. They were mutation-checked.go test ./...andgolangci-lint run ./...pass.Summary by CodeRabbit