Skip to content

Conversation

@enisdenjo
Copy link
Collaborator

@enisdenjo enisdenjo commented Jul 25, 2025

Alternative to and closes #2632

This repo does not have proper benchmarking set up to have detected this regression, but other repo's have - like feTS:

~9% increase in perf benchmarking Hive Gateway, memory consumption feels lower too
~20% increase in perf benchmarking feTS

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 25, 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 14 minutes and 29 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 a373836 and bbeeaa7.

📒 Files selected for processing (5)
  • .changeset/purple-towns-begin.md (1 hunks)
  • packages/node-fetch/src/Body.ts (2 hunks)
  • packages/node-fetch/src/fetchCurl.ts (4 hunks)
  • packages/node-fetch/src/fetchNodeHttp.ts (2 hunks)
  • packages/node-fetch/src/utils.ts (3 hunks)
📝 Walkthrough

Summary by CodeRabbit

  • Refactor
    • Improved internal stream handling for HTTP responses, replacing the previous promise-based pipeline with a more direct piping approach.
    • Enhanced error and abort signal management during stream operations for more robust handling.
    • No changes to the public interface or user-facing APIs.

"""

Walkthrough

This change refactors internal stream handling in the @whatwg-node/node-fetch package. It replaces the use of Node.js's promise-based pipeline and a custom stream wrapper with a direct piping approach using a new pipeThrough utility. The update affects how incoming message streams are processed and piped throughout the codebase.

Changes

File(s) Change Summary
.changeset/purple-towns-begin.md Documents the patch and summarizes the change to direct stream piping, moving away from promise pipelines.
packages/node-fetch/src/Body.ts Replaces custom stream wrapper with direct PassThrough stream and uses pipeThrough for piping IncomingMessage streams.
packages/node-fetch/src/fetchCurl.ts Refactors stream handling: removes custom wrapper, pipes incoming streams via pipeThrough and updates variable names accordingly.
packages/node-fetch/src/fetchNodeHttp.ts Switches from custom wrapper to pipeThrough for piping HTTP response streams through decompression streams.
packages/node-fetch/src/utils.ts Renames and rewrites the stream utility: wrapIncomingMessageWithPassthroughpipeThrough, with a new, more generic signature.

Sequence Diagram(s)

sequenceDiagram
    participant IncomingMessage
    participant PassThrough
    participant pipeThrough
    participant Consumer

    IncomingMessage->>PassThrough: Data stream
    pipeThrough->>PassThrough: Pipe IncomingMessage into PassThrough (with abort/error handling)
    PassThrough->>Consumer: Provides readable stream
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Suggested reviewers

  • ardatan

Poem

Streams now flow with less a hitch,
No promises in the pitch!
Pipes connect with gentle ease,
Errors handled, signals please.
The rabbit hops, the data streams,
Refactored code, fulfilled dreams.
🐇💧
"""

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch perf-regression

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
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests 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.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 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/node-fetch 0.7.23-alpha-20250725224534-bbeeaa7718aa0727bc87514020a30777a0675fe6 npm ↗︎ unpkg ↗︎

@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 2025

