@modern-js/prod-server 2.8.0 → 2.9.1-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 +9 -0
- package/dist/cjs/server/index.js +8 -6
- package/dist/esm/server/index.js +71 -18
- package/dist/esm-node/server/index.js +9 -6
- package/dist/js/modern/constants.js +35 -0
- package/dist/js/modern/index.js +18 -0
- package/dist/js/modern/libs/context/context.js +160 -0
- package/dist/js/modern/libs/context/index.js +6 -0
- package/dist/js/modern/libs/hook-api/index.js +134 -0
- package/dist/js/modern/libs/hook-api/route.js +20 -0
- package/dist/js/modern/libs/hook-api/template.js +73 -0
- package/dist/js/modern/libs/loadConfig.js +62 -0
- package/dist/js/modern/libs/logger.js +111 -0
- package/dist/js/modern/libs/metrics.js +11 -0
- package/dist/js/modern/libs/proxy.js +92 -0
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +114 -0
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +254 -0
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +47 -0
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +37 -0
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +91 -0
- package/dist/js/modern/libs/render/cache/index.js +115 -0
- package/dist/js/modern/libs/render/cache/page-caches/index.js +32 -0
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +29 -0
- package/dist/js/modern/libs/render/cache/spr.js +248 -0
- package/dist/js/modern/libs/render/cache/type.js +0 -0
- package/dist/js/modern/libs/render/cache/util.js +102 -0
- package/dist/js/modern/libs/render/index.js +86 -0
- package/dist/js/modern/libs/render/measure.js +68 -0
- package/dist/js/modern/libs/render/reader.js +107 -0
- package/dist/js/modern/libs/render/ssr.js +100 -0
- package/dist/js/modern/libs/render/static.js +60 -0
- package/dist/js/modern/libs/render/type.js +9 -0
- package/dist/js/modern/libs/route/index.js +54 -0
- package/dist/js/modern/libs/route/matcher.js +87 -0
- package/dist/js/modern/libs/route/route.js +16 -0
- package/dist/js/modern/libs/serve-file.js +67 -0
- package/dist/js/modern/server/index.js +208 -0
- package/dist/js/modern/server/modern-server-split.js +74 -0
- package/dist/js/modern/server/modern-server.js +554 -0
- package/dist/js/modern/type.js +0 -0
- package/dist/js/modern/utils.js +136 -0
- package/dist/js/modern/worker-server.js +89 -0
- package/dist/js/node/constants.js +62 -0
- package/dist/js/node/index.js +44 -0
- package/dist/js/node/libs/context/context.js +189 -0
- package/dist/js/node/libs/context/index.js +30 -0
- package/dist/js/node/libs/hook-api/index.js +164 -0
- package/dist/js/node/libs/hook-api/route.js +43 -0
- package/dist/js/node/libs/hook-api/template.js +97 -0
- package/dist/js/node/libs/loadConfig.js +91 -0
- package/dist/js/node/libs/logger.js +133 -0
- package/dist/js/node/libs/metrics.js +34 -0
- package/dist/js/node/libs/proxy.js +114 -0
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +115 -0
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +245 -0
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +70 -0
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +60 -0
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +114 -0
- package/dist/js/node/libs/render/cache/index.js +134 -0
- package/dist/js/node/libs/render/cache/page-caches/index.js +55 -0
- package/dist/js/node/libs/render/cache/page-caches/lru.js +58 -0
- package/dist/js/node/libs/render/cache/spr.js +270 -0
- package/dist/js/node/libs/render/cache/type.js +15 -0
- package/dist/js/node/libs/render/cache/util.js +138 -0
- package/dist/js/node/libs/render/index.js +115 -0
- package/dist/js/node/libs/render/measure.js +90 -0
- package/dist/js/node/libs/render/reader.js +140 -0
- package/dist/js/node/libs/render/ssr.js +123 -0
- package/dist/js/node/libs/render/static.js +89 -0
- package/dist/js/node/libs/render/type.js +32 -0
- package/dist/js/node/libs/route/index.js +78 -0
- package/dist/js/node/libs/route/matcher.js +106 -0
- package/dist/js/node/libs/route/route.js +39 -0
- package/dist/js/node/libs/serve-file.js +97 -0
- package/dist/js/node/server/index.js +219 -0
- package/dist/js/node/server/modern-server-split.js +97 -0
- package/dist/js/node/server/modern-server.js +559 -0
- package/dist/js/node/type.js +15 -0
- package/dist/js/node/utils.js +166 -0
- package/dist/js/node/worker-server.js +113 -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/logger.js +205 -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 +234 -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 +143 -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 +1089 -0
- package/dist/js/treeshaking/type.js +1 -0
- package/dist/js/treeshaking/utils.js +147 -0
- package/dist/js/treeshaking/worker-server.js +233 -0
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/utils.d.ts +1 -1
- package/package.json +9 -9
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const errorConfiguration = [
|
|
2
|
+
{ level: 1 },
|
|
3
|
+
{
|
|
4
|
+
level: 1,
|
|
5
|
+
includes: {}
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
level: 1,
|
|
9
|
+
includes: { query: [] }
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
level: 2,
|
|
13
|
+
includes: {}
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
level: 2,
|
|
17
|
+
includes: { header: [] }
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
level: 3,
|
|
21
|
+
includes: { header: [] }
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
level: 3,
|
|
25
|
+
includes: { query: [] }
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
level: 3,
|
|
29
|
+
includes: {
|
|
30
|
+
query: [],
|
|
31
|
+
header: []
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
];
|
|
35
|
+
export {
|
|
36
|
+
errorConfiguration
|
|
37
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const matchedCacheableAry = [
|
|
2
|
+
[
|
|
3
|
+
{
|
|
4
|
+
requestOpt: {
|
|
5
|
+
url: "/level-one",
|
|
6
|
+
headers: {},
|
|
7
|
+
query: { name: "byted" }
|
|
8
|
+
},
|
|
9
|
+
cacheConfig: {
|
|
10
|
+
level: 1,
|
|
11
|
+
includes: { query: ["name"] },
|
|
12
|
+
matches: { query: { name: { weixin: "^byted" } } }
|
|
13
|
+
},
|
|
14
|
+
content: "level1"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
url: "/level-one",
|
|
18
|
+
headers: {},
|
|
19
|
+
query: { name: "byted_likely" }
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
url: "/level-one",
|
|
23
|
+
headers: {},
|
|
24
|
+
query: { name: "not_byted" }
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
[
|
|
28
|
+
{
|
|
29
|
+
requestOpt: {
|
|
30
|
+
url: "/level-two",
|
|
31
|
+
query: {},
|
|
32
|
+
headers: { age: "17" }
|
|
33
|
+
},
|
|
34
|
+
cacheConfig: {
|
|
35
|
+
level: 2,
|
|
36
|
+
includes: { header: ["age"] },
|
|
37
|
+
matches: { header: { age: { one: "^1" } } }
|
|
38
|
+
},
|
|
39
|
+
content: "level2"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
url: "/level-two",
|
|
43
|
+
query: {},
|
|
44
|
+
headers: { age: "11" }
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
url: "/level-two",
|
|
48
|
+
query: {},
|
|
49
|
+
headers: { age: "22" }
|
|
50
|
+
}
|
|
51
|
+
],
|
|
52
|
+
[
|
|
53
|
+
{
|
|
54
|
+
requestOpt: {
|
|
55
|
+
url: "/level-three",
|
|
56
|
+
headers: { age: "17" },
|
|
57
|
+
query: { name: "byted" }
|
|
58
|
+
},
|
|
59
|
+
cacheConfig: {
|
|
60
|
+
level: 3,
|
|
61
|
+
includes: {
|
|
62
|
+
query: ["name"],
|
|
63
|
+
header: ["age"]
|
|
64
|
+
},
|
|
65
|
+
matches: {
|
|
66
|
+
query: { name: { weixin: "^byted" } },
|
|
67
|
+
header: { age: { one: "^1" } }
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
content: "level3"
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
url: "/level-three",
|
|
74
|
+
query: { name: "byted_likely" },
|
|
75
|
+
headers: { age: "19" }
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
url: "/level-three",
|
|
79
|
+
query: { name: "byted_likely" },
|
|
80
|
+
headers: {}
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
url: "/level-three",
|
|
84
|
+
query: { name: "not_byted" },
|
|
85
|
+
headers: { age: "19" }
|
|
86
|
+
}
|
|
87
|
+
]
|
|
88
|
+
];
|
|
89
|
+
export {
|
|
90
|
+
matchedCacheableAry
|
|
91
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __async = (__this, __arguments, generator) => {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
var fulfilled = (value) => {
|
|
20
|
+
try {
|
|
21
|
+
step(generator.next(value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var rejected = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.throw(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
import { Transform } from "stream";
|
|
38
|
+
import { ERROR_DIGEST } from "../../../constants";
|
|
39
|
+
import { createCache } from "./spr";
|
|
40
|
+
import { namespaceHash, withCoalescedInvoke } from "./util";
|
|
41
|
+
var cache_default = (renderFn, ctx) => {
|
|
42
|
+
const sprCache = createCache();
|
|
43
|
+
const doRender = (context) => __async(void 0, null, function* () {
|
|
44
|
+
const cacheContext = __spreadValues({
|
|
45
|
+
entry: context.entryName
|
|
46
|
+
}, context.request);
|
|
47
|
+
const cacheFile = yield sprCache.get(cacheContext);
|
|
48
|
+
function afterRender(source, onAfterRender) {
|
|
49
|
+
return __async(this, null, function* () {
|
|
50
|
+
if (!source) {
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
if (typeof source === "string") {
|
|
54
|
+
yield onAfterRender(source);
|
|
55
|
+
return source;
|
|
56
|
+
} else {
|
|
57
|
+
let htmlForStream = "";
|
|
58
|
+
const cacheStream = new Transform({
|
|
59
|
+
write(chunk, _, callback) {
|
|
60
|
+
htmlForStream += chunk.toString();
|
|
61
|
+
this.push(chunk);
|
|
62
|
+
callback();
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
cacheStream.on("close", () => onAfterRender(htmlForStream));
|
|
66
|
+
return source(cacheStream);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function saveHtmlIntoCache(html) {
|
|
71
|
+
return __async(this, null, function* () {
|
|
72
|
+
const { cacheConfig } = context;
|
|
73
|
+
if (html && cacheConfig) {
|
|
74
|
+
yield sprCache.set(cacheContext, html, cacheConfig);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
if (!cacheFile) {
|
|
79
|
+
const renderResult = yield renderFn(context);
|
|
80
|
+
return afterRender(renderResult, saveHtmlIntoCache);
|
|
81
|
+
}
|
|
82
|
+
const cacheHash = cacheFile == null ? void 0 : cacheFile.hash;
|
|
83
|
+
if (cacheFile.isGarbage) {
|
|
84
|
+
const renderResult = yield renderFn(context);
|
|
85
|
+
return afterRender(renderResult, saveHtmlIntoCache);
|
|
86
|
+
} else if (cacheFile.isStale) {
|
|
87
|
+
const render = withCoalescedInvoke(() => renderFn(context)).bind(
|
|
88
|
+
null,
|
|
89
|
+
namespaceHash("render", cacheFile.hash),
|
|
90
|
+
[]
|
|
91
|
+
);
|
|
92
|
+
render().then((res) => __async(void 0, null, function* () {
|
|
93
|
+
if (res.value && res.isOrigin) {
|
|
94
|
+
const { cacheConfig } = context;
|
|
95
|
+
if (cacheConfig) {
|
|
96
|
+
afterRender(res.value, (html) => __async(void 0, null, function* () {
|
|
97
|
+
sprCache.set(cacheContext, html, cacheConfig);
|
|
98
|
+
}));
|
|
99
|
+
} else {
|
|
100
|
+
sprCache.del(cacheContext, cacheHash);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
})).catch((e) => {
|
|
104
|
+
sprCache.del(cacheContext, cacheHash);
|
|
105
|
+
ctx.error(ERROR_DIGEST.ERENDER, e);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
ctx.res.setHeader("x-modern-spr", "1");
|
|
109
|
+
return cacheFile.content;
|
|
110
|
+
});
|
|
111
|
+
return doRender;
|
|
112
|
+
};
|
|
113
|
+
export {
|
|
114
|
+
cache_default as default
|
|
115
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
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 { LRUCaches } from "./lru";
|
|
22
|
+
function createPageCaches(max) {
|
|
23
|
+
return __async(this, null, function* () {
|
|
24
|
+
const constructorOptions = { max };
|
|
25
|
+
const cacheInstance = new LRUCaches(constructorOptions);
|
|
26
|
+
yield cacheInstance.init();
|
|
27
|
+
return cacheInstance;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
createPageCaches
|
|
32
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import LRU from "lru-cache";
|
|
2
|
+
class LRUCaches {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
this.max = options.max;
|
|
5
|
+
this.caches = new LRU(this.max);
|
|
6
|
+
}
|
|
7
|
+
init() {
|
|
8
|
+
return Promise.resolve();
|
|
9
|
+
}
|
|
10
|
+
keys() {
|
|
11
|
+
return this.caches.keys();
|
|
12
|
+
}
|
|
13
|
+
get(key) {
|
|
14
|
+
return Promise.resolve(this.caches.get(key) || null);
|
|
15
|
+
}
|
|
16
|
+
peek(key) {
|
|
17
|
+
return this.caches.peek(key) || null;
|
|
18
|
+
}
|
|
19
|
+
set(key, cache) {
|
|
20
|
+
this.caches.set(key, cache);
|
|
21
|
+
return Promise.resolve();
|
|
22
|
+
}
|
|
23
|
+
del(key) {
|
|
24
|
+
this.caches.del(key);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
LRUCaches
|
|
29
|
+
};
|
|
@@ -0,0 +1,248 @@
|
|
|
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 crypto from "crypto";
|
|
22
|
+
import LRUCache from "lru-cache";
|
|
23
|
+
import { mime } from "@modern-js/utils";
|
|
24
|
+
import {
|
|
25
|
+
cacheAddition,
|
|
26
|
+
connectFactor,
|
|
27
|
+
fname,
|
|
28
|
+
maybeSync,
|
|
29
|
+
namespaceHash,
|
|
30
|
+
valueFactory,
|
|
31
|
+
withCoalescedInvoke
|
|
32
|
+
} from "./util";
|
|
33
|
+
import { createPageCaches } from "./page-caches";
|
|
34
|
+
const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
|
|
35
|
+
const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
|
|
36
|
+
const BASE_LEVEL = 0;
|
|
37
|
+
const QUERY_LEVEL = 1;
|
|
38
|
+
const HEADER_LEVEL = 2;
|
|
39
|
+
const QUERY_HEADER_LEVEL = 3;
|
|
40
|
+
class CacheManager {
|
|
41
|
+
constructor(cacheOptions) {
|
|
42
|
+
this.find = (() => {
|
|
43
|
+
{
|
|
44
|
+
const _this = this;
|
|
45
|
+
return {
|
|
46
|
+
[fname(BASE_LEVEL)](context, cacheKey) {
|
|
47
|
+
return _this.md5(cacheKey);
|
|
48
|
+
},
|
|
49
|
+
[fname(QUERY_LEVEL)](context, cacheKey, data) {
|
|
50
|
+
const queryFactor = _this.queryFactor(context, data);
|
|
51
|
+
if (!queryFactor) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return _this.md5(connectFactor(cacheKey, queryFactor));
|
|
55
|
+
},
|
|
56
|
+
[fname(HEADER_LEVEL)](context, cacheKey, data) {
|
|
57
|
+
const headerFactor = _this.headerFactor(context, data);
|
|
58
|
+
if (!headerFactor) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
return _this.md5(connectFactor(cacheKey, headerFactor));
|
|
62
|
+
},
|
|
63
|
+
[fname(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
|
|
64
|
+
const queryFactor = _this.queryFactor(context, data);
|
|
65
|
+
const headerFactor = _this.headerFactor(context, data);
|
|
66
|
+
if (!queryFactor || !headerFactor) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
return _this.md5(connectFactor(cacheKey, headerFactor, queryFactor));
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
})();
|
|
74
|
+
this.cacheOptions = cacheOptions;
|
|
75
|
+
this.cache = new LRUCache({
|
|
76
|
+
max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
|
|
77
|
+
length(n) {
|
|
78
|
+
const len = n.caches.keys().reduce((total, cur) => {
|
|
79
|
+
var _a;
|
|
80
|
+
return total + (((_a = n.caches.peek(cur)) == null ? void 0 : _a.size) || 0);
|
|
81
|
+
}, 1);
|
|
82
|
+
return len;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
md5(content) {
|
|
87
|
+
const md5 = crypto.createHash("md5");
|
|
88
|
+
return md5.update(content).digest("hex");
|
|
89
|
+
}
|
|
90
|
+
generateRequestKey(context) {
|
|
91
|
+
const { pathname, entry } = context;
|
|
92
|
+
return this.md5(`${pathname}_${entry}`);
|
|
93
|
+
}
|
|
94
|
+
replaceValue(value, matcher) {
|
|
95
|
+
let final = value;
|
|
96
|
+
Object.keys(matcher).some((replacer) => {
|
|
97
|
+
const reg = new RegExp(matcher[replacer]);
|
|
98
|
+
if (reg.test(value)) {
|
|
99
|
+
final = replacer;
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
});
|
|
104
|
+
return final;
|
|
105
|
+
}
|
|
106
|
+
factor(keys, obj, matches = {}) {
|
|
107
|
+
keys.sort();
|
|
108
|
+
const getValue = valueFactory(obj);
|
|
109
|
+
const factorAry = keys.reduce((ary, key) => {
|
|
110
|
+
let value = getValue(key) || "";
|
|
111
|
+
const matcher = matches[key];
|
|
112
|
+
if (matcher) {
|
|
113
|
+
value = this.replaceValue(value, matcher);
|
|
114
|
+
}
|
|
115
|
+
return ary.concat([key, value]);
|
|
116
|
+
}, []);
|
|
117
|
+
return factorAry.join(",");
|
|
118
|
+
}
|
|
119
|
+
queryFactor(context, data) {
|
|
120
|
+
var _a, _b;
|
|
121
|
+
const queryKeys = (_a = data.includes) == null ? void 0 : _a.query;
|
|
122
|
+
const queryMatches = (_b = data.matches) == null ? void 0 : _b.query;
|
|
123
|
+
if (!queryKeys || queryKeys.length === 0) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
const requestQuery = context.query;
|
|
127
|
+
const queryFactor = this.factor(queryKeys, requestQuery, queryMatches);
|
|
128
|
+
return queryFactor;
|
|
129
|
+
}
|
|
130
|
+
headerFactor(context, data) {
|
|
131
|
+
var _a, _b;
|
|
132
|
+
const headerKeys = (_a = data.includes) == null ? void 0 : _a.header;
|
|
133
|
+
const headerMatches = (_b = data.matches) == null ? void 0 : _b.header;
|
|
134
|
+
if (!headerKeys || headerKeys.length === 0) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const requestHeader = context.headers;
|
|
138
|
+
const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
|
|
139
|
+
return headerFactor;
|
|
140
|
+
}
|
|
141
|
+
best(context, cacheKey, data) {
|
|
142
|
+
return __async(this, null, function* () {
|
|
143
|
+
const { level } = data;
|
|
144
|
+
const cacheHash = this.find[fname(level)](context, cacheKey, data);
|
|
145
|
+
if (!cacheHash) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
return data.caches.get(cacheHash);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
createCacheContent(config, caches) {
|
|
152
|
+
return {
|
|
153
|
+
level: config.level,
|
|
154
|
+
interval: config.interval,
|
|
155
|
+
includes: config.includes || null,
|
|
156
|
+
limit: config.staleLimit,
|
|
157
|
+
matches: config.matches || null,
|
|
158
|
+
caches
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
get(context) {
|
|
162
|
+
return __async(this, null, function* () {
|
|
163
|
+
const cacheKey = this.generateRequestKey(context);
|
|
164
|
+
const data = this.cache.get(cacheKey);
|
|
165
|
+
if (!data) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
const dest = yield this.best(context, cacheKey, data);
|
|
169
|
+
if (!dest) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
const { expireTime, limitTime, html, cacheHash } = dest;
|
|
173
|
+
const isStale = Date.now() - expireTime > 0;
|
|
174
|
+
const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
|
|
175
|
+
return {
|
|
176
|
+
content: html || "",
|
|
177
|
+
contentType: mime.contentType("html"),
|
|
178
|
+
isStale,
|
|
179
|
+
isGarbage,
|
|
180
|
+
hash: cacheHash
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
set(context, html, cacheConfig, sync = false) {
|
|
185
|
+
return __async(this, null, function* () {
|
|
186
|
+
if (!cacheConfig) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
const cacheKey = this.generateRequestKey(context);
|
|
190
|
+
let data = this.cache.get(cacheKey);
|
|
191
|
+
if (!data) {
|
|
192
|
+
const caches = yield createPageCaches(MAX_CACHE_EACH_REQ);
|
|
193
|
+
data = this.createCacheContent(cacheConfig, caches);
|
|
194
|
+
}
|
|
195
|
+
const cacheHash = this.find[fname(cacheConfig.level)](
|
|
196
|
+
context,
|
|
197
|
+
cacheKey,
|
|
198
|
+
data
|
|
199
|
+
);
|
|
200
|
+
if (!cacheHash) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
const cacheSyncOrAsync = () => __async(this, null, function* () {
|
|
204
|
+
const next = data;
|
|
205
|
+
const limit = cacheConfig.staleLimit;
|
|
206
|
+
const storeHTML = cacheAddition(html, cacheHash);
|
|
207
|
+
const size = storeHTML.length;
|
|
208
|
+
yield next.caches.set(cacheHash, {
|
|
209
|
+
expireTime: Date.now() + cacheConfig.interval * 1e3,
|
|
210
|
+
limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
|
|
211
|
+
cacheHash,
|
|
212
|
+
html: storeHTML,
|
|
213
|
+
size
|
|
214
|
+
});
|
|
215
|
+
this.cache.set(cacheKey, next);
|
|
216
|
+
return true;
|
|
217
|
+
});
|
|
218
|
+
const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(
|
|
219
|
+
null,
|
|
220
|
+
namespaceHash("stream", cacheHash),
|
|
221
|
+
[]
|
|
222
|
+
);
|
|
223
|
+
return maybeSync(doCache)(sync);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
del(context, cacheHash) {
|
|
227
|
+
return __async(this, null, function* () {
|
|
228
|
+
const cacheKey = this.generateRequestKey(context);
|
|
229
|
+
const data = this.cache.get(cacheKey);
|
|
230
|
+
data == null ? void 0 : data.caches.del(cacheHash);
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
let manager;
|
|
235
|
+
function createCache() {
|
|
236
|
+
if (manager) {
|
|
237
|
+
return manager;
|
|
238
|
+
}
|
|
239
|
+
manager = new CacheManager({ max: 0 });
|
|
240
|
+
return manager;
|
|
241
|
+
}
|
|
242
|
+
function destroyCache() {
|
|
243
|
+
manager = null;
|
|
244
|
+
}
|
|
245
|
+
export {
|
|
246
|
+
createCache,
|
|
247
|
+
destroyCache
|
|
248
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,102 @@
|
|
|
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 url from "url";
|
|
22
|
+
function namespaceHash(namespace, hash) {
|
|
23
|
+
return `${namespace}/${hash}`;
|
|
24
|
+
}
|
|
25
|
+
function fname(lv) {
|
|
26
|
+
return `f${lv}`;
|
|
27
|
+
}
|
|
28
|
+
function connectFactor(...args) {
|
|
29
|
+
return args.join("-");
|
|
30
|
+
}
|
|
31
|
+
function valueFactory(obj) {
|
|
32
|
+
if (obj instanceof url.URLSearchParams) {
|
|
33
|
+
return function(key) {
|
|
34
|
+
return obj.get(key);
|
|
35
|
+
};
|
|
36
|
+
} else {
|
|
37
|
+
return function(key) {
|
|
38
|
+
const value = obj[key];
|
|
39
|
+
if (Array.isArray(value)) {
|
|
40
|
+
return value.join(",");
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function getTime([s, ns]) {
|
|
47
|
+
return Math.floor(s * 1e3 + ns / 1e6);
|
|
48
|
+
}
|
|
49
|
+
const RE_START_IN_HEAD = /<head>/;
|
|
50
|
+
function cacheAddition(html, hash) {
|
|
51
|
+
const additionHtml = html.replace(
|
|
52
|
+
RE_START_IN_HEAD,
|
|
53
|
+
`<head><meta name="x-moden-spr" content="${hash}">`
|
|
54
|
+
);
|
|
55
|
+
return additionHtml;
|
|
56
|
+
}
|
|
57
|
+
const globalInvokeCache = /* @__PURE__ */ new Map();
|
|
58
|
+
function withCoalescedInvoke(func) {
|
|
59
|
+
return function(key, args) {
|
|
60
|
+
return __async(this, null, function* () {
|
|
61
|
+
const entry = globalInvokeCache.get(key);
|
|
62
|
+
if (entry) {
|
|
63
|
+
return entry.then((res) => ({
|
|
64
|
+
isOrigin: false,
|
|
65
|
+
value: res.value
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
function __wrapper() {
|
|
69
|
+
return func(...args);
|
|
70
|
+
}
|
|
71
|
+
const future = __wrapper().then((res) => {
|
|
72
|
+
globalInvokeCache.delete(key);
|
|
73
|
+
return { isOrigin: true, value: res };
|
|
74
|
+
}).catch((err) => {
|
|
75
|
+
globalInvokeCache.delete(key);
|
|
76
|
+
throw err;
|
|
77
|
+
});
|
|
78
|
+
globalInvokeCache.set(key, future);
|
|
79
|
+
return future;
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function maybeSync(fn) {
|
|
84
|
+
return (sync) => {
|
|
85
|
+
if (sync) {
|
|
86
|
+
return fn();
|
|
87
|
+
} else {
|
|
88
|
+
fn();
|
|
89
|
+
return Promise.resolve();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
cacheAddition,
|
|
95
|
+
connectFactor,
|
|
96
|
+
fname,
|
|
97
|
+
getTime,
|
|
98
|
+
maybeSync,
|
|
99
|
+
namespaceHash,
|
|
100
|
+
valueFactory,
|
|
101
|
+
withCoalescedInvoke
|
|
102
|
+
};
|