0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/lib/st-local.js

125 lines
3 KiB
JavaScript
Raw Normal View History

2013-06-07 20:16:28 -05:00
var storage = wrap(require('./drivers/fs'));
var UError = require('./error').UserError;
var info_file = 'package.json';
2013-06-13 09:21:14 -05:00
var fs = require('fs');
2013-06-07 20:16:28 -05:00
function wrap(driver) {
if (typeof(driver.create_json) !== 'function') {
driver.create_json = function(name, value, cb) {
driver.create(name, JSON.stringify(value), cb);
};
}
if (typeof(driver.update_json) !== 'function') {
driver.update_json = function(name, value, cb) {
driver.update(name, JSON.stringify(value), cb);
};
}
if (typeof(driver.read_json) !== 'function') {
driver.read_json = function(name, cb) {
driver.read(name, function(err, res) {
if (err) return cb(err);
cb(null, JSON.parse(res));
});
};
}
return driver;
}
2013-06-13 09:21:14 -05:00
function Storage(config) {
if (!(this instanceof Storage)) return new Storage(config);
this.config = config;
try {
fs.mkdirSync(config.storage);
console.log('created new packages directory: ', config.storage);
} catch(err) {
if (err.code !== 'EEXIST') throw new Error(err);
}
return this;
}
Storage.prototype.add_package = function(name, metadata, callback) {
2013-06-07 20:16:28 -05:00
storage.create_json(name + '/' + info_file, metadata, function(err) {
if (err && err.code === 'EEXISTS') {
return callback(new UError({
status: 409,
msg: 'this package is already present'
}));
}
callback();
});
}
2013-06-13 09:21:14 -05:00
Storage.prototype.add_version = function(name, version, metadata, tag, callback) {
2013-06-07 20:16:28 -05:00
storage.read_json(name + '/' + info_file, function(err, data) {
// TODO: race condition
if (err) return callback(err);
if (data.versions[version] != null) {
return callback(new UError({
status: 409,
msg: 'this version already present'
}));
}
data.versions[version] = metadata;
data['dist-tags'][tag] = version;
storage.update_json(name + '/' + info_file, data, callback);
});
}
2013-06-13 09:21:14 -05:00
Storage.prototype.add_tarball = function(name, filename, stream, callback) {
2013-06-07 20:16:28 -05:00
if (name === info_file) {
return callback(new UError({
status: 403,
msg: 'can\'t use this filename'
}));
}
var data = new Buffer(0);
stream.on('data', function(d) {
var tmp = data;
data = new Buffer(tmp.length+d.length);
tmp.copy(data, 0);
d.copy(data, tmp.length);
});
stream.on('end', function(d) {
storage.create(name + '/' + filename, data, function(err) {
if (err && err.code === 'EEXISTS') {
return callback(new UError({
status: 409,
msg: 'this tarball is already present'
}));
}
callback.apply(null, arguments);
});
});
}
2013-06-13 09:21:14 -05:00
Storage.prototype.get_tarball = function(name, filename, callback) {
2013-06-07 20:16:28 -05:00
storage.read(name + '/' + filename, function(err) {
if (err && err.code === 'ENOENT') {
return callback(new UError({
status: 404,
msg: 'no such package available'
}));
}
callback.apply(null, arguments);
});
}
2013-06-13 09:21:14 -05:00
Storage.prototype.get_package = function(name, callback) {
2013-06-07 20:16:28 -05:00
storage.read_json(name + '/' + info_file, function(err) {
if (err && err.code === 'ENOENT') {
return callback(new UError({
status: 404,
msg: 'no such package available'
}));
}
callback.apply(null, arguments);
});
}
2013-06-13 09:21:14 -05:00
module.exports = Storage;