1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00

Fix eslint warnings on up-storage.js

This commit is contained in:
Juan Picado 2017-04-29 23:08:50 +02:00
parent b264e9f3df
commit 1e4ac1f842
No known key found for this signature in database
GPG Key ID: 18AC54485952D158

@ -13,7 +13,61 @@ const encode = function(thing) {
return encodeURIComponent(thing).replace(/^%40/, '@');
};
const _setupProxy = function(hostname, config, mainconfig, isHTTPS) {
/**
* Implements Storage interface
* (same for storage.js, local-storage.js, up-storage.js)
*/
class Storage {
/**
* Constructor
* @param {*} config
* @param {*} mainconfig
*/
constructor(config, mainconfig) {
this.config = config;
this.failed_requests = 0;
this.userAgent = mainconfig.user_agent;
this.ca = config.ca;
this.logger = Logger.logger.child({sub: 'out'});
this.server_id = mainconfig.server_id;
this.url = URL.parse(this.config.url);
this._setupProxy(this.url.hostname, config, mainconfig, this.url.protocol === 'https:');
this.config.url = this.config.url.replace(/\/$/, '');
if (Number(this.config.timeout) >= 1000) {
this.logger.warn(['Too big timeout value: ' + this.config.timeout,
'We changed time format to nginx-like one',
'(see http://wiki.nginx.org/ConfigNotation)',
'so please update your config accordingly'].join('\n'));
}
// a bunch of different configurable timers
this.maxage = parse_interval(config_get('maxage', '2m' ));
this.timeout = parse_interval(config_get('timeout', '30s'));
this.max_fails = Number(config_get('max_fails', 2 ));
this.fail_timeout = parse_interval(config_get('fail_timeout', '5m' ));
return this;
/**
* Just a helper (`config[key] || default` doesn't work because of zeroes)
* @param {*} key
* @param {*} def
* @return {String}
*/
function config_get(key, def) {
return config[key] != null ? config[key] : def;
}
}
/**
* Set up a proxy.
* @param {*} hostname
* @param {*} config
* @param {*} mainconfig
* @param {*} isHTTPS
*/
_setupProxy(hostname, config, mainconfig, isHTTPS) {
let no_proxy;
let proxy_key = isHTTPS ? 'https_proxy' : 'http_proxy';
@ -58,57 +112,18 @@ const _setupProxy = function(hostname, config, mainconfig, isHTTPS) {
this.logger.debug( {url: this.url.href, proxy: this.proxy}
, 'using proxy @{proxy} for @{url}' );
}
};
//
// Implements Storage interface
// (same for storage.js, local-storage.js, up-storage.js)
//
class Storage {
}
/**
*
* @param {*} config
* @param {*} mainconfig
* Fetch an asset.
* @param {*} options
* @param {*} cb
* @return {Request}
*/
constructor(config, mainconfig) {
this.config = config;
this.failed_requests = 0;
this.userAgent = mainconfig.user_agent;
this.ca = config.ca;
this.logger = Logger.logger.child({sub: 'out'});
this.server_id = mainconfig.server_id;
this.url = URL.parse(this.config.url);
_setupProxy.call(this, this.url.hostname, config, mainconfig, this.url.protocol === 'https:');
this.config.url = this.config.url.replace(/\/$/, '');
if (Number(this.config.timeout) >= 1000) {
this.logger.warn(['Too big timeout value: ' + this.config.timeout,
'We changed time format to nginx-like one',
'(see http://wiki.nginx.org/ConfigNotation)',
'so please update your config accordingly'].join('\n'));
}
// a bunch of different configurable timers
this.maxage = parse_interval(config_get('maxage', '2m' ));
this.timeout = parse_interval(config_get('timeout', '30s'));
this.max_fails = Number(config_get('max_fails', 2 ));
this.fail_timeout = parse_interval(config_get('fail_timeout', '5m' ));
return this;
// just a helper (`config[key] || default` doesn't work because of zeroes)
function config_get(key, def) {
return config[key] != null ? config[key] : def;
}
}
}
Storage.prototype.request = function(options, cb) {
request(options, cb) {
let json;
if (!this.status_check()) {
var req = new Stream.Readable();
let req = new Stream.Readable();
process.nextTick(function() {
if (typeof(cb) === 'function') cb(Error('uplink is offline'));
req.emit('error', Error('uplink is offline'));
@ -129,8 +144,10 @@ Storage.prototype.request = function(options, cb) {
// add/override headers specified in the config
for (let key in this.config.headers) {
if (Object.prototype.hasOwnProperty.call(this.config.headers, key)) {
headers[key] = this.config.headers[key];
}
}
let method = options.method || 'GET';
let uri = options.uri_full || (this.config.url + options.uri);
@ -142,7 +159,7 @@ Storage.prototype.request = function(options, cb) {
}, 'making request: \'@{method} @{uri}\'');
if (Utils.is_object(options.json)) {
var json = JSON.stringify(options.json);
json = JSON.stringify(options.json);
headers['Content-Type'] = headers['Content-Type'] || 'application/json';
}
@ -154,6 +171,9 @@ Storage.prototype.request = function(options, cb) {
do_log();
cb(err, res, body);
/**
* Perform a decode.
*/
function do_decode() {
if (err) {
error = err.message;
@ -176,7 +196,9 @@ Storage.prototype.request = function(options, cb) {
}
}
}
/**
* Perform a log.
*/
function do_log() {
let message = '@{!status}, req: \'@{request.method} @{request.url}\'';
message += error
@ -196,7 +218,7 @@ Storage.prototype.request = function(options, cb) {
}
}) : undefined;
var req = request({
const req = request({
url: uri,
method: method,
headers: headers,
@ -233,9 +255,14 @@ Storage.prototype.request = function(options, cb) {
}
});
return req;
};
}
Storage.prototype.status_check = function(alive) {
/**
* Check whether the remote host is available.
* @param {*} alive
* @return {Boolean}
*/
status_check(alive) {
if (arguments.length === 0) {
if (this.failed_requests >= this.max_fails
&& Math.abs(Date.now() - this.last_request_time) < this.fail_timeout) {
@ -257,27 +284,39 @@ Storage.prototype.status_check = function(alive) {
}
this.last_request_time = Date.now();
}
};
}
Storage.prototype.can_fetch_url = function(url) {
/**
* Determine whether can fetch from the provided URL.
* @param {*} url
* @return {Boolean}
*/
can_fetch_url(url) {
url = URL.parse(url);
return url.protocol === this.url.protocol
&& url.host === this.url.host
&& url.path.indexOf(this.url.path) === 0;
};
}
Storage.prototype.get_package = function(name, options, callback) {
if (typeof(options) === 'function') callback = options, options = {};
let headers = {};
/**
* Get a remote package.
* @param {*} name
* @param {*} options
* @param {*} callback
*/
get_package(name, options, callback) {
if (typeof(options) === 'function') {
callback = options;
options = {};
}
const headers = {};
if (options.etag) {
headers['If-None-Match'] = options.etag;
headers['Accept'] = 'application/octet-stream';
}
this.request({
uri: '/' + encode(name),
uri: `/${encode(name)}`,
json: true,
headers: headers,
req: options.req,
@ -293,18 +332,33 @@ Storage.prototype.get_package = function(name, options, callback) {
}
callback(null, body, res.headers.etag);
});
};
}
Storage.prototype.get_tarball = function(name, options, filename) {
if (!options) options = {};
return this.get_url(this.config.url + '/' + name + '/-/' + filename);
};
/**
* Retrieve a tarball.
* @param {*} name
* @param {*} options
* @param {*} filename
* @return {Stream}
*/
get_tarball(name, options, filename) {
// FUTURE: es6 note: this must be default parameter
if (!options) {
options = {};
}
return this.get_url(`${this.config.url}'/'${name}/-/${filename}`);
}
Storage.prototype.get_url = function(url) {
let stream = MyStreams.ReadTarballStream();
/**
* Get an url.
* @param {String} url
* @return {Stream}
*/
get_url(url) {
const stream = MyStreams.ReadTarballStream();
stream.abort = function() {};
let current_length = 0, expected_length;
let current_length = 0;
let expected_length;
let rstream = this.request({
uri_full: url,
encoding: null,
@ -338,14 +392,17 @@ Storage.prototype.get_url = function(url) {
stream.emit('error', Error('content length mismatch'));
});
return stream;
};
}
Storage.prototype.search = function(startkey, options) {
let self = this;
let stream = new Stream.PassThrough({objectMode: true});
let req = self.request({
/**
* Perform a stream search.
* @param {*} startkey keyword
* @param {*} options request options
* @return {Stream}
*/
search(startkey, options) {
const stream = new Stream.PassThrough({objectMode: true});
let req = this.request({
uri: options.req.url,
req: options.req,
headers: {
@ -363,6 +420,7 @@ Storage.prototype.search = function(startkey, options) {
stream.emit('data', pkg);
}
});
res.on('end', () => {
stream.emit('end');
});
@ -377,11 +435,15 @@ Storage.prototype.search = function(startkey, options) {
stream.emit('end');
};
return stream;
};
}
Storage.prototype._add_proxy_headers = function(req, headers) {
/**
* Add proxy headers.
* @param {*} req the http request
* @param {*} headers the request headers
*/
_add_proxy_headers(req, headers) {
if (req) {
// Only submit X-Forwarded-For field if we don't have a proxy selected
// in the config file.
@ -405,7 +467,8 @@ Storage.prototype._add_proxy_headers = function(req, headers) {
: '';
headers['Via'] += '1.1 ' + this.server_id + ' (Verdaccio)';
};
}
}
module.exports = Storage;