2013-10-18 15:46:13 -05:00
|
|
|
var fs = require('fs')
|
|
|
|
, semver = require('semver')
|
|
|
|
, Path = require('path')
|
|
|
|
, fs_storage = require('./local-fs')
|
|
|
|
, UError = require('./error').UserError
|
|
|
|
, utils = require('./utils')
|
|
|
|
, mystreams = require('./streams')
|
|
|
|
, Logger = require('./logger')
|
|
|
|
, info_file = 'package.json'
|
2013-06-07 20:16:28 -05:00
|
|
|
|
2013-09-25 04:12:33 -05:00
|
|
|
//
|
|
|
|
// Implements Storage interface
|
|
|
|
// (same for storage.js, local-storage.js, up-storage.js)
|
|
|
|
//
|
2013-06-13 09:21:14 -05:00
|
|
|
function Storage(config) {
|
2013-10-18 15:46:13 -05:00
|
|
|
if (!(this instanceof Storage)) return new Storage(config)
|
|
|
|
this.config = config
|
|
|
|
var path = Path.resolve(Path.dirname(this.config.self_path), this.config.storage)
|
|
|
|
this.storage = new fs_storage(path)
|
|
|
|
this.logger = Logger.logger.child({sub: 'fs'})
|
|
|
|
return this
|
2013-06-13 09:21:14 -05:00
|
|
|
}
|
|
|
|
|
2013-06-18 13:14:55 -05:00
|
|
|
// returns the minimal package file
|
|
|
|
function get_boilerplate(name) {
|
|
|
|
return {
|
2013-06-19 11:58:16 -05:00
|
|
|
// standard things
|
2013-06-18 13:14:55 -05:00
|
|
|
name: name,
|
|
|
|
versions: {},
|
|
|
|
'dist-tags': {},
|
2013-06-19 11:58:16 -05:00
|
|
|
|
|
|
|
// our own object
|
|
|
|
// type: "filename"->"metadata"
|
|
|
|
'_distfiles': {},
|
2013-06-18 13:14:55 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-10-18 16:17:53 -05:00
|
|
|
Storage.prototype._internal_error = function(err, file, msg) {
|
|
|
|
this.logger.error( {err: err, file: this.storage.path_to(file)}
|
|
|
|
, msg + ' @{file}: @{!err.message}'
|
|
|
|
)
|
|
|
|
return new UError({
|
|
|
|
status: 500,
|
|
|
|
msg: 'internal server error'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-06-13 09:21:14 -05:00
|
|
|
Storage.prototype.add_package = function(name, metadata, callback) {
|
2013-06-20 08:07:34 -05:00
|
|
|
this.storage.create_json(name + '/' + info_file, get_boilerplate(name), function(err) {
|
2013-06-07 20:16:28 -05:00
|
|
|
if (err && err.code === 'EEXISTS') {
|
|
|
|
return callback(new UError({
|
|
|
|
status: 409,
|
|
|
|
msg: 'this package is already present'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-06 03:27:50 -05:00
|
|
|
Storage.prototype.remove_package = function(name, callback) {
|
|
|
|
this.storage.unlink(name + '/' + info_file, function(err) {
|
|
|
|
if (err && err.code === 'ENOENT') {
|
|
|
|
return callback(new UError({
|
|
|
|
status: 404,
|
|
|
|
msg: 'no such package available',
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-18 13:14:55 -05:00
|
|
|
Storage.prototype._read_create_package = function(name, callback) {
|
2013-10-18 16:17:53 -05:00
|
|
|
var self = this
|
|
|
|
, file = name + '/' + info_file
|
|
|
|
self.storage.read_json(file, function(err, data) {
|
2013-06-07 20:16:28 -05:00
|
|
|
// TODO: race condition
|
2013-06-18 13:14:55 -05:00
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
// if package doesn't exist, we create it here
|
|
|
|
data = get_boilerplate(name);
|
|
|
|
} else {
|
2013-10-18 16:17:53 -05:00
|
|
|
return callback(self._internal_error(err, file, 'error reading'))
|
2013-06-18 13:14:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(null, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// synchronize remote package info with the local one
|
|
|
|
// TODO: readfile called twice
|
|
|
|
Storage.prototype.update_versions = function(name, newdata, callback) {
|
|
|
|
var self = this;
|
|
|
|
self._read_create_package(name, function(err, data) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
|
|
|
|
var change = false;
|
|
|
|
for (var ver in newdata.versions) {
|
|
|
|
if (data.versions[ver] == null) {
|
2013-06-19 11:58:16 -05:00
|
|
|
var verdata = newdata.versions[ver];
|
|
|
|
|
|
|
|
// why does anyone need to keep that in database?
|
|
|
|
delete verdata.readme;
|
|
|
|
|
2013-06-18 13:14:55 -05:00
|
|
|
change = true;
|
2013-06-19 11:58:16 -05:00
|
|
|
data.versions[ver] = verdata;
|
|
|
|
|
|
|
|
if (verdata.dist && verdata.dist.tarball) {
|
|
|
|
var url = utils.parse_tarball_url(
|
|
|
|
verdata.dist.__sinopia_orig_tarball || verdata.dist.tarball
|
|
|
|
);
|
|
|
|
|
|
|
|
// we do NOT overwrite any existing records
|
|
|
|
if (url != null && data._distfiles[url.filename] == null) {
|
|
|
|
data._distfiles[url.filename] = {
|
2013-06-20 08:41:07 -05:00
|
|
|
url: verdata.dist.__sinopia_orig_tarball || verdata.dist.tarball,
|
2013-06-19 11:58:16 -05:00
|
|
|
sha: verdata.dist.shasum,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2013-06-18 13:14:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var tag in newdata['dist-tags']) {
|
|
|
|
// if tag is updated to reference latter version, that's fine
|
|
|
|
var need_change =
|
|
|
|
(data['dist-tags'][tag] == null) ||
|
2013-06-21 19:19:46 -05:00
|
|
|
(!semver.gte(newdata['dist-tags'][tag], data['dist-tags'][tag]));
|
2013-06-18 13:14:55 -05:00
|
|
|
|
|
|
|
if (need_change) {
|
|
|
|
change = true;
|
|
|
|
data['dist-tags'][tag] = newdata['dist-tags'][tag];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change) {
|
|
|
|
self.storage.write_json(name + '/' + info_file, data, callback);
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage.prototype.add_version = function(name, version, metadata, tag, callback) {
|
|
|
|
var self = this;
|
|
|
|
self._read_create_package(name, function(err, data) {
|
2013-06-19 11:58:16 -05:00
|
|
|
// why does anyone need to keep that in database?
|
|
|
|
delete metadata.readme;
|
|
|
|
|
2013-06-07 20:16:28 -05:00
|
|
|
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;
|
2013-06-14 03:34:29 -05:00
|
|
|
self.storage.update_json(name + '/' + info_file, data, callback);
|
2013-06-07 20:16:28 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-20 08:07:34 -05:00
|
|
|
Storage.prototype.add_tarball = function(name, filename) {
|
2013-09-27 06:31:28 -05:00
|
|
|
var stream = new mystreams.UploadTarballStream();
|
2013-09-28 07:19:40 -05:00
|
|
|
var _transform = stream._transform;
|
|
|
|
var length = 0;
|
|
|
|
stream._transform = function(data) {
|
|
|
|
length += data.length;
|
|
|
|
_transform.apply(stream, arguments);
|
|
|
|
};
|
2013-06-20 08:07:34 -05:00
|
|
|
|
2013-06-14 03:34:29 -05:00
|
|
|
var self = this;
|
2013-09-27 06:31:28 -05:00
|
|
|
if (name === info_file || name === '__proto__') {
|
2013-06-20 08:07:34 -05:00
|
|
|
stream.emit('error', new UError({
|
2013-06-07 20:16:28 -05:00
|
|
|
status: 403,
|
|
|
|
msg: 'can\'t use this filename'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2013-06-20 08:07:34 -05:00
|
|
|
var wstream = this.storage.write_stream(name + '/' + filename);
|
|
|
|
|
|
|
|
wstream.on('error', function(err) {
|
|
|
|
if (err.code === 'EEXISTS') {
|
|
|
|
stream.emit('error', new UError({
|
|
|
|
status: 409,
|
|
|
|
msg: 'this tarball is already present'
|
|
|
|
}));
|
2013-09-28 07:19:40 -05:00
|
|
|
} else if (err.code === 'ENOENT') {
|
|
|
|
// check if package exists to throw an appropriate message
|
|
|
|
self.get_package(name, function(_err, res) {
|
|
|
|
if (_err) {
|
|
|
|
stream.emit('error', _err);
|
|
|
|
} else {
|
|
|
|
stream.emit('error', err);
|
|
|
|
}
|
|
|
|
});
|
2013-06-20 08:07:34 -05:00
|
|
|
} else {
|
|
|
|
stream.emit('error', err);
|
|
|
|
}
|
2013-06-07 20:16:28 -05:00
|
|
|
});
|
2013-06-20 08:07:34 -05:00
|
|
|
|
2013-09-27 03:56:13 -05:00
|
|
|
wstream.on('open', function() {
|
|
|
|
// re-emitting open because it's handled in storage.js
|
|
|
|
stream.emit('open');
|
|
|
|
});
|
2013-09-27 06:31:28 -05:00
|
|
|
wstream.on('success', function() {
|
|
|
|
// re-emitting open because it's handled in index.js
|
|
|
|
stream.emit('success');
|
2013-09-24 01:28:26 -05:00
|
|
|
});
|
2013-09-27 06:31:28 -05:00
|
|
|
stream.abort = function() {
|
|
|
|
wstream.abort();
|
|
|
|
};
|
|
|
|
stream.done = function() {
|
2013-09-28 07:19:40 -05:00
|
|
|
if (!length) {
|
|
|
|
stream.emit('error', new UError({
|
|
|
|
status: 422,
|
|
|
|
msg: 'refusing to accept zero-length file'
|
|
|
|
}));
|
|
|
|
wstream.abort();
|
|
|
|
} else {
|
|
|
|
wstream.done();
|
|
|
|
}
|
2013-09-27 06:31:28 -05:00
|
|
|
};
|
|
|
|
stream.pipe(wstream);
|
2013-09-24 01:28:26 -05:00
|
|
|
|
2013-06-20 08:07:34 -05:00
|
|
|
return stream;
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2013-06-13 09:21:14 -05:00
|
|
|
Storage.prototype.get_tarball = function(name, filename, callback) {
|
2013-09-27 03:56:13 -05:00
|
|
|
var stream = new mystreams.ReadTarballStream();
|
|
|
|
stream.abort = function() {
|
|
|
|
rstream.close();
|
|
|
|
};
|
2013-06-20 08:07:34 -05:00
|
|
|
|
|
|
|
var rstream = this.storage.read_stream(name + '/' + filename);
|
|
|
|
rstream.on('error', function(err) {
|
2013-06-07 20:16:28 -05:00
|
|
|
if (err && err.code === 'ENOENT') {
|
2013-06-20 08:07:34 -05:00
|
|
|
stream.emit('error', new UError({
|
2013-06-07 20:16:28 -05:00
|
|
|
status: 404,
|
2013-06-20 08:07:34 -05:00
|
|
|
msg: 'no such file available',
|
2013-06-07 20:16:28 -05:00
|
|
|
}));
|
2013-06-20 08:07:34 -05:00
|
|
|
} else {
|
|
|
|
stream.emit('error', err);
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
});
|
2013-06-20 08:07:34 -05:00
|
|
|
rstream.on('open', function() {
|
2013-09-27 03:56:13 -05:00
|
|
|
// re-emitting open because it's handled in storage.js
|
2013-06-20 08:07:34 -05:00
|
|
|
stream.emit('open');
|
|
|
|
rstream.pipe(stream);
|
|
|
|
});
|
|
|
|
return stream;
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2013-06-13 09:21:14 -05:00
|
|
|
Storage.prototype.get_package = function(name, callback) {
|
2013-10-18 15:46:13 -05:00
|
|
|
var self = this
|
|
|
|
, file = name + '/' + info_file
|
|
|
|
|
|
|
|
self.storage.read_json(file, function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return callback(new UError({
|
|
|
|
status: 404,
|
|
|
|
msg: 'no such package available'
|
|
|
|
}))
|
|
|
|
} else {
|
2013-10-18 16:17:53 -05:00
|
|
|
return callback(self._internal_error(err, file, 'error reading'))
|
2013-10-18 15:46:13 -05:00
|
|
|
}
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
2013-10-18 15:46:13 -05:00
|
|
|
callback.apply(null, arguments)
|
|
|
|
})
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2013-06-13 09:21:14 -05:00
|
|
|
module.exports = Storage;
|
|
|
|
|