@modern-js/prod-server 2.0.0-beta.0 → 2.0.0-beta.2

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 (113) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/dist/js/modern/constants.js +0 -2
  3. package/dist/js/modern/index.js +0 -1
  4. package/dist/js/modern/libs/context/context.js +10 -56
  5. package/dist/js/modern/libs/hook-api/index.js +0 -31
  6. package/dist/js/modern/libs/hook-api/route.js +0 -8
  7. package/dist/js/modern/libs/hook-api/template.js +0 -16
  8. package/dist/js/modern/libs/loadConfig.js +3 -10
  9. package/dist/js/modern/libs/metrics.js +6 -6
  10. package/dist/js/modern/libs/proxy.js +7 -12
  11. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -1
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -9
  13. package/dist/js/modern/libs/render/cache/index.js +8 -16
  14. package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -10
  15. package/dist/js/modern/libs/render/cache/spr.js +12 -62
  16. package/dist/js/modern/libs/render/cache/util.js +0 -6
  17. package/dist/js/modern/libs/render/index.js +6 -11
  18. package/dist/js/modern/libs/render/measure.js +1 -11
  19. package/dist/js/modern/libs/render/modern/index.js +2 -13
  20. package/dist/js/modern/libs/render/reader.js +13 -24
  21. package/dist/js/modern/libs/render/ssr.js +5 -6
  22. package/dist/js/modern/libs/render/static.js +6 -9
  23. package/dist/js/modern/libs/render/type.js +0 -1
  24. package/dist/js/modern/libs/route/index.js +8 -19
  25. package/dist/js/modern/libs/route/matcher.js +21 -29
  26. package/dist/js/modern/libs/route/route.js +0 -13
  27. package/dist/js/modern/libs/serve-file.js +5 -6
  28. package/dist/js/modern/server/index.js +25 -52
  29. package/dist/js/modern/server/modern-server-split.js +0 -11
  30. package/dist/js/modern/server/modern-server.js +87 -167
  31. package/dist/js/modern/utils.js +12 -27
  32. package/dist/js/modern/worker-server.js +34 -0
  33. package/dist/js/node/constants.js +0 -2
  34. package/dist/js/node/index.js +0 -10
  35. package/dist/js/node/libs/context/context.js +10 -65
  36. package/dist/js/node/libs/context/index.js +0 -3
  37. package/dist/js/node/libs/hook-api/index.js +0 -42
  38. package/dist/js/node/libs/hook-api/route.js +0 -10
  39. package/dist/js/node/libs/hook-api/template.js +0 -22
  40. package/dist/js/node/libs/loadConfig.js +3 -22
  41. package/dist/js/node/libs/metrics.js +6 -6
  42. package/dist/js/node/libs/proxy.js +7 -17
  43. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -5
  44. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -12
  45. package/dist/js/node/libs/render/cache/index.js +8 -22
  46. package/dist/js/node/libs/render/cache/page-caches/index.js +0 -2
  47. package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -14
  48. package/dist/js/node/libs/render/cache/spr.js +12 -71
  49. package/dist/js/node/libs/render/cache/util.js +0 -18
  50. package/dist/js/node/libs/render/index.js +6 -26
  51. package/dist/js/node/libs/render/measure.js +0 -17
  52. package/dist/js/node/libs/render/modern/index.js +2 -20
  53. package/dist/js/node/libs/render/reader.js +12 -39
  54. package/dist/js/node/libs/render/ssr.js +4 -16
  55. package/dist/js/node/libs/render/static.js +6 -18
  56. package/dist/js/node/libs/render/type.js +0 -1
  57. package/dist/js/node/libs/route/index.js +8 -22
  58. package/dist/js/node/libs/route/matcher.js +18 -34
  59. package/dist/js/node/libs/route/route.js +0 -15
  60. package/dist/js/node/libs/serve-file.js +5 -13
  61. package/dist/js/node/server/index.js +25 -72
  62. package/dist/js/node/server/modern-server-split.js +0 -13
  63. package/dist/js/node/server/modern-server.js +87 -196
  64. package/dist/js/node/utils.js +13 -52
  65. package/dist/js/node/worker-server.js +41 -0
  66. package/dist/js/treeshaking/constants.js +28 -0
  67. package/dist/js/treeshaking/index.js +13 -0
  68. package/dist/js/treeshaking/libs/context/context.js +243 -0
  69. package/dist/js/treeshaking/libs/context/index.js +5 -0
  70. package/dist/js/treeshaking/libs/hook-api/index.js +157 -0
  71. package/dist/js/treeshaking/libs/hook-api/route.js +33 -0
  72. package/dist/js/treeshaking/libs/hook-api/template.js +91 -0
  73. package/dist/js/treeshaking/libs/loadConfig.js +39 -0
  74. package/dist/js/treeshaking/libs/metrics.js +12 -0
  75. package/dist/js/treeshaking/libs/proxy.js +80 -0
  76. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +124 -0
  77. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +464 -0
  78. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +53 -0
  79. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +35 -0
  80. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +121 -0
  81. package/dist/js/treeshaking/libs/render/cache/index.js +184 -0
  82. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +30 -0
  83. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +46 -0
  84. package/dist/js/treeshaking/libs/render/cache/spr.js +362 -0
  85. package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
  86. package/dist/js/treeshaking/libs/render/cache/util.js +101 -0
  87. package/dist/js/treeshaking/libs/render/index.js +100 -0
  88. package/dist/js/treeshaking/libs/render/measure.js +61 -0
  89. package/dist/js/treeshaking/libs/render/modern/browser-list.js +7 -0
  90. package/dist/js/treeshaking/libs/render/modern/index.js +39 -0
  91. package/dist/js/treeshaking/libs/render/reader.js +191 -0
  92. package/dist/js/treeshaking/libs/render/ssr.js +98 -0
  93. package/dist/js/treeshaking/libs/render/static.js +84 -0
  94. package/dist/js/treeshaking/libs/render/type.js +6 -0
  95. package/dist/js/treeshaking/libs/route/index.js +94 -0
  96. package/dist/js/treeshaking/libs/route/matcher.js +113 -0
  97. package/dist/js/treeshaking/libs/route/route.js +26 -0
  98. package/dist/js/treeshaking/libs/serve-file.js +75 -0
  99. package/dist/js/treeshaking/server/index.js +339 -0
  100. package/dist/js/treeshaking/server/modern-server-split.js +152 -0
  101. package/dist/js/treeshaking/server/modern-server.js +946 -0
  102. package/dist/js/treeshaking/type.js +1 -0
  103. package/dist/js/treeshaking/utils.js +90 -0
  104. package/dist/js/treeshaking/worker-server.js +56 -0
  105. package/dist/types/index.d.ts +0 -2
  106. package/dist/types/libs/context/context.d.ts +0 -3
  107. package/dist/types/libs/loadConfig.d.ts +0 -1
  108. package/dist/types/libs/render/cache/index.d.ts +0 -2
  109. package/dist/types/libs/render/cache/spr.d.ts +0 -2
  110. package/dist/types/server/index.d.ts +0 -3
  111. package/dist/types/utils.d.ts +2 -3
  112. package/dist/types/worker-server.d.ts +16 -0
  113. package/package.json +31 -15
