@modern-js/prod-server 2.5.0 → 2.5.1-alpha.1
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/dist/cjs/server/index.js +4 -2
- package/dist/cjs/server/modern-server.js +1 -0
- package/dist/esm/server/index.js +4 -2
- package/dist/esm/server/modern-server.js +1 -0
- package/dist/esm-node/server/index.js +4 -2
- package/dist/esm-node/server/modern-server.js +1 -0
- 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/type.d.ts +8 -1
- package/dist/types/utils.d.ts +1 -1
- package/package.json +9 -9
package/dist/cjs/server/index.js
CHANGED
|
@@ -142,14 +142,16 @@ class Server {
|
|
|
142
142
|
}
|
|
143
143
|
initAppContext() {
|
|
144
144
|
const { options } = this;
|
|
145
|
-
const { pwd: appDirectory, plugins = [], config } = options;
|
|
145
|
+
const { pwd: appDirectory, plugins = [], config, appContext } = options;
|
|
146
146
|
const serverPlugins = plugins.map((p) => ({
|
|
147
147
|
server: p
|
|
148
148
|
}));
|
|
149
149
|
return {
|
|
150
150
|
appDirectory,
|
|
151
|
+
apiDirectory: appContext == null ? void 0 : appContext.apiDirectory,
|
|
152
|
+
lambdaDirectory: appContext == null ? void 0 : appContext.lambdaDirectory,
|
|
153
|
+
sharedDirectory: (appContext == null ? void 0 : appContext.sharedDirectory) || import_path.default.resolve(appDirectory, import_utils.SHARED_DIR),
|
|
151
154
|
distDirectory: import_path.default.join(appDirectory, config.output.path || "dist"),
|
|
152
|
-
sharedDirectory: import_path.default.resolve(appDirectory, import_utils.SHARED_DIR),
|
|
153
155
|
plugins: serverPlugins
|
|
154
156
|
};
|
|
155
157
|
}
|
|
@@ -230,6 +230,7 @@ class ModernServer {
|
|
|
230
230
|
pwd: workDir,
|
|
231
231
|
config: extension,
|
|
232
232
|
prefix: Array.isArray(prefix) ? prefix[0] : prefix,
|
|
233
|
+
httpMethodDecider: bff == null ? void 0 : bff.httpMethodDecider,
|
|
233
234
|
render: this.render.bind(this)
|
|
234
235
|
},
|
|
235
236
|
{ onLast: () => null }
|
package/dist/esm/server/index.js
CHANGED
|
@@ -471,7 +471,7 @@ var Server = /*#__PURE__*/ function() {
|
|
|
471
471
|
key: "initAppContext",
|
|
472
472
|
value: function initAppContext() {
|
|
473
473
|
var options = this.options;
|
|
474
|
-
var appDirectory = options.pwd, _options_plugins = options.plugins, plugins = _options_plugins === void 0 ? [] : _options_plugins, config = options.config;
|
|
474
|
+
var appDirectory = options.pwd, _options_plugins = options.plugins, plugins = _options_plugins === void 0 ? [] : _options_plugins, config = options.config, appContext = options.appContext;
|
|
475
475
|
var serverPlugins = plugins.map(function(p) {
|
|
476
476
|
return {
|
|
477
477
|
server: p
|
|
@@ -479,8 +479,10 @@ var Server = /*#__PURE__*/ function() {
|
|
|
479
479
|
});
|
|
480
480
|
return {
|
|
481
481
|
appDirectory: appDirectory,
|
|
482
|
+
apiDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.apiDirectory,
|
|
483
|
+
lambdaDirectory: appContext === null || appContext === void 0 ? void 0 : appContext.lambdaDirectory,
|
|
484
|
+
sharedDirectory: (appContext === null || appContext === void 0 ? void 0 : appContext.sharedDirectory) || path.resolve(appDirectory, SHARED_DIR),
|
|
482
485
|
distDirectory: path.join(appDirectory, config.output.path || "dist"),
|
|
483
|
-
sharedDirectory: path.resolve(appDirectory, SHARED_DIR),
|
|
484
486
|
plugins: serverPlugins
|
|
485
487
|
};
|
|
486
488
|
}
|
|
@@ -610,6 +610,7 @@ var ModernServer = /*#__PURE__*/ function() {
|
|
|
610
610
|
pwd: workDir,
|
|
611
611
|
config: extension,
|
|
612
612
|
prefix: Array.isArray(prefix) ? prefix[0] : prefix,
|
|
613
|
+
httpMethodDecider: bff === null || bff === void 0 ? void 0 : bff.httpMethodDecider,
|
|
613
614
|
render: _this.render.bind(_this)
|
|
614
615
|
}, {
|
|
615
616
|
onLast: function() {
|
|
@@ -130,14 +130,16 @@ class Server {
|
|
|
130
130
|
}
|
|
131
131
|
initAppContext() {
|
|
132
132
|
const { options } = this;
|
|
133
|
-
const { pwd: appDirectory, plugins = [], config } = options;
|
|
133
|
+
const { pwd: appDirectory, plugins = [], config, appContext } = options;
|
|
134
134
|
const serverPlugins = plugins.map((p) => ({
|
|
135
135
|
server: p
|
|
136
136
|
}));
|
|
137
137
|
return {
|
|
138
138
|
appDirectory,
|
|
139
|
+
apiDirectory: appContext == null ? void 0 : appContext.apiDirectory,
|
|
140
|
+
lambdaDirectory: appContext == null ? void 0 : appContext.lambdaDirectory,
|
|
141
|
+
sharedDirectory: (appContext == null ? void 0 : appContext.sharedDirectory) || path.resolve(appDirectory, SHARED_DIR),
|
|
139
142
|
distDirectory: path.join(appDirectory, config.output.path || "dist"),
|
|
140
|
-
sharedDirectory: path.resolve(appDirectory, SHARED_DIR),
|
|
141
143
|
plugins: serverPlugins
|
|
142
144
|
};
|
|
143
145
|
}
|
|
@@ -224,6 +224,7 @@ class ModernServer {
|
|
|
224
224
|
pwd: workDir,
|
|
225
225
|
config: extension,
|
|
226
226
|
prefix: Array.isArray(prefix) ? prefix[0] : prefix,
|
|
227
|
+
httpMethodDecider: bff == null ? void 0 : bff.httpMethodDecider,
|
|
227
228
|
render: this.render.bind(this)
|
|
228
229
|
},
|
|
229
230
|
{ onLast: () => null }
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const AGGRED_DIR = {
|
|
2
|
+
mock: "config/mock",
|
|
3
|
+
server: "server",
|
|
4
|
+
api: "api",
|
|
5
|
+
shared: "shared",
|
|
6
|
+
lambda: "lambda"
|
|
7
|
+
};
|
|
8
|
+
var ApiServerMode = /* @__PURE__ */ ((ApiServerMode2) => {
|
|
9
|
+
ApiServerMode2["func"] = "function";
|
|
10
|
+
ApiServerMode2["frame"] = "framework";
|
|
11
|
+
return ApiServerMode2;
|
|
12
|
+
})(ApiServerMode || {});
|
|
13
|
+
const ERROR_DIGEST = {
|
|
14
|
+
INIT: "Server init error",
|
|
15
|
+
ENOTF: "Page could not be found",
|
|
16
|
+
WARMUP: "SSR warmup failed",
|
|
17
|
+
EINTER: "Internal server error",
|
|
18
|
+
ERENDER: "SSR render failed",
|
|
19
|
+
EMICROINJ: "Get micro-frontend info failed"
|
|
20
|
+
};
|
|
21
|
+
const ERROR_PAGE_TEXT = {
|
|
22
|
+
404: "This page could not be found.",
|
|
23
|
+
500: "Internal Server Error."
|
|
24
|
+
};
|
|
25
|
+
const RUN_MODE = {
|
|
26
|
+
FULL: "full",
|
|
27
|
+
TYPE: "type"
|
|
28
|
+
};
|
|
29
|
+
export {
|
|
30
|
+
AGGRED_DIR,
|
|
31
|
+
ApiServerMode,
|
|
32
|
+
ERROR_DIGEST,
|
|
33
|
+
ERROR_PAGE_TEXT,
|
|
34
|
+
RUN_MODE
|
|
35
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Server } from "./server";
|
|
2
|
+
import { ModernServer } from "./server/modern-server";
|
|
3
|
+
import { createProxyHandler } from "./libs/proxy";
|
|
4
|
+
export * from "./type";
|
|
5
|
+
export * from "./constants";
|
|
6
|
+
var src_default = (options) => {
|
|
7
|
+
if (options == null) {
|
|
8
|
+
throw new Error("can not start mserver without options");
|
|
9
|
+
}
|
|
10
|
+
const server = new Server(options);
|
|
11
|
+
return server.init();
|
|
12
|
+
};
|
|
13
|
+
export {
|
|
14
|
+
ModernServer,
|
|
15
|
+
Server,
|
|
16
|
+
createProxyHandler,
|
|
17
|
+
src_default as default
|
|
18
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { URL } from "url";
|
|
2
|
+
import qs from "querystring";
|
|
3
|
+
import { Buffer } from "buffer";
|
|
4
|
+
import createEtag from "etag";
|
|
5
|
+
import fresh from "fresh";
|
|
6
|
+
import { headersWithoutCookie } from "../../utils";
|
|
7
|
+
class ModernServerContext {
|
|
8
|
+
constructor(req, res, options) {
|
|
9
|
+
this.params = {};
|
|
10
|
+
this.options = {};
|
|
11
|
+
this.req = req;
|
|
12
|
+
this.res = res;
|
|
13
|
+
this.options = options || {};
|
|
14
|
+
this.serverData = {};
|
|
15
|
+
this.bind();
|
|
16
|
+
}
|
|
17
|
+
get logger() {
|
|
18
|
+
return this.req.logger;
|
|
19
|
+
}
|
|
20
|
+
get metrics() {
|
|
21
|
+
return this.req.metrics;
|
|
22
|
+
}
|
|
23
|
+
bind() {
|
|
24
|
+
const { req, res } = this;
|
|
25
|
+
req.get = (key) => this.getReqHeader(key);
|
|
26
|
+
res.set = (key, value) => this.res.setHeader(key, value);
|
|
27
|
+
res.send = (body) => {
|
|
28
|
+
this.send(body);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
send(body) {
|
|
32
|
+
try {
|
|
33
|
+
const generateETag = !this.res.getHeader("ETag") && this.options.etag;
|
|
34
|
+
if (body !== void 0 && generateETag) {
|
|
35
|
+
const encoding = typeof body === "string" ? "utf-8" : void 0;
|
|
36
|
+
const buf = !Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body;
|
|
37
|
+
const etag = createEtag(buf, { weak: true });
|
|
38
|
+
if (etag) {
|
|
39
|
+
this.res.setHeader("ETag", etag);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (this.fresh) {
|
|
43
|
+
this.status = 304;
|
|
44
|
+
}
|
|
45
|
+
} catch (e) {
|
|
46
|
+
this.logger.error(e.message);
|
|
47
|
+
}
|
|
48
|
+
this.res.end(body);
|
|
49
|
+
}
|
|
50
|
+
setParams(params) {
|
|
51
|
+
this.params = params;
|
|
52
|
+
}
|
|
53
|
+
setServerData(key, value) {
|
|
54
|
+
this.serverData[key] = value;
|
|
55
|
+
}
|
|
56
|
+
getReqHeader(key) {
|
|
57
|
+
const { req } = this;
|
|
58
|
+
const field = key.toLowerCase();
|
|
59
|
+
switch (field) {
|
|
60
|
+
case "referer":
|
|
61
|
+
case "referrer":
|
|
62
|
+
return req.headers.referrer || req.headers.referer || "";
|
|
63
|
+
default:
|
|
64
|
+
return req.headers[field] || "";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
get fresh() {
|
|
68
|
+
const { status, res, method } = this;
|
|
69
|
+
if ("GET" !== method && "HEAD" !== method) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
if (status >= 200 && status < 300 || 304 === status) {
|
|
73
|
+
return fresh(this.headers, {
|
|
74
|
+
etag: res.getHeader("ETag"),
|
|
75
|
+
"last-modified": res.getHeader("Last-Modified")
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
get headers() {
|
|
81
|
+
return this.req.headers;
|
|
82
|
+
}
|
|
83
|
+
get method() {
|
|
84
|
+
return this.req.method;
|
|
85
|
+
}
|
|
86
|
+
get url() {
|
|
87
|
+
return this.req.url || "";
|
|
88
|
+
}
|
|
89
|
+
set url(val) {
|
|
90
|
+
this.req.url = val;
|
|
91
|
+
}
|
|
92
|
+
get host() {
|
|
93
|
+
let host = this.getReqHeader("X-Forwarded-Host");
|
|
94
|
+
if (!host) {
|
|
95
|
+
host = this.getReqHeader("Host");
|
|
96
|
+
}
|
|
97
|
+
return host.split(/\s*,\s*/, 1)[0] || "";
|
|
98
|
+
}
|
|
99
|
+
get protocol() {
|
|
100
|
+
if (this.req.socket.encrypted) {
|
|
101
|
+
return "https";
|
|
102
|
+
}
|
|
103
|
+
const proto = this.getReqHeader("X-Forwarded-Proto");
|
|
104
|
+
return proto ? proto.split(/\s*,\s*/, 1)[0] : "http";
|
|
105
|
+
}
|
|
106
|
+
get origin() {
|
|
107
|
+
return `${this.protocol}://${this.host}`;
|
|
108
|
+
}
|
|
109
|
+
get href() {
|
|
110
|
+
return this.origin + this.url;
|
|
111
|
+
}
|
|
112
|
+
get parsedURL() {
|
|
113
|
+
const url = new URL(this.req.url, this.origin);
|
|
114
|
+
return url;
|
|
115
|
+
}
|
|
116
|
+
get path() {
|
|
117
|
+
return this.parsedURL.pathname;
|
|
118
|
+
}
|
|
119
|
+
set path(p) {
|
|
120
|
+
const url = new URL(this.req.url, this.origin);
|
|
121
|
+
if (!url || !p) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (url.pathname === p) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
url.pathname = p;
|
|
128
|
+
this.url = url.toString();
|
|
129
|
+
}
|
|
130
|
+
get querystring() {
|
|
131
|
+
if (!this.req) {
|
|
132
|
+
return "";
|
|
133
|
+
}
|
|
134
|
+
return this.parsedURL.search.replace(/^\?/, "") || "";
|
|
135
|
+
}
|
|
136
|
+
get query() {
|
|
137
|
+
const str = this.querystring;
|
|
138
|
+
return qs.parse(str);
|
|
139
|
+
}
|
|
140
|
+
get status() {
|
|
141
|
+
return this.res.statusCode;
|
|
142
|
+
}
|
|
143
|
+
set status(statusCode) {
|
|
144
|
+
this.res.statusCode = statusCode;
|
|
145
|
+
}
|
|
146
|
+
resHasHandled() {
|
|
147
|
+
return this.res.writableEnded;
|
|
148
|
+
}
|
|
149
|
+
error(dig, e = "") {
|
|
150
|
+
this.logger.error(
|
|
151
|
+
`Web Server Error - ${dig}, error = %s, req.url = %s, req.headers = %o`,
|
|
152
|
+
e instanceof Error ? e.stack || e.message : e,
|
|
153
|
+
this.path,
|
|
154
|
+
headersWithoutCookie(this.headers)
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
export {
|
|
159
|
+
ModernServerContext
|
|
160
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
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 cookie from "cookie";
|
|
21
|
+
import { RouteAPI } from "./route";
|
|
22
|
+
import { TemplateAPI } from "./template";
|
|
23
|
+
class Response {
|
|
24
|
+
constructor(res) {
|
|
25
|
+
this.res = res;
|
|
26
|
+
this._cookie = cookie.parse(res.getHeader("set-cookie") || "");
|
|
27
|
+
this.cookies = {
|
|
28
|
+
get: this.getCookie.bind(this),
|
|
29
|
+
set: this.setCookie.bind(this),
|
|
30
|
+
delete: this.deleteCookie.bind(this),
|
|
31
|
+
clear: this.clearCookie.bind(this),
|
|
32
|
+
apply: this.applyCookie.bind(this)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
get(key) {
|
|
36
|
+
return this.res.getHeader(key);
|
|
37
|
+
}
|
|
38
|
+
set(key, value) {
|
|
39
|
+
return this.res.setHeader(key, value);
|
|
40
|
+
}
|
|
41
|
+
status(code) {
|
|
42
|
+
this.res.statusCode = code;
|
|
43
|
+
}
|
|
44
|
+
getCookie(key) {
|
|
45
|
+
return this._cookie[key];
|
|
46
|
+
}
|
|
47
|
+
setCookie(key, value) {
|
|
48
|
+
this._cookie[key] = value;
|
|
49
|
+
}
|
|
50
|
+
deleteCookie(key) {
|
|
51
|
+
if (this._cookie[key]) {
|
|
52
|
+
delete this._cookie[key];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
clearCookie() {
|
|
56
|
+
this._cookie = {};
|
|
57
|
+
}
|
|
58
|
+
applyCookie() {
|
|
59
|
+
const str = Object.entries(this._cookie).map(([key, value]) => {
|
|
60
|
+
return cookie.serialize(key, value);
|
|
61
|
+
}).join("; ");
|
|
62
|
+
if (str) {
|
|
63
|
+
this.res.setHeader("set-cookie", str);
|
|
64
|
+
} else {
|
|
65
|
+
this.res.removeHeader("set-cookie");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
raw(body, options) {
|
|
69
|
+
const { status, headers = {} } = options || {};
|
|
70
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
71
|
+
this.res.setHeader(key, value);
|
|
72
|
+
});
|
|
73
|
+
if (status) {
|
|
74
|
+
this.res.statusCode = status;
|
|
75
|
+
}
|
|
76
|
+
this.res.end(body);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
class Request {
|
|
80
|
+
constructor(ctx) {
|
|
81
|
+
this.url = ctx.url;
|
|
82
|
+
this.host = ctx.host;
|
|
83
|
+
this.pathname = ctx.path;
|
|
84
|
+
this.query = ctx.query;
|
|
85
|
+
this.headers = ctx.headers;
|
|
86
|
+
this.cookie = ctx.headers.cookie || "";
|
|
87
|
+
this._cookie = cookie.parse(this.cookie);
|
|
88
|
+
this.cookies = {
|
|
89
|
+
get: this.getCookie.bind(this)
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
getCookie(key) {
|
|
93
|
+
return this._cookie[key];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const base = (context) => {
|
|
97
|
+
const { res } = context;
|
|
98
|
+
return {
|
|
99
|
+
response: new Response(res),
|
|
100
|
+
request: new Request(context),
|
|
101
|
+
logger: context.logger,
|
|
102
|
+
metrics: context.metrics
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
const createAfterMatchContext = (context, entryName) => {
|
|
106
|
+
const baseContext = base(context);
|
|
107
|
+
return __spreadProps(__spreadValues({}, baseContext), {
|
|
108
|
+
router: new RouteAPI(entryName)
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
const createAfterRenderContext = (context, content) => {
|
|
112
|
+
const baseContext = base(context);
|
|
113
|
+
return __spreadProps(__spreadValues({}, baseContext), {
|
|
114
|
+
template: new TemplateAPI(content)
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
const createMiddlewareContext = (context) => {
|
|
118
|
+
const baseContext = base(context);
|
|
119
|
+
return __spreadProps(__spreadValues({}, baseContext), {
|
|
120
|
+
response: __spreadProps(__spreadValues({}, baseContext.response), {
|
|
121
|
+
locals: context.res.locals || {}
|
|
122
|
+
}),
|
|
123
|
+
source: {
|
|
124
|
+
req: context.req,
|
|
125
|
+
res: context.res
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
export {
|
|
130
|
+
base,
|
|
131
|
+
createAfterMatchContext,
|
|
132
|
+
createAfterRenderContext,
|
|
133
|
+
createMiddlewareContext
|
|
134
|
+
};
|
|
@@ -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,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
|
+
};
|