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

Commit

Permalink
Merge pull request #816 from cnpm/feat-semver
Browse files Browse the repository at this point in the history
feat: support semver
  • Loading branch information
fengmk2 committed Feb 1, 2016
2 parents 933ae69 + 44976f4 commit 3a46b97
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 9 deletions.
4 changes: 4 additions & 0 deletions controllers/registry/package/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ module.exports = function* show() {
var name = this.params.name || this.params[0];
var tag = this.params.version || this.params[1];
var version = semver.valid(tag);
var range = semver.validRange(tag);
var mod;
if (version) {
mod = yield* packageService.getModule(name, version);
} else if (range) {
mod = yield* packageService.getModuleByRange(name, range);
} else {
mod = yield* packageService.getModuleByTag(name, tag);
}

if (mod) {
setDownloadURL(mod.package, this);
mod.package._cnpm_publish_time = mod.publish_time;
Expand Down
4 changes: 2 additions & 2 deletions routes/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ function routes(app) {

// module
// scope package: params: [$name]
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)$/, syncByInstall, listAllVersions);
app.get(/^\/(@[\w\-\.]+\/[^\/]+)$/, syncByInstall, listAllVersions);
// scope package: params: [$name, $version]
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/([\w\.\-]+)$/, syncByInstall, getOneVersion);
app.get(/^\/(@[\w\-\.]+\/[\w\-\.]+)\/([^\/]+)$/, syncByInstall, getOneVersion);

app.get('/:name', syncByInstall, listAllVersions);
app.get('/:name/:version', syncByInstall, getOneVersion);
Expand Down
13 changes: 13 additions & 0 deletions services/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Module dependencies.
*/

var semver = require('semver');
var models = require('../models');
var common = require('./common');
var Tag = models.Tag;
Expand Down Expand Up @@ -73,6 +74,18 @@ exports.getModuleByTag = function* (name, tag) {
return yield* exports.getModule(tag.name, tag.version);
};

exports.getModuleByRange = function* (name, range) {
var rows = yield* exports.listModulesByName(name);
var versionMap = {};
var versions = rows.map(function(row) {
versionMap[row.version] = row;
return row.version;
});

var version = semver.maxSatisfying(versions, range);
return versionMap[version];
};

exports.getLatestModule = function* (name) {
return yield* exports.getModuleByTag(name, 'latest');
};
Expand Down
7 changes: 3 additions & 4 deletions test/controllers/registry/package/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,17 @@ describe('controllers/registry/package/list.test.js', function () {

describe('unpublished', function () {
before(function (done) {
utils.sync('tfs', done);
utils.sync('moduletest1', done);
});

it('should show unpublished info', function (done) {
mm(config, 'syncModel', 'all');
request(app.listen())
.get('/tfs')
.get('/moduletest1')
.expect(404, function (err, res) {
should.not.exist(err);
var data = res.body;
data.time.unpublished.name.should.equal('fengmk2');
data.time.unpublished.description.should.equal('tfs');
data.time.unpublished.name.should.equal('dead_horse');
done();
});
});
Expand Down
51 changes: 49 additions & 2 deletions test/controllers/registry/package/show.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ describe('controllers/registry/package/show.test.js', function () {
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
.expect(201, function(err) {
should.not.exist(err);
pkg = utils.getPackage('@cnpmtest/testmodule-show', '1.1.0', utils.admin);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);
});
});

it('should return one version', function (done) {
Expand All @@ -45,6 +53,45 @@ describe('controllers/registry/package/show.test.js', function () {
});
});

it('should return max satisfied package with semver range', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-show/^1.0.0')
.expect(200, function (err, res) {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('1.1.0');
data.dist.tarball.should.containEql('/@cnpmtest/testmodule-show/download/@cnpmtest/testmodule-show-1.1.0.tgz');
done();
});
});

it('should return max satisfied package with complex semver range', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-show/>1.2.0 <=2 || 0.0.1')
.expect(200, function (err, res) {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('0.0.1');
data.dist.tarball.should.containEql('/@cnpmtest/testmodule-show/download/@cnpmtest/testmodule-show-0.0.1.tgz');
done();
});
});

it('should return max satisfied package with *', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-show/*')
.expect(200, function (err, res) {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('1.1.0');
data.dist.tarball.should.containEql('/@cnpmtest/testmodule-show/download/@cnpmtest/testmodule-show-1.1.0.tgz');
done();
});
});

it('should support jsonp', function (done) {
request(app.listen())
.get('/@cnpmtest/testmodule-show/0.0.1?callback=jsonp')
Expand All @@ -59,7 +106,7 @@ describe('controllers/registry/package/show.test.js', function () {
should.not.exist(err);
var data = res.body;
data.name.should.equal('@cnpmtest/testmodule-show');
data.version.should.equal('0.0.1');
data.version.should.equal('1.1.0');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/web/package/show.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('controllers/web/package/show.test.js', function () {
it('should display unpublished info', function (done) {
mm(config, 'syncModel', 'all');
request(app)
.get('/package/tfs')
.get('/package/moduletest1')
.expect(200)
.expect(/This package has been unpublished\./, done);
});
Expand Down
20 changes: 20 additions & 0 deletions test/services/package.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,26 @@ describe('test/services/package.test.js', function () {
});
});

describe('getModuleByRange()', function() {
it('should get undefined when not match semver range', function* () {
yield* createModule('test-getModuleByRange-module-0', '1.0.0');
yield* createModule('test-getModuleByRange-module-0', '1.1.0');
yield* createModule('test-getModuleByRange-module-0', '2.0.0');
var mod = yield* Package.getModuleByRange('test-getModuleByRange-module-0', '~2.1.0');
should.not.exist(mod);
});

it('should get package with semver range', function* () {
yield* createModule('test-getModuleByRange-module-1', '1.0.0');
yield* createModule('test-getModuleByRange-module-1', '1.1.0');
yield* createModule('test-getModuleByRange-module-1', '2.0.0');
var mod = yield* Package.getModuleByRange('test-getModuleByRange-module-1', '1');
mod.package.name.should.equal(mod.name);
mod.name.should.equal('test-getModuleByRange-module-1');
mod.version.should.equal('1.1.0');
});
});

describe('updateModulePackage()', function () {
it('should update not exists package return null', function* () {
var r = yield* Package.updateModulePackage(101010101, {});
Expand Down

0 comments on commit 3a46b97

Please sign in to comment.