0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-27 22:59:51 -05:00

update uplink code

This commit is contained in:
Alex Kocharin 2013-09-28 21:31:58 +04:00
parent a503bce863
commit f5b542724b

View file

@ -10,7 +10,8 @@ var mystreams = require('./streams');
function Storage(config, mainconfig) {
if (!(this instanceof Storage)) return new Storage(config);
this.config = config;
this.ua = mainconfig.user_agent;
this.is_alive = false;
this.userAgent = mainconfig.user_agent;
this.ca;
this.url = URL.parse(this.config.url);
@ -29,6 +30,42 @@ function Storage(config, mainconfig) {
return this;
}
Storage.prototype.request = function(options, cb) {
var self = this;
var headers = options.headers || {};
headers.accept = headers.accept || 'application/json';
headers['user-agent'] = headers['user-agent'] || this.userAgent;
var req = request({
url: this.config.url + options.uri,
method: options.method || 'GET',
headers: headers,
json: options.json || true,
ca: this.ca,
}, function(err) {
if (cb) cb.apply(self, arguments);
});
req.on('response', function() {
self.status_check(true);
});
req.on('error', function() {
self.status_check(false);
});
return req;
}
Storage.prototype.status_check = function(alive) {
if (arguments.length === 0) {
if (!this.is_alive && Math.abs(Date.now() - this.is_alive_time()) > 60*1000) {
return false;
} else {
return true;
}
} else {
this.is_alive = alive;
this.is_alive_time = Date.now();
}
}
Storage.prototype.can_fetch_url = function(url) {
url = URL.parse(url);
@ -38,13 +75,9 @@ Storage.prototype.can_fetch_url = function(url) {
}
Storage.prototype.add_package = function(name, metadata, callback) {
request({
url: this.config.url + '/' + escape(name),
headers: {
'User-Agent': this.ua,
},
this.request({
uri: '/' + escape(name),
method: 'PUT',
ca: this.ca,
json: metadata,
}, function(err, res, body) {
if (err) return callback(err);
@ -56,13 +89,9 @@ Storage.prototype.add_package = function(name, metadata, callback) {
}
Storage.prototype.add_version = function(name, version, metadata, tag, callback) {
request({
url: this.config.url + '/' + escape(name) + '/' + escape(version) + '/-tag/' + escape(tag),
headers: {
'User-Agent': this.ua,
},
this.request({
uri: '/' + escape(name) + '/' + escape(version) + '/-tag/' + escape(tag),
method: 'PUT',
ca: this.ca,
json: metadata,
}, function(err, res, body) {
if (err) return callback(err);
@ -77,14 +106,12 @@ Storage.prototype.add_tarball = function(name, filename) {
var stream = new mystreams.UploadTarballStream();
var self = this;
var wstream = request({
uri: this.config.url + '/' + escape(name) + '/-/' + escape(filename) + '/whatever',
var wstream = this.request({
uri: '/' + escape(name) + '/-/' + escape(filename) + '/whatever',
method: 'PUT',
headers: {
'User-Agent': this.ua,
'content-type': 'application/octet-stream'
},
ca: this.ca,
});
wstream.on('response', function(res) {
@ -115,13 +142,9 @@ Storage.prototype.add_tarball = function(name, filename) {
}
Storage.prototype.get_package = function(name, callback) {
request({
url: this.config.url + '/' + escape(name),
this.request({
uri: '/' + escape(name),
json: true,
headers: {
'User-Agent': this.ua,
},
ca: this.ca,
}, function(err, res, body) {
if (err) return callback(err);
if (res.statusCode === 404) {
@ -142,15 +165,12 @@ Storage.prototype.get_tarball = function(name, filename) {
}
Storage.prototype.get_url = function(url) {
url = URL.parse(url);
var stream = new mystreams.ReadTarballStream();
stream.abort = function() {};
var rstream = request({
url: url,
headers: {
'User-Agent': this.ua,
},
ca: this.ca,
var rstream = this.request({
uri: url.path,
encoding: null,
});