@modern-js/prod-server 2.4.0 → 2.5.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 (137) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/{js/node → cjs}/constants.js +0 -0
  3. package/dist/{js/node → cjs}/index.js +0 -0
  4. package/dist/{js/node → cjs}/libs/context/context.js +0 -0
  5. package/dist/{js/node → cjs}/libs/context/index.js +0 -0
  6. package/dist/{js/node → cjs}/libs/hook-api/index.js +12 -25
  7. package/dist/{js/node → cjs}/libs/hook-api/route.js +0 -0
  8. package/dist/{js/node → cjs}/libs/hook-api/template.js +0 -0
  9. package/dist/{js/node → cjs}/libs/loadConfig.js +6 -21
  10. package/dist/cjs/libs/logger.js +122 -0
  11. package/dist/{js/node → cjs}/libs/metrics.js +0 -0
  12. package/dist/{js/node → cjs}/libs/proxy.js +6 -39
  13. package/dist/{js/node → cjs}/libs/render/cache/__tests__/cache.fun.test.js +12 -34
  14. package/dist/{js/node → cjs}/libs/render/cache/__tests__/cache.test.js +43 -78
  15. package/dist/{js/node → cjs}/libs/render/cache/__tests__/cacheable.js +0 -0
  16. package/dist/{js/node → cjs}/libs/render/cache/__tests__/error-configuration.js +0 -0
  17. package/dist/{js/node → cjs}/libs/render/cache/__tests__/matched-cache.js +0 -0
  18. package/dist/cjs/libs/render/cache/index.js +97 -0
  19. package/dist/cjs/libs/render/cache/page-caches/index.js +33 -0
  20. package/dist/{js/node → cjs}/libs/render/cache/page-caches/lru.js +0 -0
  21. package/dist/{js/node → cjs}/libs/render/cache/spr.js +69 -97
  22. package/dist/{js/node → cjs}/libs/render/cache/type.js +0 -0
  23. package/dist/{js/node → cjs}/libs/render/cache/util.js +19 -41
  24. package/dist/cjs/libs/render/index.js +93 -0
  25. package/dist/{js/node → cjs}/libs/render/measure.js +6 -21
  26. package/dist/{js/node → cjs}/libs/render/reader.js +24 -46
  27. package/dist/{js/node → cjs}/libs/render/ssr.js +3 -23
  28. package/dist/{js/node → cjs}/libs/render/static.js +22 -44
  29. package/dist/{js/node → cjs}/libs/render/type.js +0 -0
  30. package/dist/{js/node → cjs}/libs/route/index.js +0 -0
  31. package/dist/{js/node → cjs}/libs/route/matcher.js +0 -0
  32. package/dist/{js/node → cjs}/libs/route/route.js +0 -0
  33. package/dist/{js/node → cjs}/libs/serve-file.js +2 -22
  34. package/dist/{js/node → cjs}/server/index.js +56 -106
  35. package/dist/{js/node → cjs}/server/modern-server-split.js +9 -40
  36. package/dist/cjs/server/modern-server.js +490 -0
  37. package/dist/{js/node → cjs}/type.js +0 -0
  38. package/dist/{js/node → cjs}/utils.js +1 -15
  39. package/dist/{js/node → cjs}/worker-server.js +51 -35
  40. package/dist/{js/treeshaking → esm}/constants.js +0 -0
  41. package/dist/{js/treeshaking → esm}/index.js +0 -0
  42. package/dist/{js/treeshaking → esm}/libs/context/context.js +2 -2
  43. package/dist/{js/treeshaking → esm}/libs/context/index.js +0 -0
  44. package/dist/{js/treeshaking → esm}/libs/hook-api/index.js +1 -1
  45. package/dist/{js/treeshaking → esm}/libs/hook-api/route.js +0 -0
  46. package/dist/{js/treeshaking → esm}/libs/hook-api/template.js +0 -0
  47. package/dist/{js/treeshaking → esm}/libs/loadConfig.js +0 -0
  48. package/dist/esm/libs/logger.js +205 -0
  49. package/dist/{js/treeshaking → esm}/libs/metrics.js +0 -0
  50. package/dist/{js/treeshaking → esm}/libs/proxy.js +0 -0
  51. package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cache.fun.test.js +3 -3
  52. package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cache.test.js +2 -2
  53. package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cacheable.js +0 -0
  54. package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/error-configuration.js +0 -0
  55. package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/matched-cache.js +0 -0
  56. package/dist/{js/treeshaking → esm}/libs/render/cache/index.js +0 -0
  57. package/dist/{js/treeshaking → esm}/libs/render/cache/page-caches/index.js +0 -0
  58. package/dist/{js/treeshaking → esm}/libs/render/cache/page-caches/lru.js +0 -0
  59. package/dist/{js/treeshaking → esm}/libs/render/cache/spr.js +8 -8
  60. package/dist/{js/treeshaking → esm}/libs/render/cache/type.js +0 -0
  61. package/dist/{js/treeshaking → esm}/libs/render/cache/util.js +0 -0
  62. package/dist/{js/treeshaking → esm}/libs/render/index.js +4 -3
  63. package/dist/{js/treeshaking → esm}/libs/render/measure.js +2 -2
  64. package/dist/{js/treeshaking → esm}/libs/render/reader.js +0 -0
  65. package/dist/{js/treeshaking → esm}/libs/render/ssr.js +3 -3
  66. package/dist/{js/treeshaking → esm}/libs/render/static.js +0 -0
  67. package/dist/{js/treeshaking → esm}/libs/render/type.js +0 -0
  68. package/dist/{js/treeshaking → esm}/libs/route/index.js +0 -0
  69. package/dist/{js/treeshaking → esm}/libs/route/matcher.js +2 -2
  70. package/dist/{js/treeshaking → esm}/libs/route/route.js +0 -0
  71. package/dist/{js/treeshaking → esm}/libs/serve-file.js +2 -2
  72. package/dist/{js/treeshaking → esm}/server/index.js +3 -3
  73. package/dist/{js/treeshaking → esm}/server/modern-server-split.js +0 -0
  74. package/dist/{js/treeshaking → esm}/server/modern-server.js +21 -15
  75. package/dist/{js/treeshaking → esm}/type.js +0 -0
  76. package/dist/{js/treeshaking → esm}/utils.js +1 -1
  77. package/dist/{js/treeshaking → esm}/worker-server.js +64 -8
  78. package/dist/{js/modern → esm-node}/constants.js +0 -0
  79. package/dist/{js/modern → esm-node}/index.js +0 -0
  80. package/dist/{js/modern → esm-node}/libs/context/context.js +0 -0
  81. package/dist/{js/modern → esm-node}/libs/context/index.js +0 -0
  82. package/dist/{js/modern → esm-node}/libs/hook-api/index.js +12 -27
  83. package/dist/{js/modern → esm-node}/libs/hook-api/route.js +0 -0
  84. package/dist/{js/modern → esm-node}/libs/hook-api/template.js +0 -0
  85. package/dist/esm-node/libs/loadConfig.js +45 -0
  86. package/dist/esm-node/libs/logger.js +98 -0
  87. package/dist/{js/modern → esm-node}/libs/metrics.js +0 -0
  88. package/dist/{js/modern → esm-node}/libs/proxy.js +6 -41
  89. package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +83 -0
  90. package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +210 -0
  91. package/dist/{js/modern → esm-node}/libs/render/cache/__tests__/cacheable.js +0 -0
  92. package/dist/{js/modern → esm-node}/libs/render/cache/__tests__/error-configuration.js +0 -0
  93. package/dist/{js/modern → esm-node}/libs/render/cache/__tests__/matched-cache.js +0 -0
  94. package/dist/esm-node/libs/render/cache/index.js +76 -0
  95. package/dist/esm-node/libs/render/cache/page-caches/index.js +10 -0
  96. package/dist/{js/modern → esm-node}/libs/render/cache/page-caches/lru.js +0 -0
  97. package/dist/{js/modern → esm-node}/libs/render/cache/spr.js +69 -97
  98. package/dist/{js/modern → esm-node}/libs/render/cache/type.js +0 -0
  99. package/dist/{js/modern → esm-node}/libs/render/cache/util.js +19 -41
  100. package/dist/esm-node/libs/render/index.js +64 -0
  101. package/dist/{js/modern → esm-node}/libs/render/measure.js +6 -23
  102. package/dist/esm-node/libs/render/reader.js +85 -0
  103. package/dist/{js/modern → esm-node}/libs/render/ssr.js +3 -23
  104. package/dist/esm-node/libs/render/static.js +38 -0
  105. package/dist/{js/modern → esm-node}/libs/render/type.js +0 -0
  106. package/dist/{js/modern → esm-node}/libs/route/index.js +0 -0
  107. package/dist/{js/modern → esm-node}/libs/route/matcher.js +0 -0
  108. package/dist/{js/modern → esm-node}/libs/route/route.js +0 -0
  109. package/dist/{js/modern → esm-node}/libs/serve-file.js +2 -22
  110. package/dist/esm-node/server/index.js +156 -0
  111. package/dist/esm-node/server/modern-server-split.js +43 -0
  112. package/dist/esm-node/server/modern-server.js +483 -0
  113. package/dist/{js/modern → esm-node}/type.js +0 -0
  114. package/dist/{js/modern → esm-node}/utils.js +1 -17
  115. package/dist/esm-node/worker-server.js +69 -0
  116. package/dist/types/libs/context/context.d.ts +1 -1
  117. package/dist/types/libs/logger.d.ts +63 -0
  118. package/dist/types/libs/render/index.d.ts +3 -1
  119. package/dist/types/utils.d.ts +1 -1
  120. package/dist/types/worker-server.d.ts +2 -1
  121. package/package.json +15 -16
  122. package/dist/js/modern/libs/loadConfig.js +0 -62
  123. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -114
  124. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -254
  125. package/dist/js/modern/libs/render/cache/index.js +0 -115
  126. package/dist/js/modern/libs/render/cache/page-caches/index.js +0 -32
  127. package/dist/js/modern/libs/render/index.js +0 -84
  128. package/dist/js/modern/libs/render/reader.js +0 -107
  129. package/dist/js/modern/libs/render/static.js +0 -60
  130. package/dist/js/modern/server/index.js +0 -208
  131. package/dist/js/modern/server/modern-server-split.js +0 -74
  132. package/dist/js/modern/server/modern-server.js +0 -548
  133. package/dist/js/modern/worker-server.js +0 -54
  134. package/dist/js/node/libs/render/cache/index.js +0 -134
  135. package/dist/js/node/libs/render/cache/page-caches/index.js +0 -55
  136. package/dist/js/node/libs/render/index.js +0 -113
  137. package/dist/js/node/server/modern-server.js +0 -553
