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

Migrate storages to classes

This commit is contained in:
Juan Picado 2017-04-19 20:45:48 +02:00
parent d79f12d45a
commit 7970e52068
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
4 changed files with 1224 additions and 1049 deletions

@ -11,10 +11,10 @@ var Storage = require('./storage')
module.exports = function(config_hash) {
Logger.setup(config_hash.logs)
var config = Config(config_hash)
var storage = Storage(config)
var auth = Auth(config)
var app = express()
var config = Config(config_hash);
var storage = new Storage(config);
var auth = Auth(config);
var app = express();
// run in production mode by default, just in case
// it shouldn't make any difference anyway

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,59 +1,19 @@
"use strict";
var JSONStream = require('JSONStream')
var Error = require('http-errors')
var request = require('request')
var Stream = require('readable-stream')
var URL = require('url')
var parse_interval = require('./config').parse_interval
var Logger = require('./logger')
var MyStreams = require('./streams')
var Utils = require('./utils')
var encode = function(thing) {
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) {
return encodeURIComponent(thing).replace(/^%40/, '@');
};
module.exports = Storage
//
// Implements Storage interface
// (same for storage.js, local-storage.js, up-storage.js)
//
function Storage(config, mainconfig) {
var self = Object.create(Storage.prototype)
self.config = config
self.failed_requests = 0
self.userAgent = mainconfig.user_agent
self.ca = config.ca
self.logger = Logger.logger.child({sub: 'out'})
self.server_id = mainconfig.server_id
self.url = URL.parse(self.config.url)
_setupProxy.call(self, self.url.hostname, config, mainconfig, self.url.protocol === 'https:')
self.config.url = self.config.url.replace(/\/$/, '')
if (Number(self.config.timeout) >= 1000) {
self.logger.warn([ 'Too big timeout value: ' + self.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
self.maxage = parse_interval(config_get('maxage' , '2m' ))
self.timeout = parse_interval(config_get('timeout' , '30s'))
self.max_fails = Number(config_get('max_fails' , 2 ))
self.fail_timeout = parse_interval(config_get('fail_timeout', '5m' ))
return self
// just a helper (`config[key] || default` doesn't work because of zeroes)
function config_get(key, def) {
return config[key] != null ? config[key] : def
}
}
function _setupProxy(hostname, config, mainconfig, isHTTPS) {
const _setupProxy = function(hostname, config, mainconfig, isHTTPS) {
var no_proxy
var proxy_key = isHTTPS ? 'https_proxy' : 'http_proxy'
@ -100,6 +60,52 @@ function _setupProxy(hostname, config, mainconfig, isHTTPS) {
}
}
//
// Implements Storage interface
// (same for storage.js, local-storage.js, up-storage.js)
//
class Storage {
/**
*
* @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)
_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) {
if (!this.status_check()) {
var req = new Stream.Readable()
@ -399,3 +405,6 @@ Storage.prototype._add_proxy_headers = function(req, headers) {
headers['Via'] += '1.1 ' + this.server_id + ' (Verdaccio)'
}
module.exports = Storage;