@benchmarks/node-fetch results (consumeBody)

   ✓ active_handles.................: avg=140.569525 min=18     med=142     max=185      p(90)=160     p(95)=165    
     data_received..................: 24 MB  785 kB/s
     data_sent......................: 15 MB  503 kB/s
     http_req_blocked...............: avg=1.88µs     min=621ns  med=1.46µs  max=4.16ms   p(90)=2.03µs  p(95)=2.29µs 
     http_req_connecting............: avg=121ns      min=0s     med=0s      max=1.7ms    p(90)=0s      p(95)=0s     
     http_req_duration..............: avg=19.43ms    min=1.83ms med=19.01ms max=862.54ms p(90)=24.89ms p(95)=26.37ms
       { expected_response:true }...: avg=19.43ms    min=1.83ms med=19.01ms max=862.54ms p(90)=24.89ms p(95)=26.37ms
     http_req_failed................: 0.00%  ✓ 0           ✗ 153861
     http_req_receiving.............: avg=34.06µs    min=8.18µs med=23.82µs max=23.16ms  p(90)=38.04µs p(95)=45.53µs
     http_req_sending...............: avg=10.56µs    min=3.22µs med=6.79µs  max=8.14ms   p(90)=9.76µs  p(95)=14.83µs
     http_req_tls_handshaking.......: avg=0s         min=0s     med=0s      max=0s       p(90)=0s      p(95)=0s     
     http_req_waiting...............: avg=19.38ms    min=1.8ms  med=18.97ms max=862.45ms p(90)=24.85ms p(95)=26.31ms
     http_reqs......................: 153861 5127.838421/s
     iteration_duration.............: avg=38.96ms    min=9.64ms med=37.58ms max=881.63ms p(90)=43.16ms p(95)=47.7ms 
     iterations.....................: 76908  2563.169336/s
     vus............................: 80     min=80        max=100 
     vus_max........................: 100    min=100       max=100 

@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 2025

@benchmarks/server results (undici)

     ✓ no-errors
     ✓ expected-result

   ✓ checks.........................: 100.00% ✓ 170886      ✗ 0    
     data_received..................: 17 MB   567 kB/s
     data_sent......................: 13 MB   424 kB/s
     http_req_blocked...............: avg=1.68µs   min=962ns    med=1.64µs   max=214.64µs p(90)=2.03µs   p(95)=2.23µs  
     http_req_connecting............: avg=1ns      min=0s       med=0s       max=142.92µs p(90)=0s       p(95)=0s      
     http_req_duration..............: avg=274.69µs min=202.95µs med=253.69µs max=15.14ms  p(90)=288.62µs p(95)=306.41µs
       { expected_response:true }...: avg=274.69µs min=202.95µs med=253.69µs max=15.14ms  p(90)=288.62µs p(95)=306.41µs
     http_req_failed................: 0.00%   ✓ 0           ✗ 85443
     http_req_receiving.............: avg=26.49µs  min=14.69µs  med=24.86µs  max=2.84ms   p(90)=30.93µs  p(95)=34.19µs 
     http_req_sending...............: avg=9.51µs   min=5.63µs   med=9.52µs   max=338.32µs p(90)=11.6µs   p(95)=13.48µs 
     http_req_tls_handshaking.......: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=238.68µs min=177.11µs med=218.66µs max=15.06ms  p(90)=251.27µs p(95)=267.87µs
     http_reqs......................: 85443   2848.004179/s
     iteration_duration.............: avg=346.48µs min=263.16µs med=324.4µs  max=15.28ms  p(90)=362.65µs p(95)=384.25µs
     iterations.....................: 85443   2848.004179/s
     vus............................: 1       min=1         max=1  
     vus_max........................: 1       min=1         max=1  

@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 2025

@benchmarks/node-fetch results (noConsumeBody)

   ✓ active_handles.................: avg=140.537209 min=46      med=142     max=184      p(90)=161     p(95)=165    
     data_received..................: 24 MB  789 kB/s
     data_sent......................: 15 MB  511 kB/s
     http_req_blocked...............: avg=3.82µs     min=611ns   med=1.55µs  max=25.43ms  p(90)=2.09µs  p(95)=2.43µs 
     http_req_connecting............: avg=1.6µs      min=0s      med=0s      max=4.92ms   p(90)=0s      p(95)=0s     
     http_req_duration..............: avg=19.32ms    min=2.05ms  med=18.85ms max=776.76ms p(90)=25.1ms  p(95)=26.69ms
       { expected_response:true }...: avg=19.32ms    min=2.05ms  med=18.85ms max=776.76ms p(90)=25.1ms  p(95)=26.69ms
     http_req_failed................: 0.00%  ✓ 0           ✗ 154663
     http_req_receiving.............: avg=35.89µs    min=8.49µs  med=25.19µs max=23.55ms  p(90)=39.87µs p(95)=48.21µs
     http_req_sending...............: avg=12.22µs    min=3.2µs   med=7.64µs  max=21.49ms  p(90)=10.86µs p(95)=16.5µs 
     http_req_tls_handshaking.......: avg=0s         min=0s      med=0s      max=0s       p(90)=0s      p(95)=0s     
     http_req_waiting...............: avg=19.27ms    min=1.99ms  med=18.81ms max=776.69ms p(90)=25.06ms p(95)=26.63ms
     http_reqs......................: 154663 5154.945791/s
     iteration_duration.............: avg=38.76ms    min=10.99ms med=37.63ms max=793.36ms p(90)=42.52ms p(95)=47.48ms
     iterations.....................: 77307  2576.656306/s
     vus............................: 100    min=100       max=100 
     vus_max........................: 100    min=100       max=100 

