fix(event): keep event.context and req.context as one reference#1499
Merged
Conversation
The H3Event constructor resolved `context` from `context || req.context || new EmptyObject()` but only assigned the result to `event.context`, never writing it back to `req.context`. The two objects therefore diverged whenever an explicit context was passed or `req.context` was unset (the default path, since srvx does not populate `req.context`). This silently broke `getRequestIP`, which reads `event.req.context?.clientAddress`: a value written to the documented surface `event.context.clientAddress` (H3EventContext, where the field is declared) never reached the reader and the call fell through to `event.req.ip`. Assign the resolved context back to `req.context` so `event.context === event.req.context` holds across all construction paths. Co-Authored-By: Claude Opus 4.8 <[email protected]>
📝 WalkthroughWalkthrough
ChangesContext synchronization
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
pi0
approved these changes
Jul 22, 2026
event.context and req.context as one reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
getRequestIPsilently ignoresevent.context.clientAddress. Writing to the documented type surface —clientAddressis declared onH3EventContext(src/types/context.ts:27) — never reaches the reader atsrc/utils/request.ts:542, which readsevent.req.context?.clientAddress. The call falls through toevent.req.ipinstead.Root cause
The invariant
event.context === event.req.contextis supposed to hold, but the constructor never established it. Two adjacent PRs conspired:req.contextandServerRequestContexttypes #1194 changed the ctor tothis.context = context || req.context || new EmptyObject()— readingreq.contextas a fallback source, but never writing the resolved value back toreq.context.HTTPEventfor more agnostic usage #1195 then moved thegetRequestIPreader fromevent.context.clientAddressontoevent.req.context?.clientAddress, assuming the two were always the same object.They only coincide when the runtime pre-populated
req.context— and srvx does not do so by default, so the common path diverges. The?.masked the breakage as a silentundefined.Fix
Assign the resolved context back to
req.contextso both point at one object across all three construction paths (explicitcontext, inheritedreq.context, fresh fallback):Tests
Added regression tests in
test/unit/event.test.ts:event.context === event.req.contextwhen neither is providedcontextis passedreq.contextis reusedgetRequestIPobservesclientAddresswritten toevent.contextAll three failing-before assertions pass after the fix; full suite (1581 tests) + lint/typecheck green.
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests