Skip to content

BUG :res.end() inside streaming loop truncates responses at 16KB #123

@SUVAJIT-KARMAKAR

Description

@SUVAJIT-KARMAKAR

Description of the bug

In setResponse (src/adapters/node/request.ts), res.end() is called inside the for loop that streams the response body. When res.write(value) succeeds (returns true), execution falls through to res.end(), terminating the response after the first chunk. Any response larger than 16KB is silently truncated.

My Observation on the existing code

for (;;) {
    const { done, value } = await reader.read();
    if (done) break;
    if (!res.write(value))
        if (process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.LAMBDA_TASK_ROOT)
            continue;
        else {
            res.once("drain", next);
            return;
        }
    res.end(); // called inside the loop after first successful write
}

Expected

res.end() should be placed after the for loop, so the response is only finalized once all chunks have been written.

Reproduction

Use toNodeHandler with any framework (Express, etc.) and serve a route that returns a response larger than 16KB. The response will be truncated at exactly 16,384 bytes.

Issue fixed in pull-request

#124

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