@modern-js/prod-server 2.0.0-beta.3 → 2.0.0-beta.6

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.
Files changed (130) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/dist/js/modern/constants.js +32 -25
  3. package/dist/js/modern/index.js +11 -6
  4. package/dist/js/modern/libs/context/context.js +52 -84
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +44 -35
  7. package/dist/js/modern/libs/hook-api/route.js +6 -7
  8. package/dist/js/modern/libs/hook-api/template.js +20 -34
  9. package/dist/js/modern/libs/loadConfig.js +45 -24
  10. package/dist/js/modern/libs/metrics.js +3 -4
  11. package/dist/js/modern/libs/proxy.js +68 -37
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -67
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -216
  14. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
  15. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
  16. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
  17. package/dist/js/modern/libs/render/cache/index.js +88 -54
  18. package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
  19. package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -6
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -117
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -39
  23. package/dist/js/modern/libs/render/index.js +76 -56
  24. package/dist/js/modern/libs/render/measure.js +38 -27
  25. package/dist/js/modern/libs/render/reader.js +65 -62
  26. package/dist/js/modern/libs/render/ssr.js +50 -32
  27. package/dist/js/modern/libs/render/static.js +50 -33
  28. package/dist/js/modern/libs/render/type.js +9 -6
  29. package/dist/js/modern/libs/route/index.js +8 -15
  30. package/dist/js/modern/libs/route/matcher.js +20 -34
  31. package/dist/js/modern/libs/route/route.js +9 -18
  32. package/dist/js/modern/libs/serve-file.js +33 -20
  33. package/dist/js/modern/server/index.js +150 -145
  34. package/dist/js/modern/server/modern-server-split.js +46 -12
  35. package/dist/js/modern/server/modern-server.js +384 -422
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -43
  38. package/dist/js/modern/worker-server.js +34 -14
  39. package/dist/js/node/constants.js +53 -30
  40. package/dist/js/node/index.js +37 -57
  41. package/dist/js/node/libs/context/context.js +83 -94
  42. package/dist/js/node/libs/context/index.js +28 -13
  43. package/dist/js/node/libs/hook-api/index.js +76 -48
  44. package/dist/js/node/libs/hook-api/route.js +26 -11
  45. package/dist/js/node/libs/hook-api/template.js +41 -39
  46. package/dist/js/node/libs/loadConfig.js +73 -35
  47. package/dist/js/node/libs/metrics.js +25 -9
  48. package/dist/js/node/libs/proxy.js +89 -44
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -56
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -128
  51. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
  52. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
  53. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
  54. package/dist/js/node/libs/render/cache/index.js +110 -64
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +54 -14
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -12
  57. package/dist/js/node/libs/render/cache/spr.js +161 -129
  58. package/dist/js/node/libs/render/cache/type.js +15 -5
  59. package/dist/js/node/libs/render/cache/util.js +99 -45
  60. package/dist/js/node/libs/render/index.js +106 -67
  61. package/dist/js/node/libs/render/measure.js +58 -31
  62. package/dist/js/node/libs/render/reader.js +95 -70
  63. package/dist/js/node/libs/render/ssr.js +80 -47
  64. package/dist/js/node/libs/render/static.js +79 -40
  65. package/dist/js/node/libs/render/type.js +31 -12
  66. package/dist/js/node/libs/route/index.js +31 -26
  67. package/dist/js/node/libs/route/matcher.js +40 -41
  68. package/dist/js/node/libs/route/route.js +29 -22
  69. package/dist/js/node/libs/serve-file.js +66 -32
  70. package/dist/js/node/server/index.js +168 -160
  71. package/dist/js/node/server/modern-server-split.js +72 -22
  72. package/dist/js/node/server/modern-server.js +403 -445
  73. package/dist/js/node/type.js +15 -3
  74. package/dist/js/node/utils.js +85 -52
  75. package/dist/js/node/worker-server.js +57 -21
  76. package/dist/js/treeshaking/constants.js +26 -25
  77. package/dist/js/treeshaking/index.js +10 -10
  78. package/dist/js/treeshaking/libs/context/context.js +268 -237
  79. package/dist/js/treeshaking/libs/context/index.js +3 -3
  80. package/dist/js/treeshaking/libs/hook-api/index.js +267 -143
  81. package/dist/js/treeshaking/libs/hook-api/route.js +65 -30
  82. package/dist/js/treeshaking/libs/hook-api/template.js +121 -85
  83. package/dist/js/treeshaking/libs/loadConfig.js +80 -37
  84. package/dist/js/treeshaking/libs/metrics.js +4 -10
  85. package/dist/js/treeshaking/libs/proxy.js +240 -76
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +288 -121
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +772 -455
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +65 -51
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -35
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +144 -118
  91. package/dist/js/treeshaking/libs/render/cache/index.js +337 -175
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +151 -27
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +80 -42
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +470 -340
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -1
  96. package/dist/js/treeshaking/libs/render/cache/util.js +271 -92
  97. package/dist/js/treeshaking/libs/render/index.js +228 -95
  98. package/dist/js/treeshaking/libs/render/measure.js +142 -57
  99. package/dist/js/treeshaking/libs/render/reader.js +325 -177
  100. package/dist/js/treeshaking/libs/render/ssr.js +220 -95
  101. package/dist/js/treeshaking/libs/render/static.js +210 -78
  102. package/dist/js/treeshaking/libs/render/type.js +7 -6
  103. package/dist/js/treeshaking/libs/route/index.js +125 -89
  104. package/dist/js/treeshaking/libs/route/matcher.js +132 -107
  105. package/dist/js/treeshaking/libs/route/route.js +40 -26
  106. package/dist/js/treeshaking/libs/serve-file.js +177 -68
  107. package/dist/js/treeshaking/server/index.js +493 -327
  108. package/dist/js/treeshaking/server/modern-server-split.js +352 -144
  109. package/dist/js/treeshaking/server/modern-server.js +1048 -909
  110. package/dist/js/treeshaking/type.js +1 -1
  111. package/dist/js/treeshaking/utils.js +138 -81
  112. package/dist/js/treeshaking/worker-server.js +176 -55
  113. package/dist/types/index.d.ts +2 -0
  114. package/dist/types/libs/context/context.d.ts +4 -1
  115. package/dist/types/libs/loadConfig.d.ts +1 -0
  116. package/dist/types/libs/render/cache/index.d.ts +2 -0
  117. package/dist/types/libs/render/cache/spr.d.ts +2 -0
  118. package/dist/types/libs/route/route.d.ts +0 -1
  119. package/dist/types/server/index.d.ts +3 -0
  120. package/dist/types/utils.d.ts +1 -1
  121. package/dist/types/worker-server.d.ts +1 -2
  122. package/package.json +7 -14
  123. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  124. package/dist/js/modern/libs/render/modern/index.js +0 -37
  125. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  126. package/dist/js/node/libs/render/modern/index.js +0 -46
  127. package/dist/js/treeshaking/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/treeshaking/libs/render/modern/index.js +0 -39
  129. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  130. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,13 +1,162 @@
