@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,62 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import * as path from "path";
|
|
21
|
+
import { compatRequire, fs, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
|
|
22
|
+
import mergeDeep from "merge-deep";
|
|
23
|
+
const getServerConfigPath = (distDirectory, serverConfigFile = DEFAULT_SERVER_CONFIG) => {
|
|
24
|
+
const serverConfigPath = path.join(distDirectory, serverConfigFile);
|
|
25
|
+
return `${serverConfigPath}.js`;
|
|
26
|
+
};
|
|
27
|
+
const requireConfig = (serverConfigPath) => {
|
|
28
|
+
if (fs.pathExistsSync(serverConfigPath)) {
|
|
29
|
+
return compatRequire(serverConfigPath);
|
|
30
|
+
}
|
|
31
|
+
return {};
|
|
32
|
+
};
|
|
33
|
+
const loadConfig = ({
|
|
34
|
+
cliConfig,
|
|
35
|
+
serverConfig,
|
|
36
|
+
resolvedConfigPath
|
|
37
|
+
}) => {
|
|
38
|
+
let config = null;
|
|
39
|
+
if (process.env.NODE_ENV === "production") {
|
|
40
|
+
const resolvedConfig = requireConfig(resolvedConfigPath);
|
|
41
|
+
config = mergeDeep(
|
|
42
|
+
__spreadProps(__spreadValues({}, resolvedConfig), {
|
|
43
|
+
plugins: []
|
|
44
|
+
}),
|
|
45
|
+
serverConfig,
|
|
46
|
+
cliConfig
|
|
47
|
+
);
|
|
48
|
+
} else {
|
|
49
|
+
config = mergeDeep(
|
|
50
|
+
__spreadProps(__spreadValues({}, cliConfig), {
|
|
51
|
+
plugins: []
|
|
52
|
+
}),
|
|
53
|
+
serverConfig
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return config;
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
getServerConfigPath,
|
|
60
|
+
loadConfig,
|
|
61
|
+
requireConfig
|
|
62
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
const LOG_LEVEL = {
|
|
18
|
+
error: 0,
|
|
19
|
+
warn: 1,
|
|
20
|
+
info: 2,
|
|
21
|
+
debug: 3,
|
|
22
|
+
log: 4
|
|
23
|
+
};
|
|
24
|
+
const LOG_TYPES = {
|
|
25
|
+
error: {
|
|
26
|
+
color: "red",
|
|
27
|
+
label: "error",
|
|
28
|
+
level: "error"
|
|
29
|
+
},
|
|
30
|
+
info: {
|
|
31
|
+
color: "cyan",
|
|
32
|
+
label: "info",
|
|
33
|
+
level: "info"
|
|
34
|
+
},
|
|
35
|
+
success: {
|
|
36
|
+
color: "green",
|
|
37
|
+
label: "Success",
|
|
38
|
+
level: "info"
|
|
39
|
+
},
|
|
40
|
+
warn: {
|
|
41
|
+
color: "yellow",
|
|
42
|
+
label: "warn",
|
|
43
|
+
level: "warn"
|
|
44
|
+
},
|
|
45
|
+
debug: {
|
|
46
|
+
color: "red",
|
|
47
|
+
label: "debug",
|
|
48
|
+
level: "debug"
|
|
49
|
+
},
|
|
50
|
+
log: { level: "log" }
|
|
51
|
+
};
|
|
52
|
+
const DEFAULT_CONFIG = {
|
|
53
|
+
displayLabel: true,
|
|
54
|
+
uppercaseLabel: false
|
|
55
|
+
};
|
|
56
|
+
class Logger {
|
|
57
|
+
constructor(options = {}) {
|
|
58
|
+
this.level = options.level || LOG_TYPES.log.level;
|
|
59
|
+
this.config = __spreadValues(__spreadValues({}, DEFAULT_CONFIG), options.config || {});
|
|
60
|
+
this.types = __spreadValues(__spreadValues({}, LOG_TYPES), options.types || {});
|
|
61
|
+
this.longestLabel = this.getLongestLabel();
|
|
62
|
+
Object.keys(this.types).forEach((type) => {
|
|
63
|
+
this[type] = this._log.bind(this, type);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
_log(type, message, ...args) {
|
|
67
|
+
if (message === void 0 || message === null) {
|
|
68
|
+
console.log();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (LOG_LEVEL[type] > LOG_LEVEL[this.level]) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
let label = "";
|
|
75
|
+
let text = "";
|
|
76
|
+
const logType = this.types[type];
|
|
77
|
+
if (this.config.displayLabel && logType.label) {
|
|
78
|
+
label = this.config.uppercaseLabel ? logType.label.toUpperCase() : logType.label;
|
|
79
|
+
label = label.padEnd(this.longestLabel.length);
|
|
80
|
+
}
|
|
81
|
+
if (message instanceof Error) {
|
|
82
|
+
if (message.stack) {
|
|
83
|
+
const [name, ...rest] = message.stack.split("\n");
|
|
84
|
+
text = `${name}
|
|
85
|
+
${rest.join("\n")}`;
|
|
86
|
+
} else {
|
|
87
|
+
text = message.message;
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
text = `${message}`;
|
|
91
|
+
}
|
|
92
|
+
const log = label.length > 0 ? `${label} ${text}` : text;
|
|
93
|
+
console.log(log, ...args);
|
|
94
|
+
}
|
|
95
|
+
getLongestLabel() {
|
|
96
|
+
let longestLabel = "";
|
|
97
|
+
Object.keys(this.types).forEach((type) => {
|
|
98
|
+
const { label = "" } = this.types[type];
|
|
99
|
+
if (label.length > longestLabel.length) {
|
|
100
|
+
longestLabel = label;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return longestLabel;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const logger = new Logger();
|
|
107
|
+
logger.Logger = Logger;
|
|
108
|
+
export {
|
|
109
|
+
Logger,
|
|
110
|
+
logger
|
|
111
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
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 { createProxyMiddleware } from "http-proxy-middleware";
|
|
38
|
+
import { debug } from "../utils";
|
|
39
|
+
function formatProxyOptions(proxyOptions) {
|
|
40
|
+
const formattedProxy = [];
|
|
41
|
+
if (!Array.isArray(proxyOptions)) {
|
|
42
|
+
if ("target" in proxyOptions) {
|
|
43
|
+
formattedProxy.push(proxyOptions);
|
|
44
|
+
} else {
|
|
45
|
+
Array.prototype.push.apply(
|
|
46
|
+
formattedProxy,
|
|
47
|
+
Object.keys(proxyOptions).reduce(
|
|
48
|
+
(total, source) => {
|
|
49
|
+
const option = proxyOptions[source];
|
|
50
|
+
total.push(__spreadValues({
|
|
51
|
+
context: source,
|
|
52
|
+
changeOrigin: true,
|
|
53
|
+
logLevel: "warn"
|
|
54
|
+
}, typeof option === "string" ? { target: option } : option));
|
|
55
|
+
return total;
|
|
56
|
+
},
|
|
57
|
+
[]
|
|
58
|
+
)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
formattedProxy.push(...proxyOptions);
|
|
63
|
+
}
|
|
64
|
+
return formattedProxy;
|
|
65
|
+
}
|
|
66
|
+
const createProxyHandler = (proxyOptions) => {
|
|
67
|
+
debug("createProxyHandler", proxyOptions);
|
|
68
|
+
if (!proxyOptions) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const formattedProxy = formatProxyOptions(proxyOptions);
|
|
72
|
+
const middlewares = formattedProxy.map((option) => {
|
|
73
|
+
const middleware = createProxyMiddleware(option.context, option);
|
|
74
|
+
return (ctx, next) => __async(void 0, null, function* () {
|
|
75
|
+
const { req, res } = ctx;
|
|
76
|
+
const bypassUrl = typeof option.bypass === "function" ? option.bypass(req, res, option) : null;
|
|
77
|
+
if (typeof bypassUrl === "boolean") {
|
|
78
|
+
ctx.status = 404;
|
|
79
|
+
return next();
|
|
80
|
+
} else if (typeof bypassUrl === "string") {
|
|
81
|
+
ctx.url = bypassUrl;
|
|
82
|
+
return next();
|
|
83
|
+
}
|
|
84
|
+
middleware(req, res, next);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
return middlewares;
|
|
88
|
+
};
|
|
89
|
+
export {
|
|
90
|
+
createProxyHandler,
|
|
91
|
+
formatProxyOptions
|
|
92
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
3
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
4
|
+
};
|
|
5
|
+
var __async = (__this, __arguments, generator) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
var fulfilled = (value) => {
|
|
8
|
+
try {
|
|
9
|
+
step(generator.next(value));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
reject(e);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var rejected = (value) => {
|
|
15
|
+
try {
|
|
16
|
+
step(generator.throw(value));
|
|
17
|
+
} catch (e) {
|
|
18
|
+
reject(e);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
22
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
import url from "url";
|
|
26
|
+
import { createCache, destroyCache } from "../spr";
|
|
27
|
+
import {
|
|
28
|
+
cacheAddition,
|
|
29
|
+
connectFactor,
|
|
30
|
+
fname,
|
|
31
|
+
maybeSync,
|
|
32
|
+
namespaceHash,
|
|
33
|
+
valueFactory,
|
|
34
|
+
withCoalescedInvoke
|
|
35
|
+
} from "../util";
|
|
36
|
+
var require_cache_fun_test = __commonJS({
|
|
37
|
+
"src/libs/render/cache/__tests__/cache.fun.test.ts"(exports) {
|
|
38
|
+
describe("test spr util functions", () => {
|
|
39
|
+
it("should return value correctly", () => {
|
|
40
|
+
expect(connectFactor("bar", "foo")).toBe("bar-foo");
|
|
41
|
+
expect(fname(1)).toBe("f1");
|
|
42
|
+
expect(namespaceHash("modern", "!@#$%^&")).toBe("modern/!@#$%^&");
|
|
43
|
+
});
|
|
44
|
+
it("should create or destroy instance correctly", () => {
|
|
45
|
+
const ins1 = createCache();
|
|
46
|
+
const ins2 = createCache();
|
|
47
|
+
expect(ins1 === ins2).toBe(true);
|
|
48
|
+
destroyCache();
|
|
49
|
+
const ins3 = createCache();
|
|
50
|
+
expect(ins1 === ins3).toBe(false);
|
|
51
|
+
expect(ins2 === ins3).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
it("should return function correctly", () => {
|
|
54
|
+
const urlParams = (() => new url.URLSearchParams())();
|
|
55
|
+
urlParams.set("name", "modern");
|
|
56
|
+
const getParam = valueFactory(urlParams);
|
|
57
|
+
expect(getParam("name")).toBe("modern");
|
|
58
|
+
const headers = { age: "12345" };
|
|
59
|
+
const getHeader = valueFactory(headers);
|
|
60
|
+
expect(getHeader("age")).toBe("12345");
|
|
61
|
+
});
|
|
62
|
+
it("should add target html content", () => {
|
|
63
|
+
const contentNoHead = "<div>123</div>";
|
|
64
|
+
const html = cacheAddition(contentNoHead, Math.random().toString());
|
|
65
|
+
expect(html).toBe(contentNoHead);
|
|
66
|
+
const contentWithHead = "<head></head><div>123</div>";
|
|
67
|
+
const hash = Math.random().toString();
|
|
68
|
+
const htmlWithHead = cacheAddition(contentWithHead, hash);
|
|
69
|
+
expect(htmlWithHead).toBe(
|
|
70
|
+
`<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
it("should only invoke func one time", () => __async(exports, null, function* () {
|
|
74
|
+
let index = 0;
|
|
75
|
+
const fn = withCoalescedInvoke(
|
|
76
|
+
() => __async(exports, null, function* () {
|
|
77
|
+
return new Promise((resolve) => {
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
index += 1;
|
|
80
|
+
resolve(index);
|
|
81
|
+
}, 500);
|
|
82
|
+
});
|
|
83
|
+
})
|
|
84
|
+
);
|
|
85
|
+
const key = "test";
|
|
86
|
+
const [res1, res2] = yield Promise.all([fn(key, []), fn(key, [])]);
|
|
87
|
+
expect(res1.isOrigin && res2.isOrigin).toBe(false);
|
|
88
|
+
expect(res1.isOrigin || res2.isOrigin).toBe(true);
|
|
89
|
+
expect(res1.value).toBe(1);
|
|
90
|
+
expect(res2.value).toBe(1);
|
|
91
|
+
}));
|
|
92
|
+
it("should invoke sync or async", () => __async(exports, null, function* () {
|
|
93
|
+
const foo = "";
|
|
94
|
+
const async = yield maybeSync(
|
|
95
|
+
() => new Promise((resolve) => {
|
|
96
|
+
setTimeout(() => {
|
|
97
|
+
resolve(foo);
|
|
98
|
+
}, 100);
|
|
99
|
+
})
|
|
100
|
+
)(false);
|
|
101
|
+
expect(async).toBeUndefined();
|
|
102
|
+
const sync = yield maybeSync(
|
|
103
|
+
() => new Promise((resolve) => {
|
|
104
|
+
setTimeout(() => {
|
|
105
|
+
resolve(foo);
|
|
106
|
+
}, 100);
|
|
107
|
+
})
|
|
108
|
+
)(true);
|
|
109
|
+
expect(sync).toBe(foo);
|
|
110
|
+
}));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
export default require_cache_fun_test();
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __spreadValues = (a, b) => {
|
|
8
|
+
for (var prop in b || (b = {}))
|
|
9
|
+
if (__hasOwnProp.call(b, prop))
|
|
10
|
+
__defNormalProp(a, prop, b[prop]);
|
|
11
|
+
if (__getOwnPropSymbols)
|
|
12
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
13
|
+
if (__propIsEnum.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
}
|
|
16
|
+
return a;
|
|
17
|
+
};
|
|
18
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
19
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
20
|
+
};
|
|
21
|
+
var __async = (__this, __arguments, generator) => {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
var fulfilled = (value) => {
|
|
24
|
+
try {
|
|
25
|
+
step(generator.next(value));
|
|
26
|
+
} catch (e) {
|
|
27
|
+
reject(e);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var rejected = (value) => {
|
|
31
|
+
try {
|
|
32
|
+
step(generator.throw(value));
|
|
33
|
+
} catch (e) {
|
|
34
|
+
reject(e);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
38
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
import { createCache, destroyCache } from "../spr";
|
|
42
|
+
import { errorConfiguration } from "./error-configuration";
|
|
43
|
+
import { cacheabelAry } from "./cacheable";
|
|
44
|
+
import { matchedCacheableAry } from "./matched-cache";
|
|
45
|
+
var require_cache_test = __commonJS({
|
|
46
|
+
"src/libs/render/cache/__tests__/cache.test.ts"(exports) {
|
|
47
|
+
const createCacheConfig = (config = {}) => __spreadValues({
|
|
48
|
+
excludes: null,
|
|
49
|
+
includes: null,
|
|
50
|
+
interval: 10,
|
|
51
|
+
staleLimit: false,
|
|
52
|
+
level: 0,
|
|
53
|
+
fallback: false,
|
|
54
|
+
matches: null
|
|
55
|
+
}, config);
|
|
56
|
+
jest.setTimeout(6e4);
|
|
57
|
+
describe("cache", () => {
|
|
58
|
+
it("should cache correctly", () => __async(exports, null, function* () {
|
|
59
|
+
destroyCache();
|
|
60
|
+
const cache = createCache();
|
|
61
|
+
const context = {
|
|
62
|
+
entry: "",
|
|
63
|
+
pathname: "",
|
|
64
|
+
query: {},
|
|
65
|
+
headers: {}
|
|
66
|
+
};
|
|
67
|
+
const content = "hello";
|
|
68
|
+
const cacheConfig = createCacheConfig();
|
|
69
|
+
yield cache.set(context, content, cacheConfig, true);
|
|
70
|
+
const cacheResult = yield cache.get(context);
|
|
71
|
+
expect(cacheResult).not.toBe(null);
|
|
72
|
+
expect(cacheResult == null ? void 0 : cacheResult.content).toBe("hello");
|
|
73
|
+
}));
|
|
74
|
+
it("should ignore cache set when cache config not exist", () => __async(exports, null, function* () {
|
|
75
|
+
destroyCache();
|
|
76
|
+
destroyCache();
|
|
77
|
+
const cache = createCache();
|
|
78
|
+
const context = {
|
|
79
|
+
entry: "",
|
|
80
|
+
pathname: "",
|
|
81
|
+
query: {},
|
|
82
|
+
headers: {}
|
|
83
|
+
};
|
|
84
|
+
const content = "hello";
|
|
85
|
+
const shouldCache = yield cache.set(context, content, null, true);
|
|
86
|
+
expect(shouldCache).toBe(false);
|
|
87
|
+
}));
|
|
88
|
+
it("should calcual cache key error", () => __async(exports, null, function* () {
|
|
89
|
+
destroyCache();
|
|
90
|
+
const cache = createCache();
|
|
91
|
+
const content = "hello";
|
|
92
|
+
for (const config of errorConfiguration) {
|
|
93
|
+
const cacheConfig = createCacheConfig(config);
|
|
94
|
+
const tmpEntry = Math.random().toString();
|
|
95
|
+
const context = {
|
|
96
|
+
entry: tmpEntry,
|
|
97
|
+
pathname: "",
|
|
98
|
+
query: {},
|
|
99
|
+
headers: {}
|
|
100
|
+
};
|
|
101
|
+
const shouldCache = yield cache.set(context, content, cacheConfig);
|
|
102
|
+
expect(shouldCache).toBe(false);
|
|
103
|
+
}
|
|
104
|
+
}));
|
|
105
|
+
it("should get nothing for diff requestKey", () => __async(exports, null, function* () {
|
|
106
|
+
destroyCache();
|
|
107
|
+
const cache = createCache();
|
|
108
|
+
const context = {
|
|
109
|
+
entry: "",
|
|
110
|
+
pathname: "",
|
|
111
|
+
query: {},
|
|
112
|
+
headers: {}
|
|
113
|
+
};
|
|
114
|
+
const content = "hello";
|
|
115
|
+
const cacheConfig = createCacheConfig({
|
|
116
|
+
level: 1,
|
|
117
|
+
includes: { query: ["name"] }
|
|
118
|
+
});
|
|
119
|
+
yield cache.set(context, content, cacheConfig, true);
|
|
120
|
+
const context_req = {
|
|
121
|
+
entry: "",
|
|
122
|
+
pathname: "/home",
|
|
123
|
+
query: {},
|
|
124
|
+
headers: {}
|
|
125
|
+
};
|
|
126
|
+
const cacheResult = yield cache.get(context_req);
|
|
127
|
+
expect(cacheResult).toBe(null);
|
|
128
|
+
}));
|
|
129
|
+
it("should get nothing for diff cacheHash", () => __async(exports, null, function* () {
|
|
130
|
+
destroyCache();
|
|
131
|
+
const cache = createCache();
|
|
132
|
+
const context = {
|
|
133
|
+
entry: "",
|
|
134
|
+
pathname: "",
|
|
135
|
+
query: {},
|
|
136
|
+
headers: {}
|
|
137
|
+
};
|
|
138
|
+
const content = "hello";
|
|
139
|
+
const cacheConfig = createCacheConfig({
|
|
140
|
+
level: 1,
|
|
141
|
+
includes: { query: ["name"] }
|
|
142
|
+
});
|
|
143
|
+
yield cache.set(context, content, cacheConfig, true);
|
|
144
|
+
const context_req = {
|
|
145
|
+
entry: "",
|
|
146
|
+
pathname: "",
|
|
147
|
+
query: { name: "zll" },
|
|
148
|
+
headers: {}
|
|
149
|
+
};
|
|
150
|
+
const cacheResult = yield cache.get(context_req);
|
|
151
|
+
expect(cacheResult).toBe(null);
|
|
152
|
+
}));
|
|
153
|
+
it("should get cache correctly", () => __async(exports, null, function* () {
|
|
154
|
+
destroyCache();
|
|
155
|
+
const cache = createCache();
|
|
156
|
+
for (const cacheable of cacheabelAry) {
|
|
157
|
+
const context = {
|
|
158
|
+
entry: "",
|
|
159
|
+
pathname: cacheable.requestOpt.url,
|
|
160
|
+
query: cacheable.requestOpt.query || {},
|
|
161
|
+
headers: cacheable.requestOpt.headers || {}
|
|
162
|
+
};
|
|
163
|
+
const cacheConfig = createCacheConfig(cacheable.cacheConfig || {});
|
|
164
|
+
yield cache.set(context, cacheable.content, cacheConfig, true);
|
|
165
|
+
const cacheResult = yield cache.get(context);
|
|
166
|
+
expect(cacheResult == null ? void 0 : cacheResult.content).toBe(cacheable.content);
|
|
167
|
+
}
|
|
168
|
+
}));
|
|
169
|
+
it("should match cache correctly", () => __async(exports, null, function* () {
|
|
170
|
+
destroyCache();
|
|
171
|
+
const cache = createCache();
|
|
172
|
+
for (const cacheable of matchedCacheableAry) {
|
|
173
|
+
const [baseCacheable, matchOne, ...other] = cacheable;
|
|
174
|
+
const { requestOpt = {}, cacheConfig, content } = baseCacheable;
|
|
175
|
+
const context = {
|
|
176
|
+
entry: "",
|
|
177
|
+
pathname: requestOpt.url,
|
|
178
|
+
query: requestOpt.query,
|
|
179
|
+
headers: requestOpt.headers
|
|
180
|
+
};
|
|
181
|
+
yield cache.set(context, content, createCacheConfig(cacheConfig), true);
|
|
182
|
+
const matchContext = {
|
|
183
|
+
entry: "",
|
|
184
|
+
pathname: matchOne.url,
|
|
185
|
+
query: matchOne.query,
|
|
186
|
+
headers: matchOne.headers
|
|
187
|
+
};
|
|
188
|
+
const cacheResult = yield cache.get(matchContext);
|
|
189
|
+
expect(cacheResult == null ? void 0 : cacheResult.content).toBe(content);
|
|
190
|
+
for (const notMatch of other) {
|
|
191
|
+
const notMatchContext = {
|
|
192
|
+
entry: "",
|
|
193
|
+
pathname: notMatch.url,
|
|
194
|
+
query: notMatch.query,
|
|
195
|
+
headers: notMatch.headers
|
|
196
|
+
};
|
|
197
|
+
const nothing = yield cache.get(notMatchContext);
|
|
198
|
+
expect(nothing).toBe(null);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}));
|
|
202
|
+
it("should stale cache correctly", () => __async(exports, null, function* () {
|
|
203
|
+
destroyCache();
|
|
204
|
+
const cache = createCache();
|
|
205
|
+
const context = {
|
|
206
|
+
entry: "",
|
|
207
|
+
pathname: "",
|
|
208
|
+
query: {},
|
|
209
|
+
headers: {}
|
|
210
|
+
};
|
|
211
|
+
const config = createCacheConfig({ interval: 5 });
|
|
212
|
+
const content = "hello";
|
|
213
|
+
const shouldCache = yield cache.set(context, content, config, true);
|
|
214
|
+
expect(shouldCache.value).toBe(true);
|
|
215
|
+
const freshResult = yield cache.get(context);
|
|
216
|
+
expect(freshResult == null ? void 0 : freshResult.isStale).toBe(false);
|
|
217
|
+
yield new Promise((resolve) => {
|
|
218
|
+
setTimeout(() => {
|
|
219
|
+
resolve();
|
|
220
|
+
}, 6e3);
|
|
221
|
+
});
|
|
222
|
+
const staleResult = yield cache.get(context);
|
|
223
|
+
expect(staleResult == null ? void 0 : staleResult.isStale).toBe(true);
|
|
224
|
+
}));
|
|
225
|
+
it("should garbage cache correctly", () => __async(exports, null, function* () {
|
|
226
|
+
destroyCache();
|
|
227
|
+
const cache = createCache();
|
|
228
|
+
const context = {
|
|
229
|
+
entry: "",
|
|
230
|
+
pathname: "",
|
|
231
|
+
query: {},
|
|
232
|
+
headers: {}
|
|
233
|
+
};
|
|
234
|
+
const config = createCacheConfig({
|
|
235
|
+
interval: 3,
|
|
236
|
+
staleLimit: 8
|
|
237
|
+
});
|
|
238
|
+
const content = "hello";
|
|
239
|
+
const shouldCache = yield cache.set(context, content, config, true);
|
|
240
|
+
expect(shouldCache.value).toBe(true);
|
|
241
|
+
const freshResult = yield cache.get(context);
|
|
242
|
+
expect(freshResult == null ? void 0 : freshResult.isGarbage).toBe(false);
|
|
243
|
+
yield new Promise((resolve) => {
|
|
244
|
+
setTimeout(() => {
|
|
245
|
+
resolve();
|
|
246
|
+
}, 1e4);
|
|
247
|
+
});
|
|
248
|
+
const staleResult = yield cache.get(context);
|
|
249
|
+
expect(staleResult == null ? void 0 : staleResult.isGarbage).toBe(true);
|
|
250
|
+
}));
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
export default require_cache_test();
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const cacheabelAry = [
|
|
2
|
+
{
|
|
3
|
+
requestOpt: { url: "/" },
|
|
4
|
+
cacheConfig: {},
|
|
5
|
+
content: "level0"
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
requestOpt: {
|
|
9
|
+
url: "/level-one",
|
|
10
|
+
query: { name: "modern" }
|
|
11
|
+
},
|
|
12
|
+
cacheConfig: {
|
|
13
|
+
level: 1,
|
|
14
|
+
includes: { query: ["name"] }
|
|
15
|
+
},
|
|
16
|
+
content: "level1"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
requestOpt: {
|
|
20
|
+
url: "/level-two",
|
|
21
|
+
headers: { age: "18" }
|
|
22
|
+
},
|
|
23
|
+
cacheConfig: {
|
|
24
|
+
level: 2,
|
|
25
|
+
includes: { header: ["age"] }
|
|
26
|
+
},
|
|
27
|
+
content: "level2"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
requestOpt: {
|
|
31
|
+
url: "/level-three",
|
|
32
|
+
query: { name: "modern" },
|
|
33
|
+
headers: { age: "18" }
|
|
34
|
+
},
|
|
35
|
+
cacheConfig: {
|
|
36
|
+
level: 3,
|
|
37
|
+
includes: {
|
|
38
|
+
query: ["name"],
|
|
39
|
+
header: ["age"]
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
content: "level3"
|
|
43
|
+
}
|
|
44
|
+
];
|
|
45
|
+
export {
|
|
46
|
+
cacheabelAry
|
|
47
|
+
};
|