Skip to content

refactor(runner): use node:http to fetch worker#4004

Merged
pi0 merged 2 commits into
mainfrom
refactor/http-fetch
Feb 6, 2026
Merged

refactor(runner): use node:http to fetch worker#4004
pi0 merged 2 commits into
mainfrom
refactor/http-fetch

Conversation

@pi0
Copy link
Copy Markdown
Member

@pi0 pi0 commented Feb 6, 2026

This PR migrates from using undici to directly node:http to fetch the dev worker's socket.

Improves stability (hopefully for windows), speed and also reduces install size (-1.46MB)

@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
nitro.build Ready Ready Preview, Comment Feb 6, 2026 9:51am

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 6, 2026

📝 Walkthrough

Walkthrough

Removes the undici dependency, refactors src/runner/proxy.ts to use Node's http.request for TCP and Unix socket requests with manual body/stream handling and header filtering, and adds comprehensive unit tests exercising TCP and Unix socket behaviors.

Changes

Cohort / File(s) Summary
Dependency removal
package.json
Removed the undici package from dependencies and its resolutions entry.
HTTP request refactor
src/runner/proxy.ts
Replaced fetch-based logic with Node's http.request. Builds RequestOptions for socketPath vs host/port, streams request bodies (including ReadableStream), converts IncomingMessage to a Web ReadableStream response, strips transfer-encoding/keep-alive headers, preserves multi-valued headers, and removed Deno/Bun branches.
Tests added
test/unit/proxy.test.ts
Added extensive unit tests: TCP server routes (/json, /echo, /headers, /redirect, /multi-cookie, /no-content), Unix socket server, and tests for JSON parsing, POST streaming, header forwarding, manual redirect behavior, 204 handling, multi Set-Cookie preservation, header stripping, Request input, query strings, socket access, and connection error handling.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits format with 'refactor' type and accurately describes the main change: replacing undici with node:http for worker fetching.
Description check ✅ Passed The description clearly explains the migration from undici to node:http and mentions the benefits (stability, speed, reduced install size), directly relating to the changeset.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/http-fetch

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.

Copy link
Copy Markdown
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: 1

🤖 Fix all issues with AI agents
In `@test/unit/proxy.test.ts`:
- Around line 109-117: The test "forwards custom headers" uses res.json() whose
return type is unknown; update the body declaration in that test (the one using
fetchAddress and res.json()) to include a type assertion, e.g. assert the result
as an object with headers (for example: as { headers: Record<string, string> }),
so accessing body.headers is type-safe; locate the test block referencing
fetchAddress and change the body assignment to use this type assertion.
🧹 Nitpick comments (3)
src/runner/proxy.ts (1)

109-115: pipe() won't propagate source-stream errors — consider pipeline(); also, req.end(body) only accepts string | Buffer | Uint8Array.

Two edge-case concerns with the body handling:

  1. If the ReadableStream emits an error, pipe() won't forward it to req, so the wrapping Promise may hang (no resolve or reject). Using pipeline() from node:stream/promises or manually wiring .on("error") on the readable side would make this more resilient.

  2. req.end(body) on line 112 only accepts string | Buffer | Uint8Array, but BodyInit also includes Blob, ArrayBuffer, FormData, and URLSearchParams. If any of these are ever passed, it would silently produce incorrect output or throw.

Both are unlikely in the current dev-proxy context, so just flagging for awareness.

🔧 Optional: safer streaming with pipeline()
-import { Readable } from "node:stream";
+import { Readable, pipeline } from "node:stream";
     if (init!.body instanceof ReadableStream) {
-      Readable.fromWeb(init!.body as import("node:stream/web").ReadableStream).pipe(req);
+      const nodeBody = Readable.fromWeb(init!.body as import("node:stream/web").ReadableStream);
+      pipeline(nodeBody, req, (err) => {
+        if (err) reject(err);
+      });
     } else if (init!.body) {
test/unit/proxy.test.ts (2)

1-7: Use pathe instead of node:path per project coding guidelines.

Line 3 imports join from node:path. The usage is for a Unix socket path, which is platform-specific anyway, but the guideline applies to all *.ts files.

♻️ Suggested fix
-import { join } from "node:path";
+import { join } from "pathe";

As per coding guidelines: **/*.{js,ts}: Use pathe for cross-platform path operations instead of node:path.


84-86: Socket file not cleaned up after tests.

socketServer.close() stops listening but the .sock file may remain on disk in tmpdir(). Consider unlinking it in afterAll for clean test runs.

♻️ Optional cleanup
+import { unlinkSync } from "node:fs";
 afterAll(() => {
   socketServer?.close();
+  try { unlinkSync(socketPath); } catch {}
 });

Comment thread test/unit/proxy.test.ts
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Feb 6, 2026

Open in StackBlitz

npm i https://pkg.pr.new/nitrojs/nitro@4004

commit: aabb9b0

@pi0 pi0 merged commit db30877 into main Feb 6, 2026
12 checks passed
@pi0 pi0 deleted the refactor/http-fetch branch February 6, 2026 11:19
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