@modern-js/prod-server 2.4.1-beta.0 → 2.5.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 +18 -3
- package/dist/cjs/constants.js +62 -0
- package/dist/cjs/index.js +44 -0
- package/dist/cjs/libs/context/context.js +189 -0
- package/dist/cjs/libs/context/index.js +30 -0
- package/dist/cjs/libs/hook-api/index.js +151 -0
- package/dist/cjs/libs/hook-api/route.js +43 -0
- package/dist/cjs/libs/hook-api/template.js +97 -0
- package/dist/cjs/libs/loadConfig.js +76 -0
- package/dist/cjs/libs/logger.js +122 -0
- package/dist/cjs/libs/metrics.js +34 -0
- package/dist/cjs/libs/proxy.js +81 -0
- package/dist/cjs/libs/render/cache/__tests__/cache.fun.test.js +93 -0
- package/dist/cjs/libs/render/cache/__tests__/cache.test.js +210 -0
- package/dist/cjs/libs/render/cache/__tests__/cacheable.js +70 -0
- package/dist/cjs/libs/render/cache/__tests__/error-configuration.js +60 -0
- package/dist/cjs/libs/render/cache/__tests__/matched-cache.js +114 -0
- package/dist/cjs/libs/render/cache/index.js +97 -0
- package/dist/cjs/libs/render/cache/page-caches/index.js +33 -0
- package/dist/cjs/libs/render/cache/page-caches/lru.js +58 -0
- package/dist/cjs/libs/render/cache/spr.js +242 -0
- package/dist/cjs/libs/render/cache/type.js +15 -0
- package/dist/cjs/libs/render/cache/util.js +116 -0
- package/dist/cjs/libs/render/index.js +93 -0
- package/dist/cjs/libs/render/measure.js +75 -0
- package/dist/cjs/libs/render/reader.js +118 -0
- package/dist/cjs/libs/render/ssr.js +103 -0
- package/dist/cjs/libs/render/static.js +67 -0
- package/dist/cjs/libs/render/type.js +32 -0
- package/dist/cjs/libs/route/index.js +78 -0
- package/dist/cjs/libs/route/matcher.js +106 -0
- package/dist/cjs/libs/route/route.js +39 -0
- package/dist/cjs/libs/serve-file.js +77 -0
- package/dist/cjs/server/index.js +169 -0
- package/dist/cjs/server/modern-server-split.js +66 -0
- package/dist/cjs/server/modern-server.js +491 -0
- package/dist/cjs/type.js +15 -0
- package/dist/cjs/utils.js +152 -0
- package/dist/cjs/worker-server.js +93 -0
- package/dist/esm/constants.js +29 -0
- package/dist/esm/index.js +13 -0
- package/dist/esm/libs/context/context.js +274 -0
- package/dist/esm/libs/context/index.js +5 -0
- package/dist/esm/libs/hook-api/index.js +281 -0
- package/dist/esm/libs/hook-api/route.js +68 -0
- package/dist/esm/libs/hook-api/template.js +127 -0
- package/dist/esm/libs/loadConfig.js +82 -0
- package/dist/esm/libs/logger.js +205 -0
- package/dist/esm/libs/metrics.js +6 -0
- package/dist/esm/libs/proxy.js +244 -0
- package/dist/esm/libs/render/cache/__tests__/cache.fun.test.js +291 -0
- package/dist/esm/libs/render/cache/__tests__/cache.test.js +781 -0
- package/dist/esm/libs/render/cache/__tests__/cacheable.js +67 -0
- package/dist/esm/libs/render/cache/__tests__/error-configuration.js +45 -0
- package/dist/esm/libs/render/cache/__tests__/matched-cache.js +147 -0
- package/dist/esm/libs/render/cache/index.js +346 -0
- package/dist/esm/libs/render/cache/page-caches/index.js +154 -0
- package/dist/esm/libs/render/cache/page-caches/lru.js +84 -0
- package/dist/esm/libs/render/cache/spr.js +492 -0
- package/dist/esm/libs/render/cache/type.js +1 -0
- package/dist/esm/libs/render/cache/util.js +280 -0
- package/dist/esm/libs/render/index.js +234 -0
- package/dist/esm/libs/render/measure.js +146 -0
- package/dist/esm/libs/render/reader.js +339 -0
- package/dist/esm/libs/render/ssr.js +223 -0
- package/dist/esm/libs/render/static.js +216 -0
- package/dist/esm/libs/render/type.js +7 -0
- package/dist/esm/libs/route/index.js +130 -0
- package/dist/esm/libs/route/matcher.js +143 -0
- package/dist/esm/libs/route/route.js +40 -0
- package/dist/esm/libs/serve-file.js +184 -0
- package/dist/esm/server/index.js +505 -0
- package/dist/esm/server/modern-server-split.js +360 -0
- package/dist/esm/server/modern-server.js +1090 -0
- package/dist/esm/type.js +1 -0
- package/dist/esm/utils.js +147 -0
- package/dist/esm/worker-server.js +233 -0
- package/dist/esm-node/constants.js +35 -0
- package/dist/esm-node/index.js +18 -0
- package/dist/esm-node/libs/context/context.js +160 -0
- package/dist/esm-node/libs/context/index.js +6 -0
- package/dist/esm-node/libs/hook-api/index.js +119 -0
- package/dist/esm-node/libs/hook-api/route.js +20 -0
- package/dist/esm-node/libs/hook-api/template.js +73 -0
- package/dist/esm-node/libs/loadConfig.js +45 -0
- package/dist/esm-node/libs/logger.js +98 -0
- package/dist/esm-node/libs/metrics.js +11 -0
- package/dist/esm-node/libs/proxy.js +57 -0
- package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +83 -0
- package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +210 -0
- package/dist/esm-node/libs/render/cache/__tests__/cacheable.js +47 -0
- package/dist/esm-node/libs/render/cache/__tests__/error-configuration.js +37 -0
- package/dist/esm-node/libs/render/cache/__tests__/matched-cache.js +91 -0
- package/dist/esm-node/libs/render/cache/index.js +76 -0
- package/dist/esm-node/libs/render/cache/page-caches/index.js +10 -0
- package/dist/esm-node/libs/render/cache/page-caches/lru.js +29 -0
- package/dist/esm-node/libs/render/cache/spr.js +220 -0
- package/dist/esm-node/libs/render/cache/type.js +0 -0
- package/dist/esm-node/libs/render/cache/util.js +80 -0
- package/dist/esm-node/libs/render/index.js +64 -0
- package/dist/esm-node/libs/render/measure.js +51 -0
- package/dist/esm-node/libs/render/reader.js +85 -0
- package/dist/esm-node/libs/render/ssr.js +80 -0
- package/dist/esm-node/libs/render/static.js +38 -0
- package/dist/esm-node/libs/render/type.js +9 -0
- package/dist/esm-node/libs/route/index.js +54 -0
- package/dist/esm-node/libs/route/matcher.js +87 -0
- package/dist/esm-node/libs/route/route.js +16 -0
- package/dist/esm-node/libs/serve-file.js +47 -0
- package/dist/esm-node/server/index.js +156 -0
- package/dist/esm-node/server/modern-server-split.js +43 -0
- package/dist/esm-node/server/modern-server.js +484 -0
- package/dist/esm-node/type.js +0 -0
- package/dist/esm-node/utils.js +120 -0
- package/dist/esm-node/worker-server.js +69 -0
- package/dist/js/modern/libs/render/index.js +4 -2
- package/dist/js/modern/server/modern-server.js +14 -8
- package/dist/js/node/libs/render/index.js +4 -2
- package/dist/js/node/server/modern-server.js +14 -8
- package/dist/js/treeshaking/libs/render/index.js +4 -3
- package/dist/js/treeshaking/server/modern-server.js +14 -8
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/render/index.d.ts +3 -1
- package/dist/types/utils.d.ts +1 -1
- package/package.json +17 -18
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import cookie from "cookie";
|
|
2
|
+
import { RouteAPI } from "./route";
|
|
3
|
+
import { TemplateAPI } from "./template";
|
|
4
|
+
class Response {
|
|
5
|
+
constructor(res) {
|
|
6
|
+
this.res = res;
|
|
7
|
+
this._cookie = cookie.parse(res.getHeader("set-cookie") || "");
|
|
8
|
+
this.cookies = {
|
|
9
|
+
get: this.getCookie.bind(this),
|
|
10
|
+
set: this.setCookie.bind(this),
|
|
11
|
+
delete: this.deleteCookie.bind(this),
|
|
12
|
+
clear: this.clearCookie.bind(this),
|
|
13
|
+
apply: this.applyCookie.bind(this)
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
get(key) {
|
|
17
|
+
return this.res.getHeader(key);
|
|
18
|
+
}
|
|
19
|
+
set(key, value) {
|
|
20
|
+
return this.res.setHeader(key, value);
|
|
21
|
+
}
|
|
22
|
+
status(code) {
|
|
23
|
+
this.res.statusCode = code;
|
|
24
|
+
}
|
|
25
|
+
getCookie(key) {
|
|
26
|
+
return this._cookie[key];
|
|
27
|
+
}
|
|
28
|
+
setCookie(key, value) {
|
|
29
|
+
this._cookie[key] = value;
|
|
30
|
+
}
|
|
31
|
+
deleteCookie(key) {
|
|
32
|
+
if (this._cookie[key]) {
|
|
33
|
+
delete this._cookie[key];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
clearCookie() {
|
|
37
|
+
this._cookie = {};
|
|
38
|
+
}
|
|
39
|
+
applyCookie() {
|
|
40
|
+
const str = Object.entries(this._cookie).map(([key, value]) => {
|
|
41
|
+
return cookie.serialize(key, value);
|
|
42
|
+
}).join("; ");
|
|
43
|
+
if (str) {
|
|
44
|
+
this.res.setHeader("set-cookie", str);
|
|
45
|
+
} else {
|
|
46
|
+
this.res.removeHeader("set-cookie");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
raw(body, options) {
|
|
50
|
+
const { status, headers = {} } = options || {};
|
|
51
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
52
|
+
this.res.setHeader(key, value);
|
|
53
|
+
});
|
|
54
|
+
if (status) {
|
|
55
|
+
this.res.statusCode = status;
|
|
56
|
+
}
|
|
57
|
+
this.res.end(body);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
class Request {
|
|
61
|
+
constructor(ctx) {
|
|
62
|
+
this.url = ctx.url;
|
|
63
|
+
this.host = ctx.host;
|
|
64
|
+
this.pathname = ctx.path;
|
|
65
|
+
this.query = ctx.query;
|
|
66
|
+
this.headers = ctx.headers;
|
|
67
|
+
this.cookie = ctx.headers.cookie || "";
|
|
68
|
+
this._cookie = cookie.parse(this.cookie);
|
|
69
|
+
this.cookies = {
|
|
70
|
+
get: this.getCookie.bind(this)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
getCookie(key) {
|
|
74
|
+
return this._cookie[key];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const base = (context) => {
|
|
78
|
+
const { res } = context;
|
|
79
|
+
return {
|
|
80
|
+
response: new Response(res),
|
|
81
|
+
request: new Request(context),
|
|
82
|
+
logger: context.logger,
|
|
83
|
+
metrics: context.metrics
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
const createAfterMatchContext = (context, entryName) => {
|
|
87
|
+
const baseContext = base(context);
|
|
88
|
+
return {
|
|
89
|
+
...baseContext,
|
|
90
|
+
router: new RouteAPI(entryName)
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
const createAfterRenderContext = (context, content) => {
|
|
94
|
+
const baseContext = base(context);
|
|
95
|
+
return {
|
|
96
|
+
...baseContext,
|
|
97
|
+
template: new TemplateAPI(content)
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
const createMiddlewareContext = (context) => {
|
|
101
|
+
const baseContext = base(context);
|
|
102
|
+
return {
|
|
103
|
+
...baseContext,
|
|
104
|
+
response: {
|
|
105
|
+
...baseContext.response,
|
|
106
|
+
locals: context.res.locals || {}
|
|
107
|
+
},
|
|
108
|
+
source: {
|
|
109
|
+
req: context.req,
|
|
110
|
+
res: context.res
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
export {
|
|
115
|
+
base,
|
|
116
|
+
createAfterMatchContext,
|
|
117
|
+
createAfterRenderContext,
|
|
118
|
+
createMiddlewareContext
|
|
119
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class RouteAPI {
|
|
2
|
+
constructor(entryName) {
|
|
3
|
+
this.current = entryName;
|
|
4
|
+
this.status = 200;
|
|
5
|
+
this.url = "";
|
|
6
|
+
}
|
|
7
|
+
redirect(url, status = 302) {
|
|
8
|
+
this.url = url;
|
|
9
|
+
this.status = status;
|
|
10
|
+
}
|
|
11
|
+
rewrite(entryName) {
|
|
12
|
+
this.current = entryName;
|
|
13
|
+
}
|
|
14
|
+
use(entryName) {
|
|
15
|
+
this.rewrite(entryName);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
RouteAPI
|
|
20
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Transform } from "stream";
|
|
2
|
+
const RegList = {
|
|
3
|
+
before: {
|
|
4
|
+
head: "<head>",
|
|
5
|
+
body: "<body>"
|
|
6
|
+
},
|
|
7
|
+
after: {
|
|
8
|
+
head: "</head>",
|
|
9
|
+
body: "</body>"
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
class TemplateAPI {
|
|
13
|
+
constructor(content) {
|
|
14
|
+
this.content = content;
|
|
15
|
+
}
|
|
16
|
+
get() {
|
|
17
|
+
return this.content;
|
|
18
|
+
}
|
|
19
|
+
set(content) {
|
|
20
|
+
this.content = content;
|
|
21
|
+
}
|
|
22
|
+
prependHead(fragment) {
|
|
23
|
+
const { head } = RegList.before;
|
|
24
|
+
return this.replace(head, `${head}${fragment}`);
|
|
25
|
+
}
|
|
26
|
+
appendHead(fragment) {
|
|
27
|
+
const { head } = RegList.after;
|
|
28
|
+
return this.replace(head, `${fragment}${head}`);
|
|
29
|
+
}
|
|
30
|
+
prependBody(fragment) {
|
|
31
|
+
const { body } = RegList.before;
|
|
32
|
+
return this.replace(body, `${body}${fragment}`);
|
|
33
|
+
}
|
|
34
|
+
appendBody(fragment) {
|
|
35
|
+
const { body } = RegList.after;
|
|
36
|
+
return this.replace(body, `${fragment}${body}`);
|
|
37
|
+
}
|
|
38
|
+
replace(reg, text) {
|
|
39
|
+
this.content = this.content.replace(reg, text);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const templateInjectableStream = ({
|
|
43
|
+
prependHead,
|
|
44
|
+
appendHead,
|
|
45
|
+
prependBody,
|
|
46
|
+
appendBody
|
|
47
|
+
}) => new Transform({
|
|
48
|
+
write(chunk, _, callback) {
|
|
49
|
+
let chunk_str = chunk.toString();
|
|
50
|
+
if (prependHead) {
|
|
51
|
+
const { head } = RegList.before;
|
|
52
|
+
chunk_str = chunk_str.replace(head, `${head}${prependHead}`);
|
|
53
|
+
}
|
|
54
|
+
if (appendHead) {
|
|
55
|
+
const { head } = RegList.after;
|
|
56
|
+
chunk_str = chunk_str.replace(head, `${appendHead}${head}`);
|
|
57
|
+
}
|
|
58
|
+
if (prependBody) {
|
|
59
|
+
const { body } = RegList.before;
|
|
60
|
+
chunk_str = chunk_str.replace(body, `${body}${prependBody}`);
|
|
61
|
+
}
|
|
62
|
+
if (appendBody) {
|
|
63
|
+
const { body } = RegList.after;
|
|
64
|
+
chunk_str = chunk_str.replace(body, `${appendBody}${body}`);
|
|
65
|
+
}
|
|
66
|
+
this.push(chunk_str);
|
|
67
|
+
callback();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
export {
|
|
71
|
+
TemplateAPI,
|
|
72
|
+
templateInjectableStream
|
|
73
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import { compatRequire, fs, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
|
|
3
|
+
import mergeDeep from "merge-deep";
|
|
4
|
+
const getServerConfigPath = (distDirectory, serverConfigFile = DEFAULT_SERVER_CONFIG) => {
|
|
5
|
+
const serverConfigPath = path.join(distDirectory, serverConfigFile);
|
|
6
|
+
return `${serverConfigPath}.js`;
|
|
7
|
+
};
|
|
8
|
+
const requireConfig = (serverConfigPath) => {
|
|
9
|
+
if (fs.pathExistsSync(serverConfigPath)) {
|
|
10
|
+
return compatRequire(serverConfigPath);
|
|
11
|
+
}
|
|
12
|
+
return {};
|
|
13
|
+
};
|
|
14
|
+
const loadConfig = ({
|
|
15
|
+
cliConfig,
|
|
16
|
+
serverConfig,
|
|
17
|
+
resolvedConfigPath
|
|
18
|
+
}) => {
|
|
19
|
+
let config = null;
|
|
20
|
+
if (process.env.NODE_ENV === "production") {
|
|
21
|
+
const resolvedConfig = requireConfig(resolvedConfigPath);
|
|
22
|
+
config = mergeDeep(
|
|
23
|
+
{
|
|
24
|
+
...resolvedConfig,
|
|
25
|
+
plugins: []
|
|
26
|
+
},
|
|
27
|
+
serverConfig,
|
|
28
|
+
cliConfig
|
|
29
|
+
);
|
|
30
|
+
} else {
|
|
31
|
+
config = mergeDeep(
|
|
32
|
+
{
|
|
33
|
+
...cliConfig,
|
|
34
|
+
plugins: []
|
|
35
|
+
},
|
|
36
|
+
serverConfig
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return config;
|
|
40
|
+
};
|
|
41
|
+
export {
|
|
42
|
+
getServerConfigPath,
|
|
43
|
+
loadConfig,
|
|
44
|
+
requireConfig
|
|
45
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const LOG_LEVEL = {
|
|
2
|
+
error: 0,
|
|
3
|
+
warn: 1,
|
|
4
|
+
info: 2,
|
|
5
|
+
debug: 3,
|
|
6
|
+
log: 4
|
|
7
|
+
};
|
|
8
|
+
const LOG_TYPES = {
|
|
9
|
+
error: {
|
|
10
|
+
color: "red",
|
|
11
|
+
label: "error",
|
|
12
|
+
level: "error"
|
|
13
|
+
},
|
|
14
|
+
info: {
|
|
15
|
+
color: "cyan",
|
|
16
|
+
label: "info",
|
|
17
|
+
level: "info"
|
|
18
|
+
},
|
|
19
|
+
success: {
|
|
20
|
+
color: "green",
|
|
21
|
+
label: "Success",
|
|
22
|
+
level: "info"
|
|
23
|
+
},
|
|
24
|
+
warn: {
|
|
25
|
+
color: "yellow",
|
|
26
|
+
label: "warn",
|
|
27
|
+
level: "warn"
|
|
28
|
+
},
|
|
29
|
+
debug: {
|
|
30
|
+
color: "red",
|
|
31
|
+
label: "debug",
|
|
32
|
+
level: "debug"
|
|
33
|
+
},
|
|
34
|
+
log: { level: "log" }
|
|
35
|
+
};
|
|
36
|
+
const DEFAULT_CONFIG = {
|
|
37
|
+
displayLabel: true,
|
|
38
|
+
uppercaseLabel: false
|
|
39
|
+
};
|
|
40
|
+
class Logger {
|
|
41
|
+
constructor(options = {}) {
|
|
42
|
+
this.level = options.level || LOG_TYPES.log.level;
|
|
43
|
+
this.config = { ...DEFAULT_CONFIG, ...options.config || {} };
|
|
44
|
+
this.types = {
|
|
45
|
+
...LOG_TYPES,
|
|
46
|
+
...options.types || {}
|
|
47
|
+
};
|
|
48
|
+
this.longestLabel = this.getLongestLabel();
|
|
49
|
+
Object.keys(this.types).forEach((type) => {
|
|
50
|
+
this[type] = this._log.bind(this, type);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
_log(type, message, ...args) {
|
|
54
|
+
if (message === void 0 || message === null) {
|
|
55
|
+
console.log();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (LOG_LEVEL[type] > LOG_LEVEL[this.level]) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
let label = "";
|
|
62
|
+
let text = "";
|
|
63
|
+
const logType = this.types[type];
|
|
64
|
+
if (this.config.displayLabel && logType.label) {
|
|
65
|
+
label = this.config.uppercaseLabel ? logType.label.toUpperCase() : logType.label;
|
|
66
|
+
label = label.padEnd(this.longestLabel.length);
|
|
67
|
+
}
|
|
68
|
+
if (message instanceof Error) {
|
|
69
|
+
if (message.stack) {
|
|
70
|
+
const [name, ...rest] = message.stack.split("\n");
|
|
71
|
+
text = `${name}
|
|
72
|
+
${rest.join("\n")}`;
|
|
73
|
+
} else {
|
|
74
|
+
text = message.message;
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
text = `${message}`;
|
|
78
|
+
}
|
|
79
|
+
const log = label.length > 0 ? `${label} ${text}` : text;
|
|
80
|
+
console.log(log, ...args);
|
|
81
|
+
}
|
|
82
|
+
getLongestLabel() {
|
|
83
|
+
let longestLabel = "";
|
|
84
|
+
Object.keys(this.types).forEach((type) => {
|
|
85
|
+
const { label = "" } = this.types[type];
|
|
86
|
+
if (label.length > longestLabel.length) {
|
|
87
|
+
longestLabel = label;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return longestLabel;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const logger = new Logger();
|
|
94
|
+
logger.Logger = Logger;
|
|
95
|
+
export {
|
|
96
|
+
Logger,
|
|
97
|
+
logger
|
|
98
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createProxyMiddleware } from "http-proxy-middleware";
|
|
2
|
+
import { debug } from "../utils";
|
|
3
|
+
function formatProxyOptions(proxyOptions) {
|
|
4
|
+
const formattedProxy = [];
|
|
5
|
+
if (!Array.isArray(proxyOptions)) {
|
|
6
|
+
if ("target" in proxyOptions) {
|
|
7
|
+
formattedProxy.push(proxyOptions);
|
|
8
|
+
} else {
|
|
9
|
+
Array.prototype.push.apply(
|
|
10
|
+
formattedProxy,
|
|
11
|
+
Object.keys(proxyOptions).reduce(
|
|
12
|
+
(total, source) => {
|
|
13
|
+
const option = proxyOptions[source];
|
|
14
|
+
total.push({
|
|
15
|
+
context: source,
|
|
16
|
+
changeOrigin: true,
|
|
17
|
+
logLevel: "warn",
|
|
18
|
+
...typeof option === "string" ? { target: option } : option
|
|
19
|
+
});
|
|
20
|
+
return total;
|
|
21
|
+
},
|
|
22
|
+
[]
|
|
23
|
+
)
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
formattedProxy.push(...proxyOptions);
|
|
28
|
+
}
|
|
29
|
+
return formattedProxy;
|
|
30
|
+
}
|
|
31
|
+
const createProxyHandler = (proxyOptions) => {
|
|
32
|
+
debug("createProxyHandler", proxyOptions);
|
|
33
|
+
if (!proxyOptions) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
const formattedProxy = formatProxyOptions(proxyOptions);
|
|
37
|
+
const middlewares = formattedProxy.map((option) => {
|
|
38
|
+
const middleware = createProxyMiddleware(option.context, option);
|
|
39
|
+
return async (ctx, next) => {
|
|
40
|
+
const { req, res } = ctx;
|
|
41
|
+
const bypassUrl = typeof option.bypass === "function" ? option.bypass(req, res, option) : null;
|
|
42
|
+
if (typeof bypassUrl === "boolean") {
|
|
43
|
+
ctx.status = 404;
|
|
44
|
+
return next();
|
|
45
|
+
} else if (typeof bypassUrl === "string") {
|
|
46
|
+
ctx.url = bypassUrl;
|
|
47
|
+
return next();
|
|
48
|
+
}
|
|
49
|
+
middleware(req, res, next);
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
return middlewares;
|
|
53
|
+
};
|
|
54
|
+
export {
|
|
55
|
+
createProxyHandler,
|
|
56
|
+
formatProxyOptions
|
|
57
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import url from "url";
|
|
2
|
+
import { createCache, destroyCache } from "../spr";
|
|
3
|
+
import {
|
|
4
|
+
cacheAddition,
|
|
5
|
+
connectFactor,
|
|
6
|
+
fname,
|
|
7
|
+
maybeSync,
|
|
8
|
+
namespaceHash,
|
|
9
|
+
valueFactory,
|
|
10
|
+
withCoalescedInvoke
|
|
11
|
+
} from "../util";
|
|
12
|
+
describe("test spr util functions", () => {
|
|
13
|
+
it("should return value correctly", () => {
|
|
14
|
+
expect(connectFactor("bar", "foo")).toBe("bar-foo");
|
|
15
|
+
expect(fname(1)).toBe("f1");
|
|
16
|
+
expect(namespaceHash("modern", "!@#$%^&")).toBe("modern/!@#$%^&");
|
|
17
|
+
});
|
|
18
|
+
it("should create or destroy instance correctly", () => {
|
|
19
|
+
const ins1 = createCache();
|
|
20
|
+
const ins2 = createCache();
|
|
21
|
+
expect(ins1 === ins2).toBe(true);
|
|
22
|
+
destroyCache();
|
|
23
|
+
const ins3 = createCache();
|
|
24
|
+
expect(ins1 === ins3).toBe(false);
|
|
25
|
+
expect(ins2 === ins3).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
it("should return function correctly", () => {
|
|
28
|
+
const urlParams = (() => new url.URLSearchParams())();
|
|
29
|
+
urlParams.set("name", "modern");
|
|
30
|
+
const getParam = valueFactory(urlParams);
|
|
31
|
+
expect(getParam("name")).toBe("modern");
|
|
32
|
+
const headers = { age: "12345" };
|
|
33
|
+
const getHeader = valueFactory(headers);
|
|
34
|
+
expect(getHeader("age")).toBe("12345");
|
|
35
|
+
});
|
|
36
|
+
it("should add target html content", () => {
|
|
37
|
+
const contentNoHead = "<div>123</div>";
|
|
38
|
+
const html = cacheAddition(contentNoHead, Math.random().toString());
|
|
39
|
+
expect(html).toBe(contentNoHead);
|
|
40
|
+
const contentWithHead = "<head></head><div>123</div>";
|
|
41
|
+
const hash = Math.random().toString();
|
|
42
|
+
const htmlWithHead = cacheAddition(contentWithHead, hash);
|
|
43
|
+
expect(htmlWithHead).toBe(
|
|
44
|
+
`<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
it("should only invoke func one time", async () => {
|
|
48
|
+
let index = 0;
|
|
49
|
+
const fn = withCoalescedInvoke(
|
|
50
|
+
async () => new Promise((resolve) => {
|
|
51
|
+
setTimeout(() => {
|
|
52
|
+
index += 1;
|
|
53
|
+
resolve(index);
|
|
54
|
+
}, 500);
|
|
55
|
+
})
|
|
56
|
+
);
|
|
57
|
+
const key = "test";
|
|
58
|
+
const [res1, res2] = await Promise.all([fn(key, []), fn(key, [])]);
|
|
59
|
+
expect(res1.isOrigin && res2.isOrigin).toBe(false);
|
|
60
|
+
expect(res1.isOrigin || res2.isOrigin).toBe(true);
|
|
61
|
+
expect(res1.value).toBe(1);
|
|
62
|
+
expect(res2.value).toBe(1);
|
|
63
|
+
});
|
|
64
|
+
it("should invoke sync or async", async () => {
|
|
65
|
+
const foo = "";
|
|
66
|
+
const async = await maybeSync(
|
|
67
|
+
() => new Promise((resolve) => {
|
|
68
|
+
setTimeout(() => {
|
|
69
|
+
resolve(foo);
|
|
70
|
+
}, 100);
|
|
71
|
+
})
|
|
72
|
+
)(false);
|
|
73
|
+
expect(async).toBeUndefined();
|
|
74
|
+
const sync = await maybeSync(
|
|
75
|
+
() => new Promise((resolve) => {
|
|
76
|
+
setTimeout(() => {
|
|
77
|
+
resolve(foo);
|
|
78
|
+
}, 100);
|
|
79
|
+
})
|
|
80
|
+
)(true);
|
|
81
|
+
expect(sync).toBe(foo);
|
|
82
|
+
});
|
|
83
|
+
});
|