@modern-js/server 1.4.1 → 1.4.4-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 +22 -0
- package/dist/js/modern/dev-tools/mock/index.js +1 -1
- package/dist/js/modern/dev-tools/socket-server.js +63 -46
- package/dist/js/modern/index.js +1 -1
- package/dist/js/modern/server/{dev-server/dev-server-split.js → dev-server-split.js} +0 -0
- package/dist/js/modern/server/{dev-server/dev-server.js → dev-server.js} +19 -14
- package/dist/js/modern/server/index.js +15 -138
- package/dist/js/modern/{libs/render/cache/type.js → types.js} +0 -0
- package/dist/js/node/dev-tools/mock/index.js +2 -2
- package/dist/js/node/dev-tools/socket-server.js +63 -47
- package/dist/js/node/index.js +2 -2
- package/dist/js/node/server/{dev-server/dev-server-split.js → dev-server-split.js} +0 -0
- package/dist/js/node/server/{dev-server/dev-server.js → dev-server.js} +22 -24
- package/dist/js/node/server/index.js +17 -152
- package/dist/js/node/{libs/render/cache/type.js → types.js} +0 -0
- package/dist/types/dev-tools/babel/register.d.ts +1 -2
- package/dist/types/dev-tools/dev-server-plugin.d.ts +1 -1
- package/dist/types/dev-tools/launch-editor/index.d.ts +1 -2
- package/dist/types/dev-tools/mock/getMockData.d.ts +1 -2
- package/dist/types/dev-tools/mock/index.d.ts +1 -2
- package/dist/types/dev-tools/socket-server.d.ts +5 -1
- package/dist/types/index.d.ts +4 -5
- package/dist/types/server/{dev-server/dev-server-split.d.ts → dev-server-split.d.ts} +6 -5
- package/dist/types/server/{dev-server/dev-server.d.ts → dev-server.d.ts} +4 -3
- package/dist/types/server/index.d.ts +4 -19
- package/dist/types/types.d.ts +28 -0
- package/modern.config.js +5 -1
- package/package.json +13 -42
- package/tests/dev.test.ts +17 -0
- package/tests/server.test.ts +10 -3
- package/dist/js/modern/constants.js +0 -26
- package/dist/js/modern/libs/context/context.js +0 -180
- package/dist/js/modern/libs/context/index.js +0 -3
- package/dist/js/modern/libs/hook-api/route.js +0 -39
- package/dist/js/modern/libs/hook-api/template.js +0 -61
- package/dist/js/modern/libs/metrics.js +0 -12
- package/dist/js/modern/libs/proxy.js +0 -59
- package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -70
- package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -233
- package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +0 -53
- package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +0 -35
- package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +0 -121
- package/dist/js/modern/libs/render/cache/index.js +0 -74
- package/dist/js/modern/libs/render/cache/page-caches/index.js +0 -9
- package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -35
- package/dist/js/modern/libs/render/cache/spr.js +0 -280
- package/dist/js/modern/libs/render/cache/util.js +0 -79
- package/dist/js/modern/libs/render/index.js +0 -65
- package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
- package/dist/js/modern/libs/render/modern/index.js +0 -42
- package/dist/js/modern/libs/render/reader.js +0 -112
- package/dist/js/modern/libs/render/ssr.js +0 -58
- package/dist/js/modern/libs/render/static.js +0 -46
- package/dist/js/modern/libs/render/type.js +0 -7
- package/dist/js/modern/libs/route/index.js +0 -68
- package/dist/js/modern/libs/route/matcher.js +0 -94
- package/dist/js/modern/libs/route/route.js +0 -24
- package/dist/js/modern/libs/serve-file.js +0 -28
- package/dist/js/modern/server/dev-server/index.js +0 -2
- package/dist/js/modern/server/modern-server-split.js +0 -81
- package/dist/js/modern/server/modern-server.js +0 -584
- package/dist/js/modern/type.js +0 -1
- package/dist/js/modern/utils.js +0 -112
- package/dist/js/node/constants.js +0 -36
- package/dist/js/node/libs/context/context.js +0 -194
- package/dist/js/node/libs/context/index.js +0 -18
- package/dist/js/node/libs/hook-api/route.js +0 -48
- package/dist/js/node/libs/hook-api/template.js +0 -69
- package/dist/js/node/libs/metrics.js +0 -18
- package/dist/js/node/libs/proxy.js +0 -69
- package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -77
- package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -238
- package/dist/js/node/libs/render/cache/__tests__/cacheable.js +0 -60
- package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +0 -42
- package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +0 -128
- package/dist/js/node/libs/render/cache/index.js +0 -86
- package/dist/js/node/libs/render/cache/page-caches/index.js +0 -17
- package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -47
- package/dist/js/node/libs/render/cache/spr.js +0 -298
- package/dist/js/node/libs/render/cache/util.js +0 -105
- package/dist/js/node/libs/render/index.js +0 -91
- package/dist/js/node/libs/render/modern/browser-list.js +0 -14
- package/dist/js/node/libs/render/modern/index.js +0 -58
- package/dist/js/node/libs/render/reader.js +0 -139
- package/dist/js/node/libs/render/ssr.js +0 -76
- package/dist/js/node/libs/render/static.js +0 -62
- package/dist/js/node/libs/render/type.js +0 -14
- package/dist/js/node/libs/route/index.js +0 -83
- package/dist/js/node/libs/route/matcher.js +0 -108
- package/dist/js/node/libs/route/route.js +0 -33
- package/dist/js/node/libs/serve-file.js +0 -41
- package/dist/js/node/server/dev-server/index.js +0 -27
- package/dist/js/node/server/modern-server-split.js +0 -97
- package/dist/js/node/server/modern-server.js +0 -622
- package/dist/js/node/type.js +0 -5
- package/dist/js/node/utils.js +0 -143
- package/dist/types/constants.d.ts +0 -20
- package/dist/types/libs/context/context.d.ts +0 -61
- package/dist/types/libs/context/index.d.ts +0 -4
- package/dist/types/libs/hook-api/route.d.ts +0 -14
- package/dist/types/libs/hook-api/template.d.ts +0 -14
- package/dist/types/libs/metrics.d.ts +0 -3
- package/dist/types/libs/proxy.d.ts +0 -16
- package/dist/types/libs/render/cache/__tests__/cache.fun.test.d.ts +0 -1
- package/dist/types/libs/render/cache/__tests__/cache.test.d.ts +0 -1
- package/dist/types/libs/render/cache/__tests__/cacheable.d.ts +0 -62
- package/dist/types/libs/render/cache/__tests__/error-configuration.d.ts +0 -28
- package/dist/types/libs/render/cache/__tests__/matched-cache.d.ts +0 -124
- package/dist/types/libs/render/cache/index.d.ts +0 -6
- package/dist/types/libs/render/cache/page-caches/index.d.ts +0 -2
- package/dist/types/libs/render/cache/page-caches/lru.d.ts +0 -15
- package/dist/types/libs/render/cache/spr.d.ts +0 -24
- package/dist/types/libs/render/cache/type.d.ts +0 -48
- package/dist/types/libs/render/cache/util.d.ts +0 -17
- package/dist/types/libs/render/index.d.ts +0 -18
- package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
- package/dist/types/libs/render/modern/index.d.ts +0 -3
- package/dist/types/libs/render/reader.d.ts +0 -18
- package/dist/types/libs/render/ssr.d.ts +0 -10
- package/dist/types/libs/render/static.d.ts +0 -3
- package/dist/types/libs/render/type.d.ts +0 -34
- package/dist/types/libs/route/index.d.ts +0 -15
- package/dist/types/libs/route/matcher.d.ts +0 -15
- package/dist/types/libs/route/route.d.ts +0 -14
- package/dist/types/libs/serve-file.d.ts +0 -8
- package/dist/types/server/dev-server/index.d.ts +0 -2
- package/dist/types/server/modern-server-split.d.ts +0 -26
- package/dist/types/server/modern-server.d.ts +0 -72
- package/dist/types/type.d.ts +0 -80
- package/dist/types/utils.d.ts +0 -19
- package/tests/context.test.ts +0 -52
- package/tests/fixtures/hosting-files/static/index.js +0 -1
- package/tests/fixtures/reader/index.ts +0 -3
- package/tests/fixtures/route-spec/dynamic.json +0 -13
- package/tests/fixtures/route-spec/index.json +0 -29
- package/tests/fixtures/ssr/bundle.js +0 -5
- package/tests/fixtures/static-dir/bar.html +0 -11
- package/tests/fixtures/static-dir/baz/index.html +0 -11
- package/tests/fixtures/static-dir/foo/index.html +0 -11
- package/tests/helper.ts +0 -8
- package/tests/hook.test.ts +0 -44
- package/tests/middleware.test.ts +0 -179
- package/tests/render.test.ts +0 -102
- package/tests/route.test.ts +0 -77
- package/tests/utils.test.ts +0 -106
|
@@ -1,622 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.ModernServer = void 0;
|
|
7
|
-
|
|
8
|
-
var _http = require("http");
|
|
9
|
-
|
|
10
|
-
var _util = _interopRequireDefault(require("util"));
|
|
11
|
-
|
|
12
|
-
var _path = _interopRequireDefault(require("path"));
|
|
13
|
-
|
|
14
|
-
var _utils = require("@modern-js/utils");
|
|
15
|
-
|
|
16
|
-
var _mimeTypes = _interopRequireDefault(require("mime-types"));
|
|
17
|
-
|
|
18
|
-
var _axios = _interopRequireDefault(require("axios"));
|
|
19
|
-
|
|
20
|
-
var _lodash = _interopRequireDefault(require("lodash.clone"));
|
|
21
|
-
|
|
22
|
-
var _route = require("../libs/route");
|
|
23
|
-
|
|
24
|
-
var _render = require("../libs/render");
|
|
25
|
-
|
|
26
|
-
var _serveFile = require("../libs/serve-file");
|
|
27
|
-
|
|
28
|
-
var _utils2 = require("../utils");
|
|
29
|
-
|
|
30
|
-
var reader = _interopRequireWildcard(require("../libs/render/reader"));
|
|
31
|
-
|
|
32
|
-
var _proxy = require("../libs/proxy");
|
|
33
|
-
|
|
34
|
-
var _context = require("../libs/context");
|
|
35
|
-
|
|
36
|
-
var _constants = require("../constants");
|
|
37
|
-
|
|
38
|
-
var _template = require("../libs/hook-api/template");
|
|
39
|
-
|
|
40
|
-
var _route2 = require("../libs/hook-api/route");
|
|
41
|
-
|
|
42
|
-
const _excluded = ["getMiddlewares"];
|
|
43
|
-
|
|
44
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
45
|
-
|
|
46
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
47
|
-
|
|
48
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
49
|
-
|
|
50
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
51
|
-
|
|
52
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
53
|
-
|
|
54
|
-
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; }
|
|
55
|
-
|
|
56
|
-
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
57
|
-
|
|
58
|
-
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
59
|
-
|
|
60
|
-
const API_DIR = './api';
|
|
61
|
-
const SERVER_DIR = './server';
|
|
62
|
-
|
|
63
|
-
class ModernServer {
|
|
64
|
-
// appDirectory
|
|
65
|
-
// product dist dir
|
|
66
|
-
// work on src or dist
|
|
67
|
-
constructor({
|
|
68
|
-
pwd,
|
|
69
|
-
config,
|
|
70
|
-
dev,
|
|
71
|
-
routes,
|
|
72
|
-
staticGenerate,
|
|
73
|
-
logger,
|
|
74
|
-
metrics,
|
|
75
|
-
proxyTarget
|
|
76
|
-
}) {
|
|
77
|
-
var _config$output;
|
|
78
|
-
|
|
79
|
-
this.pwd = void 0;
|
|
80
|
-
this.distDir = void 0;
|
|
81
|
-
this.workDir = void 0;
|
|
82
|
-
this.router = void 0;
|
|
83
|
-
this.conf = void 0;
|
|
84
|
-
this.handlers = [];
|
|
85
|
-
this.presetRoutes = void 0;
|
|
86
|
-
this.runner = void 0;
|
|
87
|
-
this.logger = void 0;
|
|
88
|
-
this.metrics = void 0;
|
|
89
|
-
this.proxyTarget = void 0;
|
|
90
|
-
this.isDev = false;
|
|
91
|
-
this.staticFileHandler = void 0;
|
|
92
|
-
this.routeRenderHandler = void 0;
|
|
93
|
-
this.frameWebHandler = null;
|
|
94
|
-
this.frameAPIHandler = null;
|
|
95
|
-
this.proxyHandler = null;
|
|
96
|
-
this._handler = void 0;
|
|
97
|
-
this.staticGenerate = false;
|
|
98
|
-
|
|
99
|
-
require('ignore-styles');
|
|
100
|
-
|
|
101
|
-
this.isDev = Boolean(dev);
|
|
102
|
-
this.pwd = pwd;
|
|
103
|
-
this.distDir = _path.default.join(pwd, ((_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist');
|
|
104
|
-
this.workDir = this.isDev ? pwd : this.distDir;
|
|
105
|
-
this.conf = config;
|
|
106
|
-
this.logger = logger;
|
|
107
|
-
this.metrics = metrics;
|
|
108
|
-
this.router = new _route.RouteMatchManager();
|
|
109
|
-
this.presetRoutes = routes;
|
|
110
|
-
this.proxyTarget = proxyTarget;
|
|
111
|
-
|
|
112
|
-
if (staticGenerate) {
|
|
113
|
-
this.staticGenerate = staticGenerate;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
process.env.BUILD_TYPE = `${this.staticGenerate ? 'ssg' : 'ssr'}`;
|
|
117
|
-
} // exposed requestHandler
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
getRequestHandler() {
|
|
121
|
-
return this.requestHandler.bind(this);
|
|
122
|
-
} // server prepare
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
async init(runner) {
|
|
126
|
-
var _conf$bff;
|
|
127
|
-
|
|
128
|
-
this.runner = runner;
|
|
129
|
-
const {
|
|
130
|
-
distDir,
|
|
131
|
-
isDev,
|
|
132
|
-
staticGenerate,
|
|
133
|
-
conf
|
|
134
|
-
} = this;
|
|
135
|
-
this.addHandler((ctx, next) => {
|
|
136
|
-
ctx.res.setHeader('Access-Control-Allow-Origin', '*');
|
|
137
|
-
ctx.res.setHeader('Access-Control-Allow-Credentials', 'false');
|
|
138
|
-
next();
|
|
139
|
-
}); // proxy handler, each proxy has own handler
|
|
140
|
-
|
|
141
|
-
this.proxyHandler = (0, _proxy.createProxyHandler)((_conf$bff = conf.bff) === null || _conf$bff === void 0 ? void 0 : _conf$bff.proxy);
|
|
142
|
-
|
|
143
|
-
if (this.proxyHandler) {
|
|
144
|
-
this.proxyHandler.forEach(handler => {
|
|
145
|
-
this.addHandler(handler);
|
|
146
|
-
});
|
|
147
|
-
} // start reader, include an time interval
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
reader.init(); // use preset routes priority
|
|
151
|
-
|
|
152
|
-
this.router.reset(this.filterRoutes(this.presetRoutes || this.readRouteSpec()));
|
|
153
|
-
|
|
154
|
-
if (!isDev) {
|
|
155
|
-
// The route spec may not be produced at this phase in development
|
|
156
|
-
this.warmupSSRBundle();
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
await this.prepareFrameHandler(); // Only work when without setting `assetPrefix`.
|
|
160
|
-
// Setting `assetPrefix` means these resources should be uploaded to CDN.
|
|
161
|
-
|
|
162
|
-
const staticPathRegExp = (0, _utils2.getStaticReg)(this.conf.output || {});
|
|
163
|
-
this.staticFileHandler = (0, _serveFile.createStaticFileHandler)([{
|
|
164
|
-
path: staticPathRegExp,
|
|
165
|
-
target: distDir
|
|
166
|
-
}]);
|
|
167
|
-
this.routeRenderHandler = (0, _render.createRenderHandler)({
|
|
168
|
-
distDir,
|
|
169
|
-
staticGenerate
|
|
170
|
-
});
|
|
171
|
-
await this.preServerInit();
|
|
172
|
-
this.addHandler(this.staticFileHandler);
|
|
173
|
-
this.addHandler(this.routeHandler.bind(this));
|
|
174
|
-
this.compose();
|
|
175
|
-
} // server ready
|
|
176
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
ready(_) {} // invoke when http server listen
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
onListening(_) {// empty
|
|
183
|
-
} // close any thing run in server
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
close() {
|
|
187
|
-
reader.close();
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
async createHTTPServer(handler) {
|
|
191
|
-
return (0, _http.createServer)(handler);
|
|
192
|
-
} // read route spec from route.json
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
readRouteSpec() {
|
|
196
|
-
const file = _path.default.join(this.distDir, _utils.ROUTE_SPEC_FILE);
|
|
197
|
-
|
|
198
|
-
if (_utils.fs.existsSync(file)) {
|
|
199
|
-
const content = _utils.fs.readJSONSync(file);
|
|
200
|
-
|
|
201
|
-
return content.routes;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return [];
|
|
205
|
-
} // add promisify request handler to server
|
|
206
|
-
// handler should do not do more things after invoke next
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
addHandler(handler) {
|
|
210
|
-
if (handler[Symbol.toStringTag] === 'AsyncFunction') {
|
|
211
|
-
this.handlers.push(handler);
|
|
212
|
-
} else {
|
|
213
|
-
this.handlers.push(_util.default.promisify(handler));
|
|
214
|
-
}
|
|
215
|
-
} // return 404 page
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
render404(context) {
|
|
219
|
-
context.error(_constants.ERROR_DIGEST.ENOTF);
|
|
220
|
-
this.renderErrorPage(context, 404);
|
|
221
|
-
} // gather frame extension and get framework handler
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
async prepareFrameHandler() {
|
|
225
|
-
const {
|
|
226
|
-
workDir,
|
|
227
|
-
runner
|
|
228
|
-
} = this; // server hook, gather plugin inject
|
|
229
|
-
|
|
230
|
-
const _createMiddlewareColl = (0, _utils2.createMiddlewareCollecter)(),
|
|
231
|
-
{
|
|
232
|
-
getMiddlewares
|
|
233
|
-
} = _createMiddlewareColl,
|
|
234
|
-
collector = _objectWithoutProperties(_createMiddlewareColl, _excluded);
|
|
235
|
-
|
|
236
|
-
await runner.gather(collector);
|
|
237
|
-
const {
|
|
238
|
-
api: pluginAPIExt,
|
|
239
|
-
web: pluginWebExt
|
|
240
|
-
} = getMiddlewares();
|
|
241
|
-
|
|
242
|
-
const apiDir = _path.default.join(workDir, API_DIR);
|
|
243
|
-
|
|
244
|
-
const serverDir = _path.default.join(workDir, SERVER_DIR); // get api or web server handler from server-framework plugin
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
if (await _utils.fs.pathExists(_path.default.join(serverDir))) {
|
|
248
|
-
const webExtension = (0, _utils2.mergeExtension)(pluginWebExt);
|
|
249
|
-
this.frameWebHandler = await this.prepareWebHandler(webExtension);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
if (_utils.fs.existsSync(apiDir)) {
|
|
253
|
-
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
|
|
254
|
-
|
|
255
|
-
const apiExtension = (0, _utils2.mergeExtension)(pluginAPIExt);
|
|
256
|
-
this.frameAPIHandler = await this.prepareAPIHandler(mode, apiExtension);
|
|
257
|
-
}
|
|
258
|
-
} // Todo
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
async proxy() {
|
|
262
|
-
return null;
|
|
263
|
-
}
|
|
264
|
-
/* —————————————————————— function will be overwrite —————————————————————— */
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
async prepareWebHandler(extension) {
|
|
268
|
-
const {
|
|
269
|
-
workDir,
|
|
270
|
-
runner
|
|
271
|
-
} = this;
|
|
272
|
-
return runner.prepareWebServer({
|
|
273
|
-
pwd: workDir,
|
|
274
|
-
config: extension
|
|
275
|
-
}, {
|
|
276
|
-
onLast: () => null
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
async prepareAPIHandler(mode, extension) {
|
|
281
|
-
const {
|
|
282
|
-
workDir,
|
|
283
|
-
runner,
|
|
284
|
-
conf
|
|
285
|
-
} = this;
|
|
286
|
-
const {
|
|
287
|
-
bff
|
|
288
|
-
} = conf;
|
|
289
|
-
const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
|
|
290
|
-
return runner.prepareApiServer({
|
|
291
|
-
pwd: workDir,
|
|
292
|
-
mode,
|
|
293
|
-
config: extension,
|
|
294
|
-
prefix: Array.isArray(prefix) ? prefix[0] : prefix
|
|
295
|
-
}, {
|
|
296
|
-
onLast: () => null
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
filterRoutes(routes) {
|
|
301
|
-
return routes;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
async emitRouteHook(eventName, input) {
|
|
305
|
-
input.context = (0, _lodash.default)(input.context);
|
|
306
|
-
return this.runner[eventName](input, {
|
|
307
|
-
onLast: _utils2.noop
|
|
308
|
-
});
|
|
309
|
-
} // warmup ssr function
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
warmupSSRBundle() {
|
|
313
|
-
const {
|
|
314
|
-
distDir
|
|
315
|
-
} = this;
|
|
316
|
-
const bundles = this.router.getBundles();
|
|
317
|
-
bundles.forEach(bundle => {
|
|
318
|
-
const filepath = _path.default.join(distDir, bundle); // if error, just throw and let process die
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
require(filepath);
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
async preServerInit() {
|
|
326
|
-
const {
|
|
327
|
-
conf,
|
|
328
|
-
runner
|
|
329
|
-
} = this;
|
|
330
|
-
const preMiddleware = await runner.preServerInit(conf);
|
|
331
|
-
preMiddleware.flat().forEach(mid => {
|
|
332
|
-
this.addHandler(mid);
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
async handleAPI(context) {
|
|
337
|
-
const {
|
|
338
|
-
req,
|
|
339
|
-
res
|
|
340
|
-
} = context;
|
|
341
|
-
|
|
342
|
-
if (!this.frameAPIHandler) {
|
|
343
|
-
throw new Error('can not found api hanlder');
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
await this.frameAPIHandler(req, res);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
async handleWeb(context, route) {
|
|
350
|
-
return this.routeRenderHandler({
|
|
351
|
-
ctx: context,
|
|
352
|
-
route,
|
|
353
|
-
runner: this.runner
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
verifyMatch(_c, _m) {// empty
|
|
358
|
-
}
|
|
359
|
-
/* —————————————————————— private function —————————————————————— */
|
|
360
|
-
// handler route.json, include api / csr / ssr
|
|
361
|
-
// eslint-disable-next-line max-statements
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
async routeHandler(context) {
|
|
365
|
-
const {
|
|
366
|
-
req,
|
|
367
|
-
res
|
|
368
|
-
} = context;
|
|
369
|
-
await this.emitRouteHook('beforeMatch', {
|
|
370
|
-
context
|
|
371
|
-
}); // match routes in the route spec
|
|
372
|
-
|
|
373
|
-
const matched = this.router.match(context.path);
|
|
374
|
-
|
|
375
|
-
if (!matched) {
|
|
376
|
-
this.render404(context);
|
|
377
|
-
return;
|
|
378
|
-
} else {
|
|
379
|
-
this.verifyMatch(context, matched);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
if (res.headersSent) {
|
|
383
|
-
return;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
const routeAPI = (0, _route2.createRouteAPI)(matched, this.router, context.url);
|
|
387
|
-
await this.emitRouteHook('afterMatch', {
|
|
388
|
-
context,
|
|
389
|
-
routeAPI
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
if (res.headersSent) {
|
|
393
|
-
return;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
const {
|
|
397
|
-
current
|
|
398
|
-
} = routeAPI;
|
|
399
|
-
const route = current.generate(context.url);
|
|
400
|
-
context.setParams(route.params);
|
|
401
|
-
context.setServerData('router', {
|
|
402
|
-
baseUrl: route.urlPath,
|
|
403
|
-
params: route.params
|
|
404
|
-
}); // route is api service
|
|
405
|
-
|
|
406
|
-
if (route.isApi) {
|
|
407
|
-
await this.handleAPI(context);
|
|
408
|
-
return;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
if (this.frameWebHandler) {
|
|
412
|
-
await this.frameWebHandler(req, res);
|
|
413
|
-
} // frameWebHandler has process request
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
if (res.headersSent) {
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
if (route.entryName) {
|
|
421
|
-
await this.emitRouteHook('beforeRender', {
|
|
422
|
-
context
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
const file = await this.handleWeb(context, route);
|
|
427
|
-
|
|
428
|
-
if (!file) {
|
|
429
|
-
this.render404(context);
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
if (file.redirect) {
|
|
434
|
-
res.statusCode = file.statusCode;
|
|
435
|
-
res.setHeader('Location', file.content);
|
|
436
|
-
res.end();
|
|
437
|
-
return;
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
let response = file.content;
|
|
441
|
-
|
|
442
|
-
if (route.entryName) {
|
|
443
|
-
const templateAPI = (0, _template.createTemplateAPI)(file.content.toString());
|
|
444
|
-
await this.emitRouteHook('afterRender', {
|
|
445
|
-
context,
|
|
446
|
-
templateAPI
|
|
447
|
-
});
|
|
448
|
-
await this.injectMicroFE(context, templateAPI);
|
|
449
|
-
templateAPI.appendHead(`<script>window._SERVER_DATA=${JSON.stringify(context.serverData)}</script>`);
|
|
450
|
-
response = templateAPI.get();
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
res.setHeader('content-type', file.contentType);
|
|
454
|
-
res.end(response);
|
|
455
|
-
} // eslint-disable-next-line max-statements
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
async injectMicroFE(context, templateAPI) {
|
|
459
|
-
var _conf$runtime, _conf$server;
|
|
460
|
-
|
|
461
|
-
const {
|
|
462
|
-
conf
|
|
463
|
-
} = this;
|
|
464
|
-
const masterApp = (_conf$runtime = conf.runtime) === null || _conf$runtime === void 0 ? void 0 : _conf$runtime.masterApp; // no inject if not master App
|
|
465
|
-
|
|
466
|
-
if (!masterApp) {
|
|
467
|
-
return;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
const manifest = masterApp.manifest || {};
|
|
471
|
-
let modules = [];
|
|
472
|
-
const {
|
|
473
|
-
modules: configModules = []
|
|
474
|
-
} = manifest; // while config modules is an string, fetch data from remote
|
|
475
|
-
|
|
476
|
-
if (typeof configModules === 'string') {
|
|
477
|
-
const moduleRequestUrl = configModules;
|
|
478
|
-
|
|
479
|
-
try {
|
|
480
|
-
const {
|
|
481
|
-
data: remoteModules
|
|
482
|
-
} = await _axios.default.get(moduleRequestUrl);
|
|
483
|
-
|
|
484
|
-
if (Array.isArray(remoteModules)) {
|
|
485
|
-
modules.push(...remoteModules);
|
|
486
|
-
}
|
|
487
|
-
} catch (e) {
|
|
488
|
-
context.error(_constants.ERROR_DIGEST.EMICROINJ, e);
|
|
489
|
-
}
|
|
490
|
-
} else if (Array.isArray(configModules)) {
|
|
491
|
-
modules.push(...configModules);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
const {
|
|
495
|
-
headers
|
|
496
|
-
} = context.req;
|
|
497
|
-
const debugName = headers['x-micro-frontend-module-name'] || context.query['__debug__micro-frontend-module-name'];
|
|
498
|
-
const debugEntry = headers['x-micro-frontend-module-entry'] || context.query['__debug__micro-frontend-module-entry']; // add debug micro App to first
|
|
499
|
-
|
|
500
|
-
if (debugName && debugEntry && (_conf$server = conf.server) !== null && _conf$server !== void 0 && _conf$server.enableMicroFrontendDebug) {
|
|
501
|
-
modules = modules.map(m => {
|
|
502
|
-
if (m.name === debugName) {
|
|
503
|
-
return {
|
|
504
|
-
name: debugName,
|
|
505
|
-
entry: debugEntry
|
|
506
|
-
};
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
return m;
|
|
510
|
-
});
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
try {
|
|
514
|
-
// Todo Safety xss
|
|
515
|
-
const injection = JSON.stringify(_objectSpread(_objectSpread({}, manifest), {}, {
|
|
516
|
-
modules
|
|
517
|
-
}));
|
|
518
|
-
templateAPI.appendHead(`<script>window.modern_manifest=${injection}</script>`);
|
|
519
|
-
} catch (e) {
|
|
520
|
-
context.error(_constants.ERROR_DIGEST.EMICROINJ, e);
|
|
521
|
-
}
|
|
522
|
-
} // compose handlers and create the final handler
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
compose() {
|
|
526
|
-
const {
|
|
527
|
-
handlers
|
|
528
|
-
} = this;
|
|
529
|
-
|
|
530
|
-
if (!Array.isArray(handlers)) {
|
|
531
|
-
throw new TypeError('Middleware stack must be an array!');
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
for (const fn of handlers) {
|
|
535
|
-
if (typeof fn !== 'function') {
|
|
536
|
-
throw new TypeError('Middleware must be composed of functions!');
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
this._handler = (context, next) => {
|
|
541
|
-
let i = 0;
|
|
542
|
-
|
|
543
|
-
const dispatch = () => {
|
|
544
|
-
const handler = handlers[i++];
|
|
545
|
-
|
|
546
|
-
if (!handler) {
|
|
547
|
-
return next();
|
|
548
|
-
} // eslint-disable-next-line promise/prefer-await-to-then
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
return handler(context, dispatch).catch(onError);
|
|
552
|
-
};
|
|
553
|
-
|
|
554
|
-
const onError = err => {
|
|
555
|
-
this.onError(context, err);
|
|
556
|
-
};
|
|
557
|
-
|
|
558
|
-
return dispatch();
|
|
559
|
-
};
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
requestHandler(req, res, next = () => {// empty
|
|
563
|
-
}) {
|
|
564
|
-
res.statusCode = 200;
|
|
565
|
-
req.logger = req.logger || this.logger;
|
|
566
|
-
req.metrics = req.metrics || this.metrics;
|
|
567
|
-
const context = (0, _context.createContext)(req, res);
|
|
568
|
-
|
|
569
|
-
try {
|
|
570
|
-
this._handler(context, next);
|
|
571
|
-
} catch (err) {
|
|
572
|
-
this.onError(context, err);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
onError(context, err) {
|
|
577
|
-
context.error(_constants.ERROR_DIGEST.EINTER, err);
|
|
578
|
-
this.renderErrorPage(context, 500);
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
async renderErrorPage(context, status) {
|
|
582
|
-
const {
|
|
583
|
-
res
|
|
584
|
-
} = context;
|
|
585
|
-
context.status = status;
|
|
586
|
-
res.setHeader('content-type', _mimeTypes.default.contentType('html'));
|
|
587
|
-
const statusPage = `/${status}`;
|
|
588
|
-
const customErrorPage = `/_error`;
|
|
589
|
-
const matched = this.router.match(statusPage) || this.router.match(customErrorPage); // if no custom status page find
|
|
590
|
-
|
|
591
|
-
if (matched) {
|
|
592
|
-
const route = matched.generate(context.url);
|
|
593
|
-
const {
|
|
594
|
-
entryName
|
|
595
|
-
} = route; // check entryName, aviod matched '/' route
|
|
596
|
-
|
|
597
|
-
if (entryName === status.toString() || entryName === '_error') {
|
|
598
|
-
try {
|
|
599
|
-
const file = await this.routeRenderHandler({
|
|
600
|
-
route,
|
|
601
|
-
ctx: context,
|
|
602
|
-
runner: this.runner
|
|
603
|
-
});
|
|
604
|
-
|
|
605
|
-
if (file) {
|
|
606
|
-
context.res.end(file.content);
|
|
607
|
-
return;
|
|
608
|
-
}
|
|
609
|
-
} catch (e) {// just catch error when the rendering error occurred in the custom error page.
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
const text = _constants.ERROR_PAGE_TEXT[status] || _constants.ERROR_PAGE_TEXT[500];
|
|
615
|
-
res.end((0, _utils2.createErrorDocument)(status, text));
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
}
|
|
619
|
-
/* eslint-enable max-lines */
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
exports.ModernServer = ModernServer;
|