2013-10-18 22:46:13 +02:00
|
|
|
var fs = require('fs')
|
|
|
|
, Path = require('path')
|
2013-10-22 09:00:04 +02:00
|
|
|
, crypto = require('crypto')
|
2013-12-15 21:54:29 +01:00
|
|
|
, assert = require('assert')
|
2014-01-13 17:48:51 +01:00
|
|
|
, fs_storage = require('./local-fs')
|
2014-09-10 19:55:26 +02:00
|
|
|
, Error = require('http-errors')
|
2013-10-18 22:46:13 +02:00
|
|
|
, utils = require('./utils')
|
|
|
|
, mystreams = require('./streams')
|
|
|
|
, Logger = require('./logger')
|
|
|
|
, info_file = 'package.json'
|
2014-05-08 18:13:39 +02:00
|
|
|
, targz = require('tar.gz')
|
|
|
|
, search = require('./search');
|
2013-06-08 03:16:28 +02:00
|
|
|
|
2013-09-25 11:12:33 +02:00
|
|
|
//
|
|
|
|
// Implements Storage interface
|
|
|
|
// (same for storage.js, local-storage.js, up-storage.js)
|
|
|
|
//
|
2013-06-13 16:21:14 +02:00
|
|
|
function Storage(config) {
|
2013-10-18 22:46:13 +02:00
|
|
|
if (!(this instanceof Storage)) return new Storage(config)
|
|
|
|
this.config = config
|
|
|
|
this.logger = Logger.logger.child({sub: 'fs'})
|
|
|
|
return this
|
2013-06-13 16:21:14 +02:00
|
|
|
}
|
|
|
|
|
2013-06-18 20:14:55 +02:00
|
|
|
// returns the minimal package file
|
|
|
|
function get_boilerplate(name) {
|
|
|
|
return {
|
2013-06-19 18:58:16 +02:00
|
|
|
// standard things
|
2013-06-18 20:14:55 +02:00
|
|
|
name: name,
|
|
|
|
versions: {},
|
|
|
|
'dist-tags': {},
|
2013-06-19 18:58:16 +02:00
|
|
|
|
|
|
|
// our own object
|
|
|
|
'_distfiles': {},
|
2013-10-22 09:00:04 +02:00
|
|
|
'_attachments': {},
|
2013-10-22 11:31:48 +02:00
|
|
|
'_uplinks': {},
|
2013-10-26 14:18:36 +02:00
|
|
|
}
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 21:31:01 +02:00
|
|
|
Storage.prototype._internal_error = function(err, file, message) {
|
2014-01-13 17:48:51 +01:00
|
|
|
this.logger.error( {err: err, file: file}
|
2014-07-22 21:31:01 +02:00
|
|
|
, message + ' @{file}: @{!err.message}'
|
2013-10-18 23:17:53 +02:00
|
|
|
)
|
2014-09-10 19:55:26 +02:00
|
|
|
return Error[500]()
|
2013-10-18 23:17:53 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 23:34:16 +02:00
|
|
|
Storage.prototype.add_package = function(name, info, callback) {
|
2014-09-06 22:50:34 +02:00
|
|
|
var self = this
|
2014-08-08 03:08:41 +02:00
|
|
|
var storage = this.storage(name)
|
2014-09-10 19:55:26 +02:00
|
|
|
if (!storage) return callback(Error[404]('this package cannot be added'))
|
2014-09-06 22:50:34 +02:00
|
|
|
|
2014-08-08 03:08:41 +02:00
|
|
|
storage.create_json(info_file, get_boilerplate(name), function(err) {
|
2013-06-08 03:16:28 +02:00
|
|
|
if (err && err.code === 'EEXISTS') {
|
2014-09-10 19:55:26 +02:00
|
|
|
return callback(Error[409]('this package is already present'))
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
2014-05-07 00:40:21 +02:00
|
|
|
|
2014-08-08 03:58:25 +02:00
|
|
|
var latest = info['dist-tags'].latest
|
|
|
|
if (latest && info.versions[latest]) {
|
|
|
|
search.add(info.versions[latest])
|
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
callback()
|
|
|
|
})
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
|
|
|
|
2013-10-06 10:27:50 +02:00
|
|
|
Storage.prototype.remove_package = function(name, callback) {
|
2013-10-22 09:53:59 +02:00
|
|
|
var self = this
|
2013-10-23 08:15:17 +02:00
|
|
|
self.logger.info({name: name}, 'unpublishing @{name} (all)')
|
2014-08-08 03:08:41 +02:00
|
|
|
|
|
|
|
var storage = self.storage(name)
|
2014-09-10 19:55:26 +02:00
|
|
|
if (!storage) return callback(Error[404]('no such package available'))
|
|
|
|
|
2014-08-08 03:08:41 +02:00
|
|
|
storage.read_json(info_file, function(err, data) {
|
2013-10-22 15:33:39 +02:00
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
2014-09-10 19:55:26 +02:00
|
|
|
return callback(Error[404]('no such package available'))
|
2013-10-22 15:33:39 +02:00
|
|
|
} else {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2013-10-06 10:27:50 +02:00
|
|
|
}
|
2013-10-22 15:33:39 +02:00
|
|
|
self._normalize_package(data)
|
|
|
|
|
2014-08-08 03:08:41 +02:00
|
|
|
storage.unlink(info_file, function(err) {
|
2013-10-22 15:33:39 +02:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
var files = Object.keys(data._attachments)
|
|
|
|
|
|
|
|
function unlinkNext(cb) {
|
|
|
|
if (files.length === 0) return cb()
|
|
|
|
|
|
|
|
var file = files.shift()
|
2014-08-08 03:08:41 +02:00
|
|
|
storage.unlink(file, function() {
|
2013-10-22 15:33:39 +02:00
|
|
|
unlinkNext(cb)
|
|
|
|
})
|
|
|
|
}
|
2013-10-22 09:53:59 +02:00
|
|
|
|
2013-10-22 15:33:39 +02:00
|
|
|
unlinkNext(function() {
|
|
|
|
// try to unlink the directory, but ignore errors because it can fail
|
2014-08-08 03:08:41 +02:00
|
|
|
storage.rmdir('.', function(err) {
|
2014-04-14 02:44:17 +02:00
|
|
|
callback(err)
|
2014-05-07 00:40:21 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-05-08 23:48:15 +02:00
|
|
|
|
|
|
|
search.remove(name);
|
2014-09-06 22:50:34 +02:00
|
|
|
this.config.localList.remove(name);
|
2013-10-06 10:27:50 +02:00
|
|
|
}
|
|
|
|
|
2013-06-18 20:14:55 +02:00
|
|
|
Storage.prototype._read_create_package = function(name, callback) {
|
2013-10-18 23:17:53 +02:00
|
|
|
var self = this
|
2014-08-08 03:08:41 +02:00
|
|
|
var storage = self.storage(name)
|
|
|
|
if (!storage) {
|
|
|
|
var data = get_boilerplate(name)
|
|
|
|
self._normalize_package(data)
|
|
|
|
return callback(null, data)
|
|
|
|
}
|
|
|
|
storage.read_json(info_file, function(err, data) {
|
2013-06-08 03:16:28 +02:00
|
|
|
// TODO: race condition
|
2013-06-18 20:14:55 +02:00
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
// if package doesn't exist, we create it here
|
2013-10-26 14:18:36 +02:00
|
|
|
data = get_boilerplate(name)
|
2013-06-18 20:14:55 +02:00
|
|
|
} else {
|
2014-01-13 17:48:51 +01:00
|
|
|
return callback(self._internal_error(err, info_file, 'error reading'))
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-22 09:00:04 +02:00
|
|
|
self._normalize_package(data)
|
|
|
|
callback(null, data)
|
2013-10-26 14:18:36 +02:00
|
|
|
})
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// synchronize remote package info with the local one
|
|
|
|
// TODO: readfile called twice
|
|
|
|
Storage.prototype.update_versions = function(name, newdata, callback) {
|
2013-10-22 11:31:48 +02:00
|
|
|
var self = this
|
2013-06-18 20:14:55 +02:00
|
|
|
self._read_create_package(name, function(err, data) {
|
2013-10-22 11:31:48 +02:00
|
|
|
if (err) return callback(err)
|
2013-06-18 20:14:55 +02:00
|
|
|
|
2013-10-22 11:31:48 +02:00
|
|
|
var change = false
|
2013-06-18 20:14:55 +02:00
|
|
|
for (var ver in newdata.versions) {
|
|
|
|
if (data.versions[ver] == null) {
|
2013-10-22 11:31:48 +02:00
|
|
|
var verdata = newdata.versions[ver]
|
2013-06-19 18:58:16 +02:00
|
|
|
|
|
|
|
// why does anyone need to keep that in database?
|
2013-10-22 11:31:48 +02:00
|
|
|
delete verdata.readme
|
2013-06-19 18:58:16 +02:00
|
|
|
|
2013-10-22 11:31:48 +02:00
|
|
|
change = true
|
|
|
|
data.versions[ver] = verdata
|
2013-10-26 14:18:36 +02:00
|
|
|
|
2013-06-19 18:58:16 +02:00
|
|
|
if (verdata.dist && verdata.dist.tarball) {
|
|
|
|
var url = utils.parse_tarball_url(
|
2014-04-13 04:04:48 +02:00
|
|
|
/*verdata.dist.__sinopia_orig_tarball ||*/ verdata.dist.tarball
|
2013-10-22 11:31:48 +02:00
|
|
|
)
|
2013-06-19 18:58:16 +02:00
|
|
|
|
|
|
|
// we do NOT overwrite any existing records
|
|
|
|
if (url != null && data._distfiles[url.filename] == null) {
|
|
|
|
data._distfiles[url.filename] = {
|
2014-04-13 04:04:48 +02:00
|
|
|
url: /*verdata.dist.__sinopia_orig_tarball ||*/ verdata.dist.tarball,
|
2013-06-19 18:58:16 +02:00
|
|
|
sha: verdata.dist.shasum,
|
2013-10-22 11:31:48 +02:00
|
|
|
}
|
2013-06-19 18:58:16 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var tag in newdata['dist-tags']) {
|
2013-12-10 10:55:35 +01:00
|
|
|
if (!Array.isArray(data['dist-tags'][tag]) || data['dist-tags'][tag].length != newdata['dist-tags'][tag].length) {
|
|
|
|
// backward compat
|
|
|
|
var need_change = true
|
|
|
|
} else {
|
|
|
|
for (var i=0; i<data['dist-tags'][tag].length; i++) {
|
|
|
|
if (data['dist-tags'][tag][i] != newdata['dist-tags'][tag][i]) {
|
|
|
|
var need_change = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-18 20:14:55 +02:00
|
|
|
|
|
|
|
if (need_change) {
|
2013-10-22 11:31:48 +02:00
|
|
|
change = true
|
|
|
|
data['dist-tags'][tag] = newdata['dist-tags'][tag]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var up in newdata._uplinks) {
|
2013-12-29 01:58:48 +01:00
|
|
|
var need_change =
|
2014-03-02 10:47:09 +01:00
|
|
|
!utils.is_object(data._uplinks[up]) || (newdata._uplinks[up].etag !== data._uplinks[up].etag || (newdata._uplinks[up].fetched !== data._uplinks[up].fetched))
|
2013-10-22 11:31:48 +02:00
|
|
|
|
|
|
|
if (need_change) {
|
|
|
|
change = true
|
|
|
|
data._uplinks[up] = newdata._uplinks[up]
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
|
2013-06-18 20:14:55 +02:00
|
|
|
if (change) {
|
2013-10-22 11:31:48 +02:00
|
|
|
self.logger.debug('updating package info')
|
2013-12-15 00:15:58 +01:00
|
|
|
self._write_package(name, data, function(err) {
|
|
|
|
callback(err, data)
|
|
|
|
})
|
2013-06-18 20:14:55 +02:00
|
|
|
} else {
|
2013-12-15 00:15:58 +01:00
|
|
|
callback(null, data)
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
2013-10-22 11:31:48 +02:00
|
|
|
})
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Storage.prototype.add_version = function(name, version, metadata, tag, callback) {
|
2013-10-22 07:10:25 +02:00
|
|
|
var self = this
|
|
|
|
self.update_package(name, function updater(data, cb) {
|
2013-06-19 18:58:16 +02:00
|
|
|
// why does anyone need to keep that in database?
|
2013-10-22 07:10:25 +02:00
|
|
|
delete metadata.readme
|
2013-06-08 03:16:28 +02:00
|
|
|
|
|
|
|
if (data.versions[version] != null) {
|
2014-09-10 19:55:26 +02:00
|
|
|
return cb(Error[409]('this version already present'))
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
2013-10-22 09:29:57 +02:00
|
|
|
|
2013-10-22 09:45:19 +02:00
|
|
|
// if uploaded tarball has a different shasum, it's very likely that we have some kind of error
|
|
|
|
if (utils.is_object(metadata.dist) && typeof(metadata.dist.tarball) === 'string') {
|
|
|
|
var tarball = metadata.dist.tarball.replace(/.*\//, '')
|
|
|
|
if (utils.is_object(data._attachments[tarball])) {
|
|
|
|
if (data._attachments[tarball].shasum != null && metadata.dist.shasum != null) {
|
|
|
|
if (data._attachments[tarball].shasum != metadata.dist.shasum) {
|
2014-09-10 19:55:26 +02:00
|
|
|
return cb(Error[400]('shasum error, '
|
|
|
|
+ data._attachments[tarball].shasum
|
|
|
|
+ ' != ' + metadata.dist.shasum))
|
2013-10-22 09:45:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data._attachments[tarball].version = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 07:10:25 +02:00
|
|
|
data.versions[version] = metadata
|
2014-03-29 03:31:34 +01:00
|
|
|
utils.tag_version(data, version, tag, self.config)
|
2014-09-06 22:50:34 +02:00
|
|
|
self.config.localList.add(name)
|
2013-10-22 07:10:25 +02:00
|
|
|
cb()
|
|
|
|
}, callback)
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
|
|
|
|
2014-04-13 04:04:48 +02:00
|
|
|
Storage.prototype.add_tags = function(name, tags, callback) {
|
2013-12-27 14:06:30 +01:00
|
|
|
var self = this
|
2013-12-29 07:41:31 +01:00
|
|
|
|
2013-12-27 14:06:30 +01:00
|
|
|
self.update_package(name, function updater(data, cb) {
|
2014-04-13 04:04:48 +02:00
|
|
|
for (var t in tags) {
|
|
|
|
if (data.versions[tags[t]] == null) {
|
2014-09-10 19:55:26 +02:00
|
|
|
return cb(Error[404]("this version doesn't exist"))
|
2014-04-13 04:04:48 +02:00
|
|
|
}
|
2013-12-27 14:06:30 +01:00
|
|
|
|
2014-04-13 04:04:48 +02:00
|
|
|
utils.tag_version(data, tags[t], t, self.config)
|
|
|
|
}
|
2014-09-06 23:20:28 +02:00
|
|
|
cb()
|
2013-12-27 14:06:30 +01:00
|
|
|
}, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
// change package info to tag a specific version
|
|
|
|
function _add_tag(data, version, tag) {
|
|
|
|
data['dist-tags'][tag] = version
|
|
|
|
}
|
|
|
|
|
2013-10-23 08:15:17 +02:00
|
|
|
// currently supports unpublishing only
|
|
|
|
Storage.prototype.change_package = function(name, metadata, revision, callback) {
|
|
|
|
var self = this
|
|
|
|
|
|
|
|
if (!utils.is_object(metadata.versions) || !utils.is_object(metadata['dist-tags'])) {
|
2014-09-10 19:55:26 +02:00
|
|
|
return callback(Error[422]('bad data'))
|
2013-10-23 08:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self.update_package(name, function updater(data, cb) {
|
|
|
|
for (var ver in data.versions) {
|
|
|
|
if (metadata.versions[ver] == null) {
|
|
|
|
self.logger.info({name: name, version: ver}, 'unpublishing @{name}@@{version}')
|
|
|
|
delete data.versions[ver]
|
|
|
|
|
|
|
|
for (var file in data._attachments) {
|
|
|
|
if (data._attachments[file].version === ver) {
|
|
|
|
delete data._attachments[file].version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data['dist-tags'] = metadata['dist-tags']
|
|
|
|
cb()
|
|
|
|
}, function(err) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage.prototype.remove_tarball = function(name, filename, revision, callback) {
|
|
|
|
var self = this
|
2013-12-15 21:54:29 +01:00
|
|
|
assert(utils.validate_name(filename))
|
2013-10-23 08:15:17 +02:00
|
|
|
|
|
|
|
self.update_package(name, function updater(data, cb) {
|
|
|
|
if (data._attachments[filename]) {
|
|
|
|
delete data._attachments[filename]
|
|
|
|
cb()
|
|
|
|
} else {
|
2014-09-10 19:55:26 +02:00
|
|
|
cb(Error[404]('no such file available'))
|
2013-10-23 08:15:17 +02:00
|
|
|
}
|
|
|
|
}, function(err) {
|
|
|
|
if (err) return callback(err)
|
2014-08-08 03:08:41 +02:00
|
|
|
var storage = self.storage(name)
|
|
|
|
if (storage) storage.unlink(filename, callback)
|
2013-10-23 08:15:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-06-20 15:07:34 +02:00
|
|
|
Storage.prototype.add_tarball = function(name, filename) {
|
2013-12-15 21:54:29 +01:00
|
|
|
assert(utils.validate_name(filename))
|
|
|
|
|
2013-10-22 09:12:06 +02:00
|
|
|
var stream = new mystreams.UploadTarballStream()
|
|
|
|
, _transform = stream._transform
|
|
|
|
, length = 0
|
|
|
|
, shasum = crypto.createHash('sha1')
|
|
|
|
|
2014-08-08 03:08:41 +02:00
|
|
|
stream.abort = stream.done = function(){}
|
|
|
|
|
2013-09-28 14:19:40 +02:00
|
|
|
stream._transform = function(data) {
|
2013-10-22 09:12:06 +02:00
|
|
|
shasum.update(data)
|
|
|
|
length += data.length
|
|
|
|
_transform.apply(stream, arguments)
|
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
|
2013-10-22 09:12:06 +02:00
|
|
|
var self = this
|
2013-09-27 13:31:28 +02:00
|
|
|
if (name === info_file || name === '__proto__') {
|
2014-08-08 03:08:41 +02:00
|
|
|
process.nextTick(function() {
|
2014-09-10 19:55:26 +02:00
|
|
|
stream.emit('error', Error[403]("can't use this filename"))
|
2014-08-08 03:08:41 +02:00
|
|
|
})
|
|
|
|
return stream
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 03:08:41 +02:00
|
|
|
var storage = self.storage(name)
|
|
|
|
if (!storage) {
|
|
|
|
process.nextTick(function() {
|
2014-09-10 19:55:26 +02:00
|
|
|
stream.emit('error', Error[404]("can't upload this package"))
|
2014-08-08 03:08:41 +02:00
|
|
|
})
|
|
|
|
return stream
|
|
|
|
}
|
|
|
|
|
|
|
|
var wstream = storage.write_stream(filename)
|
2013-06-20 15:07:34 +02:00
|
|
|
|
|
|
|
wstream.on('error', function(err) {
|
|
|
|
if (err.code === 'EEXISTS') {
|
2014-09-10 19:55:26 +02:00
|
|
|
stream.emit('error', Error[409]('this tarball is already present'))
|
2013-09-28 14:19:40 +02:00
|
|
|
} else if (err.code === 'ENOENT') {
|
|
|
|
// check if package exists to throw an appropriate message
|
|
|
|
self.get_package(name, function(_err, res) {
|
|
|
|
if (_err) {
|
2013-10-26 14:18:36 +02:00
|
|
|
stream.emit('error', _err)
|
2013-09-28 14:19:40 +02:00
|
|
|
} else {
|
2013-10-26 14:18:36 +02:00
|
|
|
stream.emit('error', err)
|
2013-09-28 14:19:40 +02:00
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
})
|
2013-06-20 15:07:34 +02:00
|
|
|
} else {
|
2013-10-26 14:18:36 +02:00
|
|
|
stream.emit('error', err)
|
2013-06-20 15:07:34 +02:00
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
})
|
2013-06-20 15:07:34 +02:00
|
|
|
|
2013-09-27 10:56:13 +02:00
|
|
|
wstream.on('open', function() {
|
|
|
|
// re-emitting open because it's handled in storage.js
|
2013-10-26 14:18:36 +02:00
|
|
|
stream.emit('open')
|
2013-10-22 09:12:06 +02:00
|
|
|
})
|
2013-09-27 13:31:28 +02:00
|
|
|
wstream.on('success', function() {
|
2013-10-22 09:12:06 +02:00
|
|
|
self.update_package(name, function updater(data, cb) {
|
|
|
|
data._attachments[filename] = {
|
|
|
|
shasum: shasum.digest('hex'),
|
|
|
|
}
|
|
|
|
cb()
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
stream.emit('error', err)
|
|
|
|
} else {
|
|
|
|
stream.emit('success')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2013-09-27 13:31:28 +02:00
|
|
|
stream.abort = function() {
|
2013-10-22 09:12:06 +02:00
|
|
|
wstream.abort()
|
|
|
|
}
|
2013-09-27 13:31:28 +02:00
|
|
|
stream.done = function() {
|
2013-09-28 14:19:40 +02:00
|
|
|
if (!length) {
|
2014-09-10 19:55:26 +02:00
|
|
|
stream.emit('error', Error[422]('refusing to accept zero-length file'))
|
2013-10-26 14:18:36 +02:00
|
|
|
wstream.abort()
|
2013-09-28 14:19:40 +02:00
|
|
|
} else {
|
2013-10-26 14:18:36 +02:00
|
|
|
wstream.done()
|
2013-09-28 14:19:40 +02:00
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
}
|
|
|
|
stream.pipe(wstream)
|
2013-09-24 08:28:26 +02:00
|
|
|
|
2013-10-26 14:18:36 +02:00
|
|
|
return stream
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
|
|
|
|
2014-05-07 21:28:10 +02:00
|
|
|
Storage.prototype.unpack_tarball = function(file, callback) {
|
|
|
|
new targz().extract(file + '.tgz', file, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
Storage.prototype.get_readme = function(name, version, callback) {
|
2014-08-08 04:17:05 +02:00
|
|
|
var self = this
|
|
|
|
, storage = this.storage(name)
|
|
|
|
if (!storage) return callback('')
|
|
|
|
|
|
|
|
var fileName = storage.path + '/' + name + '-' + version
|
2014-05-07 21:28:10 +02:00
|
|
|
|
|
|
|
fs.exists(fileName, function(exists) {
|
2014-08-08 04:17:05 +02:00
|
|
|
if (exists) {
|
2014-05-07 21:28:10 +02:00
|
|
|
returnReadme();
|
2014-08-08 04:17:05 +02:00
|
|
|
} else {
|
2014-05-07 21:28:10 +02:00
|
|
|
self.unpack_tarball(fileName, function(err) {
|
|
|
|
returnReadme();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function returnReadme() {
|
|
|
|
var readmeFileName = fileName + '/package/README.md';
|
|
|
|
|
2014-08-08 04:17:05 +02:00
|
|
|
fs.readFile(readmeFileName, {encoding: 'utf8'}, function(err, data) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
callback(data)
|
|
|
|
})
|
2014-05-07 21:28:10 +02:00
|
|
|
}
|
2014-09-10 19:55:26 +02:00
|
|
|
}
|
2014-05-07 21:28:10 +02:00
|
|
|
|
2013-06-13 16:21:14 +02:00
|
|
|
Storage.prototype.get_tarball = function(name, filename, callback) {
|
2013-12-15 21:54:29 +01:00
|
|
|
assert(utils.validate_name(filename))
|
2014-08-08 03:08:41 +02:00
|
|
|
var self = this
|
2013-12-15 21:54:29 +01:00
|
|
|
|
2013-10-26 14:18:36 +02:00
|
|
|
var stream = new mystreams.ReadTarballStream()
|
2013-09-27 10:56:13 +02:00
|
|
|
stream.abort = function() {
|
2014-08-08 03:08:41 +02:00
|
|
|
if (rstream) rstream.abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
var storage = self.storage(name)
|
|
|
|
if (!storage) {
|
|
|
|
process.nextTick(function() {
|
2014-09-10 19:55:26 +02:00
|
|
|
stream.emit('error', Error[404]('no such file available'))
|
2014-08-08 03:08:41 +02:00
|
|
|
})
|
|
|
|
return stream
|
2013-10-26 14:18:36 +02:00
|
|
|
}
|
2013-06-20 15:07:34 +02:00
|
|
|
|
2014-08-08 03:08:41 +02:00
|
|
|
var rstream = storage.read_stream(filename)
|
2013-06-20 15:07:34 +02:00
|
|
|
rstream.on('error', function(err) {
|
2013-06-08 03:16:28 +02:00
|
|
|
if (err && err.code === 'ENOENT') {
|
2014-09-10 19:55:26 +02:00
|
|
|
stream.emit('error', Error(404, 'no such file available'))
|
2013-06-20 15:07:34 +02:00
|
|
|
} else {
|
2013-10-26 14:18:36 +02:00
|
|
|
stream.emit('error', err)
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
})
|
2014-03-07 20:39:20 +01:00
|
|
|
rstream.on('content-length', function(v) {
|
|
|
|
stream.emit('content-length', v)
|
|
|
|
})
|
2013-06-20 15:07:34 +02:00
|
|
|
rstream.on('open', function() {
|
2013-09-27 10:56:13 +02:00
|
|
|
// re-emitting open because it's handled in storage.js
|
2013-10-26 14:18:36 +02:00
|
|
|
stream.emit('open')
|
|
|
|
rstream.pipe(stream)
|
|
|
|
})
|
|
|
|
return stream
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
|
|
|
|
2013-12-09 04:58:25 +01:00
|
|
|
Storage.prototype.get_package = function(name, options, callback) {
|
|
|
|
if (typeof(options) === 'function') callback = options, options = {}
|
|
|
|
|
2013-10-18 22:46:13 +02:00
|
|
|
var self = this
|
2014-08-08 03:08:41 +02:00
|
|
|
var storage = self.storage(name)
|
2014-09-10 19:55:26 +02:00
|
|
|
if (!storage) return callback(Error[404]('no such package available'))
|
2013-10-18 22:46:13 +02:00
|
|
|
|
2014-08-08 03:08:41 +02:00
|
|
|
storage.read_json(info_file, function(err, result) {
|
2013-10-18 22:46:13 +02:00
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
2014-09-10 19:55:26 +02:00
|
|
|
return callback(Error[404]('no such package available'))
|
2013-10-18 22:46:13 +02:00
|
|
|
} else {
|
2014-01-13 17:48:51 +01:00
|
|
|
return callback(self._internal_error(err, info_file, 'error reading'))
|
2013-10-18 22:46:13 +02:00
|
|
|
}
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
2013-10-22 09:00:04 +02:00
|
|
|
self._normalize_package(result)
|
|
|
|
callback(err, result)
|
2013-10-18 22:46:13 +02:00
|
|
|
})
|
2013-06-08 03:16:28 +02:00
|
|
|
}
|
|
|
|
|
2014-04-12 19:20:26 +02:00
|
|
|
Storage.prototype.get_recent_packages = function(startkey, callback) {
|
|
|
|
var self = this
|
|
|
|
var i = 0
|
|
|
|
var list = []
|
2014-08-08 03:08:41 +02:00
|
|
|
|
|
|
|
var storage = self.storage('')
|
|
|
|
if (!storage) return callback(null, [])
|
|
|
|
|
|
|
|
fs.readdir(storage.path, function(err, files) {
|
2014-04-12 19:20:26 +02:00
|
|
|
if (err) return callback(null, [])
|
|
|
|
|
|
|
|
var filesL = files.length
|
|
|
|
|
|
|
|
files.forEach(function(file) {
|
2014-08-08 03:08:41 +02:00
|
|
|
fs.stat(storage.path, function(err, stats) {
|
2014-04-12 19:20:26 +02:00
|
|
|
if (err) return callback(err)
|
|
|
|
if (stats.mtime > startkey) {
|
|
|
|
list.push({
|
|
|
|
time: stats.mtime,
|
|
|
|
name: file
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (++i !== filesL) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return callback(null, list)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-10-22 07:10:25 +02:00
|
|
|
//
|
|
|
|
// This function allows to update the package thread-safely
|
|
|
|
//
|
|
|
|
// Arguments:
|
|
|
|
// - name - package name
|
|
|
|
// - updateFn - function(package, cb) - update function
|
|
|
|
// - callback - callback that gets invoked after it's all updated
|
|
|
|
//
|
|
|
|
// Algorithm:
|
|
|
|
// 1. lock package.json for writing
|
|
|
|
// 2. read package.json
|
|
|
|
// 3. updateFn(pkg, cb), and wait for cb
|
|
|
|
// 4. write package.json.tmp
|
|
|
|
// 5. move package.json.tmp package.json
|
|
|
|
// 6. callback(err?)
|
|
|
|
//
|
|
|
|
Storage.prototype.update_package = function(name, updateFn, _callback) {
|
|
|
|
var self = this
|
2014-08-08 03:08:41 +02:00
|
|
|
var storage = self.storage(name)
|
2014-09-10 19:55:26 +02:00
|
|
|
if (!storage) return _callback(Error[404]('no such package available'))
|
2014-08-08 03:08:41 +02:00
|
|
|
storage.lock_and_read_json(info_file, function(err, fd, json) {
|
2013-10-22 07:10:25 +02:00
|
|
|
function callback() {
|
|
|
|
var _args = arguments
|
|
|
|
if (fd) {
|
|
|
|
fs.close(fd, function(err) {
|
|
|
|
if (err) return _callback(err)
|
|
|
|
_callback.apply(null, _args)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
_callback.apply(null, _args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
if (err.code === 'EAGAIN') {
|
2014-09-10 19:55:26 +02:00
|
|
|
return callback(Error[503]('resource temporarily unavailable'))
|
2013-10-22 07:10:25 +02:00
|
|
|
} else if (err.code === 'ENOENT') {
|
2014-09-10 19:55:26 +02:00
|
|
|
return callback(Error[404]('no such package available'))
|
2013-10-22 07:10:25 +02:00
|
|
|
} else {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 09:00:04 +02:00
|
|
|
self._normalize_package(json)
|
2013-10-22 07:10:25 +02:00
|
|
|
updateFn(json, function(err) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
2013-10-22 09:00:04 +02:00
|
|
|
self._write_package(name, json, callback)
|
2013-10-22 07:10:25 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-10-22 09:00:04 +02:00
|
|
|
Storage.prototype._normalize_package = function(pkg) {
|
2013-10-26 14:18:36 +02:00
|
|
|
;['versions', 'dist-tags', '_distfiles', '_attachments', '_uplinks'].forEach(function(key) {
|
2013-10-22 09:29:57 +02:00
|
|
|
if (!utils.is_object(pkg[key])) pkg[key] = {}
|
2013-10-26 14:18:36 +02:00
|
|
|
})
|
2013-10-22 09:00:04 +02:00
|
|
|
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('-')
|
2013-10-23 08:15:17 +02:00
|
|
|
json._rev = ((+rev[0] || 0) + 1) + '-' + crypto.pseudoRandomBytes(8).toString('hex')
|
2013-10-22 09:00:04 +02:00
|
|
|
|
2014-08-08 03:08:41 +02:00
|
|
|
var storage = this.storage(name)
|
|
|
|
if (!storage) return callback()
|
|
|
|
storage.write_json(info_file, json, callback)
|
2014-01-13 17:48:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Storage.prototype.storage = function(package) {
|
2014-08-08 03:08:41 +02:00
|
|
|
var path = this.config.get_package_setting(package, 'storage')
|
|
|
|
if (path == null) path = this.config.storage
|
|
|
|
if (path == null || path === false) {
|
|
|
|
this.logger.debug({name: package}, 'this package has no storage defined: @{name}')
|
|
|
|
return null
|
|
|
|
}
|
2014-01-13 17:48:51 +01:00
|
|
|
return new Path_Wrapper(
|
|
|
|
Path.join(
|
2014-08-08 03:08:41 +02:00
|
|
|
Path.resolve(Path.dirname(this.config.self_path), path),
|
2014-01-13 17:48:51 +01:00
|
|
|
package
|
|
|
|
)
|
|
|
|
)
|
2013-10-22 09:00:04 +02:00
|
|
|
}
|
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
var Path_Wrapper = (function() {
|
|
|
|
// a wrapper adding paths to fs_storage methods
|
|
|
|
function Wrapper(path) {
|
|
|
|
this.path = path
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i in fs_storage) {
|
|
|
|
if (fs_storage.hasOwnProperty(i)) {
|
|
|
|
Wrapper.prototype[i] = wrapper(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapper(method) {
|
|
|
|
return function(/*...*/) {
|
|
|
|
var args = Array.prototype.slice.apply(arguments)
|
|
|
|
args[0] = Path.join(this.path, args[0] || '')
|
|
|
|
return fs_storage[method].apply(null, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Wrapper
|
|
|
|
})()
|
|
|
|
|
2013-10-26 14:18:36 +02:00
|
|
|
module.exports = Storage
|
2013-06-13 16:21:14 +02:00
|
|
|
|