@@ -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
0 commit comments