Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
feat: support nfs.url return multi urls (#1344)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jun 5, 2018
1 parent 0121de3 commit 533c27f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
28 changes: 24 additions & 4 deletions controllers/registry/package/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,37 @@ module.exports = function* download(next) {
// can not get dist
var url = null;

if (typeof nfs.url === 'function') {
var query = this.query || {};
// allow download from specific store bucket
var options = query.bucket ? { bucket: query.bucket } : null;
var query = this.query || {};
// allow download from specific store bucket
var options = query.bucket ? { bucket: query.bucket } : null;

if (typeof nfs.urls === 'function') {
if (is.generatorFunction(nfs.urls)) {
url = yield nfs.urls(common.getCDNKey(name, filename), options);
} else {
url = nfs.urls(common.getCDNKey(name, filename), options);
}
} else if (typeof nfs.url === 'function') {
if (is.generatorFunction(nfs.url)) {
url = yield nfs.url(common.getCDNKey(name, filename), options);
} else {
url = nfs.url(common.getCDNKey(name, filename), options);
}
}

if (url && Array.isArray(url)) {
var newUrl = url[0];
if (url.length > 1) {
var otherUrls = url.slice(1).join(',');
if (newUrl.indexOf('?') === -1) {
newUrl += '?other_urls=' + encodeURIComponent(otherUrls);
} else {
newUrl += '&other_urls=' + encodeURIComponent(otherUrls);
}
}
url = newUrl;
}

debug('download %s %s %s %s', name, filename, version, url);

if (!row || !row.package || !row.package.dist) {
Expand Down
32 changes: 32 additions & 0 deletions test/controllers/registry/package/download.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ describe('test/controllers/registry/package/download.test.js', function () {
.expect('Location', 'http://foo-us1.oss.com/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz')
.expect(302, done);
});

it('should download from multi urls', function(done) {
mm(config.nfs, 'urls', function(key, options) {
return [
'http://' + options.bucket + '.oss.com' + key,
'http://default.oss.com' + key,
'http://backup.oss.com' + key,
];
});
mm(config, 'downloadRedirectToNFS', true);

request(app)
.get('/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz?bucket=foo-us1')
.expect('Location', 'http://foo-us1.oss.com/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz?other_urls=http%3A%2F%2Fdefault.oss.com%2F%40cnpmtest%2Fdownload-test-module%2F-%2F%40cnpmtest%2Fdownload-test-module-1.0.0.tgz%2Chttp%3A%2F%2Fbackup.oss.com%2F%40cnpmtest%2Fdownload-test-module%2F-%2F%40cnpmtest%2Fdownload-test-module-1.0.0.tgz')
.expect(302, done);
});

it('should download from multi urls dont modifiy raw query', function(done) {
mm(config.nfs, 'urls', function(key, options) {
return [
'http://' + options.bucket + '.oss.com' + key + '?foo=bar',
'http://default.oss.com' + key,
'http://backup.oss.com' + key,
];
});
mm(config, 'downloadRedirectToNFS', true);

request(app)
.get('/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz?bucket=foo-us1')
.expect('Location', 'http://foo-us1.oss.com/@cnpmtest/download-test-module/-/@cnpmtest/download-test-module-1.0.0.tgz?foo=bar&other_urls=http%3A%2F%2Fdefault.oss.com%2F%40cnpmtest%2Fdownload-test-module%2F-%2F%40cnpmtest%2Fdownload-test-module-1.0.0.tgz%2Chttp%3A%2F%2Fbackup.oss.com%2F%40cnpmtest%2Fdownload-test-module%2F-%2F%40cnpmtest%2Fdownload-test-module-1.0.0.tgz')
.expect(302, done);
});
});
});
});

0 comments on commit 533c27f

Please sign in to comment.