@@ -0,0 +1,184 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
4
+ import { Transform } from 'stream';
5
+ import { ERROR_DIGEST } from "../../../constants";
6
+ import { createCache } from "./spr";
7
+ import { namespaceHash, withCoalescedInvoke } from "./util";
8
+ export default (function (renderFn, ctx) {
9
+ var sprCache = createCache();
10
+ var doRender = /*#__PURE__*/function () {
11
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(context) {
12
+ var cacheContext, cacheFile, afterRender, _afterRender, saveHtmlIntoCache, _saveHtmlIntoCache, renderResult, cacheHash, _renderResult, render;
13
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
14
+ while (1) {
15
+ switch (_context5.prev = _context5.next) {
16
+ case 0:
17
+ _saveHtmlIntoCache = function _saveHtmlIntoCache3() {
18
+ _saveHtmlIntoCache = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(html) {
19
+ var cacheConfig;
20
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
21
+ while (1) {
22
+ switch (_context4.prev = _context4.next) {
23
+ case 0:
24
+ cacheConfig = context.cacheConfig;
25
+ if (!(html && cacheConfig)) {
26
+ _context4.next = 4;
27
+ break;
28
+ }
29
+ _context4.next = 4;
30
+ return sprCache.set(cacheContext, html, cacheConfig);
31
+ case 4:
32
+ case "end":
33
+ return _context4.stop();
34
+ }
35
+ }
36
+ }, _callee4);
37
+ }));
38
+ return _saveHtmlIntoCache.apply(this, arguments);
39
+ };
40
+ saveHtmlIntoCache = function _saveHtmlIntoCache2(_x4) {
41
+ return _saveHtmlIntoCache.apply(this, arguments);
42
+ };
43
+ _afterRender = function _afterRender3() {
44
+ _afterRender = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(source, onAfterRender) {
45
+ var htmlForStream, cacheStream;
46
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
47
+ while (1) {
48
+ switch (_context3.prev = _context3.next) {
49
+ case 0:
50
+ if (source) {
51
+ _context3.next = 2;
52
+ break;
53
+ }
54
+ return _context3.abrupt("return", '');
55
+ case 2:
56
+ if (!(typeof source === 'string')) {
57
+ _context3.next = 8;
58
+ break;
59
+ }
60
+ _context3.next = 5;
61
+ return onAfterRender(source);
62
+ case 5:
63
+ return _context3.abrupt("return", source);
64
+ case 8:
65
+ htmlForStream = '';
66
+ cacheStream = new Transform({
67
+ write: function write(chunk, _, callback) {
68
+ htmlForStream += chunk.toString();
69
+ this.push(chunk);
70
+ callback();
71
+ }
72
+ });
73
+ cacheStream.on('close', function () {
74
+ return onAfterRender(htmlForStream);
75
+ });
76
+ return _context3.abrupt("return", source(cacheStream));
77
+ case 12:
78
+ case "end":
79
+ return _context3.stop();
80
+ }
81
+ }
82
+ }, _callee3);
83
+ }));
84
+ return _afterRender.apply(this, arguments);
85
+ };
86
+ afterRender = function _afterRender2(_x2, _x3) {
87
+ return _afterRender.apply(this, arguments);
88
+ };
89
+ cacheContext = _objectSpread({
90
+ entry: context.entryName
91
+ }, context.request);
92
+ _context5.next = 7;
93
+ return sprCache.get(cacheContext);
94
+ case 7:
95
+ cacheFile = _context5.sent;
96
+ if (cacheFile) {
97
+ _context5.next = 13;
98
+ break;
99
+ }
100
+ _context5.next = 11;
101
+ return renderFn(context);
102
+ case 11:
103
+ renderResult = _context5.sent;
104
+ return _context5.abrupt("return", afterRender(renderResult, saveHtmlIntoCache));
105
+ case 13:
106
+ cacheHash = cacheFile === null || cacheFile === void 0 ? void 0 : cacheFile.hash; // completely expired
107
+ if (!cacheFile.isGarbage) {
108
+ _context5.next = 21;
109
+ break;
110
+ }
111
+ _context5.next = 17;
112
+ return renderFn(context);
113
+ case 17:
114
+ _renderResult = _context5.sent;
115
+ return _context5.abrupt("return", afterRender(_renderResult, saveHtmlIntoCache));
116
+ case 21:
117
+ if (cacheFile.isStale) {
118
+ // if file is stale, request async
119
+ render = withCoalescedInvoke(function () {
120
+ return renderFn(context);
121
+ }).bind(null, namespaceHash('render', cacheFile.hash), []);
122
+ render().then( /*#__PURE__*/function () {
123
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(res) {
124
+ var cacheConfig;
125
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
126
+ while (1) {
127
+ switch (_context2.prev = _context2.next) {
128
+ case 0:
129
+ if (res.value && res.isOrigin) {
130
+ cacheConfig = context.cacheConfig;
131
+ if (cacheConfig) {
132
+ afterRender(res.value, /*#__PURE__*/function () {
133
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(html) {
134
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
135
+ while (1) {
136
+ switch (_context.prev = _context.next) {
137
+ case 0:
138
+ sprCache.set(cacheContext, html, cacheConfig);
139
+ case 1:
140
+ case "end":
141
+ return _context.stop();
142
+ }
143
+ }
144
+ }, _callee);
145
+ }));
146
+ return function (_x6) {
147
+ return _ref3.apply(this, arguments);
148
+ };
149
+ }());
150
+ } else {
151
+ sprCache.del(cacheContext, cacheHash);
152
+ }
153
+ }
154
+ case 1:
155
+ case "end":
156
+ return _context2.stop();
157
+ }
158
+ }
159
+ }, _callee2);
160
+ }));
161
+ return function (_x5) {
162
+ return _ref2.apply(this, arguments);
163
+ };
164
+ }())["catch"](function (e) {
165
+ sprCache.del(cacheContext, cacheHash);
166
+ ctx.error(ERROR_DIGEST.ERENDER, e);
167
+ });
168
+ }
169
+ case 22:
170
+ ctx.res.setHeader('x-modern-spr', '1');
171
+ return _context5.abrupt("return", cacheFile.content);
172
+ case 24:
173
+ case "end":
174
+ return _context5.stop();
175
+ }
176
+ }
177
+ }, _callee5);
178
+ }));
179
+ return function doRender(_x) {
180
+ return _ref.apply(this, arguments);
181
+ };
182
+ }();
183
+ return doRender;
184
+ });
@@ -0,0 +1,30 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+ import { LRUCaches } from "./lru";
4
+ export function createPageCaches(_x) {
5
+ return _createPageCaches.apply(this, arguments);
6
+ }
7
+ function _createPageCaches() {
8
+ _createPageCaches = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(max) {
9
+ var constructorOptions, cacheInstance;
10
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
11
+ while (1) {
12
+ switch (_context.prev = _context.next) {
13
+ case 0:
14
+ constructorOptions = {
15
+ max: max
16
+ };
17
+ cacheInstance = new LRUCaches(constructorOptions);
18
+ _context.next = 4;
19
+ return cacheInstance.init();
20
+ case 4:
21
+ return _context.abrupt("return", cacheInstance);
22
+ case 5:
23
+ case "end":
24
+ return _context.stop();
25
+ }
26
+ }
27
+ }, _callee);
28
+ }));
29
+ return _createPageCaches.apply(this, arguments);
30
+ }
@@ -0,0 +1,46 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
+ import LRU from 'lru-cache';
5
+ export var LRUCaches = /*#__PURE__*/function () {
6
+ function LRUCaches(options) {
7
+ _classCallCheck(this, LRUCaches);
8
+ _defineProperty(this, "caches", void 0);
9
+ _defineProperty(this, "max", void 0);
10
+ this.max = options.max;
11
+ this.caches = new LRU(this.max);
12
+ }
13
+ _createClass(LRUCaches, [{
14
+ key: "init",
15
+ value: function init() {
16
+ return Promise.resolve();
17
+ }
18
+ }, {
19
+ key: "keys",
20
+ value: function keys() {
21
+ return this.caches.keys();
22
+ }
23
+ }, {
24
+ key: "get",
25
+ value: function get(key) {
26
+ return Promise.resolve(this.caches.get(key) || null);
27
+ }
28
+ }, {
29
+ key: "peek",
30
+ value: function peek(key) {
31
+ return this.caches.peek(key) || null;
32
+ }
33
+ }, {
34
+ key: "set",
35
+ value: function set(key, cache) {
36
+ this.caches.set(key, cache);
37
+ return Promise.resolve();
38
+ }
39
+ }, {
40
+ key: "del",
41
+ value: function del(key) {
42
+ this.caches.del(key);
43
+ }
44
+ }]);
45
+ return LRUCaches;
46
+ }();
@@ -0,0 +1,362 @@
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';
11
+ import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "./util";
12
+ import { createPageCaches } from "./page-caches";
13
+ var MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
14
+ var MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
15
+ var BASE_LEVEL = 0;
16
+ var QUERY_LEVEL = 1;
17
+ var HEADER_LEVEL = 2;
18
+ 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();
160
+ }
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
+ };
180
+ }
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
217
+ });
218
+ case 13:
219
+ case "end":
220
+ return _context2.stop();
221
+ }
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;
260
+ }
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;
271
+ }
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();
299
+ }
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();
312
+ }
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();
336
+ }
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;
347
+ }();
348
+ var manager;
349
+ export function createCache() {
350
+ if (manager) {
351
+ return manager;
352
+ }
353
+ manager = new CacheManager({
354
+ max: 0
355
+ });
356
+ return manager;
357
+ }
358
+ export function destroyCache() {
359
+ manager = null;
360
+ }
361
+ /* eslint-enable no-lone-blocks */
362
+ /* eslint-enable @typescript-eslint/member-ordering */
@@ -0,0 +1 @@
1
+ export {};