@modern-js/prod-server 2.14.0 → 2.16.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 (122) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/cjs/constants.js +15 -31
  3. package/dist/cjs/index.js +35 -37
  4. package/dist/cjs/libs/context/context.js +53 -66
  5. package/dist/cjs/libs/context/index.js +15 -28
  6. package/dist/cjs/libs/hook-api/index.js +59 -54
  7. package/dist/cjs/libs/hook-api/route.js +28 -30
  8. package/dist/cjs/libs/hook-api/template.js +30 -34
  9. package/dist/cjs/libs/loadConfig.js +74 -65
  10. package/dist/cjs/libs/logger.js +46 -37
  11. package/dist/cjs/libs/metrics.js +7 -25
  12. package/dist/cjs/libs/proxy.js +29 -46
  13. package/dist/cjs/libs/render/cache/__tests__/cache.fun.test.js +48 -64
  14. package/dist/cjs/libs/render/cache/__tests__/cache.test.js +53 -37
  15. package/dist/cjs/libs/render/cache/__tests__/cacheable.js +38 -34
  16. package/dist/cjs/libs/render/cache/__tests__/error-configuration.js +22 -30
  17. package/dist/cjs/libs/render/cache/__tests__/matched-cache.js +86 -46
  18. package/dist/cjs/libs/render/cache/index.js +17 -37
  19. package/dist/cjs/libs/render/cache/page-caches/index.js +12 -28
  20. package/dist/cjs/libs/render/cache/page-caches/lru.js +32 -40
  21. package/dist/cjs/libs/render/cache/spr.js +104 -111
  22. package/dist/cjs/libs/render/cache/type.js +4 -15
  23. package/dist/cjs/libs/render/cache/util.js +29 -53
  24. package/dist/cjs/libs/render/index.js +75 -73
  25. package/dist/cjs/libs/render/measure.js +19 -34
  26. package/dist/cjs/libs/render/reader.js +52 -61
  27. package/dist/cjs/libs/render/ssr.js +34 -66
  28. package/dist/cjs/libs/render/static.js +22 -45
  29. package/dist/cjs/libs/render/type.js +10 -28
  30. package/dist/cjs/libs/render/utils.js +17 -38
  31. package/dist/cjs/libs/route/index.js +32 -31
  32. package/dist/cjs/libs/route/matcher.js +38 -38
  33. package/dist/cjs/libs/route/route.js +29 -26
  34. package/dist/cjs/libs/serveFile.js +23 -41
  35. package/dist/cjs/server/index.js +101 -107
  36. package/dist/cjs/server/modernServer.js +191 -171
  37. package/dist/cjs/server/modernServerSplit.js +12 -30
  38. package/dist/cjs/type.js +4 -15
  39. package/dist/cjs/utils.js +50 -50
  40. package/dist/cjs/workerServer.js +26 -36
  41. package/dist/esm/constants.js +24 -25
  42. package/dist/esm/index.js +11 -10
  43. package/dist/esm/libs/context/context.js +271 -264
  44. package/dist/esm/libs/context/index.js +3 -3
  45. package/dist/esm/libs/hook-api/index.js +237 -223
  46. package/dist/esm/libs/hook-api/route.js +62 -60
  47. package/dist/esm/libs/hook-api/template.js +117 -115
  48. package/dist/esm/libs/loadConfig.js +68 -69
  49. package/dist/esm/libs/logger.js +188 -174
  50. package/dist/esm/libs/metrics.js +6 -3
  51. package/dist/esm/libs/proxy.js +236 -221
  52. package/dist/esm/libs/render/cache/__tests__/cache.fun.test.js +286 -267
  53. package/dist/esm/libs/render/cache/__tests__/cache.test.js +765 -745
  54. package/dist/esm/libs/render/cache/__tests__/cacheable.js +62 -63
  55. package/dist/esm/libs/render/cache/__tests__/error-configuration.js +42 -43
  56. package/dist/esm/libs/render/cache/__tests__/matched-cache.js +134 -135
  57. package/dist/esm/libs/render/cache/index.js +337 -326
  58. package/dist/esm/libs/render/cache/page-caches/index.js +147 -141
  59. package/dist/esm/libs/render/cache/page-caches/lru.js +78 -76
  60. package/dist/esm/libs/render/cache/spr.js +465 -456
  61. package/dist/esm/libs/render/cache/type.js +1 -1
  62. package/dist/esm/libs/render/cache/util.js +266 -246
  63. package/dist/esm/libs/render/index.js +226 -218
  64. package/dist/esm/libs/render/measure.js +136 -128
  65. package/dist/esm/libs/render/reader.js +315 -304
  66. package/dist/esm/libs/render/ssr.js +214 -209
  67. package/dist/esm/libs/render/static.js +208 -198
  68. package/dist/esm/libs/render/type.js +6 -7
  69. package/dist/esm/libs/render/utils.js +8 -9
  70. package/dist/esm/libs/route/index.js +134 -123
  71. package/dist/esm/libs/route/matcher.js +140 -130
  72. package/dist/esm/libs/route/route.js +36 -37
  73. package/dist/esm/libs/serveFile.js +177 -168
  74. package/dist/esm/server/index.js +578 -549
  75. package/dist/esm/server/modernServer.js +1137 -1074
  76. package/dist/esm/server/modernServerSplit.js +344 -328
  77. package/dist/esm/type.js +1 -1
  78. package/dist/esm/utils.js +133 -124
  79. package/dist/esm/workerServer.js +226 -220
  80. package/dist/esm-node/constants.js +7 -14
  81. package/dist/esm-node/index.js +4 -9
  82. package/dist/esm-node/libs/context/context.js +32 -25
  83. package/dist/esm-node/libs/context/index.js +2 -5
  84. package/dist/esm-node/libs/hook-api/index.js +40 -21
  85. package/dist/esm-node/libs/hook-api/route.js +21 -8
  86. package/dist/esm-node/libs/hook-api/template.js +19 -14
  87. package/dist/esm-node/libs/loadConfig.js +11 -28
  88. package/dist/esm-node/libs/logger.js +37 -17
  89. package/dist/esm-node/libs/metrics.js +1 -3
  90. package/dist/esm-node/libs/proxy.js +14 -22
  91. package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +25 -36
  92. package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +23 -11
  93. package/dist/esm-node/libs/render/cache/__tests__/cacheable.js +32 -13
  94. package/dist/esm-node/libs/render/cache/__tests__/error-configuration.js +16 -9
  95. package/dist/esm-node/libs/render/cache/__tests__/matched-cache.js +80 -25
  96. package/dist/esm-node/libs/render/cache/index.js +3 -10
  97. package/dist/esm-node/libs/render/cache/page-caches/index.js +4 -5
  98. package/dist/esm-node/libs/render/cache/page-caches/lru.js +20 -8
  99. package/dist/esm-node/libs/render/cache/spr.js +79 -80
  100. package/dist/esm-node/libs/render/cache/type.js +1 -0
  101. package/dist/esm-node/libs/render/cache/util.js +13 -23
  102. package/dist/esm-node/libs/render/index.js +10 -28
  103. package/dist/esm-node/libs/render/measure.js +7 -13
  104. package/dist/esm-node/libs/render/reader.js +31 -26
  105. package/dist/esm-node/libs/render/ssr.js +5 -21
  106. package/dist/esm-node/libs/render/static.js +1 -4
  107. package/dist/esm-node/libs/render/type.js +3 -6
  108. package/dist/esm-node/libs/render/utils.js +6 -18
  109. package/dist/esm-node/libs/route/index.js +20 -9
  110. package/dist/esm-node/libs/route/matcher.js +28 -17
  111. package/dist/esm-node/libs/route/route.js +23 -5
  112. package/dist/esm-node/libs/serveFile.js +2 -6
  113. package/dist/esm-node/server/index.js +72 -75
  114. package/dist/esm-node/server/modernServer.js +102 -130
  115. package/dist/esm-node/server/modernServerSplit.js +1 -4
  116. package/dist/esm-node/type.js +1 -0
  117. package/dist/esm-node/utils.js +41 -32
  118. package/dist/esm-node/workerServer.js +9 -10
  119. package/dist/types/libs/context/context.d.ts +1 -1
  120. package/dist/types/server/modernServer.d.ts +1 -1
  121. package/dist/types/utils.d.ts +1 -1
  122. package/package.json +12 -8
