Skip to content

Commit

Permalink
fix: allow download without scope filename (cnpm#1665)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu authored Sep 19, 2021
1 parent 5e1c608 commit 2e7354c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions controllers/registry/package/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ let globalDownloads = new Map();
module.exports = function* download(next) {
var name = this.params.name || this.params[0];
var filename = this.params.filename || this.params[1];
// scope pkg and download with out scope
if (name.startsWith('@') && !filename.startsWith('@')) {
var scope = name.slice(0, name.indexOf('/'));
// fix filename with scope
filename = `${scope}/${filename}`;
}


var version = filename.slice(name.length + 1, -4);
// can not get dist
var url = null;
Expand Down
2 changes: 2 additions & 0 deletions routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ function routes(app) {

// need limit by ip
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/download\/(@[\w\-\.]+\/[\w\-\.]+)$/, limit, downloadPackage);
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/download\/([\w\-\.]+)$/, limit, downloadPackage);
app.get('/:name/download/:filename', limit, downloadPackage);
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/\-\/(@[\w\-\.]+\/[\w\-\.]+)$/, limit, downloadPackage);
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/\-\/([\w\-\.]+)$/, limit, downloadPackage);
app.get('/:name/-/:filename', limit, downloadPackage);

// delete tarball and remove one version
Expand Down
12 changes: 12 additions & 0 deletions test/controllers/registry/package/download.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,24 @@ describe('test/controllers/registry/package/download.test.js', function () {
.expect(200, done);
});

it('should download a file with 200', function (done) {
request(app)
.get('/@cnpmtest/download-test-module/download/download-test-module-1.0.0.tgz')
.expect(200, done);
});

it('should alias /:name/-/:filename to /:name/download/:filename', function (done) {
request(app)
.get('/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz')
.expect(200, done);
});

it('should alias /:name/-/:filename to /:name/download/:filename', function (done) {
request(app)
.get('/@cnpmtest/download-test-module/-/download-test-module-1.0.0.tgz')
.expect(200, done);
});

it('should 404 when package not exists', function (done) {
request(app)
.get('/@cnpmtest/download-test-module-not-exists/download/@cnpmtest/download-test-module-not-exists-1.0.0.tgz')
Expand Down

0 comments on commit 2e7354c

Please sign in to comment.