@modern-js/prod-server 2.0.0-beta.3 → 2.0.0-beta.6
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 +139 -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 +44 -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 +150 -145
- package/dist/js/modern/server/modern-server-split.js +46 -12
- package/dist/js/modern/server/modern-server.js +384 -422
- 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 +53 -30
- package/dist/js/node/index.js +37 -57
- package/dist/js/node/libs/context/context.js +83 -94
- package/dist/js/node/libs/context/index.js +28 -13
- package/dist/js/node/libs/hook-api/index.js +76 -48
- package/dist/js/node/libs/hook-api/route.js +26 -11
- package/dist/js/node/libs/hook-api/template.js +41 -39
- package/dist/js/node/libs/loadConfig.js +73 -35
- package/dist/js/node/libs/metrics.js +25 -9
- package/dist/js/node/libs/proxy.js +89 -44
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -56
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -128
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
- package/dist/js/node/libs/render/cache/index.js +110 -64
- package/dist/js/node/libs/render/cache/page-caches/index.js +54 -14
- package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -12
- package/dist/js/node/libs/render/cache/spr.js +161 -129
- package/dist/js/node/libs/render/cache/type.js +15 -5
- package/dist/js/node/libs/render/cache/util.js +99 -45
- package/dist/js/node/libs/render/index.js +106 -67
- package/dist/js/node/libs/render/measure.js +58 -31
- package/dist/js/node/libs/render/reader.js +95 -70
- package/dist/js/node/libs/render/ssr.js +80 -47
- package/dist/js/node/libs/render/static.js +79 -40
- package/dist/js/node/libs/render/type.js +31 -12
- package/dist/js/node/libs/route/index.js +31 -26
- package/dist/js/node/libs/route/matcher.js +40 -41
- package/dist/js/node/libs/route/route.js +29 -22
- package/dist/js/node/libs/serve-file.js +66 -32
- package/dist/js/node/server/index.js +168 -160
- package/dist/js/node/server/modern-server-split.js +72 -22
- package/dist/js/node/server/modern-server.js +403 -445
- package/dist/js/node/type.js +15 -3
- package/dist/js/node/utils.js +85 -52
- package/dist/js/node/worker-server.js +57 -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 +267 -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 +493 -327
- package/dist/js/treeshaking/server/modern-server-split.js +352 -144
- package/dist/js/treeshaking/server/modern-server.js +1048 -909
- 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,18 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
import { Transform } from 'stream';
|
|
1
|
+
import { Transform } from "stream";
|
|
3
2
|
const RegList = {
|
|
4
3
|
before: {
|
|
5
|
-
head:
|
|
6
|
-
body:
|
|
4
|
+
head: "<head>",
|
|
5
|
+
body: "<body>"
|
|
7
6
|
},
|
|
8
7
|
after: {
|
|
9
|
-
head:
|
|
10
|
-
body:
|
|
8
|
+
head: "</head>",
|
|
9
|
+
body: "</body>"
|
|
11
10
|
}
|
|
12
11
|
};
|
|
13
|
-
|
|
12
|
+
class TemplateAPI {
|
|
14
13
|
constructor(content) {
|
|
15
|
-
_defineProperty(this, "content", void 0);
|
|
16
14
|
this.content = content;
|
|
17
15
|
}
|
|
18
16
|
get() {
|
|
@@ -22,34 +20,26 @@ export class TemplateAPI {
|
|
|
22
20
|
this.content = content;
|
|
23
21
|
}
|
|
24
22
|
prependHead(fragment) {
|
|
25
|
-
const {
|
|
26
|
-
head
|
|
27
|
-
} = RegList.before;
|
|
23
|
+
const { head } = RegList.before;
|
|
28
24
|
return this.replace(head, `${head}${fragment}`);
|
|
29
25
|
}
|
|
30
26
|
appendHead(fragment) {
|
|
31
|
-
const {
|
|
32
|
-
head
|
|
33
|
-
} = RegList.after;
|
|
27
|
+
const { head } = RegList.after;
|
|
34
28
|
return this.replace(head, `${fragment}${head}`);
|
|
35
29
|
}
|
|
36
30
|
prependBody(fragment) {
|
|
37
|
-
const {
|
|
38
|
-
body
|
|
39
|
-
} = RegList.before;
|
|
31
|
+
const { body } = RegList.before;
|
|
40
32
|
return this.replace(body, `${body}${fragment}`);
|
|
41
33
|
}
|
|
42
34
|
appendBody(fragment) {
|
|
43
|
-
const {
|
|
44
|
-
body
|
|
45
|
-
} = RegList.after;
|
|
35
|
+
const { body } = RegList.after;
|
|
46
36
|
return this.replace(body, `${fragment}${body}`);
|
|
47
37
|
}
|
|
48
38
|
replace(reg, text) {
|
|
49
39
|
this.content = this.content.replace(reg, text);
|
|
50
40
|
}
|
|
51
41
|
}
|
|
52
|
-
|
|
42
|
+
const templateInjectableStream = ({
|
|
53
43
|
prependHead,
|
|
54
44
|
appendHead,
|
|
55
45
|
prependBody,
|
|
@@ -58,30 +48,26 @@ export const templateInjectableStream = ({
|
|
|
58
48
|
write(chunk, _, callback) {
|
|
59
49
|
let chunk_str = chunk.toString();
|
|
60
50
|
if (prependHead) {
|
|
61
|
-
const {
|
|
62
|
-
head
|
|
63
|
-
} = RegList.before;
|
|
51
|
+
const { head } = RegList.before;
|
|
64
52
|
chunk_str = chunk_str.replace(head, `${head}${prependHead}`);
|
|
65
53
|
}
|
|
66
54
|
if (appendHead) {
|
|
67
|
-
const {
|
|
68
|
-
head
|
|
69
|
-
} = RegList.after;
|
|
55
|
+
const { head } = RegList.after;
|
|
70
56
|
chunk_str = chunk_str.replace(head, `${appendHead}${head}`);
|
|
71
57
|
}
|
|
72
58
|
if (prependBody) {
|
|
73
|
-
const {
|
|
74
|
-
body
|
|
75
|
-
} = RegList.before;
|
|
59
|
+
const { body } = RegList.before;
|
|
76
60
|
chunk_str = chunk_str.replace(body, `${body}${prependBody}`);
|
|
77
61
|
}
|
|
78
62
|
if (appendBody) {
|
|
79
|
-
const {
|
|
80
|
-
body
|
|
81
|
-
} = RegList.after;
|
|
63
|
+
const { body } = RegList.after;
|
|
82
64
|
chunk_str = chunk_str.replace(body, `${appendBody}${body}`);
|
|
83
65
|
}
|
|
84
66
|
this.push(chunk_str);
|
|
85
67
|
callback();
|
|
86
68
|
}
|
|
87
|
-
});
|
|
69
|
+
});
|
|
70
|
+
export {
|
|
71
|
+
TemplateAPI,
|
|
72
|
+
templateInjectableStream
|
|
73
|
+
};
|
|
@@ -1,41 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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) => {
|
|
8
24
|
const serverConfigPath = path.join(distDirectory, serverConfigFile);
|
|
9
25
|
return `${serverConfigPath}.js`;
|
|
10
26
|
};
|
|
11
|
-
|
|
27
|
+
const requireConfig = (serverConfigPath) => {
|
|
12
28
|
if (fs.pathExistsSync(serverConfigPath)) {
|
|
13
29
|
return compatRequire(serverConfigPath);
|
|
14
30
|
}
|
|
15
31
|
return {};
|
|
16
32
|
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 对配置进行合并,开发环境下,cliConfig 与 serverConfig 进行深合并
|
|
20
|
-
* 生产环境下,resolvedConfig 与 serverConfig 进行深合并
|
|
21
|
-
* resolvedConfigPath: 构建序列化后的 modern.config.js 文件路径
|
|
22
|
-
*/
|
|
23
|
-
export const loadConfig = ({
|
|
33
|
+
const loadConfig = ({
|
|
24
34
|
cliConfig,
|
|
25
35
|
serverConfig,
|
|
26
36
|
resolvedConfigPath
|
|
27
37
|
}) => {
|
|
28
38
|
let config = null;
|
|
29
|
-
if (process.env.NODE_ENV ===
|
|
39
|
+
if (process.env.NODE_ENV === "production") {
|
|
30
40
|
const resolvedConfig = requireConfig(resolvedConfigPath);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
41
|
+
config = mergeDeep(
|
|
42
|
+
__spreadProps(__spreadValues({}, resolvedConfig), {
|
|
43
|
+
plugins: []
|
|
44
|
+
}),
|
|
45
|
+
serverConfig,
|
|
46
|
+
cliConfig
|
|
47
|
+
);
|
|
35
48
|
} else {
|
|
36
|
-
config = mergeDeep(
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
config = mergeDeep(
|
|
50
|
+
__spreadProps(__spreadValues({}, cliConfig), {
|
|
51
|
+
plugins: []
|
|
52
|
+
}),
|
|
53
|
+
serverConfig
|
|
54
|
+
);
|
|
39
55
|
}
|
|
40
56
|
return config;
|
|
41
|
-
};
|
|
57
|
+
};
|
|
58
|
+
export {
|
|
59
|
+
getServerConfigPath,
|
|
60
|
+
loadConfig,
|
|
61
|
+
requireConfig
|
|
62
|
+
};
|
|
@@ -1,61 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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";
|
|
5
38
|
import { debug } from "../utils";
|
|
6
|
-
|
|
39
|
+
function formatProxyOptions(proxyOptions) {
|
|
7
40
|
const formattedProxy = [];
|
|
8
41
|
if (!Array.isArray(proxyOptions)) {
|
|
9
|
-
if (
|
|
42
|
+
if ("target" in proxyOptions) {
|
|
10
43
|
formattedProxy.push(proxyOptions);
|
|
11
44
|
} else {
|
|
12
|
-
Array.prototype.push.apply(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
+
);
|
|
23
60
|
}
|
|
24
61
|
} else {
|
|
25
62
|
formattedProxy.push(...proxyOptions);
|
|
26
63
|
}
|
|
27
64
|
return formattedProxy;
|
|
28
65
|
}
|
|
29
|
-
|
|
30
|
-
debug(
|
|
66
|
+
const createProxyHandler = (proxyOptions) => {
|
|
67
|
+
debug("createProxyHandler", proxyOptions);
|
|
31
68
|
if (!proxyOptions) {
|
|
32
69
|
return null;
|
|
33
70
|
}
|
|
34
|
-
|
|
35
|
-
// If it is not an array, it may be an object that uses the context attribute
|
|
36
|
-
// or an object in the form of { source: ProxyDetail }
|
|
37
71
|
const formattedProxy = formatProxyOptions(proxyOptions);
|
|
38
|
-
const middlewares = formattedProxy.map(option => {
|
|
72
|
+
const middlewares = formattedProxy.map((option) => {
|
|
39
73
|
const middleware = createProxyMiddleware(option.context, option);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
req,
|
|
45
|
-
res
|
|
46
|
-
} = ctx;
|
|
47
|
-
const bypassUrl = typeof option.bypass === 'function' ? option.bypass(req, res, option) : null;
|
|
48
|
-
|
|
49
|
-
// only false, no true
|
|
50
|
-
if (typeof bypassUrl === 'boolean') {
|
|
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") {
|
|
51
78
|
ctx.status = 404;
|
|
52
79
|
return next();
|
|
53
|
-
} else if (typeof bypassUrl ===
|
|
80
|
+
} else if (typeof bypassUrl === "string") {
|
|
54
81
|
ctx.url = bypassUrl;
|
|
55
82
|
return next();
|
|
56
83
|
}
|
|
57
84
|
middleware(req, res, next);
|
|
58
|
-
};
|
|
85
|
+
});
|
|
59
86
|
});
|
|
60
87
|
return middlewares;
|
|
61
|
-
};
|
|
88
|
+
};
|
|
89
|
+
export {
|
|
90
|
+
createProxyHandler,
|
|
91
|
+
formatProxyOptions
|
|
92
|
+
};
|
|
@@ -1,69 +1,114 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
expect(ins1 === ins2).toBe(true);
|
|
14
|
-
destroyCache();
|
|
15
|
-
const ins3 = createCache();
|
|
16
|
-
expect(ins1 === ins3).toBe(false);
|
|
17
|
-
expect(ins2 === ins3).toBe(false);
|
|
18
|
-
});
|
|
19
|
-
it('should return function correctly', () => {
|
|
20
|
-
const urlParams = (() => new url.URLSearchParams())();
|
|
21
|
-
urlParams.set('name', 'modern');
|
|
22
|
-
const getParam = valueFactory(urlParams);
|
|
23
|
-
expect(getParam('name')).toBe('modern');
|
|
24
|
-
const headers = {
|
|
25
|
-
age: '12345'
|
|
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
|
+
}
|
|
26
13
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const htmlWithHead = cacheAddition(contentWithHead, hash);
|
|
37
|
-
expect(htmlWithHead).toBe(`<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`);
|
|
38
|
-
});
|
|
39
|
-
it('should only invoke func one time', async () => {
|
|
40
|
-
let index = 0;
|
|
41
|
-
const fn = withCoalescedInvoke(async () => new Promise(resolve => {
|
|
42
|
-
setTimeout(() => {
|
|
43
|
-
index += 1;
|
|
44
|
-
resolve(index);
|
|
45
|
-
}, 500);
|
|
46
|
-
}));
|
|
47
|
-
const key = 'test';
|
|
48
|
-
const [res1, res2] = await Promise.all([fn(key, []), fn(key, [])]);
|
|
49
|
-
expect(res1.isOrigin && res2.isOrigin).toBe(false);
|
|
50
|
-
expect(res1.isOrigin || res2.isOrigin).toBe(true);
|
|
51
|
-
expect(res1.value).toBe(1);
|
|
52
|
-
expect(res2.value).toBe(1);
|
|
53
|
-
});
|
|
54
|
-
it('should invoke sync or async', async () => {
|
|
55
|
-
const foo = '';
|
|
56
|
-
const async = await maybeSync(() => new Promise(resolve => {
|
|
57
|
-
setTimeout(() => {
|
|
58
|
-
resolve(foo);
|
|
59
|
-
}, 100);
|
|
60
|
-
}))(false);
|
|
61
|
-
expect(async).toBeUndefined();
|
|
62
|
-
const sync = await maybeSync(() => new Promise(resolve => {
|
|
63
|
-
setTimeout(() => {
|
|
64
|
-
resolve(foo);
|
|
65
|
-
}, 100);
|
|
66
|
-
}))(true);
|
|
67
|
-
expect(sync).toBe(foo);
|
|
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());
|
|
68
23
|
});
|
|
69
|
-
}
|
|
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();
|