stream-utils / raw-body

Get and validate the raw body of a readable stream

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

check limit before decoding bytes

kevinburke opened this issue · comments

In this function:

  function onData (chunk) {
    if (complete) return

    received += chunk.length

    if (decoder) {
      buffer += decoder.write(chunk)
    } else {
      buffer.push(chunk)
    }

    if (limit !== null && received > limit) {
      done(createError(413, 'request entity too large', {
        limit: limit,
        received: received,
        type: 'entity.too.large'
      }))
    }
  }

We know if we are going to return an error when we set received; it seems like the error check would be better if it went before the write/push calls since we'd just throw away the written/pushed data in the event of too much data being read.

Good optimization. I hope it's OK if I commit the change with you as the author?