@@ -9,7 +9,6 @@ const contentDisposition = require('content-disposition');
9
9
const ensureErrorHandler = require ( 'error-inject' ) ;
10
10
const getType = require ( 'cache-content-type' ) ;
11
11
const onFinish = require ( 'on-finished' ) ;
12
- const isJSON = require ( 'koa-is-json' ) ;
13
12
const escape = require ( 'escape-html' ) ;
14
13
const typeis = require ( 'type-is' ) . is ;
15
14
const statuses = require ( 'statuses' ) ;
@@ -20,6 +19,7 @@ const vary = require('vary');
20
19
const only = require ( 'only' ) ;
21
20
const util = require ( 'util' ) ;
22
21
const encodeUrl = require ( 'encodeurl' ) ;
22
+ const Stream = require ( 'stream' ) ;
23
23
24
24
/**
25
25
* Prototype.
@@ -201,18 +201,15 @@ module.exports = {
201
201
*/
202
202
203
203
get length ( ) {
204
- const len = this . header [ 'content-length' ] ;
205
- const body = this . body ;
206
-
207
- if ( null == len ) {
208
- if ( ! body ) return ;
209
- if ( 'string' == typeof body ) return Buffer . byteLength ( body ) ;
210
- if ( Buffer . isBuffer ( body ) ) return body . length ;
211
- if ( isJSON ( body ) ) return Buffer . byteLength ( JSON . stringify ( body ) ) ;
212
- return ;
204
+ if ( this . has ( 'Content-Length' ) ) {
205
+ return parseInt ( this . get ( 'Content-Length' ) , 10 ) || 0 ;
213
206
}
214
207
215
- return Math . trunc ( len ) || 0 ;
208
+ const { body } = this ;
209
+ if ( ! body || body instanceof Stream ) return undefined ;
210
+ if ( 'string' === typeof body ) return Buffer . byteLength ( body ) ;
211
+ if ( Buffer . isBuffer ( body ) ) return body . length ;
212
+ return Buffer . byteLength ( JSON . stringify ( body ) ) ;
216
213
} ,
217
214
218
215
/**
0 commit comments