@modern-js/server-core 2.65.4 → 2.65.6-alpha.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.
Files changed (75) hide show
  1. package/dist/cjs/adapters/node/hono.js +24 -0
  2. package/dist/cjs/adapters/node/index.js +2 -0
  3. package/dist/cjs/index.js +2 -5
  4. package/dist/cjs/plugins/compat/hooks.js +92 -0
  5. package/dist/cjs/plugins/compat/index.js +65 -0
  6. package/dist/cjs/plugins/customServer/index.js +11 -17
  7. package/dist/cjs/plugins/default.js +2 -0
  8. package/dist/cjs/plugins/index.js +5 -0
  9. package/dist/cjs/plugins/render/index.js +2 -2
  10. package/dist/cjs/serverBase.js +31 -40
  11. package/dist/cjs/types/index.js +2 -2
  12. package/dist/cjs/types/{plugin.js → plugins/base.js} +2 -2
  13. package/dist/cjs/types/plugins/index.js +24 -0
  14. package/dist/cjs/types/plugins/new.js +16 -0
  15. package/dist/cjs/types/plugins/old.js +16 -0
  16. package/dist/esm/adapters/node/hono.js +33 -0
  17. package/dist/esm/adapters/node/index.js +2 -1
  18. package/dist/esm/index.js +1 -3
  19. package/dist/esm/plugins/compat/hooks.js +70 -0
  20. package/dist/esm/plugins/compat/index.js +45 -0
  21. package/dist/esm/plugins/customServer/index.js +30 -22
  22. package/dist/esm/plugins/default.js +2 -0
  23. package/dist/esm/plugins/index.js +3 -0
  24. package/dist/esm/plugins/render/index.js +3 -3
  25. package/dist/esm/serverBase.js +34 -38
  26. package/dist/esm/types/index.js +1 -1
  27. package/dist/esm/types/plugins/base.js +0 -0
  28. package/dist/esm/types/plugins/index.js +2 -0
  29. package/dist/esm/types/plugins/new.js +0 -0
  30. package/dist/esm/types/plugins/old.js +0 -0
  31. package/dist/esm-node/adapters/node/hono.js +23 -0
  32. package/dist/esm-node/adapters/node/index.js +2 -1
  33. package/dist/esm-node/index.js +1 -3
  34. package/dist/esm-node/plugins/compat/hooks.js +66 -0
  35. package/dist/esm-node/plugins/compat/index.js +40 -0
  36. package/dist/esm-node/plugins/customServer/index.js +10 -16
  37. package/dist/esm-node/plugins/default.js +2 -0
  38. package/dist/esm-node/plugins/index.js +3 -0
  39. package/dist/esm-node/plugins/render/index.js +2 -2
  40. package/dist/esm-node/serverBase.js +31 -40
  41. package/dist/esm-node/types/index.js +1 -1
  42. package/dist/esm-node/types/plugins/index.js +2 -0
  43. package/dist/esm-node/types/plugins/old.js +0 -0
  44. package/dist/types/adapters/node/hono.d.ts +6 -0
  45. package/dist/types/adapters/node/index.d.ts +1 -1
  46. package/dist/types/adapters/node/plugins/nodeServer.d.ts +4 -2
  47. package/dist/types/adapters/node/plugins/resource.d.ts +3 -3
  48. package/dist/types/adapters/node/plugins/static.d.ts +2 -2
  49. package/dist/types/index.d.ts +1 -2
  50. package/dist/types/plugins/compat/hooks.d.ts +8 -0
  51. package/dist/types/plugins/compat/index.d.ts +3 -0
  52. package/dist/types/plugins/customServer/index.d.ts +3 -3
  53. package/dist/types/plugins/default.d.ts +2 -2
  54. package/dist/types/plugins/favicon.d.ts +2 -2
  55. package/dist/types/plugins/index.d.ts +1 -0
  56. package/dist/types/plugins/log.d.ts +2 -2
  57. package/dist/types/plugins/monitors.d.ts +4 -4
  58. package/dist/types/plugins/processedBy.d.ts +2 -2
  59. package/dist/types/plugins/render/index.d.ts +2 -2
  60. package/dist/types/plugins/render/inject.d.ts +2 -2
  61. package/dist/types/plugins/route.d.ts +2 -2
  62. package/dist/types/serverBase.d.ts +11 -22
  63. package/dist/types/types/index.d.ts +1 -1
  64. package/dist/types/types/plugins/base.d.ts +80 -0
  65. package/dist/types/types/plugins/index.d.ts +3 -0
  66. package/dist/types/types/plugins/new.d.ts +34 -0
  67. package/dist/types/types/plugins/old.d.ts +56 -0
  68. package/package.json +9 -9
  69. package/dist/cjs/pluginManager.js +0 -106
  70. package/dist/esm/pluginManager.js +0 -140
  71. package/dist/esm-node/pluginManager.js +0 -82
  72. package/dist/types/pluginManager.d.ts +0 -31
  73. package/dist/types/types/plugin.d.ts +0 -150
  74. /package/dist/esm-node/types/{plugin.js → plugins/base.js} +0 -0
  75. /package/dist/{esm/types/plugin.js → esm-node/types/plugins/new.js} +0 -0
