Skip to content

Commit

Permalink
chore: remove isJSON in res.length (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinovyatkin authored and dead-horse committed Oct 15, 2019
1 parent d48d88e commit 4f96829
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const contentDisposition = require('content-disposition');
const ensureErrorHandler = require('error-inject');
const getType = require('cache-content-type');
const onFinish = require('on-finished');
const isJSON = require('koa-is-json');
const escape = require('escape-html');
const typeis = require('type-is').is;
const statuses = require('statuses');
Expand All @@ -20,6 +19,7 @@ const vary = require('vary');
const only = require('only');
const util = require('util');
const encodeUrl = require('encodeurl');
const Stream = require('stream');

/**
* Prototype.
Expand Down Expand Up @@ -201,18 +201,15 @@ module.exports = {
*/

get length() {
const len = this.header['content-length'];
const body = this.body;

if (null == len) {
if (!body) return;
if ('string' == typeof body) return Buffer.byteLength(body);
if (Buffer.isBuffer(body)) return body.length;
if (isJSON(body)) return Buffer.byteLength(JSON.stringify(body));
return;
if (this.has('Content-Length')) {
return parseInt(this.get('Content-Length'), 10) || 0;
}

return Math.trunc(len) || 0;
const { body } = this;
if (!body || body instanceof Stream) return undefined;
if ('string' === typeof body) return Buffer.byteLength(body);
if (Buffer.isBuffer(body)) return body.length;
return Buffer.byteLength(JSON.stringify(body));
},

/**
Expand Down

0 comments on commit 4f96829

Please sign in to comment.