@modern-js/prod-server 2.4.0 → 2.5.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 +27 -0
- package/dist/{js/node → cjs}/constants.js +0 -0
- package/dist/{js/node → cjs}/index.js +0 -0
- package/dist/{js/node → cjs}/libs/context/context.js +0 -0
- package/dist/{js/node → cjs}/libs/context/index.js +0 -0
- package/dist/{js/node → cjs}/libs/hook-api/index.js +12 -25
- package/dist/{js/node → cjs}/libs/hook-api/route.js +0 -0
- package/dist/{js/node → cjs}/libs/hook-api/template.js +0 -0
- package/dist/{js/node → cjs}/libs/loadConfig.js +6 -21
- package/dist/cjs/libs/logger.js +122 -0
- package/dist/{js/node → cjs}/libs/metrics.js +0 -0
- package/dist/{js/node → cjs}/libs/proxy.js +6 -39
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/cache.fun.test.js +12 -34
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/cache.test.js +43 -78
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/cacheable.js +0 -0
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/error-configuration.js +0 -0
- package/dist/{js/node → cjs}/libs/render/cache/__tests__/matched-cache.js +0 -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/{js/node → cjs}/libs/render/cache/page-caches/lru.js +0 -0
- package/dist/{js/node → cjs}/libs/render/cache/spr.js +69 -97
- package/dist/{js/node → cjs}/libs/render/cache/type.js +0 -0
- package/dist/{js/node → cjs}/libs/render/cache/util.js +19 -41
- package/dist/cjs/libs/render/index.js +93 -0
- package/dist/{js/node → cjs}/libs/render/measure.js +6 -21
- package/dist/{js/node → cjs}/libs/render/reader.js +24 -46
- package/dist/{js/node → cjs}/libs/render/ssr.js +3 -23
- package/dist/{js/node → cjs}/libs/render/static.js +22 -44
- package/dist/{js/node → cjs}/libs/render/type.js +0 -0
- package/dist/{js/node → cjs}/libs/route/index.js +0 -0
- package/dist/{js/node → cjs}/libs/route/matcher.js +0 -0
- package/dist/{js/node → cjs}/libs/route/route.js +0 -0
- package/dist/{js/node → cjs}/libs/serve-file.js +2 -22
- package/dist/{js/node → cjs}/server/index.js +56 -106
- package/dist/{js/node → cjs}/server/modern-server-split.js +9 -40
- package/dist/cjs/server/modern-server.js +490 -0
- package/dist/{js/node → cjs}/type.js +0 -0
- package/dist/{js/node → cjs}/utils.js +1 -15
- package/dist/{js/node → cjs}/worker-server.js +51 -35
- package/dist/{js/treeshaking → esm}/constants.js +0 -0
- package/dist/{js/treeshaking → esm}/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/context/context.js +2 -2
- package/dist/{js/treeshaking → esm}/libs/context/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/hook-api/index.js +1 -1
- package/dist/{js/treeshaking → esm}/libs/hook-api/route.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/hook-api/template.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/loadConfig.js +0 -0
- package/dist/esm/libs/logger.js +205 -0
- package/dist/{js/treeshaking → esm}/libs/metrics.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/proxy.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cache.fun.test.js +3 -3
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cache.test.js +2 -2
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cacheable.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/error-configuration.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/matched-cache.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/page-caches/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/page-caches/lru.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/spr.js +8 -8
- package/dist/{js/treeshaking → esm}/libs/render/cache/type.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/util.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/index.js +4 -3
- package/dist/{js/treeshaking → esm}/libs/render/measure.js +2 -2
- package/dist/{js/treeshaking → esm}/libs/render/reader.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/ssr.js +3 -3
- package/dist/{js/treeshaking → esm}/libs/render/static.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/type.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/route/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/route/matcher.js +2 -2
- package/dist/{js/treeshaking → esm}/libs/route/route.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/serve-file.js +2 -2
- package/dist/{js/treeshaking → esm}/server/index.js +3 -3
- package/dist/{js/treeshaking → esm}/server/modern-server-split.js +0 -0
- package/dist/{js/treeshaking → esm}/server/modern-server.js +21 -15
- package/dist/{js/treeshaking → esm}/type.js +0 -0
- package/dist/{js/treeshaking → esm}/utils.js +1 -1
- package/dist/{js/treeshaking → esm}/worker-server.js +64 -8
- package/dist/{js/modern → esm-node}/constants.js +0 -0
- package/dist/{js/modern → esm-node}/index.js +0 -0
- package/dist/{js/modern → esm-node}/libs/context/context.js +0 -0
- package/dist/{js/modern → esm-node}/libs/context/index.js +0 -0
- package/dist/{js/modern → esm-node}/libs/hook-api/index.js +12 -27
- package/dist/{js/modern → esm-node}/libs/hook-api/route.js +0 -0
- package/dist/{js/modern → esm-node}/libs/hook-api/template.js +0 -0
- package/dist/esm-node/libs/loadConfig.js +45 -0
- package/dist/esm-node/libs/logger.js +98 -0
- package/dist/{js/modern → esm-node}/libs/metrics.js +0 -0
- package/dist/{js/modern → esm-node}/libs/proxy.js +6 -41
- 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/{js/modern → esm-node}/libs/render/cache/__tests__/cacheable.js +0 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/__tests__/error-configuration.js +0 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/__tests__/matched-cache.js +0 -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/{js/modern → esm-node}/libs/render/cache/page-caches/lru.js +0 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/spr.js +69 -97
- package/dist/{js/modern → esm-node}/libs/render/cache/type.js +0 -0
- package/dist/{js/modern → esm-node}/libs/render/cache/util.js +19 -41
- package/dist/esm-node/libs/render/index.js +64 -0
- package/dist/{js/modern → esm-node}/libs/render/measure.js +6 -23
- package/dist/esm-node/libs/render/reader.js +85 -0
- package/dist/{js/modern → esm-node}/libs/render/ssr.js +3 -23
- package/dist/esm-node/libs/render/static.js +38 -0
- package/dist/{js/modern → esm-node}/libs/render/type.js +0 -0
- package/dist/{js/modern → esm-node}/libs/route/index.js +0 -0
- package/dist/{js/modern → esm-node}/libs/route/matcher.js +0 -0
- package/dist/{js/modern → esm-node}/libs/route/route.js +0 -0
- package/dist/{js/modern → esm-node}/libs/serve-file.js +2 -22
- 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 +483 -0
- package/dist/{js/modern → esm-node}/type.js +0 -0
- package/dist/{js/modern → esm-node}/utils.js +1 -17
- package/dist/esm-node/worker-server.js +69 -0
- package/dist/types/libs/context/context.d.ts +1 -1
- package/dist/types/libs/logger.d.ts +63 -0
- package/dist/types/libs/render/index.d.ts +3 -1
- package/dist/types/utils.d.ts +1 -1
- package/dist/types/worker-server.d.ts +2 -1
- package/package.json +15 -16
- package/dist/js/modern/libs/loadConfig.js +0 -62
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -114
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -254
- package/dist/js/modern/libs/render/cache/index.js +0 -115
- package/dist/js/modern/libs/render/cache/page-caches/index.js +0 -32
- package/dist/js/modern/libs/render/index.js +0 -84
- package/dist/js/modern/libs/render/reader.js +0 -107
- package/dist/js/modern/libs/render/static.js +0 -60
- package/dist/js/modern/server/index.js +0 -208
- package/dist/js/modern/server/modern-server-split.js +0 -74
- package/dist/js/modern/server/modern-server.js +0 -548
- package/dist/js/modern/worker-server.js +0 -54
- package/dist/js/node/libs/render/cache/index.js +0 -134
- package/dist/js/node/libs/render/cache/page-caches/index.js +0 -55
- package/dist/js/node/libs/render/index.js +0 -113
- package/dist/js/node/server/modern-server.js +0 -553
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
import { createServer } from "http";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fs, isPromise, mime, ROUTE_SPEC_FILE } from "@modern-js/utils";
|
|
4
|
+
import {
|
|
5
|
+
RouteMatchManager
|
|
6
|
+
} from "../libs/route";
|
|
7
|
+
import { createRenderHandler } from "../libs/render";
|
|
8
|
+
import {
|
|
9
|
+
createStaticFileHandler,
|
|
10
|
+
faviconFallbackHandler
|
|
11
|
+
} from "../libs/serve-file";
|
|
12
|
+
import {
|
|
13
|
+
createErrorDocument,
|
|
14
|
+
createMiddlewareCollecter,
|
|
15
|
+
getStaticReg,
|
|
16
|
+
mergeExtension,
|
|
17
|
+
noop,
|
|
18
|
+
debug,
|
|
19
|
+
isRedirect
|
|
20
|
+
} from "../utils";
|
|
21
|
+
import * as reader from "../libs/render/reader";
|
|
22
|
+
import { createProxyHandler } from "../libs/proxy";
|
|
23
|
+
import { createContext } from "../libs/context";
|
|
24
|
+
import { templateInjectableStream } from "../libs/hook-api/template";
|
|
25
|
+
import {
|
|
26
|
+
AGGRED_DIR,
|
|
27
|
+
ERROR_DIGEST,
|
|
28
|
+
ERROR_PAGE_TEXT,
|
|
29
|
+
RUN_MODE
|
|
30
|
+
} from "../constants";
|
|
31
|
+
import {
|
|
32
|
+
createAfterMatchContext,
|
|
33
|
+
createAfterRenderContext,
|
|
34
|
+
createMiddlewareContext
|
|
35
|
+
} from "../libs/hook-api";
|
|
36
|
+
const API_DIR = "./api";
|
|
37
|
+
const SERVER_DIR = "./server";
|
|
38
|
+
class ModernServer {
|
|
39
|
+
constructor({
|
|
40
|
+
pwd,
|
|
41
|
+
config,
|
|
42
|
+
routes,
|
|
43
|
+
staticGenerate,
|
|
44
|
+
logger,
|
|
45
|
+
metrics,
|
|
46
|
+
runMode,
|
|
47
|
+
proxyTarget
|
|
48
|
+
}) {
|
|
49
|
+
this.handlers = [];
|
|
50
|
+
this.reader = reader;
|
|
51
|
+
this.beforeRouteHandler = null;
|
|
52
|
+
this.frameWebHandler = null;
|
|
53
|
+
this.frameAPIHandler = null;
|
|
54
|
+
this.proxyHandler = null;
|
|
55
|
+
require("ignore-styles");
|
|
56
|
+
this.pwd = pwd;
|
|
57
|
+
this.distDir = path.join(pwd, config.output.path || "dist");
|
|
58
|
+
this.workDir = this.distDir;
|
|
59
|
+
this.conf = config;
|
|
60
|
+
debug("server conf", this.conf);
|
|
61
|
+
this.logger = logger;
|
|
62
|
+
this.metrics = metrics;
|
|
63
|
+
this.router = new RouteMatchManager();
|
|
64
|
+
this.presetRoutes = routes;
|
|
65
|
+
this.proxyTarget = proxyTarget;
|
|
66
|
+
this.staticGenerate = staticGenerate || false;
|
|
67
|
+
this.runMode = runMode || RUN_MODE.FULL;
|
|
68
|
+
}
|
|
69
|
+
async onInit(runner, app) {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
this.runner = runner;
|
|
72
|
+
const { distDir, staticGenerate, conf } = this;
|
|
73
|
+
debug("final server conf", this.conf);
|
|
74
|
+
this.proxyHandler = createProxyHandler((_a = conf.bff) == null ? void 0 : _a.proxy);
|
|
75
|
+
if (this.proxyHandler) {
|
|
76
|
+
this.proxyHandler.forEach((handler) => {
|
|
77
|
+
this.addHandler(handler);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
this.reader.init();
|
|
81
|
+
app.on("close", () => {
|
|
82
|
+
this.reader.close();
|
|
83
|
+
});
|
|
84
|
+
const usageRoutes = this.filterRoutes(this.getRoutes());
|
|
85
|
+
this.router.reset(usageRoutes);
|
|
86
|
+
this.warmupSSRBundle();
|
|
87
|
+
await this.prepareFrameHandler();
|
|
88
|
+
await this.prepareBeforeRouteHandler(usageRoutes, distDir);
|
|
89
|
+
const staticPathRegExp = getStaticReg(
|
|
90
|
+
this.conf.output || {},
|
|
91
|
+
this.conf.html
|
|
92
|
+
);
|
|
93
|
+
this.staticFileHandler = createStaticFileHandler(
|
|
94
|
+
[
|
|
95
|
+
{
|
|
96
|
+
path: staticPathRegExp,
|
|
97
|
+
target: distDir
|
|
98
|
+
}
|
|
99
|
+
],
|
|
100
|
+
this.conf.output
|
|
101
|
+
);
|
|
102
|
+
const ssrConfig = (_b = this.conf.server) == null ? void 0 : _b.ssr;
|
|
103
|
+
const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
|
|
104
|
+
this.routeRenderHandler = createRenderHandler({
|
|
105
|
+
distDir,
|
|
106
|
+
staticGenerate,
|
|
107
|
+
forceCSR
|
|
108
|
+
});
|
|
109
|
+
await this.setupBeforeProdMiddleware();
|
|
110
|
+
this.addHandler(this.staticFileHandler);
|
|
111
|
+
this.addHandler(faviconFallbackHandler);
|
|
112
|
+
this.addBeforeRouteHandler();
|
|
113
|
+
this.addHandler(this.routeHandler.bind(this));
|
|
114
|
+
this.compose();
|
|
115
|
+
}
|
|
116
|
+
onRepack(_) {
|
|
117
|
+
}
|
|
118
|
+
addBeforeRouteHandler() {
|
|
119
|
+
this.addHandler(async (context, next) => {
|
|
120
|
+
if (this.beforeRouteHandler) {
|
|
121
|
+
await this.beforeRouteHandler(context);
|
|
122
|
+
if (this.isSend(context.res)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return next();
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
onServerChange({ filepath }) {
|
|
130
|
+
const { pwd } = this;
|
|
131
|
+
const { api, server } = AGGRED_DIR;
|
|
132
|
+
const apiPath = path.normalize(path.join(pwd, api));
|
|
133
|
+
const serverPath = path.normalize(path.join(pwd, server));
|
|
134
|
+
const onlyApi = filepath.startsWith(apiPath);
|
|
135
|
+
const onlyWeb = filepath.startsWith(serverPath);
|
|
136
|
+
this.prepareFrameHandler({ onlyWeb, onlyApi });
|
|
137
|
+
}
|
|
138
|
+
getRequestHandler() {
|
|
139
|
+
return this.requestHandler.bind(this);
|
|
140
|
+
}
|
|
141
|
+
async render(req, res, url) {
|
|
142
|
+
req.logger = req.logger || this.logger;
|
|
143
|
+
req.metrics = req.metrics || this.metrics;
|
|
144
|
+
const context = createContext(req, res);
|
|
145
|
+
const matched = this.router.match(url || context.path);
|
|
146
|
+
if (!matched) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const route = matched.generate(context.url);
|
|
150
|
+
const result = await this.handleWeb(context, route);
|
|
151
|
+
if (!result) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
return result.content.toString();
|
|
155
|
+
}
|
|
156
|
+
async createHTTPServer(handler) {
|
|
157
|
+
return createServer(handler);
|
|
158
|
+
}
|
|
159
|
+
getRoutes() {
|
|
160
|
+
if (this.presetRoutes) {
|
|
161
|
+
return this.presetRoutes;
|
|
162
|
+
}
|
|
163
|
+
const file = path.join(this.distDir, ROUTE_SPEC_FILE);
|
|
164
|
+
if (fs.existsSync(file)) {
|
|
165
|
+
const content = fs.readJSONSync(file);
|
|
166
|
+
return content.routes;
|
|
167
|
+
}
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
addHandler(handler) {
|
|
171
|
+
this.handlers.push(handler);
|
|
172
|
+
}
|
|
173
|
+
render404(context) {
|
|
174
|
+
context.error(ERROR_DIGEST.ENOTF, "404 Not Found");
|
|
175
|
+
this.renderErrorPage(context, 404);
|
|
176
|
+
}
|
|
177
|
+
async prepareBeforeRouteHandler(specs, distDir) {
|
|
178
|
+
const { runner } = this;
|
|
179
|
+
const handler = await runner.preparebeforeRouteHandler(
|
|
180
|
+
{
|
|
181
|
+
serverRoutes: specs,
|
|
182
|
+
distDir
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
onLast: () => null
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
this.beforeRouteHandler = handler;
|
|
189
|
+
}
|
|
190
|
+
async prepareFrameHandler(options) {
|
|
191
|
+
const { workDir, runner } = this;
|
|
192
|
+
const { onlyApi, onlyWeb } = options || {};
|
|
193
|
+
const { getMiddlewares, ...collector } = createMiddlewareCollecter();
|
|
194
|
+
await runner.gather(collector);
|
|
195
|
+
const { api: pluginAPIExt, web: pluginWebExt } = getMiddlewares();
|
|
196
|
+
const apiDir = path.join(workDir, API_DIR);
|
|
197
|
+
const serverDir = path.join(workDir, SERVER_DIR);
|
|
198
|
+
if (await fs.pathExists(path.join(serverDir)) && !onlyApi) {
|
|
199
|
+
const webExtension = mergeExtension(pluginWebExt);
|
|
200
|
+
this.frameWebHandler = await this.prepareWebHandler(webExtension);
|
|
201
|
+
}
|
|
202
|
+
if (fs.existsSync(apiDir) && !onlyWeb) {
|
|
203
|
+
const apiExtension = mergeExtension(pluginAPIExt);
|
|
204
|
+
this.frameAPIHandler = await this.prepareAPIHandler(apiExtension);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
async prepareWebHandler(extension) {
|
|
208
|
+
const { workDir, runner } = this;
|
|
209
|
+
const handler = await runner.prepareWebServer(
|
|
210
|
+
{
|
|
211
|
+
pwd: workDir,
|
|
212
|
+
config: extension
|
|
213
|
+
},
|
|
214
|
+
{ onLast: () => null }
|
|
215
|
+
);
|
|
216
|
+
return handler;
|
|
217
|
+
}
|
|
218
|
+
async prepareAPIHandler(extension) {
|
|
219
|
+
const { workDir, runner, conf } = this;
|
|
220
|
+
const { bff } = conf;
|
|
221
|
+
const prefix = (bff == null ? void 0 : bff.prefix) || "/api";
|
|
222
|
+
return runner.prepareApiServer(
|
|
223
|
+
{
|
|
224
|
+
pwd: workDir,
|
|
225
|
+
config: extension,
|
|
226
|
+
prefix: Array.isArray(prefix) ? prefix[0] : prefix,
|
|
227
|
+
render: this.render.bind(this)
|
|
228
|
+
},
|
|
229
|
+
{ onLast: () => null }
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
filterRoutes(routes) {
|
|
233
|
+
return routes;
|
|
234
|
+
}
|
|
235
|
+
async setupBeforeProdMiddleware() {
|
|
236
|
+
const { conf, runner } = this;
|
|
237
|
+
const preMiddleware = await runner.beforeProdServer(conf);
|
|
238
|
+
preMiddleware.flat().forEach((mid) => {
|
|
239
|
+
this.addHandler(mid);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
async handleAPI(context) {
|
|
243
|
+
const { req, res } = context;
|
|
244
|
+
if (!this.frameAPIHandler) {
|
|
245
|
+
throw new Error("can not found api handler");
|
|
246
|
+
}
|
|
247
|
+
await this.frameAPIHandler(req, res);
|
|
248
|
+
}
|
|
249
|
+
async handleWeb(context, route) {
|
|
250
|
+
return this.routeRenderHandler({
|
|
251
|
+
ctx: context,
|
|
252
|
+
route,
|
|
253
|
+
runner: this.runner
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
async proxy() {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
warmupSSRBundle() {
|
|
260
|
+
const { distDir } = this;
|
|
261
|
+
const bundles = this.router.getBundles();
|
|
262
|
+
bundles.forEach((bundle) => {
|
|
263
|
+
const filepath = path.join(distDir, bundle);
|
|
264
|
+
if (fs.existsSync(filepath)) {
|
|
265
|
+
require(filepath);
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
createContext(req, res, options = {}) {
|
|
270
|
+
return createContext(req, res, options);
|
|
271
|
+
}
|
|
272
|
+
async routeHandler(context) {
|
|
273
|
+
const { res } = context;
|
|
274
|
+
const matched = this.router.match(context.path);
|
|
275
|
+
if (!matched) {
|
|
276
|
+
this.render404(context);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
let route = matched.generate(context.url);
|
|
280
|
+
if (route.isApi) {
|
|
281
|
+
await this.handleAPI(context);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const afterMatchContext = createAfterMatchContext(context, route.entryName);
|
|
285
|
+
if (this.runMode === RUN_MODE.FULL) {
|
|
286
|
+
await this.runner.afterMatch(afterMatchContext, { onLast: noop });
|
|
287
|
+
}
|
|
288
|
+
if (this.isSend(res)) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const { current, url, status } = afterMatchContext.router;
|
|
292
|
+
if (url) {
|
|
293
|
+
this.redirect(res, url, status);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (route.entryName !== current) {
|
|
297
|
+
const matched2 = this.router.matchEntry(current);
|
|
298
|
+
if (!matched2) {
|
|
299
|
+
this.render404(context);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
route = matched2.generate(context.url);
|
|
303
|
+
}
|
|
304
|
+
context.setParams(route.params);
|
|
305
|
+
context.setServerData("router", {
|
|
306
|
+
baseUrl: route.urlPath,
|
|
307
|
+
params: route.params
|
|
308
|
+
});
|
|
309
|
+
if (this.frameWebHandler) {
|
|
310
|
+
res.locals = res.locals || {};
|
|
311
|
+
const middlewareContext = createMiddlewareContext(context);
|
|
312
|
+
await this.frameWebHandler(middlewareContext);
|
|
313
|
+
res.locals = {
|
|
314
|
+
...res.locals,
|
|
315
|
+
...middlewareContext.response.locals
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
if (this.isSend(res)) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (route.responseHeaders) {
|
|
322
|
+
Object.keys(route.responseHeaders).forEach((key) => {
|
|
323
|
+
const value = route.responseHeaders[key];
|
|
324
|
+
if (value) {
|
|
325
|
+
context.res.setHeader(key, value);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
const renderResult = await this.handleWeb(context, route);
|
|
330
|
+
if (!renderResult) {
|
|
331
|
+
this.render404(context);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (renderResult.redirect) {
|
|
335
|
+
this.redirect(
|
|
336
|
+
res,
|
|
337
|
+
renderResult.content,
|
|
338
|
+
renderResult.statusCode
|
|
339
|
+
);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
if (this.isSend(res)) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
res.setHeader("content-type", renderResult.contentType);
|
|
346
|
+
const { contentStream } = renderResult;
|
|
347
|
+
if (contentStream) {
|
|
348
|
+
contentStream.pipe(
|
|
349
|
+
templateInjectableStream({
|
|
350
|
+
prependHead: route.entryName ? `<script>window._SERVER_DATA=${JSON.stringify(
|
|
351
|
+
context.serverData
|
|
352
|
+
)}<\/script>` : void 0
|
|
353
|
+
})
|
|
354
|
+
).pipe(res);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
let response = renderResult.content;
|
|
358
|
+
if (route.entryName) {
|
|
359
|
+
const afterRenderContext = createAfterRenderContext(
|
|
360
|
+
context,
|
|
361
|
+
response.toString()
|
|
362
|
+
);
|
|
363
|
+
if (this.runMode === RUN_MODE.FULL) {
|
|
364
|
+
await this.runner.afterRender(afterRenderContext, { onLast: noop });
|
|
365
|
+
}
|
|
366
|
+
if (this.isSend(res)) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
afterRenderContext.template.prependHead(
|
|
370
|
+
`<script>window._SERVER_DATA=${JSON.stringify(
|
|
371
|
+
context.serverData
|
|
372
|
+
)}<\/script>`
|
|
373
|
+
);
|
|
374
|
+
response = afterRenderContext.template.get();
|
|
375
|
+
}
|
|
376
|
+
res.end(response);
|
|
377
|
+
}
|
|
378
|
+
isSend(res) {
|
|
379
|
+
if (res.headersSent) {
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
if (res.getHeader("Location") && isRedirect(res.statusCode)) {
|
|
383
|
+
res.end();
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
compose() {
|
|
389
|
+
const { handlers } = this;
|
|
390
|
+
if (!Array.isArray(handlers)) {
|
|
391
|
+
throw new TypeError("Middleware stack must be an array!");
|
|
392
|
+
}
|
|
393
|
+
for (const fn of handlers) {
|
|
394
|
+
if (typeof fn !== "function") {
|
|
395
|
+
throw new TypeError("Middleware must be composed of functions!");
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
this._handler = (context, next) => {
|
|
399
|
+
let i = 0;
|
|
400
|
+
const dispatch = (error) => {
|
|
401
|
+
if (error) {
|
|
402
|
+
return this.onError(context, error);
|
|
403
|
+
}
|
|
404
|
+
const handler = handlers[i++];
|
|
405
|
+
if (!handler) {
|
|
406
|
+
return next();
|
|
407
|
+
}
|
|
408
|
+
try {
|
|
409
|
+
const result = handler(context, dispatch);
|
|
410
|
+
if (isPromise(result)) {
|
|
411
|
+
return result.catch(onError);
|
|
412
|
+
}
|
|
413
|
+
} catch (e) {
|
|
414
|
+
return onError(e);
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
const onError = (err) => {
|
|
418
|
+
this.onError(context, err);
|
|
419
|
+
};
|
|
420
|
+
return dispatch();
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
requestHandler(req, res, next = () => {
|
|
424
|
+
}) {
|
|
425
|
+
res.statusCode = 200;
|
|
426
|
+
req.logger = req.logger || this.logger;
|
|
427
|
+
req.metrics = req.metrics || this.metrics;
|
|
428
|
+
let context;
|
|
429
|
+
try {
|
|
430
|
+
context = this.createContext(req, res);
|
|
431
|
+
} catch (e) {
|
|
432
|
+
this.logger.error(e);
|
|
433
|
+
res.statusCode = 500;
|
|
434
|
+
res.setHeader("content-type", mime.contentType("html"));
|
|
435
|
+
return res.end(createErrorDocument(500, ERROR_PAGE_TEXT[500]));
|
|
436
|
+
}
|
|
437
|
+
try {
|
|
438
|
+
return this._handler(context, next);
|
|
439
|
+
} catch (err) {
|
|
440
|
+
return this.onError(context, err);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
redirect(res, url, status = 302) {
|
|
444
|
+
res.setHeader("Location", url);
|
|
445
|
+
res.statusCode = status;
|
|
446
|
+
res.end();
|
|
447
|
+
}
|
|
448
|
+
onError(context, err) {
|
|
449
|
+
context.error(ERROR_DIGEST.EINTER, err);
|
|
450
|
+
this.renderErrorPage(context, 500);
|
|
451
|
+
}
|
|
452
|
+
async renderErrorPage(context, status) {
|
|
453
|
+
const { res } = context;
|
|
454
|
+
context.status = status;
|
|
455
|
+
res.setHeader("content-type", mime.contentType("html"));
|
|
456
|
+
const statusPage = `/${status}`;
|
|
457
|
+
const customErrorPage = `/_error`;
|
|
458
|
+
const matched = this.router.match(statusPage) || this.router.match(customErrorPage);
|
|
459
|
+
if (matched) {
|
|
460
|
+
const route = matched.generate(context.url);
|
|
461
|
+
const { entryName } = route;
|
|
462
|
+
if (entryName === status.toString() || entryName === "_error") {
|
|
463
|
+
try {
|
|
464
|
+
const file = await this.routeRenderHandler({
|
|
465
|
+
route,
|
|
466
|
+
ctx: context,
|
|
467
|
+
runner: this.runner
|
|
468
|
+
});
|
|
469
|
+
if (file) {
|
|
470
|
+
context.res.end(file.content);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
} catch (e) {
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
const text = ERROR_PAGE_TEXT[status] || ERROR_PAGE_TEXT[500];
|
|
478
|
+
context.res.end(createErrorDocument(status, text));
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
export {
|
|
482
|
+
ModernServer
|
|
483
|
+
};
|
|
File without changes
|
|
@@ -1,19 +1,3 @@
|
|
|
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
1
|
import { createDebugger, isProd } from "@modern-js/utils";
|
|
18
2
|
const debug = createDebugger("prod-server");
|
|
19
3
|
const mergeExtension = (users) => {
|
|
@@ -113,7 +97,7 @@ const prepareFavicons = (favicon, faviconByEntries) => {
|
|
|
113
97
|
};
|
|
114
98
|
const headersWithoutCookie = (headers) => {
|
|
115
99
|
if (typeof headers.cookie !== "undefined") {
|
|
116
|
-
const safeHeaders =
|
|
100
|
+
const safeHeaders = { ...headers };
|
|
117
101
|
delete safeHeaders.cookie;
|
|
118
102
|
return safeHeaders;
|
|
119
103
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Logger } from "./libs/logger";
|
|
2
|
+
import { RouteMatchManager } from "./libs/route";
|
|
3
|
+
import { metrics as defaultMetrics } from "./libs/metrics";
|
|
4
|
+
const handleUrl = (url) => {
|
|
5
|
+
return url.replace(/^https?:\/\/.*?\//gi, "/");
|
|
6
|
+
};
|
|
7
|
+
const createHandler = (manifest) => {
|
|
8
|
+
const routeMgr = new RouteMatchManager();
|
|
9
|
+
const { pages, routes } = manifest;
|
|
10
|
+
routeMgr.reset(routes);
|
|
11
|
+
return async (ctx) => {
|
|
12
|
+
var _a, _b, _c, _d, _e, _f;
|
|
13
|
+
const pageMatch = routeMgr.match(ctx.url);
|
|
14
|
+
if (!pageMatch) {
|
|
15
|
+
ctx.body = "404: Page not found";
|
|
16
|
+
ctx.status = 404;
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const page = pages[pageMatch.spec.urlPath];
|
|
20
|
+
(_b = (_a = ctx.request).query) != null ? _b : _a.query = ctx.query;
|
|
21
|
+
(_d = (_c = ctx.request).pathname) != null ? _d : _c.pathname = ctx.pathname;
|
|
22
|
+
(_f = (_e = ctx.request).params) != null ? _f : _e.params = ctx.params;
|
|
23
|
+
const params = pageMatch.parseURLParams(ctx.url);
|
|
24
|
+
if (page.serverRender) {
|
|
25
|
+
try {
|
|
26
|
+
ctx.body = await page.serverRender({
|
|
27
|
+
entryName: page.entryName,
|
|
28
|
+
template: page.template,
|
|
29
|
+
query: ctx.query,
|
|
30
|
+
request: ctx.request,
|
|
31
|
+
response: ctx.response,
|
|
32
|
+
pathname: ctx.pathname,
|
|
33
|
+
req: ctx.request,
|
|
34
|
+
res: ctx.response,
|
|
35
|
+
params: ctx.params || params || {},
|
|
36
|
+
logger: ctx.logger || new Logger({
|
|
37
|
+
level: "warn"
|
|
38
|
+
}),
|
|
39
|
+
metrics: ctx.metrics || defaultMetrics,
|
|
40
|
+
loadableStats: ctx.loadableStats,
|
|
41
|
+
routeManifest: ctx.routeManifest
|
|
42
|
+
});
|
|
43
|
+
ctx.status = 200;
|
|
44
|
+
return;
|
|
45
|
+
} catch (e) {
|
|
46
|
+
if (page.template) {
|
|
47
|
+
ctx.body = page.template;
|
|
48
|
+
ctx.status = 200;
|
|
49
|
+
return;
|
|
50
|
+
} else {
|
|
51
|
+
ctx.body = "404: not found";
|
|
52
|
+
ctx.status = 404;
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (page.template) {
|
|
58
|
+
ctx.body = page.template;
|
|
59
|
+
ctx.status = 200;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
ctx.body = "404: not found";
|
|
63
|
+
ctx.status = 404;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
export {
|
|
67
|
+
createHandler,
|
|
68
|
+
handleUrl
|
|
69
|
+
};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
/// <reference types="node" />
|
|
5
5
|
/// <reference types="node/http" />
|
|
6
|
-
/// <reference types=".dts-temp/
|
|
6
|
+
/// <reference types=".dts-temp/-lHmu6nzaXgMDLlmkuoLo/src/type" />
|
|
7
7
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
8
8
|
import { URL } from 'url';
|
|
9
9
|
import qs from 'querystring';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
2
|
+
type LogMsg = number | string | Error | null;
|
|
3
|
+
interface LoggerConfiguration {
|
|
4
|
+
label?: string;
|
|
5
|
+
level?: LogLevel;
|
|
6
|
+
}
|
|
7
|
+
interface InstanceConfiguration {
|
|
8
|
+
displayLabel?: boolean;
|
|
9
|
+
uppercaseLabel?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface ConstructorOptions {
|
|
12
|
+
config?: InstanceConfiguration;
|
|
13
|
+
level?: string;
|
|
14
|
+
types?: Record<string, LoggerConfiguration>;
|
|
15
|
+
}
|
|
16
|
+
type LoggerFunction = (message?: LogMsg, ...args: any[]) => void;
|
|
17
|
+
declare const LOG_TYPES: {
|
|
18
|
+
error: {
|
|
19
|
+
color: string;
|
|
20
|
+
label: string;
|
|
21
|
+
level: string;
|
|
22
|
+
};
|
|
23
|
+
info: {
|
|
24
|
+
color: string;
|
|
25
|
+
label: string;
|
|
26
|
+
level: string;
|
|
27
|
+
};
|
|
28
|
+
success: {
|
|
29
|
+
color: string;
|
|
30
|
+
label: string;
|
|
31
|
+
level: string;
|
|
32
|
+
};
|
|
33
|
+
warn: {
|
|
34
|
+
color: string;
|
|
35
|
+
label: string;
|
|
36
|
+
level: string;
|
|
37
|
+
};
|
|
38
|
+
debug: {
|
|
39
|
+
color: string;
|
|
40
|
+
label: string;
|
|
41
|
+
level: string;
|
|
42
|
+
};
|
|
43
|
+
log: {
|
|
44
|
+
level: string;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
declare class Logger {
|
|
49
|
+
private readonly level;
|
|
50
|
+
private readonly config;
|
|
51
|
+
private readonly types;
|
|
52
|
+
private readonly longestLabel;
|
|
53
|
+
[key: string]: any;
|
|
54
|
+
constructor(options?: ConstructorOptions);
|
|
55
|
+
private _log;
|
|
56
|
+
private getLongestLabel;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
type LoggerInterface = { [key in keyof typeof LOG_TYPES]: LoggerFunction };
|
|
60
|
+
declare const logger: Logger & LoggerInterface;
|
|
61
|
+
export { Logger };
|
|
62
|
+
export { logger };
|
|
63
|
+
export type { LoggerInterface };
|
|
@@ -3,10 +3,12 @@ import { RenderResult, ServerHookRunner } from '../../type';
|
|
|
3
3
|
import { ModernRoute } from '../route';
|
|
4
4
|
export declare const createRenderHandler: ({
|
|
5
5
|
distDir,
|
|
6
|
-
staticGenerate
|
|
6
|
+
staticGenerate,
|
|
7
|
+
forceCSR
|
|
7
8
|
}: {
|
|
8
9
|
distDir: string;
|
|
9
10
|
staticGenerate: boolean;
|
|
11
|
+
forceCSR?: boolean | undefined;
|
|
10
12
|
}) => ({
|
|
11
13
|
ctx,
|
|
12
14
|
route,
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node/http" />
|
|
3
|
-
/// <reference types=".dts-temp/
|
|
3
|
+
/// <reference types=".dts-temp/-lHmu6nzaXgMDLlmkuoLo/src/type" />
|
|
4
4
|
import { IncomingMessage } from 'http';
|
|
5
5
|
import type { OutputNormalizedConfig, HtmlNormalizedConfig } from '@modern-js/server-core';
|
|
6
6
|
export declare const debug: any;
|
|
@@ -8,8 +8,9 @@ export type Manifest = {
|
|
|
8
8
|
{
|
|
9
9
|
entryName: string;
|
|
10
10
|
template: string;
|
|
11
|
-
serverRender
|
|
11
|
+
serverRender?: (ctx: Record<string, any>) => Promise<string>;
|
|
12
12
|
}>;
|
|
13
13
|
routes: ModernRouteInterface[];
|
|
14
14
|
};
|
|
15
|
+
export declare const handleUrl: (url: string) => string;
|
|
15
16
|
export declare const createHandler: (manifest: Manifest) => (ctx: Context) => Promise<void>;
|