@@ -1,12 +1,16 @@
1
- const errorConfiguration = [
2
- { level: 1 },
1
+ export const errorConfiguration = [
2
+ {
3
+ level: 1
4
+ },
3
5
  {
4
6
  level: 1,
5
7
  includes: {}
6
8
  },
7
9
  {
8
10
  level: 1,
9
- includes: { query: [] }
11
+ includes: {
12
+ query: []
13
+ }
10
14
  },
11
15
  {
12
16
  level: 2,
@@ -14,15 +18,21 @@ const errorConfiguration = [
14
18
  },
15
19
  {
16
20
  level: 2,
17
- includes: { header: [] }
21
+ includes: {
22
+ header: []
23
+ }
18
24
  },
19
25
  {
20
26
  level: 3,
21
- includes: { header: [] }
27
+ includes: {
28
+ header: []
29
+ }
22
30
  },
23
31
  {
24
32
  level: 3,
25
- includes: { query: [] }
33
+ includes: {
34
+ query: []
35
+ }
26
36
  },
27
37
  {
28
38
  level: 3,
@@ -32,6 +42,3 @@ const errorConfiguration = [
32
42
  }
33
43
  }
34
44
  ];
35
- export {
36
- errorConfiguration
37
- };
@@ -1,27 +1,43 @@
1
- const matchedCacheableAry = [
1
+ export const matchedCacheableAry = [
2
2
  [
3
3
  {
4
4
  requestOpt: {
5
5
  url: "/level-one",
6
6
  headers: {},
7
- query: { name: "byted" }
7
+ query: {
8
+ name: "byted"
9
+ }
8
10
  },
9
11
  cacheConfig: {
10
12
  level: 1,
11
- includes: { query: ["name"] },
12
- matches: { query: { name: { weixin: "^byted" } } }
13
+ includes: {
14
+ query: [
15
+ "name"
16
+ ]
17
+ },
18
+ matches: {
19
+ query: {
20
+ name: {
21
+ weixin: "^byted"
22
+ }
23
+ }
24
+ }
13
25
  },
14
26
  content: "level1"
15
27
  },
16
28
  {
17
29
  url: "/level-one",
18
30
  headers: {},
19
- query: { name: "byted_likely" }
31
+ query: {
32
+ name: "byted_likely"
33
+ }
20
34
  },
21
35
  {
22
36
  url: "/level-one",
23
37
  headers: {},
24
- query: { name: "not_byted" }
38
+ query: {
39
+ name: "not_byted"
40
+ }
25
41
  }
26
42
  ],
27
43
  [
@@ -29,63 +45,102 @@ const matchedCacheableAry = [
29
45
  requestOpt: {
30
46
  url: "/level-two",
31
47
  query: {},
32
- headers: { age: "17" }
48
+ headers: {
49
+ age: "17"
50
+ }
33
51
  },
34
52
  cacheConfig: {
35
53
  level: 2,
36
- includes: { header: ["age"] },
37
- matches: { header: { age: { one: "^1" } } }
54
+ includes: {
55
+ header: [
56
+ "age"
57
+ ]
58
+ },
59
+ matches: {
60
+ header: {
61
+ age: {
62
+ one: "^1"
63
+ }
64
+ }
65
+ }
38
66
  },
39
67
  content: "level2"
40
68
  },
41
69
  {
42
70
  url: "/level-two",
43
71
  query: {},
44
- headers: { age: "11" }
72
+ headers: {
73
+ age: "11"
74
+ }
45
75
  },
46
76
  {
47
77
  url: "/level-two",
48
78
  query: {},
49
- headers: { age: "22" }
79
+ headers: {
80
+ age: "22"
81
+ }
50
82
  }
51
83
  ],
52
84
  [
53
85
  {
54
86
  requestOpt: {
55
87
  url: "/level-three",
56
- headers: { age: "17" },
57
- query: { name: "byted" }
88
+ headers: {
89
+ age: "17"
90
+ },
91
+ query: {
92
+ name: "byted"
93
+ }
58
94
  },
59
95
  cacheConfig: {
60
96
  level: 3,
61
97
  includes: {
62
- query: ["name"],
63
- header: ["age"]
98
+ query: [
99
+ "name"
100
+ ],
101
+ header: [
102
+ "age"
103
+ ]
64
104
  },
65
105
  matches: {
66
- query: { name: { weixin: "^byted" } },
67
- header: { age: { one: "^1" } }
106
+ query: {
107
+ name: {
108
+ weixin: "^byted"
109
+ }
110
+ },
111
+ header: {
112
+ age: {
113
+ one: "^1"
114
+ }
115
+ }
68
116
  }
69
117
  },
70
118
  content: "level3"
71
119
  },
72
120
  {
73
121
  url: "/level-three",
74
- query: { name: "byted_likely" },
75
- headers: { age: "19" }
122
+ query: {
123
+ name: "byted_likely"
124
+ },
125
+ headers: {
126
+ age: "19"
127
+ }
76
128
  },
77
129
  {
78
130
  url: "/level-three",
79
- query: { name: "byted_likely" },
131
+ query: {
132
+ name: "byted_likely"
133
+ },
80
134
  headers: {}
81
135
  },
82
136
  {
83
137
  url: "/level-three",
84
- query: { name: "not_byted" },
85
- headers: { age: "19" }
138
+ query: {
139
+ name: "not_byted"
140
+ },
141
+ headers: {
142
+ age: "19"
143
+ }
86
144
  }
87
145
  ]
88
146
  ];
89
- export {
90
- matchedCacheableAry
91
- };
@@ -2,7 +2,7 @@ import { Transform } from "stream";
2
2
  import { ERROR_DIGEST } from "../../../constants";
3
3
  import { createCache } from "./spr";
4
4
  import { namespaceHash, withCoalescedInvoke } from "./util";
5
- var cache_default = (renderFn, ctx) => {
5
+ export default (renderFn, ctx) => {
6
6
  const sprCache = createCache();
7
7
  const doRender = async (context) => {
8
8
  const cacheContext = {
@@ -40,16 +40,12 @@ var cache_default = (renderFn, ctx) => {
40
40
  const renderResult = await renderFn(context);
41
41
  return afterRender(renderResult, saveHtmlIntoCache);
42
42
  }
43
- const cacheHash = cacheFile == null ? void 0 : cacheFile.hash;
43
+ const cacheHash = cacheFile === null || cacheFile === void 0 ? void 0 : cacheFile.hash;
44
44
  if (cacheFile.isGarbage) {
45
45
  const renderResult = await renderFn(context);
46
46
  return afterRender(renderResult, saveHtmlIntoCache);
47
47
  } else if (cacheFile.isStale) {
48
- const render = withCoalescedInvoke(() => renderFn(context)).bind(
49
- null,
50
- namespaceHash("render", cacheFile.hash),
51
- []
52
- );
48
+ const render = withCoalescedInvoke(() => renderFn(context)).bind(null, namespaceHash("render", cacheFile.hash), []);
53
49
  render().then(async (res) => {
54
50
  if (res.value && res.isOrigin) {
55
51
  const { cacheConfig } = context;
@@ -71,6 +67,3 @@ var cache_default = (renderFn, ctx) => {
71
67
  };
72
68
  return doRender;
73
69
  };
74
- export {
75
- cache_default as default
76
- };
@@ -1,10 +1,9 @@
1
1
  import { LRUCaches } from "./lru";
2
- async function createPageCaches(max) {
3
- const constructorOptions = { max };
2
+ export async function createPageCaches(max) {
3
+ const constructorOptions = {
4
+ max
5
+ };
4
6
  const cacheInstance = new LRUCaches(constructorOptions);
5
7
  await cacheInstance.init();
6
8
  return cacheInstance;
7
9
  }
8
- export {
9
- createPageCaches
10
- };
@@ -1,9 +1,18 @@
1
- import LRU from "lru-cache";
2
- class LRUCaches {
3
- constructor(options) {
4
- this.max = options.max;
5
- this.caches = new LRU(this.max);
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
6
11
  }
12
+ return obj;
13
+ }
14
+ import LRU from "lru-cache";
15
+ export class LRUCaches {
7
16
  init() {
8
17
  return Promise.resolve();
9
18
  }
@@ -23,7 +32,10 @@ class LRUCaches {
23
32
  del(key) {
24
33
  this.caches.del(key);
25
34
  }
35
+ constructor(options) {
36
+ _define_property(this, "caches", void 0);
37
+ _define_property(this, "max", void 0);
38
+ this.max = options.max;
39
+ this.caches = new LRU(this.max);
40
+ }
26
41
  }
27
- export {
28
- LRUCaches
29
- };
@@ -1,15 +1,20 @@
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
1
14
  import crypto from "crypto";
2
15
  import LRUCache from "lru-cache";
3
16
  import { mime } from "@modern-js/utils";
4
- import {
5
- cacheAddition,
6
- connectFactor,
7
- fname,
8
- maybeSync,
9
- namespaceHash,
10
- valueFactory,
11
- withCoalescedInvoke
12
- } from "./util";
17
+ import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "./util";
13
18
  import { createPageCaches } from "./page-caches";
14
19
  const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
15
20
  const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
@@ -18,52 +23,6 @@ const QUERY_LEVEL = 1;
18
23
  const HEADER_LEVEL = 2;
19
24
  const QUERY_HEADER_LEVEL = 3;
20
25
  class CacheManager {
21
- constructor(cacheOptions) {
22
- this.find = (() => {
23
- {
24
- const _this = this;
25
- return {
26
- [fname(BASE_LEVEL)](context, cacheKey) {
27
- return _this.md5(cacheKey);
28
- },
29
- [fname(QUERY_LEVEL)](context, cacheKey, data) {
30
- const queryFactor = _this.queryFactor(context, data);
31
- if (!queryFactor) {
32
- return null;
33
- }
34
- return _this.md5(connectFactor(cacheKey, queryFactor));
35
- },
36
- [fname(HEADER_LEVEL)](context, cacheKey, data) {
37
- const headerFactor = _this.headerFactor(context, data);
38
- if (!headerFactor) {
39
- return null;
40
- }
41
- return _this.md5(connectFactor(cacheKey, headerFactor));
42
- },
43
- [fname(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
44
- const queryFactor = _this.queryFactor(context, data);
45
- const headerFactor = _this.headerFactor(context, data);
46
- if (!queryFactor || !headerFactor) {
47
- return null;
48
- }
49
- return _this.md5(connectFactor(cacheKey, headerFactor, queryFactor));
50
- }
51
- };
52
- }
53
- })();
54
- this.cacheOptions = cacheOptions;
55
- this.cache = new LRUCache({
56
- max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
57
- // 默认存 100M,最大 600M
58
- length(n) {
59
- const len = n.caches.keys().reduce((total, cur) => {
60
- var _a;
61
- return total + (((_a = n.caches.peek(cur)) == null ? void 0 : _a.size) || 0);
62
- }, 1);
63
- return len;
64
- }
65
- });
66
- }
67
26
  md5(content) {
68
27
  const md5 = crypto.createHash("md5");
69
28
  return md5.update(content).digest("hex");
@@ -93,14 +52,17 @@ class CacheManager {
93
52
  if (matcher) {
94
53
  value = this.replaceValue(value, matcher);
95
54
  }
96
- return ary.concat([key, value]);
55
+ return ary.concat([
56
+ key,
57
+ value
58
+ ]);
97
59
  }, []);
98
60
  return factorAry.join(",");
99
61
  }
100
62
  queryFactor(context, data) {
101
- var _a, _b;
102
- const queryKeys = (_a = data.includes) == null ? void 0 : _a.query;
103
- const queryMatches = (_b = data.matches) == null ? void 0 : _b.query;
63
+ var _data_includes, _data_matches;
64
+ const queryKeys = (_data_includes = data.includes) === null || _data_includes === void 0 ? void 0 : _data_includes.query;
65
+ const queryMatches = (_data_matches = data.matches) === null || _data_matches === void 0 ? void 0 : _data_matches.query;
104
66
  if (!queryKeys || queryKeys.length === 0) {
105
67
  return null;
106
68
  }
@@ -109,9 +71,9 @@ class CacheManager {
109
71
  return queryFactor;
110
72
  }
111
73
  headerFactor(context, data) {
112
- var _a, _b;
113
- const headerKeys = (_a = data.includes) == null ? void 0 : _a.header;
114
- const headerMatches = (_b = data.matches) == null ? void 0 : _b.header;
74
+ var _data_includes, _data_matches;
75
+ const headerKeys = (_data_includes = data.includes) === null || _data_includes === void 0 ? void 0 : _data_includes.header;
76
+ const headerMatches = (_data_matches = data.matches) === null || _data_matches === void 0 ? void 0 : _data_matches.header;
115
77
  if (!headerKeys || headerKeys.length === 0) {
116
78
  return null;
117
79
  }
@@ -168,11 +130,7 @@ class CacheManager {
168
130
  const caches = await createPageCaches(MAX_CACHE_EACH_REQ);
169
131
  data = this.createCacheContent(cacheConfig, caches);
170
132
  }
171
- const cacheHash = this.find[fname(cacheConfig.level)](
172
- context,
173
- cacheKey,
174
- data
175
- );
133
+ const cacheHash = this.find[fname(cacheConfig.level)](context, cacheKey, data);
176
134
  if (!cacheHash) {
177
135
  return false;
178
136
  }
@@ -191,31 +149,72 @@ class CacheManager {
191
149
  this.cache.set(cacheKey, next);
192
150
  return true;
193
151
  };
194
- const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(
195
- null,
196
- namespaceHash("stream", cacheHash),
197
- []
198
- );
152
+ const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(null, namespaceHash("stream", cacheHash), []);
199
153
  return maybeSync(doCache)(sync);
200
154
  }
201
155
  async del(context, cacheHash) {
202
156
  const cacheKey = this.generateRequestKey(context);
203
157
  const data = this.cache.get(cacheKey);
204
- data == null ? void 0 : data.caches.del(cacheHash);
158
+ data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
159
+ }
160
+ constructor(cacheOptions) {
161
+ _define_property(this, "cache", void 0);
162
+ _define_property(this, "cacheOptions", void 0);
163
+ _define_property(this, "find", (() => {
164
+ {
165
+ const _this = this;
166
+ return {
167
+ [fname(BASE_LEVEL)](context, cacheKey) {
168
+ return _this.md5(cacheKey);
169
+ },
170
+ [fname(QUERY_LEVEL)](context, cacheKey, data) {
171
+ const queryFactor = _this.queryFactor(context, data);
172
+ if (!queryFactor) {
173
+ return null;
174
+ }
175
+ return _this.md5(connectFactor(cacheKey, queryFactor));
176
+ },
177
+ [fname(HEADER_LEVEL)](context, cacheKey, data) {
178
+ const headerFactor = _this.headerFactor(context, data);
179
+ if (!headerFactor) {
180
+ return null;
181
+ }
182
+ return _this.md5(connectFactor(cacheKey, headerFactor));
183
+ },
184
+ [fname(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
185
+ const queryFactor = _this.queryFactor(context, data);
186
+ const headerFactor = _this.headerFactor(context, data);
187
+ if (!queryFactor || !headerFactor) {
188
+ return null;
189
+ }
190
+ return _this.md5(connectFactor(cacheKey, headerFactor, queryFactor));
191
+ }
192
+ };
193
+ }
194
+ })());
195
+ this.cacheOptions = cacheOptions;
196
+ this.cache = new LRUCache({
197
+ max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
198
+ length(n) {
199
+ const len = n.caches.keys().reduce((total, cur) => {
200
+ var _n_caches_peek;
201
+ return total + (((_n_caches_peek = n.caches.peek(cur)) === null || _n_caches_peek === void 0 ? void 0 : _n_caches_peek.size) || 0);
202
+ }, 1);
203
+ return len;
204
+ }
205
+ });
205
206
  }
206
207
  }
207
208
  let manager;
208
- function createCache() {
209
+ export function createCache() {
209
210
  if (manager) {
210
211
  return manager;
211
212
  }
212
- manager = new CacheManager({ max: 0 });
213
+ manager = new CacheManager({
214
+ max: 0
215
+ });
213
216
  return manager;
214
217
  }
215
- function destroyCache() {
218
+ export function destroyCache() {
216
219
  manager = null;
217
220
  }
218
- export {
219
- createCache,
220
- destroyCache
221
- };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,14 +1,14 @@
1
1
  import url from "url";
2
- function namespaceHash(namespace, hash) {
2
+ export function namespaceHash(namespace, hash) {
3
3
  return `${namespace}/${hash}`;
4
4
  }
5
- function fname(lv) {
5
+ export function fname(lv) {
6
6
  return `f${lv}`;
7
7
  }
8
- function connectFactor(...args) {
8
+ export function connectFactor(...args) {
9
9
  return args.join("-");
10
10
  }
11
- function valueFactory(obj) {
11
+ export function valueFactory(obj) {
12
12
  if (obj instanceof url.URLSearchParams) {
13
13
  return function(key) {
14
14
  return obj.get(key);
@@ -23,19 +23,16 @@ function valueFactory(obj) {
23
23
  };
24
24
  }
25
25
  }
26
- function getTime([s, ns]) {
26
+ export function getTime([s, ns]) {
27
27
  return Math.floor(s * 1e3 + ns / 1e6);
28
28
  }
29
29
  const RE_START_IN_HEAD = /<head>/;
30
- function cacheAddition(html, hash) {
31
- const additionHtml = html.replace(
32
- RE_START_IN_HEAD,
33
- `<head><meta name="x-moden-spr" content="${hash}">`
34
- );
30
+ export function cacheAddition(html, hash) {
31
+ const additionHtml = html.replace(RE_START_IN_HEAD, `<head><meta name="x-moden-spr" content="${hash}">`);
35
32
  return additionHtml;
36
33
  }
37
34
  const globalInvokeCache = /* @__PURE__ */ new Map();
38
- function withCoalescedInvoke(func) {
35
+ export function withCoalescedInvoke(func) {
39
36
  return async function(key, args) {
40
37
  const entry = globalInvokeCache.get(key);
41
38
  if (entry) {
@@ -49,7 +46,10 @@ function withCoalescedInvoke(func) {
49
46
  }
50
47
  const future = __wrapper().then((res) => {
51
48
  globalInvokeCache.delete(key);
52
- return { isOrigin: true, value: res };
49
+ return {
50
+ isOrigin: true,
51
+ value: res
52
+ };
53
53
  }).catch((err) => {
54
54
  globalInvokeCache.delete(key);
55
55
  throw err;
@@ -58,7 +58,7 @@ function withCoalescedInvoke(func) {
58
58
  return future;
59
59
  };
60
60
  }
61
- function maybeSync(fn) {
61
+ export function maybeSync(fn) {
62
62
  return (sync) => {
63
63
  if (sync) {
64
64
  return fn();
@@ -68,13 +68,3 @@ function maybeSync(fn) {
68
68
  }
69
69
  };
70
70
  }
71
- export {
72
- cacheAddition,
73
- connectFactor,
74
- fname,
75
- getTime,
76
- maybeSync,
77
- namespaceHash,
78
- valueFactory,
79
- withCoalescedInvoke
80
- };
@@ -5,15 +5,7 @@ import { handleDirectory } from "./static";
5
5
  import { readFile } from "./reader";
6
6
  import * as ssr from "./ssr";
7
7
  import { injectServerData } from "./utils";
8
- const createRenderHandler = ({
9
- distDir,
10
- staticGenerate,
11
- forceCSR
12
- }) => async function render({
13
- ctx,
14
- route,
15
- runner
16
- }) {
8
+ export const createRenderHandler = ({ distDir, staticGenerate, forceCSR }) => async function render({ ctx, route, runner }) {
17
9
  if (ctx.resHasHandled()) {
18
10
  return null;
19
11
  }
@@ -34,24 +26,17 @@ const createRenderHandler = ({
34
26
  const useCSR = forceCSR && ctx.query.csr;
35
27
  if (route.isSSR && !useCSR) {
36
28
  try {
37
- const result = await ssr.render(
38
- ctx,
39
- {
40
- distDir,
41
- entryName: route.entryName,
42
- urlPath: route.urlPath,
43
- bundle: route.bundle,
44
- template: content.toString(),
45
- staticGenerate
46
- },
47
- runner
48
- );
29
+ const result = await ssr.render(ctx, {
30
+ distDir,
31
+ entryName: route.entryName,
32
+ urlPath: route.urlPath,
33
+ bundle: route.bundle,
34
+ template: content.toString(),
35
+ staticGenerate
36
+ }, runner);
49
37
  return result;
50
38
  } catch (err) {
51
- ctx.error(
52
- ERROR_DIGEST.ERENDER,
53
- err.stack || err.message
54
- );
39
+ ctx.error(ERROR_DIGEST.ERENDER, err.stack || err.message);
55
40
  ctx.res.setHeader("x-modern-ssr-fallback", "1");
56
41
  }
57
42
  }
@@ -60,6 +45,3 @@ const createRenderHandler = ({
60
45
  contentType: mime.contentType(path.extname(templatePath))
61
46
  };
62
47
  };
63
- export {
64
- createRenderHandler
65
- };