@modern-js/prod-server 1.22.1 → 2.0.0-alpha.0

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 (132) hide show
  1. package/CHANGELOG.md +271 -17
  2. package/dist/js/modern/constants.js +32 -27
  3. package/dist/js/modern/index.js +11 -7
  4. package/dist/js/modern/libs/context/context.js +54 -132
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +134 -0
  7. package/dist/js/modern/libs/hook-api/route.js +13 -37
  8. package/dist/js/modern/libs/hook-api/template.js +41 -32
  9. package/dist/js/modern/libs/loadConfig.js +46 -32
  10. package/dist/js/modern/libs/metrics.js +6 -7
  11. package/dist/js/modern/libs/proxy.js +70 -44
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -68
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -225
  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 +93 -50
  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 -16
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -167
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -45
  23. package/dist/js/modern/libs/render/index.js +76 -61
  24. package/dist/js/modern/libs/render/measure.js +42 -34
  25. package/dist/js/modern/libs/render/reader.js +68 -76
  26. package/dist/js/modern/libs/render/ssr.js +63 -33
  27. package/dist/js/modern/libs/render/static.js +51 -37
  28. package/dist/js/modern/libs/render/type.js +9 -7
  29. package/dist/js/modern/libs/route/index.js +12 -30
  30. package/dist/js/modern/libs/route/matcher.js +28 -50
  31. package/dist/js/modern/libs/route/route.js +9 -31
  32. package/dist/js/modern/libs/serve-file.js +40 -20
  33. package/dist/js/modern/server/index.js +152 -168
  34. package/dist/js/modern/server/modern-server-split.js +44 -60
  35. package/dist/js/modern/server/modern-server.js +416 -526
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -58
  38. package/dist/js/modern/worker-server.js +54 -0
  39. package/dist/js/node/constants.js +53 -32
  40. package/dist/js/node/index.js +37 -67
  41. package/dist/js/node/libs/context/context.js +84 -150
  42. package/dist/js/node/libs/context/index.js +28 -16
  43. package/dist/js/node/libs/hook-api/index.js +164 -0
  44. package/dist/js/node/libs/hook-api/route.js +35 -45
  45. package/dist/js/node/libs/hook-api/template.js +64 -40
  46. package/dist/js/node/libs/loadConfig.js +74 -55
  47. package/dist/js/node/libs/metrics.js +28 -12
  48. package/dist/js/node/libs/proxy.js +90 -55
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -61
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -140
  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 +115 -65
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +54 -16
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -26
  57. package/dist/js/node/libs/render/cache/spr.js +161 -188
  58. package/dist/js/node/libs/render/cache/type.js +15 -5
  59. package/dist/js/node/libs/render/cache/util.js +99 -63
  60. package/dist/js/node/libs/render/index.js +106 -87
  61. package/dist/js/node/libs/render/measure.js +61 -44
  62. package/dist/js/node/libs/render/reader.js +98 -100
  63. package/dist/js/node/libs/render/ssr.js +92 -58
  64. package/dist/js/node/libs/render/static.js +80 -53
  65. package/dist/js/node/libs/render/type.js +31 -13
  66. package/dist/js/node/libs/route/index.js +35 -44
  67. package/dist/js/node/libs/route/matcher.js +48 -65
  68. package/dist/js/node/libs/route/route.js +29 -37
  69. package/dist/js/node/libs/serve-file.js +74 -37
  70. package/dist/js/node/server/index.js +170 -203
  71. package/dist/js/node/server/modern-server-split.js +70 -73
  72. package/dist/js/node/server/modern-server.js +432 -577
  73. package/dist/js/node/type.js +15 -3
  74. package/dist/js/node/utils.js +87 -93
  75. package/dist/js/node/worker-server.js +77 -0
  76. package/dist/js/treeshaking/constants.js +29 -0
  77. package/dist/js/treeshaking/index.js +13 -0
  78. package/dist/js/treeshaking/libs/context/context.js +274 -0
  79. package/dist/js/treeshaking/libs/context/index.js +5 -0
  80. package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
  81. package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
  82. package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
  83. package/dist/js/treeshaking/libs/loadConfig.js +82 -0
  84. package/dist/js/treeshaking/libs/metrics.js +6 -0
  85. package/dist/js/treeshaking/libs/proxy.js +244 -0
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
  91. package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
  96. package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
  97. package/dist/js/treeshaking/libs/render/index.js +233 -0
  98. package/dist/js/treeshaking/libs/render/measure.js +146 -0
  99. package/dist/js/treeshaking/libs/render/reader.js +339 -0
  100. package/dist/js/treeshaking/libs/render/ssr.js +223 -0
  101. package/dist/js/treeshaking/libs/render/static.js +216 -0
  102. package/dist/js/treeshaking/libs/render/type.js +7 -0
  103. package/dist/js/treeshaking/libs/route/index.js +130 -0
  104. package/dist/js/treeshaking/libs/route/matcher.js +138 -0
  105. package/dist/js/treeshaking/libs/route/route.js +40 -0
  106. package/dist/js/treeshaking/libs/serve-file.js +184 -0
  107. package/dist/js/treeshaking/server/index.js +505 -0
  108. package/dist/js/treeshaking/server/modern-server-split.js +360 -0
  109. package/dist/js/treeshaking/server/modern-server.js +1083 -0
  110. package/dist/js/treeshaking/type.js +1 -0
  111. package/dist/js/treeshaking/utils.js +147 -0
  112. package/dist/js/treeshaking/worker-server.js +177 -0
  113. package/dist/types/libs/context/context.d.ts +1 -1
  114. package/dist/types/libs/hook-api/index.d.ts +5 -0
  115. package/dist/types/libs/hook-api/route.d.ts +9 -14
  116. package/dist/types/libs/hook-api/template.d.ts +19 -9
  117. package/dist/types/libs/render/cache/index.d.ts +4 -2
  118. package/dist/types/libs/render/type.d.ts +3 -1
  119. package/dist/types/libs/route/route.d.ts +0 -1
  120. package/dist/types/libs/serve-file.d.ts +2 -1
  121. package/dist/types/server/index.d.ts +2 -0
  122. package/dist/types/server/modern-server.d.ts +11 -11
  123. package/dist/types/type.d.ts +8 -10
  124. package/dist/types/utils.d.ts +3 -4
  125. package/dist/types/worker-server.d.ts +15 -0
  126. package/package.json +31 -45
  127. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/modern/libs/render/modern/index.js +0 -48
  129. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  130. package/dist/js/node/libs/render/modern/index.js +0 -64
  131. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  132. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,17 +1,55 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var __async = (__this, __arguments, generator) => {
19
+ return new Promise((resolve, reject) => {
20
+ var fulfilled = (value) => {
21
+ try {
22
+ step(generator.next(value));
23
+ } catch (e) {
24
+ reject(e);
25
+ }
26
+ };
27
+ var rejected = (value) => {
28
+ try {
29
+ step(generator.throw(value));
30
+ } catch (e) {
31
+ reject(e);
32
+ }
33
+ };
34
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
35
+ step((generator = generator.apply(__this, __arguments)).next());
36
+ });
37
+ };
38
+ var page_caches_exports = {};
39
+ __export(page_caches_exports, {
40
+ createPageCaches: () => createPageCaches
41
+ });
42
+ module.exports = __toCommonJS(page_caches_exports);
43
+ var import_lru = require("./lru");
44
+ function createPageCaches(max) {
45
+ return __async(this, null, function* () {
46
+ const constructorOptions = { max };
47
+ const cacheInstance = new import_lru.LRUCaches(constructorOptions);
48
+ yield cacheInstance.init();
49
+ return cacheInstance;
50
+ });
51
+ }
52
+ // Annotate the CommonJS export names for ESM import in node:
53
+ 0 && (module.exports = {
54
+ createPageCaches
5
55
  });
