2013-10-18 15:46:13 -05:00
|
|
|
var fs = require('fs')
|
|
|
|
, Path = require('path')
|
2013-10-22 02:00:04 -05:00
|
|
|
, crypto = require('crypto')
|
2013-12-15 15:54:29 -05:00
|
|
|
, assert = require('assert')
|
2014-01-13 11:48:51 -05:00
|
|
|
, fs_storage = require('./local-fs')
|
2013-10-18 15:46:13 -05:00
|
|
|
, 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
|
|
|
|
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
|
|
|
|
'_distfiles': {},
|
2013-10-22 02:00:04 -05:00
|
|
|
'_attachments': {},
|
2013-10-22 04:31:48 -05:00
|
|
|
'_uplinks': {},
|
2013-10-26 07:18:36 -05:00
|
|
|
}
|
2013-06-18 13:14:55 -05:00
|
|
|
}
|
|
|
|
|
2013-10-18 16:17:53 -05:00
|
|
|
Storage.prototype._internal_error = function(err, file, msg) {
|
2014-01-13 11:48:51 -05:00
|
|
|
this.logger.error( {err: err, file: file}
|
2013-10-18 16:17:53 -05:00
|
|
|
, 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) {
|
2014-01-13 11:48:51 -05:00
|
|
|
this.storage(name).create_json(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'
|
2013-10-26 07:18:36 -05:00
|
|
|
}))
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
callback()
|
|
|
|
})
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2013-10-06 03:27:50 -05:00
|
|
|
Storage.prototype.remove_package = function(name, callback) {
|
2013-10-22 02:53:59 -05:00
|
|
|
var self = this
|
2013-10-23 01:15:17 -05:00
|
|
|
self.logger.info({name: name}, 'unpublishing @{name} (all)')
|
2014-01-13 11:48:51 -05:00
|
|
|
self.storage(name).read_json(info_file, function(err, data) {
|
2013-10-22 08:33:39 -05:00
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return callback(new UError({
|
|
|
|
status: 404,
|
|
|
|
msg: 'no such package available',
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2013-10-06 03:27:50 -05:00
|
|
|
}
|
2013-10-22 08:33:39 -05:00
|
|
|
self._normalize_package(data)
|
|
|
|
|
2014-01-13 11:48:51 -05:00
|
|
|
self.storage(name).unlink(info_file, function(err) {
|
2013-10-22 08:33:39 -05: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-01-13 11:48:51 -05:00
|
|
|
self.storage(name).unlink(file, function() {
|
2013-10-22 08:33:39 -05:00
|
|
|
unlinkNext(cb)
|
|
|
|
})
|
|
|
|
}
|
2013-10-22 02:53:59 -05:00
|
|
|
|
2013-10-22 08:33:39 -05:00
|
|
|
unlinkNext(function() {
|
|
|
|
// try to unlink the directory, but ignore errors because it can fail
|
2014-01-13 11:48:51 -05:00
|
|
|
self.storage(name).rmdir('.', function(err) {
|
2013-10-22 08:33:39 -05:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
})
|
2013-10-22 02:53:59 -05:00
|
|
|
})
|
|
|
|
})
|
2013-10-06 03:27:50 -05:00
|
|
|
}
|
|
|
|
|
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
|
2014-01-13 11:48:51 -05:00
|
|
|
self.storage(name).read_json(info_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
|
2013-10-26 07:18:36 -05:00
|
|
|
data = get_boilerplate(name)
|
2013-06-18 13:14:55 -05:00
|
|
|
} else {
|
2014-01-13 11:48:51 -05:00
|
|
|
return callback(self._internal_error(err, info_file, 'error reading'))
|
2013-06-18 13:14:55 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-22 02:00:04 -05:00
|
|
|
self._normalize_package(data)
|
|
|
|
callback(null, data)
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
2013-06-18 13:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// synchronize remote package info with the local one
|
|
|
|
// TODO: readfile called twice
|
|
|
|
Storage.prototype.update_versions = function(name, newdata, callback) {
|
2013-10-22 04:31:48 -05:00
|
|
|
var self = this
|
2013-06-18 13:14:55 -05:00
|
|
|
self._read_create_package(name, function(err, data) {
|
2013-10-22 04:31:48 -05:00
|
|
|
if (err) return callback(err)
|
2013-06-18 13:14:55 -05:00
|
|
|
|
2013-10-22 04:31:48 -05:00
|
|
|
var change = false
|
2013-06-18 13:14:55 -05:00
|
|
|
for (var ver in newdata.versions) {
|
|
|
|
if (data.versions[ver] == null) {
|
2013-10-22 04:31:48 -05:00
|
|
|
var verdata = newdata.versions[ver]
|
2013-06-19 11:58:16 -05:00
|
|
|
|
|
|
|
// why does anyone need to keep that in database?
|
2013-10-22 04:31:48 -05:00
|
|
|
delete verdata.readme
|
2013-06-19 11:58:16 -05:00
|
|
|
|
2013-10-22 04:31:48 -05:00
|
|
|
change = true
|
|
|
|
data.versions[ver] = verdata
|
2013-10-26 07:18:36 -05:00
|
|
|
|
2013-06-19 11:58:16 -05:00
|
|
|
if (verdata.dist && verdata.dist.tarball) {
|
|
|
|
var url = utils.parse_tarball_url(
|
2014-04-12 21:04:48 -05:00
|
|
|
/*verdata.dist.__sinopia_orig_tarball ||*/ verdata.dist.tarball
|
2013-10-22 04:31:48 -05:00
|
|
|
)
|
2013-06-19 11:58:16 -05:00
|
|
|
|
|
|
|
// we do NOT overwrite any existing records
|
|
|
|
if (url != null && data._distfiles[url.filename] == null) {
|
|
|
|
data._distfiles[url.filename] = {
|
2014-04-12 21:04:48 -05:00
|
|
|
url: /*verdata.dist.__sinopia_orig_tarball ||*/ verdata.dist.tarball,
|
2013-06-19 11:58:16 -05:00
|
|
|
sha: verdata.dist.shasum,
|
2013-10-22 04:31:48 -05:00
|
|
|
}
|
2013-06-19 11:58:16 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-18 13:14:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var tag in newdata['dist-tags']) {
|
2013-12-10 04:55:35 -05: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 13:14:55 -05:00
|
|
|
|
|
|
|
if (need_change) {
|
2013-10-22 04:31:48 -05:00
|
|
|
change = true
|
|
|
|
data['dist-tags'][tag] = newdata['dist-tags'][tag]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var up in newdata._uplinks) {
|
2013-12-28 19:58:48 -05:00
|
|
|
var need_change =
|
2014-03-02 04:47:09 -05: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 04:31:48 -05:00
|
|
|
|
|
|
|
if (need_change) {
|
|
|
|
change = true
|
|
|
|
data._uplinks[up] = newdata._uplinks[up]
|
2013-06-18 13:14:55 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
|
2013-06-18 13:14:55 -05:00
|
|
|
if (change) {
|
2013-10-22 04:31:48 -05:00
|
|
|
self.logger.debug('updating package info')
|
2013-12-14 18:15:58 -05:00
|
|
|
self._write_package(name, data, function(err) {
|
|
|
|
callback(err, data)
|
|
|
|
})
|
2013-06-18 13:14:55 -05:00
|
|
|
} else {
|
2013-12-14 18:15:58 -05:00
|
|
|
callback(null, data)
|
2013-06-18 13:14:55 -05:00
|
|
|
}
|
2013-10-22 04:31:48 -05:00
|
|
|
})
|
2013-06-18 13:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Storage.prototype.add_version = function(name, version, metadata, tag, callback) {
|
2013-10-22 00:10:25 -05:00
|
|
|
var self = this
|
|
|
|
self.update_package(name, function updater(data, cb) {
|
2013-06-19 11:58:16 -05:00
|
|
|
// why does anyone need to keep that in database?
|
2013-10-22 00:10:25 -05:00
|
|
|
delete metadata.readme
|
2013-06-07 20:16:28 -05:00
|
|
|
|
|
|
|
if (data.versions[version] != null) {
|
2013-10-22 00:10:25 -05:00
|
|
|
return cb(new UError({
|
2013-06-07 20:16:28 -05:00
|
|
|
status: 409,
|
|
|
|
msg: 'this version already present'
|
2013-10-22 00:10:25 -05:00
|
|
|
}))
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
2013-10-22 02:29:57 -05:00
|
|
|
|
2013-10-22 02:45:19 -05: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) {
|
|
|
|
return cb(new UError({
|
|
|
|
status: 400,
|
|
|
|
msg: 'shasum error, ' + data._attachments[tarball].shasum + ' != ' + metadata.dist.shasum,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data._attachments[tarball].version = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 00:10:25 -05:00
|
|
|
data.versions[version] = metadata
|
2014-03-28 21:31:34 -05:00
|
|
|
utils.tag_version(data, version, tag, self.config)
|
2013-10-22 00:10:25 -05:00
|
|
|
cb()
|
|
|
|
}, callback)
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2014-04-12 21:04:48 -05:00
|
|
|
Storage.prototype.add_tags = function(name, tags, callback) {
|
2013-12-27 08:06:30 -05:00
|
|
|
var self = this
|
2013-12-29 01:41:31 -05:00
|
|
|
|
2013-12-27 08:06:30 -05:00
|
|
|
self.update_package(name, function updater(data, cb) {
|
2014-04-12 21:04:48 -05:00
|
|
|
for (var t in tags) {
|
|
|
|
if (data.versions[tags[t]] == null) {
|
|
|
|
return cb(new UError({
|
|
|
|
status: 404,
|
|
|
|
msg: "this version doesn't exist"
|
|
|
|
}))
|
|
|
|
}
|
2013-12-27 08:06:30 -05:00
|
|
|
|
2014-04-12 21:04:48 -05:00
|
|
|
utils.tag_version(data, tags[t], t, self.config)
|
|
|
|
cb()
|
|
|
|
}
|
2013-12-27 08:06:30 -05:00
|
|
|
}, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
// change package info to tag a specific version
|
|
|
|
function _add_tag(data, version, tag) {
|
|
|
|
data['dist-tags'][tag] = version
|
|
|
|
}
|
|
|
|
|
2013-10-23 01:15:17 -05: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'])) {
|
|
|
|
return callback(new UError({
|
|
|
|
status: 422,
|
|
|
|
msg: 'bad data',
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
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 15:54:29 -05:00
|
|
|
assert(utils.validate_name(filename))
|
2013-10-23 01:15:17 -05:00
|
|
|
|
|
|
|
self.update_package(name, function updater(data, cb) {
|
|
|
|
if (data._attachments[filename]) {
|
|
|
|
delete data._attachments[filename]
|
|
|
|
cb()
|
|
|
|
} else {
|
|
|
|
cb(new UError({
|
|
|
|
status: 404,
|
|
|
|
msg: 'no such file available',
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}, function(err) {
|
|
|
|
if (err) return callback(err)
|
2014-01-13 11:48:51 -05:00
|
|
|
self.storage(name).unlink(filename, callback)
|
2013-10-23 01:15:17 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-06-20 08:07:34 -05:00
|
|
|
Storage.prototype.add_tarball = function(name, filename) {
|
2013-12-15 15:54:29 -05:00
|
|
|
assert(utils.validate_name(filename))
|
|
|
|
|
2013-10-22 02:12:06 -05:00
|
|
|
var stream = new mystreams.UploadTarballStream()
|
|
|
|
, _transform = stream._transform
|
|
|
|
, length = 0
|
|
|
|
, shasum = crypto.createHash('sha1')
|
|
|
|
|
2013-09-28 07:19:40 -05:00
|
|
|
stream._transform = function(data) {
|
2013-10-22 02:12:06 -05:00
|
|
|
shasum.update(data)
|
|
|
|
length += data.length
|
|
|
|
_transform.apply(stream, arguments)
|
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
|
2013-10-22 02:12:06 -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-10-22 02:12:06 -05:00
|
|
|
}))
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2014-01-13 11:48:51 -05:00
|
|
|
var wstream = this.storage(name).write_stream(filename)
|
2013-06-20 08:07:34 -05:00
|
|
|
|
|
|
|
wstream.on('error', function(err) {
|
|
|
|
if (err.code === 'EEXISTS') {
|
|
|
|
stream.emit('error', new UError({
|
|
|
|
status: 409,
|
|
|
|
msg: 'this tarball is already present'
|
2013-10-26 07:18:36 -05:00
|
|
|
}))
|
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) {
|
2013-10-26 07:18:36 -05:00
|
|
|
stream.emit('error', _err)
|
2013-09-28 07:19:40 -05:00
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
stream.emit('error', err)
|
2013-09-28 07:19:40 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
2013-06-20 08:07:34 -05:00
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
stream.emit('error', err)
|
2013-06-20 08:07:34 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -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
|
2013-10-26 07:18:36 -05:00
|
|
|
stream.emit('open')
|
2013-10-22 02:12:06 -05:00
|
|
|
})
|
2013-09-27 06:31:28 -05:00
|
|
|
wstream.on('success', function() {
|
2013-10-22 02:12:06 -05: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 06:31:28 -05:00
|
|
|
stream.abort = function() {
|
2013-10-22 02:12:06 -05:00
|
|
|
wstream.abort()
|
|
|
|
}
|
2013-09-27 06:31:28 -05:00
|
|
|
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'
|
2013-10-26 07:18:36 -05:00
|
|
|
}))
|
|
|
|
wstream.abort()
|
2013-09-28 07:19:40 -05:00
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
wstream.done()
|
2013-09-28 07:19:40 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
}
|
|
|
|
stream.pipe(wstream)
|
2013-09-24 01:28:26 -05:00
|
|
|
|
2013-10-26 07:18:36 -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-12-15 15:54:29 -05:00
|
|
|
assert(utils.validate_name(filename))
|
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
var stream = new mystreams.ReadTarballStream()
|
2013-09-27 03:56:13 -05:00
|
|
|
stream.abort = function() {
|
2014-03-07 14:39:20 -05:00
|
|
|
rstream.abort()
|
2013-10-26 07:18:36 -05:00
|
|
|
}
|
2013-06-20 08:07:34 -05:00
|
|
|
|
2014-01-13 11:48:51 -05:00
|
|
|
var rstream = this.storage(name).read_stream(filename)
|
2013-06-20 08:07:34 -05:00
|
|
|
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-10-26 07:18:36 -05:00
|
|
|
}))
|
2013-06-20 08:07:34 -05:00
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
stream.emit('error', err)
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
2014-03-07 14:39:20 -05:00
|
|
|
rstream.on('content-length', function(v) {
|
|
|
|
stream.emit('content-length', v)
|
|
|
|
})
|
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-10-26 07:18:36 -05:00
|
|
|
stream.emit('open')
|
|
|
|
rstream.pipe(stream)
|
|
|
|
})
|
|
|
|
return stream
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2013-12-08 22:58:25 -05:00
|
|
|
Storage.prototype.get_package = function(name, options, callback) {
|
|
|
|
if (typeof(options) === 'function') callback = options, options = {}
|
|
|
|
|
2013-10-18 15:46:13 -05:00
|
|
|
var self = this
|
|
|
|
|
2014-01-13 11:48:51 -05:00
|
|
|
self.storage(name).read_json(info_file, function(err, result) {
|
2013-10-18 15:46:13 -05:00
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return callback(new UError({
|
|
|
|
status: 404,
|
|
|
|
msg: 'no such package available'
|
|
|
|
}))
|
|
|
|
} else {
|
2014-01-13 11:48:51 -05:00
|
|
|
return callback(self._internal_error(err, info_file, 'error reading'))
|
2013-10-18 15:46:13 -05:00
|
|
|
}
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
2013-10-22 02:00:04 -05:00
|
|
|
self._normalize_package(result)
|
|
|
|
callback(err, result)
|
2013-10-18 15:46:13 -05:00
|
|
|
})
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
|
|
|
|
2013-10-22 00:10:25 -05: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-01-13 11:48:51 -05:00
|
|
|
self.storage(name).lock_and_read_json(info_file, function(err, fd, json) {
|
2013-10-22 00:10:25 -05: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') {
|
|
|
|
return callback(new UError({
|
|
|
|
status: 503,
|
|
|
|
msg: 'resource temporarily unavailable'
|
|
|
|
}))
|
|
|
|
} else if (err.code === 'ENOENT') {
|
|
|
|
return callback(new UError({
|
|
|
|
status: 404,
|
|
|
|
msg: 'no such package available',
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-22 02:00:04 -05:00
|
|
|
self._normalize_package(json)
|
2013-10-22 00:10:25 -05:00
|
|
|
updateFn(json, function(err) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
2013-10-22 02:00:04 -05:00
|
|
|
self._write_package(name, json, callback)
|
2013-10-22 00:10:25 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-10-22 02:00:04 -05:00
|
|
|
Storage.prototype._normalize_package = function(pkg) {
|
2013-10-26 07:18:36 -05:00
|
|
|
;['versions', 'dist-tags', '_distfiles', '_attachments', '_uplinks'].forEach(function(key) {
|
2013-10-22 02:29:57 -05:00
|
|
|
if (!utils.is_object(pkg[key])) pkg[key] = {}
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
2013-10-22 02:00:04 -05: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 01:15:17 -05:00
|
|
|
json._rev = ((+rev[0] || 0) + 1) + '-' + crypto.pseudoRandomBytes(8).toString('hex')
|
2013-10-22 02:00:04 -05:00
|
|
|
|
2014-01-13 11:48:51 -05:00
|
|
|
this.storage(name).write_json(info_file, json, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage.prototype.storage = function(package) {
|
|
|
|
return new Path_Wrapper(
|
|
|
|
Path.join(
|
|
|
|
Path.resolve(
|
|
|
|
Path.dirname(this.config.self_path),
|
2014-01-13 13:56:36 -05:00
|
|
|
this.config.get_package_setting(package, 'storage') || this.config.storage
|
2014-01-13 11:48:51 -05:00
|
|
|
),
|
|
|
|
package
|
|
|
|
)
|
|
|
|
)
|
2013-10-22 02:00:04 -05:00
|
|
|
}
|
|
|
|
|
2014-01-13 11:48:51 -05: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 07:18:36 -05:00
|
|
|
module.exports = Storage
|
2013-06-13 09:21:14 -05:00
|
|
|
|