fix(core): defer sse writehead until after lifecycle completes#16753
Open
jkalberer wants to merge 1 commit intonestjs:masterfrom
Open
fix(core): defer sse writehead until after lifecycle completes#16753jkalberer wants to merge 1 commit intonestjs:masterfrom
jkalberer wants to merge 1 commit intonestjs:masterfrom
Conversation
Coverage Report for CI Build 0Coverage decreased (-0.03%) to 89.878%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
SSE returned HTTP 200 on ValidationPipe errors because SseStream.pipe() called writeHead before pipes ran inside the cold interceptor Observable.
- SseStream: defer writeHead to commitHeaders() (and _transform when data flows)
- RouterResponseController.sse(): catchError re-throws before headers commit;
pipe(..., { end: false }); setTimeout(0) commitHeaders after subscribe
- RouterExecutionContext: pass statusCode and getHeaders() at handle time
- Unit tests and Express/Fastify integration tests (400 before stream opens)
d2cb176 to
457630a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
When using
@Sse()endpoints withValidationPipe, pipe errors (e.g.BadRequestException) were returned with HTTP 200 becauseSseStream.pipe()eagerly calledwriteHead(200)before the interceptor chain had finished subscribing and running pipes. Once headers were sent, exception filters could not override the status code.Issue Number: N/A
What is the new behavior?
SseStream:writeHeadis no longer called frompipe(); it runs only viacommitHeaders()(also invoked from_transform()when data is written).RouterResponseController.sse(): Subscribes withcatchErrorso errors before headers are committed propagate to exception filters; after commit, errors become SSEerrorevents. Usespipe(..., { end: false })and asetTimeout(0)call tocommitHeaders()so validation/pipe failures (microtasks) skip sending headers, while successful routes open the stream quickly without waiting for the first emission.RouterExecutionContext: PassesstatusCodeandgetHeaders()from the response at handle time.Does this PR introduce a breaking change?
Previously, mutating
response.statusCodeorgetHeaders()only inside the returned observable (after the handle ran) could affect which values were sent with SSE headers. Status and headers are now captured when the SSE response handler runs, consistent with the rest of the HTTP pipeline.Other information