6
- exports.createPageCaches = createPageCaches;
7
-
8
- var _lru = require("./lru");
9
-
10
- async function createPageCaches(max) {
11
- const constructorOptions = {
12
- max
13
- };
14
- const cacheInstance = new _lru.LRUCaches(constructorOptions);
15
- await cacheInstance.init();
16
- return cacheInstance;
17
- }
@@ -1,51 +1,58 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+ var lru_exports = {};
25
+ __export(lru_exports, {
26
+ LRUCaches: () => LRUCaches
5
27
  });
6
- exports.LRUCaches = void 0;
7
-
8
- var _lruCache = _interopRequireDefault(require("lru-cache"));
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
-
12
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
-
28
+ module.exports = __toCommonJS(lru_exports);
29
+ var import_lru_cache = __toESM(require("lru-cache"));
14
30
  class LRUCaches {
15
31
  constructor(options) {
16
- _defineProperty(this, "caches", void 0);
17
-
18
- _defineProperty(this, "max", void 0);
19
-
20
32
  this.max = options.max;
21
- this.caches = new _lruCache.default(this.max);
33
+ this.caches = new import_lru_cache.default(this.max);
22
34
  }
23
-
24
35
  init() {
25
36
  return Promise.resolve();
26
37
  }
27
-
28
38
  keys() {
29
39
  return this.caches.keys();
30
40
  }
31
-
32
41
  get(key) {
33
42
  return Promise.resolve(this.caches.get(key) || null);
34
43
  }
35
-
36
44
  peek(key) {
37
45
  return this.caches.peek(key) || null;
38
46
  }
39
-
40
47
  set(key, cache) {
41
48
  this.caches.set(key, cache);
42
49
  return Promise.resolve();
43
50
  }
44
-
45
51
  del(key) {
46
52
  this.caches.del(key);
47
53
  }
48
-
49
54
  }
