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

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 (130) hide show
  1. package/CHANGELOG.md +132 -0
  2. package/dist/js/modern/constants.js +32 -25
  3. package/dist/js/modern/index.js +11 -6
  4. package/dist/js/modern/libs/context/context.js +52 -84
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +42 -35
  7. package/dist/js/modern/libs/hook-api/route.js +6 -7
  8. package/dist/js/modern/libs/hook-api/template.js +20 -34
  9. package/dist/js/modern/libs/loadConfig.js +45 -24
  10. package/dist/js/modern/libs/metrics.js +3 -4
  11. package/dist/js/modern/libs/proxy.js +68 -37
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -67
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -216
  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 +88 -54
  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 -6
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -117
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -39
  23. package/dist/js/modern/libs/render/index.js +76 -56
  24. package/dist/js/modern/libs/render/measure.js +38 -27
  25. package/dist/js/modern/libs/render/reader.js +65 -62
  26. package/dist/js/modern/libs/render/ssr.js +50 -32
  27. package/dist/js/modern/libs/render/static.js +50 -33
  28. package/dist/js/modern/libs/render/type.js +9 -6
  29. package/dist/js/modern/libs/route/index.js +8 -15
  30. package/dist/js/modern/libs/route/matcher.js +20 -34
  31. package/dist/js/modern/libs/route/route.js +9 -18
  32. package/dist/js/modern/libs/serve-file.js +33 -20
  33. package/dist/js/modern/server/index.js +144 -145
  34. package/dist/js/modern/server/modern-server-split.js +46 -12
  35. package/dist/js/modern/server/modern-server.js +377 -419
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -43
  38. package/dist/js/modern/worker-server.js +34 -14
  39. package/dist/js/node/constants.js +45 -30
  40. package/dist/js/node/index.js +31 -57
  41. package/dist/js/node/libs/context/context.js +79 -94
  42. package/dist/js/node/libs/context/index.js +23 -13
  43. package/dist/js/node/libs/hook-api/index.js +69 -48
  44. package/dist/js/node/libs/hook-api/route.js +22 -11
  45. package/dist/js/node/libs/hook-api/template.js +36 -39
  46. package/dist/js/node/libs/loadConfig.js +69 -35
  47. package/dist/js/node/libs/metrics.js +21 -9
  48. package/dist/js/node/libs/proxy.js +86 -44
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +132 -70
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +268 -218
  51. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +61 -55
  52. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +54 -40
  53. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +101 -119
  54. package/dist/js/node/libs/render/cache/index.js +110 -64
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +50 -14
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +29 -12
  57. package/dist/js/node/libs/render/cache/spr.js +156 -129
  58. package/dist/js/node/libs/render/cache/type.js +0 -5
  59. package/dist/js/node/libs/render/cache/util.js +88 -45
  60. package/dist/js/node/libs/render/index.js +102 -67
  61. package/dist/js/node/libs/render/measure.js +55 -31
  62. package/dist/js/node/libs/render/reader.js +87 -70
  63. package/dist/js/node/libs/render/ssr.js +76 -47
  64. package/dist/js/node/libs/render/static.js +75 -40
  65. package/dist/js/node/libs/render/type.js +27 -12
  66. package/dist/js/node/libs/route/index.js +26 -26
  67. package/dist/js/node/libs/route/matcher.js +36 -41
  68. package/dist/js/node/libs/route/route.js +25 -22
  69. package/dist/js/node/libs/serve-file.js +61 -32
  70. package/dist/js/node/server/index.js +160 -160
  71. package/dist/js/node/server/modern-server-split.js +68 -22
  72. package/dist/js/node/server/modern-server.js +395 -443
  73. package/dist/js/node/type.js +0 -3
  74. package/dist/js/node/utils.js +74 -52
  75. package/dist/js/node/worker-server.js +53 -21
  76. package/dist/js/treeshaking/constants.js +26 -25
  77. package/dist/js/treeshaking/index.js +10 -10
  78. package/dist/js/treeshaking/libs/context/context.js +268 -237
  79. package/dist/js/treeshaking/libs/context/index.js +3 -3
  80. package/dist/js/treeshaking/libs/hook-api/index.js +265 -143
  81. package/dist/js/treeshaking/libs/hook-api/route.js +65 -30
  82. package/dist/js/treeshaking/libs/hook-api/template.js +121 -85
  83. package/dist/js/treeshaking/libs/loadConfig.js +80 -37
  84. package/dist/js/treeshaking/libs/metrics.js +4 -10
  85. package/dist/js/treeshaking/libs/proxy.js +240 -76
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +288 -121
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +772 -455
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +65 -51
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -35
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +144 -118
  91. package/dist/js/treeshaking/libs/render/cache/index.js +337 -175
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +151 -27
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +80 -42
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +470 -340
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -1
  96. package/dist/js/treeshaking/libs/render/cache/util.js +271 -92
  97. package/dist/js/treeshaking/libs/render/index.js +228 -95
  98. package/dist/js/treeshaking/libs/render/measure.js +142 -57
  99. package/dist/js/treeshaking/libs/render/reader.js +325 -177
  100. package/dist/js/treeshaking/libs/render/ssr.js +220 -95
  101. package/dist/js/treeshaking/libs/render/static.js +210 -78
  102. package/dist/js/treeshaking/libs/render/type.js +7 -6
  103. package/dist/js/treeshaking/libs/route/index.js +125 -89
  104. package/dist/js/treeshaking/libs/route/matcher.js +132 -107
  105. package/dist/js/treeshaking/libs/route/route.js +40 -26
  106. package/dist/js/treeshaking/libs/serve-file.js +177 -68
  107. package/dist/js/treeshaking/server/index.js +468 -327
  108. package/dist/js/treeshaking/server/modern-server-split.js +352 -144
  109. package/dist/js/treeshaking/server/modern-server.js +1046 -911
  110. package/dist/js/treeshaking/type.js +1 -1
  111. package/dist/js/treeshaking/utils.js +138 -81
  112. package/dist/js/treeshaking/worker-server.js +176 -55
  113. package/dist/types/index.d.ts +2 -0
  114. package/dist/types/libs/context/context.d.ts +4 -1
  115. package/dist/types/libs/loadConfig.d.ts +1 -0
  116. package/dist/types/libs/render/cache/index.d.ts +2 -0
  117. package/dist/types/libs/render/cache/spr.d.ts +2 -0
  118. package/dist/types/libs/route/route.d.ts +0 -1
  119. package/dist/types/server/index.d.ts +3 -0
  120. package/dist/types/utils.d.ts +1 -1
  121. package/dist/types/worker-server.d.ts +1 -2
  122. package/package.json +7 -14
  123. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  124. package/dist/js/modern/libs/render/modern/index.js +0 -37
  125. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  126. package/dist/js/node/libs/render/modern/index.js +0 -46
  127. package/dist/js/treeshaking/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/treeshaking/libs/render/modern/index.js +0 -39
  129. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  130. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,10 +1,35 @@
