Skip to content
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): Request.url access error on Bun & Express #2117

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ardatan
Copy link
Owner

@ardatan ardatan commented Feb 28, 2025

Workaround for a potential bug in Bun itself, this might cause issues if the created Proxy's methods are used within another scope/this, but most of cases are fine. So the change is applied only for Bun.

Fixes oven-sh/bun#12368 (comment)
Fixes dotansimha/graphql-yoga#3446

It seems Bun doesn't pass the right receiver in Proxy.get so Reflect.get's receiver doesn't work properly

Summary by CodeRabbit

  • Bug Fixes

    • Resolved a runtime error affecting request handling when using the Express framework on the Bun runtime.
    • Improved compatibility in Bun environments by refining how request data is accessed.
  • Tests

    • Introduced automated tests to confirm correct handling of JSON requests and responses under these conditions.

Copy link

coderabbitai bot commented Feb 28, 2025

Warning

Rate limit exceeded

@ardatan has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 10 minutes and 14 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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.

📥 Commits

Reviewing files that changed from the base of the PR and between d5cef5d and 3b978aa.

📒 Files selected for processing (1)
  • packages/server/test/reproductions.spec.ts (1 hunks)

Walkthrough

This pull request introduces a patch for the @whatwg-node/server package to fix an error related to the Request.url getter when used with the Express framework on the Bun runtime. It also adds a conditional check in the normalizeNodeRequest utility to handle an issue with the Bun environment's proxy behavior. Additionally, a new Jest test case has been added to verify the fix for Bun issue#12368 by ensuring that the request handling returns the expected JSON structure.

Changes

File(s) Change Summary
.changeset/three-shirts-make.md Patch added for @whatwg-node/server to fix the error "The Request.url getter can only be used on instances of Request."
packages/server/src/utils.ts Added a conditional check in normalizeNodeRequest to bypass the faulty Proxy.get behavior when globalThis.Bun is truthy, using Reflect.get to retrieve the property.
packages/server/test/reproductions.spec.ts Introduced a new Jest test case for bun issue#12368, which sends a POST request to an Express endpoint and verifies the JSON response includes the correct request URL and body.

Sequence Diagram(s)

sequenceDiagram
    participant T as Test Case
    participant E as Express App
    participant A as Server Adapter
    participant U as normalizeNodeRequest (Utils)
    participant B as Bun Environment Check

    T->>E: Send POST /my-path with JSON payload
    E->>A: Forward request to adapter
    A->>U: Invoke normalizeNodeRequest
    U->>B: Check if globalThis.Bun is truthy
    B-->>U: Confirm Bun environment detected
    U-->>A: Return property via Reflect.get workaround
    A-->>E: Process normalized request
    E-->>T: Respond with JSON (body and URL)
Loading

Possibly related PRs

Suggested reviewers

  • dotansimha

Poem

Hoppin' through each line of code,
I spot a patch that lightens the load.
With Bun and Express now in tune,
My little paws dance 'neath the moon.
Celebrate the fix with a joyful hop—
Bugs and errors, I bid you drop!
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

github-actions bot commented Feb 28, 2025

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@whatwg-node/promise-helpers 1.2.1-alpha-20250228112106-3b978aa26e4b784701bfa3cbddb050861d5a1d7c npm ↗︎ unpkg ↗︎
@whatwg-node/server 0.9.71-alpha-20250228112106-3b978aa26e4b784701bfa3cbddb050861d5a1d7c npm ↗︎ unpkg ↗︎

Copy link
Contributor

github-actions bot commented Feb 28, 2025

@benchmarks/node-fetch results (consumeBody)

   ✓ active_handles.................: avg=140.332427 min=18     med=140     max=199     p(90)=160     p(95)=163    
     data_received..................: 20 MB  664 kB/s
     data_sent......................: 13 MB  426 kB/s
     http_req_blocked...............: avg=3.38µs     min=621ns  med=1.29µs  max=4.46ms  p(90)=2.01µs  p(95)=2.25µs 
     http_req_connecting............: avg=1.57µs     min=0s     med=0s      max=4.42ms  p(90)=0s      p(95)=0s     
     http_req_duration..............: avg=22.95ms    min=1.73ms med=22.3ms  max=1.17s   p(90)=28.64ms p(95)=30.54ms
       { expected_response:true }...: avg=22.95ms    min=1.73ms med=22.3ms  max=1.17s   p(90)=28.64ms p(95)=30.54ms
     http_req_failed................: 0.00%  ✓ 0           ✗ 130292
     http_req_receiving.............: avg=34.09µs    min=9.09µs med=23.76µs max=19.17ms p(90)=37.82µs p(95)=44.16µs
     http_req_sending...............: avg=10.32µs    min=3.41µs med=6.05µs  max=6.23ms  p(90)=9.75µs  p(95)=13.38µs
     http_req_tls_handshaking.......: avg=0s         min=0s     med=0s      max=0s      p(90)=0s      p(95)=0s     
     http_req_waiting...............: avg=22.9ms     min=1.57ms med=22.27ms max=1.17s   p(90)=28.6ms  p(95)=30.48ms
     http_reqs......................: 130292 4342.515372/s
     iteration_duration.............: avg=46.01ms    min=9.06ms med=44.33ms max=1.19s   p(90)=50.44ms p(95)=56.1ms 
     iterations.....................: 65124  2170.524446/s
     vus............................: 100    min=100       max=100 
     vus_max........................: 100    min=100       max=100 