@@ -1,55 +1,20 @@
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
1
  var import_spr = require("../spr");
38
2
  var import_error_configuration = require("./error-configuration");
39
3
  var import_cacheable = require("./cacheable");
40
4
  var import_matched_cache = require("./matched-cache");
41
- const createCacheConfig = (config = {}) => __spreadValues({
5
+ const createCacheConfig = (config = {}) => ({
42
6
  excludes: null,
43
7
  includes: null,
44
8
  interval: 10,
45
9
  staleLimit: false,
46
10
  level: 0,
47
11
  fallback: false,
48
- matches: null
49
- }, config);
12
+ matches: null,
13
+ ...config
14
+ });
50
15
  jest.setTimeout(6e4);
51
16
  describe("cache", () => {
52
- it("should cache correctly", () => __async(exports, null, function* () {
17
+ it("should cache correctly", async () => {
53
18
  (0, import_spr.destroyCache)();
54
19
  const cache = (0, import_spr.createCache)();
55
20
  const context = {
@@ -60,12 +25,12 @@ describe("cache", () => {
60
25
  };
61
26
  const content = "hello";
62
27
  const cacheConfig = createCacheConfig();
63
- yield cache.set(context, content, cacheConfig, true);
64
- const cacheResult = yield cache.get(context);
28
+ await cache.set(context, content, cacheConfig, true);
29
+ const cacheResult = await cache.get(context);
65
30
  expect(cacheResult).not.toBe(null);
66
31
  expect(cacheResult == null ? void 0 : cacheResult.content).toBe("hello");
67
- }));
68
- it("should ignore cache set when cache config not exist", () => __async(exports, null, function* () {
32
+ });
33
+ it("should ignore cache set when cache config not exist", async () => {
69
34
  (0, import_spr.destroyCache)();
70
35
  (0, import_spr.destroyCache)();
71
36
  const cache = (0, import_spr.createCache)();
@@ -76,10 +41,10 @@ describe("cache", () => {
76
41
  headers: {}
77
42
  };
78
43
  const content = "hello";
79
- const shouldCache = yield cache.set(context, content, null, true);
44
+ const shouldCache = await cache.set(context, content, null, true);
80
45
  expect(shouldCache).toBe(false);
81
- }));
82
- it("should calcual cache key error", () => __async(exports, null, function* () {
46
+ });
47
+ it("should calcual cache key error", async () => {
83
48
  (0, import_spr.destroyCache)();
84
49
  const cache = (0, import_spr.createCache)();
85
50
  const content = "hello";
@@ -92,11 +57,11 @@ describe("cache", () => {
92
57
  query: {},
93
58
  headers: {}
94
59
  };
95
- const shouldCache = yield cache.set(context, content, cacheConfig);
60
+ const shouldCache = await cache.set(context, content, cacheConfig);
96
61
  expect(shouldCache).toBe(false);
97
62
  }
98
- }));
99
- it("should get nothing for diff requestKey", () => __async(exports, null, function* () {
63
+ });
64
+ it("should get nothing for diff requestKey", async () => {
100
65
  (0, import_spr.destroyCache)();
101
66
  const cache = (0, import_spr.createCache)();
102
67
  const context = {
@@ -110,17 +75,17 @@ describe("cache", () => {
110
75
  level: 1,
111
76
  includes: { query: ["name"] }
112
77
  });
113
- yield cache.set(context, content, cacheConfig, true);
78
+ await cache.set(context, content, cacheConfig, true);
114
79
  const context_req = {
115
80
  entry: "",
116
81
  pathname: "/home",
117
82
  query: {},
118
83
  headers: {}
119
84
  };
120
- const cacheResult = yield cache.get(context_req);
85
+ const cacheResult = await cache.get(context_req);
121
86
  expect(cacheResult).toBe(null);
122
- }));
123
- it("should get nothing for diff cacheHash", () => __async(exports, null, function* () {
87
+ });
88
+ it("should get nothing for diff cacheHash", async () => {
124
89
  (0, import_spr.destroyCache)();
125
90
  const cache = (0, import_spr.createCache)();
126
91
  const context = {
@@ -134,17 +99,17 @@ describe("cache", () => {
134
99
  level: 1,
135
100
  includes: { query: ["name"] }
136
101
  });
137
- yield cache.set(context, content, cacheConfig, true);
102
+ await cache.set(context, content, cacheConfig, true);
138
103
  const context_req = {
139
104
  entry: "",
140
105
  pathname: "",
141
106
  query: { name: "zll" },
142
107
  headers: {}
143
108
  };
144
- const cacheResult = yield cache.get(context_req);
109
+ const cacheResult = await cache.get(context_req);
145
110
  expect(cacheResult).toBe(null);
146
- }));
147
- it("should get cache correctly", () => __async(exports, null, function* () {
111
+ });
112
+ it("should get cache correctly", async () => {
148
113
  (0, import_spr.destroyCache)();
149
114
  const cache = (0, import_spr.createCache)();
150
115
  for (const cacheable of import_cacheable.cacheabelAry) {
@@ -155,12 +120,12 @@ describe("cache", () => {
155
120
  headers: cacheable.requestOpt.headers || {}
156
121
  };
157
122
  const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
158
- yield cache.set(context, cacheable.content, cacheConfig, true);
159
- const cacheResult = yield cache.get(context);
123
+ await cache.set(context, cacheable.content, cacheConfig, true);
124
+ const cacheResult = await cache.get(context);
160
125
  expect(cacheResult == null ? void 0 : cacheResult.content).toBe(cacheable.content);
161
126
  }
162
- }));
163
- it("should match cache correctly", () => __async(exports, null, function* () {
127
+ });
128
+ it("should match cache correctly", async () => {
164
129
  (0, import_spr.destroyCache)();
165
130
  const cache = (0, import_spr.createCache)();
166
131
  for (const cacheable of import_matched_cache.matchedCacheableAry) {
@@ -172,14 +137,14 @@ describe("cache", () => {
172
137
  query: requestOpt.query,
173
138
  headers: requestOpt.headers
174
139
  };
175
- yield cache.set(context, content, createCacheConfig(cacheConfig), true);
140
+ await cache.set(context, content, createCacheConfig(cacheConfig), true);
176
141
  const matchContext = {
177
142
  entry: "",
178
143
  pathname: matchOne.url,
179
144
  query: matchOne.query,
180
145
  headers: matchOne.headers
181
146
  };
182
- const cacheResult = yield cache.get(matchContext);
147
+ const cacheResult = await cache.get(matchContext);
183
148
  expect(cacheResult == null ? void 0 : cacheResult.content).toBe(content);
184
149
  for (const notMatch of other) {
185
150
  const notMatchContext = {
@@ -188,12 +153,12 @@ describe("cache", () => {
188
153
  query: notMatch.query,
189
154
  headers: notMatch.headers
190
155
  };
191
- const nothing = yield cache.get(notMatchContext);
156
+ const nothing = await cache.get(notMatchContext);
192
157
  expect(nothing).toBe(null);
193
158
  }
194
159
  }
195
- }));
196
- it("should stale cache correctly", () => __async(exports, null, function* () {
160
+ });
161
+ it("should stale cache correctly", async () => {
197
162
  (0, import_spr.destroyCache)();
198
163
  const cache = (0, import_spr.createCache)();
199
164
  const context = {
@@ -204,19 +169,19 @@ describe("cache", () => {
204
169
  };
205
170
  const config = createCacheConfig({ interval: 5 });
206
171
  const content = "hello";
207
- const shouldCache = yield cache.set(context, content, config, true);
172
+ const shouldCache = await cache.set(context, content, config, true);
208
173
  expect(shouldCache.value).toBe(true);
209
- const freshResult = yield cache.get(context);
174
+ const freshResult = await cache.get(context);
210
175
  expect(freshResult == null ? void 0 : freshResult.isStale).toBe(false);
211
- yield new Promise((resolve) => {
176
+ await new Promise((resolve) => {
212
177
  setTimeout(() => {
213
178
  resolve();
214
179
  }, 6e3);
215
180
  });
216
- const staleResult = yield cache.get(context);
181
+ const staleResult = await cache.get(context);
217
182
  expect(staleResult == null ? void 0 : staleResult.isStale).toBe(true);
218
- }));
219
- it("should garbage cache correctly", () => __async(exports, null, function* () {
183
+ });
184
+ it("should garbage cache correctly", async () => {
220
185
  (0, import_spr.destroyCache)();
221
186
  const cache = (0, import_spr.createCache)();
222
187
  const context = {
@@ -230,16 +195,16 @@ describe("cache", () => {
230
195
  staleLimit: 8
231
196
  });
232
197
  const content = "hello";
233
- const shouldCache = yield cache.set(context, content, config, true);
198
+ const shouldCache = await cache.set(context, content, config, true);
234
199
  expect(shouldCache.value).toBe(true);
235
- const freshResult = yield cache.get(context);
200
+ const freshResult = await cache.get(context);
236
201
  expect(freshResult == null ? void 0 : freshResult.isGarbage).toBe(false);
237
- yield new Promise((resolve) => {
202
+ await new Promise((resolve) => {
238
203
  setTimeout(() => {
239
204
  resolve();
240
205
  }, 1e4);
241
206
  });
242
- const staleResult = yield cache.get(context);
207
+ const staleResult = await cache.get(context);
243
208
  expect(staleResult == null ? void 0 : staleResult.isGarbage).toBe(true);
244
- }));
209
+ });
245
210
  });
@@ -0,0 +1,97 @@
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 cache_exports = {};
19
+ __export(cache_exports, {
20
+ default: () => cache_default
21
+ });
22
+ module.exports = __toCommonJS(cache_exports);
23
+ var import_stream = require("stream");
24
+ var import_constants = require("../../../constants");
25
+ var import_spr = require("./spr");
26
+ var import_util = require("./util");
27
+ var cache_default = (renderFn, ctx) => {
28
+ const sprCache = (0, import_spr.createCache)();
29
+ const doRender = async (context) => {
30
+ const cacheContext = {
31
+ entry: context.entryName,
32
+ ...context.request
33
+ };
34
+ const cacheFile = await sprCache.get(cacheContext);
35
+ async function afterRender(source, onAfterRender) {
36
+ if (!source) {
37
+ return "";
38
+ }
39
+ if (typeof source === "string") {
40
+ await onAfterRender(source);
41
+ return source;
42
+ } else {
43
+ let htmlForStream = "";
44
+ const cacheStream = new import_stream.Transform({
45
+ write(chunk, _, callback) {
46
+ htmlForStream += chunk.toString();
47
+ this.push(chunk);
48
+ callback();
49
+ }
50
+ });
51
+ cacheStream.on("close", () => onAfterRender(htmlForStream));
52
+ return source(cacheStream);
53
+ }
54
+ }
55
+ async function saveHtmlIntoCache(html) {
56
+ const { cacheConfig } = context;
57
+ if (html && cacheConfig) {
58
+ await sprCache.set(cacheContext, html, cacheConfig);
59
+ }
60
+ }
61
+ if (!cacheFile) {
62
+ const renderResult = await renderFn(context);
63
+ return afterRender(renderResult, saveHtmlIntoCache);
64
+ }
65
+ const cacheHash = cacheFile == null ? void 0 : cacheFile.hash;
66
+ if (cacheFile.isGarbage) {
67
+ const renderResult = await renderFn(context);
68
+ return afterRender(renderResult, saveHtmlIntoCache);
69
+ } else if (cacheFile.isStale) {
70
+ const render = (0, import_util.withCoalescedInvoke)(() => renderFn(context)).bind(
71
+ null,
72
+ (0, import_util.namespaceHash)("render", cacheFile.hash),
73
+ []
74
+ );
75
+ render().then(async (res) => {
76
+ if (res.value && res.isOrigin) {
77
+ const { cacheConfig } = context;
78
+ if (cacheConfig) {
79
+ afterRender(res.value, async (html) => {
80
+ sprCache.set(cacheContext, html, cacheConfig);
81
+ });
82
+ } else {
83
+ sprCache.del(cacheContext, cacheHash);
84
+ }
85
+ }
86
+ }).catch((e) => {
87
+ sprCache.del(cacheContext, cacheHash);
88
+ ctx.error(import_constants.ERROR_DIGEST.ERENDER, e);
89
+ });
90
+ }
91
+ ctx.res.setHeader("x-modern-spr", "1");
92
+ return cacheFile.content;
93
+ };
94
+ return doRender;
95
+ };
96
+ // Annotate the CommonJS export names for ESM import in node:
97
+ 0 && (module.exports = {});
@@ -0,0 +1,33 @@
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 page_caches_exports = {};
19
+ __export(page_caches_exports, {
20
+ createPageCaches: () => createPageCaches
21
+ });
22
+ module.exports = __toCommonJS(page_caches_exports);
23
+ var import_lru = require("./lru");
24
+ async function createPageCaches(max) {
25
+ const constructorOptions = { max };
26
+ const cacheInstance = new import_lru.LRUCaches(constructorOptions);
27
+ await cacheInstance.init();
28
+ return cacheInstance;
29
+ }
30
+ // Annotate the CommonJS export names for ESM import in node:
31
+ 0 && (module.exports = {
32
+ createPageCaches
33
+ });
@@ -21,26 +21,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  mod
22
22
  ));
23
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
24
  var spr_exports = {};
45
25
  __export(spr_exports, {
46
26
  createCache: () => createCache,
@@ -159,15 +139,13 @@ class CacheManager {
159
139
  const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
160
140
  return headerFactor;
161
141
  }
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
- });
142
+ async best(context, cacheKey, data) {
143
+ const { level } = data;
144
+ const cacheHash = this.find[(0, import_util.fname)(level)](context, cacheKey, data);
145
+ if (!cacheHash) {
146
+ return null;
147
+ }
148
+ return data.caches.get(cacheHash);
171
149
  }
172
150
  createCacheContent(config, caches) {
173
151
  return {
@@ -179,77 +157,71 @@ class CacheManager {
179
157
  caches
180
158
  };
181
159
  }
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
- });
160
+ async get(context) {
161
+ const cacheKey = this.generateRequestKey(context);
162
+ const data = this.cache.get(cacheKey);
163
+ if (!data) {
164
+ return null;
165
+ }
166
+ const dest = await this.best(context, cacheKey, data);
167
+ if (!dest) {
168
+ return null;
169
+ }
170
+ const { expireTime, limitTime, html, cacheHash } = dest;
171
+ const isStale = Date.now() - expireTime > 0;
172
+ const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
173
+ return {
174
+ content: html || "",
175
+ contentType: import_utils.mime.contentType("html"),
176
+ isStale,
177
+ isGarbage,
178
+ hash: cacheHash
179
+ };
204
180
  }
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;
181
+ async set(context, html, cacheConfig, sync = false) {
182
+ if (!cacheConfig) {
183
+ return false;
184
+ }
185
+ const cacheKey = this.generateRequestKey(context);
186
+ let data = this.cache.get(cacheKey);
187
+ if (!data) {
188
+ const caches = await (0, import_page_caches.createPageCaches)(MAX_CACHE_EACH_REQ);
189
+ data = this.createCacheContent(cacheConfig, caches);
190
+ }
191
+ const cacheHash = this.find[(0, import_util.fname)(cacheConfig.level)](
192
+ context,
193
+ cacheKey,
194
+ data
195
+ );
196
+ if (!cacheHash) {
197
+ return false;
198
+ }
199
+ const cacheSyncOrAsync = async () => {
200
+ const next = data;
201
+ const limit = cacheConfig.staleLimit;
202
+ const storeHTML = (0, import_util.cacheAddition)(html, cacheHash);
203
+ const size = storeHTML.length;
204
+ await next.caches.set(cacheHash, {
205
+ expireTime: Date.now() + cacheConfig.interval * 1e3,
206
+ limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
207
+ cacheHash,
208
+ html: storeHTML,
209
+ size
238
210
  });
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
- });
211
+ this.cache.set(cacheKey, next);
212
+ return true;
213
+ };
214
+ const doCache = (0, import_util.withCoalescedInvoke)(cacheSyncOrAsync).bind(
215
+ null,
216
+ (0, import_util.namespaceHash)("stream", cacheHash),
217
+ []
218
+ );
219
+ return (0, import_util.maybeSync)(doCache)(sync);
246
220
  }
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
- });
221
+ async del(context, cacheHash) {
222
+ const cacheKey = this.generateRequestKey(context);
223
+ const data = this.cache.get(cacheKey);
224
+ data == null ? void 0 : data.caches.del(cacheHash);
253
225
  }
254
226
  }
255
227
  let manager;
File without changes
@@ -21,26 +21,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  mod
22
22
  ));
23
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
24
  var util_exports = {};
45
25
  __export(util_exports, {
46
26
  cacheAddition: () => cacheAddition,
@@ -91,28 +71,26 @@ function cacheAddition(html, hash) {
91
71
  }
92
72
  const globalInvokeCache = /* @__PURE__ */ new Map();
93
73
  function withCoalescedInvoke(func) {
94
- return function(key, args) {
95
- return __async(this, null, function* () {
96
- const entry = globalInvokeCache.get(key);
97
- if (entry) {
98
- return entry.then((res) => ({
99
- isOrigin: false,
100
- value: res.value
101
- }));
102
- }
103
- function __wrapper() {
104
- return func(...args);
105
- }
106
- const future = __wrapper().then((res) => {
107
- globalInvokeCache.delete(key);
108
- return { isOrigin: true, value: res };
109
- }).catch((err) => {
110
- globalInvokeCache.delete(key);
111
- throw err;
112
- });
113
- globalInvokeCache.set(key, future);
114
- return future;
74
+ return async function(key, args) {
75
+ const entry = globalInvokeCache.get(key);
76
+ if (entry) {
77
+ return entry.then((res) => ({
78
+ isOrigin: false,
79
+ value: res.value
80
+ }));
81
+ }
82
+ function __wrapper() {
83
+ return func(...args);
84
+ }
85
+ const future = __wrapper().then((res) => {
86
+ globalInvokeCache.delete(key);
87
+ return { isOrigin: true, value: res };
88
+ }).catch((err) => {
89
+ globalInvokeCache.delete(key);
90
+ throw err;
115
91
  });
92
+ globalInvokeCache.set(key, future);
93
+ return future;
116
94
  };
117
95
  }
118
96
  function maybeSync(fn) {