Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/cli/plugin-bff/src/runtime/hono/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export class HonoAdapter {
if (result instanceof Response) {
return result;
}
} else {
logger.error(err);
}
} catch (configError) {
logger.error(`Error in serverConfig.onError handler: ${configError}`);
Expand Down Expand Up @@ -104,11 +106,7 @@ export class HonoAdapter {
before,
handler: async (c: Context, next: Next) => {
if (this.apiServer) {
const response = await this.apiServer.fetch(
c.req as unknown as Request,
c.env,
);

const response = await this.apiServer.fetch(c.req.raw, c.env);
if (response.status !== 404) {
return new Response(response.body, response);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/plugin-bff/src/utils/createHonoRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const handleResponseMeta = (c: Context, handler: Handler) => {
case ResponseMetaType.Redirect:
return c.redirect(meta.value as string);
case ResponseMetaType.StatusCode:
c.status(meta.value as number);
c.status(meta.value as any);
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ Developers can use middleware by configuring middlewares in `server/modern.serve
import {
type MiddlewareHandler,
defineServerConfig,
getCookie
} from '@modern-js/server-runtime';

const requireAuthForApi: MiddlewareHandler = async (c, next) => {
if (c.req.path.startsWith('/api') && c.req.path !== '/api/login') {
const sid = c.req.cookie('sid');
const sid = getCookie(c, 'sid');
if (!sid) {
return c.json({ code: -1, message: 'need login' }, 400);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ type MiddlewareContext = {
Differences between Middleware `Context` and Hono `Context`:
| UnstableMiddleware | Hono | Description |
| :----------------------- | :---------------------------- | :--------------------------------------------------------------------------- |
| `c.request.cookie` | `c.req.cookie()` | Refer to [Hono Cookie Helper](https://hono.dev/docs/helpers/cookie) documentation |
| `c.request.cookie` | `getCookie()` | Refer to [Hono Cookie Helper](https://hono.dev/docs/helpers/cookie) documentation |
| `c.request.pathname` | `c.req.path` | Refer to [HonoRequest path](https://hono.dev/docs/api/request#path) documentation |
| `c.request.url` | - | Hono `c.req.url` provides the full request URL, calculate manually from URL |
| `c.request.host` | `c.req.header('Host')` | Obtain host through header |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ Modern.js 支持用户通过 [Middleware](/guides/advanced-features/web-server.h
import {
type MiddlewareHandler,
defineServerConfig,
getCookie
} from '@modern-js/server-runtime';

const requireAuthForApi: MiddlewareHandler = async (c, next) => {
if (c.req.path.startsWith('/api') && c.req.path !== '/api/login') {
const sid = c.req.cookie('sid');
const sid = getCookie(c, 'sid');
if (!sid) {
return c.json({ code: -1, message: 'need login' }, 400);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ Middleware `Context` 和 Hono `Context` 的具体差异:

| UnstableMiddleware | Hono | 说明 |
| :------------------- | :--------------------- | :----------------------------------------------------------------------- |
| `c.request.cookie` | `c.req.cookie()` | 参考 [Hono Cookie Helper](https://hono.dev/docs/helpers/cookie) 文档 |
| `c.request.cookie` | `getCookie()` | 参考 [Hono Cookie Helper](https://hono.dev/docs/helpers/cookie) 文档 |
| `c.request.pathname` | `c.req.path` | 参考 [HonoRequest path](https://hono.dev/docs/api/request#path) 文档 |
| `c.request.url` | - | Hono `c.req.url` 为完整请求路径,自行通过 url 计算 |
| `c.request.host` | `c.req.header('Host')` | 通过 header 获取 host |
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/plugin-i18n/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getLanguageFromPath = (
urlPath: string,
languages: string[],
): string | null => {
const url = new URL(req.url, `http://${req.headers.host}`);
const url = new URL(req.url, `http://${req.header().host}`);
const pathname = url.pathname;

// Remove urlPath prefix to get remaining path
Expand Down
16 changes: 15 additions & 1 deletion packages/server/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
"jsnext:source": "./src/adapters/node/index.ts",
"import": "./dist/esm-node/adapters/node/index.js",
"default": "./dist/cjs/adapters/node/index.js"
},
"./hono": {
"types": "./dist/types/hono.d.ts",
"jsnext:source": "./src/hono.ts",
"import": "./dist/esm-node/hono.js",
"default": "./dist/cjs/hono.js"
}
},
"typesVersions": {
Expand All @@ -41,6 +47,9 @@
],
"node": [
"./dist/types/adapters/node/index.d.ts"
],
"hono": [
"./dist/types/hono.d.ts"
]
}
},
Expand All @@ -64,7 +73,7 @@
"@web-std/stream": "^1.0.3",
"cloneable-readable": "^3.0.0",
"flatted": "^3.3.3",
"hono": "^3.12.2",
"hono": "^4.10.4",
"ts-deepmerge": "7.0.3"
},
"devDependencies": {
Expand Down Expand Up @@ -95,6 +104,11 @@
"types": "./dist/types/adapters/node/index.d.ts",
"import": "./dist/esm-node/adapters/node/index.js",
"default": "./dist/cjs/adapters/node/index.js"
},
"./hono": {
"types": "./dist/types/hono.d.ts",
"import": "./dist/esm-node/hono.js",
"default": "./dist/cjs/hono.js"
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/server/core/src/adapters/node/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const httpCallBack2HonoMid = (handler: Handler) => {
}

if (isResFinalized(res)) {
// Fix: ensure context.res is initialized before setting finalized
// This prevents c.#res from being undefined in Hono
const _ = context.res;
context.finalized = true;
} else {
await next();
Expand Down Expand Up @@ -92,6 +95,9 @@ export const connectMockMid2HonoMid = (
// The function lenth < 3 means the handler is not a function with next
if (handler.length < 3) {
res.once('finish', () => {
// Fix: ensure context.res is initialized before setting finalized
// This prevents c.#res from being undefined in Hono
const _ = context.res;
context.finalized = true;
resolve();
});
Expand Down
17 changes: 17 additions & 0 deletions packages/server/core/src/hono.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// === Export Hono.js Core Types and APIs ===

// Core types from Hono
export type {
Context,
Next,
MiddlewareHandler,
MiddlewareHandler as Middleware,
HonoRequest,
} from 'hono';

// Hono utilities
export {
setCookie,
getCookie,
deleteCookie,
} from 'hono/cookie';
3 changes: 2 additions & 1 deletion packages/server/core/src/plugins/render/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ServerRoute } from '@modern-js/types';
import type { ContentfulStatusCode } from 'hono/utils/http-status';
import type { ServerNodeEnv } from '../../adapters/node/hono';
import { getLoaderCtx } from '../../helper';
import type {
Expand Down Expand Up @@ -114,6 +115,6 @@ function createRenderHandler(
headersData[k] = v;
});

return c.body(body, status, headersData);
return c.body(body!, status as ContentfulStatusCode, headersData);
};
}
2 changes: 2 additions & 0 deletions packages/server/server-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export {
type ServerConfig,
} from '@modern-js/server-core';

export * from '@modern-js/server-core/hono';

export type {
Container,
CacheControl,
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/integration/bff-hono/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@modern-js/plugin-bff": "workspace:*",
"@modern-js/runtime": "workspace:*",
"@modern-js/server-runtime": "workspace:*",
"hono": "^3.12.2",
"hono": "^4.10.4",
"react": "^19.2.0",
"react-dom": "^19.2.0",
"ts-node": "^10.9.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const timing: MiddlewareHandler = async (c, next) => {
const text = await res.text();

const newText = text.replace('<body>', '<body>bytedance');
c.res = c.body(newText, {

c.res = new Response(newText, {
status: res.status,
headers: res.headers,
});
Expand Down