mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-02-17 23:45:29 -05:00
Migrate storages to classes
This commit is contained in:
parent
e267ef1f54
commit
20f9436672
4 changed files with 1224 additions and 1081 deletions
|
@ -11,10 +11,10 @@ var Storage = require('./storage')
|
||||||
module.exports = function(config_hash) {
|
module.exports = function(config_hash) {
|
||||||
Logger.setup(config_hash.logs)
|
Logger.setup(config_hash.logs)
|
||||||
|
|
||||||
var config = Config(config_hash)
|
var config = Config(config_hash);
|
||||||
var storage = Storage(config)
|
var storage = new Storage(config);
|
||||||
var auth = Auth(config)
|
var auth = Auth(config);
|
||||||
var app = express()
|
var app = express();
|
||||||
|
|
||||||
// run in production mode by default, just in case
|
// run in production mode by default, just in case
|
||||||
// it shouldn't make any difference anyway
|
// it shouldn't make any difference anyway
|
||||||
|
|
1242
lib/local-storage.js
1242
lib/local-storage.js
File diff suppressed because it is too large
Load diff
944
lib/storage.js
944
lib/storage.js
File diff suppressed because it is too large
Load diff
|
@ -1,59 +1,19 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var JSONStream = require('JSONStream')
|
const JSONStream = require('JSONStream')
|
||||||
var Error = require('http-errors')
|
const Error = require('http-errors')
|
||||||
var request = require('request')
|
const request = require('request')
|
||||||
var Stream = require('readable-stream')
|
const Stream = require('readable-stream')
|
||||||
var URL = require('url')
|
const URL = require('url')
|
||||||
var parse_interval = require('./config').parse_interval
|
const parse_interval = require('./config').parse_interval
|
||||||
var Logger = require('./logger')
|
const Logger = require('./logger')
|
||||||
var MyStreams = require('./streams')
|
const MyStreams = require('./streams')
|
||||||
var Utils = require('./utils')
|
const Utils = require('./utils')
|
||||||
var encode = function(thing) {
|
const encode = function(thing) {
|
||||||
return encodeURIComponent(thing).replace(/^%40/, '@');
|
return encodeURIComponent(thing).replace(/^%40/, '@');
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = Storage
|
const _setupProxy = function(hostname, config, mainconfig, isHTTPS) {
|
||||||
|
|
||||||
//
|
|
||||||
// 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) {
|
|
||||||
var no_proxy
|
var no_proxy
|
||||||
var proxy_key = isHTTPS ? 'https_proxy' : 'http_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) {
|
Storage.prototype.request = function(options, cb) {
|
||||||
if (!this.status_check()) {
|
if (!this.status_check()) {
|
||||||
var req = new Stream.Readable()
|
var req = new Stream.Readable()
|
||||||
|
@ -396,3 +402,6 @@ Storage.prototype._add_proxy_headers = function(req, headers) {
|
||||||
|
|
||||||
headers['Via'] += '1.1 ' + this.server_id + ' (Verdaccio)'
|
headers['Via'] += '1.1 ' + this.server_id + ' (Verdaccio)'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = Storage;
|
||||||
|
|
Loading…
Add table
Reference in a new issue