diff --git a/lib/local-storage.js b/lib/local-storage.js index 3b70ed7d2..230a42403 100644 --- a/lib/local-storage.js +++ b/lib/local-storage.js @@ -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; diff --git a/lib/storage.js b/lib/storage.js index 94df5b3f2..7ba9306a1 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -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 {