0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-03-18 02:22:46 -05:00

added couchdb-like revisions

This commit is contained in:
Alex Kocharin 2013-10-22 11:00:04 +04:00
parent 8b314040d9
commit 5622b2283d
2 changed files with 28 additions and 6 deletions

View file

@ -1,6 +1,7 @@
var fs = require('fs')
, semver = require('semver')
, Path = require('path')
, crypto = require('crypto')
, fs_storage = require('./local-fs')
, UError = require('./error').UserError
, utils = require('./utils')
@ -30,8 +31,8 @@ function get_boilerplate(name) {
'dist-tags': {},
// our own object
// type: "filename"->"metadata"
'_distfiles': {},
'_attachments': {},
};
}
@ -82,7 +83,8 @@ Storage.prototype._read_create_package = function(name, callback) {
return callback(self._internal_error(err, file, 'error reading'))
}
}
callback(null, data);
self._normalize_package(data)
callback(null, data)
});
}
@ -132,7 +134,7 @@ Storage.prototype.update_versions = function(name, newdata, callback) {
}
if (change) {
self.storage.write_json(name + '/' + info_file, data, callback);
self._write_package(name, data, callback)
} else {
callback();
}
@ -263,7 +265,8 @@ Storage.prototype.get_package = function(name, callback) {
return callback(self._internal_error(err, file, 'error reading'))
}
}
callback.apply(null, arguments)
self._normalize_package(result)
callback(err, result)
})
}
@ -318,13 +321,31 @@ Storage.prototype.update_package = function(name, updateFn, _callback) {
}
}
self._normalize_package(json)
updateFn(json, function(err) {
if (err) return callback(err)
self.storage.write_json(name + '/' + info_file, json, callback)
self._write_package(name, json, callback)
})
})
}
Storage.prototype._normalize_package = function(pkg) {
['versions', 'dist-tags', '_distfiles', '_attachments'].forEach(function(key) {
if (typeof(pkg[key]) != 'object' || pkg[key] == null) pkg[key] = {}
});
if (typeof(pkg._rev) !== 'string') pkg._rev = '0-0000000000000000'
}
Storage.prototype._write_package = function(name, json, callback) {
// calculate revision a la couchdb
if (typeof(json._rev) !== 'string') json._rev = '0-0000000000000000'
var rev = json._rev.split('-')
json._rev = ((+rev[0] || 0) + 1) + '-' + crypto.pseudoRandomBytes(16).toString('hex')
this.storage.write_json(name + '/' + info_file, json, callback)
}
module.exports = Storage;

View file

@ -342,7 +342,8 @@ Storage.prototype.get_package = function(name, callback) {
if (up === self.local) {
// file exists in local repo
exists = true;
exists = true
result._rev = up_res._rev
}
try {