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

feat: support semver #816

Merged
merged 4 commits into from
Feb 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不需要修改吧。

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个正则范围会不会太广了。[\^\~\>\=\<\|]*[\w\.\-]+ 即可

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

呃,不对,semver 会很复杂。。。还是你改的这样吧。如 >=1.2.7 <1.3.0


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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yield

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

到处都是 yield* ,先保留,后续找时间重构的时候全部替换掉吧

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
22 changes: 22 additions & 0 deletions test/controllers/registry/package/show.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

var should = require('should');
var request = require('supertest');
var pedding = require('pedding');
var mm = require('mm');
var app = require('../../../../servers/registry');
var utils = require('../../../utils');
Expand All @@ -24,12 +25,20 @@ describe('controllers/registry/package/show.test.js', function () {
afterEach(mm.restore);

before(function (done) {
done = pedding(done, 2);
var pkg = utils.getPackage('@cnpmtest/testmodule-show', '0.0.1', utils.admin);
request(app.listen())
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.expect(201, done);

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 +54,19 @@ 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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一些复杂的情况 >=1.2.7 <1.3.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.2.7 || >=1.2.9 <2.0.0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试补了

.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 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