- From: Matthieu Sieben <notifications@github.com>
- Date: Thu, 21 Dec 2023 05:56:58 -0800
- To: whatwg/fetch <fetch@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/fetch/issues/1592/1866300231@github.com>
@wattroll, your approach is great but it does load everything in memory. The following does essentially the same without draining the stream: ```ts export async function fetchMaxSizeProcessor( response: Resonse, maxBytes: number ) { if (!response.body) return response const contentLength = response.headers.get('content-length') if (contentLength) { const length = Number(contentLength) if (length > maxBytes) { const err = createError(502, 'Response too large', { response, }) await response.body.cancel(err) throw err } } const newBody = response.body.pipeThrough( new ByteCounterTransformStream(maxBytes) ) const newResponse = new Response(newBody, response) // Some props do not get copied by the Response constructor (e.g. url) for (const key in response) { const prop = key as keyof typeof response const value = response[prop] if (value !== newResponse[prop]) { Object.defineProperty(newResponse, prop, { value, writable: false, enumerable: true, configurable: true, }) } } return newResponse } ``` -- Reply to this email directly or view it on GitHub: https://github.com/whatwg/fetch/issues/1592#issuecomment-1866300231 You are receiving this because you are subscribed to this thread. Message ID: <whatwg/fetch/issues/1592/1866300231@github.com>
Received on Thursday, 21 December 2023 13:57:04 UTC