@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 2025

@benchmarks/server results (native)

     ✓ no-errors
     ✓ expected-result

   ✓ checks.........................: 100.00% ✓ 171734      ✗ 0    
     data_received..................: 17 MB   570 kB/s
     data_sent......................: 13 MB   427 kB/s
     http_req_blocked...............: avg=1.67µs   min=942ns    med=1.65µs   max=190.48µs p(90)=2.06µs   p(95)=2.27µs  
     http_req_connecting............: avg=1ns      min=0s       med=0s       max=133.65µs p(90)=0s       p(95)=0s      
     http_req_duration..............: avg=274.58µs min=203.71µs med=254.31µs max=11.88ms  p(90)=288.94µs p(95)=306.28µs
       { expected_response:true }...: avg=274.58µs min=203.71µs med=254.31µs max=11.88ms  p(90)=288.94µs p(95)=306.28µs
     http_req_failed................: 0.00%   ✓ 0           ✗ 85867
     http_req_receiving.............: avg=26.64µs  min=14.52µs  med=25.2µs   max=2.72ms   p(90)=31.37µs  p(95)=34.22µs 
     http_req_sending...............: avg=9.45µs   min=5.72µs   med=9.48µs   max=778.6µs  p(90)=11.52µs  p(95)=13.11µs 
     http_req_tls_handshaking.......: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=238.48µs min=174.41µs med=219.05µs max=11.81ms  p(90)=251.29µs p(95)=267.44µs
     http_reqs......................: 85867   2862.102797/s
     iteration_duration.............: avg=344.81µs min=264.33µs med=323.3µs  max=12.01ms  p(90)=362.17µs p(95)=384.95µs
     iterations.....................: 85867   2862.102797/s
     vus............................: 1       min=1         max=1  
     vus_max........................: 1       min=1         max=1  

@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 2025

@benchmarks/server results (ponyfill)

     ✓ no-errors
     ✓ expected-result

   ✓ checks.........................: 100.00% ✓ 230656      ✗ 0     
     data_received..................: 23 MB   757 kB/s
     data_sent......................: 17 MB   573 kB/s
     http_req_blocked...............: avg=1.57µs   min=952ns    med=1.52µs   max=263.52µs p(90)=1.96µs   p(95)=2.13µs  
     http_req_connecting............: avg=1ns      min=0s       med=0s       max=135.89µs p(90)=0s       p(95)=0s      
     http_req_duration..............: avg=185.26µs min=133.97µs med=178.54µs max=5.65ms   p(90)=201.04µs p(95)=209.88µs
       { expected_response:true }...: avg=185.26µs min=133.97µs med=178.54µs max=5.65ms   p(90)=201.04µs p(95)=209.88µs
     http_req_failed................: 0.00%   ✓ 0           ✗ 115328
     http_req_receiving.............: avg=24.37µs  min=12.8µs   med=23.09µs  max=3ms      p(90)=28.27µs  p(95)=31.01µs 
     http_req_sending...............: avg=9.03µs   min=5.6µs    med=9.21µs   max=287.06µs p(90)=11.11µs  p(95)=12.83µs 
     http_req_tls_handshaking.......: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=151.85µs min=110.02µs med=145.71µs max=5.53ms   p(90)=166.43µs p(95)=174.73µs
     http_reqs......................: 115328  3844.090104/s
     iteration_duration.............: avg=255.42µs min=191.37µs med=247.64µs max=6.16ms   p(90)=272.68µs p(95)=284.88µs
     iterations.....................: 115328  3844.090104/s
     vus............................: 1       min=1         max=1   
     vus_max........................: 1       min=1         max=1   

Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
packages/node-fetch/src/utils.ts (1)

