question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.size option not supported on res.body

See original GitHub issue

It’s not mentioned in the README that when using res.body and not res.json() etc, that the .size option will not trigger any response size limiting errors.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
jimmywartingcommented, Sep 18, 2021

As an attempt to implement nodes new stream-consumers then this became more apparent to me. Right now i feel like removing size option all together. it’s not part of the spec and it is not implemented in any other fetch implementation.

3reactions
jimmywartingcommented, May 13, 2021

Here is my take on looking at the size limit in a cross platform way-ish and looking at download progress at the same time

const ctrl = new AbortController()
const limit = 1024
const res = await fetch(url, { signal: ctrl.signal })
const knownLength = Number(res.headers.get('content-length') || 0)
const notEncoded = !res.headers.get('content-encoding')
const decoder = new TextDecoder()
let responseText = ''
let bytesWritten = 0


if (knownLength > limit) {
  ctrl.abort()
  throw new Error(`content size at ${res.url} over limit`)
}

for await (const chunk of res.body) {
  bytesWritten += chunk.byteLength
  if (bytesWritten > limit) {
    ctrl.abort()
    throw new Error(`content size at ${res.url} over limit`)
  }
  if (notEncoded && knownLength) {
    console.log(`progress = ${bytesWritten / knownLength}`)
  }

  // Do something else with chunk
  responseText += decoder.decode(chunk, { stream: true })
}

responseText += decoder.decode() // flush the remaning bytes
const json = JSON.stringify(responseText)
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Error: request entity too large - Stack Overflow
After some digging, I found that setting app.use(express.bodyParser({limit: '50mb'})); did set the limit correctly. When adding a console.log ...
Read more >
body-parser - npm
Node.js body parsing middleware. Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
Read more >
API 參照 - Express 4.x
Returns middleware that only parses urlencoded bodies and only looks at requests where the Content-Type header matches the type option. This parser accepts...
Read more >
5 ways to make HTTP requests in Node.js - LogRocket Blog
We will walk through five options to make the GET HTTP call to the ... response header, and the user ID and name...
Read more >
API Guide | restify Documentation
Restify comes with automatic DTrace support for all your handlers, ... Much as not sending an Accept header means the client gets the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found