mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-02-17 23:45:29 -05:00
change interval formatting in config
All intervals are now in milliseconds. But you can add multiples ("ms", "s", "m", "h", "d", "M", "y") to set value using different units. For example, value "1.5h" would mean 1.5 hours.
This commit is contained in:
parent
9ff1203688
commit
6b9001ef6c
4 changed files with 49 additions and 9 deletions
|
@ -151,3 +151,21 @@ Config.prototype.authenticate = function(user, password) {
|
|||
|
||||
module.exports = Config
|
||||
|
||||
var parse_interval_table = {
|
||||
'': 1,
|
||||
ms: 1,
|
||||
s: 1000,
|
||||
m: 60*1000,
|
||||
h: 60*60*1000,
|
||||
d: 86400000,
|
||||
M: 30*86400000,
|
||||
y: 365.25*30*86400000,
|
||||
}
|
||||
|
||||
module.exports.parse_interval = function(interval) {
|
||||
if (typeof(interval) === 'number') return interval
|
||||
var m = interval.match(/^((0|[1-9][0-9]*)(\.[0-9]+)?)(ms|s|m|h|d|M|y|)$/)
|
||||
if (!m) throw new Error('invalid interval: ' + interval)
|
||||
return Number(m[1]) * parse_interval_table[m[4]]
|
||||
}
|
||||
|
||||
|
|
|
@ -12,15 +12,15 @@ uplinks:
|
|||
npmjs:
|
||||
url: https://registry.npmjs.org/
|
||||
|
||||
# amount of time (in milliseconds) to wait for repository to respond
|
||||
# amount of time to wait for repository to respond
|
||||
# before giving up and use the local cached copy
|
||||
#timeout: 30000
|
||||
#timeout: 30s
|
||||
|
||||
# maximum time (in seconds) in which data is considered up to date
|
||||
# maximum time in which data is considered up to date
|
||||
#
|
||||
# default is 2 minutes, so server won't request the same data from
|
||||
# uplink if a similar request was made less than 2 minutes ago
|
||||
#maxage: 120
|
||||
#maxage: 2m
|
||||
|
||||
packages:
|
||||
# uncomment this for packages with "local-" prefix to be available
|
||||
|
|
|
@ -5,6 +5,7 @@ var URL = require('url')
|
|||
, mystreams = require('./streams')
|
||||
, Logger = require('./logger')
|
||||
, utils = require('./utils')
|
||||
, parse_interval = require('./config').parse_interval
|
||||
, encode = encodeURIComponent
|
||||
|
||||
//
|
||||
|
@ -20,7 +21,6 @@ function Storage(config, mainconfig) {
|
|||
this.logger = Logger.logger.child({sub: 'out'})
|
||||
this.server_id = mainconfig.server_id
|
||||
|
||||
this.maxage = (parseInt(this.config.maxage, 10) || 0) * 1000
|
||||
this.url = URL.parse(this.config.url)
|
||||
if (this.url.hostname === 'registry.npmjs.org') {
|
||||
// npm registry is too slow working with ssl :(
|
||||
|
@ -34,9 +34,10 @@ function Storage(config, mainconfig) {
|
|||
_setupProxy.call(this, this.url.hostname, config, mainconfig, this.url.protocol === 'https:')
|
||||
|
||||
this.config.url = this.config.url.replace(/\/$/, '')
|
||||
if (isNaN(parseFloat(this.config.timeout)) || !isFinite(this.config.timeout)) {
|
||||
this.config.timeout = 30000
|
||||
}
|
||||
this.maxage = parse_interval(this.config.maxage || '2m')
|
||||
this.timeout = parse_interval(this.config.timeout || '30s')
|
||||
this.max_fails = Number(this.config.max_fails) || 2
|
||||
this.fail_timeout = parse_interval(this.config.fail_timeout || '5m')
|
||||
return this
|
||||
}
|
||||
|
||||
|
@ -122,7 +123,7 @@ Storage.prototype.request = function(options, cb) {
|
|||
body: json,
|
||||
ca: this.ca,
|
||||
proxy: this.proxy,
|
||||
timeout: this.config.timeout
|
||||
timeout: this.timeout
|
||||
}, function(err, res, body) {
|
||||
var error
|
||||
if (!err) {
|
||||
|
|
21
test/unit/parse_interval.js
Normal file
21
test/unit/parse_interval.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
var assert = require('assert')
|
||||
, parse_interval = require('../../lib/config').parse_interval
|
||||
|
||||
describe('Parse interval', function() {
|
||||
function add_test(str, res) {
|
||||
it('parse ' + str, function() {
|
||||
assert.strictEqual(parse_interval(str), res)
|
||||
})
|
||||
}
|
||||
|
||||
add_test(12345, 12345)
|
||||
add_test('1000', 1000)
|
||||
add_test('1.5s', 1500)
|
||||
add_test('25ms', 25)
|
||||
add_test('2m', 2*1000*60)
|
||||
add_test('3h', 3*1000*60*60)
|
||||
add_test('0.5d', 0.5*1000*60*60*24)
|
||||
add_test('1M', 1000*60*60*24*30)
|
||||
add_test('1y', 1000*60*60*24*30*365.25)
|
||||
})
|
||||
|
Loading…
Add table
Reference in a new issue