@modern-js/prod-server 2.4.1-beta.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 +18 -3
- 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/{js/node → cjs}/libs/logger.js +5 -16
- 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 +3 -23
- 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 +0 -0
- package/dist/{js/treeshaking → esm}/libs/context/index.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/hook-api/index.js +0 -0
- 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/{js/treeshaking → esm}/libs/logger.js +0 -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 +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/cache/__tests__/cache.test.js +0 -0
- 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 +0 -0
- 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 +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/reader.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/render/ssr.js +0 -0
- 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 +0 -0
- package/dist/{js/treeshaking → esm}/libs/route/route.js +0 -0
- package/dist/{js/treeshaking → esm}/libs/serve-file.js +0 -0
- package/dist/{js/treeshaking → esm}/server/index.js +0 -0
- package/dist/{js/treeshaking → esm}/server/modern-server-split.js +0 -0
- package/dist/{js/treeshaking → esm}/server/modern-server.js +14 -8
- package/dist/{js/treeshaking → esm}/type.js +0 -0
- package/dist/{js/treeshaking → esm}/utils.js +0 -0
- package/dist/{js/treeshaking → esm}/worker-server.js +0 -0
- 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/{js/modern → esm-node}/libs/logger.js +5 -18
- 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/{js/modern → esm-node}/worker-server.js +3 -23
- 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 +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/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,156 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import {
|
|
4
|
+
Logger,
|
|
5
|
+
SHARED_DIR,
|
|
6
|
+
OUTPUT_CONFIG_FILE,
|
|
7
|
+
dotenv,
|
|
8
|
+
dotenvExpand,
|
|
9
|
+
INTERNAL_SERVER_PLUGINS
|
|
10
|
+
} from "@modern-js/utils";
|
|
11
|
+
import {
|
|
12
|
+
serverManager,
|
|
13
|
+
AppContext,
|
|
14
|
+
ConfigContext,
|
|
15
|
+
loadPlugins
|
|
16
|
+
} from "@modern-js/server-core";
|
|
17
|
+
import { metrics as defaultMetrics } from "../libs/metrics";
|
|
18
|
+
import {
|
|
19
|
+
loadConfig,
|
|
20
|
+
getServerConfigPath,
|
|
21
|
+
requireConfig
|
|
22
|
+
} from "../libs/loadConfig";
|
|
23
|
+
import { debug } from "../utils";
|
|
24
|
+
import { createProdServer } from "./modern-server-split";
|
|
25
|
+
class Server {
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.serverImpl = createProdServer;
|
|
28
|
+
options.logger = options.logger || new Logger({
|
|
29
|
+
level: "warn"
|
|
30
|
+
});
|
|
31
|
+
options.metrics = options.metrics || defaultMetrics;
|
|
32
|
+
this.options = options;
|
|
33
|
+
this.serverConfig = {};
|
|
34
|
+
}
|
|
35
|
+
async init() {
|
|
36
|
+
const { options } = this;
|
|
37
|
+
this.loadServerEnv(options);
|
|
38
|
+
this.initServerConfig(options);
|
|
39
|
+
await this.injectContext(this.runner, options);
|
|
40
|
+
this.runner = await this.createHookRunner();
|
|
41
|
+
await this.initConfig(this.runner, options);
|
|
42
|
+
await this.injectContext(this.runner, options);
|
|
43
|
+
this.server = this.serverImpl(options);
|
|
44
|
+
await this.runPrepareHook(this.runner);
|
|
45
|
+
this.app = await this.server.createHTTPServer(this.getRequestHandler());
|
|
46
|
+
await this.server.onInit(this.runner, this.app);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
runConfigHook(runner, serverConfig) {
|
|
50
|
+
const newServerConfig = runner.config(serverConfig || {});
|
|
51
|
+
return newServerConfig;
|
|
52
|
+
}
|
|
53
|
+
async runPrepareHook(runner) {
|
|
54
|
+
runner.prepare();
|
|
55
|
+
}
|
|
56
|
+
initServerConfig(options) {
|
|
57
|
+
const { pwd, serverConfigFile } = options;
|
|
58
|
+
const distDirectory = path.join(pwd, options.config.output.path || "dist");
|
|
59
|
+
const serverConfigPath = getServerConfigPath(
|
|
60
|
+
distDirectory,
|
|
61
|
+
serverConfigFile
|
|
62
|
+
);
|
|
63
|
+
const serverConfig = requireConfig(serverConfigPath);
|
|
64
|
+
this.serverConfig = serverConfig;
|
|
65
|
+
}
|
|
66
|
+
async initConfig(runner, options) {
|
|
67
|
+
const { pwd, config } = options;
|
|
68
|
+
const { serverConfig } = this;
|
|
69
|
+
const finalServerConfig = this.runConfigHook(runner, serverConfig);
|
|
70
|
+
const resolvedConfigPath = path.join(
|
|
71
|
+
pwd,
|
|
72
|
+
config.output.path || "dist",
|
|
73
|
+
OUTPUT_CONFIG_FILE
|
|
74
|
+
);
|
|
75
|
+
options.config = loadConfig({
|
|
76
|
+
cliConfig: config,
|
|
77
|
+
serverConfig: finalServerConfig,
|
|
78
|
+
resolvedConfigPath
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async close() {
|
|
82
|
+
this.app.close();
|
|
83
|
+
}
|
|
84
|
+
listen(options, listener) {
|
|
85
|
+
const callback = () => {
|
|
86
|
+
listener == null ? void 0 : listener();
|
|
87
|
+
};
|
|
88
|
+
if (typeof options === "object") {
|
|
89
|
+
this.app.listen(options, callback);
|
|
90
|
+
} else {
|
|
91
|
+
this.app.listen(process.env.PORT || options || 8080, callback);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
getRequestHandler() {
|
|
95
|
+
return (req, res, next) => {
|
|
96
|
+
const requestHandler = this.server.getRequestHandler();
|
|
97
|
+
return requestHandler(req, res, next);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async render(req, res, url) {
|
|
101
|
+
return this.server.render(req, res, url);
|
|
102
|
+
}
|
|
103
|
+
async createHookRunner() {
|
|
104
|
+
serverManager.clear();
|
|
105
|
+
const { options } = this;
|
|
106
|
+
const {
|
|
107
|
+
internalPlugins = INTERNAL_SERVER_PLUGINS,
|
|
108
|
+
pwd,
|
|
109
|
+
plugins = []
|
|
110
|
+
} = options;
|
|
111
|
+
const serverPlugins = this.serverConfig.plugins || [];
|
|
112
|
+
const loadedPlugins = loadPlugins(pwd, [...serverPlugins, ...plugins], {
|
|
113
|
+
internalPlugins
|
|
114
|
+
});
|
|
115
|
+
debug("plugins", loadedPlugins);
|
|
116
|
+
loadedPlugins.forEach((p) => {
|
|
117
|
+
serverManager.usePlugin(p);
|
|
118
|
+
});
|
|
119
|
+
const hooksRunner = await serverManager.init();
|
|
120
|
+
return hooksRunner;
|
|
121
|
+
}
|
|
122
|
+
async injectContext(runner, options) {
|
|
123
|
+
const appContext = this.initAppContext();
|
|
124
|
+
const { config, pwd } = options;
|
|
125
|
+
ConfigContext.set(config);
|
|
126
|
+
AppContext.set({
|
|
127
|
+
...appContext,
|
|
128
|
+
distDirectory: path.join(pwd, config.output.path || "dist")
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
initAppContext() {
|
|
132
|
+
const { options } = this;
|
|
133
|
+
const { pwd: appDirectory, plugins = [], config } = options;
|
|
134
|
+
const serverPlugins = plugins.map((p) => ({
|
|
135
|
+
server: p
|
|
136
|
+
}));
|
|
137
|
+
return {
|
|
138
|
+
appDirectory,
|
|
139
|
+
distDirectory: path.join(appDirectory, config.output.path || "dist"),
|
|
140
|
+
sharedDirectory: path.resolve(appDirectory, SHARED_DIR),
|
|
141
|
+
plugins: serverPlugins
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
loadServerEnv(options) {
|
|
145
|
+
const { pwd: appDirectory } = options;
|
|
146
|
+
const serverEnv = process.env.MODERN_ENV;
|
|
147
|
+
const serverEnvPath = path.resolve(appDirectory, `.env.${serverEnv}`);
|
|
148
|
+
if (serverEnv && fs.existsSync(serverEnvPath) && !fs.statSync(serverEnvPath).isDirectory()) {
|
|
149
|
+
const envConfig = dotenv.config({ path: serverEnvPath });
|
|
150
|
+
dotenvExpand(envConfig);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
export {
|
|
155
|
+
Server
|
|
156
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ModernServer } from "./modern-server";
|
|
2
|
+
class ModernSSRServer extends ModernServer {
|
|
3
|
+
prepareAPIHandler(_) {
|
|
4
|
+
return null;
|
|
5
|
+
}
|
|
6
|
+
async handleAPI(context) {
|
|
7
|
+
return this.render404(context);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
class ModernAPIServer extends ModernServer {
|
|
11
|
+
prepareWebHandler(_) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
filterRoutes(routes) {
|
|
15
|
+
return routes.filter((route) => route.isApi);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class ModernWebServer extends ModernServer {
|
|
19
|
+
async warmupSSRBundle() {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
async handleAPI(context) {
|
|
23
|
+
return this.render404(context);
|
|
24
|
+
}
|
|
25
|
+
async handleWeb(context, route) {
|
|
26
|
+
route.isSSR = false;
|
|
27
|
+
return super.handleWeb(context, route);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const createProdServer = (options) => {
|
|
31
|
+
if (options.apiOnly) {
|
|
32
|
+
return new ModernAPIServer(options);
|
|
33
|
+
} else if (options.ssrOnly) {
|
|
34
|
+
return new ModernSSRServer(options);
|
|
35
|
+
} else if (options.webOnly) {
|
|
36
|
+
return new ModernWebServer(options);
|
|
37
|
+
} else {
|
|
38
|
+
return new ModernServer(options);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
export {
|
|
42
|
+
createProdServer
|
|
43
|
+
};
|
|
@@ -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
|
}
|