@modern-js/prod-server 2.0.0-beta.2 → 2.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +132 -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 -419
- 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 -443
- 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 -911
- 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,40 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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));
|
|
6
20
|
import { headersWithoutCookie } from "../../utils";
|
|
7
|
-
|
|
8
|
-
const {
|
|
9
|
-
|
|
10
|
-
request
|
|
11
|
-
} = context;
|
|
12
|
-
const {
|
|
13
|
-
pathname = ''
|
|
14
|
-
} = request || {};
|
|
21
|
+
const createMetrics = (context, metrics) => {
|
|
22
|
+
const { entryName: entry, request } = context;
|
|
23
|
+
const { pathname = "" } = request || {};
|
|
15
24
|
const emitTimer = (name, cost, tags = {}) => {
|
|
16
|
-
metrics.emitTimer(name, cost,
|
|
25
|
+
metrics.emitTimer(name, cost, __spreadProps(__spreadValues({}, tags), {
|
|
17
26
|
pathname,
|
|
18
27
|
entry
|
|
19
28
|
}));
|
|
20
29
|
};
|
|
21
30
|
const emitCounter = (name, counter, tags = {}) => {
|
|
22
|
-
metrics.emitCounter(name, counter,
|
|
31
|
+
metrics.emitCounter(name, counter, __spreadProps(__spreadValues({}, tags), {
|
|
23
32
|
pathname,
|
|
24
33
|
entry
|
|
25
34
|
}));
|
|
26
35
|
};
|
|
27
|
-
return {
|
|
28
|
-
emitTimer,
|
|
29
|
-
emitCounter
|
|
30
|
-
};
|
|
36
|
+
return { emitTimer, emitCounter };
|
|
31
37
|
};
|
|
32
|
-
|
|
38
|
+
const createLogger = (serverContext, logger) => {
|
|
33
39
|
const request = serverContext.request || {};
|
|
34
|
-
const {
|
|
35
|
-
headers = {},
|
|
36
|
-
pathname = ''
|
|
37
|
-
} = request;
|
|
40
|
+
const { headers = {}, pathname = "" } = request;
|
|
38
41
|
const debug = (message, ...args) => {
|
|
39
42
|
logger.debug(`SSR Debug - ${message}, req.url = %s`, ...args, pathname);
|
|
40
43
|
};
|
|
@@ -44,9 +47,14 @@ export const createLogger = (serverContext, logger) => {
|
|
|
44
47
|
const error = (message, e) => {
|
|
45
48
|
if (!e) {
|
|
46
49
|
e = message;
|
|
47
|
-
message =
|
|
50
|
+
message = "";
|
|
48
51
|
}
|
|
49
|
-
logger.error(
|
|
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
|
+
);
|
|
50
58
|
};
|
|
51
59
|
return {
|
|
52
60
|
error,
|
|
@@ -54,4 +62,7 @@ export const createLogger = (serverContext, logger) => {
|
|
|
54
62
|
debug
|
|
55
63
|
};
|
|
56
64
|
};
|
|
57
|
-
|
|
65
|
+
export {
|
|
66
|
+
createLogger,
|
|
67
|
+
createMetrics
|
|
68
|
+
};
|
|
@@ -1,66 +1,71 @@
|
|
|
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 { fs } from "@modern-js/utils";
|
|
22
|
+
import LRU from "lru-cache";
|
|
4
23
|
const Byte = 1;
|
|
5
24
|
const KB = 1024 * Byte;
|
|
6
25
|
const MB = 1024 * KB;
|
|
7
|
-
const getContentLength = cache => cache.content.length;
|
|
8
|
-
const createCacheItem =
|
|
9
|
-
const content =
|
|
26
|
+
const getContentLength = (cache) => cache.content.length;
|
|
27
|
+
const createCacheItem = (filepath, mtime) => __async(void 0, null, function* () {
|
|
28
|
+
const content = yield fs.readFile(filepath);
|
|
10
29
|
return {
|
|
11
30
|
content,
|
|
12
31
|
mtime
|
|
13
32
|
};
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// private timer?: NodeJS.Timeout;
|
|
17
|
-
|
|
33
|
+
});
|
|
34
|
+
class LruReader {
|
|
18
35
|
constructor() {
|
|
19
|
-
_defineProperty(this, "cache", void 0);
|
|
20
36
|
this.cache = new LRU({
|
|
21
37
|
max: 256 * MB,
|
|
22
38
|
length: getContentLength,
|
|
23
|
-
maxAge: 5 * 60 *
|
|
39
|
+
maxAge: 5 * 60 * 5e3
|
|
24
40
|
});
|
|
25
41
|
}
|
|
26
|
-
|
|
27
42
|
init() {
|
|
28
|
-
// this.timeTask();
|
|
29
43
|
}
|
|
30
44
|
close() {
|
|
31
|
-
// if (this.timer) {
|
|
32
|
-
// clearInterval(this.timer);
|
|
33
|
-
// }
|
|
34
45
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
content
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
const item = await createCacheItem(filepath, stat.mtime);
|
|
57
|
-
this.cache.set(filepath, item);
|
|
58
|
-
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
|
+
});
|
|
59
66
|
}
|
|
60
67
|
update() {
|
|
61
|
-
const {
|
|
62
|
-
cache
|
|
63
|
-
} = this;
|
|
68
|
+
const { cache } = this;
|
|
64
69
|
const files = cache.keys();
|
|
65
70
|
for (const filepath of files) {
|
|
66
71
|
if (!fs.existsSync(filepath)) {
|
|
@@ -69,36 +74,34 @@ export class LruReader {
|
|
|
69
74
|
try {
|
|
70
75
|
const item = cache.get(filepath);
|
|
71
76
|
const stat = fs.statSync(filepath);
|
|
72
|
-
const {
|
|
73
|
-
mtime
|
|
74
|
-
} = stat;
|
|
75
|
-
// file is modify
|
|
77
|
+
const { mtime } = stat;
|
|
76
78
|
if (item.mtime < mtime) {
|
|
77
79
|
cache.del(filepath);
|
|
78
80
|
}
|
|
79
81
|
} catch (e) {
|
|
80
|
-
// for safe
|
|
81
82
|
cache.del(filepath);
|
|
82
83
|
}
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
|
-
|
|
86
|
-
// private timeTask() {
|
|
87
|
-
// this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
|
|
88
|
-
// }
|
|
89
86
|
}
|
|
90
|
-
|
|
91
87
|
const reader = new LruReader();
|
|
92
|
-
|
|
93
|
-
const file =
|
|
94
|
-
return file
|
|
95
|
-
};
|
|
96
|
-
|
|
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 = () => {
|
|
97
93
|
reader.update();
|
|
98
94
|
};
|
|
99
|
-
|
|
95
|
+
const init = () => {
|
|
100
96
|
reader.init();
|
|
101
97
|
};
|
|
102
|
-
|
|
98
|
+
const close = () => {
|
|
103
99
|
reader.close();
|
|
104
|
-
};
|
|
100
|
+
};
|
|
101
|
+
export {
|
|
102
|
+
LruReader,
|
|
103
|
+
close,
|
|
104
|
+
init,
|
|
105
|
+
readFile,
|
|
106
|
+
updateFile
|
|
107
|
+
};
|
|
@@ -1,23 +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
|
-
var
|
|
8
|
-
const {
|
|
9
|
-
urlPath,
|
|
10
|
-
bundle,
|
|
11
|
-
distDir,
|
|
12
|
-
template,
|
|
13
|
-
entryName,
|
|
14
|
-
staticGenerate
|
|
15
|
-
} = renderOptions;
|
|
32
|
+
const render = (ctx, renderOptions, runner) => __async(void 0, null, function* () {
|
|
33
|
+
var _a;
|
|
34
|
+
const { urlPath, bundle, distDir, template, entryName, staticGenerate } = renderOptions;
|
|
16
35
|
const bundleJS = path.join(distDir, bundle);
|
|
17
36
|
const loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
|
|
18
|
-
const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) :
|
|
37
|
+
const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
|
|
19
38
|
const routesManifestUri = path.join(distDir, ROUTE_MINIFEST_FILE);
|
|
20
|
-
const routeManifest = fs.existsSync(routesManifestUri) ? require(routesManifestUri) :
|
|
39
|
+
const routeManifest = fs.existsSync(routesManifestUri) ? require(routesManifestUri) : void 0;
|
|
21
40
|
const context = {
|
|
22
41
|
request: {
|
|
23
42
|
baseUrl: urlPath,
|
|
@@ -26,27 +45,26 @@ export const render = async (ctx, renderOptions, runner) => {
|
|
|
26
45
|
host: ctx.host,
|
|
27
46
|
query: ctx.query,
|
|
28
47
|
url: ctx.href,
|
|
29
|
-
cookieMap: cookie.parse(ctx.headers.cookie ||
|
|
48
|
+
cookieMap: cookie.parse(ctx.headers.cookie || ""),
|
|
30
49
|
headers: ctx.headers
|
|
31
50
|
},
|
|
32
51
|
response: {
|
|
33
52
|
setHeader: (key, value) => {
|
|
34
53
|
return ctx.res.setHeader(key, value);
|
|
35
54
|
},
|
|
36
|
-
status: code => {
|
|
55
|
+
status: (code) => {
|
|
37
56
|
ctx.res.statusCode = code;
|
|
38
57
|
},
|
|
39
|
-
locals: ((
|
|
58
|
+
locals: ((_a = ctx.res) == null ? void 0 : _a.locals) || {}
|
|
40
59
|
},
|
|
41
60
|
redirection: {},
|
|
42
61
|
template,
|
|
43
62
|
loadableStats,
|
|
44
63
|
routeManifest,
|
|
45
|
-
// for streaming ssr
|
|
46
64
|
entryName,
|
|
47
65
|
staticGenerate,
|
|
48
|
-
logger:
|
|
49
|
-
metrics:
|
|
66
|
+
logger: void 0,
|
|
67
|
+
metrics: void 0,
|
|
50
68
|
req: ctx.req,
|
|
51
69
|
res: ctx.res
|
|
52
70
|
};
|
|
@@ -54,29 +72,29 @@ export const render = async (ctx, renderOptions, runner) => {
|
|
|
54
72
|
context.metrics = createMetrics(context, ctx.metrics);
|
|
55
73
|
runner.extendSSRContext(context);
|
|
56
74
|
const serverRender = require(bundleJS)[SERVER_RENDER_FUNCTION_NAME];
|
|
57
|
-
const content =
|
|
58
|
-
const {
|
|
59
|
-
url,
|
|
60
|
-
status = 302
|
|
61
|
-
} = context.redirection;
|
|
75
|
+
const content = yield cache(serverRender, ctx)(context);
|
|
76
|
+
const { url, status = 302 } = context.redirection;
|
|
62
77
|
if (url) {
|
|
63
78
|
return {
|
|
64
79
|
content: url,
|
|
65
|
-
contentType:
|
|
80
|
+
contentType: "",
|
|
66
81
|
statusCode: status,
|
|
67
82
|
redirect: true
|
|
68
83
|
};
|
|
69
84
|
}
|
|
70
|
-
if (typeof content ===
|
|
85
|
+
if (typeof content === "string") {
|
|
71
86
|
return {
|
|
72
87
|
content,
|
|
73
|
-
contentType: mime.contentType(
|
|
88
|
+
contentType: mime.contentType("html")
|
|
74
89
|
};
|
|
75
90
|
} else {
|
|
76
91
|
return {
|
|
77
|
-
content:
|
|
92
|
+
content: "",
|
|
78
93
|
contentStream: content,
|
|
79
|
-
contentType: mime.contentType(
|
|
94
|
+
contentType: mime.contentType("html")
|
|
80
95
|
};
|
|
81
96
|
}
|
|
82
|
-
};
|
|
97
|
+
});
|
|
98
|
+
export {
|
|
99
|
+
render
|
|
100
|
+
};
|
|
@@ -1,43 +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
|
-
content
|
|
20
|
-
|
|
21
|
-
content = await readFile(`${filepath}/index.html`);
|
|
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");
|
|
22
41
|
}
|
|
23
42
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (content) {
|
|
27
|
-
contentType = mime.contentType('html');
|
|
43
|
+
if (!content) {
|
|
44
|
+
return null;
|
|
28
45
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
content,
|
|
35
|
-
contentType: contentType || ''
|
|
36
|
-
};
|
|
46
|
+
return {
|
|
47
|
+
content,
|
|
48
|
+
contentType: contentType || ""
|
|
49
|
+
};
|
|
50
|
+
});
|
|
37
51
|
}
|
|
38
52
|
const trimLeft = (str, prefix) => {
|
|
39
53
|
if (str.startsWith(prefix)) {
|
|
40
54
|
return str.substring(prefix.length);
|
|
41
55
|
}
|
|
42
56
|
return str;
|
|
43
|
-
};
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
handleDirectory
|
|
60
|
+
};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
})(RenderLevel ||
|
|
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
|
+
};
|
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
2
1
|
import { RouteMatcher } from "./matcher";
|
|
3
|
-
|
|
2
|
+
class RouteMatchManager {
|
|
4
3
|
constructor() {
|
|
5
|
-
|
|
6
|
-
_defineProperty(this, "specs", []);
|
|
4
|
+
this.specs = [];
|
|
7
5
|
this.matchers = [];
|
|
8
6
|
}
|
|
9
|
-
|
|
10
|
-
// get all routes matches pathname
|
|
11
7
|
filter(pathname) {
|
|
12
8
|
return this.matchers.reduce((matches, matcher) => {
|
|
13
9
|
if (matcher.matchUrlPath(pathname)) {
|
|
@@ -16,8 +12,6 @@ export class RouteMatchManager {
|
|
|
16
12
|
return matches;
|
|
17
13
|
}, []);
|
|
18
14
|
}
|
|
19
|
-
|
|
20
|
-
// get best match from a set of matches
|
|
21
15
|
best(pathname, matches) {
|
|
22
16
|
let best;
|
|
23
17
|
let matchedLen = 0;
|
|
@@ -33,8 +27,6 @@ export class RouteMatchManager {
|
|
|
33
27
|
}
|
|
34
28
|
return best;
|
|
35
29
|
}
|
|
36
|
-
|
|
37
|
-
// reset routes matcher
|
|
38
30
|
reset(specs) {
|
|
39
31
|
this.specs = specs;
|
|
40
32
|
const matchers = specs.reduce((ms, spec) => {
|
|
@@ -43,19 +35,20 @@ export class RouteMatchManager {
|
|
|
43
35
|
}, []);
|
|
44
36
|
this.matchers = matchers;
|
|
45
37
|
}
|
|
46
|
-
|
|
47
|
-
// get best match from all matcher in manager
|
|
48
38
|
match(pathname) {
|
|
49
39
|
const matches = this.filter(pathname);
|
|
50
40
|
const best = this.best(pathname, matches);
|
|
51
41
|
return best;
|
|
52
42
|
}
|
|
53
43
|
matchEntry(entryname) {
|
|
54
|
-
return this.matchers.find(matcher => matcher.matchEntry(entryname));
|
|
44
|
+
return this.matchers.find((matcher) => matcher.matchEntry(entryname));
|
|
55
45
|
}
|
|
56
46
|
getBundles() {
|
|
57
|
-
const bundles = this.specs.filter(route => route.isSSR).map(route => route.bundle);
|
|
47
|
+
const bundles = this.specs.filter((route) => route.isSSR).map((route) => route.bundle);
|
|
58
48
|
return bundles;
|
|
59
49
|
}
|
|
60
50
|
}
|
|
61
|
-
export {
|
|
51
|
+
export {
|
|
52
|
+
RouteMatchManager,
|
|
53
|
+
RouteMatcher
|
|
54
|
+
};
|
|
@@ -1,29 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
match,
|
|
3
|
+
pathToRegexp,
|
|
4
|
+
compile
|
|
5
|
+
} from "path-to-regexp";
|
|
3
6
|
import { ModernRoute } from "./route";
|
|
4
|
-
|
|
5
|
-
// avoid import @modern-js/utils
|
|
6
|
-
const removeTailSlash = s => s.replace(/\/+$/, '');
|
|
7
|
+
const removeTailSlash = (s) => s.replace(/\/+$/, "");
|
|
7
8
|
const toPath = (reg, params) => {
|
|
8
|
-
const fn = compile(reg, {
|
|
9
|
-
encode: encodeURIComponent
|
|
10
|
-
});
|
|
9
|
+
const fn = compile(reg, { encode: encodeURIComponent });
|
|
11
10
|
return fn(params);
|
|
12
11
|
};
|
|
13
|
-
|
|
14
|
-
// eslint-disable-next-line no-useless-escape
|
|
15
12
|
const regCharsDetector = /[^a-zA-Z\-_0-9\/\.]/;
|
|
16
|
-
|
|
13
|
+
class RouteMatcher {
|
|
17
14
|
constructor(spec) {
|
|
18
|
-
|
|
19
|
-
_defineProperty(this, "urlPath", '');
|
|
20
|
-
_defineProperty(this, "urlMatcher", void 0);
|
|
21
|
-
_defineProperty(this, "urlReg", void 0);
|
|
15
|
+
this.urlPath = "";
|
|
22
16
|
this.spec = spec;
|
|
23
17
|
this.setupUrlPath();
|
|
24
18
|
}
|
|
25
|
-
|
|
26
|
-
// generate modern route object
|
|
27
19
|
generate(url) {
|
|
28
20
|
const route = new ModernRoute(this.spec);
|
|
29
21
|
if (this.urlPath) {
|
|
@@ -41,30 +33,25 @@ export class RouteMatcher {
|
|
|
41
33
|
return matchResult.params;
|
|
42
34
|
}
|
|
43
35
|
}
|
|
44
|
-
|
|
45
|
-
// get match url length
|
|
46
36
|
matchLength(pathname) {
|
|
37
|
+
var _a;
|
|
47
38
|
if (!this.urlReg) {
|
|
48
39
|
return this.urlPath.length;
|
|
49
40
|
} else {
|
|
50
|
-
var _result$;
|
|
51
41
|
const result = this.urlReg.exec(pathname);
|
|
52
|
-
return (
|
|
42
|
+
return ((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.length) || null;
|
|
53
43
|
}
|
|
54
44
|
}
|
|
55
|
-
|
|
56
|
-
// if match url path
|
|
57
45
|
matchUrlPath(requestUrl) {
|
|
58
|
-
let urlWithoutSlash = requestUrl.endsWith(
|
|
59
|
-
if (urlWithoutSlash.endsWith(
|
|
46
|
+
let urlWithoutSlash = requestUrl.endsWith("/") && requestUrl !== "/" ? requestUrl.slice(0, -1) : requestUrl;
|
|
47
|
+
if (urlWithoutSlash.endsWith(".html")) {
|
|
60
48
|
urlWithoutSlash = urlWithoutSlash.slice(0, -5);
|
|
61
49
|
}
|
|
62
50
|
if (this.urlMatcher) {
|
|
63
51
|
return Boolean(this.urlMatcher(urlWithoutSlash));
|
|
64
52
|
} else {
|
|
65
53
|
if (urlWithoutSlash.startsWith(this.urlPath)) {
|
|
66
|
-
|
|
67
|
-
if (this.urlPath !== '/' && urlWithoutSlash.length > this.urlPath.length && !urlWithoutSlash.startsWith(`${this.urlPath}/`)) {
|
|
54
|
+
if (this.urlPath !== "/" && urlWithoutSlash.length > this.urlPath.length && !urlWithoutSlash.startsWith(`${this.urlPath}/`)) {
|
|
68
55
|
return false;
|
|
69
56
|
}
|
|
70
57
|
return true;
|
|
@@ -75,13 +62,9 @@ export class RouteMatcher {
|
|
|
75
62
|
matchEntry(entryName) {
|
|
76
63
|
return this.spec.entryName === entryName;
|
|
77
64
|
}
|
|
78
|
-
|
|
79
|
-
// compiler urlPath to regexp if necessary
|
|
80
65
|
setupUrlPath() {
|
|
81
|
-
const {
|
|
82
|
-
|
|
83
|
-
} = this.spec;
|
|
84
|
-
this.urlPath = urlPath === '/' ? urlPath : removeTailSlash(urlPath);
|
|
66
|
+
const { urlPath } = this.spec;
|
|
67
|
+
this.urlPath = urlPath === "/" ? urlPath : removeTailSlash(urlPath);
|
|
85
68
|
const useReg = regCharsDetector.test(urlPath);
|
|
86
69
|
if (useReg) {
|
|
87
70
|
this.urlMatcher = match(urlPath, {
|
|
@@ -93,4 +76,7 @@ export class RouteMatcher {
|
|
|
93
76
|
});
|
|
94
77
|
}
|
|
95
78
|
}
|
|
96
|
-
}
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
RouteMatcher
|
|
82
|
+
};
|