@modern-js/server 1.1.1-canary.3 → 1.1.3-beta.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 +28 -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/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 +61 -62
- package/dist/js/modern/server/{api-server.js → modern-server-split.js} +6 -10
- package/dist/js/modern/server/modern-server.js +78 -25
- 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 +67 -63
- package/dist/js/node/server/{web-server.js → modern-server-split.js} +10 -9
- package/dist/js/node/server/modern-server.js +80 -25
- 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/{api-server.d.ts → dev-server/dev-server-split.d.ts} +7 -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/{web-server.d.ts → modern-server-split.d.ts} +5 -4
- package/dist/types/server/modern-server.d.ts +7 -5
- package/dist/types/utils.d.ts +1 -1
- package/package.json +10 -10
- 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/{api-server.ts → dev-server/dev-server-split.ts} +9 -11
- 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 +69 -46
- package/src/server/{web-server.ts → modern-server-split.ts} +12 -10
- package/src/server/modern-server.ts +87 -40
- 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,54 @@ 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.AppContext.set(_objectSpread(_objectSpread({}, appContext), {}, {
|
|
136
|
+
distDirectory: _path.default.join(options.pwd, options.config.output.path || 'dist')
|
|
137
|
+
}));
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
(_options$plugins = options.plugins) === null || _options$plugins === void 0 ? void 0 : _options$plugins.forEach(p => {
|
|
141
|
+
_serverPlugin.serverManager.usePlugin(p);
|
|
142
|
+
});
|
|
143
|
+
return _serverPlugin.serverManager.init({});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async initAppContext() {
|
|
147
|
+
var _this$options$plugins;
|
|
148
|
+
|
|
149
|
+
const appDirectory = await (0, _core.initAppDir)();
|
|
150
|
+
const loaded = await (0, _core.loadUserConfig)(appDirectory);
|
|
151
|
+
const plugins = (_this$options$plugins = this.options.plugins) === null || _this$options$plugins === void 0 ? void 0 : _this$options$plugins.map(p => ({
|
|
152
|
+
server: p,
|
|
153
|
+
cli: undefined
|
|
154
|
+
}));
|
|
155
|
+
const appContext = (0, _core.initAppContext)(appDirectory, plugins || [], loaded.filePath);
|
|
156
|
+
return appContext;
|
|
157
|
+
}
|
|
158
|
+
|
|
155
159
|
}
|
|
156
160
|
|
|
157
161
|
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,15 +258,17 @@ 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, apiExtension)
|
|
268
|
+
const apiExtension = (0, _utils2.mergeExtension)(pluginAPIExt);
|
|
269
|
+
this.frameAPIHandler = await this.prepareAPIHandler(mode, _objectSpread(_objectSpread({}, apiExtension), {}, {
|
|
270
|
+
modernJsConfig: this.conf
|
|
271
|
+
}));
|
|
262
272
|
}
|
|
263
273
|
}
|
|
264
274
|
/* —————————————————————— function will be overwrite —————————————————————— */
|
|
@@ -291,7 +301,7 @@ class ModernServer {
|
|
|
291
301
|
pwd: workDir,
|
|
292
302
|
mode,
|
|
293
303
|
config: extension,
|
|
294
|
-
prefix
|
|
304
|
+
prefix: Array.isArray(prefix) ? prefix[0] : prefix
|
|
295
305
|
}, {
|
|
296
306
|
onLast: () => null
|
|
297
307
|
});
|
|
@@ -306,10 +316,30 @@ class ModernServer {
|
|
|
306
316
|
conf
|
|
307
317
|
} = this;
|
|
308
318
|
const preMiddleware = await this.runner.preServerInit(conf);
|
|
309
|
-
preMiddleware.forEach(mid => {
|
|
319
|
+
preMiddleware.flat().forEach(mid => {
|
|
310
320
|
this.addHandler(mid);
|
|
311
321
|
});
|
|
312
322
|
}
|
|
323
|
+
|
|
324
|
+
prepareFavicons(favicon, faviconByEntries) {
|
|
325
|
+
const faviconNames = [];
|
|
326
|
+
|
|
327
|
+
if (favicon) {
|
|
328
|
+
faviconNames.push(favicon.substring(favicon.lastIndexOf('/') + 1));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (faviconByEntries) {
|
|
332
|
+
Object.keys(faviconByEntries).forEach(f => {
|
|
333
|
+
const curFavicon = faviconByEntries[f];
|
|
334
|
+
|
|
335
|
+
if (curFavicon) {
|
|
336
|
+
faviconNames.push(curFavicon.substring(curFavicon.lastIndexOf('/') + 1));
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return faviconNames;
|
|
342
|
+
}
|
|
313
343
|
/* —————————————————————— private function —————————————————————— */
|
|
314
344
|
// handler route.json, include api / csr / ssr
|
|
315
345
|
// eslint-disable-next-line max-statements
|
|
@@ -319,7 +349,12 @@ class ModernServer {
|
|
|
319
349
|
const {
|
|
320
350
|
req,
|
|
321
351
|
res
|
|
322
|
-
} = context;
|
|
352
|
+
} = context;
|
|
353
|
+
await this.runner.beforeMatch({
|
|
354
|
+
context
|
|
355
|
+
}, {
|
|
356
|
+
onLast: _utils2.noop
|
|
357
|
+
}); // match routes in the route spec
|
|
323
358
|
|
|
324
359
|
const matched = this.router.match(context.url);
|
|
325
360
|
|
|
@@ -328,8 +363,23 @@ class ModernServer {
|
|
|
328
363
|
return;
|
|
329
364
|
}
|
|
330
365
|
|
|
331
|
-
const
|
|
332
|
-
|
|
366
|
+
const routeAPI = (0, _route2.createRouteAPI)(matched, this.router);
|
|
367
|
+
await this.runner.afterMatch({
|
|
368
|
+
context,
|
|
369
|
+
routeAPI
|
|
370
|
+
}, {
|
|
371
|
+
onLast: _utils2.noop
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
if (res.headersSent) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const {
|
|
379
|
+
current
|
|
380
|
+
} = routeAPI;
|
|
381
|
+
const route = current.generate();
|
|
382
|
+
const params = current.parseURLParams(context.url);
|
|
333
383
|
context.setParams(params); // route is api service
|
|
334
384
|
|
|
335
385
|
if (route.isApi) {
|
|
@@ -350,6 +400,11 @@ class ModernServer {
|
|
|
350
400
|
return;
|
|
351
401
|
}
|
|
352
402
|
|
|
403
|
+
await this.runner.beforeRender({
|
|
404
|
+
context
|
|
405
|
+
}, {
|
|
406
|
+
onLast: _utils2.noop
|
|
407
|
+
});
|
|
353
408
|
const file = await this.routeRenderHandler(context, route);
|
|
354
409
|
|
|
355
410
|
if (!file) {
|
|
@@ -367,7 +422,7 @@ class ModernServer {
|
|
|
367
422
|
let response = file.content;
|
|
368
423
|
|
|
369
424
|
if (route.entryName) {
|
|
370
|
-
const templateAPI = (0,
|
|
425
|
+
const templateAPI = (0, _template.createTemplateAPI)(file.content.toString());
|
|
371
426
|
await this.runner.afterRender({
|
|
372
427
|
context,
|
|
373
428
|
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,13 +1,12 @@
|
|
|
1
|
+
import type { APIServerStartInput } from '@modern-js/server-plugin';
|
|
1
2
|
import { ModernDevServer } from './dev-server';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
protected prepareWebHandler(
|
|
8
|
-
protected prepareAPIHandler(mode: ApiServerMode, extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
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 {
|
|
7
|
+
protected prepareAPIHandler(_m: ApiServerMode, _: APIServerStartInput['config']): any;
|
|
8
|
+
protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
9
9
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
10
|
-
protected preServerInit(): Promise<void>;
|
|
11
10
|
}
|
|
12
11
|
export declare class APIModernDevServer extends ModernDevServer {
|
|
13
12
|
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>): any;
|
|
@@ -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,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { APIServerStartInput } from '@modern-js/server-plugin';
|
|
2
2
|
import { ModernServer } from './modern-server';
|
|
3
3
|
import { mergeExtension } from "../utils.d";
|
|
4
4
|
import { ModernRouteInterface } from "../libs/route";
|
|
@@ -8,8 +8,9 @@ export declare class WebModernServer extends ModernServer {
|
|
|
8
8
|
protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
9
9
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
10
10
|
}
|
|
11
|
-
export declare class
|
|
12
|
-
protected
|
|
13
|
-
protected
|
|
11
|
+
export declare class APIModernServer extends ModernServer {
|
|
12
|
+
protected prepareWebHandler(_: ReturnType<typeof mergeExtension>): any;
|
|
13
|
+
protected prepareAPIHandler(mode: ApiServerMode, extension: APIServerStartInput['config']): Promise<import("@modern-js/server-plugin").Adapter>;
|
|
14
14
|
protected filterRoutes(routes: ModernRouteInterface[]): ModernRouteInterface[];
|
|
15
|
+
protected preServerInit(): Promise<void>;
|
|
15
16
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { IncomingMessage, ServerResponse, Server } from 'http';
|
|
3
|
-
import { Adapter } from '@modern-js/server-plugin';
|
|
3
|
+
import { Adapter, APIServerStartInput } from '@modern-js/server-plugin';
|
|
4
4
|
import type { NormalizedConfig } from '@modern-js/core';
|
|
5
5
|
import { ModernServerOptions, NextFunction, ServerHookRunner, Measure, Logger, ReadyOptions } from '../type';
|
|
6
6
|
import { RouteMatchManager, ModernRouteInterface } from '../libs/route';
|
|
@@ -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,21 +36,23 @@ 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;
|
|
48
49
|
protected render404(context: ModernServerContext): void;
|
|
49
50
|
protected prepareFrameHandler(): Promise<void>;
|
|
50
51
|
protected prepareWebHandler(extension: ReturnType<typeof mergeExtension>): Promise<Adapter>;
|
|
51
|
-
protected prepareAPIHandler(mode: ApiServerMode, extension:
|
|
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