@modern-js/prod-server 2.0.0-beta.3 → 2.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +69 -0
- package/dist/js/modern/constants.js +32 -25
- package/dist/js/modern/index.js +11 -6
- package/dist/js/modern/libs/context/context.js +52 -84
- package/dist/js/modern/libs/context/index.js +5 -2
- package/dist/js/modern/libs/hook-api/index.js +42 -35
- package/dist/js/modern/libs/hook-api/route.js +6 -7
- package/dist/js/modern/libs/hook-api/template.js +20 -34
- package/dist/js/modern/libs/loadConfig.js +45 -24
- package/dist/js/modern/libs/metrics.js +3 -4
- package/dist/js/modern/libs/proxy.js +68 -37
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -67
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -216
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
- package/dist/js/modern/libs/render/cache/index.js +88 -54
- package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -6
- package/dist/js/modern/libs/render/cache/spr.js +133 -117
- package/dist/js/modern/libs/render/cache/type.js +0 -1
- package/dist/js/modern/libs/render/cache/util.js +71 -39
- package/dist/js/modern/libs/render/index.js +76 -56
- package/dist/js/modern/libs/render/measure.js +38 -27
- package/dist/js/modern/libs/render/reader.js +65 -62
- package/dist/js/modern/libs/render/ssr.js +50 -32
- package/dist/js/modern/libs/render/static.js +50 -33
- package/dist/js/modern/libs/render/type.js +9 -6
- package/dist/js/modern/libs/route/index.js +8 -15
- package/dist/js/modern/libs/route/matcher.js +20 -34
- package/dist/js/modern/libs/route/route.js +9 -18
- package/dist/js/modern/libs/serve-file.js +33 -20
- package/dist/js/modern/server/index.js +144 -145
- package/dist/js/modern/server/modern-server-split.js +46 -12
- package/dist/js/modern/server/modern-server.js +377 -417
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +62 -43
- package/dist/js/modern/worker-server.js +34 -14
- package/dist/js/node/constants.js +45 -30
- package/dist/js/node/index.js +31 -57
- package/dist/js/node/libs/context/context.js +79 -94
- package/dist/js/node/libs/context/index.js +23 -13
- package/dist/js/node/libs/hook-api/index.js +69 -48
- package/dist/js/node/libs/hook-api/route.js +22 -11
- package/dist/js/node/libs/hook-api/template.js +36 -39
- package/dist/js/node/libs/loadConfig.js +69 -35
- package/dist/js/node/libs/metrics.js +21 -9
- package/dist/js/node/libs/proxy.js +86 -44
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +132 -70
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +268 -218
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +61 -55
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +54 -40
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +101 -119
- package/dist/js/node/libs/render/cache/index.js +110 -64
- package/dist/js/node/libs/render/cache/page-caches/index.js +50 -14
- package/dist/js/node/libs/render/cache/page-caches/lru.js +29 -12
- package/dist/js/node/libs/render/cache/spr.js +156 -129
- package/dist/js/node/libs/render/cache/type.js +0 -5
- package/dist/js/node/libs/render/cache/util.js +88 -45
- package/dist/js/node/libs/render/index.js +102 -67
- package/dist/js/node/libs/render/measure.js +55 -31
- package/dist/js/node/libs/render/reader.js +87 -70
- package/dist/js/node/libs/render/ssr.js +76 -47
- package/dist/js/node/libs/render/static.js +75 -40
- package/dist/js/node/libs/render/type.js +27 -12
- package/dist/js/node/libs/route/index.js +26 -26
- package/dist/js/node/libs/route/matcher.js +36 -41
- package/dist/js/node/libs/route/route.js +25 -22
- package/dist/js/node/libs/serve-file.js +61 -32
- package/dist/js/node/server/index.js +160 -160
- package/dist/js/node/server/modern-server-split.js +68 -22
- package/dist/js/node/server/modern-server.js +395 -441
- package/dist/js/node/type.js +0 -3
- package/dist/js/node/utils.js +74 -52
- package/dist/js/node/worker-server.js +53 -21
- package/dist/js/treeshaking/constants.js +26 -25
- package/dist/js/treeshaking/index.js +10 -10
- package/dist/js/treeshaking/libs/context/context.js +268 -237
- package/dist/js/treeshaking/libs/context/index.js +3 -3
- package/dist/js/treeshaking/libs/hook-api/index.js +265 -143
- package/dist/js/treeshaking/libs/hook-api/route.js +65 -30
- package/dist/js/treeshaking/libs/hook-api/template.js +121 -85
- package/dist/js/treeshaking/libs/loadConfig.js +80 -37
- package/dist/js/treeshaking/libs/metrics.js +4 -10
- package/dist/js/treeshaking/libs/proxy.js +240 -76
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +288 -121
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +772 -455
- package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +65 -51
- package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -35
- package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +144 -118
- package/dist/js/treeshaking/libs/render/cache/index.js +337 -175
- package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +151 -27
- package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +80 -42
- package/dist/js/treeshaking/libs/render/cache/spr.js +470 -340
- package/dist/js/treeshaking/libs/render/cache/type.js +1 -1
- package/dist/js/treeshaking/libs/render/cache/util.js +271 -92
- package/dist/js/treeshaking/libs/render/index.js +228 -95
- package/dist/js/treeshaking/libs/render/measure.js +142 -57
- package/dist/js/treeshaking/libs/render/reader.js +325 -177
- package/dist/js/treeshaking/libs/render/ssr.js +220 -95
- package/dist/js/treeshaking/libs/render/static.js +210 -78
- package/dist/js/treeshaking/libs/render/type.js +7 -6
- package/dist/js/treeshaking/libs/route/index.js +125 -89
- package/dist/js/treeshaking/libs/route/matcher.js +132 -107
- package/dist/js/treeshaking/libs/route/route.js +40 -26
- package/dist/js/treeshaking/libs/serve-file.js +177 -68
- package/dist/js/treeshaking/server/index.js +468 -327
- package/dist/js/treeshaking/server/modern-server-split.js +352 -144
- package/dist/js/treeshaking/server/modern-server.js +1046 -909
- package/dist/js/treeshaking/type.js +1 -1
- package/dist/js/treeshaking/utils.js +138 -81
- package/dist/js/treeshaking/worker-server.js +176 -55
- package/dist/types/index.d.ts +2 -0
- package/dist/types/libs/context/context.d.ts +4 -1
- package/dist/types/libs/loadConfig.d.ts +1 -0
- package/dist/types/libs/render/cache/index.d.ts +2 -0
- package/dist/types/libs/render/cache/spr.d.ts +2 -0
- package/dist/types/libs/route/route.d.ts +0 -1
- package/dist/types/server/index.d.ts +3 -0
- package/dist/types/utils.d.ts +1 -1
- package/dist/types/worker-server.d.ts +1 -2
- package/package.json +7 -14
- package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
- package/dist/js/modern/libs/render/modern/index.js +0 -37
- package/dist/js/node/libs/render/modern/browser-list.js +0 -14
- package/dist/js/node/libs/render/modern/index.js +0 -46
- package/dist/js/treeshaking/libs/render/modern/browser-list.js +0 -7
- package/dist/js/treeshaking/libs/render/modern/index.js +0 -39
- package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
- package/dist/types/libs/render/modern/index.d.ts +0 -3
|
@@ -1,13 +1,162 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
+
try {
|
|
3
|
+
var info = gen[key](arg);
|
|
4
|
+
var value = info.value;
|
|
5
|
+
} catch (error) {
|
|
6
|
+
reject(error);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (info.done) {
|
|
10
|
+
resolve(value);
|
|
11
|
+
} else {
|
|
12
|
+
Promise.resolve(value).then(_next, _throw);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function _asyncToGenerator(fn) {
|
|
16
|
+
return function() {
|
|
17
|
+
var self = this, args = arguments;
|
|
18
|
+
return new Promise(function(resolve, reject) {
|
|
19
|
+
var gen = fn.apply(self, args);
|
|
20
|
+
function _next(value) {
|
|
21
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
+
}
|
|
23
|
+
function _throw(err) {
|
|
24
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
+
}
|
|
26
|
+
_next(undefined);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function _classCallCheck(instance, Constructor) {
|
|
31
|
+
if (!(instance instanceof Constructor)) {
|
|
32
|
+
throw new TypeError("Cannot call a class as a function");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function _defineProperties(target, props) {
|
|
36
|
+
for(var i = 0; i < props.length; i++){
|
|
37
|
+
var descriptor = props[i];
|
|
38
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
39
|
+
descriptor.configurable = true;
|
|
40
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
41
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
45
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
46
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
47
|
+
return Constructor;
|
|
48
|
+
}
|
|
49
|
+
function _defineProperty(obj, key, value) {
|
|
50
|
+
if (key in obj) {
|
|
51
|
+
Object.defineProperty(obj, key, {
|
|
52
|
+
value: value,
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
obj[key] = value;
|
|
59
|
+
}
|
|
60
|
+
return obj;
|
|
61
|
+
}
|
|
62
|
+
var __generator = this && this.__generator || function(thisArg, body) {
|
|
63
|
+
var f, y, t, g, _ = {
|
|
64
|
+
label: 0,
|
|
65
|
+
sent: function() {
|
|
66
|
+
if (t[0] & 1) throw t[1];
|
|
67
|
+
return t[1];
|
|
68
|
+
},
|
|
69
|
+
trys: [],
|
|
70
|
+
ops: []
|
|
71
|
+
};
|
|
72
|
+
return(g = {
|
|
73
|
+
next: verb(0),
|
|
74
|
+
"throw": verb(1),
|
|
75
|
+
"return": verb(2)
|
|
76
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
77
|
+
return this;
|
|
78
|
+
}), g);
|
|
79
|
+
function verb(n) {
|
|
80
|
+
return function(v) {
|
|
81
|
+
return step([
|
|
82
|
+
n,
|
|
83
|
+
v
|
|
84
|
+
]);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function step(op) {
|
|
88
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
89
|
+
while(_)try {
|
|
90
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
91
|
+
if (y = 0, t) op = [
|
|
92
|
+
op[0] & 2,
|
|
93
|
+
t.value
|
|
94
|
+
];
|
|
95
|
+
switch(op[0]){
|
|
96
|
+
case 0:
|
|
97
|
+
case 1:
|
|
98
|
+
t = op;
|
|
99
|
+
break;
|
|
100
|
+
case 4:
|
|
101
|
+
_.label++;
|
|
102
|
+
return {
|
|
103
|
+
value: op[1],
|
|
104
|
+
done: false
|
|
105
|
+
};
|
|
106
|
+
case 5:
|
|
107
|
+
_.label++;
|
|
108
|
+
y = op[1];
|
|
109
|
+
op = [
|
|
110
|
+
0
|
|
111
|
+
];
|
|
112
|
+
continue;
|
|
113
|
+
case 7:
|
|
114
|
+
op = _.ops.pop();
|
|
115
|
+
_.trys.pop();
|
|
116
|
+
continue;
|
|
117
|
+
default:
|
|
118
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
119
|
+
_ = 0;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
123
|
+
_.label = op[1];
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
127
|
+
_.label = t[1];
|
|
128
|
+
t = op;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
if (t && _.label < t[2]) {
|
|
132
|
+
_.label = t[2];
|
|
133
|
+
_.ops.push(op);
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
if (t[2]) _.ops.pop();
|
|
137
|
+
_.trys.pop();
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
op = body.call(thisArg, _);
|
|
141
|
+
} catch (e) {
|
|
142
|
+
op = [
|
|
143
|
+
6,
|
|
144
|
+
e
|
|
145
|
+
];
|
|
146
|
+
y = 0;
|
|
147
|
+
} finally{
|
|
148
|
+
f = t = 0;
|
|
149
|
+
}
|
|
150
|
+
if (op[0] & 5) throw op[1];
|
|
151
|
+
return {
|
|
152
|
+
value: op[0] ? op[1] : void 0,
|
|
153
|
+
done: true
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
import crypto from "crypto";
|
|
158
|
+
import LRUCache from "lru-cache";
|
|
159
|
+
import { mime } from "@modern-js/utils";
|
|
11
160
|
import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "./util";
|
|
12
161
|
import { createPageCaches } from "./page-caches";
|
|
13
162
|
var MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
|
|
@@ -16,347 +165,328 @@ var BASE_LEVEL = 0;
|
|
|
16
165
|
var QUERY_LEVEL = 1;
|
|
17
166
|
var HEADER_LEVEL = 2;
|
|
18
167
|
var QUERY_HEADER_LEVEL = 3;
|
|
19
|
-
var CacheManager = /*#__PURE__*/function
|
|
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
|
-
|
|
63
|
-
return total + (((_n$caches$peek = n.caches.peek(cur)) === null || _n$caches$peek === void 0 ? void 0 : _n$caches$peek.size) || 0);
|
|
64
|
-
}, 1);
|
|
65
|
-
return len;
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
_createClass(CacheManager, [{
|
|
70
|
-
key: "md5",
|
|
71
|
-
value: function md5(content) {
|
|
72
|
-
var md5 = crypto.createHash('md5');
|
|
73
|
-
return md5.update(content).digest('hex');
|
|
74
|
-
}
|
|
75
|
-
}, {
|
|
76
|
-
key: "generateRequestKey",
|
|
77
|
-
value: function generateRequestKey(context) {
|
|
78
|
-
var pathname = context.pathname,
|
|
79
|
-
entry = context.entry;
|
|
80
|
-
return this.md5("".concat(pathname, "_").concat(entry));
|
|
81
|
-
}
|
|
82
|
-
}, {
|
|
83
|
-
key: "replaceValue",
|
|
84
|
-
value: function replaceValue(value, matcher) {
|
|
85
|
-
var _final = value;
|
|
86
|
-
Object.keys(matcher).some(function (replacer) {
|
|
87
|
-
var reg = new RegExp(matcher[replacer]);
|
|
88
|
-
if (reg.test(value)) {
|
|
89
|
-
_final = replacer;
|
|
90
|
-
return true;
|
|
91
|
-
}
|
|
92
|
-
return false;
|
|
93
|
-
});
|
|
94
|
-
return _final;
|
|
95
|
-
}
|
|
96
|
-
}, {
|
|
97
|
-
key: "factor",
|
|
98
|
-
value: function factor(keys, obj) {
|
|
99
|
-
var _this3 = this;
|
|
100
|
-
var matches = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
101
|
-
keys.sort();
|
|
102
|
-
var getValue = valueFactory(obj);
|
|
103
|
-
var factorAry = keys.reduce(function (ary, key) {
|
|
104
|
-
var value = getValue(key) || '';
|
|
105
|
-
var matcher = matches[key];
|
|
106
|
-
if (matcher) {
|
|
107
|
-
value = _this3.replaceValue(value, matcher);
|
|
108
|
-
}
|
|
109
|
-
return ary.concat([key, value]);
|
|
110
|
-
}, []);
|
|
111
|
-
return factorAry.join(',');
|
|
112
|
-
}
|
|
113
|
-
}, {
|
|
114
|
-
key: "queryFactor",
|
|
115
|
-
value: function queryFactor(context, data) {
|
|
116
|
-
var _data$includes, _data$matches;
|
|
117
|
-
var queryKeys = (_data$includes = data.includes) === null || _data$includes === void 0 ? void 0 : _data$includes.query;
|
|
118
|
-
var queryMatches = (_data$matches = data.matches) === null || _data$matches === void 0 ? void 0 : _data$matches.query;
|
|
119
|
-
if (!queryKeys || queryKeys.length === 0) {
|
|
120
|
-
return null;
|
|
121
|
-
}
|
|
122
|
-
var requestQuery = context.query;
|
|
123
|
-
var queryFactor = this.factor(queryKeys, requestQuery, queryMatches);
|
|
124
|
-
return queryFactor;
|
|
125
|
-
}
|
|
126
|
-
}, {
|
|
127
|
-
key: "headerFactor",
|
|
128
|
-
value: function headerFactor(context, data) {
|
|
129
|
-
var _data$includes2, _data$matches2;
|
|
130
|
-
var headerKeys = (_data$includes2 = data.includes) === null || _data$includes2 === void 0 ? void 0 : _data$includes2.header;
|
|
131
|
-
var headerMatches = (_data$matches2 = data.matches) === null || _data$matches2 === void 0 ? void 0 : _data$matches2.header;
|
|
132
|
-
if (!headerKeys || headerKeys.length === 0) {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
var requestHeader = context.headers;
|
|
136
|
-
var headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
|
|
137
|
-
return headerFactor;
|
|
138
|
-
}
|
|
139
|
-
}, {
|
|
140
|
-
key: "best",
|
|
141
|
-
value: function () {
|
|
142
|
-
var _best = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(context, cacheKey, data) {
|
|
143
|
-
var level, cacheHash;
|
|
144
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
145
|
-
while (1) {
|
|
146
|
-
switch (_context.prev = _context.next) {
|
|
147
|
-
case 0:
|
|
148
|
-
level = data.level;
|
|
149
|
-
cacheHash = this.find[fname(level)](context, cacheKey, data);
|
|
150
|
-
if (cacheHash) {
|
|
151
|
-
_context.next = 4;
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
return _context.abrupt("return", null);
|
|
155
|
-
case 4:
|
|
156
|
-
return _context.abrupt("return", data.caches.get(cacheHash));
|
|
157
|
-
case 5:
|
|
158
|
-
case "end":
|
|
159
|
-
return _context.stop();
|
|
168
|
+
var CacheManager = /*#__PURE__*/ function() {
|
|
169
|
+
"use strict";
|
|
170
|
+
function CacheManager(cacheOptions) {
|
|
171
|
+
var _this = this;
|
|
172
|
+
_classCallCheck(this, CacheManager);
|
|
173
|
+
_defineProperty(this, "cache", void 0);
|
|
174
|
+
_defineProperty(this, "cacheOptions", void 0);
|
|
175
|
+
_defineProperty(this, "find", function() {
|
|
176
|
+
{
|
|
177
|
+
var _this1 = _this;
|
|
178
|
+
var _obj;
|
|
179
|
+
return(_obj = {}, _defineProperty(_obj, fname(BASE_LEVEL), function(context, cacheKey) {
|
|
180
|
+
return _this1.md5(cacheKey);
|
|
181
|
+
}), _defineProperty(_obj, fname(QUERY_LEVEL), function(context, cacheKey, data) {
|
|
182
|
+
var queryFactor = _this1.queryFactor(context, data);
|
|
183
|
+
if (!queryFactor) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
return _this1.md5(connectFactor(cacheKey, queryFactor));
|
|
187
|
+
}), _defineProperty(_obj, fname(HEADER_LEVEL), function(context, cacheKey, data) {
|
|
188
|
+
var headerFactor = _this1.headerFactor(context, data);
|
|
189
|
+
if (!headerFactor) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
return _this1.md5(connectFactor(cacheKey, headerFactor));
|
|
193
|
+
}), _defineProperty(_obj, fname(QUERY_HEADER_LEVEL), function(context, cacheKey, data) {
|
|
194
|
+
var queryFactor = _this1.queryFactor(context, data);
|
|
195
|
+
var headerFactor = _this1.headerFactor(context, data);
|
|
196
|
+
if (!queryFactor || !headerFactor) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
return _this1.md5(connectFactor(cacheKey, headerFactor, queryFactor));
|
|
200
|
+
}), _obj);
|
|
201
|
+
}
|
|
202
|
+
}());
|
|
203
|
+
this.cacheOptions = cacheOptions;
|
|
204
|
+
this.cache = new LRUCache({
|
|
205
|
+
max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
|
|
206
|
+
length: function length(n) {
|
|
207
|
+
var len = n.caches.keys().reduce(function(total, cur) {
|
|
208
|
+
var ref;
|
|
209
|
+
return total + (((ref = n.caches.peek(cur)) === null || ref === void 0 ? void 0 : ref.size) || 0);
|
|
210
|
+
}, 1);
|
|
211
|
+
return len;
|
|
160
212
|
}
|
|
161
|
-
|
|
162
|
-
}, _callee, this);
|
|
163
|
-
}));
|
|
164
|
-
function best(_x, _x2, _x3) {
|
|
165
|
-
return _best.apply(this, arguments);
|
|
166
|
-
}
|
|
167
|
-
return best;
|
|
168
|
-
}()
|
|
169
|
-
}, {
|
|
170
|
-
key: "createCacheContent",
|
|
171
|
-
value: function createCacheContent(config, caches) {
|
|
172
|
-
return {
|
|
173
|
-
level: config.level,
|
|
174
|
-
interval: config.interval,
|
|
175
|
-
includes: config.includes || null,
|
|
176
|
-
limit: config.staleLimit,
|
|
177
|
-
matches: config.matches || null,
|
|
178
|
-
caches: caches
|
|
179
|
-
};
|
|
213
|
+
});
|
|
180
214
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
case 9:
|
|
208
|
-
expireTime = dest.expireTime, limitTime = dest.limitTime, html = dest.html, cacheHash = dest.cacheHash;
|
|
209
|
-
isStale = Date.now() - expireTime > 0;
|
|
210
|
-
isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
|
|
211
|
-
return _context2.abrupt("return", {
|
|
212
|
-
content: html || '',
|
|
213
|
-
contentType: mime.contentType('html'),
|
|
214
|
-
isStale: isStale,
|
|
215
|
-
isGarbage: isGarbage,
|
|
216
|
-
hash: cacheHash
|
|
215
|
+
_createClass(CacheManager, [
|
|
216
|
+
{
|
|
217
|
+
key: "md5",
|
|
218
|
+
value: function md5(content) {
|
|
219
|
+
var md5 = crypto.createHash("md5");
|
|
220
|
+
return md5.update(content).digest("hex");
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
key: "generateRequestKey",
|
|
225
|
+
value: function generateRequestKey(context) {
|
|
226
|
+
var pathname = context.pathname, entry = context.entry;
|
|
227
|
+
return this.md5("".concat(pathname, "_").concat(entry));
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
key: "replaceValue",
|
|
232
|
+
value: function replaceValue(value, matcher) {
|
|
233
|
+
var final = value;
|
|
234
|
+
Object.keys(matcher).some(function(replacer) {
|
|
235
|
+
var reg = new RegExp(matcher[replacer]);
|
|
236
|
+
if (reg.test(value)) {
|
|
237
|
+
final = replacer;
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
return false;
|
|
217
241
|
});
|
|
218
|
-
|
|
219
|
-
case "end":
|
|
220
|
-
return _context2.stop();
|
|
242
|
+
return final;
|
|
221
243
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
return _context4.abrupt("return", false);
|
|
253
|
-
case 3:
|
|
254
|
-
// each version with route is a separate cache
|
|
255
|
-
cacheKey = this.generateRequestKey(context);
|
|
256
|
-
data = this.cache.get(cacheKey);
|
|
257
|
-
if (data) {
|
|
258
|
-
_context4.next = 10;
|
|
259
|
-
break;
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
key: "factor",
|
|
247
|
+
value: function factor(keys, obj) {
|
|
248
|
+
var matches = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
249
|
+
var _this = this;
|
|
250
|
+
keys.sort();
|
|
251
|
+
var getValue = valueFactory(obj);
|
|
252
|
+
var factorAry = keys.reduce(function(ary, key) {
|
|
253
|
+
var value = getValue(key) || "";
|
|
254
|
+
var matcher = matches[key];
|
|
255
|
+
if (matcher) {
|
|
256
|
+
value = _this.replaceValue(value, matcher);
|
|
257
|
+
}
|
|
258
|
+
return ary.concat([
|
|
259
|
+
key,
|
|
260
|
+
value
|
|
261
|
+
]);
|
|
262
|
+
}, []);
|
|
263
|
+
return factorAry.join(",");
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
key: "queryFactor",
|
|
268
|
+
value: function queryFactor(context, data) {
|
|
269
|
+
var ref, ref1;
|
|
270
|
+
var queryKeys = (ref = data.includes) === null || ref === void 0 ? void 0 : ref.query;
|
|
271
|
+
var queryMatches = (ref1 = data.matches) === null || ref1 === void 0 ? void 0 : ref1.query;
|
|
272
|
+
if (!queryKeys || queryKeys.length === 0) {
|
|
273
|
+
return null;
|
|
260
274
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
275
|
+
var requestQuery = context.query;
|
|
276
|
+
var queryFactor = this.factor(queryKeys, requestQuery, queryMatches);
|
|
277
|
+
return queryFactor;
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
key: "headerFactor",
|
|
282
|
+
value: function headerFactor(context, data) {
|
|
283
|
+
var ref, ref1;
|
|
284
|
+
var headerKeys = (ref = data.includes) === null || ref === void 0 ? void 0 : ref.header;
|
|
285
|
+
var headerMatches = (ref1 = data.matches) === null || ref1 === void 0 ? void 0 : ref1.header;
|
|
286
|
+
if (!headerKeys || headerKeys.length === 0) {
|
|
287
|
+
return null;
|
|
271
288
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
size: size
|
|
292
|
-
});
|
|
293
|
-
case 6:
|
|
294
|
-
_this4.cache.set(cacheKey, next);
|
|
295
|
-
return _context3.abrupt("return", true);
|
|
296
|
-
case 8:
|
|
297
|
-
case "end":
|
|
298
|
-
return _context3.stop();
|
|
289
|
+
var requestHeader = context.headers;
|
|
290
|
+
var headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
|
|
291
|
+
return headerFactor;
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
{
|
|
295
|
+
key: "best",
|
|
296
|
+
value: function best(context, cacheKey, data) {
|
|
297
|
+
var _this = this;
|
|
298
|
+
return _asyncToGenerator(function() {
|
|
299
|
+
var level, cacheHash;
|
|
300
|
+
return __generator(this, function(_state) {
|
|
301
|
+
level = data.level;
|
|
302
|
+
cacheHash = _this.find[fname(level)](context, cacheKey, data);
|
|
303
|
+
if (!cacheHash) {
|
|
304
|
+
return [
|
|
305
|
+
2,
|
|
306
|
+
null
|
|
307
|
+
];
|
|
299
308
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
}(); // cache set is async, each hash is cached only once at the same time
|
|
307
|
-
doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(null, namespaceHash('stream', cacheHash), []);
|
|
308
|
-
return _context4.abrupt("return", maybeSync(doCache)(sync));
|
|
309
|
-
case 16:
|
|
310
|
-
case "end":
|
|
311
|
-
return _context4.stop();
|
|
309
|
+
return [
|
|
310
|
+
2,
|
|
311
|
+
data.caches.get(cacheHash)
|
|
312
|
+
];
|
|
313
|
+
});
|
|
314
|
+
})();
|
|
312
315
|
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
var cacheKey, data;
|
|
326
|
-
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
327
|
-
while (1) {
|
|
328
|
-
switch (_context5.prev = _context5.next) {
|
|
329
|
-
case 0:
|
|
330
|
-
cacheKey = this.generateRequestKey(context);
|
|
331
|
-
data = this.cache.get(cacheKey);
|
|
332
|
-
data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
|
|
333
|
-
case 3:
|
|
334
|
-
case "end":
|
|
335
|
-
return _context5.stop();
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
key: "createCacheContent",
|
|
319
|
+
value: function createCacheContent(config, caches) {
|
|
320
|
+
return {
|
|
321
|
+
level: config.level,
|
|
322
|
+
interval: config.interval,
|
|
323
|
+
includes: config.includes || null,
|
|
324
|
+
limit: config.staleLimit,
|
|
325
|
+
matches: config.matches || null,
|
|
326
|
+
caches: caches
|
|
327
|
+
};
|
|
336
328
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
key: "get",
|
|
332
|
+
value: function get(context) {
|
|
333
|
+
var _this = this;
|
|
334
|
+
return _asyncToGenerator(function() {
|
|
335
|
+
var cacheKey, data, dest, expireTime, limitTime, html, cacheHash, isStale, isGarbage;
|
|
336
|
+
return __generator(this, function(_state) {
|
|
337
|
+
switch(_state.label){
|
|
338
|
+
case 0:
|
|
339
|
+
cacheKey = _this.generateRequestKey(context);
|
|
340
|
+
data = _this.cache.get(cacheKey);
|
|
341
|
+
if (!data) {
|
|
342
|
+
return [
|
|
343
|
+
2,
|
|
344
|
+
null
|
|
345
|
+
];
|
|
346
|
+
}
|
|
347
|
+
return [
|
|
348
|
+
4,
|
|
349
|
+
_this.best(context, cacheKey, data)
|
|
350
|
+
];
|
|
351
|
+
case 1:
|
|
352
|
+
dest = _state.sent();
|
|
353
|
+
if (!dest) {
|
|
354
|
+
return [
|
|
355
|
+
2,
|
|
356
|
+
null
|
|
357
|
+
];
|
|
358
|
+
}
|
|
359
|
+
expireTime = dest.expireTime, limitTime = dest.limitTime, html = dest.html, cacheHash = dest.cacheHash;
|
|
360
|
+
isStale = Date.now() - expireTime > 0;
|
|
361
|
+
isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
|
|
362
|
+
return [
|
|
363
|
+
2,
|
|
364
|
+
{
|
|
365
|
+
content: html || "",
|
|
366
|
+
contentType: mime.contentType("html"),
|
|
367
|
+
isStale: isStale,
|
|
368
|
+
isGarbage: isGarbage,
|
|
369
|
+
hash: cacheHash
|
|
370
|
+
}
|
|
371
|
+
];
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
})();
|
|
375
|
+
}
|
|
376
|
+
},
|
|
377
|
+
{
|
|
378
|
+
key: "set",
|
|
379
|
+
value: function set(context, html, cacheConfig) {
|
|
380
|
+
var sync = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : false;
|
|
381
|
+
var _this = this;
|
|
382
|
+
return _asyncToGenerator(function() {
|
|
383
|
+
var cacheKey, data, caches, cacheHash, cacheSyncOrAsync, doCache;
|
|
384
|
+
return __generator(this, function(_state) {
|
|
385
|
+
switch(_state.label){
|
|
386
|
+
case 0:
|
|
387
|
+
if (!cacheConfig) {
|
|
388
|
+
return [
|
|
389
|
+
2,
|
|
390
|
+
false
|
|
391
|
+
];
|
|
392
|
+
}
|
|
393
|
+
cacheKey = _this.generateRequestKey(context);
|
|
394
|
+
data = _this.cache.get(cacheKey);
|
|
395
|
+
if (!!data) return [
|
|
396
|
+
3,
|
|
397
|
+
2
|
|
398
|
+
];
|
|
399
|
+
return [
|
|
400
|
+
4,
|
|
401
|
+
createPageCaches(MAX_CACHE_EACH_REQ)
|
|
402
|
+
];
|
|
403
|
+
case 1:
|
|
404
|
+
caches = _state.sent();
|
|
405
|
+
data = _this.createCacheContent(cacheConfig, caches);
|
|
406
|
+
_state.label = 2;
|
|
407
|
+
case 2:
|
|
408
|
+
cacheHash = _this.find[fname(cacheConfig.level)](context, cacheKey, data);
|
|
409
|
+
if (!cacheHash) {
|
|
410
|
+
return [
|
|
411
|
+
2,
|
|
412
|
+
false
|
|
413
|
+
];
|
|
414
|
+
}
|
|
415
|
+
cacheSyncOrAsync = function() {
|
|
416
|
+
var _ref = _asyncToGenerator(function() {
|
|
417
|
+
var next, limit, storeHTML, size;
|
|
418
|
+
return __generator(this, function(_state) {
|
|
419
|
+
switch(_state.label){
|
|
420
|
+
case 0:
|
|
421
|
+
next = data;
|
|
422
|
+
limit = cacheConfig.staleLimit;
|
|
423
|
+
storeHTML = cacheAddition(html, cacheHash);
|
|
424
|
+
size = storeHTML.length;
|
|
425
|
+
return [
|
|
426
|
+
4,
|
|
427
|
+
next.caches.set(cacheHash, {
|
|
428
|
+
expireTime: Date.now() + cacheConfig.interval * 1e3,
|
|
429
|
+
limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
|
|
430
|
+
cacheHash: cacheHash,
|
|
431
|
+
html: storeHTML,
|
|
432
|
+
size: size
|
|
433
|
+
})
|
|
434
|
+
];
|
|
435
|
+
case 1:
|
|
436
|
+
_state.sent();
|
|
437
|
+
_this.cache.set(cacheKey, next);
|
|
438
|
+
return [
|
|
439
|
+
2,
|
|
440
|
+
true
|
|
441
|
+
];
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
return function cacheSyncOrAsync() {
|
|
446
|
+
return _ref.apply(this, arguments);
|
|
447
|
+
};
|
|
448
|
+
}();
|
|
449
|
+
doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(null, namespaceHash("stream", cacheHash), []);
|
|
450
|
+
return [
|
|
451
|
+
2,
|
|
452
|
+
maybeSync(doCache)(sync)
|
|
453
|
+
];
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
})();
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
{
|
|
460
|
+
key: "del",
|
|
461
|
+
value: function del(context, cacheHash) {
|
|
462
|
+
var _this = this;
|
|
463
|
+
return _asyncToGenerator(function() {
|
|
464
|
+
var cacheKey, data;
|
|
465
|
+
return __generator(this, function(_state) {
|
|
466
|
+
cacheKey = _this.generateRequestKey(context);
|
|
467
|
+
data = _this.cache.get(cacheKey);
|
|
468
|
+
data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
|
|
469
|
+
return [
|
|
470
|
+
2
|
|
471
|
+
];
|
|
472
|
+
});
|
|
473
|
+
})();
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
]);
|
|
477
|
+
return CacheManager;
|
|
347
478
|
}();
|
|
348
479
|
var manager;
|
|
349
|
-
|
|
350
|
-
|
|
480
|
+
function createCache() {
|
|
481
|
+
if (manager) {
|
|
482
|
+
return manager;
|
|
483
|
+
}
|
|
484
|
+
manager = new CacheManager({
|
|
485
|
+
max: 0
|
|
486
|
+
});
|
|
351
487
|
return manager;
|
|
352
|
-
}
|
|
353
|
-
manager = new CacheManager({
|
|
354
|
-
max: 0
|
|
355
|
-
});
|
|
356
|
-
return manager;
|
|
357
488
|
}
|
|
358
|
-
|
|
359
|
-
|
|
489
|
+
function destroyCache() {
|
|
490
|
+
manager = null;
|
|
360
491
|
}
|
|
361
|
-
|
|
362
|
-
/* eslint-enable @typescript-eslint/member-ordering */
|
|
492
|
+
export { createCache, destroyCache };
|