Skip to content

Conversation

@kitlangton
Copy link
Contributor

Type

  • Bug Fix

Description

Fixes HttpServerRequest.toWeb throwing TypeError: RequestInit: duplex option is required when sending a body in Node.js when converting requests with streaming bodies (POST, PUT, PATCH).

The Bug

When calling HttpServerRequest.toWeb() on a Node.js server request with a body, it throws:

TypeError: RequestInit: duplex option is required when sending a body.
    at new Request (node:internal/deps/undici/undici:10638:19)

The Fix

Add duplex: "half" option when constructing a Request with a streaming body:

return new Request(ourl.value, {
  method: self.method,
  body,
  headers: self.headers,
  // @ts-expect-error - duplex is required for streaming bodies in Node 18+
  duplex: body !== undefined ? "half" : undefined
})

Why it only affects Node.js

The code has an early return for when source is already a Request:

if (self.source instanceof Request) {
  return self.source
}

In Bun/Deno/web contexts, it returns the native Request directly. The bug only manifests in Node.js where we construct a new Request from an IncomingMessage.

Test

Added test case that converts a POST request with JSON body via toWeb() and reads it back.

@github-project-automation github-project-automation bot moved this to Discussion Ongoing in PR Backlog Dec 12, 2025
@changeset-bot
Copy link

changeset-bot bot commented Dec 12, 2025

🦋 Changeset detected

Latest commit: 127167a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 31 packages
Name Type
@effect/platform Patch
@effect/cli Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/rpc Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-drizzle Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-node Patch
@effect/sql Patch
@effect/workflow Patch
@effect/ai Patch
@effect/ai-amazon-bedrock Patch
@effect/ai-anthropic Patch
@effect/ai-google Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/sql-kysely Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kitlangton
Copy link
Contributor Author

@copilot review this you sweet robot friend.

Copy link

Copilot AI commented Dec 12, 2025

@kitlangton I've opened a new pull request, #5900, to work on those changes. Once the pull request is ready, I'll request review from you.

@kitlangton
Copy link
Contributor Author

@copilot ... You opened a new pull request instead of reviewing this one. You have failed me today, brother.

@kitlangton kitlangton requested a review from Copilot December 12, 2025 23:03
Copy link

Copilot AI commented Dec 12, 2025

@kitlangton I've opened a new pull request, #5901, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug in HttpServerRequest.toWeb where Node.js throws a TypeError about a missing duplex option when converting HTTP requests with streaming bodies. The fix adds the required duplex: "half" option when constructing a Request object with a body stream.

  • Added duplex: "half" option to Request constructor when body is present
  • Added test coverage for POST requests with JSON bodies
  • Refactored code to extract body variable for clarity

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/platform/src/HttpServerRequest.ts Added duplex option to Request constructor and refactored body handling for clarity
packages/platform-node/test/HttpServer.test.ts Added test case to verify POST requests with bodies can be converted to Web Request objects
.changeset/tall-buses-sing.md Added changeset documenting the bug fix

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

body,
headers: self.headers,
// @ts-expect-error - duplex is required for streaming bodies in Node 18+
duplex: body !== undefined ? "half" : undefined
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

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

Consider using the spread operator to conditionally include the duplex property only when body is defined, rather than setting it to undefined. This approach is more idiomatic and avoids passing undefined values to the Request constructor. For example: ...(body !== undefined ? { duplex: "half" as const } : {})

Suggested change
duplex: body !== undefined ? "half" : undefined
...(body !== undefined ? { duplex: "half" as const } : {})

Copilot uses AI. Check for mistakes.
@tim-smart
Copy link
Contributor

#5902

@tim-smart tim-smart closed this Dec 13, 2025
@github-project-automation github-project-automation bot moved this from Discussion Ongoing to Done in PR Backlog Dec 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants