@modern-js/prod-server 2.4.1-beta.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.
- package/CHANGELOG.md +18 -3
- package/dist/{js/node → cjs}/constants.js +0 -0
- package/dist/{js/node → cjs}/index.js +0 -0
- package/dist/{js/node → cjs}/libs/context/context.js +0 -0
- package/dist/{js/node → cjs}/libs/context/index.js +0 -0
- package/dist/{js/node → cjs}/libs/hook-api/index.js +12 -25
- package/dist/{js/node → cjs}/libs/hook-api/route.js +0 -0
- package/dist/{js/node → cjs}/libs/hook-api/template.js +0 -0
- package/dist/{js/node → cjs}/libs/loadConfig.js +6 -21
- package/dist/{js/node → cjs}/libs/logger.js +5 -16
- package/dist/{js/node → cjs}/libs/metrics.js +0 -0
- package/dist/{js/node → cjs}/libs/proxy.js +6 -39
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/cache.fun.test.js +12 -34
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/cache.test.js +43 -78
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/cacheable.js +0 -0
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/error-configuration.js +0 -0
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/matched-cache.js +0 -0
- package/dist/cjs/libs/render/cache/index.js +97 -0
- package/dist/cjs/libs/render/cache/page-caches/index.js +33 -0
- package/dist/{js/node → cjs}/libs/render/cache/page-caches/lru.js +0 -0
- package/dist/{js/node → cjs}/libs/render/cache/spr.js +69 -97
- package/dist/{js/node → cjs}/libs/render/cache/type.js +0 -0
- package/dist/{js/node → cjs}/libs/render/cache/util.js +19 -41
- package/dist/cjs/libs/render/index.js +93 -0
- package/dist/{js/node → cjs}/libs/render/measure.js +6 -21
- package/dist/{js/node → cjs}/libs/render/reader.js +24 -46
- package/dist/{js/node → cjs}/libs/render/ssr.js +3 -23
- package/dist/{js/node → cjs}/libs/render/static.js +22 -44
- package/dist/{js/node → cjs}/libs/render/type.js +0 -0
- package/dist/{js/node → cjs}/libs/route/index.js +0 -0
- package/dist/{js/node → cjs}/libs/route/matcher.js +0 -0
- package/dist/{js/node → cjs}/libs/route/route.js +0 -0
- package/dist/{js/node → cjs}/libs/serve-file.js +2 -22
- package/dist/{js/node → cjs}/server/index.js +56 -106
- package/dist/{js/node → cjs}/server/modern-server-split.js +9 -40
- package/dist/cjs/server/modern-server.js +490 -0
- package/dist/{js/node → cjs}/type.js +0 -0
- package/dist/{js/node → cjs}/utils.js +1 -15
- package/dist/{js/node → cjs}/worker-server.js +3 -23
- package/dist/{js/treeshaking → esm}/constants.js +0 -0
- package/dist/{js/treeshaking → esm}/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/context/context.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/context/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/hook-api/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/hook-api/route.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/hook-api/template.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/loadConfig.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/logger.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/metrics.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/proxy.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cache.fun.test.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cache.test.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cacheable.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/error-configuration.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/matched-cache.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/page-caches/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/page-caches/lru.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/spr.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/type.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/util.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/index.js +4 -3
- package/dist/{js/treeshaking → esm}/libs/render/measure.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/reader.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/ssr.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/static.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/type.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/route/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/route/matcher.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/route/route.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/serve-file.js +0 -0
- package/dist/{js/treeshaking → esm}/server/index.js +0 -0
- package/dist/{js/treeshaking → esm}/server/modern-server-split.js +0 -0
- package/dist/{js/treeshaking → esm}/server/modern-server.js +14 -8
- package/dist/{js/treeshaking → esm}/type.js +0 -0
- package/dist/{js/treeshaking → esm}/utils.js +0 -0
- package/dist/{js/treeshaking → esm}/worker-server.js +0 -0
- package/dist/{js/modern → esm-node}/constants.js +0 -0
- package/dist/{js/modern → esm-node}/index.js +0 -0
- package/dist/{js/modern → esm-node}/libs/context/context.js +0 -0
- package/dist/{js/modern → esm-node}/libs/context/index.js +0 -0
- package/dist/{js/modern → esm-node}/libs/hook-api/index.js +12 -27
- package/dist/{js/modern → esm-node}/libs/hook-api/route.js +0 -0
- package/dist/{js/modern → esm-node}/libs/hook-api/template.js +0 -0
- package/dist/esm-node/libs/loadConfig.js +45 -0
- package/dist/{js/modern → esm-node}/libs/logger.js +5 -18
- package/dist/{js/modern → esm-node}/libs/metrics.js +0 -0
- package/dist/{js/modern → esm-node}/libs/proxy.js +6 -41
- package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +83 -0
- package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +210 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/__tests__/cacheable.js +0 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/__tests__/error-configuration.js +0 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/__tests__/matched-cache.js +0 -0
- package/dist/esm-node/libs/render/cache/index.js +76 -0
- package/dist/esm-node/libs/render/cache/page-caches/index.js +10 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/page-caches/lru.js +0 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/spr.js +69 -97
- package/dist/{js/modern → esm-node}/libs/render/cache/type.js +0 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/util.js +19 -41
- package/dist/esm-node/libs/render/index.js +64 -0
- package/dist/{js/modern → esm-node}/libs/render/measure.js +6 -23
- package/dist/esm-node/libs/render/reader.js +85 -0
- package/dist/{js/modern → esm-node}/libs/render/ssr.js +3 -23
- package/dist/esm-node/libs/render/static.js +38 -0
- package/dist/{js/modern → esm-node}/libs/render/type.js +0 -0
- package/dist/{js/modern → esm-node}/libs/route/index.js +0 -0
- package/dist/{js/modern → esm-node}/libs/route/matcher.js +0 -0
- package/dist/{js/modern → esm-node}/libs/route/route.js +0 -0
- package/dist/{js/modern → esm-node}/libs/serve-file.js +2 -22
- package/dist/esm-node/server/index.js +156 -0
- package/dist/esm-node/server/modern-server-split.js +43 -0
- package/dist/esm-node/server/modern-server.js +483 -0
- package/dist/{js/modern → esm-node}/type.js +0 -0
- package/dist/{js/modern → esm-node}/utils.js +1 -17
- package/dist/{js/modern → esm-node}/worker-server.js +3 -23
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/render/index.d.ts +3 -1
- package/dist/types/utils.d.ts +1 -1
- package/package.json +15 -16
- package/dist/js/modern/libs/loadConfig.js +0 -62
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -114
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -254
- package/dist/js/modern/libs/render/cache/index.js +0 -115
- package/dist/js/modern/libs/render/cache/page-caches/index.js +0 -32
- package/dist/js/modern/libs/render/index.js +0 -84
- package/dist/js/modern/libs/render/reader.js +0 -107
- package/dist/js/modern/libs/render/static.js +0 -60
- package/dist/js/modern/server/index.js +0 -208
- package/dist/js/modern/server/modern-server-split.js +0 -74
- package/dist/js/modern/server/modern-server.js +0 -548
- package/dist/js/node/libs/render/cache/index.js +0 -134
- package/dist/js/node/libs/render/cache/page-caches/index.js +0 -55
- package/dist/js/node/libs/render/index.js +0 -113
- package/dist/js/node/server/modern-server.js +0 -553
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LRUCaches } from "./lru";
|
|
2
|
+
async function createPageCaches(max) {
|
|
3
|
+
const constructorOptions = { max };
|
|
4
|
+
const cacheInstance = new LRUCaches(constructorOptions);
|
|
5
|
+
await cacheInstance.init();
|
|
6
|
+
return cacheInstance;
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
createPageCaches
|
|
10
|
+
};
|
|
File without changes
|
|
@@ -1,23 +1,3 @@
|
|
|
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
1
|
import crypto from "crypto";
|
|
22
2
|
import LRUCache from "lru-cache";
|
|
23
3
|
import { mime } from "@modern-js/utils";
|
|
@@ -138,15 +118,13 @@ class CacheManager {
|
|
|
138
118
|
const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
|
|
139
119
|
return headerFactor;
|
|
140
120
|
}
|
|
141
|
-
best(context, cacheKey, data) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
return data.caches.get(cacheHash);
|
|
149
|
-
});
|
|
121
|
+
async best(context, cacheKey, data) {
|
|
122
|
+
const { level } = data;
|
|
123
|
+
const cacheHash = this.find[fname(level)](context, cacheKey, data);
|
|
124
|
+
if (!cacheHash) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
return data.caches.get(cacheHash);
|
|
150
128
|
}
|
|
151
129
|
createCacheContent(config, caches) {
|
|
152
130
|
return {
|
|
@@ -158,77 +136,71 @@ class CacheManager {
|
|
|
158
136
|
caches
|
|
159
137
|
};
|
|
160
138
|
}
|
|
161
|
-
get(context) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
};
|
|
182
|
-
});
|
|
139
|
+
async get(context) {
|
|
140
|
+
const cacheKey = this.generateRequestKey(context);
|
|
141
|
+
const data = this.cache.get(cacheKey);
|
|
142
|
+
if (!data) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
const dest = await this.best(context, cacheKey, data);
|
|
146
|
+
if (!dest) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const { expireTime, limitTime, html, cacheHash } = dest;
|
|
150
|
+
const isStale = Date.now() - expireTime > 0;
|
|
151
|
+
const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
|
|
152
|
+
return {
|
|
153
|
+
content: html || "",
|
|
154
|
+
contentType: mime.contentType("html"),
|
|
155
|
+
isStale,
|
|
156
|
+
isGarbage,
|
|
157
|
+
hash: cacheHash
|
|
158
|
+
};
|
|
183
159
|
}
|
|
184
|
-
set(context, html, cacheConfig, sync = false) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
size
|
|
214
|
-
});
|
|
215
|
-
this.cache.set(cacheKey, next);
|
|
216
|
-
return true;
|
|
160
|
+
async set(context, html, cacheConfig, sync = false) {
|
|
161
|
+
if (!cacheConfig) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
const cacheKey = this.generateRequestKey(context);
|
|
165
|
+
let data = this.cache.get(cacheKey);
|
|
166
|
+
if (!data) {
|
|
167
|
+
const caches = await createPageCaches(MAX_CACHE_EACH_REQ);
|
|
168
|
+
data = this.createCacheContent(cacheConfig, caches);
|
|
169
|
+
}
|
|
170
|
+
const cacheHash = this.find[fname(cacheConfig.level)](
|
|
171
|
+
context,
|
|
172
|
+
cacheKey,
|
|
173
|
+
data
|
|
174
|
+
);
|
|
175
|
+
if (!cacheHash) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
const cacheSyncOrAsync = async () => {
|
|
179
|
+
const next = data;
|
|
180
|
+
const limit = cacheConfig.staleLimit;
|
|
181
|
+
const storeHTML = cacheAddition(html, cacheHash);
|
|
182
|
+
const size = storeHTML.length;
|
|
183
|
+
await next.caches.set(cacheHash, {
|
|
184
|
+
expireTime: Date.now() + cacheConfig.interval * 1e3,
|
|
185
|
+
limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
|
|
186
|
+
cacheHash,
|
|
187
|
+
html: storeHTML,
|
|
188
|
+
size
|
|
217
189
|
});
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
190
|
+
this.cache.set(cacheKey, next);
|
|
191
|
+
return true;
|
|
192
|
+
};
|
|
193
|
+
const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(
|
|
194
|
+
null,
|
|
195
|
+
namespaceHash("stream", cacheHash),
|
|
196
|
+
[]
|
|
197
|
+
);
|
|
198
|
+
return maybeSync(doCache)(sync);
|
|
225
199
|
}
|
|
226
|
-
del(context, cacheHash) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
data == null ? void 0 : data.caches.del(cacheHash);
|
|
231
|
-
});
|
|
200
|
+
async del(context, cacheHash) {
|
|
201
|
+
const cacheKey = this.generateRequestKey(context);
|
|
202
|
+
const data = this.cache.get(cacheKey);
|
|
203
|
+
data == null ? void 0 : data.caches.del(cacheHash);
|
|
232
204
|
}
|
|
233
205
|
}
|
|
234
206
|
let manager;
|
|
File without changes
|
|
@@ -1,23 +1,3 @@
|
|
|
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
1
|
import url from "url";
|
|
22
2
|
function namespaceHash(namespace, hash) {
|
|
23
3
|
return `${namespace}/${hash}`;
|
|
@@ -56,28 +36,26 @@ function cacheAddition(html, hash) {
|
|
|
56
36
|
}
|
|
57
37
|
const globalInvokeCache = /* @__PURE__ */ new Map();
|
|
58
38
|
function withCoalescedInvoke(func) {
|
|
59
|
-
return function(key, args) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
throw err;
|
|
77
|
-
});
|
|
78
|
-
globalInvokeCache.set(key, future);
|
|
79
|
-
return future;
|
|
39
|
+
return async function(key, args) {
|
|
40
|
+
const entry = globalInvokeCache.get(key);
|
|
41
|
+
if (entry) {
|
|
42
|
+
return entry.then((res) => ({
|
|
43
|
+
isOrigin: false,
|
|
44
|
+
value: res.value
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
function __wrapper() {
|
|
48
|
+
return func(...args);
|
|
49
|
+
}
|
|
50
|
+
const future = __wrapper().then((res) => {
|
|
51
|
+
globalInvokeCache.delete(key);
|
|
52
|
+
return { isOrigin: true, value: res };
|
|
53
|
+
}).catch((err) => {
|
|
54
|
+
globalInvokeCache.delete(key);
|
|
55
|
+
throw err;
|
|
80
56
|
});
|
|
57
|
+
globalInvokeCache.set(key, future);
|
|
58
|
+
return future;
|
|
81
59
|
};
|
|
82
60
|
}
|
|
83
61
|
function maybeSync(fn) {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { fs, mime } from "@modern-js/utils";
|
|
3
|
+
import { ERROR_DIGEST } from "../../constants";
|
|
4
|
+
import { handleDirectory } from "./static";
|
|
5
|
+
import { readFile } from "./reader";
|
|
6
|
+
import * as ssr from "./ssr";
|
|
7
|
+
const createRenderHandler = ({
|
|
8
|
+
distDir,
|
|
9
|
+
staticGenerate,
|
|
10
|
+
forceCSR
|
|
11
|
+
}) => async function render({
|
|
12
|
+
ctx,
|
|
13
|
+
route,
|
|
14
|
+
runner
|
|
15
|
+
}) {
|
|
16
|
+
if (ctx.resHasHandled()) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
const { entryPath, urlPath } = route;
|
|
20
|
+
const entry = path.join(distDir, entryPath);
|
|
21
|
+
if (!route.isSPA) {
|
|
22
|
+
const result = await handleDirectory(ctx, entry, urlPath);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
const templatePath = entry;
|
|
26
|
+
if (!fs.existsSync(templatePath)) {
|
|
27
|
+
throw new Error(`Could not find template file: ${templatePath}`);
|
|
28
|
+
}
|
|
29
|
+
const content = await readFile(templatePath);
|
|
30
|
+
if (!content) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const useCSR = forceCSR && ctx.query.csr;
|
|
34
|
+
if (route.isSSR && !useCSR) {
|
|
35
|
+
try {
|
|
36
|
+
const result = await ssr.render(
|
|
37
|
+
ctx,
|
|
38
|
+
{
|
|
39
|
+
distDir,
|
|
40
|
+
entryName: route.entryName,
|
|
41
|
+
urlPath: route.urlPath,
|
|
42
|
+
bundle: route.bundle,
|
|
43
|
+
template: content.toString(),
|
|
44
|
+
staticGenerate
|
|
45
|
+
},
|
|
46
|
+
runner
|
|
47
|
+
);
|
|
48
|
+
return result;
|
|
49
|
+
} catch (err) {
|
|
50
|
+
ctx.error(
|
|
51
|
+
ERROR_DIGEST.ERENDER,
|
|
52
|
+
err.stack || err.message
|
|
53
|
+
);
|
|
54
|
+
ctx.res.setHeader("x-modern-ssr-fallback", "1");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
content,
|
|
59
|
+
contentType: mime.contentType(path.extname(templatePath))
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
export {
|
|
63
|
+
createRenderHandler
|
|
64
|
+
};
|
|
@@ -1,37 +1,20 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
1
|
import { headersWithoutCookie } from "../../utils";
|
|
21
2
|
const createMetrics = (context, metrics) => {
|
|
22
3
|
const { entryName: entry, request } = context;
|
|
23
4
|
const { pathname = "" } = request || {};
|
|
24
5
|
const emitTimer = (name, cost, tags = {}) => {
|
|
25
|
-
metrics.emitTimer(name, cost,
|
|
6
|
+
metrics.emitTimer(name, cost, {
|
|
7
|
+
...tags,
|
|
26
8
|
pathname,
|
|
27
9
|
entry
|
|
28
|
-
})
|
|
10
|
+
});
|
|
29
11
|
};
|
|
30
12
|
const emitCounter = (name, counter, tags = {}) => {
|
|
31
|
-
metrics.emitCounter(name, counter,
|
|
13
|
+
metrics.emitCounter(name, counter, {
|
|
14
|
+
...tags,
|
|
32
15
|
pathname,
|
|
33
16
|
entry
|
|
34
|
-
})
|
|
17
|
+
});
|
|
35
18
|
};
|
|
36
19
|
return { emitTimer, emitCounter };
|
|
37
20
|
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { fs } from "@modern-js/utils";
|
|
2
|
+
import LRU from "lru-cache";
|
|
3
|
+
const Byte = 1;
|
|
4
|
+
const KB = 1024 * Byte;
|
|
5
|
+
const MB = 1024 * KB;
|
|
6
|
+
const getContentLength = (cache) => cache.content.length;
|
|
7
|
+
const createCacheItem = async (filepath, mtime) => {
|
|
8
|
+
const content = await fs.readFile(filepath);
|
|
9
|
+
return {
|
|
10
|
+
content,
|
|
11
|
+
mtime
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
class LruReader {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.cache = new LRU({
|
|
17
|
+
max: 256 * MB,
|
|
18
|
+
length: getContentLength,
|
|
19
|
+
maxAge: 5 * 60 * 5e3
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
init() {
|
|
23
|
+
}
|
|
24
|
+
close() {
|
|
25
|
+
}
|
|
26
|
+
async read(filepath) {
|
|
27
|
+
if (this.cache.has(filepath)) {
|
|
28
|
+
const { content } = this.cache.get(filepath);
|
|
29
|
+
return { content };
|
|
30
|
+
}
|
|
31
|
+
if (!fs.existsSync(filepath)) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const stat = fs.statSync(filepath);
|
|
35
|
+
if (stat.isDirectory()) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
if (stat.size > 20 * MB) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const item = await createCacheItem(filepath, stat.mtime);
|
|
42
|
+
this.cache.set(filepath, item);
|
|
43
|
+
return item;
|
|
44
|
+
}
|
|
45
|
+
update() {
|
|
46
|
+
const { cache } = this;
|
|
47
|
+
const files = cache.keys();
|
|
48
|
+
for (const filepath of files) {
|
|
49
|
+
if (!fs.existsSync(filepath)) {
|
|
50
|
+
cache.del(filepath);
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const item = cache.get(filepath);
|
|
54
|
+
const stat = fs.statSync(filepath);
|
|
55
|
+
const { mtime } = stat;
|
|
56
|
+
if (item.mtime < mtime) {
|
|
57
|
+
cache.del(filepath);
|
|
58
|
+
}
|
|
59
|
+
} catch (e) {
|
|
60
|
+
cache.del(filepath);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const reader = new LruReader();
|
|
66
|
+
const readFile = async (filepath) => {
|
|
67
|
+
const file = await reader.read(filepath);
|
|
68
|
+
return file == null ? void 0 : file.content;
|
|
69
|
+
};
|
|
70
|
+
const updateFile = () => {
|
|
71
|
+
reader.update();
|
|
72
|
+
};
|
|
73
|
+
const init = () => {
|
|
74
|
+
reader.init();
|
|
75
|
+
};
|
|
76
|
+
const close = () => {
|
|
77
|
+
reader.close();
|
|
78
|
+
};
|
|
79
|
+
export {
|
|
80
|
+
LruReader,
|
|
81
|
+
close,
|
|
82
|
+
init,
|
|
83
|
+
readFile,
|
|
84
|
+
updateFile
|
|
85
|
+
};
|
|
@@ -1,23 +1,3 @@
|
|
|
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
1
|
import path from "path";
|
|
22
2
|
import {
|
|
23
3
|
fs,
|
|
@@ -29,7 +9,7 @@ import {
|
|
|
29
9
|
import cookie from "cookie";
|
|
30
10
|
import cache from "./cache";
|
|
31
11
|
import { createLogger, createMetrics } from "./measure";
|
|
32
|
-
const render = (ctx, renderOptions, runner) =>
|
|
12
|
+
const render = async (ctx, renderOptions, runner) => {
|
|
33
13
|
var _a;
|
|
34
14
|
const { urlPath, bundle, distDir, template, entryName, staticGenerate } = renderOptions;
|
|
35
15
|
const bundleJS = path.join(distDir, bundle);
|
|
@@ -72,7 +52,7 @@ const render = (ctx, renderOptions, runner) => __async(void 0, null, function* (
|
|
|
72
52
|
context.metrics = createMetrics(context, ctx.metrics);
|
|
73
53
|
runner.extendSSRContext(context);
|
|
74
54
|
const serverRender = require(bundleJS)[SERVER_RENDER_FUNCTION_NAME];
|
|
75
|
-
const content =
|
|
55
|
+
const content = await cache(serverRender, ctx)(context);
|
|
76
56
|
const { url, status = 302 } = context.redirection;
|
|
77
57
|
if (url) {
|
|
78
58
|
return {
|
|
@@ -94,7 +74,7 @@ const render = (ctx, renderOptions, runner) => __async(void 0, null, function* (
|
|
|
94
74
|
contentType: mime.contentType("html")
|
|
95
75
|
};
|
|
96
76
|
}
|
|
97
|
-
}
|
|
77
|
+
};
|
|
98
78
|
export {
|
|
99
79
|
render
|
|
100
80
|
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { mime } from "@modern-js/utils";
|
|
3
|
+
import { readFile } from "./reader";
|
|
4
|
+
async function handleDirectory(ctx, entryPath, urlPath) {
|
|
5
|
+
const { path: pathname } = ctx;
|
|
6
|
+
const filepath = path.join(entryPath, trimLeft(pathname, urlPath));
|
|
7
|
+
let content = await readFile(filepath);
|
|
8
|
+
let contentType = mime.contentType(path.extname(filepath) || "");
|
|
9
|
+
if (!content) {
|
|
10
|
+
if (pathname.endsWith("/")) {
|
|
11
|
+
content = await readFile(`${filepath}index.html`);
|
|
12
|
+
} else if (!pathname.includes(".")) {
|
|
13
|
+
content = await readFile(`${filepath}.html`);
|
|
14
|
+
if (!content) {
|
|
15
|
+
content = await readFile(`${filepath}/index.html`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
if (content) {
|
|
19
|
+
contentType = mime.contentType("html");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (!content) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
content,
|
|
27
|
+
contentType: contentType || ""
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const trimLeft = (str, prefix) => {
|
|
31
|
+
if (str.startsWith(prefix)) {
|
|
32
|
+
return str.substring(prefix.length);
|
|
33
|
+
}
|
|
34
|
+
return str;
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
handleDirectory
|
|
38
|
+
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,23 +1,3 @@
|
|
|
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
1
|
import serve from "serve-static";
|
|
22
2
|
import { isString, isRegExp } from "@modern-js/utils";
|
|
23
3
|
import { useLocalPrefix } from "../utils";
|
|
@@ -40,7 +20,7 @@ const faviconFallbackHandler = (context, next) => {
|
|
|
40
20
|
next();
|
|
41
21
|
}
|
|
42
22
|
};
|
|
43
|
-
const createStaticFileHandler = (rules, output = {}) => (context, next) =>
|
|
23
|
+
const createStaticFileHandler = (rules, output = {}) => async (context, next) => {
|
|
44
24
|
const { url: requestUrl, req, res } = context;
|
|
45
25
|
const { assetPrefix = "/" } = output;
|
|
46
26
|
const hitRule = rules.find((item) => {
|
|
@@ -60,7 +40,7 @@ const createStaticFileHandler = (rules, output = {}) => (context, next) => __asy
|
|
|
60
40
|
} else {
|
|
61
41
|
return next();
|
|
62
42
|
}
|
|
63
|
-
}
|
|
43
|
+
};
|
|
64
44
|
export {
|
|
65
45
|
createStaticFileHandler,
|
|
66
46
|
faviconFallbackHandler
|