50
-
51
- exports.LRUCaches = LRUCaches;
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ LRUCaches
58
+ });
@@ -1,190 +1,174 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+ var __async = (__this, __arguments, generator) => {
25
+ return new Promise((resolve, reject) => {
26
+ var fulfilled = (value) => {
27
+ try {
28
+ step(generator.next(value));
29
+ } catch (e) {
30
+ reject(e);
31
+ }
32
+ };
33
+ var rejected = (value) => {
34
+ try {
35
+ step(generator.throw(value));
36
+ } catch (e) {
37
+ reject(e);
38
+ }
39
+ };
40
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
41
+ step((generator = generator.apply(__this, __arguments)).next());
42
+ });
43
+ };
44
+ var spr_exports = {};
45
+ __export(spr_exports, {
46
+ createCache: () => createCache,
47
+ destroyCache: () => destroyCache
5
48
  });
6
- exports.createCache = createCache;
7
- exports.destroyCache = destroyCache;
8
-
9
- var _crypto = _interopRequireDefault(require("crypto"));
10
-
11
- var _lruCache = _interopRequireDefault(require("lru-cache"));
12
-
13
- var _utils = require("@modern-js/utils");
14
-
15
- var _util = require("./util");
16
-
17
- var _pageCaches = require("./page-caches");
18
-
19
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
-
21
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
22
-
49
+ module.exports = __toCommonJS(spr_exports);
50
+ var import_crypto = __toESM(require("crypto"));
51
+ var import_lru_cache = __toESM(require("lru-cache"));
52
+ var import_utils = require("@modern-js/utils");
53
+ var import_util = require("./util");
54
+ var import_page_caches = require("./page-caches");
23
55
  const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
24
56
  const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
25
57
  const BASE_LEVEL = 0;
26
58
  const QUERY_LEVEL = 1;
27
59
  const HEADER_LEVEL = 2;
28
60
  const QUERY_HEADER_LEVEL = 3;
