Skip to content

Commit

Permalink
Removed exposing downloaded bytes from size call in response object
Browse files Browse the repository at this point in the history
  • Loading branch information
parthverma1 committed Jun 25, 2024
1 parent ab93461 commit 354823f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
56 changes: 27 additions & 29 deletions lib/collection/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,48 +409,46 @@ _.assign(Response.prototype, /** @lends Response.prototype */ {
*/
size: function () {
var sizeInfo = {
body: 0,
body: this.downloadedBytes || 0,
header: 0,
total: 0,
downloadedBytes: this.downloadedBytes
total: 0
},

contentEncoding = this.headers.get(CONTENT_ENCODING),
contentLength = this.headers.get(CONTENT_LENGTH),
isCompressed = false,
byteLength;

// if server sent encoded data, we should first try deriving length from headers
if (_.isString(contentEncoding)) {
if (!this.downloadedBytes) {
if (_.isString(contentEncoding)) {
// desensitise case of content encoding
contentEncoding = contentEncoding.toLowerCase();
// eslint-disable-next-line lodash/prefer-includes
isCompressed = (contentEncoding.indexOf('gzip') > -1) || (contentEncoding.indexOf('deflate') > -1);
}
contentEncoding = contentEncoding.toLowerCase();
// eslint-disable-next-line lodash/prefer-includes
isCompressed = (contentEncoding.indexOf('gzip') > -1) || (contentEncoding.indexOf('deflate') > -1);
}

// If there is a stream defined which looks like buffer, use it's data to calculate the body
if (this.stream) {
byteLength = this.stream.byteLength;
sizeInfo.body = util.isNumeric(byteLength) ? byteLength :
// If there is a stream defined which looks like buffer, use it's data to calculate the body
if (this.stream) {
byteLength = this.stream.byteLength;
sizeInfo.body = util.isNumeric(byteLength) ? byteLength :
/* istanbul ignore next */
0;
}
// otherwise, if body is defined, we try get the true length of the body
else if (!_.isNil(this.body)) {
sizeInfo.body = supportsBuffer ? Buffer.byteLength(this.body.toString()) :
0;
}
// otherwise, if body is defined, we try get the true length of the body
else if (!_.isNil(this.body)) {
sizeInfo.body = supportsBuffer ? Buffer.byteLength(this.body.toString()) :
/* istanbul ignore next */
this.body.toString().length;
}

if (!sizeInfo.downloadedBytes) {
if (contentLength && isCompressed && util.isNumeric(contentLength)) {
sizeInfo.downloadedBytes = _.parseInt(contentLength, 10);
this.body.toString().length;
}
else {
sizeInfo.downloadedBytes = sizeInfo.body;

if (!sizeInfo.downloadedBytes) {
if (contentLength && isCompressed && util.isNumeric(contentLength)) {
sizeInfo.downloadedBytes = _.parseInt(contentLength, 10);
}
else {
sizeInfo.downloadedBytes = sizeInfo.body;
}
}
}

// size of header is added
// https://tools.ietf.org/html/rfc7230#section-3
// HTTP-message = start-line (request-line / status-line)
Expand All @@ -462,7 +460,7 @@ _.assign(Response.prototype, /** @lends Response.prototype */ {
this.headers.contentSize();

// compute the approximate total body size by adding size of header and body
sizeInfo.total = (sizeInfo.downloadedBytes || sizeInfo.body || 0) + (sizeInfo.header);
sizeInfo.total = (sizeInfo.body || 0) + (sizeInfo.header);

return sizeInfo;
},
Expand Down
2 changes: 1 addition & 1 deletion test/unit/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ describe('Response', function () {
var response = new Response({ body: 'random', downloadedBytes: 6 });

expect(response.size()).to.eql({
body: 6, header: 32, total: 38, downloadedBytes: 6
body: 6, header: 32, total: 38
});
});
});
Expand Down

0 comments on commit 354823f

Please sign in to comment.