@modern-js/prod-server 2.9.1-alpha.0 → 2.11.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 (139) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/dist/cjs/libs/hook-api/index.js +7 -26
  3. package/dist/cjs/libs/render/index.js +2 -1
  4. package/dist/cjs/libs/render/ssr.js +7 -5
  5. package/dist/{js/node/libs/metrics.js → cjs/libs/render/utils.js} +25 -12
  6. package/dist/cjs/server/index.js +1 -1
  7. package/dist/cjs/server/modernServer.js +75 -80
  8. package/dist/esm/libs/hook-api/index.js +9 -37
  9. package/dist/esm/libs/render/index.js +2 -1
  10. package/dist/esm/libs/render/ssr.js +6 -4
  11. package/dist/esm/libs/render/utils.js +12 -0
  12. package/dist/esm/server/index.js +2 -2
  13. package/dist/esm/server/modernServer.js +108 -103
  14. package/dist/esm-node/libs/hook-api/index.js +7 -26
  15. package/dist/esm-node/libs/render/index.js +2 -1
  16. package/dist/esm-node/libs/render/ssr.js +7 -7
  17. package/dist/esm-node/libs/render/utils.js +23 -0
  18. package/dist/esm-node/server/index.js +1 -1
  19. package/dist/esm-node/server/modernServer.js +75 -80
  20. package/dist/types/libs/context/context.d.ts +1 -1
  21. package/dist/types/libs/render/utils.d.ts +5 -0
  22. package/dist/types/server/index.d.ts +2 -1
  23. package/dist/types/server/modernServer.d.ts +4 -4
  24. package/dist/types/type.d.ts +1 -1
  25. package/dist/types/utils.d.ts +1 -1
  26. package/package.json +9 -7
  27. package/dist/js/modern/constants.js +0 -35
  28. package/dist/js/modern/index.js +0 -18
  29. package/dist/js/modern/libs/context/context.js +0 -160
  30. package/dist/js/modern/libs/context/index.js +0 -6
  31. package/dist/js/modern/libs/hook-api/index.js +0 -134
  32. package/dist/js/modern/libs/hook-api/route.js +0 -20
  33. package/dist/js/modern/libs/hook-api/template.js +0 -73
  34. package/dist/js/modern/libs/loadConfig.js +0 -62
  35. package/dist/js/modern/libs/logger.js +0 -111
  36. package/dist/js/modern/libs/metrics.js +0 -11
  37. package/dist/js/modern/libs/proxy.js +0 -92
  38. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -114
  39. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -254
  40. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +0 -47
  41. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +0 -37
  42. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +0 -91
  43. package/dist/js/modern/libs/render/cache/index.js +0 -115
  44. package/dist/js/modern/libs/render/cache/page-caches/index.js +0 -32
  45. package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -29
  46. package/dist/js/modern/libs/render/cache/spr.js +0 -248
  47. package/dist/js/modern/libs/render/cache/type.js +0 -0
  48. package/dist/js/modern/libs/render/cache/util.js +0 -102
  49. package/dist/js/modern/libs/render/index.js +0 -86
  50. package/dist/js/modern/libs/render/measure.js +0 -68
  51. package/dist/js/modern/libs/render/reader.js +0 -107
  52. package/dist/js/modern/libs/render/ssr.js +0 -100
  53. package/dist/js/modern/libs/render/static.js +0 -60
  54. package/dist/js/modern/libs/render/type.js +0 -9
  55. package/dist/js/modern/libs/route/index.js +0 -54
  56. package/dist/js/modern/libs/route/matcher.js +0 -87
  57. package/dist/js/modern/libs/route/route.js +0 -16
  58. package/dist/js/modern/libs/serve-file.js +0 -67
  59. package/dist/js/modern/server/index.js +0 -208
  60. package/dist/js/modern/server/modern-server-split.js +0 -74
  61. package/dist/js/modern/server/modern-server.js +0 -554
  62. package/dist/js/modern/type.js +0 -0
  63. package/dist/js/modern/utils.js +0 -136
  64. package/dist/js/modern/worker-server.js +0 -89
  65. package/dist/js/node/constants.js +0 -62
  66. package/dist/js/node/index.js +0 -44
  67. package/dist/js/node/libs/context/context.js +0 -189
  68. package/dist/js/node/libs/context/index.js +0 -30
  69. package/dist/js/node/libs/hook-api/index.js +0 -164
  70. package/dist/js/node/libs/hook-api/route.js +0 -43
  71. package/dist/js/node/libs/hook-api/template.js +0 -97
  72. package/dist/js/node/libs/loadConfig.js +0 -91
  73. package/dist/js/node/libs/logger.js +0 -133
  74. package/dist/js/node/libs/proxy.js +0 -114
  75. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -115
  76. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -245
  77. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +0 -70
  78. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +0 -60
  79. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +0 -114
  80. package/dist/js/node/libs/render/cache/index.js +0 -134
  81. package/dist/js/node/libs/render/cache/page-caches/index.js +0 -55
  82. package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -58
  83. package/dist/js/node/libs/render/cache/spr.js +0 -270
  84. package/dist/js/node/libs/render/cache/type.js +0 -15
  85. package/dist/js/node/libs/render/cache/util.js +0 -138
  86. package/dist/js/node/libs/render/index.js +0 -115
  87. package/dist/js/node/libs/render/measure.js +0 -90
  88. package/dist/js/node/libs/render/reader.js +0 -140
  89. package/dist/js/node/libs/render/ssr.js +0 -123
  90. package/dist/js/node/libs/render/static.js +0 -89
  91. package/dist/js/node/libs/render/type.js +0 -32
  92. package/dist/js/node/libs/route/index.js +0 -78
  93. package/dist/js/node/libs/route/matcher.js +0 -106
  94. package/dist/js/node/libs/route/route.js +0 -39
  95. package/dist/js/node/libs/serve-file.js +0 -97
  96. package/dist/js/node/server/index.js +0 -219
  97. package/dist/js/node/server/modern-server-split.js +0 -97
  98. package/dist/js/node/server/modern-server.js +0 -559
  99. package/dist/js/node/type.js +0 -15
  100. package/dist/js/node/utils.js +0 -166
  101. package/dist/js/node/worker-server.js +0 -113
  102. package/dist/js/treeshaking/constants.js +0 -29
  103. package/dist/js/treeshaking/index.js +0 -13
  104. package/dist/js/treeshaking/libs/context/context.js +0 -274
  105. package/dist/js/treeshaking/libs/context/index.js +0 -5
  106. package/dist/js/treeshaking/libs/hook-api/index.js +0 -281
  107. package/dist/js/treeshaking/libs/hook-api/route.js +0 -68
  108. package/dist/js/treeshaking/libs/hook-api/template.js +0 -127
  109. package/dist/js/treeshaking/libs/loadConfig.js +0 -82
  110. package/dist/js/treeshaking/libs/logger.js +0 -205
  111. package/dist/js/treeshaking/libs/metrics.js +0 -6
  112. package/dist/js/treeshaking/libs/proxy.js +0 -244
  113. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +0 -291
  114. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +0 -781
  115. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +0 -67
  116. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +0 -45
  117. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +0 -147
  118. package/dist/js/treeshaking/libs/render/cache/index.js +0 -346
  119. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +0 -154
  120. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +0 -84
  121. package/dist/js/treeshaking/libs/render/cache/spr.js +0 -492
  122. package/dist/js/treeshaking/libs/render/cache/type.js +0 -1
  123. package/dist/js/treeshaking/libs/render/cache/util.js +0 -280
  124. package/dist/js/treeshaking/libs/render/index.js +0 -234
  125. package/dist/js/treeshaking/libs/render/measure.js +0 -146
  126. package/dist/js/treeshaking/libs/render/reader.js +0 -339
  127. package/dist/js/treeshaking/libs/render/ssr.js +0 -223
  128. package/dist/js/treeshaking/libs/render/static.js +0 -216
  129. package/dist/js/treeshaking/libs/render/type.js +0 -7
  130. package/dist/js/treeshaking/libs/route/index.js +0 -130
  131. package/dist/js/treeshaking/libs/route/matcher.js +0 -143
  132. package/dist/js/treeshaking/libs/route/route.js +0 -40
  133. package/dist/js/treeshaking/libs/serve-file.js +0 -184
  134. package/dist/js/treeshaking/server/index.js +0 -505
  135. package/dist/js/treeshaking/server/modern-server-split.js +0 -360
  136. package/dist/js/treeshaking/server/modern-server.js +0 -1089
  137. package/dist/js/treeshaking/type.js +0 -1
  138. package/dist/js/treeshaking/utils.js +0 -147
  139. package/dist/js/treeshaking/worker-server.js +0 -233
