-
-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(MockHttpSocket): handle response stream errors #548
Merged
kettanaito
merged 15 commits into
feat/yet-another-socket-interceptor
from
fix/forward-stream-error
Apr 17, 2024
Merged
Changes from 19 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a192a3a
fix(MockHttpSocket): handle response stream errors
kettanaito 3510477
test: ensure response/end are not emitted
kettanaito 69679a7
fix: flush response headers
kettanaito 60f0f27
Merge branch 'feat/yet-another-socket-interceptor' into fix/forward-s…
kettanaito a36b790
Merge branch 'feat/yet-another-socket-interceptor' into fix/forward-s…
kettanaito 8f84796
feat: handle response stream errors as 500 responses
kettanaito 41a661f
Merge branch 'feat/yet-another-socket-interceptor' into fix/forward-s…
kettanaito 07834f1
fix(MockHttpSocket): translate unhandled errors to 500 responses
kettanaito 84fca0e
fix: handle interceptor errors as 500 responses
kettanaito 22dedf5
test: fix miniflare test
kettanaito 194c9c8
fix: allow node-like errors
kettanaito e93cef2
chore: merge with the existing test
kettanaito 9f04c0e
Merge branch 'main' into fix/forward-stream-error
kettanaito 48f3338
fix(MockHttpSocket): treat all response stream errors as 500 responses
kettanaito 700212c
Merge branch 'feat/yet-another-socket-interceptor' into fix/forward-s…
kettanaito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,10 @@ import { isPropertyAccessible } from '../../utils/isPropertyAccessible' | |
import { baseUrlFromConnectionOptions } from '../Socket/utils/baseUrlFromConnectionOptions' | ||
import { parseRawHeaders } from '../Socket/utils/parseRawHeaders' | ||
import { getRawFetchHeaders } from '../../utils/getRawFetchHeaders' | ||
import { RESPONSE_STATUS_CODES_WITHOUT_BODY } from '../../utils/responseUtils' | ||
import { | ||
createServerErrorResponse, | ||
RESPONSE_STATUS_CODES_WITHOUT_BODY, | ||
} from '../../utils/responseUtils' | ||
import { createRequestId } from '../../createRequestId' | ||
|
||
type HttpConnectionOptions = any | ||
|
@@ -248,34 +251,41 @@ export class MockHttpSocket extends MockSocket { | |
} | ||
|
||
if (response.body) { | ||
const reader = response.body.getReader() | ||
|
||
while (true) { | ||
const { done, value } = await reader.read() | ||
|
||
if (done) { | ||
break | ||
} | ||
|
||
// Flush the headers upon the first chunk in the stream. | ||
// This ensures the consumer will start receiving the response | ||
// as it streams in (subsequent chunks are pushed). | ||
if (httpHeaders.length > 0) { | ||
flushHeaders(value) | ||
continue | ||
try { | ||
const reader = response.body.getReader() | ||
|
||
while (true) { | ||
const { done, value } = await reader.read() | ||
|
||
if (done) { | ||
break | ||
} | ||
|
||
// Flush the headers upon the first chunk in the stream. | ||
// This ensures the consumer will start receiving the response | ||
// as it streams in (subsequent chunks are pushed). | ||
if (httpHeaders.length > 0) { | ||
flushHeaders(value) | ||
continue | ||
} | ||
|
||
// Subsequent body chukns are push to the stream. | ||
this.push(value) | ||
} | ||
} catch (error) { | ||
// Coerce response stream errors to 500 responses. | ||
// Don't flush the original response headers because | ||
// unhandled errors translate to 500 error responses forcefully. | ||
this.respondWith(createServerErrorResponse(error)) | ||
|
||
// Subsequent body chukns are push to the stream. | ||
this.push(value) | ||
return | ||
} | ||
} | ||
|
||
// If the headers were not flushed up to this point, | ||
// this means the response either had no body or had | ||
// an empty body stream. Flush the headers. | ||
if (httpHeaders.length > 0) { | ||
flushHeaders() | ||
} | ||
flushHeaders() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's safe to call |
||
|
||
// Close the socket if the connection wasn't marked as keep-alive. | ||
if (!this.shouldKeepAlive) { | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Treating all the response stream errors as 500 error responses! Consistency.