Skip to content

Commit 03f0ebd

Browse files
authored
Use shared archives browser for cached artifacts (#512)
* Use shared archives browser for cached artifacts * Pin archives to combined browser and Hex changes * Use archives v0.9.0 release * Return 404 when browsing archive directories as files
1 parent 8bebc56 commit 03f0ebd

5 files changed

Lines changed: 84 additions & 371 deletions

File tree

‎go.mod‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/Masterminds/semver/v3 v3.5.0
88
github.com/aws/aws-sdk-go-v2/config v1.33.7
99
github.com/aws/aws-sdk-go-v2/service/ecr v1.66.2
10-
github.com/git-pkgs/archives v0.8.1
10+
github.com/git-pkgs/archives v0.9.0
1111
github.com/git-pkgs/artifacts v0.2.1
1212
github.com/git-pkgs/cooldown v0.3.0
1313
github.com/git-pkgs/enrichment v0.7.2
@@ -106,7 +106,7 @@ require (
106106
github.com/rogpeppe/go-internal v1.16.0 // indirect
107107
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529 // indirect
108108
github.com/rubyist/circuitbreaker v2.2.1+incompatible // indirect
109-
github.com/ulikunitz/xz v0.5.16 // indirect
109+
github.com/ulikunitz/xz v0.5.17 // indirect
110110
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
111111
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.69.0 // indirect
112112
go.opentelemetry.io/otel v1.45.0 // indirect

‎go.sum‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a h1:yDWHCSQ40h88yi
117117
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a/go.mod h1:7Ga40egUymuWXxAe151lTNnCv97MddSOVsjpPPkityA=
118118
github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc=
119119
github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE=
120-
github.com/git-pkgs/archives v0.8.1 h1:IcT1bVsAec8SqJ4m9vTnD644KiadQZ4ERrWnm/30t3o=
121-
github.com/git-pkgs/archives v0.8.1/go.mod h1:gIyPXPQr5e+YrMqA5KQ7x6pLrFXHNF1+CjdedNJdGUU=
120+
github.com/git-pkgs/archives v0.9.0 h1:j5+GtZLrcsefIG5QxAQ2dcdCFf57uizcn03PyGRFA3U=
121+
github.com/git-pkgs/archives v0.9.0/go.mod h1:GJ/h7NPTUYiPinOUxpYNsHFCUikYZcJXvugBvP1VGNc=
122122
github.com/git-pkgs/artifacts v0.2.1 h1:VwdxR4yTDaqBZ34h0slxQBVyr2Xfa+QGOKCKviMx2xk=
123123
github.com/git-pkgs/artifacts v0.2.1/go.mod h1:Otosgq52pXT5UNN7lh6s/lszpWKVDO8XrOjN1M70/IA=
124124
github.com/git-pkgs/cooldown v0.3.0 h1:eAl2CQFwqXxluoVP9hHmropAf1VoV3gSxLMkN+NEu8A=
@@ -273,8 +273,8 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD
273273
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
274274
github.com/swaggo/swag v1.16.6 h1:qBNcx53ZaX+M5dxVyTrgQ0PJ/ACK+NzhwcbieTt+9yI=
275275
github.com/swaggo/swag v1.16.6/go.mod h1:ngP2etMK5a0P3QBizic5MEwpRmluJZPHjXcMoj4Xesg=
276-
github.com/ulikunitz/xz v0.5.16 h1:ld6NyySjx5lowVKwJvMRLnW5nxKX/xnpSiFYZ/Lxur0=
277-
github.com/ulikunitz/xz v0.5.16/go.mod h1:H9Rt/W6/Qj27PGauhQc6nfCDy7vHpzsOThBSaYDoEhw=
276+
github.com/ulikunitz/xz v0.5.17 h1:flR0y/x1hgM8EGV1AW3Xll6T413G0glV8UfBwR617V4=
277+
github.com/ulikunitz/xz v0.5.17/go.mod h1:H9Rt/W6/Qj27PGauhQc6nfCDy7vHpzsOThBSaYDoEhw=
278278
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
279279
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
280280
go.opentelemetry.io/contrib/detectors/gcp v1.44.0 h1:NmLfL734pJhM0JKaYd2Y28+nY9dPRWYAAbxhRCrKXPw=

‎internal/server/browse.go‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"io/fs"
910
"net/http"
1011
"path"
1112
"strings"
@@ -98,20 +99,14 @@ func detectSingleRootDir(reader archives.Reader) string {
9899
return ""
99100
}
100101

101-
var root rootDetector
102-
for _, f := range files {
103-
if !root.add(f.Path) {
104-
break
105-
}
106-
}
107-
return root.prefix()
102+
return archives.CommonRoot(files)
108103
}
109104

110105
// openArchive opens a cached artifact as a random-access archive reader,
111106
// auto-detecting and stripping a single top-level directory prefix (like
112107
// GitHub zipballs). For npm, the hardcoded "package/" prefix takes precedence.
113108
// The whole artifact is buffered, so only the version diff uses it; listing
114-
// and file reads stream through browseArchive instead.
109+
// and file reads use the streaming browser instead.
115110
func openArchive(filename string, content io.Reader, ecosystem string) (archives.Reader, error) { //nolint:ireturn // wraps multiple archive implementations
116111
data, err := readBrowseInput(content, maxBrowseArchiveSize)
117112
if err != nil {
@@ -142,7 +137,7 @@ func (s *Server) browseLimitsOrDefault() archives.StreamOptions {
142137

143138
// openBrowseArchive prepares a cached artifact for streaming. content is the
144139
// already open storage reader; later passes reopen the artifact.
145-
func (s *Server) openBrowseArchive(r *http.Request, artifact *database.Artifact, content io.Reader, ecosystem string) (*browseArchive, error) {
140+
func (s *Server) openBrowseArchive(r *http.Request, artifact *database.Artifact, content io.Reader, ecosystem string) (*archives.Browser, error) {
146141
storagePath := artifact.StoragePath.String
147142
reopen := func() (io.ReadCloser, error) {
148143
return s.storage.Open(r.Context(), storagePath)
@@ -398,13 +393,9 @@ func (s *Server) browseFile(w http.ResponseWriter, r *http.Request, ecosystem, n
398393
// Find the file and stream it straight from the archive
399394
fileReader, err := archive.Extract(filePath)
400395
switch {
401-
case errors.Is(err, errBrowseNotFound):
396+
case errors.Is(err, fs.ErrNotExist), errors.Is(err, archives.ErrIsDir):
402397
notFound(w, "file not found")
403398
return
404-
case errors.Is(err, errBrowseIsDir):
405-
s.logger.Error("failed to extract file", "error", err, "path", filePath)
406-
internalError(w, "failed to extract file")
407-
return
408399
case err != nil:
409400
s.browseArchiveError(w, err, cachedArtifact.Filename)
410401
return

0 commit comments

Comments
 (0)