1
- import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
4
- import _createClass from "@babel/runtime/helpers/esm/createClass";
5
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
6
- /* eslint-disable @typescript-eslint/member-ordering */
7
- /* eslint-disable no-lone-blocks */
8
- import crypto from 'crypto';
9
- import LRUCache from 'lru-cache';
10
- import { mime } from '@modern-js/utils';
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
- function CacheManager(cacheOptions) {
21
- var _this2 = this;
22
- _classCallCheck(this, CacheManager);
23
- _defineProperty(this, "cache", void 0);
24
- _defineProperty(this, "cacheOptions", void 0);
25
- _defineProperty(this, "find", function () {
26
- {
27
- var _ref;
28
- // eslint-disable-next-line consistent-this,@typescript-eslint/no-this-alias
29
- var _this = _this2;
30
- return _ref = {}, _defineProperty(_ref, fname(BASE_LEVEL), function (context, cacheKey
31
- // data: CacheContent,
32
- ) {
33
- return _this.md5(cacheKey);
34
- }), _defineProperty(_ref, fname(QUERY_LEVEL), function (context, cacheKey, data) {
35
- var queryFactor = _this.queryFactor(context, data);
36
- if (!queryFactor) {
37
- return null;
38
- }
39
- return _this.md5(connectFactor(cacheKey, queryFactor));
40
- }), _defineProperty(_ref, fname(HEADER_LEVEL), function (context, cacheKey, data) {
41
- var headerFactor = _this.headerFactor(context, data);
42
- if (!headerFactor) {
43
- return null;
44
- }
45
- return _this.md5(connectFactor(cacheKey, headerFactor));
46
- }), _defineProperty(_ref, fname(QUERY_HEADER_LEVEL), function (context, cacheKey, data) {
47
- var queryFactor = _this.queryFactor(context, data);
48
- var headerFactor = _this.headerFactor(context, data);
49
- if (!queryFactor || !headerFactor) {
50
- return null;
51
- }
52
- return _this.md5(connectFactor(cacheKey, headerFactor, queryFactor));
53
- }), _ref;
54
- }
55
- }());
56
- this.cacheOptions = cacheOptions;
57
- this.cache = new LRUCache({
58
- max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
59
- // 默认存 100M,最大 600M
60
- length: function length(n) {
61
- var len = n.caches.keys().reduce(function (total, cur) {
62
- var _n$caches$peek;
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
- key: "get",
183
- value: function () {
184
- var _get = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(context) {
185
- var cacheKey, data, dest, expireTime, limitTime, html, cacheHash, isStale, isGarbage;
186
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
187
- while (1) {
188
- switch (_context2.prev = _context2.next) {
189
- case 0:
190
- cacheKey = this.generateRequestKey(context);
191
- data = this.cache.get(cacheKey); // no cache key matched
192
- if (data) {
193
- _context2.next = 4;
194
- break;
195
- }
196
- return _context2.abrupt("return", null);
197
- case 4:
198
- _context2.next = 6;
199
- return this.best(context, cacheKey, data);
200
- case 6:
201
- dest = _context2.sent;
202
- if (dest) {
203
- _context2.next = 9;
204
- break;
205
- }
206
- return _context2.abrupt("return", null);
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
- case 13:
219
- case "end":
220
- return _context2.stop();
242
+ return final;
221
243
  }
222
- }
223
- }, _callee2, this);
224
- }));
225
- function get(_x4) {
226
- return _get.apply(this, arguments);
227
- }
228
- return get;
229
- }()
230
- }, {
231
- key: "set",
232
- value: function () {
233
- var _set = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(context, html, cacheConfig) {
234
- var _this4 = this;
235
- var sync,
236
- cacheKey,
237
- data,
238
- caches,
239
- cacheHash,
240
- cacheSyncOrAsync,
241
- doCache,
242
- _args4 = arguments;
243
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
244
- while (1) {
245
- switch (_context4.prev = _context4.next) {
246
- case 0:
247
- sync = _args4.length > 3 && _args4[3] !== undefined ? _args4[3] : false;
248
- if (cacheConfig) {
249
- _context4.next = 3;
250
- break;
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
- _context4.next = 8;
262
- return createPageCaches(MAX_CACHE_EACH_REQ);
263
- case 8:
264
- caches = _context4.sent;
265
- data = this.createCacheContent(cacheConfig, caches);
266
- case 10:
267
- cacheHash = this.find[fname(cacheConfig.level)](context, cacheKey, data); // if cacheHash is null, maybe level not match meta key, do not cache
268
- if (cacheHash) {
269
- _context4.next = 13;
270
- break;
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
- return _context4.abrupt("return", false);
273
- case 13:
274
- cacheSyncOrAsync = /*#__PURE__*/function () {
275
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
276
- var next, limit, storeHTML, size;
277
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
278
- while (1) {
279
- switch (_context3.prev = _context3.next) {
280
- case 0:
281
- next = data;
282
- limit = cacheConfig.staleLimit;
283
- storeHTML = cacheAddition(html, cacheHash);
284
- size = storeHTML.length;
285
- _context3.next = 6;
286
- return next.caches.set(cacheHash, {
287
- expireTime: Date.now() + cacheConfig.interval * 1000,
288
- limitTime: typeof limit === 'number' ? Date.now() + limit * 1000 : null,
289
- cacheHash: cacheHash,
290
- html: storeHTML,
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
- }, _callee3);
302
- }));
303
- return function cacheSyncOrAsync() {
304
- return _ref2.apply(this, arguments);
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
- }, _callee4, this);
315
- }));
316
- function set(_x5, _x6, _x7) {
317
- return _set.apply(this, arguments);
318
- }
319
- return set;
320
- }()
321
- }, {
322
- key: "del",
323
- value: function () {
324
- var _del = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(context, cacheHash) {
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
- }, _callee5, this);
339
- }));
340
- function del(_x8, _x9) {
341
- return _del.apply(this, arguments);
342
- }
343
- return del;
344
- }()
345
- }]);
346
- return CacheManager;
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
- export function createCache() {
350
- if (manager) {
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
- export function destroyCache() {
359
- manager = null;
489
+ function destroyCache() {
490
+ manager = null;
360
491
  }
361
- /* eslint-enable no-lone-blocks */
362
- /* eslint-enable @typescript-eslint/member-ordering */
492
+ export { createCache, destroyCache };