@@ -1,254 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropNames = Object.getOwnPropertyNames;
3
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
6
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
- var __spreadValues = (a, b) => {
8
- for (var prop in b || (b = {}))
9
- if (__hasOwnProp.call(b, prop))
10
- __defNormalProp(a, prop, b[prop]);
11
- if (__getOwnPropSymbols)
12
- for (var prop of __getOwnPropSymbols(b)) {
13
- if (__propIsEnum.call(b, prop))
14
- __defNormalProp(a, prop, b[prop]);
15
- }
16
- return a;
17
- };
18
- var __commonJS = (cb, mod) => function __require() {
19
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
20
- };
21
- var __async = (__this, __arguments, generator) => {
22
- return new Promise((resolve, reject) => {
23
- var fulfilled = (value) => {
24
- try {
25
- step(generator.next(value));
26
- } catch (e) {
27
- reject(e);
28
- }
29
- };
30
- var rejected = (value) => {
31
- try {
32
- step(generator.throw(value));
33
- } catch (e) {
34
- reject(e);
35
- }
36
- };
37
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
38
- step((generator = generator.apply(__this, __arguments)).next());
39
- });
40
- };
41
- import { createCache, destroyCache } from "../spr";
42
- import { errorConfiguration } from "./error-configuration";
43
- import { cacheabelAry } from "./cacheable";
44
- import { matchedCacheableAry } from "./matched-cache";
45
- var require_cache_test = __commonJS({
46
- "src/libs/render/cache/__tests__/cache.test.ts"(exports) {
47
- const createCacheConfig = (config = {}) => __spreadValues({
48
- excludes: null,
49
- includes: null,
50
- interval: 10,
51
- staleLimit: false,
52
- level: 0,
53
- fallback: false,
54
- matches: null
55
- }, config);
56
- jest.setTimeout(6e4);
57
- describe("cache", () => {
58
- it("should cache correctly", () => __async(exports, null, function* () {
59
- destroyCache();
60
- const cache = createCache();
61
- const context = {
62
- entry: "",
63
- pathname: "",
64
- query: {},
65
- headers: {}
66
- };
67
- const content = "hello";
68
- const cacheConfig = createCacheConfig();
69
- yield cache.set(context, content, cacheConfig, true);
70
- const cacheResult = yield cache.get(context);
71
- expect(cacheResult).not.toBe(null);
72
- expect(cacheResult == null ? void 0 : cacheResult.content).toBe("hello");
73
- }));
74
- it("should ignore cache set when cache config not exist", () => __async(exports, null, function* () {
75
- destroyCache();
76
- destroyCache();
77
- const cache = createCache();
78
- const context = {
79
- entry: "",
80
- pathname: "",
81
- query: {},
82
- headers: {}
83
- };
84
- const content = "hello";
85
- const shouldCache = yield cache.set(context, content, null, true);
86
- expect(shouldCache).toBe(false);
87
- }));
88
- it("should calcual cache key error", () => __async(exports, null, function* () {
89
- destroyCache();
90
- const cache = createCache();
91
- const content = "hello";
92
- for (const config of errorConfiguration) {
93
- const cacheConfig = createCacheConfig(config);
94
- const tmpEntry = Math.random().toString();
95
- const context = {
96
- entry: tmpEntry,
97
- pathname: "",
98
- query: {},
99
- headers: {}
100
- };
101
- const shouldCache = yield cache.set(context, content, cacheConfig);
102
- expect(shouldCache).toBe(false);
103
- }
104
- }));
105
- it("should get nothing for diff requestKey", () => __async(exports, null, function* () {
106
- destroyCache();
107
- const cache = createCache();
108
- const context = {
109
- entry: "",
110
- pathname: "",
111
- query: {},
112
- headers: {}
113
- };
114
- const content = "hello";
115
- const cacheConfig = createCacheConfig({
116
- level: 1,
117
- includes: { query: ["name"] }
118
- });
119
- yield cache.set(context, content, cacheConfig, true);
120
- const context_req = {
121
- entry: "",
122
- pathname: "/home",
123
- query: {},
124
- headers: {}
125
- };
126
- const cacheResult = yield cache.get(context_req);
127
- expect(cacheResult).toBe(null);
128
- }));
129
- it("should get nothing for diff cacheHash", () => __async(exports, null, function* () {
130
- destroyCache();
131
- const cache = createCache();
132
- const context = {
133
- entry: "",
134
- pathname: "",
135
- query: {},
136
- headers: {}
137
- };
138
- const content = "hello";
139
- const cacheConfig = createCacheConfig({
140
- level: 1,
141
- includes: { query: ["name"] }
142
- });
143
- yield cache.set(context, content, cacheConfig, true);
144
- const context_req = {
145
- entry: "",
146
- pathname: "",
147
- query: { name: "zll" },
148
- headers: {}
149
- };
150
- const cacheResult = yield cache.get(context_req);
151
- expect(cacheResult).toBe(null);
152
- }));
153
- it("should get cache correctly", () => __async(exports, null, function* () {
154
- destroyCache();
155
- const cache = createCache();
156
- for (const cacheable of cacheabelAry) {
157
- const context = {
158
- entry: "",
159
- pathname: cacheable.requestOpt.url,
160
- query: cacheable.requestOpt.query || {},
161
- headers: cacheable.requestOpt.headers || {}
162
- };
163
- const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
164
- yield cache.set(context, cacheable.content, cacheConfig, true);
165
- const cacheResult = yield cache.get(context);
166
- expect(cacheResult == null ? void 0 : cacheResult.content).toBe(cacheable.content);
167
- }
168
- }));
169
- it("should match cache correctly", () => __async(exports, null, function* () {
170
- destroyCache();
171
- const cache = createCache();
172
- for (const cacheable of matchedCacheableAry) {
173
- const [baseCacheable, matchOne, ...other] = cacheable;
174
- const { requestOpt = {}, cacheConfig, content } = baseCacheable;
175
- const context = {
176
- entry: "",
177
- pathname: requestOpt.url,
178
- query: requestOpt.query,
179
- headers: requestOpt.headers
180
- };
181
- yield cache.set(context, content, createCacheConfig(cacheConfig), true);
182
- const matchContext = {
183
- entry: "",
184
- pathname: matchOne.url,
185
- query: matchOne.query,
186
- headers: matchOne.headers
187
- };
188
- const cacheResult = yield cache.get(matchContext);
189
- expect(cacheResult == null ? void 0 : cacheResult.content).toBe(content);
190
- for (const notMatch of other) {
191
- const notMatchContext = {
192
- entry: "",
193
- pathname: notMatch.url,
194
- query: notMatch.query,
195
- headers: notMatch.headers
196
- };
197
- const nothing = yield cache.get(notMatchContext);
198
- expect(nothing).toBe(null);
199
- }
200
- }
201
- }));
202
- it("should stale cache correctly", () => __async(exports, null, function* () {
203
- destroyCache();
204
- const cache = createCache();
205
- const context = {
206
- entry: "",
207
- pathname: "",
208
- query: {},
209
- headers: {}
210
- };
211
- const config = createCacheConfig({ interval: 5 });
212
- const content = "hello";
213
- const shouldCache = yield cache.set(context, content, config, true);
214
- expect(shouldCache.value).toBe(true);
215
- const freshResult = yield cache.get(context);
216
- expect(freshResult == null ? void 0 : freshResult.isStale).toBe(false);
217
- yield new Promise((resolve) => {
218
- setTimeout(() => {
219
- resolve();
220
- }, 6e3);
221
- });
222
- const staleResult = yield cache.get(context);
223
- expect(staleResult == null ? void 0 : staleResult.isStale).toBe(true);
224
- }));
225
- it("should garbage cache correctly", () => __async(exports, null, function* () {
226
- destroyCache();
227
- const cache = createCache();
228
- const context = {
229
- entry: "",
230
- pathname: "",
231
- query: {},
232
- headers: {}
233
- };
234
- const config = createCacheConfig({
235
- interval: 3,
236
- staleLimit: 8
237
- });
238
- const content = "hello";
239
- const shouldCache = yield cache.set(context, content, config, true);
240
- expect(shouldCache.value).toBe(true);
241
- const freshResult = yield cache.get(context);
242
- expect(freshResult == null ? void 0 : freshResult.isGarbage).toBe(false);
243
- yield new Promise((resolve) => {
244
- setTimeout(() => {
245
- resolve();
246
- }, 1e4);
247
- });
248
- const staleResult = yield cache.get(context);
249
- expect(staleResult == null ? void 0 : staleResult.isGarbage).toBe(true);
250
- }));
251
- });
252
- }
253
- });
254
- export default require_cache_test();
@@ -1,47 +0,0 @@
1
- const cacheabelAry = [
2
- {
3
- requestOpt: { url: "/" },
4
- cacheConfig: {},
5
- content: "level0"
6
- },
7
- {
8
- requestOpt: {
9
- url: "/level-one",
10
- query: { name: "modern" }
11
- },
12
- cacheConfig: {
13
- level: 1,
14
- includes: { query: ["name"] }
15
- },
16
- content: "level1"
17
- },
18
- {
19
- requestOpt: {
20
- url: "/level-two",
21
- headers: { age: "18" }
22
- },
23
- cacheConfig: {
24
- level: 2,
25
- includes: { header: ["age"] }
26
- },
27
- content: "level2"
28
- },
29
- {
30
- requestOpt: {
31
- url: "/level-three",
32
- query: { name: "modern" },
33
- headers: { age: "18" }
34
- },
35
- cacheConfig: {
36
- level: 3,
37
- includes: {
38
- query: ["name"],
39
- header: ["age"]
40
- }
41
- },
42
- content: "level3"
43
- }
44
- ];
45
- export {
46
- cacheabelAry
47
- };
@@ -1,37 +0,0 @@
1
- const errorConfiguration = [
2
- { level: 1 },
3
- {
4
- level: 1,
5
- includes: {}
6
- },
7
- {
8
- level: 1,
9
- includes: { query: [] }
10
- },
11
- {
12
- level: 2,
13
- includes: {}
14
- },
15
- {
16
- level: 2,
17
- includes: { header: [] }
18
- },
19
- {
20
- level: 3,
21
- includes: { header: [] }
22
- },
23
- {
24
- level: 3,
25
- includes: { query: [] }
26
- },
27
- {
28
- level: 3,
29
- includes: {
30
- query: [],
31
- header: []
32
- }
33
- }
34
- ];
35
- export {
36
- errorConfiguration
37
- };
@@ -1,91 +0,0 @@
1
- const matchedCacheableAry = [
2
- [
3
- {
4
- requestOpt: {
5
- url: "/level-one",
6
- headers: {},
7
- query: { name: "byted" }
8
- },
9
- cacheConfig: {
10
- level: 1,
11
- includes: { query: ["name"] },
12
- matches: { query: { name: { weixin: "^byted" } } }
13
- },
14
- content: "level1"
15
- },
16
- {
17
- url: "/level-one",
18
- headers: {},
19
- query: { name: "byted_likely" }
20
- },
21
- {
22
- url: "/level-one",
23
- headers: {},
24
- query: { name: "not_byted" }
25
- }
26
- ],
27
- [
28
- {
29
- requestOpt: {
30
- url: "/level-two",
31
- query: {},
32
- headers: { age: "17" }
33
- },
34
- cacheConfig: {
35
- level: 2,
36
- includes: { header: ["age"] },
37
- matches: { header: { age: { one: "^1" } } }
38
- },
39
- content: "level2"
40
- },
41
- {
42
- url: "/level-two",
43
- query: {},
44
- headers: { age: "11" }
45
- },
46
- {
47
- url: "/level-two",
48
- query: {},
49
- headers: { age: "22" }
50
- }
51
- ],
52
- [
53
- {
54
- requestOpt: {
55
- url: "/level-three",
56
- headers: { age: "17" },
57
- query: { name: "byted" }
58
- },
59
- cacheConfig: {
60
- level: 3,
61
- includes: {
62
- query: ["name"],
63
- header: ["age"]
64
- },
65
- matches: {
66
- query: { name: { weixin: "^byted" } },
67
- header: { age: { one: "^1" } }
68
- }
69
- },
70
- content: "level3"
71
- },
72
- {
73
- url: "/level-three",
74
- query: { name: "byted_likely" },
75
- headers: { age: "19" }
76
- },
77
- {
78
- url: "/level-three",
79
- query: { name: "byted_likely" },
80
- headers: {}
81
- },
82
- {
83
- url: "/level-three",
84
- query: { name: "not_byted" },
85
- headers: { age: "19" }
86
- }
87
- ]
88
- ];
89
- export {
90
- matchedCacheableAry
91
- };
@@ -1,115 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
- var __hasOwnProp = Object.prototype.hasOwnProperty;
4
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __spreadValues = (a, b) => {
7
- for (var prop in b || (b = {}))
8
- if (__hasOwnProp.call(b, prop))
9
- __defNormalProp(a, prop, b[prop]);
10
- if (__getOwnPropSymbols)
11
- for (var prop of __getOwnPropSymbols(b)) {
12
- if (__propIsEnum.call(b, prop))
13
- __defNormalProp(a, prop, b[prop]);
14
- }
15
- return a;
16
- };
17
- var __async = (__this, __arguments, generator) => {
18
- return new Promise((resolve, reject) => {
19
- var fulfilled = (value) => {
20
- try {
21
- step(generator.next(value));
22
- } catch (e) {
23
- reject(e);
24
- }
25
- };
26
- var rejected = (value) => {
27
- try {
28
- step(generator.throw(value));
29
- } catch (e) {
30
- reject(e);
31
- }
32
- };
33
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
34
- step((generator = generator.apply(__this, __arguments)).next());
35
- });
36
- };
37
- import { Transform } from "stream";
38
- import { ERROR_DIGEST } from "../../../constants";
39
- import { createCache } from "./spr";
40
- import { namespaceHash, withCoalescedInvoke } from "./util";
41
- var cache_default = (renderFn, ctx) => {
42
- const sprCache = createCache();
43
- const doRender = (context) => __async(void 0, null, function* () {
44
- const cacheContext = __spreadValues({
45
- entry: context.entryName
46
- }, context.request);
47
- const cacheFile = yield sprCache.get(cacheContext);
48
- function afterRender(source, onAfterRender) {
49
- return __async(this, null, function* () {
50
- if (!source) {
51
- return "";
52
- }
53
- if (typeof source === "string") {
54
- yield onAfterRender(source);
55
- return source;
56
- } else {
57
- let htmlForStream = "";
58
- const cacheStream = new Transform({
59
- write(chunk, _, callback) {
60
- htmlForStream += chunk.toString();
61
- this.push(chunk);
62
- callback();
63
- }
64
- });
65
- cacheStream.on("close", () => onAfterRender(htmlForStream));
66
- return source(cacheStream);
67
- }
68
- });
69
- }
70
- function saveHtmlIntoCache(html) {
71
- return __async(this, null, function* () {
72
- const { cacheConfig } = context;
73
- if (html && cacheConfig) {
74
- yield sprCache.set(cacheContext, html, cacheConfig);
75
- }
76
- });
77
- }
78
- if (!cacheFile) {
79
- const renderResult = yield renderFn(context);
80
- return afterRender(renderResult, saveHtmlIntoCache);
81
- }
82
- const cacheHash = cacheFile == null ? void 0 : cacheFile.hash;
83
- if (cacheFile.isGarbage) {
84
- const renderResult = yield renderFn(context);
85
- return afterRender(renderResult, saveHtmlIntoCache);
86
- } else if (cacheFile.isStale) {
87
- const render = withCoalescedInvoke(() => renderFn(context)).bind(
88
- null,
89
- namespaceHash("render", cacheFile.hash),
90
- []
91
- );
92
- render().then((res) => __async(void 0, null, function* () {
93
- if (res.value && res.isOrigin) {
94
- const { cacheConfig } = context;
95
- if (cacheConfig) {
96
- afterRender(res.value, (html) => __async(void 0, null, function* () {
97
- sprCache.set(cacheContext, html, cacheConfig);
98
- }));
99
- } else {
100
- sprCache.del(cacheContext, cacheHash);
101
- }
102
- }
103
- })).catch((e) => {
104
- sprCache.del(cacheContext, cacheHash);
105
- ctx.error(ERROR_DIGEST.ERENDER, e);
106
- });
107
- }
108
- ctx.res.setHeader("x-modern-spr", "1");
109
- return cacheFile.content;
110
- });
111
- return doRender;
112
- };
113
- export {
114
- cache_default as default
115
- };
@@ -1,32 +0,0 @@
1
- var __async = (__this, __arguments, generator) => {
2
- return new Promise((resolve, reject) => {
3
- var fulfilled = (value) => {
4
- try {
5
- step(generator.next(value));
6
- } catch (e) {
7
- reject(e);
8
- }
9
- };
10
- var rejected = (value) => {
11
- try {
12
- step(generator.throw(value));
13
- } catch (e) {
14
- reject(e);
15
- }
16
- };
17
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
- step((generator = generator.apply(__this, __arguments)).next());
19
- });
20
- };
21
- import { LRUCaches } from "./lru";
22
- function createPageCaches(max) {
23
- return __async(this, null, function* () {
24
- const constructorOptions = { max };
25
- const cacheInstance = new LRUCaches(constructorOptions);
26
- yield cacheInstance.init();
27
- return cacheInstance;
28
- });
29
- }
30
- export {
31
- createPageCaches
32
- };
@@ -1,29 +0,0 @@
1
- import LRU from "lru-cache";
2
- class LRUCaches {
3
- constructor(options) {
4
- this.max = options.max;
5
- this.caches = new LRU(this.max);
6
- }
7
- init() {
8
- return Promise.resolve();
9
- }
10
- keys() {
11
- return this.caches.keys();
12
- }
13
- get(key) {
14
- return Promise.resolve(this.caches.get(key) || null);
15
- }
16
- peek(key) {
17
- return this.caches.peek(key) || null;
18
- }
19
- set(key, cache) {
20
- this.caches.set(key, cache);
21
- return Promise.resolve();
22
- }
23
- del(key) {
24
- this.caches.del(key);
25
- }
26
- }
27
- export {
28
- LRUCaches
29
- };