Skip to content

Commit

Permalink
fix: consider bytes read when dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jun 22, 2024
1 parent 02c3a67 commit fa1b59f
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const kBody = Symbol('kBody')
const kAbort = Symbol('kAbort')
const kContentType = Symbol('kContentType')
const kContentLength = Symbol('kContentLength')
const kBytesRead = Symbol('kBytesRead')

const noop = () => {}

Expand All @@ -35,9 +36,10 @@ class BodyReadable extends Readable {

this[kAbort] = abort
this[kConsume] = null
this[kBytesRead] = 0
this[kBody] = null
this[kContentType] = contentType
this[kContentLength] = contentLength
this[kContentLength] = Number.isFinite(contentLength) ? contentLength : null

// Is stream being consumed through Readable API?
// This is an optimization so that we avoid checking
Expand Down Expand Up @@ -99,6 +101,8 @@ class BodyReadable extends Readable {
}

push (chunk) {
this[kBytesRead] += chunk ? chunk.length : 0

if (this[kConsume] && chunk !== null) {
consumePush(this[kConsume], chunk)
return this[kReading] ? super.push(chunk) : true
Expand Down Expand Up @@ -151,7 +155,7 @@ class BodyReadable extends Readable {
}

async dump (opts) {
let limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024
const limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024
const signal = opts?.signal

if (signal != null && (typeof signal !== 'object' || !('aborted' in signal))) {
Expand All @@ -165,7 +169,7 @@ class BodyReadable extends Readable {
}

return await new Promise((resolve, reject) => {
if (this[kContentLength] > limit) {
if (this[kContentLength] > limit || this[kBytesRead] > limit) {
this.destroy(new AbortError())
}

Expand All @@ -185,8 +189,7 @@ class BodyReadable extends Readable {
})
.on('error', noop)
.on('data', function (chunk) {
limit -= chunk.length
if (limit <= 0) {
if (this[kBytesRead] > limit) {
this.destroy()
}
})
Expand Down

0 comments on commit fa1b59f

Please sign in to comment.