From e522347667f77ba0d94127703c85520838d7da82 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Sat, 18 Jan 2014 22:57:44 +0400 Subject: [PATCH] making loose semver versions work, ref #38 --- lib/index.js | 9 +++++---- lib/utils.js | 16 ++++++++++++++++ test/functional/fixtures/tags.json | 12 ++++++------ test/functional/tags.js | 11 ++++++++++- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lib/index.js b/lib/index.js index 03e13bc0c..2ca2b016d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -116,19 +116,20 @@ module.exports = function(config_hash) { info = utils.filter_tarball_urls(info, req, config) var version = req.params.version + , t if (!version) { return res.send(info) } - if (info.versions[version] != null) { - return res.send(info.versions[version]) + if ((t = utils.get_version(info, version)) != null) { + return res.send(t) } if (info['dist-tags'] != null) { if (info['dist-tags'][version] != null) { version = info['dist-tags'][version] - if (info.versions[version] != null) { - return res.send(info.versions[version]) + if ((t = utils.get_version(info, version)) != null) { + return res.send(t) } } } diff --git a/lib/utils.js b/lib/utils.js index 729ff5ea3..29ed90dfc 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -107,6 +107,22 @@ module.exports.tag_version = function(data, version, tag) { } } +// gets version from a package object taking into account semver weirdness +module.exports.get_version = function(object, version) { + if (object.versions[version] != null) return object.versions[version] + + try { + version = semver.parse(version, true) + for (var k in object.versions) { + if (version.compare(semver.parse(k, true)) === 0) { + return object.versions[k] + } + } + } catch(err) { + return undefined + } +} + // function filters out bad semver versions and sorts the array module.exports.semver_sort = function semver_sort(array) { return array diff --git a/test/functional/fixtures/tags.json b/test/functional/fixtures/tags.json index 08b2322d9..aff3a9689 100644 --- a/test/functional/fixtures/tags.json +++ b/test/functional/fixtures/tags.json @@ -3,7 +3,7 @@ "versions": { "0.1.0": { "name": "testexp_tags", - "version": "0.0.0", + "version": "0.1.0", "dist": { "shasum": "fake", "tarball": "http://localhost:55551/testexp_tags/-/blahblah" @@ -11,7 +11,7 @@ }, "0.1.1alpha": { "name": "testexp_tags", - "version": "0.0.0", + "version": "0.1.1alpha", "dist": { "shasum": "fake", "tarball": "http://localhost:55551/testexp_tags/-/blahblah" @@ -19,7 +19,7 @@ }, "0.1.2": { "name": "testexp_tags", - "version": "0.0.0", + "version": "0.1.2", "dist": { "shasum": "fake", "tarball": "http://localhost:55551/testexp_tags/-/blahblah" @@ -27,7 +27,7 @@ }, "0.1.3alpha": { "name": "testexp_tags", - "version": "0.0.0", + "version": "0.1.3alpha", "dist": { "shasum": "fake", "tarball": "http://localhost:55551/testexp_tags/-/blahblah" @@ -35,7 +35,7 @@ }, "1.1": { "name": "testexp_tags", - "version": "0.0.0", + "version": "1.1", "dist": { "shasum": "fake", "tarball": "http://localhost:55551/testexp_tags/-/blahblah" @@ -47,4 +47,4 @@ "something": "0.1.1alpha", "bad": "1.1" } -} +} diff --git a/test/functional/tags.js b/test/functional/tags.js index 7c0ebe8f2..53d93dbfc 100644 --- a/test/functional/tags.js +++ b/test/functional/tags.js @@ -28,7 +28,6 @@ module.exports = function() { it('fetching package again', function(cb) { server.get_package('testexp_tags', function(res, body) { - // shouldn't exist yet assert.equal(res.statusCode, 200) assert.equal(typeof(body.versions['1.1']), 'object') assert.equal(body['dist-tags'].something, '0.1.1alpha') @@ -37,5 +36,15 @@ module.exports = function() { cb() }) }) + + ;['0.1.1alpha', '0.1.1-alpha', '0000.00001.001-alpha'].forEach(function(ver) { + it('fetching '+ver, function(cb) { + server.request({uri:'/testexp_tags/'+ver}, function(err, res, body) { + assert.equal(res.statusCode, 200) + assert.equal(body.version, '0.1.1alpha') + cb() + }) + }) + }) }) }