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