-
-
Notifications
You must be signed in to change notification settings - Fork 67
fix(server): restore original request.emit method in Node Body Limit Plugin #863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(server): restore original request.emit method in Node Body Limit Plugin #863
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. Warning Rate limit exceeded@unnoq has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 15 minutes and 58 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughImplements a one-time header-length check using once(), refactors request emit wrapping with bound emitters, tracks body size per data chunk, awaits the next handler with a try/finally to restore the original emit, and imports once from @orpc/shared. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant NodeServer as Node Server
participant Plugin as BodyLimitPlugin
participant Next as NextHandler
Client->>NodeServer: HTTP Request
NodeServer->>Plugin: Initialize plugin for request
Plugin->>Plugin: Wrap request.emit (bindedEmit)
Note over Plugin: once(): header-length check on first 'data'
Plugin->>Next: await next(options)
loop on 'data' events
NodeServer->>Plugin: 'data' chunk
Plugin->>Plugin: Check headers once, track currentBodySize
Plugin-->>NodeServer: Delegate to bindedEmit
end
Plugin->>Plugin: finally restore original emit
Next-->>NodeServer: Response
NodeServer-->>Client: Response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~15 minutes Possibly related PRs
Poem
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @unnoq, 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 addresses a potential issue in the Node Body Limit Plugin by ensuring that the request.emit
method is properly restored to its original state after the plugin's logic has executed. This prevents unintended side effects or interference with other parts of the application that might rely on the standard behavior of request.emit
. Additionally, it optimizes the header checking mechanism by using a once
utility to ensure the content-length validation is performed only once per request, improving efficiency and correctness.
Highlights
request.emit
restoration: Therequest.emit
method is now restored to its original implementation after theBodyLimitPlugin
has processed the request, preventing potential side effects from the overridden method.checkHeader
optimization: ThecheckHeader
function, which validates thecontent-length
header against themaxBodySize
, now uses theonce
utility to ensure it's executed only a single time.originalEmit
binding: TheoriginalEmit
method is now explicitly bound tooptions.request
to ensure correct context when called, addressing potentialthis
binding issues.- Removal of
isHeaderChecked
flag: TheisHeaderChecked
flag has been removed, as its functionality is now handled by theonce
utility.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
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 issue 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
-
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. ↩
There was a problem hiding this 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 fixes an important bug by ensuring the monkey-patched request.emit
method in the BodyLimitPlugin
is restored after use. This prevents potential side-effects. The refactoring to use the once
utility is also a nice improvement for code clarity. I have one suggestion to further improve the robustness of the fix by using a try...finally
block and correcting the call to the next interceptor.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
More templates
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/experimental-durable-event-iterator
@orpc/hey-api
@orpc/json-schema
@orpc/json-schema-typed
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/otel
@orpc/react
@orpc/react-query
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/server/src/adapters/node/body-limit-plugin.ts (2)
36-37
: Consider adding defensive null check for request.emitWhile unlikely in practice, it would be safer to verify that
request.emit
exists before storing and manipulating it.+ if (!options.request.emit || typeof options.request.emit !== 'function') { + return await options.next(options) + } + const originalEmit = options.request.emit const bindedEmit = originalEmit.bind(options.request)
55-59
: Minor: Fix ESLint brace style warningThe static analysis tool flagged a brace style issue where the closing brace appears on the same line as the subsequent block.
try { return await options.next(options) - } finally { + } + finally { options.request.emit = originalEmit }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/server/src/adapters/node/body-limit-plugin.ts
(3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/server/src/adapters/node/body-limit-plugin.ts (1)
packages/shared/src/function.ts (1)
once
(3-16)
🪛 ESLint
packages/server/src/adapters/node/body-limit-plugin.ts
[error] 57-57: Closing curly brace appears on the same line as the subsequent block.
(style/brace-style)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: publish-commit
🔇 Additional comments (2)
packages/server/src/adapters/node/body-limit-plugin.ts (2)
29-60
: Well-implemented fix that properly restores the original emit method!The refactored implementation correctly addresses the issue by:
- Storing the original
emit
method reference- Creating a bound version for safe delegation
- Using
try...finally
to guarantee restoration even if errors occur- Properly passing
options
tonext()
This ensures the original emit method is always restored, preventing potential issues with downstream handlers.
30-34
: Elegant use ofonce()
for header validationThe replacement of the
isHeaderChecked
flag with theonce()
utility function is a cleaner, more functional approach that ensures the header check runs exactly once.
Summary by CodeRabbit
Bug Fixes
Refactor