29
-
30
61
  class CacheManager {
31
62
  constructor(cacheOptions) {
32
- _defineProperty(this, "cache", void 0);
33
-
34
- _defineProperty(this, "cacheOptions", void 0);
35
-
36
- _defineProperty(this, "find", (() => {
63
+ this.find = (() => {
37
64
  {
38
- // eslint-disable-next-line consistent-this,@typescript-eslint/no-this-alias
39
65
  const _this = this;
40
-
41
66
  return {
42
- [(0, _util.fname)(BASE_LEVEL)](context, cacheKey // data: CacheContent,
43
- ) {
67
+ [(0, import_util.fname)(BASE_LEVEL)](context, cacheKey) {
44
68
  return _this.md5(cacheKey);
45
69
  },
46
-
47
- [(0, _util.fname)(QUERY_LEVEL)](context, cacheKey, data) {
70
+ [(0, import_util.fname)(QUERY_LEVEL)](context, cacheKey, data) {
48
71
  const queryFactor = _this.queryFactor(context, data);
49
-
50
72
  if (!queryFactor) {
51
73
  return null;
52
74
  }
53
-
54
- return _this.md5((0, _util.connectFactor)(cacheKey, queryFactor));
75
+ return _this.md5((0, import_util.connectFactor)(cacheKey, queryFactor));
55
76
  },
56
-
57
- [(0, _util.fname)(HEADER_LEVEL)](context, cacheKey, data) {
77
+ [(0, import_util.fname)(HEADER_LEVEL)](context, cacheKey, data) {
58
78
  const headerFactor = _this.headerFactor(context, data);
59
-
60
79
  if (!headerFactor) {
61
80
  return null;
62
81
  }
63
-
64
- return _this.md5((0, _util.connectFactor)(cacheKey, headerFactor));
82
+ return _this.md5((0, import_util.connectFactor)(cacheKey, headerFactor));
65
83
  },
66
-
67
- [(0, _util.fname)(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
84
+ [(0, import_util.fname)(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
68
85
  const queryFactor = _this.queryFactor(context, data);
69
-
70
86
  const headerFactor = _this.headerFactor(context, data);
71
-
72
87
  if (!queryFactor || !headerFactor) {
73
88
  return null;
74
89
  }
75
-
76
- return _this.md5((0, _util.connectFactor)(cacheKey, headerFactor, queryFactor));
90
+ return _this.md5((0, import_util.connectFactor)(cacheKey, headerFactor, queryFactor));
77
91
  }
78
-
79
92
  };
80
93
  }
81
- })());
82
-
94
+ })();
83
95
  this.cacheOptions = cacheOptions;
84
- this.cache = new _lruCache.default({
96
+ this.cache = new import_lru_cache.default({
85
97
  max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
86
-
87
- // 默认存 100M,最大 600M
88
98
  length(n) {
89
99
  const len = n.caches.keys().reduce((total, cur) => {
90
- var _n$caches$peek;
91
-
92
- return total + (((_n$caches$peek = n.caches.peek(cur)) === null || _n$caches$peek === void 0 ? void 0 : _n$caches$peek.size) || 0);
100
+ var _a;
101
+ return total + (((_a = n.caches.peek(cur)) == null ? void 0 : _a.size) || 0);
93
102
  }, 1);
94
103
  return len;
95
104
  }
96
-
97
105
  });
98
106
  }
99
-
100
107
  md5(content) {
101
- const md5 = _crypto.default.createHash('md5');
102
-
103
- return md5.update(content).digest('hex');
108
+ const md5 = import_crypto.default.createHash("md5");
109
+ return md5.update(content).digest("hex");
104
110
  }
105
-
106
111
  generateRequestKey(context) {
107
- const {
108
- pathname,
109
- entry
110
- } = context;
112
+ const { pathname, entry } = context;
111
113
  return this.md5(`${pathname}_${entry}`);
112
114
  }
113
-
114
115
  replaceValue(value, matcher) {
115
116
  let final = value;
116
- Object.keys(matcher).some(replacer => {
117
+ Object.keys(matcher).some((replacer) => {
117
118
  const reg = new RegExp(matcher[replacer]);
118
-
119
119
  if (reg.test(value)) {
120
120
  final = replacer;
121
121
  return true;
122
122
  }
123
-
124
123
  return false;
125
124
  });
126
125
  return final;
127
126
  }
128
-
129
127
  factor(keys, obj, matches = {}) {
130
128
  keys.sort();
131
- const getValue = (0, _util.valueFactory)(obj);
129
+ const getValue = (0, import_util.valueFactory)(obj);
132
130
  const factorAry = keys.reduce((ary, key) => {
133
- let value = getValue(key) || '';
131
+ let value = getValue(key) || "";
134
132
  const matcher = matches[key];
135
-
136
133
  if (matcher) {
137
134
  value = this.replaceValue(value, matcher);
138
135
  }
139
-
140
136
  return ary.concat([key, value]);
141
137
  }, []);
142
- return factorAry.join(',');
138
+ return factorAry.join(",");
143
139
  }
144
-
145
140
  queryFactor(context, data) {
146
- var _data$includes, _data$matches;
147
-
148
- const queryKeys = (_data$includes = data.includes) === null || _data$includes === void 0 ? void 0 : _data$includes.query;
149
- const queryMatches = (_data$matches = data.matches) === null || _data$matches === void 0 ? void 0 : _data$matches.query;
150
-
141
+ var _a, _b;
142
+ const queryKeys = (_a = data.includes) == null ? void 0 : _a.query;
143
+ const queryMatches = (_b = data.matches) == null ? void 0 : _b.query;
151
144
  if (!queryKeys || queryKeys.length === 0) {
152
145
  return null;
153
146
  }
154
-
155
147
  const requestQuery = context.query;
156
148
  const queryFactor = this.factor(queryKeys, requestQuery, queryMatches);
157
149
  return queryFactor;
158
150
  }
159
-
160
151
  headerFactor(context, data) {
161
- var _data$includes2, _data$matches2;
162
-
163
- const headerKeys = (_data$includes2 = data.includes) === null || _data$includes2 === void 0 ? void 0 : _data$includes2.header;
164
- const headerMatches = (_data$matches2 = data.matches) === null || _data$matches2 === void 0 ? void 0 : _data$matches2.header;
165
-
152
+ var _a, _b;
153
+ const headerKeys = (_a = data.includes) == null ? void 0 : _a.header;
154
+ const headerMatches = (_b = data.matches) == null ? void 0 : _b.header;
166
155
  if (!headerKeys || headerKeys.length === 0) {
167
156
  return null;
168
157
  }
169
-
170
158
  const requestHeader = context.headers;
171
159
  const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
172
160
  return headerFactor;
173
161
  }
174
-
175
- async best(context, cacheKey, data) {
176
- const {
177
- level
178
- } = data;
179
- const cacheHash = this.find[(0, _util.fname)(level)](context, cacheKey, data);
180
-
181
- if (!cacheHash) {
182
- return null;
183
- }
184
-
185
- return data.caches.get(cacheHash);
162
+ best(context, cacheKey, data) {
163
+ return __async(this, null, function* () {
164
+ const { level } = data;
165
+ const cacheHash = this.find[(0, import_util.fname)(level)](context, cacheKey, data);
166
+ if (!cacheHash) {
167
+ return null;
168
+ }
169
+ return data.caches.get(cacheHash);
170
+ });
186
171
  }
187
-
188
172
  createCacheContent(config, caches) {
189
173
  return {
190
174
  level: config.level,
@@ -195,103 +179,92 @@ class CacheManager {
195
179
  caches
196
180
  };
197
181
  }
198
-
199
- async get(context) {
200
- const cacheKey = this.generateRequestKey(context);
201
- const data = this.cache.get(cacheKey); // no cache key matched
202
-
203
- if (!data) {
204
- return null;
205
- }
206
-
207
- const dest = await this.best(context, cacheKey, data); // no cache for current page with current config
208
-
209
- if (!dest) {
210
- return null;
211
- }
212
-
213
- const {
214
- expireTime,
215
- limitTime,
216
- html,
217
- cacheHash
218
- } = dest;
219
- const isStale = Date.now() - expireTime > 0;
220
- const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
221
- return {
222
- content: html || '',
223
- contentType: _utils.mime.contentType('html'),
224
- isStale,
225
- isGarbage,
226
- hash: cacheHash
227
- };
182
+ get(context) {
183
+ return __async(this, null, function* () {
184
+ const cacheKey = this.generateRequestKey(context);
185
+ const data = this.cache.get(cacheKey);
186
+ if (!data) {
187
+ return null;
188
+ }
189
+ const dest = yield this.best(context, cacheKey, data);
190
+ if (!dest) {
191
+ return null;
192
+ }
193
+ const { expireTime, limitTime, html, cacheHash } = dest;
194
+ const isStale = Date.now() - expireTime > 0;
195
+ const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
196
+ return {
197
+ content: html || "",
198
+ contentType: import_utils.mime.contentType("html"),
199
+ isStale,
200
+ isGarbage,
201
+ hash: cacheHash
202
+ };
203
+ });
228
204
  }
229
-
230
- async set(context, html, cacheConfig, sync = false) {
231
- if (!cacheConfig) {
232
- return false;
233
- } // each version with route is a separate cache
234
-
235
-
236
- const cacheKey = this.generateRequestKey(context);
237
- let data = this.cache.get(cacheKey);
238
-
239
- if (!data) {
240
- const caches = await (0, _pageCaches.createPageCaches)(MAX_CACHE_EACH_REQ);
241
- data = this.createCacheContent(cacheConfig, caches);
242
- }
243
-
244
- const cacheHash = this.find[(0, _util.fname)(cacheConfig.level)](context, cacheKey, data); // if cacheHash is null, maybe level not match meta key, do not cache
245
-
246
- if (!cacheHash) {
247
- return false;
248
- }
249
-
250
- const cacheSyncOrAsync = async () => {
251
- const next = data;
252
- const limit = cacheConfig.staleLimit;
253
- const storeHTML = (0, _util.cacheAddition)(html, cacheHash);
254
- const size = storeHTML.length;
255
- await next.caches.set(cacheHash, {
256
- expireTime: Date.now() + cacheConfig.interval * 1000,
257
- limitTime: typeof limit === 'number' ? Date.now() + limit * 1000 : null,
258
- cacheHash,
259
- html: storeHTML,
260
- size
205
+ set(context, html, cacheConfig, sync = false) {
206
+ return __async(this, null, function* () {
207
+ if (!cacheConfig) {
208
+ return false;
209
+ }
210
+ const cacheKey = this.generateRequestKey(context);
211
+ let data = this.cache.get(cacheKey);
212
+ if (!data) {
213
+ const caches = yield (0, import_page_caches.createPageCaches)(MAX_CACHE_EACH_REQ);
214
+ data = this.createCacheContent(cacheConfig, caches);
215
+ }
216
+ const cacheHash = this.find[(0, import_util.fname)(cacheConfig.level)](
217
+ context,
218
+ cacheKey,
219
+ data
220
+ );
221
+ if (!cacheHash) {
222
+ return false;
223
+ }
224
+ const cacheSyncOrAsync = () => __async(this, null, function* () {
225
+ const next = data;
226
+ const limit = cacheConfig.staleLimit;
227
+ const storeHTML = (0, import_util.cacheAddition)(html, cacheHash);
228
+ const size = storeHTML.length;
229
+ yield next.caches.set(cacheHash, {
230
+ expireTime: Date.now() + cacheConfig.interval * 1e3,
231
+ limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
232
+ cacheHash,
233
+ html: storeHTML,
234
+ size
235
+ });
236
+ this.cache.set(cacheKey, next);
237
+ return true;
261
238
  });
262
- this.cache.set(cacheKey, next);
263
- return true;
264
- }; // cache set is async, each hash is cached only once at the same time
265
-
266
-
267
- const doCache = (0, _util.withCoalescedInvoke)(cacheSyncOrAsync).bind(null, (0, _util.namespaceHash)('stream', cacheHash), []);
268
- return (0, _util.maybeSync)(doCache)(sync);
239
+ const doCache = (0, import_util.withCoalescedInvoke)(cacheSyncOrAsync).bind(
240
+ null,
241
+ (0, import_util.namespaceHash)("stream", cacheHash),
242
+ []
243
+ );
244
+ return (0, import_util.maybeSync)(doCache)(sync);
245
+ });
269
246
  }
270
-
271
- async del(context, cacheHash) {
272
- const cacheKey = this.generateRequestKey(context);
273
- const data = this.cache.get(cacheKey);
274
- data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
247
+ del(context, cacheHash) {
248
+ return __async(this, null, function* () {
249
+ const cacheKey = this.generateRequestKey(context);
250
+ const data = this.cache.get(cacheKey);
251
+ data == null ? void 0 : data.caches.del(cacheHash);
252
+ });
275
253
  }
276
-
277
254
  }
278
-
279
255
  let manager;
280
-
281
256
  function createCache() {
282
257
  if (manager) {
283
258
  return manager;
284
259
  }
285
-
286
- manager = new CacheManager({
287
- max: 0
288
- });
260
+ manager = new CacheManager({ max: 0 });
289
261
  return manager;
290
262
  }
291
-
292
263
  function destroyCache() {
293
264
  manager = null;
294
265
  }
295
- /* eslint-enable no-lone-blocks */
296
-
297
- /* eslint-enable @typescript-eslint/member-ordering */
266
+ // Annotate the CommonJS export names for ESM import in node:
267
+ 0 && (module.exports = {
268
+ createCache,
269
+ destroyCache
270
+ });
@@ -1,5 +1,15 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+ var type_exports = {};
15
+ module.exports = __toCommonJS(type_exports);