Skip to content

Commit a6c7d3b

Browse files
committed
feat: resolve pr issues
1 parent 505dbbe commit a6c7d3b

File tree

20 files changed

+126
-235
lines changed

20 files changed

+126
-235
lines changed

packages/cli/plugin-bff/package.json

+1-7
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
"main": "./dist/cjs/cli.js",
2222
"module": "./dist/esm/cli.js",
2323
"exports": {
24+
"./types": "./types.d.ts",
2425
".": {
2526
"types": "./dist/types/cli.d.ts",
2627
"jsnext:source": "./src/cli.ts",
2728
"default": "./dist/cjs/cli.js"
2829
},
29-
"./types": "./types/index.d.ts",
3030
"./cli": {
3131
"types": "./dist/types/cli.d.ts",
3232
"jsnext:source": "./src/cli.ts",
@@ -87,7 +87,6 @@
8787
"@modern-js/server-utils": "workspace:*",
8888
"@modern-js/utils": "workspace:*",
8989
"type-is": "^1.6.18",
90-
"formidable": "^1.2.2",
9190
"@swc/helpers": "0.5.13"
9291
},
9392
"devDependencies": {
@@ -103,18 +102,13 @@
103102
"@types/jest": "^29",
104103
"@types/node": "^14",
105104
"@types/type-is": "^1.6.3",
106-
"@types/formidable": "^1.2.3",
107105
"jest": "^29",
108106
"memfs": "^3.5.1",
109107
"ts-jest": "^29.1.0",
110108
"typescript": "^5",
111109
"webpack": "^5.98.0",
112-
"hono": "^3.12.2",
113110
"zod": "^3.22.3"
114111
},
115-
"peerDependencies": {
116-
"hono": "^3.12.2"
117-
},
118112
"sideEffects": false,
119113
"publishConfig": {
120114
"registry": "https://registry.npmjs.org/",

packages/cli/plugin-bff/src/cli.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ import type { AppTools, CliPlugin } from '@modern-js/app-tools';
33
import { ApiRouter } from '@modern-js/bff-core';
44
import { compile } from '@modern-js/server-utils';
55
import type { ServerRoute } from '@modern-js/types';
6-
import {
7-
fs,
8-
API_DIR,
9-
SHARED_DIR,
10-
isProd,
11-
normalizeOutputPath,
12-
} from '@modern-js/utils';
6+
import { fs, API_DIR, SHARED_DIR, normalizeOutputPath } from '@modern-js/utils';
137
import clientGenerator from './utils/clientGenerator';
148
import pluginGenerator from './utils/pluginGenerator';
159
import runtimeGenerator from './utils/runtimeGenerator';

packages/cli/plugin-bff/src/runtime/hono/adapter.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import type { APIHandlerInfo } from '@modern-js/bff-core';
2-
import type { PluginAPI, ServerMiddleware } from '@modern-js/server-core';
2+
import type {
3+
Context,
4+
MiddlewareHandler,
5+
Next,
6+
PluginAPI,
7+
ServerMiddleware,
8+
} from '@modern-js/server-core';
9+
import { Hono } from '@modern-js/server-core';
10+
311
import { isProd } from '@modern-js/utils';
4-
import { Hono } from 'hono';
5-
import type { Context, MiddlewareHandler, Next } from 'hono';
612
import createHonoRoutes from '../../utils/createHonoRoutes';
713

814
const before = ['custom-server-hook', 'custom-server-middleware', 'render'];
@@ -76,7 +82,10 @@ export class HonoAdapter {
7682
before,
7783
handler: async (c: Context, next: Next) => {
7884
if (this.apiServer) {
79-
const response = await this.apiServer.fetch(c.req.raw, c.env);
85+
const response = await this.apiServer.fetch(
86+
c.req as unknown as Request,
87+
c.env,
88+
);
8089

8190
if (response.status !== 404) {
8291
return new Response(response.body, response);

packages/cli/plugin-bff/src/runtime/hono/operators.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { Operator } from '@modern-js/bff-core';
2-
import { useHonoContext } from '@modern-js/server-core';
3-
import type { Context, Next } from 'hono';
2+
import {
3+
type Context,
4+
type Next,
5+
useHonoContext,
6+
} from '@modern-js/server-core';
47

58
export type EndFunction = ((func: (res: Response) => void) => void) &
69
((data: unknown) => void);

packages/cli/plugin-bff/src/utils/createHonoRoutes.ts

+4-55
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { IncomingMessage } from 'node:http';
2-
import { Readable } from 'node:stream';
31
import type { APIHandlerInfo } from '@modern-js/bff-core';
42
import {
53
HttpMetadata,
@@ -8,8 +6,7 @@ import {
86
ValidationError,
97
isWithMetaHandler,
108
} from '@modern-js/bff-core';
11-
import formidable from 'formidable';
12-
import type { Context, Next } from 'hono';
9+
import type { Context, Next } from '@modern-js/server-core';
1310
import typeIs from 'type-is';
1411

1512
type Handler = APIHandlerInfo['handler'];
@@ -67,7 +64,7 @@ export const createHonoHandler = (handler: Handler) => {
6764
if (response) {
6865
return response;
6966
}
70-
if (c.env?.node?.res?.headersSent) return;
67+
if (c.finalized) return;
7168

7269
const result = await handler(input);
7370
return result && typeof result === 'object'
@@ -87,7 +84,7 @@ export const createHonoHandler = (handler: Handler) => {
8784
const args = Object.values(input.params).concat(input);
8885
try {
8986
const body = await handler(...args);
90-
if (c.env?.node?.res?.headersSent) {
87+
if (c.finalized) {
9188
return await Promise.resolve();
9289
}
9390

@@ -118,8 +115,7 @@ const getHonoInput = async (c: Context) => {
118115
if (typeIs.is(contentType, ['application/json'])) {
119116
draft.data = await c.req.json();
120117
} else if (typeIs.is(contentType, ['multipart/form-data'])) {
121-
const nodeStream = await webStreamToNodeStream(c);
122-
draft.formData = await resolveFormData(nodeStream);
118+
draft.formData = await c.req.parseBody();
123119
} else if (typeIs.is(contentType, ['application/x-www-form-urlencoded'])) {
124120
draft.formUrlencoded = await c.req.parseBody();
125121
} else {
@@ -131,51 +127,4 @@ const getHonoInput = async (c: Context) => {
131127
return draft;
132128
};
133129

134-
async function webStreamToNodeStream(c: Context) {
135-
const reader = (c.req.raw.body as ReadableStream<Uint8Array>).getReader();
136-
const nodeStream = new Readable({
137-
async read() {
138-
try {
139-
const { done, value } = await reader.read();
140-
if (done) {
141-
this.push(null);
142-
} else {
143-
this.push(value);
144-
}
145-
} catch (err) {
146-
this.destroy(err as any);
147-
}
148-
},
149-
destroy(err, callback) {
150-
reader.cancel(err).finally(() => callback(err));
151-
},
152-
}) as IncomingMessage;
153-
154-
const headers = {
155-
'content-type': c.req.header('content-type') || 'multipart/form-data',
156-
'content-length': c.req.header('content-length') || '0',
157-
};
158-
159-
nodeStream.headers = headers;
160-
return nodeStream;
161-
}
162-
163-
const resolveFormData = (
164-
request: IncomingMessage,
165-
): Promise<Record<string, any>> => {
166-
const form = formidable({ multiples: true });
167-
return new Promise((resolve, reject) => {
168-
form.parse(request, (err, fields, files) => {
169-
if (err) {
170-
reject(err);
171-
}
172-
173-
resolve({
174-
...fields,
175-
...files,
176-
});
177-
});
178-
});
179-
};
180-
181130
export default createHonoRoutes;

packages/server/core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export { AGGRED_DIR } from './constants';
55
export type { ServerBase, ServerBaseOptions } from './serverBase';
66
export { createServerBase } from './serverBase';
77
export { useHonoContext } from './context';
8-
export { CustomServer } from './plugins/customServer';
8+
export { Hono, type MiddlewareHandler } from 'hono';
99

1010
export type {
1111
Middleware,

packages/server/core/src/serverBase.ts

-8
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,6 @@ export class ServerBase<E extends Env = any> {
186186
return this.app.notFound.bind(this.app);
187187
}
188188

189-
get routes() {
190-
return this.app.routes;
191-
}
192-
193-
get route() {
194-
return this.app.route.bind(this.app);
195-
}
196-
197189
get onError() {
198190
return this.app.onError.bind(this.app);
199191
}

packages/solutions/app-tools/src/plugins/deploy/platforms/netlify-handler.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async function initServer() {
3939
sharedDirectory: p_sharedDirectory,
4040
apiDirectory: p_apiDirectory,
4141
lambdaDirectory: p_lambdaDirectory,
42+
bffRuntimeFramework: p_bffRuntimeFramework,
4243
},
4344
plugins: p_plugins,
4445
...dynamicProdOptions,

packages/solutions/app-tools/src/plugins/deploy/platforms/netlify.ts

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ export const createNetlifyPreset: CreatePreset = (
138138
.replace('p_ROUTE_SPEC_FILE', `"${ROUTE_SPEC_FILE}"`)
139139
.replace('p_dynamicProdOptions', JSON.stringify(dynamicProdOptions))
140140
.replace('p_plugins', pluginsCode)
141+
.replace(
142+
'p_bffRuntimeFramework',
143+
`"${serverAppContext.bffRuntimeFramework}"`,
144+
)
141145
.replace('p_sharedDirectory', serverAppContext.sharedDirectory)
142146
.replace('p_apiDirectory', serverAppContext.apiDirectory)
143147
.replace('p_lambdaDirectory', serverAppContext.lambdaDirectory);

packages/solutions/app-tools/src/plugins/deploy/platforms/node-entry.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ async function main() {
3535
sharedDirectory: p_sharedDirectory,
3636
apiDirectory: p_apiDirectory,
3737
lambdaDirectory: p_lambdaDirectory,
38+
bffRuntimeFramework: p_bffRuntimeFramework,
3839
},
3940
plugins: p_plugins,
4041
...dynamicProdOptions,

packages/solutions/app-tools/src/plugins/deploy/platforms/node.ts

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export const createNodePreset: CreatePreset = (appContext, config) => {
7070
.replace('p_plugins', pluginsCode)
7171
.replace('p_sharedDirectory', serverAppContext.sharedDirectory)
7272
.replace('p_apiDirectory', serverAppContext.apiDirectory)
73+
.replace(
74+
'p_bffRuntimeFramework',
75+
`"${serverAppContext.bffRuntimeFramework}"`,
76+
)
7377
.replace('p_lambdaDirectory', serverAppContext.lambdaDirectory);
7478

7579
if (isEsmProject) {

packages/solutions/app-tools/src/plugins/deploy/platforms/vercel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export const createVercelPreset: CreatePreset = (
102102
});
103103

104104
await fse.writeJSON(path.join(funcsDirectory, '.vc-config.json'), {
105-
runtime: 'nodejs20.x',
105+
runtime: 'nodejs16.x',
106106
handler: 'index.js',
107107
launcherType: 'Nodejs',
108108
shouldAddHelpers: false,

packages/solutions/app-tools/src/plugins/deploy/utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type ServerAppContext = {
88
apiDirectory: string;
99
lambdaDirectory: string;
1010
metaName: string;
11+
bffRuntimeFramework: string;
1112
};
1213

1314
export const serverAppContenxtTemplate = (
@@ -19,6 +20,7 @@ export const serverAppContenxtTemplate = (
1920
apiDirectory,
2021
lambdaDirectory,
2122
metaName,
23+
bffRuntimeFramework,
2224
} = appContext;
2325
return {
2426
sharedDirectory: `path.join(__dirname, "${path.relative(
@@ -34,6 +36,7 @@ export const serverAppContenxtTemplate = (
3436
lambdaDirectory,
3537
)}")`,
3638
metaName,
39+
bffRuntimeFramework: bffRuntimeFramework || 'hono',
3740
};
3841
};
3942

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
import type { IncomingMessage, ServerResponse } from 'node:http';
2-
import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
3-
41
export type NodeRequest = IncomingMessage | Http2ServerRequest;
52
export type NodeResponse = ServerResponse | Http2ServerResponse;

0 commit comments

Comments
 (0)