Skip to content

HttpServerResponse.toWeb throws on Node for 204/205/304 when the response still has a body #8189

Closed

Description

@xia-chao

What happens

On Node (undici), HttpServerResponse.toWeb(...) blows up if the status is 204, 205, or 304 and the Effect response still has a body (text / raw / stream / uint8Array).

The error is:

TypeError: Response constructor: Invalid response status code 204

empty({ status: 204 }) is fine. toWeb(..., { withoutBody: true }) is also fine. Bun does not throw (it keeps the body). So this is easy to miss if you only try Bun.

Fetch / undici do not allow a body on 204 / 205 / 304. toWeb currently picks the body from _tag and always passes it into new Response(...). It does not strip the body for those statuses.

How to reproduce (Node, not Bun)

  1. Use current main.
  2. Run this with Node:
import { HttpServerResponse } from "effect/unstable/http"

const emptyOk = HttpServerResponse.toWeb(
  HttpServerResponse.empty({ status: 204 })
)
console.log("empty", emptyOk.status, emptyOk.body)

HttpServerResponse.toWeb(
  HttpServerResponse.text("still a body", { status: 204 })
)
  1. First call works (body is null). Second call throws.

Same throw for raw / stream / uint8Array with status 204 / 205 / 304.

Screenshots (debugger on Node)

1. empty({ status: 204 }) — _tag is Empty, body stays empty, constructor is happy

empty 204 ok

2. text(..., { status: 204 }) — _tag is Uint8Array, still about to pass a body into new Response

text 204 before throw

3. Debug console — empty works, text throws Invalid response status code 204

console throw

Expected

toWeb should give a Web Response that Node accepts: status stays 204 / 205 / 304, body is empty (same as empty / withoutBody: true).

Where

packages/effect/src/unstable/http/HttpServerResponse.ts → toWeb

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions