@@ -11,8 +11,9 @@ const { ReadableStreamFrom } = require('../core/util')
11
11
const kConsume = Symbol ( 'kConsume' )
12
12
const kReading = Symbol ( 'kReading' )
13
13
const kBody = Symbol ( 'kBody' )
14
- const kAbort = Symbol ( 'abort ' )
14
+ const kAbort = Symbol ( 'kAbort ' )
15
15
const kContentType = Symbol ( 'kContentType' )
16
+ const kContentLength = Symbol ( 'kContentLength' )
16
17
17
18
const noop = ( ) => { }
18
19
@@ -21,6 +22,7 @@ module.exports = class BodyReadable extends Readable {
21
22
resume,
22
23
abort,
23
24
contentType = '' ,
25
+ contentLength,
24
26
highWaterMark = 64 * 1024 // Same as nodejs fs streams.
25
27
} ) {
26
28
super ( {
@@ -29,12 +31,15 @@ module.exports = class BodyReadable extends Readable {
29
31
highWaterMark
30
32
} )
31
33
34
+ contentLength = contentLength != null ? Number ( contentLength ) : null
35
+
32
36
this . _readableState . dataEmitted = false
33
37
34
38
this [ kAbort ] = abort
35
39
this [ kConsume ] = null
36
40
this [ kBody ] = null
37
41
this [ kContentType ] = contentType
42
+ this [ kContentLength ] = Number . isFinite ( contentLength ) ? contentLength : null
38
43
39
44
// Is stream being consumed through Readable API?
40
45
// This is an optimization so that we avoid checking
@@ -146,7 +151,7 @@ module.exports = class BodyReadable extends Readable {
146
151
}
147
152
148
153
async dump ( opts ) {
149
- let limit = Number . isFinite ( opts ?. limit ) ? opts . limit : 262144
154
+ let limit = Number . isFinite ( opts ?. limit ) ? opts . limit : 128 * 1024
150
155
const signal = opts ?. signal
151
156
152
157
if ( signal != null && ( typeof signal !== 'object' || ! ( 'aborted' in signal ) ) ) {
@@ -160,6 +165,10 @@ module.exports = class BodyReadable extends Readable {
160
165
}
161
166
162
167
return await new Promise ( ( resolve , reject ) => {
168
+ if ( this [ kContentLength ] > limit ) {
169
+ this . destroy ( new AbortError ( ) )
170
+ }
171
+
163
172
const onAbort = ( ) => {
164
173
this . destroy ( signal . reason ?? new AbortError ( ) )
165
174
}
0 commit comments