Skip to content

Commit 4f96829

Browse files
tinovyatkindead-horse
authored andcommitted
chore: remove isJSON in res.length (#1399)
1 parent d48d88e commit 4f96829

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

lib/response.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const contentDisposition = require('content-disposition');
99
const ensureErrorHandler = require('error-inject');
1010
const getType = require('cache-content-type');
1111
const onFinish = require('on-finished');
12-
const isJSON = require('koa-is-json');
1312
const escape = require('escape-html');
1413
const typeis = require('type-is').is;
1514
const statuses = require('statuses');
@@ -20,6 +19,7 @@ const vary = require('vary');
2019
const only = require('only');
2120
const util = require('util');
2221
const encodeUrl = require('encodeurl');
22+
const Stream = require('stream');
2323

2424
/**
2525
* Prototype.
@@ -201,18 +201,15 @@ module.exports = {
201201
*/
202202

203203
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;
213206
}
214207

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));
216213
},
217214

218215
/**

0 commit comments

Comments
 (0)