52-120: Successful refactor that achieves PR objectives.

The transformation from wrapIncomingMessageWithPassthrough to pipeThrough successfully eliminates the dependency on Node's promise-based pipeline while maintaining all necessary functionality. The more generic interface increases reusability across the codebase.

Consider adding JSDoc comments to document the function's behavior, especially the error handling semantics and the performance characteristics that motivated this refactor.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 30aa375 and a373836.

📒 Files selected for processing (1)
  • packages/node-fetch/src/utils.ts (3 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: ardatan
PR: ardatan/whatwg-node#0
File: :0-0
Timestamp: 2025-01-29T19:45:47.290Z
Learning: The `handleNodeRequestAndResponse` method in `@whatwg-node/server` can be used to integrate with Fastify by passing both the request and reply objects in the route handler, along with the context containing `req` and `reply`.
Learnt from: ardatan
PR: ardatan/whatwg-node#0
File: :0-0
Timestamp: 2025-01-29T19:45:36.028Z
Learning: The node-fetch package tries different fetch implementations in the following order: 1) node-libcurl 2) undici 3) built-in undici in Node 4) node:http as fallback
🧬 Code Graph Analysis (1)
packages/node-fetch/src/utils.ts (3)
packages/node-fetch/src/ReadableStream.ts (1)
  • pipeThrough (246-262)
packages/server/src/utils.ts (1)
  • signal (663-668)
packages/node-fetch/src/Request.ts (1)
  • signal (113-116)
⏰ 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). (13)
  • GitHub Check: unit / node 24
  • GitHub Check: unit / deno
  • GitHub Check: e2e / azure-function
  • GitHub Check: e2e / aws-lambda
  • GitHub Check: unit / node 20
  • GitHub Check: unit / node 18
  • GitHub Check: unit / bun
  • GitHub Check: alpha / snapshot
  • GitHub Check: server (undici)
  • GitHub Check: server (ponyfill)
  • GitHub Check: server (native)
  • GitHub Check: node-fetch (noConsumeBody)
  • GitHub Check: node-fetch (consumeBody)
🔇 Additional comments (5)
packages/node-fetch/src/utils.ts (5)

2-2: Import changes align well with the refactor.

The transition from specific stream types (IncomingMessage, PassThrough) to generic stream interfaces (Readable, Writable) makes the utility more flexible and reusable.


70-76: Correct error handling for pipe operations.

The manual destruction of the destination stream when the source errors is the correct approach, as Node.js doesn't automatically close the writable stream in error scenarios during pipe operations.


78-97: Robust abort signal handling with proper cleanup.

The implementation correctly uses WeakRef to prevent memory leaks and provides comprehensive cleanup by listening to multiple stream events. The abort handling properly destroys the stream with the custom AbortError.


114-120: Clean implementation of custom AbortError class.

The AbortError class follows standard patterns for custom error classes and aligns with Node.js internal implementations. The default message and name property are appropriate.


52-100: Approve pipeThrough implementation; please benchmark against Node’s built-ins

The pipeThrough refactoring is sound: it gives you explicit error- and abort-signal control, and the use of WeakRef for cleanup is a solid memory-leak safeguard. We’ve confirmed that Node.js exports both addAbortSignal (from node:stream) and finished (from node:stream/promises) as functions, so your manual implementations are valid fallbacks.

To substantiate the “faster than Node’s built-in utilities” claim, please add a brief benchmark comparing:

  • manual abort handling (current onAbort/cleanup)
  • addAbortSignal(signal, src) + finished(src, cleanup)

This will ensure the performance assertion is backed by data before merging.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants