-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
62 lines (55 loc) · 1.42 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
global.Promise = require('bluebird');
var _ = require('lodash');
var defaultStCacheOptions = {
fd: {
max: 1000,
maxAge: 1000 * 60 * 60,
},
stat: false,
content: {
max: 1024 * 1024 * 64,
maxAge: 1000 * 60 * 10,
cacheControl: 'public, max-age=600'
},
};
exports.defaultStCacheOptions = defaultStCacheOptions;
exports.matchUpstream = matchUpstream;
function matchUpstream(upstreams, url) {
return upstreams.filter(upstream => {
return upstream &&
upstream.urlExp instanceof RegExp &&
upstream.urlExp.exec(url);
})[0];
}
exports.replaceBodyRegistry = replaceBodyRegistry;
function replaceBodyRegistry(replace, body) {
return body.replace(replace[0], replace[1]);
}
exports.xtend = xtend;
function xtend() {
var args = Array.prototype.slice.call(arguments);
args.unshift({});
return _.assign.apply(_, args);
}
exports.sleep = sleep;
function sleep(ms) {
return new Promise(function(resolve) {
setTimeout(resolve, ms);
});
}
exports.pickHeaders = pickHeaders;
function pickHeaders(headers) {
return _.pick(headers, [
'etag',
'content-type',
'content-length',
]);
}
exports.randomInterval = randomInterval;
function randomInterval(config) {
var interval = config.updateInterval || 2000;
return function () {
return interval * (Math.random() * 0.4 + 0.8)
};
}