Skip to content

feat: add requestWith*URL utils and use for faster mounts#1342

Merged
pi0 merged 2 commits intomainfrom
feat/req-base
Mar 16, 2026
Merged

feat: add requestWith*URL utils and use for faster mounts#1342
pi0 merged 2 commits intomainfrom
feat/req-base

Conversation

@pi0
Copy link
Member

@pi0 pi0 commented Mar 16, 2026

Summary

  • Add requestWithURL(req, url) — creates a lightweight Proxy over a Request that overrides only the url property, avoiding new Request() allocation
  • Add requestWithBaseURL(req, base) — strips a base path prefix from the request URL using requestWithURL
  • Uses a shared ProxyHandler with a WeakMap for per-proxy URL storage to minimize allocations
  • H3.mount() now uses requestWithBaseURL instead of new Request(url, event.req)
  • Both utils are exported as public API

Test plan

  • Unit tests for requestWithURL (url override, method/headers preserved, instanceof Request, clone())
  • Unit tests for requestWithBaseURL (base stripping, root fallback, query string preservation)
  • Existing mount.test.ts passes with the refactored implementation

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added two new lightweight request utilities for URL manipulation: one for direct URL overrides and another for base path adjustments, both designed to avoid unnecessary request cloning.
  • Documentation

    • Added documentation for the new request utilities.
  • Tests

    • Added comprehensive unit tests covering the new request utilities.

pi0 and others added 2 commits March 16, 2026 11:15
Lightweight request proxying via `Proxy` to override the URL without
cloning the entire `Request` object. Uses a shared proxy handler with
a `WeakMap` for per-proxy URL storage to minimize allocations.

`requestWithBaseURL` strips a base path prefix from the request URL
and is now used internally by `H3.mount()` instead of `new Request()`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link

coderabbitai bot commented Mar 16, 2026

📝 Walkthrough

Walkthrough

This pull request introduces two new utility functions—requestWithURL and requestWithBaseURL—that create lightweight request proxies without cloning the original request object. The changes include the core implementation, integration into the h3 middleware routing, public API exports, comprehensive unit tests, and documentation.

Changes

Cohort / File(s) Summary
Core Implementation
src/utils/request.ts
Adds requestWithURL and requestWithBaseURL functions using a proxy-based mechanism to override request URLs without cloning; includes URL override map and proxy handler logic.
Integration & Exports
src/h3.ts, src/index.ts
Refactors mounted middleware to use requestWithBaseURL instead of creating a new Request; exports both new functions from the main entry point.
Testing & Documentation
test/unit/request.test.ts, test/unit/package.test.ts, docs/2.utils/1.request.md
Adds 77 lines of unit tests covering URL override behavior, pathname stripping, and proxy cloning; updates export snapshot; documents the new utilities.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Two lightweight proxies hop into the fray,
No cloning needed—just URLs at play,
Base paths stripped with elegant grace,
Light as a carrot, quick as a race! 🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title accurately describes the main change: adding two new request URL utilities (requestWithURL and requestWithBaseURL) and using them to optimize mounting logic.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/req-base
📝 Coding Plan
  • Generate coding plan for human review comments

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.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 16, 2026

Open in StackBlitz

npm i https://pkg.pr.new/h3@1342

commit: 24b9271

@pi0 pi0 changed the title feat(utils): add requestWithURL and requestWithBaseURL feat: add requestWith*URL utils and use for faster mount Mar 16, 2026
@pi0 pi0 changed the title feat: add requestWith*URL utils and use for faster mount feat: add requestWith*URL utils and use for faster mounts Mar 16, 2026
@pi0 pi0 merged commit d1da262 into main Mar 16, 2026
7 checks passed
@pi0 pi0 deleted the feat/req-base branch March 16, 2026 10:24
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.

1 participant