@modern-js/plugin-express 2.48.6 → 2.49.1

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.
@@ -36,9 +36,9 @@ var import_express = __toESM(require("express"));
36
36
  var import_cookie_parser = __toESM(require("cookie-parser"));
37
37
  var import_utils = require("@modern-js/utils");
38
38
  var import_finalhandler = __toESM(require("finalhandler"));
39
+ var import_node = require("@modern-js/server-core/base/node");
39
40
  var import_context = require("./context");
40
41
  var import_registerRoutes = __toESM(require("./registerRoutes"));
41
- const debug = (0, import_utils.createDebugger)("express");
42
42
  const findAppModule = async (apiDir) => {
43
43
  const exts = [
44
44
  ".ts",
@@ -70,6 +70,54 @@ const useRun = (app) => {
70
70
  }, next);
71
71
  });
72
72
  };
73
+ const createApp = async ({ apiDir, middlewares, mode, apiHandlerInfos, render }) => {
74
+ let app;
75
+ if (mode === "framework") {
76
+ const appModule = await findAppModule(apiDir);
77
+ app = appModule[0];
78
+ const hooks = appModule[1];
79
+ if (!(app === null || app === void 0 ? void 0 : app.use)) {
80
+ app = (0, import_express.default)();
81
+ }
82
+ initApp(app);
83
+ if (middlewares && middlewares.length > 0) {
84
+ initMiddlewares(middlewares, app);
85
+ }
86
+ useRun(app);
87
+ (0, import_registerRoutes.default)(app, apiHandlerInfos);
88
+ if (hooks) {
89
+ const { afterLambdaRegisted } = hooks;
90
+ if (afterLambdaRegisted) {
91
+ afterLambdaRegisted(app);
92
+ }
93
+ }
94
+ } else if (mode === "function") {
95
+ app = (0, import_express.default)();
96
+ initApp(app);
97
+ if (middlewares && middlewares.length > 0) {
98
+ initMiddlewares(middlewares, app);
99
+ }
100
+ useRun(app);
101
+ (0, import_registerRoutes.default)(app, apiHandlerInfos);
102
+ } else {
103
+ throw new Error(`mode must be function or framework`);
104
+ }
105
+ if (render) {
106
+ app.use(async (req, res, next) => {
107
+ const response = await render(req.__honoRequest, {
108
+ logger: import_utils.logger,
109
+ nodeReq: req,
110
+ templates: req.__templates,
111
+ serverManifest: req.__serverManifest
112
+ });
113
+ if (response) {
114
+ return (0, import_node.sendResponse)(response, res).then(next);
115
+ }
116
+ next();
117
+ });
118
+ }
119
+ return app;
120
+ };
73
121
  const initApp = (app) => {
74
122
  app.use((0, import_cookie_parser.default)());
75
123
  app.use(import_express.default.text());
@@ -79,113 +127,68 @@ const initApp = (app) => {
79
127
  app.use(import_express.default.json());
80
128
  return app;
81
129
  };
82
- var plugin_default = () => ({
83
- name: "@modern-js/plugin-express",
84
- pre: [
85
- "@modern-js/plugin-bff"
86
- ],
87
- post: [
88
- "@modern-js/plugin-server"
89
- ],
90
- setup: (api) => ({
91
- async prepareApiServer({ pwd, config, render }) {
92
- var _userConfig_bff;
93
- let app;
94
- const appContext = api.useAppContext();
95
- const apiHandlerInfos = appContext.apiHandlerInfos;
96
- const apiDirectory = appContext.apiDirectory;
97
- const apiDir = apiDirectory || path.join(pwd, "./api");
98
- const mode = appContext.apiMode;
99
- const userConfig = api.useConfigContext();
100
- if (mode === "framework") {
101
- const appModule = await findAppModule(apiDir);
102
- app = appModule[0];
103
- const hooks = appModule[1];
104
- if (!(app === null || app === void 0 ? void 0 : app.use)) {
105
- app = (0, import_express.default)();
106
- }
107
- initApp(app);
108
- if (config) {
109
- const { middleware } = config;
110
- initMiddlewares(middleware, app);
111
- }
112
- useRun(app);
113
- (0, import_registerRoutes.default)(app, apiHandlerInfos);
114
- if (hooks) {
115
- const { afterLambdaRegisted } = hooks;
116
- if (afterLambdaRegisted) {
117
- afterLambdaRegisted(app);
118
- }
119
- }
120
- } else if (mode === "function") {
121
- app = (0, import_express.default)();
122
- initApp(app);
123
- if (config) {
124
- const { middleware } = config;
125
- initMiddlewares(middleware, app);
126
- }
127
- useRun(app);
128
- (0, import_registerRoutes.default)(app, apiHandlerInfos);
129
- } else {
130
- throw new Error(`mode must be function or framework`);
131
- }
132
- if (((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render) {
133
- app.use(async (req, res, next) => {
134
- const html = await render(req, res);
135
- if (html) {
136
- res.end(html);
137
- }
138
- next();
130
+ var plugin_default = () => {
131
+ let app;
132
+ let apiDir;
133
+ let mode;
134
+ let renderHtml;
135
+ return {
136
+ name: "@modern-js/plugin-express",
137
+ pre: [
138
+ "@modern-js/plugin-bff"
139
+ ],
140
+ post: [
141
+ "@modern-js/plugin-server"
142
+ ],
143
+ setup: (api) => ({
144
+ async onApiChange(changes) {
145
+ const appContext = api.useAppContext();
146
+ const middlewares = appContext.apiMiddlewares;
147
+ const apiHandlerInfos = appContext.apiHandlerInfos;
148
+ app = await createApp({
149
+ apiDir,
150
+ middlewares,
151
+ mode,
152
+ apiHandlerInfos,
153
+ render: renderHtml
139
154
  });
140
- }
141
- return (req, res) => new Promise((resolve, reject) => {
142
- const handler = (err) => {
143
- if (err) {
144
- return reject(err);
145
- }
146
- return (0, import_finalhandler.default)(req, res, {})(null);
147
- };
148
- res.on("finish", (err) => {
149
- if (err) {
150
- return reject(err);
151
- }
152
- return resolve();
155
+ return changes;
156
+ },
157
+ async prepareApiServer({ pwd, render }) {
158
+ var _userConfig_bff;
159
+ const appContext = api.useAppContext();
160
+ const apiHandlerInfos = appContext.apiHandlerInfos;
161
+ const { apiDirectory } = appContext;
162
+ const userConfig = api.useConfigContext();
163
+ const middlewares = appContext.apiMiddlewares;
164
+ mode = appContext.apiMode;
165
+ renderHtml = ((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render ? render : void 0;
166
+ apiDir = apiDirectory || path.join(pwd, "./api");
167
+ app = await createApp({
168
+ apiDir,
169
+ middlewares,
170
+ mode,
171
+ apiHandlerInfos,
172
+ render: renderHtml
153
173
  });
154
- return app(req, res, handler);
155
- });
156
- },
157
- prepareWebServer({ config }, next) {
158
- var _userConfig_server;
159
- const userConfig = api.useConfigContext();
160
- if (!(userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_server = userConfig.server) === null || _userConfig_server === void 0 ? void 0 : _userConfig_server.enableFrameworkExt)) {
161
- return next();
162
- }
163
- const app = (0, import_express.default)();
164
- initApp(app);
165
- if (config) {
166
- const { middleware } = config;
167
- debug("web middleware", middleware);
168
- initMiddlewares(middleware, app);
169
- }
170
- return (ctx) => new Promise((resolve, reject) => {
171
- const { source: { req, res } } = ctx;
172
- const handler = (err) => {
173
- if (err) {
174
- return reject(err);
175
- }
176
- if (res.headersSent && res.statusCode !== 200) {
174
+ const handler = (req, res) => new Promise((resolve, reject) => {
175
+ const handler2 = (err) => {
176
+ if (err) {
177
+ return reject(err);
178
+ }
177
179
  (0, import_finalhandler.default)(req, res, {})(null);
178
- }
179
- return resolve();
180
- };
181
- res.on("finish", (err) => {
182
- if (err) {
183
- return reject(err);
184
- }
185
- return resolve();
180
+ return resolve();
181
+ };
182
+ res.on("finish", (err) => {
183
+ if (err) {
184
+ return reject(err);
185
+ }
186
+ return resolve();
187
+ });
188
+ return app(req, res, handler2);
186
189
  });
187
- return app(req, res, handler);
188
- });
189
- }
190
- })
191
- });
190
+ return (0, import_node.httpCallBack2HonoMid)(handler);
191
+ }
192
+ })
193
+ };
194
+ };
@@ -3,11 +3,11 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
3
  import * as path from "path";
4
4
  import express from "express";
5
5
  import cookieParser from "cookie-parser";
6
- import { fs, createDebugger, compatRequire } from "@modern-js/utils";
6
+ import { fs, compatRequire, logger } from "@modern-js/utils";
7
7
  import finalhandler from "finalhandler";
8
+ import { httpCallBack2HonoMid, sendResponse } from "@modern-js/server-core/base/node";
8
9
  import { run } from "./context";
9
10
  import registerRoutes from "./registerRoutes";
10
- var debug = createDebugger("express");
11
11
  var findAppModule = function() {
12
12
  var _ref = _async_to_generator(function(apiDir) {
13
13
  var exts, paths, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, filename, err;
@@ -113,6 +113,106 @@ var useRun = function(app) {
113
113
  }, next);
114
114
  });
115
115
  };
116
+ var createApp = function() {
117
+ var _ref = _async_to_generator(function(param) {
118
+ var apiDir, middlewares, mode, apiHandlerInfos, render, app, appModule, hooks, afterLambdaRegisted;
119
+ return _ts_generator(this, function(_state) {
120
+ switch (_state.label) {
121
+ case 0:
122
+ apiDir = param.apiDir, middlewares = param.middlewares, mode = param.mode, apiHandlerInfos = param.apiHandlerInfos, render = param.render;
123
+ if (!(mode === "framework"))
124
+ return [
125
+ 3,
126
+ 2
127
+ ];
128
+ return [
129
+ 4,
130
+ findAppModule(apiDir)
131
+ ];
132
+ case 1:
133
+ appModule = _state.sent();
134
+ app = appModule[0];
135
+ hooks = appModule[1];
136
+ if (!(app === null || app === void 0 ? void 0 : app.use)) {
137
+ app = express();
138
+ }
139
+ initApp(app);
140
+ if (middlewares && middlewares.length > 0) {
141
+ initMiddlewares(middlewares, app);
142
+ }
143
+ useRun(app);
144
+ registerRoutes(app, apiHandlerInfos);
145
+ if (hooks) {
146
+ afterLambdaRegisted = hooks.afterLambdaRegisted;
147
+ if (afterLambdaRegisted) {
148
+ afterLambdaRegisted(app);
149
+ }
150
+ }
151
+ return [
152
+ 3,
153
+ 3
154
+ ];
155
+ case 2:
156
+ if (mode === "function") {
157
+ app = express();
158
+ initApp(app);
159
+ if (middlewares && middlewares.length > 0) {
160
+ initMiddlewares(middlewares, app);
161
+ }
162
+ useRun(app);
163
+ registerRoutes(app, apiHandlerInfos);
164
+ } else {
165
+ throw new Error("mode must be function or framework");
166
+ }
167
+ _state.label = 3;
168
+ case 3:
169
+ if (render) {
170
+ app.use(function() {
171
+ var _ref2 = _async_to_generator(function(req, res, next) {
172
+ var response;
173
+ return _ts_generator(this, function(_state2) {
174
+ switch (_state2.label) {
175
+ case 0:
176
+ return [
177
+ 4,
178
+ render(req.__honoRequest, {
179
+ logger,
180
+ nodeReq: req,
181
+ templates: req.__templates,
182
+ serverManifest: req.__serverManifest
183
+ })
184
+ ];
185
+ case 1:
186
+ response = _state2.sent();
187
+ if (response) {
188
+ return [
189
+ 2,
190
+ sendResponse(response, res).then(next)
191
+ ];
192
+ }
193
+ next();
194
+ return [
195
+ 2
196
+ ];
197
+ }
198
+ });
199
+ });
200
+ return function(req, res, next) {
201
+ return _ref2.apply(this, arguments);
202
+ };
203
+ }());
204
+ }
205
+ return [
206
+ 2,
207
+ app
208
+ ];
209
+ }
210
+ });
211
+ });
212
+ return function createApp2(_) {
213
+ return _ref.apply(this, arguments);
214
+ };
215
+ }();
116
216
  var initApp = function(app) {
117
217
  app.use(cookieParser());
118
218
  app.use(express.text());
@@ -123,6 +223,10 @@ var initApp = function(app) {
123
223
  return app;
124
224
  };
125
225
  function plugin_default() {
226
+ var app;
227
+ var apiDir;
228
+ var mode;
229
+ var renderHtml;
126
230
  return {
127
231
  name: "@modern-js/plugin-express",
128
232
  pre: [
@@ -133,154 +237,87 @@ function plugin_default() {
133
237
  ],
134
238
  setup: function(api) {
135
239
  return {
240
+ onApiChange: function onApiChange(changes) {
241
+ return _async_to_generator(function() {
242
+ var appContext, middlewares, apiHandlerInfos;
243
+ return _ts_generator(this, function(_state) {
244
+ switch (_state.label) {
245
+ case 0:
246
+ appContext = api.useAppContext();
247
+ middlewares = appContext.apiMiddlewares;
248
+ apiHandlerInfos = appContext.apiHandlerInfos;
249
+ return [
250
+ 4,
251
+ createApp({
252
+ apiDir,
253
+ middlewares,
254
+ mode,
255
+ apiHandlerInfos,
256
+ render: renderHtml
257
+ })
258
+ ];
259
+ case 1:
260
+ app = _state.sent();
261
+ return [
262
+ 2,
263
+ changes
264
+ ];
265
+ }
266
+ });
267
+ })();
268
+ },
136
269
  prepareApiServer: function prepareApiServer(param) {
137
- var pwd = param.pwd, config = param.config, render = param.render;
270
+ var pwd = param.pwd, render = param.render;
138
271
  return _async_to_generator(function() {
139
- var _userConfig_bff, app, appContext, apiHandlerInfos, apiDirectory, apiDir, mode, userConfig, appModule, hooks, middleware, afterLambdaRegisted, middleware1;
272
+ var _userConfig_bff, appContext, apiHandlerInfos, apiDirectory, userConfig, middlewares, handler;
140
273
  return _ts_generator(this, function(_state) {
141
274
  switch (_state.label) {
142
275
  case 0:
143
276
  appContext = api.useAppContext();
144
277
  apiHandlerInfos = appContext.apiHandlerInfos;
145
278
  apiDirectory = appContext.apiDirectory;
146
- apiDir = apiDirectory || path.join(pwd, "./api");
147
- mode = appContext.apiMode;
148
279
  userConfig = api.useConfigContext();
149
- if (!(mode === "framework"))
150
- return [
151
- 3,
152
- 2
153
- ];
280
+ middlewares = appContext.apiMiddlewares;
281
+ mode = appContext.apiMode;
282
+ renderHtml = ((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render ? render : void 0;
283
+ apiDir = apiDirectory || path.join(pwd, "./api");
154
284
  return [
155
285
  4,
156
- findAppModule(apiDir)
286
+ createApp({
287
+ apiDir,
288
+ middlewares,
289
+ mode,
290
+ apiHandlerInfos,
291
+ render: renderHtml
292
+ })
157
293
  ];
158
294
  case 1:
159
- appModule = _state.sent();
160
- app = appModule[0];
161
- hooks = appModule[1];
162
- if (!(app === null || app === void 0 ? void 0 : app.use)) {
163
- app = express();
164
- }
165
- initApp(app);
166
- if (config) {
167
- middleware = config.middleware;
168
- initMiddlewares(middleware, app);
169
- }
170
- useRun(app);
171
- registerRoutes(app, apiHandlerInfos);
172
- if (hooks) {
173
- afterLambdaRegisted = hooks.afterLambdaRegisted;
174
- if (afterLambdaRegisted) {
175
- afterLambdaRegisted(app);
176
- }
177
- }
178
- return [
179
- 3,
180
- 3
181
- ];
182
- case 2:
183
- if (mode === "function") {
184
- app = express();
185
- initApp(app);
186
- if (config) {
187
- middleware1 = config.middleware;
188
- initMiddlewares(middleware1, app);
189
- }
190
- useRun(app);
191
- registerRoutes(app, apiHandlerInfos);
192
- } else {
193
- throw new Error("mode must be function or framework");
194
- }
195
- _state.label = 3;
196
- case 3:
197
- if (((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render) {
198
- app.use(function() {
199
- var _ref = _async_to_generator(function(req, res, next) {
200
- var html;
201
- return _ts_generator(this, function(_state2) {
202
- switch (_state2.label) {
203
- case 0:
204
- return [
205
- 4,
206
- render(req, res)
207
- ];
208
- case 1:
209
- html = _state2.sent();
210
- if (html) {
211
- res.end(html);
212
- }
213
- next();
214
- return [
215
- 2
216
- ];
217
- }
218
- });
219
- });
220
- return function(req, res, next) {
221
- return _ref.apply(this, arguments);
295
+ app = _state.sent();
296
+ handler = function(req, res) {
297
+ return new Promise(function(resolve, reject) {
298
+ var handler2 = function(err) {
299
+ if (err) {
300
+ return reject(err);
301
+ }
302
+ finalhandler(req, res, {})(null);
303
+ return resolve();
222
304
  };
223
- }());
224
- }
305
+ res.on("finish", function(err) {
306
+ if (err) {
307
+ return reject(err);
308
+ }
309
+ return resolve();
310
+ });
311
+ return app(req, res, handler2);
312
+ });
313
+ };
225
314
  return [
226
315
  2,
227
- function(req, res) {
228
- return new Promise(function(resolve, reject) {
229
- var handler = function(err) {
230
- if (err) {
231
- return reject(err);
232
- }
233
- return finalhandler(req, res, {})(null);
234
- };
235
- res.on("finish", function(err) {
236
- if (err) {
237
- return reject(err);
238
- }
239
- return resolve();
240
- });
241
- return app(req, res, handler);
242
- });
243
- }
316
+ httpCallBack2HonoMid(handler)
244
317
  ];
245
318
  }
246
319
  });
247
320
  })();
248
- },
249
- prepareWebServer: function prepareWebServer(param, next) {
250
- var config = param.config;
251
- var _userConfig_server;
252
- var userConfig = api.useConfigContext();
253
- if (!(userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_server = userConfig.server) === null || _userConfig_server === void 0 ? void 0 : _userConfig_server.enableFrameworkExt)) {
254
- return next();
255
- }
256
- var app = express();
257
- initApp(app);
258
- if (config) {
259
- var middleware = config.middleware;
260
- debug("web middleware", middleware);
261
- initMiddlewares(middleware, app);
262
- }
263
- return function(ctx) {
264
- return new Promise(function(resolve, reject) {
265
- var _ctx_source = ctx.source, req = _ctx_source.req, res = _ctx_source.res;
266
- var handler = function(err) {
267
- if (err) {
268
- return reject(err);
269
- }
270
- if (res.headersSent && res.statusCode !== 200) {
271
- finalhandler(req, res, {})(null);
272
- }
273
- return resolve();
274
- };
275
- res.on("finish", function(err) {
276
- if (err) {
277
- return reject(err);
278
- }
279
- return resolve();
280
- });
281
- return app(req, res, handler);
282
- });
283
- };
284
321
  }
285
322
  };
286
323
  }
@@ -1,11 +1,11 @@
1
1
  import * as path from "path";
2
2
  import express from "express";
3
3
  import cookieParser from "cookie-parser";
4
- import { fs, createDebugger, compatRequire } from "@modern-js/utils";
4
+ import { fs, compatRequire, logger } from "@modern-js/utils";
5
5
  import finalhandler from "finalhandler";
6
+ import { httpCallBack2HonoMid, sendResponse } from "@modern-js/server-core/base/node";
6
7
  import { run } from "./context";
7
8
  import registerRoutes from "./registerRoutes";
8
- const debug = createDebugger("express");
9
9
  const findAppModule = async (apiDir) => {
10
10
  const exts = [
11
11
  ".ts",
@@ -37,6 +37,54 @@ const useRun = (app) => {
37
37
  }, next);
38
38
  });
39
39
  };
40
+ const createApp = async ({ apiDir, middlewares, mode, apiHandlerInfos, render }) => {
41
+ let app;
42
+ if (mode === "framework") {
43
+ const appModule = await findAppModule(apiDir);
44
+ app = appModule[0];
45
+ const hooks = appModule[1];
46
+ if (!(app === null || app === void 0 ? void 0 : app.use)) {
47
+ app = express();
48
+ }
49
+ initApp(app);
50
+ if (middlewares && middlewares.length > 0) {
51
+ initMiddlewares(middlewares, app);
52
+ }
53
+ useRun(app);
54
+ registerRoutes(app, apiHandlerInfos);
55
+ if (hooks) {
56
+ const { afterLambdaRegisted } = hooks;
57
+ if (afterLambdaRegisted) {
58
+ afterLambdaRegisted(app);
59
+ }
60
+ }
61
+ } else if (mode === "function") {
62
+ app = express();
63
+ initApp(app);
64
+ if (middlewares && middlewares.length > 0) {
65
+ initMiddlewares(middlewares, app);
66
+ }
67
+ useRun(app);
68
+ registerRoutes(app, apiHandlerInfos);
69
+ } else {
70
+ throw new Error(`mode must be function or framework`);
71
+ }
72
+ if (render) {
73
+ app.use(async (req, res, next) => {
74
+ const response = await render(req.__honoRequest, {
75
+ logger,
76
+ nodeReq: req,
77
+ templates: req.__templates,
78
+ serverManifest: req.__serverManifest
79
+ });
80
+ if (response) {
81
+ return sendResponse(response, res).then(next);
82
+ }
83
+ next();
84
+ });
85
+ }
86
+ return app;
87
+ };
40
88
  const initApp = (app) => {
41
89
  app.use(cookieParser());
42
90
  app.use(express.text());
@@ -46,116 +94,71 @@ const initApp = (app) => {
46
94
  app.use(express.json());
47
95
  return app;
48
96
  };
49
- var plugin_default = () => ({
50
- name: "@modern-js/plugin-express",
51
- pre: [
52
- "@modern-js/plugin-bff"
53
- ],
54
- post: [
55
- "@modern-js/plugin-server"
56
- ],
57
- setup: (api) => ({
58
- async prepareApiServer({ pwd, config, render }) {
59
- var _userConfig_bff;
60
- let app;
61
- const appContext = api.useAppContext();
62
- const apiHandlerInfos = appContext.apiHandlerInfos;
63
- const apiDirectory = appContext.apiDirectory;
64
- const apiDir = apiDirectory || path.join(pwd, "./api");
65
- const mode = appContext.apiMode;
66
- const userConfig = api.useConfigContext();
67
- if (mode === "framework") {
68
- const appModule = await findAppModule(apiDir);
69
- app = appModule[0];
70
- const hooks = appModule[1];
71
- if (!(app === null || app === void 0 ? void 0 : app.use)) {
72
- app = express();
73
- }
74
- initApp(app);
75
- if (config) {
76
- const { middleware } = config;
77
- initMiddlewares(middleware, app);
78
- }
79
- useRun(app);
80
- registerRoutes(app, apiHandlerInfos);
81
- if (hooks) {
82
- const { afterLambdaRegisted } = hooks;
83
- if (afterLambdaRegisted) {
84
- afterLambdaRegisted(app);
85
- }
86
- }
87
- } else if (mode === "function") {
88
- app = express();
89
- initApp(app);
90
- if (config) {
91
- const { middleware } = config;
92
- initMiddlewares(middleware, app);
93
- }
94
- useRun(app);
95
- registerRoutes(app, apiHandlerInfos);
96
- } else {
97
- throw new Error(`mode must be function or framework`);
98
- }
99
- if (((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render) {
100
- app.use(async (req, res, next) => {
101
- const html = await render(req, res);
102
- if (html) {
103
- res.end(html);
104
- }
105
- next();
97
+ var plugin_default = () => {
98
+ let app;
99
+ let apiDir;
100
+ let mode;
101
+ let renderHtml;
102
+ return {
103
+ name: "@modern-js/plugin-express",
104
+ pre: [
105
+ "@modern-js/plugin-bff"
106
+ ],
107
+ post: [
108
+ "@modern-js/plugin-server"
109
+ ],
110
+ setup: (api) => ({
111
+ async onApiChange(changes) {
112
+ const appContext = api.useAppContext();
113
+ const middlewares = appContext.apiMiddlewares;
114
+ const apiHandlerInfos = appContext.apiHandlerInfos;
115
+ app = await createApp({
116
+ apiDir,
117
+ middlewares,
118
+ mode,
119
+ apiHandlerInfos,
120
+ render: renderHtml
106
121
  });
107
- }
108
- return (req, res) => new Promise((resolve, reject) => {
109
- const handler = (err) => {
110
- if (err) {
111
- return reject(err);
112
- }
113
- return finalhandler(req, res, {})(null);
114
- };
115
- res.on("finish", (err) => {
116
- if (err) {
117
- return reject(err);
118
- }
119
- return resolve();
122
+ return changes;
123
+ },
124
+ async prepareApiServer({ pwd, render }) {
125
+ var _userConfig_bff;
126
+ const appContext = api.useAppContext();
127
+ const apiHandlerInfos = appContext.apiHandlerInfos;
128
+ const { apiDirectory } = appContext;
129
+ const userConfig = api.useConfigContext();
130
+ const middlewares = appContext.apiMiddlewares;
131
+ mode = appContext.apiMode;
132
+ renderHtml = ((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render ? render : void 0;
133
+ apiDir = apiDirectory || path.join(pwd, "./api");
134
+ app = await createApp({
135
+ apiDir,
136
+ middlewares,
137
+ mode,
138
+ apiHandlerInfos,
139
+ render: renderHtml
120
140
  });
121
- return app(req, res, handler);
122
- });
123
- },
124
- prepareWebServer({ config }, next) {
125
- var _userConfig_server;
126
- const userConfig = api.useConfigContext();
127
- if (!(userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_server = userConfig.server) === null || _userConfig_server === void 0 ? void 0 : _userConfig_server.enableFrameworkExt)) {
128
- return next();
129
- }
130
- const app = express();
131
- initApp(app);
132
- if (config) {
133
- const { middleware } = config;
134
- debug("web middleware", middleware);
135
- initMiddlewares(middleware, app);
136
- }
137
- return (ctx) => new Promise((resolve, reject) => {
138
- const { source: { req, res } } = ctx;
139
- const handler = (err) => {
140
- if (err) {
141
- return reject(err);
142
- }
143
- if (res.headersSent && res.statusCode !== 200) {
141
+ const handler = (req, res) => new Promise((resolve, reject) => {
142
+ const handler2 = (err) => {
143
+ if (err) {
144
+ return reject(err);
145
+ }
144
146
  finalhandler(req, res, {})(null);
145
- }
146
- return resolve();
147
- };
148
- res.on("finish", (err) => {
149
- if (err) {
150
- return reject(err);
151
- }
152
- return resolve();
147
+ return resolve();
148
+ };
149
+ res.on("finish", (err) => {
150
+ if (err) {
151
+ return reject(err);
152
+ }
153
+ return resolve();
154
+ });
155
+ return app(req, res, handler2);
153
156
  });
154
- return app(req, res, handler);
155
- });
156
- }
157
- })
158
- });
157
+ return httpCallBack2HonoMid(handler);
158
+ }
159
+ })
160
+ };
161
+ };
159
162
  export {
160
163
  plugin_default as default
161
164
  };
@@ -1,3 +1,12 @@
1
1
  import type { ServerPlugin } from '@modern-js/server-core';
2
+ declare global {
3
+ namespace Express {
4
+ interface Request {
5
+ __honoRequest: globalThis.Request;
6
+ __templates: Record<string, string>;
7
+ __serverManifest: any;
8
+ }
9
+ }
10
+ }
2
11
  declare const _default: () => ServerPlugin;
3
12
  export default _default;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.48.6",
18
+ "version": "2.49.1",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/cli/index.d.ts",
21
21
  "main": "./dist/cjs/cli/index.js",
@@ -49,10 +49,10 @@
49
49
  "reflect-metadata": "^0.1.13",
50
50
  "type-is": "^1.6.18",
51
51
  "@swc/helpers": "0.5.3",
52
- "@modern-js/bff-core": "2.48.6",
53
- "@modern-js/bff-runtime": "2.48.6",
54
- "@modern-js/types": "2.48.6",
55
- "@modern-js/utils": "2.48.6"
52
+ "@modern-js/bff-core": "2.49.1",
53
+ "@modern-js/bff-runtime": "2.49.1",
54
+ "@modern-js/utils": "2.49.1",
55
+ "@modern-js/types": "2.49.1"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@types/cookie-parser": "^1.4.2",
@@ -68,11 +68,11 @@
68
68
  "supertest": "^6.1.6",
69
69
  "typescript": "^5",
70
70
  "zod": "^3.22.3",
71
- "@modern-js/app-tools": "2.48.6",
72
- "@modern-js/core": "2.48.6",
73
- "@scripts/jest-config": "2.48.6",
74
- "@scripts/build": "2.48.6",
75
- "@modern-js/server-core": "2.48.6"
71
+ "@modern-js/core": "2.49.1",
72
+ "@modern-js/app-tools": "2.49.1",
73
+ "@scripts/build": "2.49.1",
74
+ "@modern-js/server-core": "2.49.1",
75
+ "@scripts/jest-config": "2.49.1"
76
76
  },
77
77
  "peerDependencies": {
78
78
  "express": "^4.17.1"