Skip to content

Commit

Permalink
improving tags support, ref #8
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Dec 7, 2013
1 parent 08d1011 commit 2f45649
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 14 deletions.
65 changes: 52 additions & 13 deletions lib/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var async = require('async')
, mystreams = require('./streams')
, utils = require('./utils')
, transaction = require('./transaction')
, Logger = require('./logger')

//
// Implements Storage interface
Expand All @@ -24,6 +25,7 @@ function Storage(config) {
this.uplinks[p].upname = p
}
this.local = new Local(config)
this.logger = Logger.logger.child()

return this
}
Expand Down Expand Up @@ -422,28 +424,27 @@ Storage.prototype.get_package = function(name, callback) {
try {
utils.validate_metadata(up_res, name)
} catch(err) {
self.logger.error({
sub: 'out',
err: err,
}, 'package.json validating error @{!err.message}\n@{err.stack}')
return cb(null, [err])
}

result._uplinks[up.upname] = {
etag: etag
}

var this_version = up_res['dist-tags'].latest
if (latest == null
|| (!semver.gt(latest, this_version) && this_version)) {
latest = this_version
var is_latest = true
try {
Storage._merge_versions(result, up_res)
} catch(err) {
self.logger.error({
sub: 'out',
err: err,
}, 'package.json parsing error @{!err.message}\n@{err.stack}')
return cb(null, [err])
}

;['versions', 'dist-tags'].forEach(function(key) {
for (var i in up_res[key]) {
if (!result[key][i] || is_latest) {
result[key][i] = up_res[key][i]
}
}
})

// if we got to this point, assume that the correct package exists
// on the uplink
exists = true
Expand All @@ -465,11 +466,49 @@ Storage.prototype.get_package = function(name, callback) {
for (var i in result) {
if (!~whitelist.indexOf(i)) delete result[i]
}

result['dist-tags'].latest = Object.keys(result.versions).sort(semver.compare)
for (var i in result['dist-tags']) {
if (Array.isArray(result['dist-tags'][i])) {
result['dist-tags'][i] = result['dist-tags'][i][result['dist-tags'][i].length-1]
}
}

callback(null, result, uplink_errors)
})
})
})
}

// function gets a local info and an info from uplinks and tries to merge it
// exported for unit tests only
Storage._merge_versions = function(local, up) {
// copy new versions to a cache
// NOTE: if a certain version was updated, we can't refresh it reliably
for (var i in up.versions) {
if (local.versions[i] == null) {
local.versions[i] = up.versions[i]
}
}

// refresh dist-tags
for (var i in up['dist-tags']) {
if (i === 'latest') continue
switch(typeof(local['dist-tags'][i])) {
case 'string':
local['dist-tags'][i] = [local['dist-tags'][i]]
break
case 'object': // array
break
default:
local['dist-tags'][i] = []
}
if (local['dist-tags'][i].indexOf(up['dist-tags'][i]) === -1) {
local['dist-tags'][i].push(up['dist-tags'][i])
local['dist-tags'][i].sort(semver.compare)
}
}
}

module.exports = Storage

2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports.is_object = function(obj) {
}

module.exports.validate_metadata = function(object, name) {
assert(module.exports.is_object(object))
assert(module.exports.is_object(object), 'not a json object')
assert.equal(object.name, name)

if (!module.exports.is_object(object['dist-tags'])) {
Expand Down
43 changes: 43 additions & 0 deletions test/st_merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var assert = require('assert')
, merge = require('../lib/storage')._merge_versions

//require('../lib/logger').setup()

exports['Merge'] = {
'simple': function() {
var x = {
versions: {a:1,b:1,c:1},
'dist-tags': {},
}
merge(x, {versions: {a:2,q:2}})
assert.deepEqual(x, {
versions: {a:1,b:1,c:1,q:2},
'dist-tags': {},
})
},

'dist-tags - compat': function() {
var x = {
versions: {},
'dist-tags': {q:'1.1.1',w:['2.2.2']},
}
merge(x, {'dist-tags':{q:'2.2.2',w:'3.3.3',t:'4.4.4'}})
assert.deepEqual(x, {
versions: {},
'dist-tags': {q:['1.1.1','2.2.2'],w:['2.2.2','3.3.3'],t:['4.4.4']},
})
},

'dist-tags - sort': function() {
var x = {
versions: {},
'dist-tags': {w:['2.2.2','1.1.1','12.2.2','2.2.2-rc2']},
}
merge(x, {'dist-tags':{w:'3.3.3'}})
assert.deepEqual(x, {
versions: {},
'dist-tags': {w:["1.1.1","2.2.2-rc2","2.2.2","3.3.3","12.2.2"]},
})
},
}

0 comments on commit 2f45649

Please sign in to comment.