Copy link
Contributor

github-actions bot commented Feb 28, 2025

@benchmarks/server results (native)

     ✓ no-errors
     ✓ expected-result

   ✓ checks.........................: 100.00% ✓ 235348      ✗ 0     
     data_received..................: 24 MB   788 kB/s
     data_sent......................: 9.4 MB  314 kB/s
     http_req_blocked...............: avg=1.38µs   min=921ns    med=1.18µs   max=176.16µs p(90)=1.87µs   p(95)=2.03µs  
     http_req_connecting............: avg=0ns      min=0s       med=0s       max=115.65µs p(90)=0s       p(95)=0s      
     http_req_duration..............: avg=193.42µs min=141.37µs med=183.58µs max=7.55ms   p(90)=208.26µs p(95)=217.63µs
       { expected_response:true }...: avg=193.42µs min=141.37µs med=183.58µs max=7.55ms   p(90)=208.26µs p(95)=217.63µs
     http_req_failed................: 0.00%   ✓ 0           ✗ 117674
     http_req_receiving.............: avg=25.11µs  min=13.09µs  med=23.35µs  max=3.06ms   p(90)=30.29µs  p(95)=33.02µs 
     http_req_sending...............: avg=6.21µs   min=4.03µs   med=5.35µs   max=845.73µs p(90)=8.02µs   p(95)=8.61µs  
     http_req_tls_handshaking.......: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=162.09µs min=118.13µs med=151.91µs max=7.52ms   p(90)=174.1µs  p(95)=182.58µs
     http_reqs......................: 117674  3922.344396/s
     iteration_duration.............: avg=250.5µs  min=184.17µs med=239.65µs max=7.62ms   p(90)=267.68µs p(95)=279.32µs
     iterations.....................: 117674  3922.344396/s
     vus............................: 1       min=1         max=1   
     vus_max........................: 1       min=1         max=1   

Copy link
Contributor

github-actions bot commented Feb 28, 2025

@benchmarks/node-fetch results (noConsumeBody)

   ✓ active_handles.................: avg=139.879992 min=13      med=141     max=185      p(90)=164     p(95)=168    
     data_received..................: 19 MB  647 kB/s
     data_sent......................: 13 MB  419 kB/s
     http_req_blocked...............: avg=1.97µs     min=611ns   med=1.47µs  max=2.76ms   p(90)=2.24µs  p(95)=2.61µs 
     http_req_connecting............: avg=84ns       min=0s      med=0s      max=514.28µs p(90)=0s      p(95)=0s     
     http_req_duration..............: avg=23.55ms    min=2.57ms  med=22.86ms max=1.25s    p(90)=29.9ms  p(95)=31.46ms
       { expected_response:true }...: avg=23.55ms    min=2.57ms  med=22.86ms max=1.25s    p(90)=29.9ms  p(95)=31.46ms
     http_req_failed................: 0.00%  ✓ 0           ✗ 126943
     http_req_receiving.............: avg=38.33µs    min=11.55µs med=27.68µs max=21.08ms  p(90)=41.75µs p(95)=49.24µs
     http_req_sending...............: avg=11.03µs    min=3.63µs  med=7.39µs  max=8.25ms   p(90)=10.95µs p(95)=15.17µs
     http_req_tls_handshaking.......: avg=0s         min=0s      med=0s      max=0s       p(90)=0s      p(95)=0s     
     http_req_waiting...............: avg=23.5ms     min=2.51ms  med=22.82ms max=1.25s    p(90)=29.85ms p(95)=31.4ms 
     http_reqs......................: 126943 4230.922194/s
     iteration_duration.............: avg=47.22ms    min=10.34ms med=45.61ms max=1.27s    p(90)=50.52ms p(95)=56.59ms
     iterations.....................: 63454  2114.877834/s
     vus............................: 100    min=100       max=100 
     vus_max........................: 100    min=100       max=100 

Copy link
Contributor

github-actions bot commented Feb 28, 2025

