@modern-js/prod-server 2.15.0 → 2.16.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 +14 -0
- package/dist/cjs/constants.js +15 -31
- package/dist/cjs/index.js +35 -37
- package/dist/cjs/libs/context/context.js +53 -66
- package/dist/cjs/libs/context/index.js +15 -28
- package/dist/cjs/libs/hook-api/index.js +59 -54
- package/dist/cjs/libs/hook-api/route.js +28 -30
- package/dist/cjs/libs/hook-api/template.js +30 -34
- package/dist/cjs/libs/loadConfig.js +74 -65
- package/dist/cjs/libs/logger.js +46 -37
- package/dist/cjs/libs/metrics.js +7 -25
- package/dist/cjs/libs/proxy.js +29 -46
- package/dist/cjs/libs/render/cache/__tests__/cache.fun.test.js +48 -64
- package/dist/cjs/libs/render/cache/__tests__/cache.test.js +53 -37
- package/dist/cjs/libs/render/cache/__tests__/cacheable.js +38 -34
- package/dist/cjs/libs/render/cache/__tests__/error-configuration.js +22 -30
- package/dist/cjs/libs/render/cache/__tests__/matched-cache.js +86 -46
- package/dist/cjs/libs/render/cache/index.js +17 -37
- package/dist/cjs/libs/render/cache/page-caches/index.js +12 -28
- package/dist/cjs/libs/render/cache/page-caches/lru.js +32 -40
- package/dist/cjs/libs/render/cache/spr.js +104 -111
- package/dist/cjs/libs/render/cache/type.js +4 -15
- package/dist/cjs/libs/render/cache/util.js +29 -53
- package/dist/cjs/libs/render/index.js +75 -73
- package/dist/cjs/libs/render/measure.js +19 -34
- package/dist/cjs/libs/render/reader.js +52 -61
- package/dist/cjs/libs/render/ssr.js +34 -66
- package/dist/cjs/libs/render/static.js +22 -45
- package/dist/cjs/libs/render/type.js +10 -28
- package/dist/cjs/libs/render/utils.js +17 -38
- package/dist/cjs/libs/route/index.js +32 -31
- package/dist/cjs/libs/route/matcher.js +38 -38
- package/dist/cjs/libs/route/route.js +29 -26
- package/dist/cjs/libs/serveFile.js +23 -41
- package/dist/cjs/server/index.js +101 -107
- package/dist/cjs/server/modernServer.js +191 -171
- package/dist/cjs/server/modernServerSplit.js +12 -30
- package/dist/cjs/type.js +4 -15
- package/dist/cjs/utils.js +50 -50
- package/dist/cjs/workerServer.js +26 -36
- package/dist/esm/constants.js +24 -25
- package/dist/esm/index.js +11 -10
- package/dist/esm/libs/context/context.js +271 -264
- package/dist/esm/libs/context/index.js +3 -3
- package/dist/esm/libs/hook-api/index.js +237 -223
- package/dist/esm/libs/hook-api/route.js +62 -60
- package/dist/esm/libs/hook-api/template.js +117 -115
- package/dist/esm/libs/loadConfig.js +68 -69
- package/dist/esm/libs/logger.js +188 -174
- package/dist/esm/libs/metrics.js +6 -3
- package/dist/esm/libs/proxy.js +236 -221
- package/dist/esm/libs/render/cache/__tests__/cache.fun.test.js +286 -267
- package/dist/esm/libs/render/cache/__tests__/cache.test.js +765 -745
- package/dist/esm/libs/render/cache/__tests__/cacheable.js +62 -63
- package/dist/esm/libs/render/cache/__tests__/error-configuration.js +42 -43
- package/dist/esm/libs/render/cache/__tests__/matched-cache.js +134 -135
- package/dist/esm/libs/render/cache/index.js +337 -326
- package/dist/esm/libs/render/cache/page-caches/index.js +147 -141
- package/dist/esm/libs/render/cache/page-caches/lru.js +78 -76
- package/dist/esm/libs/render/cache/spr.js +465 -456
- package/dist/esm/libs/render/cache/type.js +1 -1
- package/dist/esm/libs/render/cache/util.js +266 -246
- package/dist/esm/libs/render/index.js +226 -218
- package/dist/esm/libs/render/measure.js +136 -128
- package/dist/esm/libs/render/reader.js +315 -304
- package/dist/esm/libs/render/ssr.js +214 -209
- package/dist/esm/libs/render/static.js +208 -198
- package/dist/esm/libs/render/type.js +6 -7
- package/dist/esm/libs/render/utils.js +8 -9
- package/dist/esm/libs/route/index.js +134 -123
- package/dist/esm/libs/route/matcher.js +140 -130
- package/dist/esm/libs/route/route.js +36 -37
- package/dist/esm/libs/serveFile.js +177 -168
- package/dist/esm/server/index.js +578 -549
- package/dist/esm/server/modernServer.js +1137 -1074
- package/dist/esm/server/modernServerSplit.js +344 -328
- package/dist/esm/type.js +1 -1
- package/dist/esm/utils.js +133 -124
- package/dist/esm/workerServer.js +226 -220
- package/dist/esm-node/constants.js +7 -14
- package/dist/esm-node/index.js +4 -9
- package/dist/esm-node/libs/context/context.js +32 -25
- package/dist/esm-node/libs/context/index.js +2 -5
- package/dist/esm-node/libs/hook-api/index.js +40 -21
- package/dist/esm-node/libs/hook-api/route.js +21 -8
- package/dist/esm-node/libs/hook-api/template.js +19 -14
- package/dist/esm-node/libs/loadConfig.js +11 -28
- package/dist/esm-node/libs/logger.js +37 -17
- package/dist/esm-node/libs/metrics.js +1 -3
- package/dist/esm-node/libs/proxy.js +14 -22
- package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +25 -36
- package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +23 -11
- package/dist/esm-node/libs/render/cache/__tests__/cacheable.js +32 -13
- package/dist/esm-node/libs/render/cache/__tests__/error-configuration.js +16 -9
- package/dist/esm-node/libs/render/cache/__tests__/matched-cache.js +80 -25
- package/dist/esm-node/libs/render/cache/index.js +3 -10
- package/dist/esm-node/libs/render/cache/page-caches/index.js +4 -5
- package/dist/esm-node/libs/render/cache/page-caches/lru.js +20 -8
- package/dist/esm-node/libs/render/cache/spr.js +79 -80
- package/dist/esm-node/libs/render/cache/type.js +1 -0
- package/dist/esm-node/libs/render/cache/util.js +13 -23
- package/dist/esm-node/libs/render/index.js +10 -28
- package/dist/esm-node/libs/render/measure.js +7 -13
- package/dist/esm-node/libs/render/reader.js +31 -26
- package/dist/esm-node/libs/render/ssr.js +5 -21
- package/dist/esm-node/libs/render/static.js +1 -4
- package/dist/esm-node/libs/render/type.js +3 -6
- package/dist/esm-node/libs/render/utils.js +6 -18
- package/dist/esm-node/libs/route/index.js +20 -9
- package/dist/esm-node/libs/route/matcher.js +28 -17
- package/dist/esm-node/libs/route/route.js +23 -5
- package/dist/esm-node/libs/serveFile.js +2 -6
- package/dist/esm-node/server/index.js +72 -75
- package/dist/esm-node/server/modernServer.js +102 -130
- package/dist/esm-node/server/modernServerSplit.js +1 -4
- package/dist/esm-node/type.js +1 -0
- package/dist/esm-node/utils.js +41 -32
- package/dist/esm-node/workerServer.js +9 -10
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/server/modernServer.d.ts +1 -1
- package/dist/types/utils.d.ts +1 -1
- package/package.json +12 -8
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
const errorConfiguration = [
|
|
2
|
-
{
|
|
1
|
+
export const errorConfiguration = [
|
|
2
|
+
{
|
|
3
|
+
level: 1
|
|
4
|
+
},
|
|
3
5
|
{
|
|
4
6
|
level: 1,
|
|
5
7
|
includes: {}
|
|
6
8
|
},
|
|
7
9
|
{
|
|
8
10
|
level: 1,
|
|
9
|
-
includes: {
|
|
11
|
+
includes: {
|
|
12
|
+
query: []
|
|
13
|
+
}
|
|
10
14
|
},
|
|
11
15
|
{
|
|
12
16
|
level: 2,
|
|
@@ -14,15 +18,21 @@ const errorConfiguration = [
|
|
|
14
18
|
},
|
|
15
19
|
{
|
|
16
20
|
level: 2,
|
|
17
|
-
includes: {
|
|
21
|
+
includes: {
|
|
22
|
+
header: []
|
|
23
|
+
}
|
|
18
24
|
},
|
|
19
25
|
{
|
|
20
26
|
level: 3,
|
|
21
|
-
includes: {
|
|
27
|
+
includes: {
|
|
28
|
+
header: []
|
|
29
|
+
}
|
|
22
30
|
},
|
|
23
31
|
{
|
|
24
32
|
level: 3,
|
|
25
|
-
includes: {
|
|
33
|
+
includes: {
|
|
34
|
+
query: []
|
|
35
|
+
}
|
|
26
36
|
},
|
|
27
37
|
{
|
|
28
38
|
level: 3,
|
|
@@ -32,6 +42,3 @@ const errorConfiguration = [
|
|
|
32
42
|
}
|
|
33
43
|
}
|
|
34
44
|
];
|
|
35
|
-
export {
|
|
36
|
-
errorConfiguration
|
|
37
|
-
};
|
|
@@ -1,27 +1,43 @@
|
|
|
1
|
-
const matchedCacheableAry = [
|
|
1
|
+
export const matchedCacheableAry = [
|
|
2
2
|
[
|
|
3
3
|
{
|
|
4
4
|
requestOpt: {
|
|
5
5
|
url: "/level-one",
|
|
6
6
|
headers: {},
|
|
7
|
-
query: {
|
|
7
|
+
query: {
|
|
8
|
+
name: "byted"
|
|
9
|
+
}
|
|
8
10
|
},
|
|
9
11
|
cacheConfig: {
|
|
10
12
|
level: 1,
|
|
11
|
-
includes: {
|
|
12
|
-
|
|
13
|
+
includes: {
|
|
14
|
+
query: [
|
|
15
|
+
"name"
|
|
16
|
+
]
|
|
17
|
+
},
|
|
18
|
+
matches: {
|
|
19
|
+
query: {
|
|
20
|
+
name: {
|
|
21
|
+
weixin: "^byted"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
13
25
|
},
|
|
14
26
|
content: "level1"
|
|
15
27
|
},
|
|
16
28
|
{
|
|
17
29
|
url: "/level-one",
|
|
18
30
|
headers: {},
|
|
19
|
-
query: {
|
|
31
|
+
query: {
|
|
32
|
+
name: "byted_likely"
|
|
33
|
+
}
|
|
20
34
|
},
|
|
21
35
|
{
|
|
22
36
|
url: "/level-one",
|
|
23
37
|
headers: {},
|
|
24
|
-
query: {
|
|
38
|
+
query: {
|
|
39
|
+
name: "not_byted"
|
|
40
|
+
}
|
|
25
41
|
}
|
|
26
42
|
],
|
|
27
43
|
[
|
|
@@ -29,63 +45,102 @@ const matchedCacheableAry = [
|
|
|
29
45
|
requestOpt: {
|
|
30
46
|
url: "/level-two",
|
|
31
47
|
query: {},
|
|
32
|
-
headers: {
|
|
48
|
+
headers: {
|
|
49
|
+
age: "17"
|
|
50
|
+
}
|
|
33
51
|
},
|
|
34
52
|
cacheConfig: {
|
|
35
53
|
level: 2,
|
|
36
|
-
includes: {
|
|
37
|
-
|
|
54
|
+
includes: {
|
|
55
|
+
header: [
|
|
56
|
+
"age"
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
matches: {
|
|
60
|
+
header: {
|
|
61
|
+
age: {
|
|
62
|
+
one: "^1"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
38
66
|
},
|
|
39
67
|
content: "level2"
|
|
40
68
|
},
|
|
41
69
|
{
|
|
42
70
|
url: "/level-two",
|
|
43
71
|
query: {},
|
|
44
|
-
headers: {
|
|
72
|
+
headers: {
|
|
73
|
+
age: "11"
|
|
74
|
+
}
|
|
45
75
|
},
|
|
46
76
|
{
|
|
47
77
|
url: "/level-two",
|
|
48
78
|
query: {},
|
|
49
|
-
headers: {
|
|
79
|
+
headers: {
|
|
80
|
+
age: "22"
|
|
81
|
+
}
|
|
50
82
|
}
|
|
51
83
|
],
|
|
52
84
|
[
|
|
53
85
|
{
|
|
54
86
|
requestOpt: {
|
|
55
87
|
url: "/level-three",
|
|
56
|
-
headers: {
|
|
57
|
-
|
|
88
|
+
headers: {
|
|
89
|
+
age: "17"
|
|
90
|
+
},
|
|
91
|
+
query: {
|
|
92
|
+
name: "byted"
|
|
93
|
+
}
|
|
58
94
|
},
|
|
59
95
|
cacheConfig: {
|
|
60
96
|
level: 3,
|
|
61
97
|
includes: {
|
|
62
|
-
query: [
|
|
63
|
-
|
|
98
|
+
query: [
|
|
99
|
+
"name"
|
|
100
|
+
],
|
|
101
|
+
header: [
|
|
102
|
+
"age"
|
|
103
|
+
]
|
|
64
104
|
},
|
|
65
105
|
matches: {
|
|
66
|
-
query: {
|
|
67
|
-
|
|
106
|
+
query: {
|
|
107
|
+
name: {
|
|
108
|
+
weixin: "^byted"
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
header: {
|
|
112
|
+
age: {
|
|
113
|
+
one: "^1"
|
|
114
|
+
}
|
|
115
|
+
}
|
|
68
116
|
}
|
|
69
117
|
},
|
|
70
118
|
content: "level3"
|
|
71
119
|
},
|
|
72
120
|
{
|
|
73
121
|
url: "/level-three",
|
|
74
|
-
query: {
|
|
75
|
-
|
|
122
|
+
query: {
|
|
123
|
+
name: "byted_likely"
|
|
124
|
+
},
|
|
125
|
+
headers: {
|
|
126
|
+
age: "19"
|
|
127
|
+
}
|
|
76
128
|
},
|
|
77
129
|
{
|
|
78
130
|
url: "/level-three",
|
|
79
|
-
query: {
|
|
131
|
+
query: {
|
|
132
|
+
name: "byted_likely"
|
|
133
|
+
},
|
|
80
134
|
headers: {}
|
|
81
135
|
},
|
|
82
136
|
{
|
|
83
137
|
url: "/level-three",
|
|
84
|
-
query: {
|
|
85
|
-
|
|
138
|
+
query: {
|
|
139
|
+
name: "not_byted"
|
|
140
|
+
},
|
|
141
|
+
headers: {
|
|
142
|
+
age: "19"
|
|
143
|
+
}
|
|
86
144
|
}
|
|
87
145
|
]
|
|
88
146
|
];
|
|
89
|
-
export {
|
|
90
|
-
matchedCacheableAry
|
|
91
|
-
};
|
|
@@ -2,7 +2,7 @@ import { Transform } from "stream";
|
|
|
2
2
|
import { ERROR_DIGEST } from "../../../constants";
|
|
3
3
|
import { createCache } from "./spr";
|
|
4
4
|
import { namespaceHash, withCoalescedInvoke } from "./util";
|
|
5
|
-
|
|
5
|
+
export default (renderFn, ctx) => {
|
|
6
6
|
const sprCache = createCache();
|
|
7
7
|
const doRender = async (context) => {
|
|
8
8
|
const cacheContext = {
|
|
@@ -40,16 +40,12 @@ var cache_default = (renderFn, ctx) => {
|
|
|
40
40
|
const renderResult = await renderFn(context);
|
|
41
41
|
return afterRender(renderResult, saveHtmlIntoCache);
|
|
42
42
|
}
|
|
43
|
-
const cacheHash = cacheFile
|
|
43
|
+
const cacheHash = cacheFile === null || cacheFile === void 0 ? void 0 : cacheFile.hash;
|
|
44
44
|
if (cacheFile.isGarbage) {
|
|
45
45
|
const renderResult = await renderFn(context);
|
|
46
46
|
return afterRender(renderResult, saveHtmlIntoCache);
|
|
47
47
|
} else if (cacheFile.isStale) {
|
|
48
|
-
const render = withCoalescedInvoke(() => renderFn(context)).bind(
|
|
49
|
-
null,
|
|
50
|
-
namespaceHash("render", cacheFile.hash),
|
|
51
|
-
[]
|
|
52
|
-
);
|
|
48
|
+
const render = withCoalescedInvoke(() => renderFn(context)).bind(null, namespaceHash("render", cacheFile.hash), []);
|
|
53
49
|
render().then(async (res) => {
|
|
54
50
|
if (res.value && res.isOrigin) {
|
|
55
51
|
const { cacheConfig } = context;
|
|
@@ -71,6 +67,3 @@ var cache_default = (renderFn, ctx) => {
|
|
|
71
67
|
};
|
|
72
68
|
return doRender;
|
|
73
69
|
};
|
|
74
|
-
export {
|
|
75
|
-
cache_default as default
|
|
76
|
-
};
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { LRUCaches } from "./lru";
|
|
2
|
-
async function createPageCaches(max) {
|
|
3
|
-
const constructorOptions = {
|
|
2
|
+
export async function createPageCaches(max) {
|
|
3
|
+
const constructorOptions = {
|
|
4
|
+
max
|
|
5
|
+
};
|
|
4
6
|
const cacheInstance = new LRUCaches(constructorOptions);
|
|
5
7
|
await cacheInstance.init();
|
|
6
8
|
return cacheInstance;
|
|
7
9
|
}
|
|
8
|
-
export {
|
|
9
|
-
createPageCaches
|
|
10
|
-
};
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
6
11
|
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
import LRU from "lru-cache";
|
|
15
|
+
export class LRUCaches {
|
|
7
16
|
init() {
|
|
8
17
|
return Promise.resolve();
|
|
9
18
|
}
|
|
@@ -23,7 +32,10 @@ class LRUCaches {
|
|
|
23
32
|
del(key) {
|
|
24
33
|
this.caches.del(key);
|
|
25
34
|
}
|
|
35
|
+
constructor(options) {
|
|
36
|
+
_define_property(this, "caches", void 0);
|
|
37
|
+
_define_property(this, "max", void 0);
|
|
38
|
+
this.max = options.max;
|
|
39
|
+
this.caches = new LRU(this.max);
|
|
40
|
+
}
|
|
26
41
|
}
|
|
27
|
-
export {
|
|
28
|
-
LRUCaches
|
|
29
|
-
};
|
|
@@ -1,15 +1,20 @@
|
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
1
14
|
import crypto from "crypto";
|
|
2
15
|
import LRUCache from "lru-cache";
|
|
3
16
|
import { mime } from "@modern-js/utils";
|
|
4
|
-
import {
|
|
5
|
-
cacheAddition,
|
|
6
|
-
connectFactor,
|
|
7
|
-
fname,
|
|
8
|
-
maybeSync,
|
|
9
|
-
namespaceHash,
|
|
10
|
-
valueFactory,
|
|
11
|
-
withCoalescedInvoke
|
|
12
|
-
} from "./util";
|
|
17
|
+
import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "./util";
|
|
13
18
|
import { createPageCaches } from "./page-caches";
|
|
14
19
|
const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
|
|
15
20
|
const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
|
|
@@ -18,52 +23,6 @@ const QUERY_LEVEL = 1;
|
|
|
18
23
|
const HEADER_LEVEL = 2;
|
|
19
24
|
const QUERY_HEADER_LEVEL = 3;
|
|
20
25
|
class CacheManager {
|
|
21
|
-
constructor(cacheOptions) {
|
|
22
|
-
this.find = (() => {
|
|
23
|
-
{
|
|
24
|
-
const _this = this;
|
|
25
|
-
return {
|
|
26
|
-
[fname(BASE_LEVEL)](context, cacheKey) {
|
|
27
|
-
return _this.md5(cacheKey);
|
|
28
|
-
},
|
|
29
|
-
[fname(QUERY_LEVEL)](context, cacheKey, data) {
|
|
30
|
-
const queryFactor = _this.queryFactor(context, data);
|
|
31
|
-
if (!queryFactor) {
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
return _this.md5(connectFactor(cacheKey, queryFactor));
|
|
35
|
-
},
|
|
36
|
-
[fname(HEADER_LEVEL)](context, cacheKey, data) {
|
|
37
|
-
const headerFactor = _this.headerFactor(context, data);
|
|
38
|
-
if (!headerFactor) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
return _this.md5(connectFactor(cacheKey, headerFactor));
|
|
42
|
-
},
|
|
43
|
-
[fname(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
|
|
44
|
-
const queryFactor = _this.queryFactor(context, data);
|
|
45
|
-
const headerFactor = _this.headerFactor(context, data);
|
|
46
|
-
if (!queryFactor || !headerFactor) {
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
return _this.md5(connectFactor(cacheKey, headerFactor, queryFactor));
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
})();
|
|
54
|
-
this.cacheOptions = cacheOptions;
|
|
55
|
-
this.cache = new LRUCache({
|
|
56
|
-
max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
|
|
57
|
-
// 默认存 100M,最大 600M
|
|
58
|
-
length(n) {
|
|
59
|
-
const len = n.caches.keys().reduce((total, cur) => {
|
|
60
|
-
var _a;
|
|
61
|
-
return total + (((_a = n.caches.peek(cur)) == null ? void 0 : _a.size) || 0);
|
|
62
|
-
}, 1);
|
|
63
|
-
return len;
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
26
|
md5(content) {
|
|
68
27
|
const md5 = crypto.createHash("md5");
|
|
69
28
|
return md5.update(content).digest("hex");
|
|
@@ -93,14 +52,17 @@ class CacheManager {
|
|
|
93
52
|
if (matcher) {
|
|
94
53
|
value = this.replaceValue(value, matcher);
|
|
95
54
|
}
|
|
96
|
-
return ary.concat([
|
|
55
|
+
return ary.concat([
|
|
56
|
+
key,
|
|
57
|
+
value
|
|
58
|
+
]);
|
|
97
59
|
}, []);
|
|
98
60
|
return factorAry.join(",");
|
|
99
61
|
}
|
|
100
62
|
queryFactor(context, data) {
|
|
101
|
-
var
|
|
102
|
-
const queryKeys = (
|
|
103
|
-
const queryMatches = (
|
|
63
|
+
var _data_includes, _data_matches;
|
|
64
|
+
const queryKeys = (_data_includes = data.includes) === null || _data_includes === void 0 ? void 0 : _data_includes.query;
|
|
65
|
+
const queryMatches = (_data_matches = data.matches) === null || _data_matches === void 0 ? void 0 : _data_matches.query;
|
|
104
66
|
if (!queryKeys || queryKeys.length === 0) {
|
|
105
67
|
return null;
|
|
106
68
|
}
|
|
@@ -109,9 +71,9 @@ class CacheManager {
|
|
|
109
71
|
return queryFactor;
|
|
110
72
|
}
|
|
111
73
|
headerFactor(context, data) {
|
|
112
|
-
var
|
|
113
|
-
const headerKeys = (
|
|
114
|
-
const headerMatches = (
|
|
74
|
+
var _data_includes, _data_matches;
|
|
75
|
+
const headerKeys = (_data_includes = data.includes) === null || _data_includes === void 0 ? void 0 : _data_includes.header;
|
|
76
|
+
const headerMatches = (_data_matches = data.matches) === null || _data_matches === void 0 ? void 0 : _data_matches.header;
|
|
115
77
|
if (!headerKeys || headerKeys.length === 0) {
|
|
116
78
|
return null;
|
|
117
79
|
}
|
|
@@ -168,11 +130,7 @@ class CacheManager {
|
|
|
168
130
|
const caches = await createPageCaches(MAX_CACHE_EACH_REQ);
|
|
169
131
|
data = this.createCacheContent(cacheConfig, caches);
|
|
170
132
|
}
|
|
171
|
-
const cacheHash = this.find[fname(cacheConfig.level)](
|
|
172
|
-
context,
|
|
173
|
-
cacheKey,
|
|
174
|
-
data
|
|
175
|
-
);
|
|
133
|
+
const cacheHash = this.find[fname(cacheConfig.level)](context, cacheKey, data);
|
|
176
134
|
if (!cacheHash) {
|
|
177
135
|
return false;
|
|
178
136
|
}
|
|
@@ -191,31 +149,72 @@ class CacheManager {
|
|
|
191
149
|
this.cache.set(cacheKey, next);
|
|
192
150
|
return true;
|
|
193
151
|
};
|
|
194
|
-
const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(
|
|
195
|
-
null,
|
|
196
|
-
namespaceHash("stream", cacheHash),
|
|
197
|
-
[]
|
|
198
|
-
);
|
|
152
|
+
const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(null, namespaceHash("stream", cacheHash), []);
|
|
199
153
|
return maybeSync(doCache)(sync);
|
|
200
154
|
}
|
|
201
155
|
async del(context, cacheHash) {
|
|
202
156
|
const cacheKey = this.generateRequestKey(context);
|
|
203
157
|
const data = this.cache.get(cacheKey);
|
|
204
|
-
data
|
|
158
|
+
data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
|
|
159
|
+
}
|
|
160
|
+
constructor(cacheOptions) {
|
|
161
|
+
_define_property(this, "cache", void 0);
|
|
162
|
+
_define_property(this, "cacheOptions", void 0);
|
|
163
|
+
_define_property(this, "find", (() => {
|
|
164
|
+
{
|
|
165
|
+
const _this = this;
|
|
166
|
+
return {
|
|
167
|
+
[fname(BASE_LEVEL)](context, cacheKey) {
|
|
168
|
+
return _this.md5(cacheKey);
|
|
169
|
+
},
|
|
170
|
+
[fname(QUERY_LEVEL)](context, cacheKey, data) {
|
|
171
|
+
const queryFactor = _this.queryFactor(context, data);
|
|
172
|
+
if (!queryFactor) {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
return _this.md5(connectFactor(cacheKey, queryFactor));
|
|
176
|
+
},
|
|
177
|
+
[fname(HEADER_LEVEL)](context, cacheKey, data) {
|
|
178
|
+
const headerFactor = _this.headerFactor(context, data);
|
|
179
|
+
if (!headerFactor) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
return _this.md5(connectFactor(cacheKey, headerFactor));
|
|
183
|
+
},
|
|
184
|
+
[fname(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
|
|
185
|
+
const queryFactor = _this.queryFactor(context, data);
|
|
186
|
+
const headerFactor = _this.headerFactor(context, data);
|
|
187
|
+
if (!queryFactor || !headerFactor) {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
return _this.md5(connectFactor(cacheKey, headerFactor, queryFactor));
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
})());
|
|
195
|
+
this.cacheOptions = cacheOptions;
|
|
196
|
+
this.cache = new LRUCache({
|
|
197
|
+
max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
|
|
198
|
+
length(n) {
|
|
199
|
+
const len = n.caches.keys().reduce((total, cur) => {
|
|
200
|
+
var _n_caches_peek;
|
|
201
|
+
return total + (((_n_caches_peek = n.caches.peek(cur)) === null || _n_caches_peek === void 0 ? void 0 : _n_caches_peek.size) || 0);
|
|
202
|
+
}, 1);
|
|
203
|
+
return len;
|
|
204
|
+
}
|
|
205
|
+
});
|
|
205
206
|
}
|
|
206
207
|
}
|
|
207
208
|
let manager;
|
|
208
|
-
function createCache() {
|
|
209
|
+
export function createCache() {
|
|
209
210
|
if (manager) {
|
|
210
211
|
return manager;
|
|
211
212
|
}
|
|
212
|
-
manager = new CacheManager({
|
|
213
|
+
manager = new CacheManager({
|
|
214
|
+
max: 0
|
|
215
|
+
});
|
|
213
216
|
return manager;
|
|
214
217
|
}
|
|
215
|
-
function destroyCache() {
|
|
218
|
+
export function destroyCache() {
|
|
216
219
|
manager = null;
|
|
217
220
|
}
|
|
218
|
-
export {
|
|
219
|
-
createCache,
|
|
220
|
-
destroyCache
|
|
221
|
-
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import url from "url";
|
|
2
|
-
function namespaceHash(namespace, hash) {
|
|
2
|
+
export function namespaceHash(namespace, hash) {
|
|
3
3
|
return `${namespace}/${hash}`;
|
|
4
4
|
}
|
|
5
|
-
function fname(lv) {
|
|
5
|
+
export function fname(lv) {
|
|
6
6
|
return `f${lv}`;
|
|
7
7
|
}
|
|
8
|
-
function connectFactor(...args) {
|
|
8
|
+
export function connectFactor(...args) {
|
|
9
9
|
return args.join("-");
|
|
10
10
|
}
|
|
11
|
-
function valueFactory(obj) {
|
|
11
|
+
export function valueFactory(obj) {
|
|
12
12
|
if (obj instanceof url.URLSearchParams) {
|
|
13
13
|
return function(key) {
|
|
14
14
|
return obj.get(key);
|
|
@@ -23,19 +23,16 @@ function valueFactory(obj) {
|
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
-
function getTime([s, ns]) {
|
|
26
|
+
export function getTime([s, ns]) {
|
|
27
27
|
return Math.floor(s * 1e3 + ns / 1e6);
|
|
28
28
|
}
|
|
29
29
|
const RE_START_IN_HEAD = /<head>/;
|
|
30
|
-
function cacheAddition(html, hash) {
|
|
31
|
-
const additionHtml = html.replace(
|
|
32
|
-
RE_START_IN_HEAD,
|
|
33
|
-
`<head><meta name="x-moden-spr" content="${hash}">`
|
|
34
|
-
);
|
|
30
|
+
export function cacheAddition(html, hash) {
|
|
31
|
+
const additionHtml = html.replace(RE_START_IN_HEAD, `<head><meta name="x-moden-spr" content="${hash}">`);
|
|
35
32
|
return additionHtml;
|
|
36
33
|
}
|
|
37
34
|
const globalInvokeCache = /* @__PURE__ */ new Map();
|
|
38
|
-
function withCoalescedInvoke(func) {
|
|
35
|
+
export function withCoalescedInvoke(func) {
|
|
39
36
|
return async function(key, args) {
|
|
40
37
|
const entry = globalInvokeCache.get(key);
|
|
41
38
|
if (entry) {
|
|
@@ -49,7 +46,10 @@ function withCoalescedInvoke(func) {
|
|
|
49
46
|
}
|
|
50
47
|
const future = __wrapper().then((res) => {
|
|
51
48
|
globalInvokeCache.delete(key);
|
|
52
|
-
return {
|
|
49
|
+
return {
|
|
50
|
+
isOrigin: true,
|
|
51
|
+
value: res
|
|
52
|
+
};
|
|
53
53
|
}).catch((err) => {
|
|
54
54
|
globalInvokeCache.delete(key);
|
|
55
55
|
throw err;
|
|
@@ -58,7 +58,7 @@ function withCoalescedInvoke(func) {
|
|
|
58
58
|
return future;
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
|
-
function maybeSync(fn) {
|
|
61
|
+
export function maybeSync(fn) {
|
|
62
62
|
return (sync) => {
|
|
63
63
|
if (sync) {
|
|
64
64
|
return fn();
|
|
@@ -68,13 +68,3 @@ function maybeSync(fn) {
|
|
|
68
68
|
}
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
|
-
export {
|
|
72
|
-
cacheAddition,
|
|
73
|
-
connectFactor,
|
|
74
|
-
fname,
|
|
75
|
-
getTime,
|
|
76
|
-
maybeSync,
|
|
77
|
-
namespaceHash,
|
|
78
|
-
valueFactory,
|
|
79
|
-
withCoalescedInvoke
|
|
80
|
-
};
|
|
@@ -5,15 +5,7 @@ import { handleDirectory } from "./static";
|
|
|
5
5
|
import { readFile } from "./reader";
|
|
6
6
|
import * as ssr from "./ssr";
|
|
7
7
|
import { injectServerData } from "./utils";
|
|
8
|
-
const createRenderHandler = ({
|
|
9
|
-
distDir,
|
|
10
|
-
staticGenerate,
|
|
11
|
-
forceCSR
|
|
12
|
-
}) => async function render({
|
|
13
|
-
ctx,
|
|
14
|
-
route,
|
|
15
|
-
runner
|
|
16
|
-
}) {
|
|
8
|
+
export const createRenderHandler = ({ distDir, staticGenerate, forceCSR }) => async function render({ ctx, route, runner }) {
|
|
17
9
|
if (ctx.resHasHandled()) {
|
|
18
10
|
return null;
|
|
19
11
|
}
|
|
@@ -34,24 +26,17 @@ const createRenderHandler = ({
|
|
|
34
26
|
const useCSR = forceCSR && ctx.query.csr;
|
|
35
27
|
if (route.isSSR && !useCSR) {
|
|
36
28
|
try {
|
|
37
|
-
const result = await ssr.render(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
staticGenerate
|
|
46
|
-
},
|
|
47
|
-
runner
|
|
48
|
-
);
|
|
29
|
+
const result = await ssr.render(ctx, {
|
|
30
|
+
distDir,
|
|
31
|
+
entryName: route.entryName,
|
|
32
|
+
urlPath: route.urlPath,
|
|
33
|
+
bundle: route.bundle,
|
|
34
|
+
template: content.toString(),
|
|
35
|
+
staticGenerate
|
|
36
|
+
}, runner);
|
|
49
37
|
return result;
|
|
50
38
|
} catch (err) {
|
|
51
|
-
ctx.error(
|
|
52
|
-
ERROR_DIGEST.ERENDER,
|
|
53
|
-
err.stack || err.message
|
|
54
|
-
);
|
|
39
|
+
ctx.error(ERROR_DIGEST.ERENDER, err.stack || err.message);
|
|
55
40
|
ctx.res.setHeader("x-modern-ssr-fallback", "1");
|
|
56
41
|
}
|
|
57
42
|
}
|
|
@@ -60,6 +45,3 @@ const createRenderHandler = ({
|
|
|
60
45
|
contentType: mime.contentType(path.extname(templatePath))
|
|
61
46
|
};
|
|
62
47
|
};
|
|
63
|
-
export {
|
|
64
|
-
createRenderHandler
|
|
65
|
-
};
|