Skip to content

Commit 0455e06

Browse files
authored
fix: consider bytes read when dumping (#3360)
1 parent 27b2589 commit 0455e06

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/api/readable.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const kBody = Symbol('kBody')
1414
const kAbort = Symbol('kAbort')
1515
const kContentType = Symbol('kContentType')
1616
const kContentLength = Symbol('kContentLength')
17+
const kBytesRead = Symbol('kBytesRead')
1718

1819
const noop = () => {}
1920

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

3637
this[kAbort] = abort
3738
this[kConsume] = null
39+
this[kBytesRead] = 0
3840
this[kBody] = null
3941
this[kContentType] = contentType
40-
this[kContentLength] = contentLength
42+
this[kContentLength] = Number.isFinite(contentLength) ? contentLength : null
4143

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

101103
push (chunk) {
104+
this[kBytesRead] += chunk ? chunk.length : 0
105+
102106
if (this[kConsume] && chunk !== null) {
103107
consumePush(this[kConsume], chunk)
104108
return this[kReading] ? super.push(chunk) : true
@@ -151,7 +155,7 @@ class BodyReadable extends Readable {
151155
}
152156

153157
async dump (opts) {
154-
let limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024
158+
const limit = Number.isFinite(opts?.limit) ? opts.limit : 128 * 1024
155159
const signal = opts?.signal
156160

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

167171
return await new Promise((resolve, reject) => {
168-
if (this[kContentLength] > limit) {
172+
if (this[kContentLength] > limit || this[kBytesRead] > limit) {
169173
this.destroy(new AbortError())
170174
}
171175

@@ -185,8 +189,7 @@ class BodyReadable extends Readable {
185189
})
186190
.on('error', noop)
187191
.on('data', function (chunk) {
188-
limit -= chunk.length
189-
if (limit <= 0) {
192+
if (this[kBytesRead] > limit) {
190193
this.destroy()
191194
}
192195
})

0 commit comments

Comments
 (0)