Skip to content

Conversation

@Dipper30
Copy link
Contributor

Background

Previously, app.httpclient was initialized lazily. However, certain plugins might access the instance earlier than expected—potentially before the final configuration is fully composed. To address this, a Proxy wrapper was introduced so that the actual HTTP client is only instantiated when its properties or methods are actually accessed.

Issue

This Proxy implementation broke compatibility with egg-mock. Specifically, mocking via:

mm(app.httpclient, 'request', async () => ({ status: 500 }))

no longer works as expected due to improper handling in the Proxy’s traps (e.g., missing or incorrect get/set behavior).

Solution

Refactor the Proxy to be more robust and compliant with standard object semantics, ensuring it correctly supports property access, method calls, and mocking libraries like egg-mock.

Impact

Only the httpclient_next initialization logic in egg.js and utils.js is affected. Existing behavior remains unchanged for all other use cases.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 13, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Dipper30, 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 resolves a critical compatibility issue where the httpclient_next lazy initialization, implemented using a JavaScript Proxy, interfered with egg-mock's ability to mock app.httpclient methods. The solution involves introducing a new createTransparentProxy utility that provides a more robust and compliant proxy implementation, ensuring that defineProperty-based monkeypatching, as used by egg-mock, functions correctly. This change streamlines the app.httpclient setup and enhances the testability of applications using egg-mock without impacting existing runtime behavior.

Highlights

  • New createTransparentProxy Utility: Introduced a new utility function createTransparentProxy in lib/core/utils.js. This function creates a robust proxy that lazily initializes the real object and is designed to be compatible with monkeypatching libraries like egg-mock by correctly handling defineProperty-based overrides and various proxy traps.
  • Refactored app.httpclient Initialization: The app.httpclient initialization logic in lib/egg.js has been updated to utilize the new createTransparentProxy function. This change resolves the compatibility issue with egg-mock while maintaining the lazy initialization behavior.
  • Enhanced Test Coverage for Proxy Functionality: Comprehensive unit tests have been added for createTransparentProxy in test/lib/core/utils.test.js to ensure its correct behavior across various scenarios, including egg-mock compatibility, method binding, property merging, and error handling. Existing tests in test/lib/core/httpclient.test.js were updated to include a specific egg-mock test case and remove redundant proxy trap validations.
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 introduces a well-designed createTransparentProxy utility to resolve compatibility issues with egg-mock when using a lazily-initialized httpclient. The implementation correctly isolates mocked properties on an overlay object, and the refactoring in egg.js simplifies the code by adopting this new utility. The accompanying tests are thorough and cover a wide range of scenarios, ensuring the new proxy is robust. I have one suggestion to improve the set trap's correctness in the new proxy implementation.

@codecov
Copy link

codecov bot commented Jan 13, 2026

Codecov Report

❌ Patch coverage is 98.31933% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.50%. Comparing base (a1169f3) to head (7cda241).
⚠️ Report is 3 commits behind head on 3.x.

Files with missing lines Patch % Lines
lib/core/utils.js 98.27% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              3.x    #5768      +/-   ##
==========================================
- Coverage   99.54%   99.50%   -0.05%     
==========================================
  Files          36       36              
  Lines        3722     3809      +87     
  Branches      562      582      +20     
==========================================
+ Hits         3705     3790      +85     
- Misses         17       19       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@killagu killagu left a comment

Choose a reason for hiding this comment

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

LGTM

@killagu killagu merged commit 04cfa1b into eggjs:3.x Jan 16, 2026
25 of 28 checks passed
fengmk2 pushed a commit that referenced this pull request Jan 16, 2026
[skip ci]

## <small>3.33.1 (2026-01-16)</small>

* fix: egg-mock for httpclient_next proxy (#5768) ([04cfa1b](04cfa1b)), closes [#5768](#5768)
@github-actions
Copy link

🎉 This PR is included in version 3.33.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@fengmk2 fengmk2 added the pick-to-v4 pull request need to pick to egg v4 label Jan 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pick-to-v4 pull request need to pick to egg v4 released on @3.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants