@modern-js/server 1.1.2-beta.0 → 1.1.3
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 +41 -0
- package/dist/js/modern/libs/hook-api/route.js +37 -0
- package/dist/js/modern/libs/{hook-api.js → hook-api/template.js} +0 -0
- package/dist/js/modern/libs/render/cache/type.js +0 -1
- package/dist/js/modern/libs/route/index.js +4 -0
- package/dist/js/modern/libs/route/matcher.js +5 -1
- package/dist/js/modern/libs/serve-file.js +23 -25
- package/dist/js/modern/server/{web-server.js → dev-server/dev-server-split.js} +9 -7
- package/dist/js/modern/server/{dev-server.js → dev-server/dev-server.js} +41 -22
- package/dist/js/modern/server/dev-server/index.js +2 -0
- package/dist/js/modern/server/index.js +62 -62
- package/dist/js/modern/server/{api-server.js → modern-server-split.js} +6 -10
- package/dist/js/modern/server/modern-server.js +76 -27
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +2 -2
- package/dist/js/node/libs/hook-api/route.js +46 -0
- package/dist/js/node/libs/{hook-api.js → hook-api/template.js} +0 -0
- package/dist/js/node/libs/route/index.js +4 -0
- package/dist/js/node/libs/route/matcher.js +5 -1
- package/dist/js/node/libs/serve-file.js +25 -26
- package/dist/js/node/server/{api-server.js → dev-server/dev-server-split.js} +7 -12
- package/dist/js/node/server/{dev-server.js → dev-server/dev-server.js} +42 -21
- package/dist/js/node/server/dev-server/index.js +27 -0
- package/dist/js/node/server/index.js +69 -63
- package/dist/js/node/server/{web-server.js → modern-server-split.js} +10 -9
- package/dist/js/node/server/modern-server.js +78 -27
- package/dist/js/node/utils.js +2 -2
- package/dist/types/libs/hook-api/route.d.ts +13 -0
- package/dist/types/libs/{hook-api.d.ts → hook-api/template.d.ts} +0 -0
- package/dist/types/libs/route/index.d.ts +1 -0
- package/dist/types/libs/route/matcher.d.ts +1 -0
- package/dist/types/libs/serve-file.d.ts +1 -1
- package/dist/types/server/{web-server.d.ts → dev-server/dev-server-split.d.ts} +8 -8
- package/dist/types/server/{dev-server.d.ts → dev-server/dev-server.d.ts} +6 -5
- package/dist/types/server/dev-server/index.d.ts +2 -0
- package/dist/types/server/index.d.ts +3 -1
- package/dist/types/server/{api-server.d.ts → modern-server-split.d.ts} +4 -6
- package/dist/types/server/modern-server.d.ts +5 -3
- package/dist/types/utils.d.ts +1 -1
- package/package.json +17 -19
- package/src/libs/hook-api/route.ts +38 -0
- package/src/libs/{hook-api.ts → hook-api/template.ts} +0 -0
- package/src/libs/route/index.ts +4 -0
- package/src/libs/route/matcher.ts +5 -1
- package/src/libs/serve-file.ts +16 -20
- package/src/server/{web-server.ts → dev-server/dev-server-split.ts} +11 -10
- package/src/server/{dev-server.ts → dev-server/dev-server.ts} +56 -23
- package/src/server/dev-server/index.ts +2 -0
- package/src/server/index.ts +72 -46
- package/src/server/{api-server.ts → modern-server-split.ts} +10 -13
- package/src/server/modern-server.ts +82 -41
- package/src/utils.ts +2 -2
|
@@ -5,30 +5,35 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.Server = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
var _https = require("https");
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
9
|
|
|
12
10
|
var _serverPlugin = require("@modern-js/server-plugin");
|
|
13
11
|
|
|
14
12
|
var _utils = require("@modern-js/utils");
|
|
15
13
|
|
|
14
|
+
var _core = require("@modern-js/core");
|
|
15
|
+
|
|
16
16
|
var _modernServer = require("./modern-server");
|
|
17
17
|
|
|
18
|
+
var _modernServerSplit = require("./modern-server-split");
|
|
19
|
+
|
|
18
20
|
var _measure = require("../libs/measure");
|
|
19
21
|
|
|
22
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
+
|
|
24
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
25
|
+
|
|
26
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
27
|
+
|
|
28
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
29
|
+
|
|
20
30
|
class Server {
|
|
21
31
|
constructor(options) {
|
|
22
|
-
var _options$plugins;
|
|
23
|
-
|
|
24
32
|
this.options = void 0;
|
|
25
33
|
this.server = void 0;
|
|
26
34
|
this.app = void 0;
|
|
27
35
|
this.runner = void 0;
|
|
28
36
|
this.options = options;
|
|
29
|
-
(_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.forEach(p => {
|
|
30
|
-
_serverPlugin.serverManager.usePlugin(p);
|
|
31
|
-
});
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
getRequestHandler() {
|
|
@@ -46,40 +51,20 @@ class Server {
|
|
|
46
51
|
const {
|
|
47
52
|
options
|
|
48
53
|
} = this;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
logger,
|
|
52
|
-
measure
|
|
53
|
-
} = await this.runner.create({
|
|
54
|
-
loggerOptions: options.logger,
|
|
55
|
-
measureOptions: options.measure
|
|
56
|
-
}, {
|
|
57
|
-
onLast: () => ({})
|
|
58
|
-
});
|
|
59
|
-
options.logger = options.logger || logger || _utils.logger;
|
|
60
|
-
options.measure = options.measure || measure || _measure.measure;
|
|
54
|
+
options.logger = options.logger || _utils.logger;
|
|
55
|
+
options.measure = options.measure || _measure.measure; // initialize server
|
|
61
56
|
|
|
62
57
|
if (options.dev) {
|
|
63
|
-
this.server = this.createDevServer();
|
|
64
|
-
|
|
65
|
-
const devHttpsOption = typeof options.dev === 'object' && options.dev.https;
|
|
66
|
-
|
|
67
|
-
if (devHttpsOption) {
|
|
68
|
-
const {
|
|
69
|
-
genHttpsOptions
|
|
70
|
-
} = require("../dev-tools/https");
|
|
71
|
-
|
|
72
|
-
const httpsOptions = await genHttpsOptions(devHttpsOption);
|
|
73
|
-
this.app = (0, _https.createServer)(httpsOptions, this.getRequestHandler());
|
|
74
|
-
} else {
|
|
75
|
-
this.app = (0, _http.createServer)(this.getRequestHandler());
|
|
76
|
-
}
|
|
58
|
+
this.server = this.createDevServer();
|
|
77
59
|
} else {
|
|
78
60
|
this.server = this.createProdServer();
|
|
79
|
-
|
|
80
|
-
|
|
61
|
+
} // check if https is configured when start dev server
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
this.app = await this.server.createHTTPServer(this.getRequestHandler());
|
|
65
|
+
this.runner = await this.createHookRunner(); // runner can only be used after server init
|
|
81
66
|
|
|
82
|
-
await this.server.init();
|
|
67
|
+
await this.server.init(this.runner);
|
|
83
68
|
return this;
|
|
84
69
|
}
|
|
85
70
|
|
|
@@ -110,19 +95,11 @@ class Server {
|
|
|
110
95
|
} = this;
|
|
111
96
|
|
|
112
97
|
if (options.apiOnly) {
|
|
113
|
-
|
|
114
|
-
APIModernServer
|
|
115
|
-
} = require("./api-server");
|
|
116
|
-
|
|
117
|
-
return new APIModernServer(options, this.runner);
|
|
98
|
+
return new _modernServerSplit.APIModernServer(options);
|
|
118
99
|
} else if (options.webOnly) {
|
|
119
|
-
|
|
120
|
-
WebModernServer
|
|
121
|
-
} = require("./web-server");
|
|
122
|
-
|
|
123
|
-
return new WebModernServer(options, this.runner);
|
|
100
|
+
return new _modernServerSplit.WebModernServer(options);
|
|
124
101
|
} else {
|
|
125
|
-
return new _modernServer.ModernServer(options
|
|
102
|
+
return new _modernServer.ModernServer(options);
|
|
126
103
|
}
|
|
127
104
|
}
|
|
128
105
|
|
|
@@ -131,27 +108,56 @@ class Server {
|
|
|
131
108
|
options
|
|
132
109
|
} = this;
|
|
133
110
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
111
|
+
const {
|
|
112
|
+
APIModernDevServer,
|
|
113
|
+
WebModernDevServer,
|
|
114
|
+
ModernDevServer
|
|
115
|
+
} = require("./dev-server");
|
|
138
116
|
|
|
139
|
-
|
|
117
|
+
if (options.apiOnly) {
|
|
118
|
+
return new APIModernDevServer(options);
|
|
140
119
|
} else if (options.webOnly) {
|
|
141
|
-
|
|
142
|
-
WebModernDevServer
|
|
143
|
-
} = require("./web-server");
|
|
144
|
-
|
|
145
|
-
return new WebModernDevServer(options, this.runner);
|
|
120
|
+
return new WebModernDevServer(options);
|
|
146
121
|
} else {
|
|
147
|
-
|
|
148
|
-
ModernDevServer
|
|
149
|
-
} = require("./dev-server");
|
|
150
|
-
|
|
151
|
-
return new ModernDevServer(options, this.runner);
|
|
122
|
+
return new ModernDevServer(options);
|
|
152
123
|
}
|
|
153
124
|
}
|
|
154
125
|
|
|
126
|
+
async createHookRunner() {
|
|
127
|
+
var _options$plugins;
|
|
128
|
+
|
|
129
|
+
const {
|
|
130
|
+
options
|
|
131
|
+
} = this;
|
|
132
|
+
const appContext = await this.initAppContext();
|
|
133
|
+
|
|
134
|
+
_serverPlugin.serverManager.run(() => {
|
|
135
|
+
_core.ConfigContext.set(this.options.config);
|
|
136
|
+
|
|
137
|
+
_core.AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
|
|
138
|
+
distDirectory: _path.default.join(options.pwd, options.config.output.path || 'dist')
|
|
139
|
+
}));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
(_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.forEach(p => {
|
|
143
|
+
_serverPlugin.serverManager.usePlugin(p);
|
|
144
|
+
});
|
|
145
|
+
return _serverPlugin.serverManager.init({});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async initAppContext() {
|
|
149
|
+
var _this$options$plugins;
|
|
150
|
+
|
|
151
|
+
const appDirectory = await (0, _core.initAppDir)();
|
|
152
|
+
const loaded = await (0, _core.loadUserConfig)(appDirectory);
|
|
153
|
+
const plugins = (_this$options$plugins = this.options.plugins) === null || _this$options$plugins === void 0 ? void 0 : _this$options$plugins.map(p => ({
|
|
154
|
+
server: p,
|
|
155
|
+
cli: undefined
|
|
156
|
+
}));
|
|
157
|
+
const appContext = (0, _core.initAppContext)(appDirectory, plugins || [], loaded.filePath);
|
|
158
|
+
return appContext;
|
|
159
|
+
}
|
|
160
|
+
|
|
155
161
|
}
|
|
156
162
|
|
|
157
163
|
exports.Server = Server;
|
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.WebModernServer = exports.
|
|
7
|
-
|
|
8
|
-
var _devServer = require("./dev-server");
|
|
6
|
+
exports.WebModernServer = exports.APIModernServer = void 0;
|
|
9
7
|
|
|
10
8
|
var _modernServer = require("./modern-server");
|
|
11
9
|
|
|
@@ -26,19 +24,22 @@ class WebModernServer extends _modernServer.ModernServer {
|
|
|
26
24
|
|
|
27
25
|
exports.WebModernServer = WebModernServer;
|
|
28
26
|
|
|
29
|
-
class
|
|
30
|
-
|
|
27
|
+
class APIModernServer extends _modernServer.ModernServer {
|
|
28
|
+
prepareWebHandler(_) {
|
|
31
29
|
return null;
|
|
32
30
|
}
|
|
33
31
|
|
|
34
|
-
async
|
|
35
|
-
return super.
|
|
32
|
+
async prepareAPIHandler(mode, extension) {
|
|
33
|
+
return super.prepareAPIHandler(mode, extension);
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
filterRoutes(routes) {
|
|
39
|
-
return routes.filter(route => route.
|
|
37
|
+
return routes.filter(route => route.isApi);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async preServerInit() {// noop
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
exports.
|
|
45
|
+
exports.APIModernServer = APIModernServer;
|
|
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.ModernServer = void 0;
|
|
7
7
|
|
|
8
|
+
var _http = require("http");
|
|
9
|
+
|
|
8
10
|
var _util = _interopRequireDefault(require("util"));
|
|
9
11
|
|
|
10
12
|
var _path = _interopRequireDefault(require("path"));
|
|
@@ -33,7 +35,9 @@ var _context = require("../libs/context");
|
|
|
33
35
|
|
|
34
36
|
var _constants = require("../constants");
|
|
35
37
|
|
|
36
|
-
var
|
|
38
|
+
var _template = require("../libs/hook-api/template");
|
|
39
|
+
|
|
40
|
+
var _route2 = require("../libs/hook-api/route");
|
|
37
41
|
|
|
38
42
|
const _excluded = ["getMiddlewares"];
|
|
39
43
|
|
|
@@ -68,7 +72,7 @@ class ModernServer {
|
|
|
68
72
|
staticGenerate,
|
|
69
73
|
logger,
|
|
70
74
|
measure
|
|
71
|
-
}
|
|
75
|
+
}) {
|
|
72
76
|
this.pwd = void 0;
|
|
73
77
|
this.distDir = void 0;
|
|
74
78
|
this.workDir = void 0;
|
|
@@ -76,9 +80,9 @@ class ModernServer {
|
|
|
76
80
|
this.conf = void 0;
|
|
77
81
|
this.handlers = [];
|
|
78
82
|
this.presetRoutes = void 0;
|
|
83
|
+
this.runner = void 0;
|
|
79
84
|
this.logger = void 0;
|
|
80
85
|
this.measure = void 0;
|
|
81
|
-
this.runner = void 0;
|
|
82
86
|
this.isDev = false;
|
|
83
87
|
this.staticFileHandler = void 0;
|
|
84
88
|
this.routeRenderHandler = void 0;
|
|
@@ -95,7 +99,6 @@ class ModernServer {
|
|
|
95
99
|
this.distDir = _path.default.join(pwd, config.output.path || '');
|
|
96
100
|
this.workDir = this.isDev ? pwd : this.distDir;
|
|
97
101
|
this.conf = config;
|
|
98
|
-
this.runner = runner;
|
|
99
102
|
this.logger = logger;
|
|
100
103
|
this.measure = measure;
|
|
101
104
|
this.router = new _route.RouteMatchManager();
|
|
@@ -114,9 +117,10 @@ class ModernServer {
|
|
|
114
117
|
} // server prepare
|
|
115
118
|
|
|
116
119
|
|
|
117
|
-
async init() {
|
|
120
|
+
async init(runner) {
|
|
118
121
|
var _conf$bff;
|
|
119
122
|
|
|
123
|
+
this.runner = runner;
|
|
120
124
|
const {
|
|
121
125
|
distDir,
|
|
122
126
|
isDev,
|
|
@@ -148,12 +152,17 @@ class ModernServer {
|
|
|
148
152
|
}
|
|
149
153
|
|
|
150
154
|
await this.prepareFrameHandler();
|
|
155
|
+
const {
|
|
156
|
+
favicon,
|
|
157
|
+
faviconByEntries
|
|
158
|
+
} = this.conf.output;
|
|
159
|
+
const favicons = this.prepareFavicons(favicon, faviconByEntries); // Only work when without setting `assetPrefix`.
|
|
160
|
+
// Setting `assetPrefix` means these resources should be uploaded to CDN.
|
|
161
|
+
|
|
162
|
+
const staticPathRegExp = new RegExp(`^/(static/|upload/|favicon.ico|icon.png${favicons.length > 0 ? `|${favicons.join('|')}` : ''})`);
|
|
151
163
|
this.staticFileHandler = (0, _serveFile.createStaticFileHandler)([{
|
|
152
|
-
path:
|
|
153
|
-
target:
|
|
154
|
-
}, {
|
|
155
|
-
path: '/upload/',
|
|
156
|
-
target: _path.default.join(distDir, 'upload')
|
|
164
|
+
path: staticPathRegExp,
|
|
165
|
+
target: distDir
|
|
157
166
|
}]);
|
|
158
167
|
this.routeRenderHandler = (0, _render.createRenderHandler)({
|
|
159
168
|
distDir,
|
|
@@ -176,6 +185,10 @@ class ModernServer {
|
|
|
176
185
|
|
|
177
186
|
close() {
|
|
178
187
|
reader.close();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async createHTTPServer(handler) {
|
|
191
|
+
return (0, _http.createServer)(handler);
|
|
179
192
|
} // warmup ssr function
|
|
180
193
|
|
|
181
194
|
|
|
@@ -225,12 +238,7 @@ class ModernServer {
|
|
|
225
238
|
const {
|
|
226
239
|
workDir,
|
|
227
240
|
runner
|
|
228
|
-
} = this; //
|
|
229
|
-
|
|
230
|
-
const {
|
|
231
|
-
api: userAPIExt,
|
|
232
|
-
web: userWebExt
|
|
233
|
-
} = (0, _serverUtils.gather)(workDir); // server hook, gather plugin inject
|
|
241
|
+
} = this; // server hook, gather plugin inject
|
|
234
242
|
|
|
235
243
|
const _createMiddlewareColl = (0, _serverUtils.createMiddlewareCollecter)(),
|
|
236
244
|
{
|
|
@@ -250,17 +258,15 @@ class ModernServer {
|
|
|
250
258
|
|
|
251
259
|
|
|
252
260
|
if (await _utils.fs.pathExists(_path.default.join(serverDir))) {
|
|
253
|
-
const webExtension = (0, _utils2.mergeExtension)(pluginWebExt
|
|
261
|
+
const webExtension = (0, _utils2.mergeExtension)(pluginWebExt);
|
|
254
262
|
this.frameWebHandler = await this.prepareWebHandler(webExtension);
|
|
255
263
|
}
|
|
256
264
|
|
|
257
265
|
if (_utils.fs.existsSync(apiDir)) {
|
|
258
266
|
const mode = _utils.fs.existsSync(_path.default.join(apiDir, _constants.AGGRED_DIR.lambda)) ? _constants.ApiServerMode.frame : _constants.ApiServerMode.func; // if use lambda/, mean framework style of writing, then discard user extension
|
|
259
267
|
|
|
260
|
-
const apiExtension = (0, _utils2.mergeExtension)(pluginAPIExt
|
|
261
|
-
this.frameAPIHandler = await this.prepareAPIHandler(mode,
|
|
262
|
-
modernJsConfig: this.conf
|
|
263
|
-
}));
|
|
268
|
+
const apiExtension = (0, _utils2.mergeExtension)(pluginAPIExt);
|
|
269
|
+
this.frameAPIHandler = await this.prepareAPIHandler(mode, apiExtension);
|
|
264
270
|
}
|
|
265
271
|
}
|
|
266
272
|
/* —————————————————————— function will be overwrite —————————————————————— */
|
|
@@ -293,7 +299,7 @@ class ModernServer {
|
|
|
293
299
|
pwd: workDir,
|
|
294
300
|
mode,
|
|
295
301
|
config: extension,
|
|
296
|
-
prefix
|
|
302
|
+
prefix: Array.isArray(prefix) ? prefix[0] : prefix
|
|
297
303
|
}, {
|
|
298
304
|
onLast: () => null
|
|
299
305
|
});
|
|
@@ -308,10 +314,30 @@ class ModernServer {
|
|
|
308
314
|
conf
|
|
309
315
|
} = this;
|
|
310
316
|
const preMiddleware = await this.runner.preServerInit(conf);
|
|
311
|
-
preMiddleware.forEach(mid => {
|
|
317
|
+
preMiddleware.flat().forEach(mid => {
|
|
312
318
|
this.addHandler(mid);
|
|
313
319
|
});
|
|
314
320
|
}
|
|
321
|
+
|
|
322
|
+
prepareFavicons(favicon, faviconByEntries) {
|
|
323
|
+
const faviconNames = [];
|
|
324
|
+
|
|
325
|
+
if (favicon) {
|
|
326
|
+
faviconNames.push(favicon.substring(favicon.lastIndexOf('/') + 1));
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (faviconByEntries) {
|
|
330
|
+
Object.keys(faviconByEntries).forEach(f => {
|
|
331
|
+
const curFavicon = faviconByEntries[f];
|
|
332
|
+
|
|
333
|
+
if (curFavicon) {
|
|
334
|
+
faviconNames.push(curFavicon.substring(curFavicon.lastIndexOf('/') + 1));
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return faviconNames;
|
|
340
|
+
}
|
|
315
341
|
/* —————————————————————— private function —————————————————————— */
|
|
316
342
|
// handler route.json, include api / csr / ssr
|
|
317
343
|
// eslint-disable-next-line max-statements
|
|
@@ -321,7 +347,12 @@ class ModernServer {
|
|
|
321
347
|
const {
|
|
322
348
|
req,
|
|
323
349
|
res
|
|
324
|
-
} = context;
|
|
350
|
+
} = context;
|
|
351
|
+
await this.runner.beforeMatch({
|
|
352
|
+
context
|
|
353
|
+
}, {
|
|
354
|
+
onLast: _utils2.noop
|
|
355
|
+
}); // match routes in the route spec
|
|
325
356
|
|
|
326
357
|
const matched = this.router.match(context.url);
|
|
327
358
|
|
|
@@ -330,8 +361,23 @@ class ModernServer {
|
|
|
330
361
|
return;
|
|
331
362
|
}
|
|
332
363
|
|
|
333
|
-
const
|
|
334
|
-
|
|
364
|
+
const routeAPI = (0, _route2.createRouteAPI)(matched, this.router);
|
|
365
|
+
await this.runner.afterMatch({
|
|
366
|
+
context,
|
|
367
|
+
routeAPI
|
|
368
|
+
}, {
|
|
369
|
+
onLast: _utils2.noop
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
if (res.headersSent) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const {
|
|
377
|
+
current
|
|
378
|
+
} = routeAPI;
|
|
379
|
+
const route = current.generate();
|
|
380
|
+
const params = current.parseURLParams(context.url);
|
|
335
381
|
context.setParams(params); // route is api service
|
|
336
382
|
|
|
337
383
|
if (route.isApi) {
|
|
@@ -352,6 +398,11 @@ class ModernServer {
|
|
|
352
398
|
return;
|
|
353
399
|
}
|
|
354
400
|
|
|
401
|
+
await this.runner.beforeRender({
|
|
402
|
+
context
|
|
403
|
+
}, {
|
|
404
|
+
onLast: _utils2.noop
|
|
405
|
+
});
|
|
355
406
|
const file = await this.routeRenderHandler(context, route);
|
|
356
407
|
|
|
357
408
|
if (!file) {
|
|
@@ -369,7 +420,7 @@ class ModernServer {
|
|
|
369
420
|
let response = file.content;
|
|
370
421
|
|
|
371
422
|
if (route.entryName) {
|
|
372
|
-
const templateAPI = (0,
|
|
423
|
+
const templateAPI = (0, _template.createTemplateAPI)(file.content.toString());
|
|
373
424
|
await this.runner.afterRender({
|
|
374
425
|
context,
|
|
375
426
|
templateAPI
|
package/dist/js/node/utils.js
CHANGED
|
@@ -5,10 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.toMessage = exports.noop = exports.mergeExtension = exports.createErrorDocument = void 0;
|
|
7
7
|
|
|
8
|
-
const mergeExtension =
|
|
8
|
+
const mergeExtension = users => {
|
|
9
9
|
const output = [];
|
|
10
10
|
return {
|
|
11
|
-
middleware: output.concat(users)
|
|
11
|
+
middleware: output.concat(users)
|
|
12
12
|
};
|
|
13
13
|
};
|
|
14
14
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RouteMatchManager, RouteMatcher } from '../route';
|
|
2
|
+
|
|
3
|
+
declare class RouteAPI {
|
|
4
|
+
private readonly router;
|
|
5
|
+
private current;
|
|
6
|
+
constructor(matched: RouteMatcher, router: RouteMatchManager);
|
|
7
|
+
cur(): import("../route").ModernRoute;
|
|
8
|
+
get(entryName: string): import("../route").ModernRoute | null;
|
|
9
|
+
use(entryName: string): boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export declare const createRouteAPI: (matched: RouteMatcher, router: RouteMatchManager) => RouteAPI;
|
|
13
|
+
export {};
|
|
File without changes
|
|
@@ -8,6 +8,7 @@ export declare class RouteMatchManager {
|
|
|
8
8
|
private best;
|
|
9
9
|
reset(specs: ModernRouteInterface[]): void;
|
|
10
10
|
match(pathname: string): RouteMatcher | undefined;
|
|
11
|
+
matchEntry(entryname: string): RouteMatcher | undefined;
|
|
11
12
|
getBundles(): (string | undefined)[];
|
|
12
13
|
}
|
|
13
14
|
export type { ModernRouteInterface, RouteMatcher, ModernRoute };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { NextFunction } from '../type';
|
|
2
2
|
import { ModernServerContext } from './context';
|
|
3
3
|
declare type Rule = {
|
|
4
|
-
path: string;
|
|
4
|
+
path: string | RegExp;
|
|
5
5
|
target: string;
|
|
6
6
|
};
|
|
7
7
|
export declare const createStaticFileHandler: (rules: Rule[]) => (context: ModernServerContext, next: NextFunction) => Promise<void>;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import type { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
2
|
import { ModernDevServer } from './dev-server';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
export declare class WebModernServer extends ModernServer {
|
|
3
|
+
import { mergeExtension } from "../../utils.d";
|
|
4
|
+
import { ModernRouteInterface } from "../../libs/route";
|
|
5
|
+
import { ApiServerMode } from "../../constants.d";
|
|
6
|
+
export declare class WebModernDevServer extends ModernDevServer {
|
|
8
7
|
protected prepareAPIHandler(_m: ApiServerMode, _: APIServerStartInput['config']): any;
|
|
9
8
|
protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
10
9
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
11
10
|
}
|
|
12
|
-
export declare class
|
|
13
|
-
protected
|
|
14
|
-
protected
|
|
11
|
+
export declare class APIModernDevServer extends ModernDevServer {
|
|
12
|
+
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>): any;
|
|
13
|
+
protected prepareAPIHandler(mode: ApiServerMode, extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
15
14
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
15
|
+
protected preServerInit(): Promise<void>;
|
|
16
16
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Server } from 'http';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import http, { Server, IncomingMessage, ServerResponse } from 'http';
|
|
3
|
+
import { ModernServer } from '../modern-server';
|
|
4
|
+
import { ModernServerOptions, ServerHookRunner, ReadyOptions } from "../../type.d";
|
|
5
5
|
export declare class ModernDevServer extends ModernServer {
|
|
6
6
|
private devProxyHandler;
|
|
7
7
|
private mockHandler;
|
|
@@ -10,11 +10,12 @@ export declare class ModernDevServer extends ModernServer {
|
|
|
10
10
|
private socketServer;
|
|
11
11
|
private watcher;
|
|
12
12
|
private devMiddleware;
|
|
13
|
-
constructor(options: ModernServerOptions
|
|
14
|
-
init(): Promise<void>;
|
|
13
|
+
constructor(options: ModernServerOptions);
|
|
14
|
+
init(runner: ServerHookRunner): Promise<void>;
|
|
15
15
|
ready(options?: ReadyOptions): void;
|
|
16
16
|
onListening(app: Server): void;
|
|
17
17
|
close(): Promise<void>;
|
|
18
|
+
createHTTPServer(handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void): Promise<http.Server | import("https").Server>;
|
|
18
19
|
private setupCompiler;
|
|
19
20
|
private setupDevServerPlugin;
|
|
20
21
|
private setupHooks;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { IncomingMessage, ServerResponse, Server as httpServer } from 'http';
|
|
3
|
-
import { ModernServerOptions, ReadyOptions } from
|
|
3
|
+
import { ModernServerOptions, ReadyOptions } from "../type.d";
|
|
4
4
|
export declare class Server {
|
|
5
5
|
options: ModernServerOptions;
|
|
6
6
|
private server;
|
|
@@ -15,4 +15,6 @@ export declare class Server {
|
|
|
15
15
|
close(): Promise<void>;
|
|
16
16
|
private createProdServer;
|
|
17
17
|
private createDevServer;
|
|
18
|
+
private createHookRunner;
|
|
19
|
+
private initAppContext;
|
|
18
20
|
}
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
|
-
import { ModernDevServer } from './dev-server';
|
|
3
2
|
import { ModernServer } from './modern-server';
|
|
4
3
|
import { mergeExtension } from "../utils.d";
|
|
5
4
|
import { ModernRouteInterface } from "../libs/route";
|
|
6
5
|
import { ApiServerMode } from "../constants.d";
|
|
7
|
-
export declare class
|
|
8
|
-
protected
|
|
9
|
-
protected
|
|
6
|
+
export declare class WebModernServer extends ModernServer {
|
|
7
|
+
protected prepareAPIHandler(_m: ApiServerMode, _: ReturnType<typeof mergeExtension>): any;
|
|
8
|
+
protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
10
9
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
11
|
-
protected preServerInit(): Promise<void>;
|
|
12
10
|
}
|
|
13
|
-
export declare class
|
|
11
|
+
export declare class APIModernServer extends ModernServer {
|
|
14
12
|
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>): any;
|
|
15
13
|
protected prepareAPIHandler(mode: ApiServerMode, extension: APIServerStartInput['config']): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
16
14
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
@@ -17,9 +17,9 @@ export declare class ModernServer {
|
|
|
17
17
|
protected conf: NormalizedConfig;
|
|
18
18
|
protected handlers: ModernServerAsyncHandler[];
|
|
19
19
|
protected presetRoutes?: ModernRouteInterface[];
|
|
20
|
+
protected runner: ServerHookRunner;
|
|
20
21
|
protected readonly logger: Logger;
|
|
21
22
|
protected readonly measure: Measure;
|
|
22
|
-
private readonly runner;
|
|
23
23
|
private readonly isDev;
|
|
24
24
|
private staticFileHandler;
|
|
25
25
|
private routeRenderHandler;
|
|
@@ -36,12 +36,13 @@ export declare class ModernServer {
|
|
|
36
36
|
staticGenerate,
|
|
37
37
|
logger,
|
|
38
38
|
measure
|
|
39
|
-
}: ModernServerOptions
|
|
39
|
+
}: ModernServerOptions);
|
|
40
40
|
getRequestHandler(): (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
|
|
41
|
-
init(): Promise<void>;
|
|
41
|
+
init(runner: ServerHookRunner): Promise<void>;
|
|
42
42
|
ready(_: ReadyOptions): void;
|
|
43
43
|
onListening(_: Server): void;
|
|
44
44
|
close(): void;
|
|
45
|
+
createHTTPServer(handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void): Promise<Server>;
|
|
45
46
|
protected warmupSSRBundle(): void;
|
|
46
47
|
protected readRouteSpec(): ModernRouteInterface[];
|
|
47
48
|
protected addHandler(handler: ModernServerHandler): void;
|
|
@@ -51,6 +52,7 @@ export declare class ModernServer {
|
|
|
51
52
|
protected prepareAPIHandler(mode: ApiServerMode, extension: APIServerStartInput['config']): Promise<Adapter>;
|
|
52
53
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
53
54
|
protected preServerInit(): Promise<void>;
|
|
55
|
+
private prepareFavicons;
|
|
54
56
|
private routeHandler;
|
|
55
57
|
private injectMicroFE;
|
|
56
58
|
private compose;
|
package/dist/types/utils.d.ts
CHANGED