Skip to content

Commit

Permalink
fix(npm): fix download package for scoped packages
Browse files Browse the repository at this point in the history
  • Loading branch information
rande committed Oct 24, 2017
1 parent 3cce7e6 commit b28601e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
18 changes: 11 additions & 7 deletions mirror/npm/npm_pat.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
"regexp"
"strings"

"goji.io"
"goji.io/pattern"
Expand All @@ -17,7 +18,7 @@ import (

func NewArchivePat(code string) goji.Pattern {
return &PackagePat{
Pattern: regexp.MustCompile(fmt.Sprintf(`\/npm\/%s\/([\w\d\.-]+)\/-\/(.*)\.(tgz)`, code)),
Pattern: regexp.MustCompile(fmt.Sprintf(`\/npm\/%s\/((@([\w\d.-]+)\/|)([@\w\d\.-]+))\/-\/(.*)\.(tgz)`, code)),
}
}

Expand All @@ -26,14 +27,17 @@ type PackagePat struct {
}

func (pp *PackagePat) Match(ctx context.Context, r *http.Request) context.Context {
if results := pp.Pattern.FindStringSubmatch(r.URL.Path); len(results) == 0 {
return nil
} else {
name := results[1]
version := results[2][len(name)+1 : len(results[2])]
var results []string

return &packagePatMatch{ctx, name, version, "tgz"}
if results = pp.Pattern.FindStringSubmatch(r.URL.Path); len(results) == 0 {
return nil
}

name := strings.Replace(results[1], "/", "%2f", -1)
version := results[5][(len(results[4]) + 1):]

return &packagePatMatch{ctx, name, version, "tgz"}

}

type packagePatMatch struct {
Expand Down
40 changes: 21 additions & 19 deletions mirror/npm/npm_pat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"golang.org/x/net/context"
)

type TestVersion struct {
Url string
Package string
Version string
}

func mustReq(method, path string) (context.Context, *http.Request) {
req, err := http.NewRequest(method, path, nil)
if err != nil {
Expand All @@ -25,30 +31,26 @@ func mustReq(method, path string) (context.Context, *http.Request) {
return ctx, req
}

func Test_Npm_Pat_Archive(t *testing.T) {
p := NewArchivePat("npm")

c, r := mustReq("GET", "/npm/npm/aspace/-/aspace-0.0.1.tgz")

result := p.Match(c, r)
func Test_Npm_Pat(t *testing.T) {

assert.NotNil(t, result)
assert.Equal(t, "aspace", result.Value(pattern.Variable("package")))
assert.Equal(t, "0.0.1", result.Value(pattern.Variable("version")))
assert.Equal(t, "tgz", result.Value(pattern.Variable("format")))
}
cases := []struct{ Url, Package, Version string }{
{"/npm/npm/aspace/-/aspace-0.0.1.tgz", "aspace", "0.0.1"},
{"/npm/npm/@type%2fnode/-/node-6.0.90.tgz", "@type%2fnode", "6.0.90"},
{"/npm/npm/dateformat/-/dateformat-1.0.2-1.2.3.tgz", "dateformat", "1.0.2-1.2.3"},
}

func Test_Npm_Pat_Archive_NonSemver(t *testing.T) {
p := NewArchivePat("npm")
matcher := NewArchivePat("npm")

c, r := mustReq("GET", "/npm/npm/dateformat/-/dateformat-1.0.2-1.2.3.tgz")
for _, p := range cases {
c, r := mustReq("GET", p.Url)

result := p.Match(c, r)
result := matcher.Match(c, r)

assert.NotNil(t, result)
assert.Equal(t, "dateformat", result.Value(pattern.Variable("package")))
assert.Equal(t, "1.0.2-1.2.3", result.Value(pattern.Variable("version")))
assert.Equal(t, "tgz", result.Value(pattern.Variable("format")))
assert.NotNil(t, result)
assert.Equal(t, p.Package, result.Value(pattern.Variable("package")))
assert.Equal(t, p.Version, result.Value(pattern.Variable("version")))
assert.Equal(t, "tgz", result.Value(pattern.Variable("format")))
}
}

func Test_Npm_Pat_AllVariables(t *testing.T) {
Expand Down

0 comments on commit b28601e

Please sign in to comment.