2017-04-23 20:02:26 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const JSONStream = require('JSONStream');
|
|
|
|
const Error = require('http-errors');
|
|
|
|
const request = require('request');
|
|
|
|
const Stream = require('readable-stream');
|
|
|
|
const URL = require('url');
|
|
|
|
const parse_interval = require('./config').parse_interval;
|
|
|
|
const Logger = require('./logger');
|
|
|
|
const MyStreams = require('./streams');
|
|
|
|
const Utils = require('./utils');
|
|
|
|
const encode = function(thing) {
|
2015-07-13 04:15:18 +02:00
|
|
|
return encodeURIComponent(thing).replace(/^%40/, '@');
|
2015-07-08 01:59:35 +02:00
|
|
|
};
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
const _setupProxy = function(hostname, config, mainconfig, isHTTPS) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let no_proxy;
|
|
|
|
let proxy_key = isHTTPS ? 'https_proxy' : 'http_proxy';
|
2014-11-12 12:14:37 +01:00
|
|
|
|
|
|
|
// get http_proxy and no_proxy configs
|
|
|
|
if (proxy_key in config) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.proxy = config[proxy_key];
|
2014-11-12 12:14:37 +01:00
|
|
|
} else if (proxy_key in mainconfig) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.proxy = mainconfig[proxy_key];
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
if ('no_proxy' in config) {
|
2017-04-23 20:02:26 +02:00
|
|
|
no_proxy = config.no_proxy;
|
2014-11-12 12:14:37 +01:00
|
|
|
} else if ('no_proxy' in mainconfig) {
|
2017-04-23 20:02:26 +02:00
|
|
|
no_proxy = mainconfig.no_proxy;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// use wget-like algorithm to determine if proxy shouldn't be used
|
2017-04-17 15:12:31 +02:00
|
|
|
if (hostname[0] !== '.') {
|
|
|
|
hostname = '.' + hostname;
|
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
if (typeof(no_proxy) === 'string' && no_proxy.length) {
|
2017-04-23 20:02:26 +02:00
|
|
|
no_proxy = no_proxy.split(',');
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
if (Array.isArray(no_proxy)) {
|
2017-04-17 15:12:31 +02:00
|
|
|
for (let i=0; i<no_proxy.length; i++) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let no_proxy_item = no_proxy[i];
|
|
|
|
if (no_proxy_item[0] !== '.') no_proxy_item = '.' + no_proxy_item;
|
2014-11-12 12:14:37 +01:00
|
|
|
if (hostname.lastIndexOf(no_proxy_item) === hostname.length - no_proxy_item.length) {
|
|
|
|
if (this.proxy) {
|
|
|
|
this.logger.debug({url: this.url.href, rule: no_proxy_item},
|
2017-04-23 20:02:26 +02:00
|
|
|
'not using proxy for @{url}, excluded by @{rule} rule');
|
|
|
|
this.proxy = false;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2017-04-17 15:12:31 +02:00
|
|
|
break;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's non-string (i.e. "false"), don't use it
|
|
|
|
if (typeof(this.proxy) !== 'string') {
|
2017-04-23 20:02:26 +02:00
|
|
|
delete this.proxy;
|
2014-11-12 12:14:37 +01:00
|
|
|
} else {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.logger.debug( {url: this.url.href, proxy: this.proxy}
|
|
|
|
, 'using proxy @{proxy} for @{url}' );
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
};
|
2013-11-24 18:07:18 +01:00
|
|
|
|
2017-04-19 20:45:48 +02:00
|
|
|
//
|
|
|
|
// Implements Storage interface
|
|
|
|
// (same for storage.js, local-storage.js, up-storage.js)
|
|
|
|
//
|
|
|
|
class Storage {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {*} config
|
|
|
|
* @param {*} mainconfig
|
|
|
|
*/
|
|
|
|
constructor(config, mainconfig) {
|
2017-04-23 20:02:26 +02:00
|
|
|
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;
|
2017-04-19 20:45:48 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
this.url = URL.parse(this.config.url);
|
2017-04-19 20:45:48 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
_setupProxy.call(this, this.url.hostname, config, mainconfig, this.url.protocol === 'https:');
|
2017-04-19 20:45:48 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
this.config.url = this.config.url.replace(/\/$/, '');
|
2017-04-19 20:45:48 +02:00
|
|
|
if (Number(this.config.timeout) >= 1000) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.logger.warn(['Too big timeout value: ' + this.config.timeout,
|
2017-04-19 20:45:48 +02:00
|
|
|
'We changed time format to nginx-like one',
|
|
|
|
'(see http://wiki.nginx.org/ConfigNotation)',
|
2017-04-23 20:02:26 +02:00
|
|
|
'so please update your config accordingly'].join('\n'));
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// a bunch of different configurable timers
|
2017-04-23 20:02:26 +02:00
|
|
|
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;
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
// just a helper (`config[key] || default` doesn't work because of zeroes)
|
|
|
|
function config_get(key, def) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return config[key] != null ? config[key] : def;
|
2017-04-19 20:45:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-28 19:31:58 +02:00
|
|
|
Storage.prototype.request = function(options, cb) {
|
2014-11-12 12:14:37 +01:00
|
|
|
if (!this.status_check()) {
|
2017-04-23 20:02:26 +02:00
|
|
|
var req = new Stream.Readable();
|
2014-11-12 12:14:37 +01:00
|
|
|
process.nextTick(function() {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (typeof(cb) === 'function') cb(Error('uplink is offline'));
|
|
|
|
req.emit('error', Error('uplink is offline'));
|
|
|
|
});
|
|
|
|
req._read = function() {};
|
2014-11-12 12:14:37 +01:00
|
|
|
// preventing 'Uncaught, unspecified "error" event'
|
2017-04-23 20:02:26 +02:00
|
|
|
req.on('error', function() {});
|
|
|
|
return req;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:28:35 +02:00
|
|
|
let self = this;
|
|
|
|
let headers = options.headers || {};
|
|
|
|
headers['Accept'] = headers['Accept'] || 'application/json';
|
|
|
|
headers['Accept-Encoding'] = headers['Accept-Encoding'] || 'gzip';
|
2017-04-21 20:33:55 +02:00
|
|
|
// registry.npmjs.org will only return search result if user-agent include string 'npm'
|
2017-04-23 22:28:35 +02:00
|
|
|
headers['User-Agent'] = headers['User-Agent'] || `npm (${this.userAgent})`;
|
|
|
|
this._add_proxy_headers(options.req, headers);
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2016-10-05 12:56:03 +02:00
|
|
|
// add/override headers specified in the config
|
2017-04-17 15:12:31 +02:00
|
|
|
for (let key in this.config.headers) {
|
2017-04-23 20:02:26 +02:00
|
|
|
headers[key] = this.config.headers[key];
|
2016-10-05 12:56:03 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let method = options.method || 'GET';
|
|
|
|
let uri = options.uri_full || (this.config.url + options.uri);
|
2014-11-12 12:14:37 +01:00
|
|
|
|
|
|
|
self.logger.info({
|
2017-04-23 20:02:26 +02:00
|
|
|
method: method,
|
|
|
|
headers: headers,
|
|
|
|
uri: uri,
|
|
|
|
}, 'making request: \'@{method} @{uri}\'');
|
2014-11-12 12:14:37 +01:00
|
|
|
|
|
|
|
if (Utils.is_object(options.json)) {
|
2017-04-23 20:02:26 +02:00
|
|
|
var json = JSON.stringify(options.json);
|
|
|
|
headers['Content-Type'] = headers['Content-Type'] || 'application/json';
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let request_callback = cb ? (function(err, res, body) {
|
|
|
|
let error;
|
|
|
|
let res_length = err ? 0 : body.length;
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
do_decode();
|
|
|
|
do_log();
|
|
|
|
cb(err, res, body);
|
2014-11-12 12:14:37 +01:00
|
|
|
|
|
|
|
function do_decode() {
|
|
|
|
if (err) {
|
2017-04-23 20:02:26 +02:00
|
|
|
error = err.message;
|
|
|
|
return;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.json && res.statusCode < 300) {
|
|
|
|
try {
|
2017-04-23 20:02:26 +02:00
|
|
|
body = JSON.parse(body.toString('utf8'));
|
2014-11-12 12:14:37 +01:00
|
|
|
} catch(_err) {
|
2017-04-23 20:02:26 +02:00
|
|
|
body = {};
|
|
|
|
err = _err;
|
|
|
|
error = err.message;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err && Utils.is_object(body)) {
|
|
|
|
if (typeof(body.error) === 'string') {
|
2017-04-23 20:02:26 +02:00
|
|
|
error = body.error;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_log() {
|
2017-04-23 20:02:26 +02:00
|
|
|
let message = '@{!status}, req: \'@{request.method} @{request.url}\'';
|
2014-11-12 12:14:37 +01:00
|
|
|
message += error
|
|
|
|
? ', error: @{!error}'
|
2017-04-23 20:02:26 +02:00
|
|
|
: ', bytes: @{bytes.in}/@{bytes.out}';
|
2014-11-12 12:14:37 +01:00
|
|
|
self.logger.warn({
|
2017-04-23 20:02:26 +02:00
|
|
|
err: err,
|
|
|
|
request: {method: method, url: uri},
|
|
|
|
level: 35, // http
|
|
|
|
status: res != null ? res.statusCode : 'ERR',
|
|
|
|
error: error,
|
|
|
|
bytes: {
|
|
|
|
in: json ? json.length : 0,
|
|
|
|
out: res_length || 0,
|
|
|
|
},
|
|
|
|
}, message);
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
}) : undefined;
|
2015-05-16 18:33:06 +02:00
|
|
|
|
|
|
|
var req = request({
|
2017-04-23 20:02:26 +02:00
|
|
|
url: uri,
|
|
|
|
method: method,
|
|
|
|
headers: headers,
|
|
|
|
body: json,
|
|
|
|
ca: this.ca,
|
|
|
|
proxy: this.proxy,
|
|
|
|
encoding: null,
|
|
|
|
gzip: true,
|
|
|
|
timeout: this.timeout,
|
|
|
|
}, request_callback);
|
|
|
|
|
|
|
|
let status_called = false;
|
2014-11-12 12:14:37 +01:00
|
|
|
req.on('response', function(res) {
|
2016-11-07 18:15:38 +01:00
|
|
|
if (!req._verdaccio_aborted && !status_called) {
|
2017-04-23 20:02:26 +02:00
|
|
|
status_called = true;
|
|
|
|
self.status_check(true);
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2015-05-16 18:33:06 +02:00
|
|
|
|
|
|
|
if (!request_callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
(function do_log() {
|
|
|
|
let message = '@{!status}, req: \'@{request.method} @{request.url}\' (streaming)';
|
2015-05-16 18:33:06 +02:00
|
|
|
self.logger.warn({
|
2017-04-23 20:02:26 +02:00
|
|
|
request: {method: method, url: uri},
|
|
|
|
level: 35, // http
|
|
|
|
status: res != null ? res.statusCode : 'ERR',
|
|
|
|
}, message);
|
|
|
|
})();
|
2015-05-16 18:33:06 +02:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
});
|
2014-11-12 12:14:37 +01:00
|
|
|
req.on('error', function(_err) {
|
2016-11-07 18:15:38 +01:00
|
|
|
if (!req._verdaccio_aborted && !status_called) {
|
2017-04-23 20:02:26 +02:00
|
|
|
status_called = true;
|
|
|
|
self.status_check(false);
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
});
|
|
|
|
return req;
|
|
|
|
};
|
2013-09-28 19:31:58 +02:00
|
|
|
|
|
|
|
Storage.prototype.status_check = function(alive) {
|
2014-11-12 12:14:37 +01:00
|
|
|
if (arguments.length === 0) {
|
|
|
|
if (this.failed_requests >= this.max_fails
|
|
|
|
&& Math.abs(Date.now() - this.last_request_time) < this.fail_timeout) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return false;
|
2014-11-12 12:14:37 +01:00
|
|
|
} else {
|
2017-04-23 20:02:26 +02:00
|
|
|
return true;
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (alive) {
|
|
|
|
if (this.failed_requests >= this.max_fails) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.logger.warn({host: this.url.host}, 'host @{host} is back online');
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
this.failed_requests = 0;
|
2014-11-12 12:14:37 +01:00
|
|
|
} else {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.failed_requests++;
|
2014-11-12 12:14:37 +01:00
|
|
|
if (this.failed_requests === this.max_fails) {
|
2017-04-23 20:02:26 +02:00
|
|
|
this.logger.warn({host: this.url.host}, 'host @{host} is now offline');
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
this.last_request_time = Date.now();
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
};
|
2013-09-28 19:31:58 +02:00
|
|
|
|
2013-06-19 18:58:16 +02:00
|
|
|
Storage.prototype.can_fetch_url = function(url) {
|
2017-04-23 20:02:26 +02:00
|
|
|
url = URL.parse(url);
|
2013-06-19 18:58:16 +02:00
|
|
|
|
2014-11-12 12:14:37 +01:00
|
|
|
return url.protocol === this.url.protocol
|
|
|
|
&& url.host === this.url.host
|
2017-04-23 20:02:26 +02:00
|
|
|
&& url.path.indexOf(this.url.path) === 0;
|
|
|
|
};
|
2013-06-19 18:58:16 +02:00
|
|
|
|
2013-12-09 04:58:25 +01:00
|
|
|
Storage.prototype.get_package = function(name, options, callback) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (typeof(options) === 'function') callback = options, options = {};
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let headers = {};
|
2014-11-12 12:14:37 +01:00
|
|
|
if (options.etag) {
|
2017-04-23 20:02:26 +02:00
|
|
|
headers['If-None-Match'] = options.etag;
|
|
|
|
headers['Accept'] = 'application/octet-stream';
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.request({
|
2017-04-23 20:02:26 +02:00
|
|
|
uri: '/' + encode(name),
|
|
|
|
json: true,
|
|
|
|
headers: headers,
|
|
|
|
req: options.req,
|
2014-11-12 12:14:37 +01:00
|
|
|
}, function(err, res, body) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (err) return callback(err);
|
2014-11-12 12:14:37 +01:00
|
|
|
if (res.statusCode === 404) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return callback( Error[404]('package doesn\'t exist on uplink') );
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
if (!(res.statusCode >= 200 && res.statusCode < 300)) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let error = Error('bad status code: ' + res.statusCode);
|
|
|
|
error.remoteStatus = res.statusCode;
|
|
|
|
return callback(error);
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
callback(null, body, res.headers.etag);
|
|
|
|
});
|
|
|
|
};
|
2013-06-08 03:16:28 +02:00
|
|
|
|
2013-12-09 04:58:25 +01:00
|
|
|
Storage.prototype.get_tarball = function(name, options, filename) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (!options) options = {};
|
|
|
|
return this.get_url(this.config.url + '/' + name + '/-/' + filename);
|
|
|
|
};
|
2013-06-20 15:41:07 +02:00
|
|
|
|
|
|
|
Storage.prototype.get_url = function(url) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let stream = MyStreams.ReadTarballStream();
|
|
|
|
stream.abort = function() {};
|
|
|
|
let current_length = 0, expected_length;
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let rstream = this.request({
|
2014-11-12 12:14:37 +01:00
|
|
|
uri_full: url,
|
|
|
|
encoding: null,
|
2017-04-23 20:02:26 +02:00
|
|
|
headers: {Accept: 'application/octet-stream'},
|
|
|
|
});
|
2014-11-12 12:14:37 +01:00
|
|
|
|
|
|
|
rstream.on('response', function(res) {
|
|
|
|
if (res.statusCode === 404) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return stream.emit('error', Error[404]('file doesn\'t exist on uplink'));
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
if (!(res.statusCode >= 200 && res.statusCode < 300)) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return stream.emit('error', Error('bad uplink status code: ' + res.statusCode));
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
if (res.headers['content-length']) {
|
2017-04-23 20:02:26 +02:00
|
|
|
expected_length = res.headers['content-length'];
|
|
|
|
stream.emit('content-length', res.headers['content-length']);
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
rstream.pipe(stream);
|
|
|
|
});
|
2014-11-12 12:14:37 +01:00
|
|
|
|
|
|
|
rstream.on('error', function(err) {
|
2017-04-23 20:02:26 +02:00
|
|
|
stream.emit('error', err);
|
|
|
|
});
|
2014-11-12 12:14:37 +01:00
|
|
|
rstream.on('data', function(d) {
|
2017-04-23 20:02:26 +02:00
|
|
|
current_length += d.length;
|
|
|
|
});
|
2014-11-12 12:14:37 +01:00
|
|
|
rstream.on('end', function(d) {
|
2017-04-23 20:02:26 +02:00
|
|
|
if (d) current_length += d.length;
|
2014-11-12 12:14:37 +01:00
|
|
|
if (expected_length && current_length != expected_length)
|
2017-04-23 20:02:26 +02:00
|
|
|
stream.emit('error', Error('content length mismatch'));
|
|
|
|
});
|
|
|
|
return stream;
|
|
|
|
};
|
2013-06-19 18:58:16 +02:00
|
|
|
|
2015-05-17 00:29:16 +02:00
|
|
|
Storage.prototype.search = function(startkey, options) {
|
2017-04-23 20:02:26 +02:00
|
|
|
let self = this;
|
2015-05-17 00:29:16 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let stream = new Stream.PassThrough({objectMode: true});
|
2015-05-17 00:29:16 +02:00
|
|
|
|
2017-04-23 22:28:35 +02:00
|
|
|
let req = self.request({
|
2015-05-17 00:29:16 +02:00
|
|
|
uri: options.req.url,
|
|
|
|
req: options.req,
|
2017-04-21 20:33:55 +02:00
|
|
|
headers: {
|
2017-04-23 20:02:26 +02:00
|
|
|
referer: options.req.headers.referer,
|
|
|
|
},
|
|
|
|
});
|
2015-05-17 00:29:16 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
req.on('response', (res) => {
|
2015-05-17 00:29:16 +02:00
|
|
|
if (!String(res.statusCode).match(/^2\d\d$/)) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return stream.emit('error', Error('bad status code ' + res.statusCode + ' from uplink'));
|
2015-05-17 00:29:16 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
res.pipe(JSONStream.parse('*')).on('data', (pkg) => {
|
2015-05-17 00:29:16 +02:00
|
|
|
if (Utils.is_object(pkg)) {
|
2017-04-23 20:02:26 +02:00
|
|
|
stream.emit('data', pkg);
|
2015-05-17 00:29:16 +02:00
|
|
|
}
|
2017-04-23 20:02:26 +02:00
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
stream.emit('end');
|
|
|
|
});
|
|
|
|
});
|
2015-05-17 00:29:16 +02:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
req.on('error', (err) => {
|
|
|
|
stream.emit('error', err);
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.abort = () => {
|
|
|
|
req.abort();
|
|
|
|
stream.emit('end');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
};
|
2015-05-17 00:29:16 +02:00
|
|
|
|
2013-12-09 04:58:25 +01:00
|
|
|
Storage.prototype._add_proxy_headers = function(req, headers) {
|
2014-11-12 12:14:37 +01:00
|
|
|
if (req) {
|
2015-05-30 17:39:47 +02:00
|
|
|
// Only submit X-Forwarded-For field if we don't have a proxy selected
|
|
|
|
// in the config file.
|
|
|
|
//
|
|
|
|
// Otherwise misconfigured proxy could return 407:
|
|
|
|
// https://github.com/rlidwka/sinopia/issues/254
|
|
|
|
//
|
|
|
|
if (!this.proxy) {
|
|
|
|
headers['X-Forwarded-For'] = (
|
|
|
|
req && req.headers['x-forwarded-for']
|
|
|
|
? req.headers['x-forwarded-for'] + ', '
|
|
|
|
: ''
|
2017-04-23 20:02:26 +02:00
|
|
|
) + req.connection.remoteAddress;
|
2015-05-30 17:39:47 +02:00
|
|
|
}
|
2014-11-12 12:14:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// always attach Via header to avoid loops, even if we're not proxying
|
|
|
|
headers['Via'] =
|
|
|
|
req && req.headers['via']
|
|
|
|
? req.headers['via'] + ', '
|
2017-04-23 20:02:26 +02:00
|
|
|
: '';
|
2014-11-12 12:14:37 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
headers['Via'] += '1.1 ' + this.server_id + ' (Verdaccio)';
|
|
|
|
};
|
2017-04-19 20:45:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = Storage;
|