Skip to content

Commit c5eecb3

Browse files
authored
Merge pull request #33 from rande/fix_npm_download
fix(npm): fix regression with download package
2 parents 2b5ad37 + 28371bd commit c5eecb3

File tree

6 files changed

+26
-57
lines changed

6 files changed

+26
-57
lines changed

fixtures/mock/npm/-/all

Lines changed: 0 additions & 50 deletions
This file was deleted.
24.7 KB
Binary file not shown.

fixtures/mock/npm/@types/react/index.html

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

mirror/npm/npm.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,14 @@ func (ns *NpmService) WriteArchive(w io.Writer, pkg, version string) error {
396396
vaultKey := fmt.Sprintf("%s/%s", pkg, version)
397397

398398
if !ns.Vault.Has(vaultKey) {
399-
url := fmt.Sprintf("%s/%s/-/%s-%s.tgz", ns.Config.SourceServer, pkg, pkg, version)
399+
var url string
400+
401+
if pkg[0] == '@' { // scoped package
402+
subNames := strings.Split(pkg, "%2f")
403+
url = fmt.Sprintf("%s/%s/%s/-/%s-%s.tgz", ns.Config.SourceServer, subNames[0], subNames[1], subNames[1], version)
404+
} else {
405+
url = fmt.Sprintf("%s/%s/-/%s-%s.tgz", ns.Config.SourceServer, pkg, pkg, version)
406+
}
400407

401408
logger.WithField("url", url).Info("Create vault entry")
402409

mirror/npm/npm_pat_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func Test_Npm_Pat(t *testing.T) {
3737
{"/npm/npm/aspace/-/aspace-0.0.1.tgz", "aspace", "0.0.1"},
3838
{"/npm/npm/@type%2fnode/-/node-6.0.90.tgz", "@type%2fnode", "6.0.90"},
3939
{"/npm/npm/dateformat/-/dateformat-1.0.2-1.2.3.tgz", "dateformat", "1.0.2-1.2.3"},
40+
{"/npm/npm/@storybook%2freact/-/react-3.0.0.tgz", "@storybook%2freact", "3.0.0"},
4041
}
4142

4243
matcher := NewArchivePat("npm")

test/mirror/npm_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,22 @@ import (
1010
"fmt"
1111
"strings"
1212
"testing"
13-
"time"
1413

1514
"github.com/rande/pkgmirror/mirror/npm"
1615
"github.com/rande/pkgmirror/test"
1716
"github.com/stretchr/testify/assert"
1817
)
1918

20-
func Test_Npm_Get_Package(t *testing.T) {
19+
func Test_Npm_Get_Standard_Package(t *testing.T) {
2120

2221
optin := &test.TestOptin{Npm: true}
2322

2423
test.RunHttpTest(t, optin, func(args *test.Arguments) {
25-
res, err := test.RunRequest("GET", fmt.Sprintf("%s/npm/angular-oauth", args.MockedServer.URL))
24+
res, err := test.RunRequest("GET", fmt.Sprintf("%s/npm/angular-oauth", args.MockedServer.URL)) // Mocked server
2625

2726
assert.NoError(t, err)
2827
assert.Equal(t, 200, res.StatusCode)
2928

30-
// wait for the synchro to complete
31-
time.Sleep(1 * time.Second)
32-
3329
res, err = test.RunRequest("GET", fmt.Sprintf("%s/npm/npm/non-existant-package", args.TestServer.URL))
3430
assert.NoError(t, err)
3531
assert.Equal(t, 404, res.StatusCode)
@@ -54,3 +50,17 @@ func Test_Npm_Get_Package(t *testing.T) {
5450
assert.Equal(t, 19497, len(res.GetBody()))
5551
})
5652
}
53+
func Test_Npm_Download_Scoped_Package_Archive(t *testing.T) {
54+
55+
optin := &test.TestOptin{Npm: true}
56+
57+
test.RunHttpTest(t, optin, func(args *test.Arguments) {
58+
url := strings.Replace("http://localhost:8000/npm/npm/@types/react/-/react-0.0.0.tgz", "http://localhost:8000", args.TestServer.URL, -1)
59+
60+
res, err := test.RunRequest("GET", url)
61+
62+
assert.NoError(t, err)
63+
assert.Equal(t, 200, res.StatusCode)
64+
assert.Equal(t, 25276, len(res.GetBody()))
65+
})
66+
}

0 commit comments

Comments
 (0)