Skip to content

refactor(task): use http.request - #3268

Merged
pi0 merged 2 commits into
nitrojs:mainfrom
kricsleo:fix/dev-fetch
Dec 16, 2025
Merged

pi0 merged 2 commits into
nitrojs:mainfrom
kricsleo:fix/dev-fetch

Conversation

@kricsleo

@kricsleo kricsleo commented Apr 1, 2025

Copy link
Copy Markdown
Member

🔗 Linked issue

resolves #3264

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Since ofetch doesn't support socketPath, this PR directly uses http.request.
The implementation is pretty simple and only handles JSON responses, but that should be enough since the task's response is required to be JSON.

Otherwise, we'd need to use undici's Agent for socketPath support, which would require adding undici as a dependency or bundling it—both options seem to add extra overhead in my opinion 🤔

Looking forward to your feedback! ❤️

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@kricsleo
kricsleo requested a review from pi0 as a code owner April 1, 2025 12:30

@pi0 pi0 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks ❤️

I have not tested PR and fetching socket is acually possible in Node, Deno and Bun but it can get buggy so we need to move away from it. Your code in PR would be a good start!

@vercel

vercel Bot commented Dec 16, 2025

Copy link
Copy Markdown

@pi0 is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Dec 16, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Replaces ofetch-based devFetch implementation with a native HTTP-based approach using Node's http module to properly support Unix socket paths and custom ports for dev server communication in task CLI commands.

Changes

Cohort / File(s) Summary
HTTP implementation refactor
src/task.ts
Replaces ofetch with native http.request-based devFetch implementation; adds support for custom baseURL and socketPath from buildInfo; implements generic GET/POST wrapper with query parameters and body support; includes request error handling and response status/JSON parsing.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • HTTP error handling: Verify error propagation for network failures, non-2xx/3xx responses, and JSON parsing edge cases
  • Socket path configuration: Ensure socketPath is correctly passed and utilized in request options without breaking HTTP requests
  • Generic type safety: Validate devFetch's generic type parameter handling and TypeScript compatibility across GET/POST paths
  • Query parameter encoding: Confirm withQuery properly constructs and encodes query strings without conflicts

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits format with a clear 'refactor' type and descriptive message about using http.request.
Description check ✅ Passed The description is directly related to the changeset, explaining why ofetch was replaced with http.request to support socketPath.
Linked Issues check ✅ Passed The PR directly addresses issue #3264 by replacing ofetch with http.request to enable socketPath support for dev server communication.
Out of Scope Changes check ✅ Passed All changes are scoped to the task.ts file and directly support the objective of enabling socketPath support without introducing external dependencies.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@pi0 pi0 changed the title fix(task): use http.request to support socketPath in dev fetch refactor(task): use http.request Dec 16, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/task.ts (2)

60-61: LGTM! Consider adding a clarifying comment.

The baseURL construction is correct. When socketPath is provided, http.request uses it for the connection while still using the URL for the HTTP request line and Host header. The defaults (localhost:3000) appear reasonable for a dev server.

Optionally, add a comment to clarify the dual usage:

+  // baseURL is used for HTTP headers and path; socketPath (if present) overrides host/port for the connection
   const baseURL = `http://${buildInfo.dev.workerAddress.host || "localhost"}:${buildInfo.dev.workerAddress.port || "3000"}`;
   const socketPath = buildInfo.dev.workerAddress.socketPath;

77-86: Consider adding a request timeout.

The request setup is correct, but without a timeout, the request could hang indefinitely if the dev server is unresponsive.

Apply this diff to add a reasonable timeout:

       const request = http.request(
         url,
         {
           socketPath,
           method: options?.method,
+          timeout: 30000, // 30 seconds
           headers: {
             Accept: "application/json",
             "Content-Type": "application/json",
           },
         },
         (response) => {
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9bf11b7 and 17955f4.

📒 Files selected for processing (1)
  • src/task.ts (2 hunks)
⏰ 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). (3)
  • GitHub Check: tests-rolldown (windows-latest)
  • GitHub Check: tests-rollup (windows-latest)
  • GitHub Check: tests-rollup (ubuntu-latest)
🔇 Additional comments (4)
src/task.ts (4)

1-1: LGTM!

The new imports are appropriate for the http.request-based implementation and provide proper typing support.

Also applies to: 5-5, 7-9


63-70: LGTM!

The function signature is well-designed with proper typing. The generic type parameter allows callers to specify the expected response type, and the options are appropriately typed and optional.


71-75: LGTM!

The URL construction using ufo utilities is clean and correct, properly handling base path joining and query parameter encoding.


105-110: LGTM!

The request finalization logic is correct: errors are caught, the body is properly serialized, and the request is ended. While setting a Content-Length header would be more explicit, it's optional in HTTP/1.1.

Comment thread src/task.ts Resolved
Comment thread src/task.ts Resolved
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.

Task CLI commands fail when dev server isn't using default port 3000

2 participants