@modern-js/server-core 2.65.5 → 2.66.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/dist/cjs/index.js +2 -5
- package/dist/cjs/plugins/compat/hooks.js +92 -0
- package/dist/cjs/plugins/compat/index.js +65 -0
- package/dist/cjs/plugins/customServer/index.js +11 -17
- package/dist/cjs/plugins/default.js +2 -0
- package/dist/cjs/plugins/index.js +5 -0
- package/dist/cjs/plugins/render/index.js +2 -2
- package/dist/cjs/serverBase.js +31 -40
- package/dist/cjs/types/index.js +2 -2
- package/dist/cjs/types/{plugin.js → plugins/base.js} +2 -2
- package/dist/cjs/types/plugins/index.js +24 -0
- package/dist/cjs/types/plugins/new.js +16 -0
- package/dist/cjs/types/plugins/old.js +16 -0
- package/dist/esm/index.js +1 -3
- package/dist/esm/plugins/compat/hooks.js +70 -0
- package/dist/esm/plugins/compat/index.js +45 -0
- package/dist/esm/plugins/customServer/index.js +30 -22
- package/dist/esm/plugins/default.js +2 -0
- package/dist/esm/plugins/index.js +3 -0
- package/dist/esm/plugins/render/index.js +3 -3
- package/dist/esm/serverBase.js +34 -38
- package/dist/esm/types/index.js +1 -1
- package/dist/esm/types/plugins/base.js +0 -0
- package/dist/esm/types/plugins/index.js +2 -0
- package/dist/esm/types/plugins/new.js +0 -0
- package/dist/esm/types/plugins/old.js +0 -0
- package/dist/esm-node/index.js +1 -3
- package/dist/esm-node/plugins/compat/hooks.js +66 -0
- package/dist/esm-node/plugins/compat/index.js +40 -0
- package/dist/esm-node/plugins/customServer/index.js +10 -16
- package/dist/esm-node/plugins/default.js +2 -0
- package/dist/esm-node/plugins/index.js +3 -0
- package/dist/esm-node/plugins/render/index.js +2 -2
- package/dist/esm-node/serverBase.js +31 -40
- package/dist/esm-node/types/index.js +1 -1
- package/dist/esm-node/types/plugins/index.js +2 -0
- package/dist/esm-node/types/plugins/old.js +0 -0
- package/dist/types/adapters/node/node.d.ts +0 -2
- package/dist/types/adapters/node/plugins/nodeServer.d.ts +3 -2
- package/dist/types/adapters/node/plugins/resource.d.ts +3 -3
- package/dist/types/adapters/node/plugins/static.d.ts +2 -2
- package/dist/types/adapters/node/polyfills/stream.d.ts +0 -2
- package/dist/types/index.d.ts +1 -2
- package/dist/types/plugins/compat/hooks.d.ts +8 -0
- package/dist/types/plugins/compat/index.d.ts +3 -0
- package/dist/types/plugins/customServer/index.d.ts +3 -3
- package/dist/types/plugins/default.d.ts +2 -2
- package/dist/types/plugins/favicon.d.ts +2 -2
- package/dist/types/plugins/index.d.ts +1 -0
- package/dist/types/plugins/log.d.ts +2 -2
- package/dist/types/plugins/monitors.d.ts +4 -4
- package/dist/types/plugins/processedBy.d.ts +2 -2
- package/dist/types/plugins/render/index.d.ts +2 -2
- package/dist/types/plugins/render/inject.d.ts +2 -2
- package/dist/types/plugins/render/ssrCache.d.ts +0 -1
- package/dist/types/plugins/render/ssrRender.d.ts +0 -1
- package/dist/types/plugins/route.d.ts +2 -2
- package/dist/types/serverBase.d.ts +13 -24
- package/dist/types/types/index.d.ts +1 -1
- package/dist/types/types/plugins/base.d.ts +79 -0
- package/dist/types/types/plugins/index.d.ts +3 -0
- package/dist/types/types/plugins/new.d.ts +34 -0
- package/dist/types/types/plugins/old.d.ts +56 -0
- package/dist/types/types/render.d.ts +0 -1
- package/package.json +8 -7
- package/dist/cjs/pluginManager.js +0 -106
- package/dist/esm/pluginManager.js +0 -140
- package/dist/esm-node/pluginManager.js +0 -82
- package/dist/types/pluginManager.d.ts +0 -31
- package/dist/types/types/plugin.d.ts +0 -150
- /package/dist/esm-node/types/{plugin.js → plugins/base.js} +0 -0
- /package/dist/{esm/types/plugin.js → esm-node/types/plugins/new.js} +0 -0
|
@@ -3,6 +3,7 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
|
3
3
|
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
4
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
5
|
import { time } from "@modern-js/runtime-utils/time";
|
|
6
|
+
import { isArray, isFunction } from "@modern-js/utils";
|
|
6
7
|
import { ServerTimings } from "../../constants";
|
|
7
8
|
import { getLoaderCtx } from "../../helper";
|
|
8
9
|
import { transformResponse } from "../../utils";
|
|
@@ -16,20 +17,16 @@ var isHtmlResponse = function(response) {
|
|
|
16
17
|
};
|
|
17
18
|
var CustomServer = /* @__PURE__ */ function() {
|
|
18
19
|
"use strict";
|
|
19
|
-
function CustomServer2(
|
|
20
|
+
function CustomServer2(hooks, serverBase, pwd) {
|
|
20
21
|
_class_call_check(this, CustomServer2);
|
|
21
|
-
this.
|
|
22
|
+
this.hooks = hooks;
|
|
22
23
|
this.serverBase = serverBase;
|
|
23
24
|
var webExtension = [];
|
|
24
|
-
this.serverMiddlewarePromise =
|
|
25
|
+
this.serverMiddlewarePromise = hooks.prepareWebServer.call({
|
|
25
26
|
pwd,
|
|
26
27
|
config: {
|
|
27
28
|
middleware: webExtension
|
|
28
29
|
}
|
|
29
|
-
}, {
|
|
30
|
-
onLast: function() {
|
|
31
|
-
return [];
|
|
32
|
-
}
|
|
33
30
|
});
|
|
34
31
|
}
|
|
35
32
|
var _proto = CustomServer2.prototype;
|
|
@@ -50,9 +47,7 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
50
47
|
getCost = time();
|
|
51
48
|
return [
|
|
52
49
|
4,
|
|
53
|
-
_this.
|
|
54
|
-
onLast: noop
|
|
55
|
-
})
|
|
50
|
+
_this.hooks.afterMatch.call(afterMatchCtx)
|
|
56
51
|
];
|
|
57
52
|
case 1:
|
|
58
53
|
_state.sent();
|
|
@@ -105,15 +100,30 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
105
100
|
3
|
|
106
101
|
];
|
|
107
102
|
afterStreamingRenderContext = createAfterStreamingRenderContext(baseHookCtx, routeInfo);
|
|
108
|
-
c.res = transformResponse(c.res, function(
|
|
109
|
-
var
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
103
|
+
c.res = transformResponse(c.res, function() {
|
|
104
|
+
var _ref2 = _async_to_generator(function(chunk) {
|
|
105
|
+
var context, _ref3, newChunk;
|
|
106
|
+
return _ts_generator(this, function(_state2) {
|
|
107
|
+
switch (_state2.label) {
|
|
108
|
+
case 0:
|
|
109
|
+
context = afterStreamingRenderContext(chunk);
|
|
110
|
+
return [
|
|
111
|
+
4,
|
|
112
|
+
_this.hooks.afterStreamingRender.call(context)
|
|
113
|
+
];
|
|
114
|
+
case 1:
|
|
115
|
+
_ref3 = _state2.sent(), newChunk = _ref3.chunk;
|
|
116
|
+
return [
|
|
117
|
+
2,
|
|
118
|
+
newChunk
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
});
|
|
115
122
|
});
|
|
116
|
-
|
|
123
|
+
return function(chunk) {
|
|
124
|
+
return _ref2.apply(this, arguments);
|
|
125
|
+
};
|
|
126
|
+
}());
|
|
117
127
|
return [
|
|
118
128
|
3,
|
|
119
129
|
6
|
|
@@ -128,9 +138,7 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
128
138
|
getCost1 = time();
|
|
129
139
|
return [
|
|
130
140
|
4,
|
|
131
|
-
_this.
|
|
132
|
-
onLast: noop
|
|
133
|
-
})
|
|
141
|
+
_this.hooks.afterRender.call(afterRenderCtx)
|
|
134
142
|
];
|
|
135
143
|
case 5:
|
|
136
144
|
_state.sent();
|
|
@@ -170,7 +178,7 @@ var CustomServer = /* @__PURE__ */ function() {
|
|
|
170
178
|
];
|
|
171
179
|
case 1:
|
|
172
180
|
serverMiddleware = _state.sent();
|
|
173
|
-
if (!serverMiddleware) {
|
|
181
|
+
if (!serverMiddleware || !isFunction(serverMiddleware) && !isArray(serverMiddleware)) {
|
|
174
182
|
return [
|
|
175
183
|
2,
|
|
176
184
|
renderMiddlewares
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { compatPlugin } from "./compat";
|
|
1
2
|
import { logPlugin } from "./log";
|
|
2
3
|
import { initMonitorsPlugin, injectServerTiming, injectloggerPlugin } from "./monitors";
|
|
3
4
|
import { processedByPlugin } from "./processedBy";
|
|
@@ -14,6 +15,7 @@ function createSilenceLogger() {
|
|
|
14
15
|
function createDefaultPlugins() {
|
|
15
16
|
var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
16
17
|
var plugins = [
|
|
18
|
+
compatPlugin(),
|
|
17
19
|
logPlugin(),
|
|
18
20
|
initMonitorsPlugin(),
|
|
19
21
|
injectRenderHandlerPlugin(options),
|
|
@@ -4,10 +4,13 @@ import { injectServerTiming, injectloggerPlugin } from "./monitors";
|
|
|
4
4
|
import { processedByPlugin } from "./processedBy";
|
|
5
5
|
import { logPlugin } from "./log";
|
|
6
6
|
import { createDefaultPlugins } from "./default";
|
|
7
|
+
import { compatPlugin, handleSetupResult } from "./compat";
|
|
7
8
|
export {
|
|
9
|
+
compatPlugin,
|
|
8
10
|
createDefaultPlugins,
|
|
9
11
|
faviconPlugin,
|
|
10
12
|
getRenderHandler,
|
|
13
|
+
handleSetupResult,
|
|
11
14
|
injectRenderHandlerPlugin,
|
|
12
15
|
injectServerTiming,
|
|
13
16
|
injectloggerPlugin,
|
|
@@ -13,19 +13,19 @@ var renderPlugin = function() {
|
|
|
13
13
|
return {
|
|
14
14
|
prepare: function prepare() {
|
|
15
15
|
return _async_to_generator(function() {
|
|
16
|
-
var _config_render, _api_useAppContext, middlewares, routes, render, pwd, serverBase,
|
|
16
|
+
var _config_render, _api_useAppContext, middlewares, routes, render, pwd, serverBase, hooks, config, customServer, serverMiddleware, pageRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, originUrlPath, _route_entryName, entryName, urlPath, customServerHookMiddleware, customServerMiddleware, err;
|
|
17
17
|
return _ts_generator(this, function(_state) {
|
|
18
18
|
switch (_state.label) {
|
|
19
19
|
case 0:
|
|
20
20
|
_api_useAppContext = api.useAppContext(), middlewares = _api_useAppContext.middlewares, routes = _api_useAppContext.routes, render = _api_useAppContext.render, pwd = _api_useAppContext.distDirectory, serverBase = _api_useAppContext.serverBase;
|
|
21
|
-
|
|
21
|
+
hooks = api.getHooks();
|
|
22
22
|
config = api.useConfigContext();
|
|
23
23
|
if (!routes) {
|
|
24
24
|
return [
|
|
25
25
|
2
|
|
26
26
|
];
|
|
27
27
|
}
|
|
28
|
-
customServer = new CustomServer(
|
|
28
|
+
customServer = new CustomServer(hooks, serverBase, pwd);
|
|
29
29
|
serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && getServerMidFromUnstableMid(config.render.middleware);
|
|
30
30
|
pageRoutes = getPageRoutes(routes);
|
|
31
31
|
middlewares.push({
|
package/dist/esm/serverBase.js
CHANGED
|
@@ -5,48 +5,54 @@ import { _ as _class_private_method_init } from "@swc/helpers/_/_class_private_m
|
|
|
5
5
|
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
6
6
|
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
7
7
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
8
|
-
import {
|
|
8
|
+
import { server } from "@modern-js/plugin-v2/server";
|
|
9
9
|
import { Hono } from "hono";
|
|
10
|
-
import {
|
|
11
|
-
|
|
10
|
+
import { handleSetupResult } from "./plugins/compat/hooks";
|
|
11
|
+
import { loadConfig } from "./utils";
|
|
12
|
+
var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
|
|
12
13
|
var ServerBase = /* @__PURE__ */ function() {
|
|
13
14
|
"use strict";
|
|
14
15
|
function ServerBase2(options) {
|
|
15
16
|
_class_call_check(this, ServerBase2);
|
|
16
|
-
_class_private_method_init(this, _getAppContext);
|
|
17
17
|
_class_private_method_init(this, _applyMiddlewares);
|
|
18
|
+
this.plugins = [];
|
|
19
|
+
this.serverContext = null;
|
|
18
20
|
this.options = options;
|
|
19
|
-
var config = options.config, serverConfig = options.serverConfig;
|
|
20
|
-
var appContext = _class_private_method_get(this, _getAppContext, getAppContext).call(this);
|
|
21
|
-
this.appContext = appContext;
|
|
22
|
-
this.pluginManager = new PluginManager({
|
|
23
|
-
cliConfig: config,
|
|
24
|
-
appContext,
|
|
25
|
-
serverConfig
|
|
26
|
-
});
|
|
27
21
|
this.app = new Hono();
|
|
28
22
|
}
|
|
29
23
|
var _proto = ServerBase2.prototype;
|
|
30
24
|
_proto.init = function init() {
|
|
31
25
|
var _this = this;
|
|
32
26
|
return _async_to_generator(function() {
|
|
33
|
-
var
|
|
27
|
+
var _this_options, serverConfig, cliConfig, mergedConfig, serverContext;
|
|
34
28
|
return _ts_generator(this, function(_state) {
|
|
35
29
|
switch (_state.label) {
|
|
36
30
|
case 0:
|
|
31
|
+
_this_options = _this.options, serverConfig = _this_options.serverConfig, cliConfig = _this_options.config;
|
|
32
|
+
mergedConfig = loadConfig({
|
|
33
|
+
cliConfig,
|
|
34
|
+
serverConfig: serverConfig || {}
|
|
35
|
+
});
|
|
36
|
+
_this.addPlugins(mergedConfig.plugins || []);
|
|
37
37
|
return [
|
|
38
38
|
4,
|
|
39
|
-
|
|
39
|
+
server.run({
|
|
40
|
+
plugins: _this.plugins,
|
|
41
|
+
options: _this.options,
|
|
42
|
+
config: mergedConfig,
|
|
43
|
+
handleSetupResult
|
|
44
|
+
})
|
|
40
45
|
];
|
|
41
46
|
case 1:
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
serverContext = _state.sent().serverContext;
|
|
48
|
+
serverContext.serverBase = _this;
|
|
44
49
|
return [
|
|
45
50
|
4,
|
|
46
|
-
|
|
51
|
+
serverContext.hooks.onPrepare.call()
|
|
47
52
|
];
|
|
48
53
|
case 2:
|
|
49
54
|
_state.sent();
|
|
55
|
+
_this.serverContext = serverContext;
|
|
50
56
|
_class_private_method_get(_this, _applyMiddlewares, applyMiddlewares).call(_this);
|
|
51
57
|
return [
|
|
52
58
|
2,
|
|
@@ -57,9 +63,16 @@ var ServerBase = /* @__PURE__ */ function() {
|
|
|
57
63
|
})();
|
|
58
64
|
};
|
|
59
65
|
_proto.addPlugins = function addPlugins(plugins) {
|
|
60
|
-
|
|
66
|
+
var _this_plugins;
|
|
67
|
+
(_this_plugins = this.plugins).push.apply(_this_plugins, _to_consumable_array(plugins));
|
|
61
68
|
};
|
|
62
69
|
_create_class(ServerBase2, [
|
|
70
|
+
{
|
|
71
|
+
key: "hooks",
|
|
72
|
+
get: function get() {
|
|
73
|
+
return this.serverContext.hooks;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
63
76
|
{
|
|
64
77
|
key: "all",
|
|
65
78
|
get: function get() {
|
|
@@ -129,25 +142,8 @@ var ServerBase = /* @__PURE__ */ function() {
|
|
|
129
142
|
]);
|
|
130
143
|
return ServerBase2;
|
|
131
144
|
}();
|
|
132
|
-
function getAppContext() {
|
|
133
|
-
var _this_options = this.options, context = _this_options.appContext, pwd = _this_options.pwd, routes = _this_options.routes, metaName = _this_options.metaName;
|
|
134
|
-
var appContext = {
|
|
135
|
-
routes,
|
|
136
|
-
middlewares: [],
|
|
137
|
-
appDirectory: (context === null || context === void 0 ? void 0 : context.appDirectory) || "",
|
|
138
|
-
apiDirectory: context === null || context === void 0 ? void 0 : context.apiDirectory,
|
|
139
|
-
internalDirectory: (context === null || context === void 0 ? void 0 : context.internalDirectory) || "",
|
|
140
|
-
lambdaDirectory: context === null || context === void 0 ? void 0 : context.lambdaDirectory,
|
|
141
|
-
sharedDirectory: (context === null || context === void 0 ? void 0 : context.sharedDirectory) || "",
|
|
142
|
-
distDirectory: pwd,
|
|
143
|
-
plugins: [],
|
|
144
|
-
metaName: metaName || "modern-js",
|
|
145
|
-
serverBase: this
|
|
146
|
-
};
|
|
147
|
-
return createContext(appContext);
|
|
148
|
-
}
|
|
149
145
|
function applyMiddlewares() {
|
|
150
|
-
var middlewares = this.
|
|
146
|
+
var middlewares = this.serverContext.middlewares;
|
|
151
147
|
var preMiddlewares = [];
|
|
152
148
|
var defaultMiddlewares = [];
|
|
153
149
|
var postMiddlewares = [];
|
|
@@ -242,8 +238,8 @@ function createServerBase(options) {
|
|
|
242
238
|
if (options == null) {
|
|
243
239
|
throw new Error("can not start server without options");
|
|
244
240
|
}
|
|
245
|
-
var
|
|
246
|
-
return
|
|
241
|
+
var server2 = new ServerBase(options);
|
|
242
|
+
return server2;
|
|
247
243
|
}
|
|
248
244
|
export {
|
|
249
245
|
ServerBase,
|
package/dist/esm/types/index.js
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/dist/esm-node/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { createErrorHtml, onError, ErrorDigest } from "./utils";
|
|
2
2
|
import { AGGRED_DIR } from "./constants";
|
|
3
3
|
import { createServerBase } from "./serverBase";
|
|
4
|
-
import { PluginManager } from "./pluginManager";
|
|
5
4
|
import { getLoaderCtx } from "./helper";
|
|
6
5
|
export * from "./plugins";
|
|
7
|
-
export * from "./types/
|
|
6
|
+
export * from "./types/plugins";
|
|
8
7
|
export * from "./types/render";
|
|
9
8
|
export * from "@modern-js/plugin";
|
|
10
9
|
export * from "./types/config";
|
|
@@ -12,7 +11,6 @@ export * from "./types/requestHandler";
|
|
|
12
11
|
export {
|
|
13
12
|
AGGRED_DIR,
|
|
14
13
|
ErrorDigest,
|
|
15
|
-
PluginManager,
|
|
16
14
|
createErrorHtml,
|
|
17
15
|
createServerBase,
|
|
18
16
|
getLoaderCtx,
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
function getHookRunners(context) {
|
|
2
|
+
const { hooks } = context;
|
|
3
|
+
return {
|
|
4
|
+
config: (params) => {
|
|
5
|
+
return hooks.modifyConfig.call(params);
|
|
6
|
+
},
|
|
7
|
+
prepare: () => {
|
|
8
|
+
return hooks.onPrepare.call();
|
|
9
|
+
},
|
|
10
|
+
reset: (params) => {
|
|
11
|
+
return hooks.onReset.call(params);
|
|
12
|
+
},
|
|
13
|
+
fallback: (input) => {
|
|
14
|
+
return hooks.fallback.call(input);
|
|
15
|
+
},
|
|
16
|
+
prepareWebServer: (input) => {
|
|
17
|
+
return hooks.prepareWebServer.call(input);
|
|
18
|
+
},
|
|
19
|
+
prepareApiServer: (input) => {
|
|
20
|
+
return hooks.prepareApiServer.call(input);
|
|
21
|
+
},
|
|
22
|
+
afterMatch: (ctx) => {
|
|
23
|
+
return hooks.afterMatch.call(ctx);
|
|
24
|
+
},
|
|
25
|
+
afterRender: (ctx) => {
|
|
26
|
+
return hooks.afterRender.call(ctx);
|
|
27
|
+
},
|
|
28
|
+
afterStreamingRender: (ctx) => {
|
|
29
|
+
return hooks.afterStreamingRender.call(ctx);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function transformHookRunner(hookRunnerName) {
|
|
34
|
+
switch (hookRunnerName) {
|
|
35
|
+
case "config":
|
|
36
|
+
return "modifyConfig";
|
|
37
|
+
case "prepare":
|
|
38
|
+
return "onPrepare";
|
|
39
|
+
case "reset":
|
|
40
|
+
return "onReset";
|
|
41
|
+
default:
|
|
42
|
+
return hookRunnerName;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function handleSetupResult(setupResult, api) {
|
|
46
|
+
if (!setupResult) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
Object.keys(setupResult).forEach((key) => {
|
|
50
|
+
const fn = setupResult[key];
|
|
51
|
+
if (typeof fn === "function") {
|
|
52
|
+
const newAPI = transformHookRunner(key);
|
|
53
|
+
if (api[newAPI]) {
|
|
54
|
+
api[newAPI]((...params) => {
|
|
55
|
+
const res = fn(...params);
|
|
56
|
+
return res;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export {
|
|
63
|
+
getHookRunners,
|
|
64
|
+
handleSetupResult,
|
|
65
|
+
transformHookRunner
|
|
66
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createAsyncHook, createAsyncPipelineHook } from "@modern-js/plugin-v2";
|
|
2
|
+
import { getHookRunners } from "./hooks";
|
|
3
|
+
import { handleSetupResult } from "./hooks";
|
|
4
|
+
const compatPlugin = () => ({
|
|
5
|
+
name: "@modern-js/server-compat",
|
|
6
|
+
registryHooks: {
|
|
7
|
+
fallback: createAsyncHook(),
|
|
8
|
+
prepareWebServer: createAsyncPipelineHook(),
|
|
9
|
+
prepareApiServer: createAsyncPipelineHook(),
|
|
10
|
+
afterMatch: createAsyncPipelineHook(),
|
|
11
|
+
afterRender: createAsyncPipelineHook(),
|
|
12
|
+
afterStreamingRender: createAsyncPipelineHook()
|
|
13
|
+
},
|
|
14
|
+
_registryApi: (getServerContext, updateServerContext) => {
|
|
15
|
+
const getInternalContext = () => {
|
|
16
|
+
return getServerContext()._internalContext;
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
useConfigContext: () => {
|
|
20
|
+
return getInternalContext().config;
|
|
21
|
+
},
|
|
22
|
+
useAppContext: () => {
|
|
23
|
+
const { _internalContext, ...serverContext } = getServerContext();
|
|
24
|
+
return serverContext;
|
|
25
|
+
},
|
|
26
|
+
setAppContext: (context) => {
|
|
27
|
+
return updateServerContext(context);
|
|
28
|
+
},
|
|
29
|
+
useHookRunners: () => {
|
|
30
|
+
return getHookRunners(getInternalContext());
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
setup: () => {
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
export {
|
|
38
|
+
compatPlugin,
|
|
39
|
+
handleSetupResult
|
|
40
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { time } from "@modern-js/runtime-utils/time";
|
|
2
|
+
import { isArray, isFunction } from "@modern-js/utils";
|
|
2
3
|
import { ServerTimings } from "../../constants";
|
|
3
4
|
import { getLoaderCtx } from "../../helper";
|
|
4
5
|
import { transformResponse } from "../../utils";
|
|
@@ -18,9 +19,7 @@ class CustomServer {
|
|
|
18
19
|
const baseHookCtx = createBaseHookContext(c);
|
|
19
20
|
const afterMatchCtx = getAfterMatchCtx(entryName, baseHookCtx);
|
|
20
21
|
const getCost = time();
|
|
21
|
-
await this.
|
|
22
|
-
onLast: noop
|
|
23
|
-
});
|
|
22
|
+
await this.hooks.afterMatch.call(afterMatchCtx);
|
|
24
23
|
const cost = getCost();
|
|
25
24
|
cost && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_HOOK_AFTER_MATCH, cost));
|
|
26
25
|
const { url, status } = afterMatchCtx.router;
|
|
@@ -50,18 +49,15 @@ class CustomServer {
|
|
|
50
49
|
}
|
|
51
50
|
if (routeInfo.isStream) {
|
|
52
51
|
const afterStreamingRenderContext = createAfterStreamingRenderContext(baseHookCtx, routeInfo);
|
|
53
|
-
c.res = transformResponse(c.res, (chunk) => {
|
|
52
|
+
c.res = transformResponse(c.res, async (chunk) => {
|
|
54
53
|
const context = afterStreamingRenderContext(chunk);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
54
|
+
const { chunk: newChunk } = await this.hooks.afterStreamingRender.call(context);
|
|
55
|
+
return newChunk;
|
|
58
56
|
});
|
|
59
57
|
} else {
|
|
60
58
|
const afterRenderCtx = await getAfterRenderCtx(c, baseHookCtx, routeInfo);
|
|
61
59
|
const getCost2 = time();
|
|
62
|
-
await this.
|
|
63
|
-
onLast: noop
|
|
64
|
-
});
|
|
60
|
+
await this.hooks.afterRender.call(afterRenderCtx);
|
|
65
61
|
const cost2 = getCost2();
|
|
66
62
|
cost2 && (monitors === null || monitors === void 0 ? void 0 : monitors.timing(ServerTimings.SERVER_HOOK_AFTER_RENDER, cost2));
|
|
67
63
|
if (afterRenderCtx.response.private_overrided) {
|
|
@@ -74,7 +70,7 @@ class CustomServer {
|
|
|
74
70
|
}
|
|
75
71
|
async getServerMiddleware(renderMiddlewares) {
|
|
76
72
|
const serverMiddleware = await this.serverMiddlewarePromise;
|
|
77
|
-
if (!serverMiddleware) {
|
|
73
|
+
if (!serverMiddleware || !isFunction(serverMiddleware) && !isArray(serverMiddleware)) {
|
|
78
74
|
return renderMiddlewares;
|
|
79
75
|
}
|
|
80
76
|
if (Array.isArray(serverMiddleware)) {
|
|
@@ -110,17 +106,15 @@ class CustomServer {
|
|
|
110
106
|
}
|
|
111
107
|
};
|
|
112
108
|
}
|
|
113
|
-
constructor(
|
|
114
|
-
this.
|
|
109
|
+
constructor(hooks, serverBase, pwd) {
|
|
110
|
+
this.hooks = hooks;
|
|
115
111
|
this.serverBase = serverBase;
|
|
116
112
|
const webExtension = [];
|
|
117
|
-
this.serverMiddlewarePromise =
|
|
113
|
+
this.serverMiddlewarePromise = hooks.prepareWebServer.call({
|
|
118
114
|
pwd,
|
|
119
115
|
config: {
|
|
120
116
|
middleware: webExtension
|
|
121
117
|
}
|
|
122
|
-
}, {
|
|
123
|
-
onLast: () => []
|
|
124
118
|
});
|
|
125
119
|
}
|
|
126
120
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { compatPlugin } from "./compat";
|
|
1
2
|
import { logPlugin } from "./log";
|
|
2
3
|
import { initMonitorsPlugin, injectServerTiming, injectloggerPlugin } from "./monitors";
|
|
3
4
|
import { processedByPlugin } from "./processedBy";
|
|
@@ -13,6 +14,7 @@ function createSilenceLogger() {
|
|
|
13
14
|
}
|
|
14
15
|
function createDefaultPlugins(options = {}) {
|
|
15
16
|
const plugins = [
|
|
17
|
+
compatPlugin(),
|
|
16
18
|
logPlugin(),
|
|
17
19
|
initMonitorsPlugin(),
|
|
18
20
|
injectRenderHandlerPlugin(options),
|
|
@@ -4,10 +4,13 @@ import { injectServerTiming, injectloggerPlugin } from "./monitors";
|
|
|
4
4
|
import { processedByPlugin } from "./processedBy";
|
|
5
5
|
import { logPlugin } from "./log";
|
|
6
6
|
import { createDefaultPlugins } from "./default";
|
|
7
|
+
import { compatPlugin, handleSetupResult } from "./compat";
|
|
7
8
|
export {
|
|
9
|
+
compatPlugin,
|
|
8
10
|
createDefaultPlugins,
|
|
9
11
|
faviconPlugin,
|
|
10
12
|
getRenderHandler,
|
|
13
|
+
handleSetupResult,
|
|
11
14
|
injectRenderHandlerPlugin,
|
|
12
15
|
injectServerTiming,
|
|
13
16
|
injectloggerPlugin,
|
|
@@ -11,12 +11,12 @@ const renderPlugin = () => ({
|
|
|
11
11
|
async prepare() {
|
|
12
12
|
var _config_render;
|
|
13
13
|
const { middlewares, routes, render, distDirectory: pwd, serverBase } = api.useAppContext();
|
|
14
|
-
const
|
|
14
|
+
const hooks = api.getHooks();
|
|
15
15
|
const config = api.useConfigContext();
|
|
16
16
|
if (!routes) {
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
|
-
const customServer = new CustomServer(
|
|
19
|
+
const customServer = new CustomServer(hooks, serverBase, pwd);
|
|
20
20
|
const serverMiddleware = ((_config_render = config.render) === null || _config_render === void 0 ? void 0 : _config_render.middleware) && getServerMidFromUnstableMid(config.render.middleware);
|
|
21
21
|
const pageRoutes = getPageRoutes(routes);
|
|
22
22
|
middlewares.push({
|
|
@@ -1,25 +1,40 @@
|
|
|
1
1
|
import { _ as _class_private_method_get } from "@swc/helpers/_/_class_private_method_get";
|
|
2
2
|
import { _ as _class_private_method_init } from "@swc/helpers/_/_class_private_method_init";
|
|
3
|
-
import {
|
|
3
|
+
import { server } from "@modern-js/plugin-v2/server";
|
|
4
4
|
import { Hono } from "hono";
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import { handleSetupResult } from "./plugins/compat/hooks";
|
|
6
|
+
import { loadConfig } from "./utils";
|
|
7
|
+
var _applyMiddlewares = /* @__PURE__ */ new WeakSet();
|
|
7
8
|
class ServerBase {
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
-
* -
|
|
11
|
-
* -
|
|
12
|
-
* - 应用 middlewares
|
|
10
|
+
* Order
|
|
11
|
+
* - server runner
|
|
12
|
+
* - apply middlewares
|
|
13
13
|
*/
|
|
14
14
|
async init() {
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const { serverConfig, config: cliConfig } = this.options;
|
|
16
|
+
const mergedConfig = loadConfig({
|
|
17
|
+
cliConfig,
|
|
18
|
+
serverConfig: serverConfig || {}
|
|
19
|
+
});
|
|
20
|
+
this.addPlugins(mergedConfig.plugins || []);
|
|
21
|
+
const { serverContext } = await server.run({
|
|
22
|
+
plugins: this.plugins,
|
|
23
|
+
options: this.options,
|
|
24
|
+
config: mergedConfig,
|
|
25
|
+
handleSetupResult
|
|
26
|
+
});
|
|
27
|
+
serverContext.serverBase = this;
|
|
28
|
+
await serverContext.hooks.onPrepare.call();
|
|
29
|
+
this.serverContext = serverContext;
|
|
18
30
|
_class_private_method_get(this, _applyMiddlewares, applyMiddlewares).call(this);
|
|
19
31
|
return this;
|
|
20
32
|
}
|
|
21
33
|
addPlugins(plugins) {
|
|
22
|
-
this.
|
|
34
|
+
this.plugins.push(...plugins);
|
|
35
|
+
}
|
|
36
|
+
get hooks() {
|
|
37
|
+
return this.serverContext.hooks;
|
|
23
38
|
}
|
|
24
39
|
get all() {
|
|
25
40
|
return this.app.all.bind(this.app);
|
|
@@ -55,39 +70,15 @@ class ServerBase {
|
|
|
55
70
|
return this.app.onError.bind(this.app);
|
|
56
71
|
}
|
|
57
72
|
constructor(options) {
|
|
58
|
-
_class_private_method_init(this, _getAppContext);
|
|
59
73
|
_class_private_method_init(this, _applyMiddlewares);
|
|
74
|
+
this.plugins = [];
|
|
75
|
+
this.serverContext = null;
|
|
60
76
|
this.options = options;
|
|
61
|
-
const { config, serverConfig } = options;
|
|
62
|
-
const appContext = _class_private_method_get(this, _getAppContext, getAppContext).call(this);
|
|
63
|
-
this.appContext = appContext;
|
|
64
|
-
this.pluginManager = new PluginManager({
|
|
65
|
-
cliConfig: config,
|
|
66
|
-
appContext,
|
|
67
|
-
serverConfig
|
|
68
|
-
});
|
|
69
77
|
this.app = new Hono();
|
|
70
78
|
}
|
|
71
79
|
}
|
|
72
|
-
function getAppContext() {
|
|
73
|
-
const { appContext: context, pwd, routes, metaName } = this.options;
|
|
74
|
-
const appContext = {
|
|
75
|
-
routes,
|
|
76
|
-
middlewares: [],
|
|
77
|
-
appDirectory: (context === null || context === void 0 ? void 0 : context.appDirectory) || "",
|
|
78
|
-
apiDirectory: context === null || context === void 0 ? void 0 : context.apiDirectory,
|
|
79
|
-
internalDirectory: (context === null || context === void 0 ? void 0 : context.internalDirectory) || "",
|
|
80
|
-
lambdaDirectory: context === null || context === void 0 ? void 0 : context.lambdaDirectory,
|
|
81
|
-
sharedDirectory: (context === null || context === void 0 ? void 0 : context.sharedDirectory) || "",
|
|
82
|
-
distDirectory: pwd,
|
|
83
|
-
plugins: [],
|
|
84
|
-
metaName: metaName || "modern-js",
|
|
85
|
-
serverBase: this
|
|
86
|
-
};
|
|
87
|
-
return createContext(appContext);
|
|
88
|
-
}
|
|
89
80
|
function applyMiddlewares() {
|
|
90
|
-
const { middlewares } = this.
|
|
81
|
+
const { middlewares } = this.serverContext;
|
|
91
82
|
const preMiddlewares = [];
|
|
92
83
|
const defaultMiddlewares = [];
|
|
93
84
|
const postMiddlewares = [];
|
|
@@ -145,8 +136,8 @@ function createServerBase(options) {
|
|
|
145
136
|
if (options == null) {
|
|
146
137
|
throw new Error("can not start server without options");
|
|
147
138
|
}
|
|
148
|
-
const
|
|
149
|
-
return
|
|
139
|
+
const server2 = new ServerBase(options);
|
|
140
|
+
return server2;
|
|
150
141
|
}
|
|
151
142
|
export {
|
|
152
143
|
ServerBase,
|
|
File without changes
|