@@ -14,6 +14,7 @@ const kBody = Symbol('kBody')
14
14
const kAbort = Symbol ( 'kAbort' )
15
15
const kContentType = Symbol ( 'kContentType' )
16
16
const kContentLength = Symbol ( 'kContentLength' )
17
+ const kBytesRead = Symbol ( 'kBytesRead' )
17
18
18
19
const noop = ( ) => { }
19
20
@@ -35,9 +36,10 @@ class BodyReadable extends Readable {
35
36
36
37
this [ kAbort ] = abort
37
38
this [ kConsume ] = null
39
+ this [ kBytesRead ] = 0
38
40
this [ kBody ] = null
39
41
this [ kContentType ] = contentType
40
- this [ kContentLength ] = contentLength
42
+ this [ kContentLength ] = Number . isFinite ( contentLength ) ? contentLength : null
41
43
42
44
// Is stream being consumed through Readable API?
43
45
// This is an optimization so that we avoid checking
@@ -99,6 +101,8 @@ class BodyReadable extends Readable {
99
101
}
100
102
101
103
push ( chunk ) {
104
+ this [ kBytesRead ] += chunk ? chunk . length : 0
105
+
102
106
if ( this [ kConsume ] && chunk !== null ) {
103
107
consumePush ( this [ kConsume ] , chunk )
104
108
return this [ kReading ] ? super . push ( chunk ) : true
@@ -151,7 +155,7 @@ class BodyReadable extends Readable {
151
155
}
152
156
153
157
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
155
159
const signal = opts ?. signal
156
160
157
161
if ( signal != null && ( typeof signal !== 'object' || ! ( 'aborted' in signal ) ) ) {
@@ -165,7 +169,7 @@ class BodyReadable extends Readable {
165
169
}
166
170
167
171
return await new Promise ( ( resolve , reject ) => {
168
- if ( this [ kContentLength ] > limit ) {
172
+ if ( this [ kContentLength ] > limit || this [ kBytesRead ] > limit ) {
169
173
this . destroy ( new AbortError ( ) )
170
174
}
171
175
@@ -185,8 +189,7 @@ class BodyReadable extends Readable {
185
189
} )
186
190
. on ( 'error' , noop )
187
191
. on ( 'data' , function ( chunk ) {
188
- limit -= chunk . length
189
- if ( limit <= 0 ) {
192
+ if ( this [ kBytesRead ] > limit ) {
190
193
this . destroy ( )
191
194
}
192
195
} )
0 commit comments