Skip to content

Commit 18338eb

Browse files
committed
feat: add tarballs caching
1 parent d04603f commit 18338eb

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

cli/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010
// start a server
1111
var listCmd = &cobra.Command{
1212
Use: "list",
13-
Short: "List all cached packages",
13+
Short: "List all cached paths",
1414
Run: func(cmd *cobra.Command, args []string) {
1515
proxy := getProxy(func() (npmproxy.Options, error) {
1616
return npmproxy.Options{
1717
DatabasePrefix: persistentOptions.RedisPrefix,
1818
}, nil
1919
})
2020

21-
metadatas, err := proxy.ListMetadata()
21+
metadatas, err := proxy.ListCachedPaths()
2222
if err != nil {
2323
panic(err)
2424
}

cli/purge.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
// start a server
99
var purgeCmd = &cobra.Command{
1010
Use: "purge",
11-
Short: "Purge all cached packages",
11+
Short: "Purge all cached paths",
1212
Run: func(cmd *cobra.Command, args []string) {
1313
proxy := getProxy(func() (npmproxy.Options, error) {
1414
return npmproxy.Options{
1515
DatabasePrefix: persistentOptions.RedisPrefix,
1616
}, nil
1717
})
1818

19-
err := proxy.PurgeMetadata()
19+
err := proxy.PurgeCachedPaths()
2020
if err != nil {
2121
panic(err)
2222
}

proxy/cache.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import (
55
"io/ioutil"
66
"log"
77
"net/http"
8+
"regexp"
89
"strings"
910
)
1011

11-
// GetMetadata returns cached NPM response for a given package path.
12-
func (proxy Proxy) GetMetadata(name string, originalPath string, request *http.Request) ([]byte, error) {
12+
// GetCachedPath returns cached upstream response for a given url path.
13+
func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, error) {
1314
options, err := proxy.GetOptions()
1415
if err != nil {
1516
return nil, err
1617
}
1718

18-
key := options.DatabasePrefix + name
19+
key := options.DatabasePrefix + path
1920

2021
// get package from database
2122
pkg, err := proxy.Database.Get(key)
@@ -31,7 +32,7 @@ func (proxy Proxy) GetMetadata(name string, originalPath string, request *http.R
3132

3233
// error is caused by nonexistent package
3334
// fetch package
34-
req, err := http.NewRequest("GET", options.UpstreamAddress+originalPath, nil)
35+
req, err := http.NewRequest("GET", options.UpstreamAddress+path, nil)
3536

3637
req.Header = request.Header
3738
req.Header.Set("Accept-Encoding", "gzip")
@@ -66,15 +67,15 @@ func (proxy Proxy) GetMetadata(name string, originalPath string, request *http.R
6667
}
6768
}
6869

69-
// replace tarball urls
70-
// FIXME: unmarshall and replace only necessary fields
71-
// convertedPkg := strings.ReplaceAll(string(pkg), options.ReplaceAddress, options.StaticServerAddress)
70+
// TODO: avoid calling MustCompile every time
71+
// find "dist": "https?://.*/ and replace to "dist": "{localurl}/
72+
pkg = regexp.MustCompile(`(?U)"tarball":"https?://.*/`).ReplaceAllString(pkg, `"dist": "http://localhost:8080/`)
7273

7374
return []byte(pkg), nil
7475
}
7576

76-
// ListMetadata returns list of all cached packages
77-
func (proxy Proxy) ListMetadata() ([]string, error) {
77+
// ListCachedPaths returns list of all cached url paths.
78+
func (proxy Proxy) ListCachedPaths() ([]string, error) {
7879
options, err := proxy.GetOptions()
7980
if err != nil {
8081
return nil, err
@@ -93,8 +94,8 @@ func (proxy Proxy) ListMetadata() ([]string, error) {
9394
return deprefixedMetadata, nil
9495
}
9596

96-
// PurgeMetadata deletes all cached packages.
97-
func (proxy Proxy) PurgeMetadata() error {
97+
// PurgeCachedPaths deletes all cached url paths.
98+
func (proxy Proxy) PurgeCachedPaths() error {
9899
options, err := proxy.GetOptions()
99100
if err != nil {
100101
return err

proxy/server.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package proxy
22

33
import (
44
"net/http"
5+
"strings"
56
"time"
67

78
ginzap "github.com/gin-contrib/zap"
@@ -32,25 +33,30 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
3233
}
3334

3435
func (proxy Proxy) getPackageHandler(c *gin.Context) {
35-
var name string
36-
if c.Param("name") != "" {
37-
name = c.Param("scope") + "/" + c.Param("name")
36+
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)
37+
38+
if err != nil {
39+
c.AbortWithError(500, err)
3840
} else {
39-
name = c.Param("scope")
41+
// c.Header("Content-Encoding", "gzip")
42+
c.Data(200, "application/json", pkg)
4043
}
44+
}
4145

42-
pkg, err := proxy.GetMetadata(name, c.Request.URL.Path, c.Request)
46+
func (proxy Proxy) getTarballHabdler(c *gin.Context) {
47+
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)
4348

4449
if err != nil {
4550
c.AbortWithError(500, err)
4651
} else {
47-
// c.Header("Content-Encoding", "gzip")
4852
c.Data(200, "application/json", pkg)
4953
}
5054
}
5155

5256
func (proxy Proxy) noRouteHandler(c *gin.Context) {
53-
if c.Request.URL.Path == "/" {
57+
if strings.Contains(c.Request.URL.Path, ".tgz") {
58+
proxy.getTarballHabdler(c)
59+
} else if c.Request.URL.Path == "/" {
5460
err := proxy.Database.Health()
5561

5662
if err != nil {

0 commit comments

Comments
 (0)