1
- 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; }
2
- /* eslint-disable @typescript-eslint/member-ordering */
3
- /* eslint-disable no-lone-blocks */
4
- import crypto from 'crypto';
5
- import LRUCache from 'lru-cache';
6
- import { mime } from '@modern-js/utils';
7
- import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "./util";
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 crypto from "crypto";
22
+ import LRUCache from "lru-cache";
23
+ import { mime } from "@modern-js/utils";
24
+ import {
25
+ cacheAddition,
26
+ connectFactor,
27
+ fname,
28
+ maybeSync,
29
+ namespaceHash,
30
+ valueFactory,
31
+ withCoalescedInvoke
32
+ } from "./util";
8
33
  import { createPageCaches } from "./page-caches";
9
34
  const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
10
35
  const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
@@ -14,16 +39,11 @@ const HEADER_LEVEL = 2;
14
39
  const QUERY_HEADER_LEVEL = 3;
15
40
  class CacheManager {
16
41
  constructor(cacheOptions) {
17
- _defineProperty(this, "cache", void 0);
18
- _defineProperty(this, "cacheOptions", void 0);
19
- _defineProperty(this, "find", (() => {
42
+ this.find = (() => {
20
43
  {
21
- // eslint-disable-next-line consistent-this,@typescript-eslint/no-this-alias
22
44
  const _this = this;
23
45
  return {
24
- [fname(BASE_LEVEL)](context, cacheKey
25
- // data: CacheContent,
26
- ) {
46
+ [fname(BASE_LEVEL)](context, cacheKey) {
27
47
  return _this.md5(cacheKey);
28
48
  },
29
49
  [fname(QUERY_LEVEL)](context, cacheKey, data) {
@@ -50,34 +70,30 @@ class CacheManager {
50
70
  }
51
71
  };
52
72
  }
53
- })());
73
+ })();
54
74
  this.cacheOptions = cacheOptions;
55
75
  this.cache = new LRUCache({
56
76
  max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
57
- // 默认存 100M,最大 600M
58
77
  length(n) {
59
78
  const len = n.caches.keys().reduce((total, cur) => {
60
- var _n$caches$peek;
61
- return total + (((_n$caches$peek = n.caches.peek(cur)) === null || _n$caches$peek === void 0 ? void 0 : _n$caches$peek.size) || 0);
79
+ var _a;
80
+ return total + (((_a = n.caches.peek(cur)) == null ? void 0 : _a.size) || 0);
62
81
  }, 1);
63
82
  return len;
64
83
  }
65
84
  });
66
85
  }
67
86
  md5(content) {
68
- const md5 = crypto.createHash('md5');
69
- return md5.update(content).digest('hex');
87
+ const md5 = crypto.createHash("md5");
88
+ return md5.update(content).digest("hex");
70
89
  }
71
90
  generateRequestKey(context) {
72
- const {
73
- pathname,
74
- entry
75
- } = context;
91
+ const { pathname, entry } = context;
76
92
  return this.md5(`${pathname}_${entry}`);
77
93
  }
78
94
  replaceValue(value, matcher) {
79
95
  let final = value;
80
- Object.keys(matcher).some(replacer => {
96
+ Object.keys(matcher).some((replacer) => {
81
97
  const reg = new RegExp(matcher[replacer]);
82
98
  if (reg.test(value)) {
83
99
  final = replacer;
@@ -91,19 +107,19 @@ class CacheManager {
91
107
  keys.sort();
92
108
  const getValue = valueFactory(obj);
93
109
  const factorAry = keys.reduce((ary, key) => {
94
- let value = getValue(key) || '';
110
+ let value = getValue(key) || "";
95
111
  const matcher = matches[key];
96
112
  if (matcher) {
97
113
  value = this.replaceValue(value, matcher);
98
114
  }
99
115
  return ary.concat([key, value]);
100
116
  }, []);
101
- return factorAry.join(',');
117
+ return factorAry.join(",");
102
118
  }
103
119
  queryFactor(context, data) {
104
- var _data$includes, _data$matches;
105
- const queryKeys = (_data$includes = data.includes) === null || _data$includes === void 0 ? void 0 : _data$includes.query;
106
- const queryMatches = (_data$matches = data.matches) === null || _data$matches === void 0 ? void 0 : _data$matches.query;
120
+ var _a, _b;
121
+ const queryKeys = (_a = data.includes) == null ? void 0 : _a.query;
122
+ const queryMatches = (_b = data.matches) == null ? void 0 : _b.query;
107
123
  if (!queryKeys || queryKeys.length === 0) {
108
124
  return null;
109
125
  }
@@ -112,9 +128,9 @@ class CacheManager {
112
128
  return queryFactor;
113
129
  }
114
130
  headerFactor(context, data) {
115
- var _data$includes2, _data$matches2;
116
- const headerKeys = (_data$includes2 = data.includes) === null || _data$includes2 === void 0 ? void 0 : _data$includes2.header;
117
- const headerMatches = (_data$matches2 = data.matches) === null || _data$matches2 === void 0 ? void 0 : _data$matches2.header;
131
+ var _a, _b;
132
+ const headerKeys = (_a = data.includes) == null ? void 0 : _a.header;
133
+ const headerMatches = (_b = data.matches) == null ? void 0 : _b.header;
118
134
  if (!headerKeys || headerKeys.length === 0) {
119
135
  return null;
120
136
  }
@@ -122,15 +138,15 @@ class CacheManager {
122
138
  const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
123
139
  return headerFactor;
124
140
  }
125
- async best(context, cacheKey, data) {
126
- const {
127
- level
128
- } = data;
129
- const cacheHash = this.find[fname(level)](context, cacheKey, data);
130
- if (!cacheHash) {
131
- return null;
132
- }
133
- return data.caches.get(cacheHash);
141
+ best(context, cacheKey, data) {
142
+ return __async(this, null, function* () {
143
+ const { level } = data;
144
+ const cacheHash = this.find[fname(level)](context, cacheKey, data);
145
+ if (!cacheHash) {
146
+ return null;
147
+ }
148
+ return data.caches.get(cacheHash);
149
+ });
134
150
  }
135
151
  createCacheContent(config, caches) {
136
152
  return {
@@ -142,91 +158,91 @@ class CacheManager {
142
158
  caches
143
159
  };
144
160
  }
145
- async get(context) {
146
- const cacheKey = this.generateRequestKey(context);
147
- const data = this.cache.get(cacheKey);
148
-
149
- // no cache key matched
150
- if (!data) {
151
- return null;
152
- }
153
- const dest = await this.best(context, cacheKey, data);
154
- // no cache for current page with current config
155
- if (!dest) {
156
- return null;
157
- }
158
- const {
159
- expireTime,
160
- limitTime,
161
- html,
162
- cacheHash
163
- } = dest;
164
- const isStale = Date.now() - expireTime > 0;
165
- const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
166
- return {
167
- content: html || '',
168
- contentType: mime.contentType('html'),
169
- isStale,
170
- isGarbage,
171
- hash: cacheHash
172
- };
161
+ get(context) {
162
+ return __async(this, null, function* () {
163
+ const cacheKey = this.generateRequestKey(context);
164
+ const data = this.cache.get(cacheKey);
165
+ if (!data) {
166
+ return null;
167
+ }
168
+ const dest = yield this.best(context, cacheKey, data);
169
+ if (!dest) {
170
+ return null;
171
+ }
172
+ const { expireTime, limitTime, html, cacheHash } = dest;
173
+ const isStale = Date.now() - expireTime > 0;
174
+ const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
175
+ return {
176
+ content: html || "",
177
+ contentType: mime.contentType("html"),
178
+ isStale,
179
+ isGarbage,
180
+ hash: cacheHash
181
+ };
182
+ });
173
183
  }
174
- async set(context, html, cacheConfig, sync = false) {
175
- if (!cacheConfig) {
176
- return false;
177
- }
178
-
179
- // each version with route is a separate cache
180
- const cacheKey = this.generateRequestKey(context);
181
- let data = this.cache.get(cacheKey);
182
- if (!data) {
183
- const caches = await createPageCaches(MAX_CACHE_EACH_REQ);
184
- data = this.createCacheContent(cacheConfig, caches);
185
- }
186
- const cacheHash = this.find[fname(cacheConfig.level)](context, cacheKey, data);
187
-
188
- // if cacheHash is null, maybe level not match meta key, do not cache
189
- if (!cacheHash) {
190
- return false;
191
- }
192
- const cacheSyncOrAsync = async () => {
193
- const next = data;
194
- const limit = cacheConfig.staleLimit;
195
- const storeHTML = cacheAddition(html, cacheHash);
196
- const size = storeHTML.length;
197
- await next.caches.set(cacheHash, {
198
- expireTime: Date.now() + cacheConfig.interval * 1000,
199
- limitTime: typeof limit === 'number' ? Date.now() + limit * 1000 : null,
200
- cacheHash,
201
- html: storeHTML,
202
- size
184
+ set(context, html, cacheConfig, sync = false) {
185
+ return __async(this, null, function* () {
186
+ if (!cacheConfig) {
187
+ return false;
188
+ }
189
+ const cacheKey = this.generateRequestKey(context);
190
+ let data = this.cache.get(cacheKey);
191
+ if (!data) {
192
+ const caches = yield createPageCaches(MAX_CACHE_EACH_REQ);
193
+ data = this.createCacheContent(cacheConfig, caches);
194
+ }
195
+ const cacheHash = this.find[fname(cacheConfig.level)](
196
+ context,
197
+ cacheKey,
198
+ data
199
+ );
200
+ if (!cacheHash) {
201
+ return false;
202
+ }
203
+ const cacheSyncOrAsync = () => __async(this, null, function* () {
204
+ const next = data;
205
+ const limit = cacheConfig.staleLimit;
206
+ const storeHTML = cacheAddition(html, cacheHash);
207
+ const size = storeHTML.length;
208
+ yield next.caches.set(cacheHash, {
209
+ expireTime: Date.now() + cacheConfig.interval * 1e3,
210
+ limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
211
+ cacheHash,
212
+ html: storeHTML,
213
+ size
214
+ });
215
+ this.cache.set(cacheKey, next);
216
+ return true;
203
217
  });
204
- this.cache.set(cacheKey, next);
205
- return true;
206
- };
207
-
208
- // cache set is async, each hash is cached only once at the same time
209
- const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(null, namespaceHash('stream', cacheHash), []);
210
- return maybeSync(doCache)(sync);
218
+ const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(
219
+ null,
220
+ namespaceHash("stream", cacheHash),
221
+ []
222
+ );
223
+ return maybeSync(doCache)(sync);
224
+ });
211
225
  }
212
- async del(context, cacheHash) {
213
- const cacheKey = this.generateRequestKey(context);
214
- const data = this.cache.get(cacheKey);
215
- data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
226
+ del(context, cacheHash) {
227
+ return __async(this, null, function* () {
228
+ const cacheKey = this.generateRequestKey(context);
229
+ const data = this.cache.get(cacheKey);
230
+ data == null ? void 0 : data.caches.del(cacheHash);
231
+ });
216
232
  }
217
233
  }
218
234
  let manager;
219
- export function createCache() {
235
+ function createCache() {
220
236
  if (manager) {
221
237
  return manager;
222
238
  }
223
- manager = new CacheManager({
224
- max: 0
225
- });
239
+ manager = new CacheManager({ max: 0 });
226
240
  return manager;
227
241
  }
228
- export function destroyCache() {
242
+ function destroyCache() {
229
243
  manager = null;
230
244
  }
231
- /* eslint-enable no-lone-blocks */
232
- /* eslint-enable @typescript-eslint/member-ordering */
245
+ export {
246
+ createCache,
247
+ destroyCache
248
+ };
@@ -1 +0,0 @@
1
- export {};
@@ -1,65 +1,87 @@
1
- import url from 'url';
2
- export function namespaceHash(namespace, hash) {
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 url from "url";
22
+ function namespaceHash(namespace, hash) {
3
23
  return `${namespace}/${hash}`;
4
24
  }
5
- export function fname(lv) {
25
+ function fname(lv) {
6
26
  return `f${lv}`;
7
27
  }
8
- export function connectFactor(...args) {
9
- return args.join('-');
28
+ function connectFactor(...args) {
29
+ return args.join("-");
10
30
  }
11
- export function valueFactory(obj) {
31
+ function valueFactory(obj) {
12
32
  if (obj instanceof url.URLSearchParams) {
13
- return function (key) {
33
+ return function(key) {
14
34
  return obj.get(key);
15
35
  };
16
36
  } else {
17
- return function (key) {
37
+ return function(key) {
18
38
  const value = obj[key];
19
39
  if (Array.isArray(value)) {
20
- return value.join(',');
40
+ return value.join(",");
21
41
  }
22
42
  return value;
23
43
  };
24
44
  }
25
45
  }
26
- export function getTime([s, ns]) {
46
+ function getTime([s, ns]) {
27
47
  return Math.floor(s * 1e3 + ns / 1e6);
28
48
  }
29
49
  const RE_START_IN_HEAD = /<head>/;
30
- export function cacheAddition(html, hash) {
31
- const additionHtml = html.replace(RE_START_IN_HEAD, `<head><meta name="x-moden-spr" content="${hash}">`);
50
+ function cacheAddition(html, hash) {
51
+ const additionHtml = html.replace(
52
+ RE_START_IN_HEAD,
53
+ `<head><meta name="x-moden-spr" content="${hash}">`
54
+ );
32
55
  return additionHtml;
33
56
  }
34
- const globalInvokeCache = new Map();
35
- export function withCoalescedInvoke(func) {
36
- return async function (key, args) {
37
- const entry = globalInvokeCache.get(key);
38
- if (entry) {
39
- return entry.then(res => ({
40
- isOrigin: false,
41
- value: res.value
42
- }));
43
- }
44
- function __wrapper() {
45
- return func(...args);
46
- }
47
- const future = __wrapper().then(res => {
48
- globalInvokeCache.delete(key);
49
- return {
50
- isOrigin: true,
51
- value: res
52
- };
53
- }).catch(err => {
54
- globalInvokeCache.delete(key);
55
- throw err;
57
+ const globalInvokeCache = /* @__PURE__ */ new Map();
58
+ function withCoalescedInvoke(func) {
59
+ return function(key, args) {
60
+ return __async(this, null, function* () {
61
+ const entry = globalInvokeCache.get(key);
62
+ if (entry) {
63
+ return entry.then((res) => ({
64
+ isOrigin: false,
65
+ value: res.value
66
+ }));
67
+ }
68
+ function __wrapper() {
69
+ return func(...args);
70
+ }
71
+ const future = __wrapper().then((res) => {
72
+ globalInvokeCache.delete(key);
73
+ return { isOrigin: true, value: res };
74
+ }).catch((err) => {
75
+ globalInvokeCache.delete(key);
76
+ throw err;
77
+ });
78
+ globalInvokeCache.set(key, future);
79
+ return future;
56
80
  });
57
- globalInvokeCache.set(key, future);
58
- return future;
59
81
  };
60
82
  }
61
- export function maybeSync(fn) {
62
- return sync => {
83
+ function maybeSync(fn) {
84
+ return (sync) => {
63
85
  if (sync) {
64
86
  return fn();
65
87
  } else {
@@ -67,4 +89,14 @@ export function maybeSync(fn) {
67
89
  return Promise.resolve();
68
90
  }
69
91
  };
70
- }
92
+ }
93
+ export {
94
+ cacheAddition,
95
+ connectFactor,
96
+ fname,
97
+ getTime,
98
+ maybeSync,
99
+ namespaceHash,
100
+ valueFactory,
101
+ withCoalescedInvoke
102
+ };
@@ -1,64 +1,84 @@
1
- import path from 'path';
2
- import { fs, mime } from '@modern-js/utils';
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 path from "path";
22
+ import { fs, mime } from "@modern-js/utils";
3
23
  import { ERROR_DIGEST } from "../../constants";
4
24
  import { handleDirectory } from "./static";
5
25
  import { readFile } from "./reader";
6
26
  import * as ssr from "./ssr";
7
- import { supportModern, getModernEntry } from "./modern";
8
- export const createRenderHandler = ({
27
+ const createRenderHandler = ({
9
28
  distDir,
10
29
  staticGenerate
11
- }) => async function render({
12
- ctx,
13
- route,
14
- runner
15
- }) {
16
- if (ctx.resHasHandled()) {
17
- return null;
18
- }
19
- const {
20
- entryPath,
21
- urlPath
22
- } = route;
23
- const entry = path.join(distDir, entryPath);
24
- if (!route.isSPA) {
25
- const result = await handleDirectory(ctx, entry, urlPath);
26
- return result;
27
- }
28
-
29
- // only spa can use es6-html
30
- const modernEntry = getModernEntry(entry);
31
- const useModern =
32
- // route.enableModernMode &&
33
- supportModern(ctx) && fs.existsSync(modernEntry);
34
- const templatePath = useModern ? modernEntry : entry;
35
- if (!fs.existsSync(templatePath)) {
36
- throw new Error(`Could not find template file: ${templatePath}`);
37
- }
38
- const content = await readFile(templatePath);
39
- if (!content) {
40
- return null;
41
- }
42
-
43
- // handles ssr first
44
- if (route.isSSR) {
45
- try {
46
- const result = await ssr.render(ctx, {
47
- distDir,
48
- entryName: route.entryName,
49
- urlPath: route.urlPath,
50
- bundle: route.bundle,
51
- template: content.toString(),
52
- staticGenerate
53
- }, runner);
30
+ }) => function render(_0) {
31
+ return __async(this, arguments, function* ({
32
+ ctx,
33
+ route,
34
+ runner
35
+ }) {
36
+ if (ctx.resHasHandled()) {
37
+ return null;
38
+ }
39
+ const { entryPath, urlPath } = route;
40
+ const entry = path.join(distDir, entryPath);
41
+ if (!route.isSPA) {
42
+ const result = yield handleDirectory(ctx, entry, urlPath);
54
43
  return result;
55
- } catch (err) {
56
- ctx.error(ERROR_DIGEST.ERENDER, err.stack || err.message);
57
- ctx.res.setHeader('x-modern-ssr-fallback', '1');
58
44
  }
59
- }
60
- return {
61
- content,
62
- contentType: mime.contentType(path.extname(templatePath))
63
- };
64
- };
45
+ const templatePath = entry;
46
+ if (!fs.existsSync(templatePath)) {
47
+ throw new Error(`Could not find template file: ${templatePath}`);
48
+ }
49
+ const content = yield readFile(templatePath);
50
+ if (!content) {
51
+ return null;
52
+ }
53
+ if (route.isSSR) {
54
+ try {
55
+ const result = yield ssr.render(
56
+ ctx,
57
+ {
58
+ distDir,
59
+ entryName: route.entryName,
60
+ urlPath: route.urlPath,
61
+ bundle: route.bundle,
62
+ template: content.toString(),
63
+ staticGenerate
64
+ },
65
+ runner
66
+ );
67
+ return result;
68
+ } catch (err) {
69
+ ctx.error(
70
+ ERROR_DIGEST.ERENDER,
71
+ err.stack || err.message
72
+ );
73
+ ctx.res.setHeader("x-modern-ssr-fallback", "1");
74
+ }
75
+ }
76
+ return {
77
+ content,
78
+ contentType: mime.contentType(path.extname(templatePath))
79
+ };
80
+ });
81
+ };
82
+ export {
83
+ createRenderHandler
84
+ };