@modern-js/prod-server 1.22.1 → 2.0.0-alpha.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 +271 -17
- package/dist/js/modern/constants.js +32 -27
- package/dist/js/modern/index.js +11 -7
- package/dist/js/modern/libs/context/context.js +54 -132
- package/dist/js/modern/libs/context/index.js +5 -2
- package/dist/js/modern/libs/hook-api/index.js +134 -0
- package/dist/js/modern/libs/hook-api/route.js +13 -37
- package/dist/js/modern/libs/hook-api/template.js +41 -32
- package/dist/js/modern/libs/loadConfig.js +46 -32
- package/dist/js/modern/libs/metrics.js +6 -7
- package/dist/js/modern/libs/proxy.js +70 -44
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -68
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -225
- 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 +93 -50
- 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 -16
- package/dist/js/modern/libs/render/cache/spr.js +133 -167
- package/dist/js/modern/libs/render/cache/type.js +0 -1
- package/dist/js/modern/libs/render/cache/util.js +71 -45
- package/dist/js/modern/libs/render/index.js +76 -61
- package/dist/js/modern/libs/render/measure.js +42 -34
- package/dist/js/modern/libs/render/reader.js +68 -76
- package/dist/js/modern/libs/render/ssr.js +63 -33
- package/dist/js/modern/libs/render/static.js +51 -37
- package/dist/js/modern/libs/render/type.js +9 -7
- package/dist/js/modern/libs/route/index.js +12 -30
- package/dist/js/modern/libs/route/matcher.js +28 -50
- package/dist/js/modern/libs/route/route.js +9 -31
- package/dist/js/modern/libs/serve-file.js +40 -20
- package/dist/js/modern/server/index.js +152 -168
- package/dist/js/modern/server/modern-server-split.js +44 -60
- package/dist/js/modern/server/modern-server.js +416 -526
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +62 -58
- package/dist/js/modern/worker-server.js +54 -0
- package/dist/js/node/constants.js +53 -32
- package/dist/js/node/index.js +37 -67
- package/dist/js/node/libs/context/context.js +84 -150
- package/dist/js/node/libs/context/index.js +28 -16
- package/dist/js/node/libs/hook-api/index.js +164 -0
- package/dist/js/node/libs/hook-api/route.js +35 -45
- package/dist/js/node/libs/hook-api/template.js +64 -40
- package/dist/js/node/libs/loadConfig.js +74 -55
- package/dist/js/node/libs/metrics.js +28 -12
- package/dist/js/node/libs/proxy.js +90 -55
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -61
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -140
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
- package/dist/js/node/libs/render/cache/index.js +115 -65
- package/dist/js/node/libs/render/cache/page-caches/index.js +54 -16
- package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -26
- package/dist/js/node/libs/render/cache/spr.js +161 -188
- package/dist/js/node/libs/render/cache/type.js +15 -5
- package/dist/js/node/libs/render/cache/util.js +99 -63
- package/dist/js/node/libs/render/index.js +106 -87
- package/dist/js/node/libs/render/measure.js +61 -44
- package/dist/js/node/libs/render/reader.js +98 -100
- package/dist/js/node/libs/render/ssr.js +92 -58
- package/dist/js/node/libs/render/static.js +80 -53
- package/dist/js/node/libs/render/type.js +31 -13
- package/dist/js/node/libs/route/index.js +35 -44
- package/dist/js/node/libs/route/matcher.js +48 -65
- package/dist/js/node/libs/route/route.js +29 -37
- package/dist/js/node/libs/serve-file.js +74 -37
- package/dist/js/node/server/index.js +170 -203
- package/dist/js/node/server/modern-server-split.js +70 -73
- package/dist/js/node/server/modern-server.js +432 -577
- package/dist/js/node/type.js +15 -3
- package/dist/js/node/utils.js +87 -93
- package/dist/js/node/worker-server.js +77 -0
- package/dist/js/treeshaking/constants.js +29 -0
- package/dist/js/treeshaking/index.js +13 -0
- package/dist/js/treeshaking/libs/context/context.js +274 -0
- package/dist/js/treeshaking/libs/context/index.js +5 -0
- package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
- package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
- package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
- package/dist/js/treeshaking/libs/loadConfig.js +82 -0
- package/dist/js/treeshaking/libs/metrics.js +6 -0
- package/dist/js/treeshaking/libs/proxy.js +244 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
- package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
- package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
- package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
- package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
- package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
- package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
- package/dist/js/treeshaking/libs/render/index.js +233 -0
- package/dist/js/treeshaking/libs/render/measure.js +146 -0
- package/dist/js/treeshaking/libs/render/reader.js +339 -0
- package/dist/js/treeshaking/libs/render/ssr.js +223 -0
- package/dist/js/treeshaking/libs/render/static.js +216 -0
- package/dist/js/treeshaking/libs/render/type.js +7 -0
- package/dist/js/treeshaking/libs/route/index.js +130 -0
- package/dist/js/treeshaking/libs/route/matcher.js +138 -0
- package/dist/js/treeshaking/libs/route/route.js +40 -0
- package/dist/js/treeshaking/libs/serve-file.js +184 -0
- package/dist/js/treeshaking/server/index.js +505 -0
- package/dist/js/treeshaking/server/modern-server-split.js +360 -0
- package/dist/js/treeshaking/server/modern-server.js +1083 -0
- package/dist/js/treeshaking/type.js +1 -0
- package/dist/js/treeshaking/utils.js +147 -0
- package/dist/js/treeshaking/worker-server.js +177 -0
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/hook-api/index.d.ts +5 -0
- package/dist/types/libs/hook-api/route.d.ts +9 -14
- package/dist/types/libs/hook-api/template.d.ts +19 -9
- package/dist/types/libs/render/cache/index.d.ts +4 -2
- package/dist/types/libs/render/type.d.ts +3 -1
- package/dist/types/libs/route/route.d.ts +0 -1
- package/dist/types/libs/serve-file.d.ts +2 -1
- package/dist/types/server/index.d.ts +2 -0
- package/dist/types/server/modern-server.d.ts +11 -11
- package/dist/types/type.d.ts +8 -10
- package/dist/types/utils.d.ts +3 -4
- package/dist/types/worker-server.d.ts +15 -0
- package/package.json +31 -45
- package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
- package/dist/js/modern/libs/render/modern/index.js +0 -48
- package/dist/js/node/libs/render/modern/browser-list.js +0 -14
- package/dist/js/node/libs/render/modern/index.js +0 -64
- package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
- package/dist/types/libs/render/modern/index.d.ts +0 -3
|
@@ -1,69 +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
|
-
|
|
21
|
-
entryPath
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const entry = path.join(distDir, entryPath);
|
|
25
|
-
|
|
26
|
-
if (!route.isSPA) {
|
|
27
|
-
const result = await handleDirectory(ctx, entry, urlPath);
|
|
28
|
-
return result;
|
|
29
|
-
} // only spa can use es6-html
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const modernEntry = getModernEntry(entry);
|
|
33
|
-
const useModern = // route.enableModernMode &&
|
|
34
|
-
supportModern(ctx) && fs.existsSync(modernEntry);
|
|
35
|
-
const templatePath = useModern ? modernEntry : entry;
|
|
36
|
-
|
|
37
|
-
if (!fs.existsSync(templatePath)) {
|
|
38
|
-
throw new Error(`Could not find template file: ${templatePath}`);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const content = await readFile(templatePath);
|
|
42
|
-
|
|
43
|
-
if (!content) {
|
|
44
|
-
return null;
|
|
45
|
-
} // handles ssr first
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (route.isSSR) {
|
|
49
|
-
try {
|
|
50
|
-
const result = await ssr.render(ctx, {
|
|
51
|
-
distDir,
|
|
52
|
-
entryName: route.entryName,
|
|
53
|
-
urlPath: route.urlPath,
|
|
54
|
-
bundle: route.bundle,
|
|
55
|
-
template: content.toString(),
|
|
56
|
-
staticGenerate
|
|
57
|
-
}, 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);
|
|
58
43
|
return result;
|
|
59
|
-
} catch (err) {
|
|
60
|
-
ctx.error(ERROR_DIGEST.ERENDER, err.stack || err.message);
|
|
61
|
-
ctx.res.setHeader('x-modern-ssr-fallback', '1');
|
|
62
44
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
+
};
|
|
@@ -1,60 +1,68 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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));
|
|
7
20
|
import { headersWithoutCookie } from "../../utils";
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
|
|
11
|
-
request
|
|
12
|
-
} = context;
|
|
13
|
-
const {
|
|
14
|
-
pathname = ''
|
|
15
|
-
} = request || {};
|
|
16
|
-
|
|
21
|
+
const createMetrics = (context, metrics) => {
|
|
22
|
+
const { entryName: entry, request } = context;
|
|
23
|
+
const { pathname = "" } = request || {};
|
|
17
24
|
const emitTimer = (name, cost, tags = {}) => {
|
|
18
|
-
metrics.emitTimer(name, cost,
|
|
25
|
+
metrics.emitTimer(name, cost, __spreadProps(__spreadValues({}, tags), {
|
|
19
26
|
pathname,
|
|
20
27
|
entry
|
|
21
28
|
}));
|
|
22
29
|
};
|
|
23
|
-
|
|
24
30
|
const emitCounter = (name, counter, tags = {}) => {
|
|
25
|
-
metrics.emitCounter(name, counter,
|
|
31
|
+
metrics.emitCounter(name, counter, __spreadProps(__spreadValues({}, tags), {
|
|
26
32
|
pathname,
|
|
27
33
|
entry
|
|
28
34
|
}));
|
|
29
35
|
};
|
|
30
|
-
|
|
31
|
-
return {
|
|
32
|
-
emitTimer,
|
|
33
|
-
emitCounter
|
|
34
|
-
};
|
|
36
|
+
return { emitTimer, emitCounter };
|
|
35
37
|
};
|
|
36
|
-
|
|
38
|
+
const createLogger = (serverContext, logger) => {
|
|
37
39
|
const request = serverContext.request || {};
|
|
38
|
-
const {
|
|
39
|
-
headers = {},
|
|
40
|
-
pathname = ''
|
|
41
|
-
} = request;
|
|
42
|
-
|
|
40
|
+
const { headers = {}, pathname = "" } = request;
|
|
43
41
|
const debug = (message, ...args) => {
|
|
44
42
|
logger.debug(`SSR Debug - ${message}, req.url = %s`, ...args, pathname);
|
|
45
43
|
};
|
|
46
|
-
|
|
47
44
|
const info = (message, ...args) => {
|
|
48
45
|
logger.info(`SSR Info - ${message}, req.url = %s`, ...args, pathname);
|
|
49
46
|
};
|
|
50
|
-
|
|
51
47
|
const error = (message, e) => {
|
|
52
|
-
|
|
48
|
+
if (!e) {
|
|
49
|
+
e = message;
|
|
50
|
+
message = "";
|
|
51
|
+
}
|
|
52
|
+
logger.error(
|
|
53
|
+
`SSR Error - ${message}, error = %s, req.url = %s, req.headers = %o`,
|
|
54
|
+
e instanceof Error ? e.stack || e.message : e,
|
|
55
|
+
pathname,
|
|
56
|
+
headersWithoutCookie(headers)
|
|
57
|
+
);
|
|
53
58
|
};
|
|
54
|
-
|
|
55
59
|
return {
|
|
56
60
|
error,
|
|
57
61
|
info,
|
|
58
62
|
debug
|
|
59
63
|
};
|
|
60
|
-
};
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
createLogger,
|
|
67
|
+
createMetrics
|
|
68
|
+
};
|
|
@@ -1,115 +1,107 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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 { fs } from "@modern-js/utils";
|
|
22
|
+
import LRU from "lru-cache";
|
|
5
23
|
const Byte = 1;
|
|
6
24
|
const KB = 1024 * Byte;
|
|
7
25
|
const MB = 1024 * KB;
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const createCacheItem = async (filepath, mtime) => {
|
|
12
|
-
const content = await fs.readFile(filepath);
|
|
26
|
+
const getContentLength = (cache) => cache.content.length;
|
|
27
|
+
const createCacheItem = (filepath, mtime) => __async(void 0, null, function* () {
|
|
28
|
+
const content = yield fs.readFile(filepath);
|
|
13
29
|
return {
|
|
14
30
|
content,
|
|
15
31
|
mtime
|
|
16
32
|
};
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export class LruReader {
|
|
20
|
-
// private timer?: NodeJS.Timeout;
|
|
33
|
+
});
|
|
34
|
+
class LruReader {
|
|
21
35
|
constructor() {
|
|
22
|
-
_defineProperty(this, "cache", void 0);
|
|
23
|
-
|
|
24
36
|
this.cache = new LRU({
|
|
25
37
|
max: 256 * MB,
|
|
26
38
|
length: getContentLength,
|
|
27
|
-
maxAge: 5 * 60 *
|
|
28
|
-
|
|
39
|
+
maxAge: 5 * 60 * 5e3
|
|
29
40
|
});
|
|
30
41
|
}
|
|
31
|
-
|
|
32
|
-
init() {// this.timeTask();
|
|
42
|
+
init() {
|
|
33
43
|
}
|
|
34
|
-
|
|
35
|
-
close() {// if (this.timer) {
|
|
36
|
-
// clearInterval(this.timer);
|
|
37
|
-
// }
|
|
44
|
+
close() {
|
|
38
45
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
content
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (stat.size > 20 * MB) {
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const item = await createCacheItem(filepath, stat.mtime);
|
|
66
|
-
this.cache.set(filepath, item);
|
|
67
|
-
return item;
|
|
46
|
+
read(filepath) {
|
|
47
|
+
return __async(this, null, function* () {
|
|
48
|
+
if (this.cache.has(filepath)) {
|
|
49
|
+
const { content } = this.cache.get(filepath);
|
|
50
|
+
return { content };
|
|
51
|
+
}
|
|
52
|
+
if (!fs.existsSync(filepath)) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const stat = fs.statSync(filepath);
|
|
56
|
+
if (stat.isDirectory()) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
if (stat.size > 20 * MB) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const item = yield createCacheItem(filepath, stat.mtime);
|
|
63
|
+
this.cache.set(filepath, item);
|
|
64
|
+
return item;
|
|
65
|
+
});
|
|
68
66
|
}
|
|
69
|
-
|
|
70
67
|
update() {
|
|
71
|
-
const {
|
|
72
|
-
cache
|
|
73
|
-
} = this;
|
|
68
|
+
const { cache } = this;
|
|
74
69
|
const files = cache.keys();
|
|
75
|
-
|
|
76
70
|
for (const filepath of files) {
|
|
77
71
|
if (!fs.existsSync(filepath)) {
|
|
78
72
|
cache.del(filepath);
|
|
79
73
|
}
|
|
80
|
-
|
|
81
74
|
try {
|
|
82
75
|
const item = cache.get(filepath);
|
|
83
76
|
const stat = fs.statSync(filepath);
|
|
84
|
-
const {
|
|
85
|
-
mtime
|
|
86
|
-
} = stat; // file is modify
|
|
87
|
-
|
|
77
|
+
const { mtime } = stat;
|
|
88
78
|
if (item.mtime < mtime) {
|
|
89
79
|
cache.del(filepath);
|
|
90
80
|
}
|
|
91
81
|
} catch (e) {
|
|
92
|
-
// for safe
|
|
93
82
|
cache.del(filepath);
|
|
94
83
|
}
|
|
95
84
|
}
|
|
96
|
-
}
|
|
97
|
-
// this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
|
|
98
|
-
// }
|
|
99
|
-
|
|
100
|
-
|
|
85
|
+
}
|
|
101
86
|
}
|
|
102
87
|
const reader = new LruReader();
|
|
103
|
-
|
|
104
|
-
const file =
|
|
105
|
-
return file
|
|
106
|
-
};
|
|
107
|
-
|
|
88
|
+
const readFile = (filepath) => __async(void 0, null, function* () {
|
|
89
|
+
const file = yield reader.read(filepath);
|
|
90
|
+
return file == null ? void 0 : file.content;
|
|
91
|
+
});
|
|
92
|
+
const updateFile = () => {
|
|
108
93
|
reader.update();
|
|
109
94
|
};
|
|
110
|
-
|
|
95
|
+
const init = () => {
|
|
111
96
|
reader.init();
|
|
112
97
|
};
|
|
113
|
-
|
|
98
|
+
const close = () => {
|
|
114
99
|
reader.close();
|
|
115
|
-
};
|
|
100
|
+
};
|
|
101
|
+
export {
|
|
102
|
+
LruReader,
|
|
103
|
+
close,
|
|
104
|
+
init,
|
|
105
|
+
readFile,
|
|
106
|
+
updateFile
|
|
107
|
+
};
|
|
@@ -1,20 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
+
import path from "path";
|
|
22
|
+
import {
|
|
23
|
+
fs,
|
|
24
|
+
LOADABLE_STATS_FILE,
|
|
25
|
+
mime,
|
|
26
|
+
ROUTE_MINIFEST_FILE,
|
|
27
|
+
SERVER_RENDER_FUNCTION_NAME
|
|
28
|
+
} from "@modern-js/utils";
|
|
29
|
+
import cookie from "cookie";
|
|
4
30
|
import cache from "./cache";
|
|
5
31
|
import { createLogger, createMetrics } from "./measure";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
bundle,
|
|
10
|
-
distDir,
|
|
11
|
-
template,
|
|
12
|
-
entryName,
|
|
13
|
-
staticGenerate
|
|
14
|
-
} = renderOptions;
|
|
32
|
+
const render = (ctx, renderOptions, runner) => __async(void 0, null, function* () {
|
|
33
|
+
var _a;
|
|
34
|
+
const { urlPath, bundle, distDir, template, entryName, staticGenerate } = renderOptions;
|
|
15
35
|
const bundleJS = path.join(distDir, bundle);
|
|
16
36
|
const loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
|
|
17
|
-
const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) :
|
|
37
|
+
const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
|
|
38
|
+
const routesManifestUri = path.join(distDir, ROUTE_MINIFEST_FILE);
|
|
39
|
+
const routeManifest = fs.existsSync(routesManifestUri) ? require(routesManifestUri) : void 0;
|
|
18
40
|
const context = {
|
|
19
41
|
request: {
|
|
20
42
|
baseUrl: urlPath,
|
|
@@ -23,48 +45,56 @@ export const render = async (ctx, renderOptions, runner) => {
|
|
|
23
45
|
host: ctx.host,
|
|
24
46
|
query: ctx.query,
|
|
25
47
|
url: ctx.href,
|
|
26
|
-
cookieMap: cookie.parse(ctx.headers.cookie ||
|
|
48
|
+
cookieMap: cookie.parse(ctx.headers.cookie || ""),
|
|
27
49
|
headers: ctx.headers
|
|
28
50
|
},
|
|
29
51
|
response: {
|
|
30
52
|
setHeader: (key, value) => {
|
|
31
53
|
return ctx.res.setHeader(key, value);
|
|
32
54
|
},
|
|
33
|
-
status: code => {
|
|
55
|
+
status: (code) => {
|
|
34
56
|
ctx.res.statusCode = code;
|
|
35
|
-
}
|
|
57
|
+
},
|
|
58
|
+
locals: ((_a = ctx.res) == null ? void 0 : _a.locals) || {}
|
|
36
59
|
},
|
|
37
60
|
redirection: {},
|
|
38
61
|
template,
|
|
39
62
|
loadableStats,
|
|
63
|
+
routeManifest,
|
|
40
64
|
entryName,
|
|
41
65
|
staticGenerate,
|
|
42
|
-
logger:
|
|
43
|
-
metrics:
|
|
66
|
+
logger: void 0,
|
|
67
|
+
metrics: void 0,
|
|
68
|
+
req: ctx.req,
|
|
69
|
+
res: ctx.res
|
|
44
70
|
};
|
|
45
71
|
context.logger = createLogger(context, ctx.logger);
|
|
46
72
|
context.metrics = createMetrics(context, ctx.metrics);
|
|
47
73
|
runner.extendSSRContext(context);
|
|
48
|
-
|
|
49
74
|
const serverRender = require(bundleJS)[SERVER_RENDER_FUNCTION_NAME];
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
const {
|
|
53
|
-
url,
|
|
54
|
-
status = 302
|
|
55
|
-
} = context.redirection;
|
|
56
|
-
|
|
75
|
+
const content = yield cache(serverRender, ctx)(context);
|
|
76
|
+
const { url, status = 302 } = context.redirection;
|
|
57
77
|
if (url) {
|
|
58
78
|
return {
|
|
59
79
|
content: url,
|
|
60
|
-
contentType:
|
|
80
|
+
contentType: "",
|
|
61
81
|
statusCode: status,
|
|
62
82
|
redirect: true
|
|
63
83
|
};
|
|
64
84
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
85
|
+
if (typeof content === "string") {
|
|
86
|
+
return {
|
|
87
|
+
content,
|
|
88
|
+
contentType: mime.contentType("html")
|
|
89
|
+
};
|
|
90
|
+
} else {
|
|
91
|
+
return {
|
|
92
|
+
content: "",
|
|
93
|
+
contentStream: content,
|
|
94
|
+
contentType: mime.contentType("html")
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
export {
|
|
99
|
+
render
|
|
100
|
+
};
|
|
@@ -1,46 +1,60 @@
|
|
|
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 { mime } from "@modern-js/utils";
|
|
3
23
|
import { readFile } from "./reader";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
path: pathname
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (
|
|
20
|
-
|
|
24
|
+
function handleDirectory(ctx, entryPath, urlPath) {
|
|
25
|
+
return __async(this, null, function* () {
|
|
26
|
+
const { path: pathname } = ctx;
|
|
27
|
+
const filepath = path.join(entryPath, trimLeft(pathname, urlPath));
|
|
28
|
+
let content = yield readFile(filepath);
|
|
29
|
+
let contentType = mime.contentType(path.extname(filepath) || "");
|
|
30
|
+
if (!content) {
|
|
31
|
+
if (pathname.endsWith("/")) {
|
|
32
|
+
content = yield readFile(`${filepath}index.html`);
|
|
33
|
+
} else if (!pathname.includes(".")) {
|
|
34
|
+
content = yield readFile(`${filepath}.html`);
|
|
35
|
+
if (!content) {
|
|
36
|
+
content = yield readFile(`${filepath}/index.html`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (content) {
|
|
40
|
+
contentType = mime.contentType("html");
|
|
21
41
|
}
|
|
22
|
-
} // set content-type as html
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (content) {
|
|
26
|
-
contentType = mime.contentType('html');
|
|
27
42
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
contentType: contentType || ''
|
|
37
|
-
};
|
|
43
|
+
if (!content) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
content,
|
|
48
|
+
contentType: contentType || ""
|
|
49
|
+
};
|
|
50
|
+
});
|
|
38
51
|
}
|
|
39
|
-
|
|
40
52
|
const trimLeft = (str, prefix) => {
|
|
41
53
|
if (str.startsWith(prefix)) {
|
|
42
54
|
return str.substring(prefix.length);
|
|
43
55
|
}
|
|
44
|
-
|
|
45
56
|
return str;
|
|
46
|
-
};
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
handleDirectory
|
|
60
|
+
};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
var RenderLevel = /* @__PURE__ */ ((RenderLevel2) => {
|
|
2
|
+
RenderLevel2[RenderLevel2["CLIENT_RENDER"] = 0] = "CLIENT_RENDER";
|
|
3
|
+
RenderLevel2[RenderLevel2["SERVER_PREFETCH"] = 1] = "SERVER_PREFETCH";
|
|
4
|
+
RenderLevel2[RenderLevel2["SERVER_RENDER"] = 2] = "SERVER_RENDER";
|
|
5
|
+
return RenderLevel2;
|
|
6
|
+
})(RenderLevel || {});
|
|
7
|
+
export {
|
|
8
|
+
RenderLevel
|
|
9
|
+
};
|