@benchmarks/server results (undici)

     ✓ no-errors
     ✓ expected-result

   ✓ checks.........................: 100.00% ✓ 213834    ✗ 0     
     data_received..................: 22 MB   716 kB/s
     data_sent......................: 8.6 MB  285 kB/s
     http_req_blocked...............: avg=1.43µs   min=882ns    med=1.2µs    max=273.27µs p(90)=1.96µs   p(95)=2.16µs  
     http_req_connecting............: avg=1ns      min=0s       med=0s       max=118.97µs p(90)=0s       p(95)=0s      
     http_req_duration..............: avg=216.43µs min=161.53µs med=204.2µs  max=47.63ms  p(90)=230.91µs p(95)=240.59µs
       { expected_response:true }...: avg=216.43µs min=161.53µs med=204.2µs  max=47.63ms  p(90)=230.91µs p(95)=240.59µs
     http_req_failed................: 0.00%   ✓ 0         ✗ 106917
     http_req_receiving.............: avg=25.92µs  min=14.06µs  med=24.17µs  max=2.78ms   p(90)=31.51µs  p(95)=34.17µs 
     http_req_sending...............: avg=6.58µs   min=4.15µs   med=5.79µs   max=435.74µs p(90)=8.39µs   p(95)=9.34µs  
     http_req_tls_handshaking.......: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=183.93µs min=134.76µs med=172.03µs max=47.56ms  p(90)=195.16µs p(95)=204.09µs
     http_reqs......................: 106917  3563.7762/s
     iteration_duration.............: avg=275.92µs min=207.02µs med=262.66µs max=47.78ms  p(90)=293.54µs p(95)=305.66µs
     iterations.....................: 106917  3563.7762/s
     vus............................: 1       min=1       max=1   
     vus_max........................: 1       min=1       max=1   

Copy link
Contributor

github-actions bot commented Feb 28, 2025

@benchmarks/server results (ponyfill)

     ✓ no-errors
     ✓ expected-result

   ✓ checks.........................: 100.00% ✓ 298090      ✗ 0     
     data_received..................: 29 MB   979 kB/s
     data_sent......................: 12 MB   397 kB/s
     http_req_blocked...............: avg=1.34µs   min=822ns    med=1.14µs   max=208.75µs p(90)=1.81µs   p(95)=1.97µs  
     http_req_connecting............: avg=0ns      min=0s       med=0s       max=134.11µs p(90)=0s       p(95)=0s      
     http_req_duration..............: avg=138.96µs min=95.47µs  med=134.44µs max=6.01ms   p(90)=156.75µs p(95)=163.81µs
       { expected_response:true }...: avg=138.96µs min=95.47µs  med=134.44µs max=6.01ms   p(90)=156.75µs p(95)=163.81µs
     http_req_failed................: 0.00%   ✓ 0           ✗ 149045
     http_req_receiving.............: avg=24.29µs  min=12.4µs   med=22.72µs  max=2.97ms   p(90)=30.09µs  p(95)=32.82µs 
     http_req_sending...............: avg=6.2µs    min=3.94µs   med=5.3µs    max=1.12ms   p(90)=8.01µs   p(95)=8.62µs  
     http_req_tls_handshaking.......: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=108.47µs min=69.33µs  med=103.69µs max=5.82ms   p(90)=122.78µs p(95)=128.51µs
     http_reqs......................: 149045  4967.975289/s
     iteration_duration.............: avg=196.86µs min=141.16µs med=191.55µs max=6.51ms   p(90)=217.04µs p(95)=226.24µs
     iterations.....................: 149045  4967.975289/s
     vus............................: 1       min=1         max=1   
     vus_max........................: 1       min=1         max=1   

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/server/test/reproductions.spec.ts (1)

7-8: Add a descriptive comment about the test purpose.

Adding a comment explaining that this test specifically verifies the fix for the Bun issue would improve clarity.

-it('bun issue#12368', async () => {
+/**
+ * This test verifies the fix for Bun issue #12368 where Request.url getter fails
+ * when used with Express due to incorrect Proxy.get receiver handling in Bun.
+ * See: https://github.com/oven-sh/bun/issues/12368
+ */
+it('bun issue#12368', async () => {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b10a486 and d5cef5d.

📒 Files selected for processing (3)
  • .changeset/three-shirts-make.md (1 hunks)
  • packages/server/src/utils.ts (1 hunks)
  • packages/server/test/reproductions.spec.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: unit / node 23
  • GitHub Check: unit / node 22
  • GitHub Check: unit / node 20
  • GitHub Check: unit / node 18
  • GitHub Check: e2e / azure-function
🔇 Additional comments (2)
.changeset/three-shirts-make.md (1)

1-7: LGTM!

The changeset file correctly describes the purpose of the patch for @whatwg-node/server, addressing the specific error that occurs when using Express on Bun.

packages/server/src/utils.ts (1)

176-180: Appropriate workaround for the Bun issue.

The implementation correctly addresses the Bun-specific issue by bypassing the receiver parameter when running in the Bun environment. The comment clearly references the GitHub issue for future reference.

Consider adding a TODO comment indicating that this workaround can be removed once Bun fixes issue #12368, to ensure this code doesn't remain longer than necessary.

-            if (globalThis.Bun) {
-              // workaround for https://github.com/oven-sh/bun/issues/12368
-              // Proxy.get doesn't seem to get `receiver` correctly
-              return Reflect.get(target, prop);
-            }
+            if (globalThis.Bun) {
+              // workaround for https://github.com/oven-sh/bun/issues/12368
+              // Proxy.get doesn't seem to get `receiver` correctly
+              // TODO: Remove this workaround once Bun fixes the issue
+              return Reflect.get(target, prop);
+            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant