Skip to content

Conversation

@mcollina
Copy link
Member

Summary

Fixes a handler lifecycle violation in the dump interceptor that was causing it to call super.onResponseEnd() prematurely from within onResponseData().

Problem

The original implementation called super.onResponseEnd(controller, {}) from within onResponseData() when size >= maxSize. This violates the handler lifecycle because:

  1. The HTTP response is still being received
  2. onResponseEnd() hasn't been called yet by the HTTP parser
  3. This causes the handler chain to complete while data is still flowing

Changes

  • onResponseData: Set #dumped = true when size limit is reached, but don't call super.onResponseEnd() prematurely. Continue consuming data by returning true.
  • onResponseEnd: Only call super.onResponseEnd() when the actual HTTP response ends, checking the #dumped flag to determine if we should pass empty trailers.
  • Abort handling: Fixed to check this.#controller.aborted (consistent with original implementation)

Known Issue

The tests still timeout due to a separate architectural issue:

When Content-Length header is set but the dump interceptor doesn't pass data to the body stream (because we're discarding it), the body stream in lib/api/api-request.js hangs waiting for data that will never arrive.

This issue exists in the original implementation too (verified by testing the original code - it also times out).

Recommended Follow-up

The body stream implementation needs to be updated to handle the case where:

  • Content-Length header is set
  • But onComplete() is called without receiving the expected amount of data via onData()

Or alternatively, update test expectations to not expect both Content-Length preservation AND empty body.

Testing

Analyzed the issue using:

NODE_DEBUG=undici npx borp -p "test/interceptors/dump-interceptor.js" --timeout 5000

The lifecycle fix is correct, but exposes the pre-existing body stream limitation.

🤖 Generated with Claude Code

The dump interceptor was calling super.onResponseEnd() from within
onResponseData() when the size limit was reached. This violates the
handler lifecycle and causes the response handling to complete
prematurely while data is still being received.

Changes:
- onResponseData: Set #dumped flag when size >= maxSize but don't
  call super.onResponseEnd() prematurely
- onResponseEnd: Call super.onResponseEnd() only when the HTTP
  response actually ends, checking the #dumped flag
- Fixed abort handling to check this.#controller.aborted

Note: Tests still timeout due to a separate issue in the body stream
implementation (lib/api/api-request.js). When Content-Length is set
but no data is passed to the body stream (because we're dumping it),
the stream hangs waiting for data. This needs to be addressed in a
follow-up fix.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Signed-off-by: Matteo Collina <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants