-
Notifications
You must be signed in to change notification settings - Fork 608
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
feat(Client): add max response size option #1729
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1729 +/- ##
==========================================
+ Coverage 89.58% 89.59% +0.01%
==========================================
Files 58 58
Lines 5262 5278 +16
==========================================
+ Hits 4714 4729 +15
- Misses 548 549 +1 ☔ View full report in Codecov by Sentry. |
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.
Thanks for opening a PR! Can you please add a unit test?
Should 0 be the default - is there ever a possibility that someone would want to ensure a response has an empty body? If so, -1 would be a better default for unlimited body size. |
I fully agree with you, I changed the default value to -1 and added a test to check even an empty payload. |
Thanks for the review @ronag and @KhafraDev 🙏. I made the requested changes, disabling the lint rule and fixing the |
@mateonunez can you please add |
This should not be added to fetch options, to keep in line with the fetch spec. You should use it as such instead: import { Agent, fetch, setGlobalDispatcher } from 'undici'
const agent = new Agent({
maxResponseSize: 5
})
// either:
setGlobalDispatcher(agent)
await fetch('http://localhost:3000')
// or
await fetch('https://localhost:3000', {
dispatcher: agent
}) |
This PR implements #1692 by introducing a new client option:
maxResponseSize
.The
maxResponseSize
parameter is set to0
which means whatever size is allowed, otherwise, the size will be considered in bytes.There is a new check before the Parser computes the
bytesRead
. If that computation is greater than themaxResponseSize
the socket is destroyed by the corresponding error.This approach enforces the
Client
to verify large responses and to close the socket before the buffer is completed.This implementation surf in a low-level way, so tests (such as ResponseContentLengthMismatchError) are covered by
scenarios
intest/errors.js