Skip to content

Commit b28601e

Browse files
committed
fix(npm): fix download package for scoped packages
1 parent 3cce7e6 commit b28601e

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

mirror/npm/npm_pat.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net/http"
1111
"regexp"
12+
"strings"
1213

1314
"goji.io"
1415
"goji.io/pattern"
@@ -17,7 +18,7 @@ import (
1718

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

@@ -26,14 +27,17 @@ type PackagePat struct {
2627
}
2728

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

35-
return &packagePatMatch{ctx, name, version, "tgz"}
32+
if results = pp.Pattern.FindStringSubmatch(r.URL.Path); len(results) == 0 {
33+
return nil
3634
}
35+
36+
name := strings.Replace(results[1], "/", "%2f", -1)
37+
version := results[5][(len(results[4]) + 1):]
38+
39+
return &packagePatMatch{ctx, name, version, "tgz"}
40+
3741
}
3842

3943
type packagePatMatch struct {

mirror/npm/npm_pat_test.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import (
1515
"golang.org/x/net/context"
1616
)
1717

18+
type TestVersion struct {
19+
Url string
20+
Package string
21+
Version string
22+
}
23+
1824
func mustReq(method, path string) (context.Context, *http.Request) {
1925
req, err := http.NewRequest(method, path, nil)
2026
if err != nil {
@@ -25,30 +31,26 @@ func mustReq(method, path string) (context.Context, *http.Request) {
2531
return ctx, req
2632
}
2733

28-
func Test_Npm_Pat_Archive(t *testing.T) {
29-
p := NewArchivePat("npm")
30-
31-
c, r := mustReq("GET", "/npm/npm/aspace/-/aspace-0.0.1.tgz")
32-
33-
result := p.Match(c, r)
34+
func Test_Npm_Pat(t *testing.T) {
3435

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

41-
func Test_Npm_Pat_Archive_NonSemver(t *testing.T) {
42-
p := NewArchivePat("npm")
42+
matcher := NewArchivePat("npm")
4343

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

46-
result := p.Match(c, r)
47+
result := matcher.Match(c, r)
4748

48-
assert.NotNil(t, result)
49-
assert.Equal(t, "dateformat", result.Value(pattern.Variable("package")))
50-
assert.Equal(t, "1.0.2-1.2.3", result.Value(pattern.Variable("version")))
51-
assert.Equal(t, "tgz", result.Value(pattern.Variable("format")))
49+
assert.NotNil(t, result)
50+
assert.Equal(t, p.Package, result.Value(pattern.Variable("package")))
51+
assert.Equal(t, p.Version, result.Value(pattern.Variable("version")))
52+
assert.Equal(t, "tgz", result.Value(pattern.Variable("format")))
53+
}
5254
}
5355

5456
func Test_Npm_Pat_AllVariables(t *testing.T) {

0 commit comments

Comments
 (0)