@@ -0,0 +1,70 @@
1
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
+ function getHookRunners(context) {
3
+ var hooks = context.hooks;
4
+ return {
5
+ config: function(params) {
6
+ return hooks.modifyConfig.call(params);
7
+ },
8
+ prepare: function() {
9
+ return hooks.onPrepare.call();
10
+ },
11
+ reset: function(params) {
12
+ return hooks.onReset.call(params);
13
+ },
14
+ fallback: function(input) {
15
+ return hooks.fallback.call(input);
16
+ },
17
+ prepareWebServer: function(input) {
18
+ return hooks.prepareWebServer.call(input);
19
+ },
20
+ prepareApiServer: function(input) {
21
+ return hooks.prepareApiServer.call(input);
22
+ },
23
+ afterMatch: function(ctx) {
24
+ return hooks.afterMatch.call(ctx);
25
+ },
26
+ afterRender: function(ctx) {
27
+ return hooks.afterRender.call(ctx);
28
+ },
29
+ afterStreamingRender: function(ctx) {
30
+ return hooks.afterStreamingRender.call(ctx);
31
+ }
32
+ };
33
+ }
34
+ function transformHookRunner(hookRunnerName) {
35
+ switch (hookRunnerName) {
36
+ case "config":
37
+ return "modifyConfig";
38
+ case "prepare":
39
+ return "onPrepare";
40
+ case "reset":
41
+ return "onReset";
42
+ default:
43
+ return hookRunnerName;
44
+ }
45
+ }
46
+ function handleSetupResult(setupResult, api) {
47
+ if (!setupResult) {
48
+ return;
49
+ }
50
+ Object.keys(setupResult).forEach(function(key) {
51
+ var fn = setupResult[key];
52
+ if (typeof fn === "function") {
53
+ var newAPI = transformHookRunner(key);
54
+ if (api[newAPI]) {
55
+ api[newAPI](function() {
56
+ for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
57
+ params[_key] = arguments[_key];
58
+ }
59
+ var res = fn.apply(void 0, _to_consumable_array(params));
60
+ return res;
61
+ });
62
+ }
63
+ }
64
+ });
65
+ }
66
+ export {
67
+ getHookRunners,
68
+ handleSetupResult,
69
+ transformHookRunner
70
+ };
@@ -0,0 +1,45 @@
1
+ import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
2
+ import { createAsyncHook, createAsyncPipelineHook } from "@modern-js/plugin-v2";
3
+ import { getHookRunners } from "./hooks";
4
+ import { handleSetupResult } from "./hooks";
5
+ var compatPlugin = function() {
6
+ return {
7
+ name: "@modern-js/server-compat",
8
+ registryHooks: {
9
+ fallback: createAsyncHook(),
10
+ prepareWebServer: createAsyncPipelineHook(),
11
+ prepareApiServer: createAsyncPipelineHook(),
12
+ afterMatch: createAsyncPipelineHook(),
13
+ afterRender: createAsyncPipelineHook(),
14
+ afterStreamingRender: createAsyncPipelineHook()
15
+ },
16
+ _registryApi: function(getServerContext, updateServerContext) {
17
+ var getInternalContext = function() {
18
+ return getServerContext()._internalContext;
19
+ };
20
+ return {
21
+ useConfigContext: function() {
22
+ return getInternalContext().config;
23
+ },
24
+ useAppContext: function() {
25
+ var _getServerContext = getServerContext(), _internalContext = _getServerContext._internalContext, serverContext = _object_without_properties(_getServerContext, [
26
+ "_internalContext"
27
+ ]);
28
+ return serverContext;
29
+ },
30
+ setAppContext: function(context) {
31
+ return updateServerContext(context);
32
+ },
33
+ useHookRunners: function() {
34
+ return getHookRunners(getInternalContext());
35
+ }
36
+ };
37
+ },
38
+ setup: function() {
39
+ }
40
+ };
41
+ };
42
+ export {
43
+ compatPlugin,
44
+ handleSetupResult
45
+ };
@@ -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(runner, serverBase, pwd) {
20
+ function CustomServer2(hooks, serverBase, pwd) {
20
21
  _class_call_check(this, CustomServer2);
21
- this.runner = runner;
22
+ this.hooks = hooks;
22
23
  this.serverBase = serverBase;
23
24
  var webExtension = [];
24
- this.serverMiddlewarePromise = runner.prepareWebServer({
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.runner.afterMatch(afterMatchCtx, {
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(chunk) {
109
- var context = afterStreamingRenderContext(chunk);
110
- return _this.runner.afterStreamingRender(context, {
111
- onLast: function(param) {
112
- var _$chunk = param.chunk;
113
- return _$chunk;
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.runner.afterRender(afterRenderCtx, {
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, runner, config, customServer, serverMiddleware, pageRoutes, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, route, originUrlPath, _route_entryName, entryName, urlPath, customServerHookMiddleware, customServerMiddleware, err;
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
- runner = api.useHookRunners();
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(runner, serverBase, pwd);
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({
@@ -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 { createContext } from "@modern-js/plugin";
8
+ import { server } from "@modern-js/plugin-v2/server";
9
9
  import { Hono } from "hono";
10
- import { PluginManager } from "./pluginManager";
11
- var _getAppContext = /* @__PURE__ */ new WeakSet(), _applyMiddlewares = /* @__PURE__ */ new WeakSet();
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 runner;
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
- _this.pluginManager.init()
39
+ server.run({
40
+ plugins: _this.plugins,
41
+ options: _this.options,
42
+ config: mergedConfig,
43
+ handleSetupResult
44
+ })
40
45
  ];
41
46
  case 1:
42
- runner = _state.sent();
43
- _this.runner = runner;
47
+ serverContext = _state.sent().serverContext;
48
+ serverContext.serverBase = _this;
44
49
  return [
45
50
  4,
46
- runner.prepare()
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
- this.pluginManager.addPlugins(plugins);
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.appContext.get().middlewares;
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 server = new ServerBase(options);
246
- return server;
241
+ var server2 = new ServerBase(options);
242
+ return server2;
247
243
  }
248
244
  export {
249
245
  ServerBase,
@@ -1,4 +1,4 @@
1
1
  export * from "./config";
2
- export * from "./plugin";
2
+ export * from "./plugins";
3
3
  export * from "./render";
4
4
  export * from "./server";
File without changes
@@ -0,0 +1,2 @@
1
+ export * from "./new";
2
+ export * from "./old";
File without changes
File without changes
@@ -44,7 +44,30 @@ const connectMid2HonoMid = (handler) => {
44
44
  });
45
45
  };
46
46
  };
47
+ const connectMockMid2HonoMid = (handler) => {
48
+ return async (context, next) => {
49
+ return new Promise((resolve, reject) => {
50
+ const { req, res } = context.env.node;
51
+ if (handler.length < 3) {
52
+ res.once("finish", () => {
53
+ context.finalized = true;
54
+ resolve();
55
+ });
56
+ handler(req, res, noop);
57
+ } else {
58
+ handler(req, res, (err) => {
59
+ if (err) {
60
+ reject(err);
61
+ } else {
62
+ resolve(next());
63
+ }
64
+ });
65
+ }
66
+ });
67
+ };
68
+ };
47
69
  export {
48
70
  connectMid2HonoMid,
71
+ connectMockMid2HonoMid,
49
72
  httpCallBack2HonoMid
50
73
  };
@@ -1,9 +1,10 @@
1
- import { httpCallBack2HonoMid, connectMid2HonoMid } from "./hono";
1
+ import { httpCallBack2HonoMid, connectMid2HonoMid, connectMockMid2HonoMid } from "./hono";
2
2
  import { createNodeServer, sendResponse, createWebRequest, writeReadableStreamToWritable } from "./node";
3
3
  import { serverStaticPlugin, injectResourcePlugin, getHtmlTemplates, getServerManifest, injectNodeSeverPlugin, injectRscManifestPlugin } from "./plugins";
4
4
  import { loadServerPlugins, loadServerEnv, loadServerRuntimeConfig, loadServerCliConfig, loadCacheConfig } from "./helper";
5
5
  export {
6
6
  connectMid2HonoMid,
7
+ connectMockMid2HonoMid,
7
8
  createNodeServer,
8
9
  createWebRequest,
9
10
  getHtmlTemplates,
@@ -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/plugin";
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.runner.afterMatch(afterMatchCtx, {
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
- return this.runner.afterStreamingRender(context, {
56
- onLast: ({ chunk: chunk2 }) => chunk2
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.runner.afterRender(afterRenderCtx, {
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(runner, serverBase, pwd) {
114
- this.runner = runner;
109
+ constructor(hooks, serverBase, pwd) {
110
+ this.hooks = hooks;
115
111
  this.serverBase = serverBase;
116
112
  const webExtension = [];
117
- this.serverMiddlewarePromise = runner.prepareWebServer({
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),