Skip to content

better-call: Node adapter ends streamed responses too early (ERR_INCOMPLETE_CHUNKED_ENCODING) #128

@AbelMaireg

Description

@AbelMaireg

Hi team, thanks for the great work on Better Auth and better-call.
I found what looks like a streaming bug in the Node adapter that truncates chunked responses.

Versions

• better-auth: 1.6.0
• better-call: 1.3.5
• Runtime: Node/NestJS (proxied by nginx, but issue reproduces at app layer)

What happens

When hitting Better Auth’s OpenAPI reference endpoint (openAPI() plugin, /api/auth/reference), browser shows:
GET ... net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK)
The response starts, but stream closes before full body is sent.

Suspected root cause

In better-call Node adapter (dist/adapters/node/request.mjs, setResponse), res.end() is called inside the read loop after a
successful res.write(value).
That ends the HTTP response after the first chunk for multi-chunk bodies.
Current behavior (simplified):

  for (;;) {
    const { done, value } = await reader.read();
    if (done) break;
    if (!res.write(value)) {
      res.once("drain", next);
      return;
    }
    res.end(); // ends too early
  }

Expected behavior:
• Keep writing all chunks
• Call res.end() only once after loop completion

Why this surfaced

Better Auth openAPI() reference page can be large (embedded schema JSON), so it streams over multiple chunks. The first chunk
is sent, then response closes early -> incomplete chunked encoding in browser.

Suggested fix

Move res.end() outside the loop:

  for (;;) {
    const { done, value } = await reader.read();
    if (done) break;
    if (!res.write(value)) {
      res.once("drain", next);
      return;
    }
  }
  res.end();

Workaround used

I applied a local package patch (Bun patchedDependencies) with the exact change above, and the issue disappeared immediately.
Happy to open a PR if you’d like.

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