0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-03 23:09:17 -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) { function Storage(config, mainconfig) {
if (!(this instanceof Storage)) return new Storage(config); if (!(this instanceof Storage)) return new Storage(config);
this.config = config; this.config = config;
this.ua = mainconfig.user_agent; this.is_alive = false;
this.userAgent = mainconfig.user_agent;
this.ca; this.ca;
this.url = URL.parse(this.config.url); this.url = URL.parse(this.config.url);
@ -29,6 +30,42 @@ function Storage(config, mainconfig) {
return this; 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) { Storage.prototype.can_fetch_url = function(url) {
url = URL.parse(url); url = URL.parse(url);
@ -38,13 +75,9 @@ Storage.prototype.can_fetch_url = function(url) {
} }
Storage.prototype.add_package = function(name, metadata, callback) { Storage.prototype.add_package = function(name, metadata, callback) {
request({ this.request({
url: this.config.url + '/' + escape(name), uri: '/' + escape(name),
headers: {
'User-Agent': this.ua,
},
method: 'PUT', method: 'PUT',
ca: this.ca,
json: metadata, json: metadata,
}, function(err, res, body) { }, function(err, res, body) {
if (err) return callback(err); 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) { Storage.prototype.add_version = function(name, version, metadata, tag, callback) {
request({ this.request({
url: this.config.url + '/' + escape(name) + '/' + escape(version) + '/-tag/' + escape(tag), uri: '/' + escape(name) + '/' + escape(version) + '/-tag/' + escape(tag),
headers: {
'User-Agent': this.ua,
},
method: 'PUT', method: 'PUT',
ca: this.ca,
json: metadata, json: metadata,
}, function(err, res, body) { }, function(err, res, body) {
if (err) return callback(err); if (err) return callback(err);
@ -77,14 +106,12 @@ Storage.prototype.add_tarball = function(name, filename) {
var stream = new mystreams.UploadTarballStream(); var stream = new mystreams.UploadTarballStream();
var self = this; var self = this;
var wstream = request({ var wstream = this.request({
uri: this.config.url + '/' + escape(name) + '/-/' + escape(filename) + '/whatever', uri: '/' + escape(name) + '/-/' + escape(filename) + '/whatever',
method: 'PUT', method: 'PUT',
headers: { headers: {
'User-Agent': this.ua,
'content-type': 'application/octet-stream' 'content-type': 'application/octet-stream'
}, },
ca: this.ca,
}); });
wstream.on('response', function(res) { wstream.on('response', function(res) {
@ -115,13 +142,9 @@ Storage.prototype.add_tarball = function(name, filename) {
} }
Storage.prototype.get_package = function(name, callback) { Storage.prototype.get_package = function(name, callback) {
request({ this.request({
url: this.config.url + '/' + escape(name), uri: '/' + escape(name),
json: true, json: true,
headers: {
'User-Agent': this.ua,
},
ca: this.ca,
}, function(err, res, body) { }, function(err, res, body) {
if (err) return callback(err); if (err) return callback(err);
if (res.statusCode === 404) { if (res.statusCode === 404) {
@ -142,15 +165,12 @@ Storage.prototype.get_tarball = function(name, filename) {
} }
Storage.prototype.get_url = function(url) { Storage.prototype.get_url = function(url) {
url = URL.parse(url);
var stream = new mystreams.ReadTarballStream(); var stream = new mystreams.ReadTarballStream();
stream.abort = function() {}; stream.abort = function() {};
var rstream = request({ var rstream = this.request({
url: url, uri: url.path,
headers: {
'User-Agent': this.ua,
},
ca: this.ca,
encoding: null, encoding: null,
}); });