feat: add tarballs caching
This commit is contained in:
parent
d04603f529
commit
18338eb0d7
@ -10,7 +10,7 @@ import (
|
||||
// start a server
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all cached packages",
|
||||
Short: "List all cached paths",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
@ -18,7 +18,7 @@ var listCmd = &cobra.Command{
|
||||
}, nil
|
||||
})
|
||||
|
||||
metadatas, err := proxy.ListMetadata()
|
||||
metadatas, err := proxy.ListCachedPaths()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
// start a server
|
||||
var purgeCmd = &cobra.Command{
|
||||
Use: "purge",
|
||||
Short: "Purge all cached packages",
|
||||
Short: "Purge all cached paths",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
proxy := getProxy(func() (npmproxy.Options, error) {
|
||||
return npmproxy.Options{
|
||||
@ -16,7 +16,7 @@ var purgeCmd = &cobra.Command{
|
||||
}, nil
|
||||
})
|
||||
|
||||
err := proxy.PurgeMetadata()
|
||||
err := proxy.PurgeCachedPaths()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -5,17 +5,18 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetMetadata returns cached NPM response for a given package path.
|
||||
func (proxy Proxy) GetMetadata(name string, originalPath string, request *http.Request) ([]byte, error) {
|
||||
// GetCachedPath returns cached upstream response for a given url path.
|
||||
func (proxy Proxy) GetCachedPath(path string, request *http.Request) ([]byte, error) {
|
||||
options, err := proxy.GetOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
key := options.DatabasePrefix + name
|
||||
key := options.DatabasePrefix + path
|
||||
|
||||
// get package from database
|
||||
pkg, err := proxy.Database.Get(key)
|
||||
@ -31,7 +32,7 @@ func (proxy Proxy) GetMetadata(name string, originalPath string, request *http.R
|
||||
|
||||
// error is caused by nonexistent package
|
||||
// fetch package
|
||||
req, err := http.NewRequest("GET", options.UpstreamAddress+originalPath, nil)
|
||||
req, err := http.NewRequest("GET", options.UpstreamAddress+path, nil)
|
||||
|
||||
req.Header = request.Header
|
||||
req.Header.Set("Accept-Encoding", "gzip")
|
||||
@ -66,15 +67,15 @@ func (proxy Proxy) GetMetadata(name string, originalPath string, request *http.R
|
||||
}
|
||||
}
|
||||
|
||||
// replace tarball urls
|
||||
// FIXME: unmarshall and replace only necessary fields
|
||||
// convertedPkg := strings.ReplaceAll(string(pkg), options.ReplaceAddress, options.StaticServerAddress)
|
||||
// TODO: avoid calling MustCompile every time
|
||||
// find "dist": "https?://.*/ and replace to "dist": "{localurl}/
|
||||
pkg = regexp.MustCompile(`(?U)"tarball":"https?://.*/`).ReplaceAllString(pkg, `"dist": "http://localhost:8080/`)
|
||||
|
||||
return []byte(pkg), nil
|
||||
}
|
||||
|
||||
// ListMetadata returns list of all cached packages
|
||||
func (proxy Proxy) ListMetadata() ([]string, error) {
|
||||
// ListCachedPaths returns list of all cached url paths.
|
||||
func (proxy Proxy) ListCachedPaths() ([]string, error) {
|
||||
options, err := proxy.GetOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -93,8 +94,8 @@ func (proxy Proxy) ListMetadata() ([]string, error) {
|
||||
return deprefixedMetadata, nil
|
||||
}
|
||||
|
||||
// PurgeMetadata deletes all cached packages.
|
||||
func (proxy Proxy) PurgeMetadata() error {
|
||||
// PurgeCachedPaths deletes all cached url paths.
|
||||
func (proxy Proxy) PurgeCachedPaths() error {
|
||||
options, err := proxy.GetOptions()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -2,6 +2,7 @@ package proxy
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
ginzap "github.com/gin-contrib/zap"
|
||||
@ -32,14 +33,7 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
|
||||
}
|
||||
|
||||
func (proxy Proxy) getPackageHandler(c *gin.Context) {
|
||||
var name string
|
||||
if c.Param("name") != "" {
|
||||
name = c.Param("scope") + "/" + c.Param("name")
|
||||
} else {
|
||||
name = c.Param("scope")
|
||||
}
|
||||
|
||||
pkg, err := proxy.GetMetadata(name, c.Request.URL.Path, c.Request)
|
||||
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
@ -49,8 +43,20 @@ func (proxy Proxy) getPackageHandler(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (proxy Proxy) getTarballHabdler(c *gin.Context) {
|
||||
pkg, err := proxy.GetCachedPath(c.Request.URL.Path, c.Request)
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
} else {
|
||||
c.Data(200, "application/json", pkg)
|
||||
}
|
||||
}
|
||||
|
||||
func (proxy Proxy) noRouteHandler(c *gin.Context) {
|
||||
if c.Request.URL.Path == "/" {
|
||||
if strings.Contains(c.Request.URL.Path, ".tgz") {
|
||||
proxy.getTarballHabdler(c)
|
||||
} else if c.Request.URL.Path == "/" {
|
||||
err := proxy.Database.Health()
|
||||
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user