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