@modern-js/server 1.4.3 → 1.4.5
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/dist/js/modern/dev-tools/mock/index.js +1 -1
- package/dist/js/modern/dev-tools/socket-server.js +2 -2
- package/dist/js/modern/index.js +1 -1
- package/dist/js/modern/server/{dev-server/dev-server-split.js → dev-server-split.js} +0 -0
- package/dist/js/modern/server/{dev-server/dev-server.js → dev-server.js} +15 -14
- package/dist/js/modern/server/index.js +15 -138
- package/dist/js/modern/{libs/render/cache/type.js → types.js} +0 -0
- package/dist/js/node/dev-tools/mock/index.js +2 -2
- package/dist/js/node/dev-tools/socket-server.js +2 -3
- package/dist/js/node/index.js +2 -2
- package/dist/js/node/server/{dev-server/dev-server-split.js → dev-server-split.js} +0 -0
- package/dist/js/node/server/{dev-server/dev-server.js → dev-server.js} +18 -24
- package/dist/js/node/server/index.js +17 -152
- package/dist/js/node/{libs/render/cache/type.js → types.js} +0 -0
- package/dist/types/dev-tools/babel/register.d.ts +1 -2
- package/dist/types/dev-tools/dev-server-plugin.d.ts +1 -1
- package/dist/types/dev-tools/launch-editor/index.d.ts +1 -2
- package/dist/types/dev-tools/mock/getMockData.d.ts +1 -2
- package/dist/types/dev-tools/mock/index.d.ts +1 -2
- package/dist/types/dev-tools/socket-server.d.ts +1 -1
- package/dist/types/index.d.ts +4 -5
- package/dist/types/server/{dev-server/dev-server-split.d.ts → dev-server-split.d.ts} +6 -5
- package/dist/types/server/{dev-server/dev-server.d.ts → dev-server.d.ts} +4 -3
- package/dist/types/server/index.d.ts +4 -19
- package/dist/types/types.d.ts +28 -0
- package/modern.config.js +5 -1
- package/package.json +11 -41
- package/tests/server.test.ts +10 -3
- package/dist/js/modern/constants.js +0 -26
- package/dist/js/modern/libs/context/context.js +0 -180
- package/dist/js/modern/libs/context/index.js +0 -3
- package/dist/js/modern/libs/hook-api/route.js +0 -39
- package/dist/js/modern/libs/hook-api/template.js +0 -61
- package/dist/js/modern/libs/metrics.js +0 -12
- package/dist/js/modern/libs/proxy.js +0 -33
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -70
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -233
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +0 -53
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +0 -35
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +0 -121
- package/dist/js/modern/libs/render/cache/index.js +0 -74
- package/dist/js/modern/libs/render/cache/page-caches/index.js +0 -9
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -35
- package/dist/js/modern/libs/render/cache/spr.js +0 -280
- package/dist/js/modern/libs/render/cache/util.js +0 -79
- package/dist/js/modern/libs/render/index.js +0 -65
- package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
- package/dist/js/modern/libs/render/modern/index.js +0 -42
- package/dist/js/modern/libs/render/reader.js +0 -112
- package/dist/js/modern/libs/render/ssr.js +0 -58
- package/dist/js/modern/libs/render/static.js +0 -46
- package/dist/js/modern/libs/render/type.js +0 -7
- package/dist/js/modern/libs/route/index.js +0 -68
- package/dist/js/modern/libs/route/matcher.js +0 -94
- package/dist/js/modern/libs/route/route.js +0 -24
- package/dist/js/modern/libs/serve-file.js +0 -28
- package/dist/js/modern/server/dev-server/index.js +0 -2
- package/dist/js/modern/server/modern-server-split.js +0 -81
- package/dist/js/modern/server/modern-server.js +0 -584
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +0 -112
- package/dist/js/node/constants.js +0 -36
- package/dist/js/node/libs/context/context.js +0 -194
- package/dist/js/node/libs/context/index.js +0 -18
- package/dist/js/node/libs/hook-api/route.js +0 -48
- package/dist/js/node/libs/hook-api/template.js +0 -69
- package/dist/js/node/libs/metrics.js +0 -18
- package/dist/js/node/libs/proxy.js +0 -44
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -77
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -238
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +0 -60
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +0 -42
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +0 -128
- package/dist/js/node/libs/render/cache/index.js +0 -86
- package/dist/js/node/libs/render/cache/page-caches/index.js +0 -17
- package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -47
- package/dist/js/node/libs/render/cache/spr.js +0 -298
- package/dist/js/node/libs/render/cache/util.js +0 -105
- package/dist/js/node/libs/render/index.js +0 -91
- package/dist/js/node/libs/render/modern/browser-list.js +0 -14
- package/dist/js/node/libs/render/modern/index.js +0 -58
- package/dist/js/node/libs/render/reader.js +0 -139
- package/dist/js/node/libs/render/ssr.js +0 -76
- package/dist/js/node/libs/render/static.js +0 -62
- package/dist/js/node/libs/render/type.js +0 -14
- package/dist/js/node/libs/route/index.js +0 -83
- package/dist/js/node/libs/route/matcher.js +0 -108
- package/dist/js/node/libs/route/route.js +0 -33
- package/dist/js/node/libs/serve-file.js +0 -41
- package/dist/js/node/server/dev-server/index.js +0 -27
- package/dist/js/node/server/modern-server-split.js +0 -97
- package/dist/js/node/server/modern-server.js +0 -622
- package/dist/js/node/type.js +0 -5
- package/dist/js/node/utils.js +0 -143
- package/dist/types/constants.d.ts +0 -20
- package/dist/types/libs/context/context.d.ts +0 -61
- package/dist/types/libs/context/index.d.ts +0 -4
- package/dist/types/libs/hook-api/route.d.ts +0 -14
- package/dist/types/libs/hook-api/template.d.ts +0 -14
- package/dist/types/libs/metrics.d.ts +0 -3
- package/dist/types/libs/proxy.d.ts +0 -4
- package/dist/types/libs/render/cache/__tests__/cache.fun.test.d.ts +0 -1
- package/dist/types/libs/render/cache/__tests__/cache.test.d.ts +0 -1
- package/dist/types/libs/render/cache/__tests__/cacheable.d.ts +0 -62
- package/dist/types/libs/render/cache/__tests__/error-configuration.d.ts +0 -28
- package/dist/types/libs/render/cache/__tests__/matched-cache.d.ts +0 -124
- package/dist/types/libs/render/cache/index.d.ts +0 -6
- package/dist/types/libs/render/cache/page-caches/index.d.ts +0 -2
- package/dist/types/libs/render/cache/page-caches/lru.d.ts +0 -15
- package/dist/types/libs/render/cache/spr.d.ts +0 -24
- package/dist/types/libs/render/cache/type.d.ts +0 -48
- package/dist/types/libs/render/cache/util.d.ts +0 -17
- package/dist/types/libs/render/index.d.ts +0 -18
- package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
- package/dist/types/libs/render/modern/index.d.ts +0 -3
- package/dist/types/libs/render/reader.d.ts +0 -18
- package/dist/types/libs/render/ssr.d.ts +0 -10
- package/dist/types/libs/render/static.d.ts +0 -3
- package/dist/types/libs/render/type.d.ts +0 -34
- package/dist/types/libs/route/index.d.ts +0 -15
- package/dist/types/libs/route/matcher.d.ts +0 -15
- package/dist/types/libs/route/route.d.ts +0 -14
- package/dist/types/libs/serve-file.d.ts +0 -8
- package/dist/types/server/dev-server/index.d.ts +0 -2
- package/dist/types/server/modern-server-split.d.ts +0 -26
- package/dist/types/server/modern-server.d.ts +0 -72
- package/dist/types/type.d.ts +0 -80
- package/dist/types/utils.d.ts +0 -19
- package/tests/context.test.ts +0 -52
- package/tests/fixtures/hosting-files/static/index.js +0 -1
- package/tests/fixtures/reader/index.ts +0 -3
- package/tests/fixtures/route-spec/dynamic.json +0 -13
- package/tests/fixtures/route-spec/index.json +0 -29
- package/tests/fixtures/ssr/bundle.js +0 -5
- package/tests/fixtures/static-dir/bar.html +0 -11
- package/tests/fixtures/static-dir/baz/index.html +0 -11
- package/tests/fixtures/static-dir/foo/index.html +0 -11
- package/tests/helper.ts +0 -8
- package/tests/hook.test.ts +0 -44
- package/tests/middleware.test.ts +0 -179
- package/tests/render.test.ts +0 -102
- package/tests/route.test.ts +0 -77
- package/tests/utils.test.ts +0 -106
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.createCache = createCache;
|
|
7
|
-
exports.destroyCache = destroyCache;
|
|
8
|
-
|
|
9
|
-
var _crypto = _interopRequireDefault(require("crypto"));
|
|
10
|
-
|
|
11
|
-
var _lruCache = _interopRequireDefault(require("lru-cache"));
|
|
12
|
-
|
|
13
|
-
var _mimeTypes = _interopRequireDefault(require("mime-types"));
|
|
14
|
-
|
|
15
|
-
var _util = require("./util");
|
|
16
|
-
|
|
17
|
-
var _pageCaches = require("./page-caches");
|
|
18
|
-
|
|
19
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
|
-
|
|
21
|
-
/* eslint-disable @typescript-eslint/member-ordering */
|
|
22
|
-
|
|
23
|
-
/* eslint-disable no-lone-blocks */
|
|
24
|
-
const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
|
|
25
|
-
const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
|
|
26
|
-
const BASE_LEVEL = 0;
|
|
27
|
-
const QUERY_LEVEL = 1;
|
|
28
|
-
const HEADER_LEVEL = 2;
|
|
29
|
-
const QUERY_HEADER_LEVEL = 3;
|
|
30
|
-
|
|
31
|
-
class CacheManager {
|
|
32
|
-
constructor(cacheOptions) {
|
|
33
|
-
this.cache = void 0;
|
|
34
|
-
this.cacheOptions = void 0;
|
|
35
|
-
|
|
36
|
-
this.find = (() => {
|
|
37
|
-
{
|
|
38
|
-
// eslint-disable-next-line consistent-this,@typescript-eslint/no-this-alias
|
|
39
|
-
const _this = this;
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
[(0, _util.fname)(BASE_LEVEL)](context, cacheKey // data: CacheContent,
|
|
43
|
-
) {
|
|
44
|
-
return _this.md5(cacheKey);
|
|
45
|
-
},
|
|
46
|
-
|
|
47
|
-
[(0, _util.fname)(QUERY_LEVEL)](context, cacheKey, data) {
|
|
48
|
-
const queryFactor = _this.queryFactor(context, data);
|
|
49
|
-
|
|
50
|
-
if (!queryFactor) {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return _this.md5((0, _util.connectFactor)(cacheKey, queryFactor));
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
[(0, _util.fname)(HEADER_LEVEL)](context, cacheKey, data) {
|
|
58
|
-
const headerFactor = _this.headerFactor(context, data);
|
|
59
|
-
|
|
60
|
-
if (!headerFactor) {
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return _this.md5((0, _util.connectFactor)(cacheKey, headerFactor));
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
[(0, _util.fname)(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
|
|
68
|
-
const queryFactor = _this.queryFactor(context, data);
|
|
69
|
-
|
|
70
|
-
const headerFactor = _this.headerFactor(context, data);
|
|
71
|
-
|
|
72
|
-
if (!queryFactor || !headerFactor) {
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return _this.md5((0, _util.connectFactor)(cacheKey, headerFactor, queryFactor));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
})();
|
|
82
|
-
|
|
83
|
-
this.cacheOptions = cacheOptions;
|
|
84
|
-
this.cache = new _lruCache.default({
|
|
85
|
-
max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
|
|
86
|
-
|
|
87
|
-
// 默认存 100M,最大 600M
|
|
88
|
-
length(n) {
|
|
89
|
-
const len = n.caches.keys().reduce((total, cur) => {
|
|
90
|
-
var _n$caches$peek;
|
|
91
|
-
|
|
92
|
-
return total + (((_n$caches$peek = n.caches.peek(cur)) === null || _n$caches$peek === void 0 ? void 0 : _n$caches$peek.size) || 0);
|
|
93
|
-
}, 1);
|
|
94
|
-
return len;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
md5(content) {
|
|
101
|
-
const md5 = _crypto.default.createHash('md5');
|
|
102
|
-
|
|
103
|
-
return md5.update(content).digest('hex');
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
generateRequestKey(context) {
|
|
107
|
-
const {
|
|
108
|
-
pathname,
|
|
109
|
-
entry
|
|
110
|
-
} = context;
|
|
111
|
-
return this.md5(`${pathname}_${entry}`);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
replaceValue(value, matcher) {
|
|
115
|
-
let final = value;
|
|
116
|
-
Object.keys(matcher).some(replacer => {
|
|
117
|
-
const reg = new RegExp(matcher[replacer]);
|
|
118
|
-
|
|
119
|
-
if (reg.test(value)) {
|
|
120
|
-
final = replacer;
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return false;
|
|
125
|
-
});
|
|
126
|
-
return final;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
factor(keys, obj, matches = {}) {
|
|
130
|
-
keys.sort();
|
|
131
|
-
const getValue = (0, _util.valueFactory)(obj);
|
|
132
|
-
const factorAry = keys.reduce((ary, key) => {
|
|
133
|
-
let value = getValue(key) || '';
|
|
134
|
-
const matcher = matches[key];
|
|
135
|
-
|
|
136
|
-
if (matcher) {
|
|
137
|
-
value = this.replaceValue(value, matcher);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return ary.concat([key, value]);
|
|
141
|
-
}, []);
|
|
142
|
-
return factorAry.join(',');
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
queryFactor(context, data) {
|
|
146
|
-
var _data$includes, _data$matches;
|
|
147
|
-
|
|
148
|
-
const queryKeys = (_data$includes = data.includes) === null || _data$includes === void 0 ? void 0 : _data$includes.query;
|
|
149
|
-
const queryMatches = (_data$matches = data.matches) === null || _data$matches === void 0 ? void 0 : _data$matches.query;
|
|
150
|
-
|
|
151
|
-
if (!queryKeys || queryKeys.length === 0) {
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const requestQuery = context.query;
|
|
156
|
-
const queryFactor = this.factor(queryKeys, requestQuery, queryMatches);
|
|
157
|
-
return queryFactor;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
headerFactor(context, data) {
|
|
161
|
-
var _data$includes2, _data$matches2;
|
|
162
|
-
|
|
163
|
-
const headerKeys = (_data$includes2 = data.includes) === null || _data$includes2 === void 0 ? void 0 : _data$includes2.header;
|
|
164
|
-
const headerMatches = (_data$matches2 = data.matches) === null || _data$matches2 === void 0 ? void 0 : _data$matches2.header;
|
|
165
|
-
|
|
166
|
-
if (!headerKeys || headerKeys.length === 0) {
|
|
167
|
-
return null;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const requestHeader = context.headers;
|
|
171
|
-
const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
|
|
172
|
-
return headerFactor;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
async best(context, cacheKey, data) {
|
|
176
|
-
const {
|
|
177
|
-
level
|
|
178
|
-
} = data;
|
|
179
|
-
const cacheHash = this.find[(0, _util.fname)(level)](context, cacheKey, data);
|
|
180
|
-
|
|
181
|
-
if (!cacheHash) {
|
|
182
|
-
return null;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return data.caches.get(cacheHash);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
createCacheContent(config, caches) {
|
|
189
|
-
return {
|
|
190
|
-
level: config.level,
|
|
191
|
-
interval: config.interval,
|
|
192
|
-
includes: config.includes || null,
|
|
193
|
-
limit: config.staleLimit,
|
|
194
|
-
matches: config.matches || null,
|
|
195
|
-
caches
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
async get(context) {
|
|
200
|
-
const cacheKey = this.generateRequestKey(context);
|
|
201
|
-
const data = this.cache.get(cacheKey); // no cache key matched
|
|
202
|
-
|
|
203
|
-
if (!data) {
|
|
204
|
-
return null;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const dest = await this.best(context, cacheKey, data); // no cache for current page with current config
|
|
208
|
-
|
|
209
|
-
if (!dest) {
|
|
210
|
-
return null;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const {
|
|
214
|
-
expireTime,
|
|
215
|
-
limitTime,
|
|
216
|
-
html,
|
|
217
|
-
cacheHash
|
|
218
|
-
} = dest;
|
|
219
|
-
const isStale = Date.now() - expireTime > 0;
|
|
220
|
-
const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
|
|
221
|
-
return {
|
|
222
|
-
content: html || '',
|
|
223
|
-
contentType: _mimeTypes.default.contentType('html'),
|
|
224
|
-
isStale,
|
|
225
|
-
isGarbage,
|
|
226
|
-
hash: cacheHash
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
async set(context, html, cacheConfig, sync = false) {
|
|
231
|
-
if (!cacheConfig) {
|
|
232
|
-
return false;
|
|
233
|
-
} // each version with route is a separate cache
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const cacheKey = this.generateRequestKey(context);
|
|
237
|
-
let data = this.cache.get(cacheKey);
|
|
238
|
-
|
|
239
|
-
if (!data) {
|
|
240
|
-
const caches = await (0, _pageCaches.createPageCaches)(MAX_CACHE_EACH_REQ); // eslint-disable-next-line require-atomic-updates
|
|
241
|
-
|
|
242
|
-
data = this.createCacheContent(cacheConfig, caches);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const cacheHash = this.find[(0, _util.fname)(cacheConfig.level)](context, cacheKey, data); // if cacheHash is null, maybe level not match meta key, do not cache
|
|
246
|
-
|
|
247
|
-
if (!cacheHash) {
|
|
248
|
-
return false;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const cacheSyncOrAsync = async () => {
|
|
252
|
-
const next = data;
|
|
253
|
-
const limit = cacheConfig.staleLimit;
|
|
254
|
-
const storeHTML = (0, _util.cacheAddition)(html, cacheHash);
|
|
255
|
-
const size = storeHTML.length;
|
|
256
|
-
await next.caches.set(cacheHash, {
|
|
257
|
-
expireTime: Date.now() + cacheConfig.interval * 1000,
|
|
258
|
-
limitTime: typeof limit === 'number' ? Date.now() + limit * 1000 : null,
|
|
259
|
-
cacheHash,
|
|
260
|
-
html: storeHTML,
|
|
261
|
-
size
|
|
262
|
-
});
|
|
263
|
-
this.cache.set(cacheKey, next);
|
|
264
|
-
return true;
|
|
265
|
-
}; // cache set is async, each hash is cached only once at the same time
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
const doCache = (0, _util.withCoalescedInvoke)(cacheSyncOrAsync).bind(null, (0, _util.namespaceHash)('stream', cacheHash), []);
|
|
269
|
-
return (0, _util.maybeSync)(doCache)(sync);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
async del(context, cacheHash) {
|
|
273
|
-
const cacheKey = this.generateRequestKey(context);
|
|
274
|
-
const data = this.cache.get(cacheKey);
|
|
275
|
-
data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
let manager;
|
|
281
|
-
|
|
282
|
-
function createCache() {
|
|
283
|
-
if (manager) {
|
|
284
|
-
return manager;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
manager = new CacheManager({
|
|
288
|
-
max: 0
|
|
289
|
-
});
|
|
290
|
-
return manager;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
function destroyCache() {
|
|
294
|
-
manager = null;
|
|
295
|
-
}
|
|
296
|
-
/* eslint-enable no-lone-blocks */
|
|
297
|
-
|
|
298
|
-
/* eslint-enable @typescript-eslint/member-ordering */
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.cacheAddition = cacheAddition;
|
|
7
|
-
exports.connectFactor = connectFactor;
|
|
8
|
-
exports.fname = fname;
|
|
9
|
-
exports.getTime = getTime;
|
|
10
|
-
exports.maybeSync = maybeSync;
|
|
11
|
-
exports.namespaceHash = namespaceHash;
|
|
12
|
-
exports.valueFactory = valueFactory;
|
|
13
|
-
exports.withCoalescedInvoke = withCoalescedInvoke;
|
|
14
|
-
|
|
15
|
-
var _url = _interopRequireDefault(require("url"));
|
|
16
|
-
|
|
17
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
18
|
-
|
|
19
|
-
function namespaceHash(namespace, hash) {
|
|
20
|
-
return `${namespace}/${hash}`;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function fname(lv) {
|
|
24
|
-
return `f${lv}`;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function connectFactor(...args) {
|
|
28
|
-
return args.join('-');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function valueFactory(obj) {
|
|
32
|
-
if (obj instanceof _url.default.URLSearchParams) {
|
|
33
|
-
return function (key) {
|
|
34
|
-
return obj.get(key);
|
|
35
|
-
};
|
|
36
|
-
} else {
|
|
37
|
-
return function (key) {
|
|
38
|
-
const value = obj[key];
|
|
39
|
-
|
|
40
|
-
if (Array.isArray(value)) {
|
|
41
|
-
return value.join(',');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return value;
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function getTime([s, ns]) {
|
|
50
|
-
return Math.floor(s * 1e3 + ns / 1e6);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const RE_START_IN_HEAD = /<head>/;
|
|
54
|
-
|
|
55
|
-
function cacheAddition(html, hash) {
|
|
56
|
-
const additionHtml = html.replace(RE_START_IN_HEAD, `<head><meta name="x-moden-spr" content="${hash}">`);
|
|
57
|
-
return additionHtml;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const globalInvokeCache = new Map();
|
|
61
|
-
|
|
62
|
-
function withCoalescedInvoke(func) {
|
|
63
|
-
return async function (key, args) {
|
|
64
|
-
const entry = globalInvokeCache.get(key);
|
|
65
|
-
|
|
66
|
-
if (entry) {
|
|
67
|
-
// eslint-disable-next-line promise/prefer-await-to-then
|
|
68
|
-
return entry.then(res => ({
|
|
69
|
-
isOrigin: false,
|
|
70
|
-
value: res.value
|
|
71
|
-
}));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function __wrapper() {
|
|
75
|
-
return func(...args);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const future = __wrapper() // eslint-disable-next-line promise/prefer-await-to-then
|
|
79
|
-
.then(res => {
|
|
80
|
-
globalInvokeCache.delete(key);
|
|
81
|
-
return {
|
|
82
|
-
isOrigin: true,
|
|
83
|
-
value: res
|
|
84
|
-
};
|
|
85
|
-
}) // eslint-disable-next-line promise/prefer-await-to-then
|
|
86
|
-
.catch(err => {
|
|
87
|
-
globalInvokeCache.delete(key);
|
|
88
|
-
throw err;
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
globalInvokeCache.set(key, future);
|
|
92
|
-
return future;
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function maybeSync(fn) {
|
|
97
|
-
return sync => {
|
|
98
|
-
if (sync) {
|
|
99
|
-
return fn();
|
|
100
|
-
} else {
|
|
101
|
-
fn();
|
|
102
|
-
return Promise.resolve();
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.createRenderHandler = void 0;
|
|
7
|
-
|
|
8
|
-
var _path = _interopRequireDefault(require("path"));
|
|
9
|
-
|
|
10
|
-
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
|
-
var _mimeTypes = _interopRequireDefault(require("mime-types"));
|
|
13
|
-
|
|
14
|
-
var _constants = require("../../constants");
|
|
15
|
-
|
|
16
|
-
var _static = require("./static");
|
|
17
|
-
|
|
18
|
-
var _reader = require("./reader");
|
|
19
|
-
|
|
20
|
-
var ssr = _interopRequireWildcard(require("./ssr"));
|
|
21
|
-
|
|
22
|
-
var _modern = require("./modern");
|
|
23
|
-
|
|
24
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
25
|
-
|
|
26
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
27
|
-
|
|
28
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
|
-
|
|
30
|
-
const createRenderHandler = ({
|
|
31
|
-
distDir,
|
|
32
|
-
staticGenerate
|
|
33
|
-
}) => async function render({
|
|
34
|
-
ctx,
|
|
35
|
-
route,
|
|
36
|
-
runner
|
|
37
|
-
}) {
|
|
38
|
-
if (ctx.resHasHandled()) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const {
|
|
43
|
-
entryPath,
|
|
44
|
-
urlPath
|
|
45
|
-
} = route;
|
|
46
|
-
|
|
47
|
-
const entry = _path.default.join(distDir, entryPath);
|
|
48
|
-
|
|
49
|
-
if (!route.isSPA) {
|
|
50
|
-
const result = await (0, _static.handleDirectory)(ctx, entry, urlPath);
|
|
51
|
-
return result;
|
|
52
|
-
} // only spa can use es6-html
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const modernEntry = (0, _modern.getModernEntry)(entry);
|
|
56
|
-
|
|
57
|
-
const useModern = // route.enableModernMode &&
|
|
58
|
-
(0, _modern.supportModern)(ctx) && _utils.fs.existsSync(modernEntry);
|
|
59
|
-
|
|
60
|
-
const templateHTML = useModern ? modernEntry : entry; // handles ssr first
|
|
61
|
-
|
|
62
|
-
if (route.isSSR) {
|
|
63
|
-
try {
|
|
64
|
-
const result = await ssr.render(ctx, {
|
|
65
|
-
distDir,
|
|
66
|
-
entryName: route.entryName,
|
|
67
|
-
urlPath: route.urlPath,
|
|
68
|
-
bundle: route.bundle,
|
|
69
|
-
template: templateHTML,
|
|
70
|
-
staticGenerate
|
|
71
|
-
}, runner);
|
|
72
|
-
return result;
|
|
73
|
-
} catch (err) {
|
|
74
|
-
ctx.error(_constants.ERROR_DIGEST.ERENDER, err.stack);
|
|
75
|
-
ctx.res.setHeader('x-modern-ssr-fallback', '1');
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const content = await (0, _reader.readFile)(templateHTML);
|
|
80
|
-
|
|
81
|
-
if (!content) {
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return {
|
|
86
|
-
content,
|
|
87
|
-
contentType: _mimeTypes.default.contentType(_path.default.extname(templateHTML))
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
exports.createRenderHandler = createRenderHandler;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.NativeModuleNameMap = void 0;
|
|
7
|
-
const NativeModuleNameMap = {
|
|
8
|
-
Chrome: 'chrome',
|
|
9
|
-
'Mobile Safari': 'ios_saf',
|
|
10
|
-
Safari: 'safari',
|
|
11
|
-
Edge: 'edge',
|
|
12
|
-
Firefox: 'firefox'
|
|
13
|
-
};
|
|
14
|
-
exports.NativeModuleNameMap = NativeModuleNameMap;
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.supportModern = exports.getModernEntry = void 0;
|
|
7
|
-
|
|
8
|
-
var _uaParserJs = _interopRequireDefault(require("ua-parser-js"));
|
|
9
|
-
|
|
10
|
-
var _compareVersions = _interopRequireDefault(require("compare-versions"));
|
|
11
|
-
|
|
12
|
-
var _browserList = require("./browser-list");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
const nativeModules = require('@babel/compat-data/native-modules');
|
|
17
|
-
|
|
18
|
-
const supportModern = context => {
|
|
19
|
-
if (context.query.modern_es6) {
|
|
20
|
-
return true;
|
|
21
|
-
} // no ua in request headers
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const userAgent = context.headers['user-agent'];
|
|
25
|
-
|
|
26
|
-
if (!userAgent || typeof userAgent !== 'string') {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const parsedUA = (0, _uaParserJs.default)(userAgent);
|
|
31
|
-
const browserName = parsedUA.browser.name;
|
|
32
|
-
const browserVersion = parsedUA.browser.version;
|
|
33
|
-
|
|
34
|
-
if (!browserName || !browserVersion) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const nativeUAName = _browserList.NativeModuleNameMap[browserName];
|
|
39
|
-
|
|
40
|
-
if (!nativeUAName) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const version = nativeModules['es6.module'][nativeUAName];
|
|
45
|
-
|
|
46
|
-
if (!version) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const result = (0, _compareVersions.default)(browserVersion, version);
|
|
51
|
-
return result >= 0;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
exports.supportModern = supportModern;
|
|
55
|
-
|
|
56
|
-
const getModernEntry = filepath => filepath.replace(/\.html$/, '-es6.html');
|
|
57
|
-
|
|
58
|
-
exports.getModernEntry = getModernEntry;
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.updateFile = exports.readFile = exports.init = exports.close = exports.LruReader = void 0;
|
|
7
|
-
|
|
8
|
-
var _utils = require("@modern-js/utils");
|
|
9
|
-
|
|
10
|
-
var _lruCache = _interopRequireDefault(require("lru-cache"));
|
|
11
|
-
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
|
|
14
|
-
const Byte = 1;
|
|
15
|
-
const KB = 1024 * Byte;
|
|
16
|
-
const MB = 1024 * KB;
|
|
17
|
-
|
|
18
|
-
const getContentLength = cache => cache.content.length;
|
|
19
|
-
|
|
20
|
-
const createCacheItem = async (filepath, mtime) => {
|
|
21
|
-
const content = await _utils.fs.readFile(filepath);
|
|
22
|
-
return {
|
|
23
|
-
content,
|
|
24
|
-
mtime
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
class LruReader {
|
|
29
|
-
// private timer?: NodeJS.Timeout;
|
|
30
|
-
constructor() {
|
|
31
|
-
this.cache = void 0;
|
|
32
|
-
this.cache = new _lruCache.default({
|
|
33
|
-
max: 256 * MB,
|
|
34
|
-
length: getContentLength,
|
|
35
|
-
maxAge: 5 * 60 * 5000 // 60s
|
|
36
|
-
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
init() {// this.timeTask();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
close() {// if (this.timer) {
|
|
44
|
-
// clearInterval(this.timer);
|
|
45
|
-
// }
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async read(filepath) {
|
|
49
|
-
if (this.cache.has(filepath)) {
|
|
50
|
-
const {
|
|
51
|
-
content
|
|
52
|
-
} = this.cache.get(filepath);
|
|
53
|
-
return {
|
|
54
|
-
content
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (!_utils.fs.existsSync(filepath)) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const stat = _utils.fs.statSync(filepath);
|
|
63
|
-
|
|
64
|
-
if (stat.isDirectory()) {
|
|
65
|
-
return null;
|
|
66
|
-
} // if file more than 20 MB
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (stat.size > 20 * MB) {
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const item = await createCacheItem(filepath, stat.mtime);
|
|
74
|
-
this.cache.set(filepath, item);
|
|
75
|
-
return item;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
update() {
|
|
79
|
-
const {
|
|
80
|
-
cache
|
|
81
|
-
} = this;
|
|
82
|
-
const files = cache.keys();
|
|
83
|
-
|
|
84
|
-
for (const filepath of files) {
|
|
85
|
-
if (!_utils.fs.existsSync(filepath)) {
|
|
86
|
-
cache.del(filepath);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
const item = cache.get(filepath);
|
|
91
|
-
|
|
92
|
-
const stat = _utils.fs.statSync(filepath);
|
|
93
|
-
|
|
94
|
-
const {
|
|
95
|
-
mtime
|
|
96
|
-
} = stat; // file is modify
|
|
97
|
-
|
|
98
|
-
if (item.mtime < mtime) {
|
|
99
|
-
cache.del(filepath);
|
|
100
|
-
}
|
|
101
|
-
} catch (e) {
|
|
102
|
-
// for safe
|
|
103
|
-
cache.del(filepath);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
} // private timeTask() {
|
|
107
|
-
// this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
|
|
108
|
-
// }
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
exports.LruReader = LruReader;
|
|
114
|
-
const reader = new LruReader();
|
|
115
|
-
|
|
116
|
-
const readFile = async filepath => {
|
|
117
|
-
const file = await reader.read(filepath);
|
|
118
|
-
return file === null || file === void 0 ? void 0 : file.content;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
exports.readFile = readFile;
|
|
122
|
-
|
|
123
|
-
const updateFile = () => {
|
|
124
|
-
reader.update();
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
exports.updateFile = updateFile;
|
|
128
|
-
|
|
129
|
-
const init = () => {
|
|
130
|
-
reader.init();
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
exports.init = init;
|
|
134
|
-
|
|
135
|
-
const close = () => {
|
|
136
|
-
reader.close();
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
exports.close = close;
|