Skip to content

refactor(js): replace reflection server express with hono#4851

Draft
Ehesp wants to merge 3 commits intofirebase:mainfrom
Ehesp:js-express-to-hono
Draft

refactor(js): replace reflection server express with hono#4851
Ehesp wants to merge 3 commits intofirebase:mainfrom
Ehesp:js-express-to-hono

Conversation

@Ehesp
Copy link
Member

@Ehesp Ehesp commented Feb 26, 2026

This PR replaces the reflection server using express with hono.

Express is mainly designed for Node, and whilst restricted runtime support exists, express and its deps are still bundled with genkit, causing issues in those environments; e.g.

#4815

Hono is much lighter, less dependencies and works in all JS based runtimes.

Also added a test suite for the reflection server. Need to do more manual testing.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Ehesp, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the reflection server by replacing the Express.js framework with Hono. This change was driven by the need for a lighter, more runtime-agnostic solution, as Express.js presented compatibility challenges in non-Node.js environments. The migration involved updating core dependencies, rewriting server-side logic to leverage Hono's features, and introducing a new test suite to ensure the stability and correctness of the refactored server.

Highlights

  • Framework Migration: The reflection server has been migrated from using Express.js to Hono, a lightweight and versatile web framework.
  • Dependency Updates: Express.js, its associated middleware (cors), and type definitions have been removed, while Hono and @hono/node-server have been added as new dependencies. The TypeScript version has also been updated to 5.8.3.
  • Server Logic Refactoring: The core server logic, including route handling, request/response processing, and error management, has been rewritten to align with Hono's API and context-based approach.
  • New Test Suite: A comprehensive test suite has been added for the reflection server, covering health checks, action listing, action execution, and action cancellation.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • js/core/package.json
    • Removed 'express', 'cors', and '@types/express' dependencies.
    • Added '@hono/node-server' and 'hono' dependencies.
    • Updated 'typescript' to version 5.8.3.
  • js/core/src/reflection.ts
    • Replaced Express imports with Hono-related imports.
    • Introduced a new utility function 'parseBodyLimitToBytes' for body size parsing.
    • Refactored server initialization and all API route handlers to use Hono's application and context objects.
    • Adjusted response handling, including header flushing and error responses, to be compatible with Hono.
    • Modified the server instance type from Express to a generic HTTP server.
  • js/core/tests/reflection_test.ts
    • Added a new test file containing a suite of tests for the ReflectionServer.
    • Implemented tests for '/api/__health', '/api/actions', '/api/runAction', '/api/cancelAction', and '/api/envs' endpoints.
  • js/pnpm-lock.yaml
    • Updated the pnpm-lock.yaml file to reflect changes in dependencies, including the addition of Hono packages and removal of Express packages.
    • Updated the TypeScript version entry.
    • Added deprecated warnings to several 'glob' and 'tar' package entries.
Activity
  • No specific activity (comments, reviews, progress updates) was provided in the context for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a solid refactoring that replaces the Express.js-based reflection server with Hono. This is a commendable move towards a lighter, more performant, and runtime-agnostic implementation. The code changes are well-executed, and the introduction of a test suite for the reflection server significantly improves the robustness of this critical component. I've identified a couple of opportunities to enhance error handling consistency and align with standard HTTP practices, which should further strengthen the API.

try {
body = await c.req.json();
} catch {
return c.text('OK', 200);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Inconsistent error handling for invalid JSON body. This endpoint returns a 200 OK response when JSON parsing fails, which can hide issues from the client. Other POST endpoints in this file, like /api/runAction and /api/cancelAction, correctly return a 400 error. For consistency and proper error reporting, this should also return a 400 error with an informative message.

Suggested change
return c.text('OK', 200);
return c.json({ error: 'Invalid JSON body' }, 400);

Comment on lines +431 to 442
app.onError((err, c) => {
logger.error(err.stack);
const error = err as Error;
const { message, stack } = error;
const errorResponse: Status = {
code: StatusCodes.INTERNAL,
message,
details: {
stack,
},
message: err.message,
details: { stack: err.stack },
};

// Headers may have been sent already (via onTraceStart), so check before setting status
res.status(200).end(JSON.stringify({ error: errorResponse }));
return (c as { json: (body: unknown, status?: number) => Response }).json(
{ error: errorResponse },
200
);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This error handler returns a 200 OK status code for what appears to be an internal server error. This is unconventional and can be misleading for clients. Most other error paths in this file correctly return 4xx or 5xx status codes. It would be better to use a 500 Internal Server Error status code here to align with standard HTTP practices and improve API consistency.

Additionally, the type assertion for c on line 438 seems unnecessary. The Context object from Hono provides a json method that should work without casting, which would improve code clarity.

Suggested change
app.onError((err, c) => {
logger.error(err.stack);
const error = err as Error;
const { message, stack } = error;
const errorResponse: Status = {
code: StatusCodes.INTERNAL,
message,
details: {
stack,
},
message: err.message,
details: { stack: err.stack },
};
// Headers may have been sent already (via onTraceStart), so check before setting status
res.status(200).end(JSON.stringify({ error: errorResponse }));
return (c as { json: (body: unknown, status?: number) => Response }).json(
{ error: errorResponse },
200
);
});
app.onError((err, c) => {
logger.error(err.stack);
const errorResponse: Status = {
code: StatusCodes.INTERNAL,
message: err.message,
details: { stack: err.stack },
};
return c.json(
{ error: errorResponse },
500
);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant