Skip to content

Conversation

@svozza
Copy link
Contributor

@svozza svozza commented Nov 29, 2025

Summary

This PR fixes the issue where if the content-length header is missing, requests will not be compressed by the compress middleware. This fix was much trickier than expected. Initially I attempted to add it to webResponseToProxyResult, which is where the Web API response body is deserialized to a JSON string/base64 encoded string. I had hoped that this meant we would only have to read the body once to set set the Content-Length header, however, this is too late because it is the last thing that runs when sending a response so all middleware has already executed.

I did consider getting the content length in handlerResultToWebResponse. This would run on every request and make the header avaialble to all middleware. However, I decided against this for two reasons, one it means every request body gets read twice, regardless of whether the user cares about the content-length header, secondly, because it makes handlerResultToWebResponse .asynchronous. I am open to persuasion here though if others think this is a better way to go, perhaps we could make the content length behaviour opt-in: const app = new Router({setContentLength: true}).

The choice I ended up going with was to do the content-length calculation in the compress middleware itself, that way only users using that middleware will read the body twice. I have also added logic to disable this calculation for HTTP streaming responses.

Changes

  • Update #resolve to take a new option identifying whether we are running in HTTP streaming mode. If we are, we set the Transfer-Ecoding header to chunked.
  • Update compress middleware to calculate the content-length if the header is not present and the Transfer-Ecoding headeris not set to chunked..
  • Added unit tests, icluding making the verification for the positive tests more robust by decompressing the body to ensure it matches the original.
  • Added unit test to ensure issue described here is fixed: test(event-handler): add e2e tests #4754 (comment)
  • Added end to test

Issue number: closes #4751


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@svozza svozza requested a review from sdangol November 29, 2025 15:53
@svozza svozza self-assigned this Nov 29, 2025
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Nov 29, 2025
@boring-cyborg boring-cyborg bot added event-handler This item relates to the Event Handler Utility tests PRs that add or change tests labels Nov 29, 2025
@sonarqubecloud
Copy link

@sdangol sdangol merged commit ad14e18 into main Dec 1, 2025
55 checks passed
@sdangol sdangol deleted the compreesion-middleware-threshold-fix branch December 1, 2025 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event-handler This item relates to the Event Handler Utility size/L PRs between 100-499 LOC tests PRs that add or change tests

Projects

Development

Successfully merging this pull request may close these issues.

Bug: compression middleware ignores threshold when content-length header is missing

2 participants