@modern-js/prod-server 2.0.0-beta.3 → 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.
- package/CHANGELOG.md +69 -0
- package/dist/js/modern/constants.js +32 -25
- package/dist/js/modern/index.js +11 -6
- package/dist/js/modern/libs/context/context.js +52 -84
- package/dist/js/modern/libs/context/index.js +5 -2
- package/dist/js/modern/libs/hook-api/index.js +42 -35
- package/dist/js/modern/libs/hook-api/route.js +6 -7
- package/dist/js/modern/libs/hook-api/template.js +20 -34
- package/dist/js/modern/libs/loadConfig.js +45 -24
- package/dist/js/modern/libs/metrics.js +3 -4
- package/dist/js/modern/libs/proxy.js +68 -37
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -67
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -216
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
- package/dist/js/modern/libs/render/cache/index.js +88 -54
- package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -6
- package/dist/js/modern/libs/render/cache/spr.js +133 -117
- package/dist/js/modern/libs/render/cache/type.js +0 -1
- package/dist/js/modern/libs/render/cache/util.js +71 -39
- package/dist/js/modern/libs/render/index.js +76 -56
- package/dist/js/modern/libs/render/measure.js +38 -27
- package/dist/js/modern/libs/render/reader.js +65 -62
- package/dist/js/modern/libs/render/ssr.js +50 -32
- package/dist/js/modern/libs/render/static.js +50 -33
- package/dist/js/modern/libs/render/type.js +9 -6
- package/dist/js/modern/libs/route/index.js +8 -15
- package/dist/js/modern/libs/route/matcher.js +20 -34
- package/dist/js/modern/libs/route/route.js +9 -18
- package/dist/js/modern/libs/serve-file.js +33 -20
- package/dist/js/modern/server/index.js +144 -145
- package/dist/js/modern/server/modern-server-split.js +46 -12
- package/dist/js/modern/server/modern-server.js +377 -417
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +62 -43
- package/dist/js/modern/worker-server.js +34 -14
- package/dist/js/node/constants.js +45 -30
- package/dist/js/node/index.js +31 -57
- package/dist/js/node/libs/context/context.js +79 -94
- package/dist/js/node/libs/context/index.js +23 -13
- package/dist/js/node/libs/hook-api/index.js +69 -48
- package/dist/js/node/libs/hook-api/route.js +22 -11
- package/dist/js/node/libs/hook-api/template.js +36 -39
- package/dist/js/node/libs/loadConfig.js +69 -35
- package/dist/js/node/libs/metrics.js +21 -9
- package/dist/js/node/libs/proxy.js +86 -44
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +132 -70
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +268 -218
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +61 -55
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +54 -40
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +101 -119
- package/dist/js/node/libs/render/cache/index.js +110 -64
- package/dist/js/node/libs/render/cache/page-caches/index.js +50 -14
- package/dist/js/node/libs/render/cache/page-caches/lru.js +29 -12
- package/dist/js/node/libs/render/cache/spr.js +156 -129
- package/dist/js/node/libs/render/cache/type.js +0 -5
- package/dist/js/node/libs/render/cache/util.js +88 -45
- package/dist/js/node/libs/render/index.js +102 -67
- package/dist/js/node/libs/render/measure.js +55 -31
- package/dist/js/node/libs/render/reader.js +87 -70
- package/dist/js/node/libs/render/ssr.js +76 -47
- package/dist/js/node/libs/render/static.js +75 -40
- package/dist/js/node/libs/render/type.js +27 -12
- package/dist/js/node/libs/route/index.js +26 -26
- package/dist/js/node/libs/route/matcher.js +36 -41
- package/dist/js/node/libs/route/route.js +25 -22
- package/dist/js/node/libs/serve-file.js +61 -32
- package/dist/js/node/server/index.js +160 -160
- package/dist/js/node/server/modern-server-split.js +68 -22
- package/dist/js/node/server/modern-server.js +395 -441
- package/dist/js/node/type.js +0 -3
- package/dist/js/node/utils.js +74 -52
- package/dist/js/node/worker-server.js +53 -21
- package/dist/js/treeshaking/constants.js +26 -25
- package/dist/js/treeshaking/index.js +10 -10
- package/dist/js/treeshaking/libs/context/context.js +268 -237
- package/dist/js/treeshaking/libs/context/index.js +3 -3
- package/dist/js/treeshaking/libs/hook-api/index.js +265 -143
- package/dist/js/treeshaking/libs/hook-api/route.js +65 -30
- package/dist/js/treeshaking/libs/hook-api/template.js +121 -85
- package/dist/js/treeshaking/libs/loadConfig.js +80 -37
- package/dist/js/treeshaking/libs/metrics.js +4 -10
- package/dist/js/treeshaking/libs/proxy.js +240 -76
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +288 -121
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +772 -455
- package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +65 -51
- package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -35
- package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +144 -118
- package/dist/js/treeshaking/libs/render/cache/index.js +337 -175
- package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +151 -27
- package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +80 -42
- package/dist/js/treeshaking/libs/render/cache/spr.js +470 -340
- package/dist/js/treeshaking/libs/render/cache/type.js +1 -1
- package/dist/js/treeshaking/libs/render/cache/util.js +271 -92
- package/dist/js/treeshaking/libs/render/index.js +228 -95
- package/dist/js/treeshaking/libs/render/measure.js +142 -57
- package/dist/js/treeshaking/libs/render/reader.js +325 -177
- package/dist/js/treeshaking/libs/render/ssr.js +220 -95
- package/dist/js/treeshaking/libs/render/static.js +210 -78
- package/dist/js/treeshaking/libs/render/type.js +7 -6
- package/dist/js/treeshaking/libs/route/index.js +125 -89
- package/dist/js/treeshaking/libs/route/matcher.js +132 -107
- package/dist/js/treeshaking/libs/route/route.js +40 -26
- package/dist/js/treeshaking/libs/serve-file.js +177 -68
- package/dist/js/treeshaking/server/index.js +468 -327
- package/dist/js/treeshaking/server/modern-server-split.js +352 -144
- package/dist/js/treeshaking/server/modern-server.js +1046 -909
- package/dist/js/treeshaking/type.js +1 -1
- package/dist/js/treeshaking/utils.js +138 -81
- package/dist/js/treeshaking/worker-server.js +176 -55
- package/dist/types/index.d.ts +2 -0
- package/dist/types/libs/context/context.d.ts +4 -1
- package/dist/types/libs/loadConfig.d.ts +1 -0
- package/dist/types/libs/render/cache/index.d.ts +2 -0
- package/dist/types/libs/render/cache/spr.d.ts +2 -0
- package/dist/types/libs/route/route.d.ts +0 -1
- package/dist/types/server/index.d.ts +3 -0
- package/dist/types/utils.d.ts +1 -1
- package/dist/types/worker-server.d.ts +1 -2
- package/package.json +7 -14
- package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
- package/dist/js/modern/libs/render/modern/index.js +0 -37
- package/dist/js/node/libs/render/modern/browser-list.js +0 -14
- package/dist/js/node/libs/render/modern/index.js +0 -46
- package/dist/js/treeshaking/libs/render/modern/browser-list.js +0 -7
- package/dist/js/treeshaking/libs/render/modern/index.js +0 -39
- package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
- package/dist/types/libs/render/modern/index.d.ts +0 -3
|
@@ -1,10 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
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
|
|
61
|
-
return total + (((
|
|
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(
|
|
69
|
-
return md5.update(content).digest(
|
|
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
|
|
105
|
-
const queryKeys = (
|
|
106
|
-
const queryMatches = (
|
|
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
|
|
116
|
-
const headerKeys = (
|
|
117
|
-
const headerMatches = (
|
|
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
|
-
|
|
126
|
-
|
|
127
|
-
level
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
218
|
+
const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(
|
|
219
|
+
null,
|
|
220
|
+
namespaceHash("stream", cacheHash),
|
|
221
|
+
[]
|
|
222
|
+
);
|
|
223
|
+
return maybeSync(doCache)(sync);
|
|
224
|
+
});
|
|
211
225
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
|
|
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
|
-
|
|
242
|
+
function destroyCache() {
|
|
229
243
|
manager = null;
|
|
230
244
|
}
|
|
231
|
-
|
|
232
|
-
|
|
245
|
+
export {
|
|
246
|
+
createCache,
|
|
247
|
+
destroyCache
|
|
248
|
+
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,65 +1,87 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
|
|
25
|
+
function fname(lv) {
|
|
6
26
|
return `f${lv}`;
|
|
7
27
|
}
|
|
8
|
-
|
|
9
|
-
return args.join(
|
|
28
|
+
function connectFactor(...args) {
|
|
29
|
+
return args.join("-");
|
|
10
30
|
}
|
|
11
|
-
|
|
31
|
+
function valueFactory(obj) {
|
|
12
32
|
if (obj instanceof url.URLSearchParams) {
|
|
13
|
-
return function
|
|
33
|
+
return function(key) {
|
|
14
34
|
return obj.get(key);
|
|
15
35
|
};
|
|
16
36
|
} else {
|
|
17
|
-
return function
|
|
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
|
-
|
|
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
|
-
|
|
31
|
-
const additionHtml = html.replace(
|
|
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
|
-
|
|
36
|
-
return
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
isOrigin: true,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2
|
-
|
|
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
|
-
|
|
8
|
-
export const createRenderHandler = ({
|
|
27
|
+
const createRenderHandler = ({
|
|
9
28
|
distDir,
|
|
10
29
|
staticGenerate
|
|
11
|
-
}) =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
entryPath,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
+
};
|