RFC: Content-Length
on Response should be taken into account?
#879
Comments
content-length
on Response should be taken into account?Content-Length
on Response should be taken into account?
browser + node compatible with combination of both size and timeout: class SizeLimit {
contorller(ctrl, limit) {
this.controller = ctrl
this.limit = limit
this.downloaded = 0
}
stopLargerResponse (res) {
if (!res.ok) {
// stops the timer
this.controller.abort()
return
}
const size = res.headers.get('content-length')
if (+size > limit) this.controller.abort()
if (encoding === 'identity') {
this.uncompressedSize = +size
}
}
count (size) {
this.downloaded += size
if (this.downloaded > this.limit) {
this.controller.abort()
}
}
}
class Timeout {
constructor (ctrl, time) {
this.controller = ctrl
this.timeout = setTimeout(() => ctrl.abort(), time)
ctrl.signal.addEventListener('abort', () => clearTimeout(this.timeout))
}
}
// ----------------------------------------
// create a shared abort controller
var ctrl = new AbortController()
var timeout = new Timeout(ctrl, 5000)
var sizeLimit = new SizeLimit(ctrl, 1024)
// vueConponent.onDestroy = ctrl.abort.bind(ctrl)
const res = await fetch(largeData, { signal: ctrl.signal })
sizeLimit.stopLargerResponse(res)
for await (let chunk of res.body) {
sizeLimit.count(chunk.length)
// do something with chunk
} The |
stumble upon a issue when i try to extend node-fetch's Request with my got an error saying "can not assign a readonly size property" originated from body.js or somewhere from node-fetch.... it was unexpected and not what i wanted. Have never actual used the size limit in node-fetch myself... |
@jimmywarting |
I would be okey by dropping it...
but i know someone are going to nag on us for removing it if we don't come up with an alternativ solution. so I'm not saying yes or no yet. Would like to know what others think first. just felt like i had to write something up to (possible) prevent unnecessary commit & test from ever getting in in the first place if we do decide to remove size before ppl start using the size extension more and more - it's often harder to remove/deprecate code |
size was also one of those things that where added way back before AbortController existed and it was not possible to abort a request |
(didn't see this issue as a bug per say just an enhancement to abort pre early - that never existed) |
Also if i where to refactor my HttpFileLike then i probably would have removed the |
Content-Length
on Response should be taken into account?Content-Length
on Response should be taken into account?
This reverts commit 8130491. For some reason I see the header via HTTPie but not in Chrome…
node-fetch
currently completely ignoringContent-Length
header while consuming response.Fetch specification about handling
Content-Length
on server response says almost nothing:https://fetch.spec.whatwg.org/#concept-http-network-fetch
(see whatwg/fetch#67)On other hand, we have a
fetch-node
specific extension to limit the size of the response.My proposal - analyze and throw early when
size
for response is specified andContent-Length
of response is greater than it.The text was updated successfully, but these errors were encountered: