Skip to content

Commit

Permalink
Updated calculation of size if downloaded bytes is present
Browse files Browse the repository at this point in the history
  • Loading branch information
parthverma1 committed Jul 21, 2024
1 parent 71cb1e3 commit aefa2e8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 48 deletions.
78 changes: 38 additions & 40 deletions lib/collection/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,59 +410,57 @@ _.assign(Response.prototype, /** @lends Response.prototype */ {
*/
size: function () {
var sizeInfo = {
body: 0,
header: 0,
total: 0,
transferedBytes: this.downloadedBytes || 0,
resourceBytes: 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)) {
// desensitise case of content encoding
contentEncoding = contentEncoding.toLowerCase();
// eslint-disable-next-line lodash/prefer-includes
isCompressed = (contentEncoding.indexOf('gzip') > -1) || (contentEncoding.indexOf('deflate') > -1);
}
body: 0,
header: 0,
total: 0,
resourceBytes: 0
};

// if 'Content-Length' header is present and encoding is of type gzip/deflate, we take body as declared by
// server. else we need to compute the same.
if (contentLength && isCompressed && util.isNumeric(contentLength)) {
sizeInfo.body = _.parseInt(contentLength, 10);
}
// Set resourceBytes
// 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;
const streamLength = util.isNumeric(byteLength) ? byteLength :
/* istanbul ignore next */
0;
const byteLength = this.stream.byteLength,
streamLength = util.isNumeric(byteLength) ? byteLength :
/* istanbul ignore next */
0;

sizeInfo.resourceBytes = streamLength;

if (!sizeInfo.body) {
sizeInfo.body = streamLength;
}
}
// otherwise, if body is defined, we try get the true length of the body
else if (!_.isNil(this.body)) {
const bodyLength = supportsBuffer ? Buffer.byteLength(this.body.toString()) :
/* istanbul ignore next */
this.body.toString().length;

sizeInfo.resourceBytes = bodyLength;
if (!sizeInfo.body) {
sizeInfo.body = bodyLength;
}
}

if (!sizeInfo.transferedBytes) {
sizeInfo.transferedBytes = sizeInfo.resourceBytes;
// Set body size as the amount of data downloaded
// If the response has a downloadedBytes property, use it to calculate the body size
if (this.downloadedBytes) {
sizeInfo.body = this.downloadedBytes;
}
else {
let contentEncoding = this.headers.get(CONTENT_ENCODING),
isCompressed = false;
const contentLength = this.headers.get(CONTENT_LENGTH);

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

// if 'Content-Length' header is present and encoding is of type gzip/deflate, we take body as declared by
// server. else we need to compute the same.
if (contentLength && isCompressed && util.isNumeric(contentLength)) {
sizeInfo.body = _.parseInt(contentLength, 10);
}
else {
sizeInfo.body = sizeInfo.resourceBytes;
}
}

// size of header is added
Expand All @@ -476,7 +474,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.transferedBytes || 0) + (sizeInfo.header);
sizeInfo.total = (sizeInfo.body || 0) + (sizeInfo.header);

return sizeInfo;
},
Expand Down
24 changes: 16 additions & 8 deletions test/unit/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,14 @@ describe('Response', function () {
it('should handle blank responses correctly', function () {
var response = new Response();

expect(response.size()).to.eql({ body: 0, header: 32, total: 32, transferedBytes: 0, resourceBytes: 0 });
expect(response.size()).to.eql({ body: 0, header: 32, total: 32, resourceBytes: 0 });
});

it('should report downloaded size correctly', function () {
var response = new Response({ body: 'random', transferedBytes: 6 });

expect(response.size()).to.eql({
body: 6, header: 32, total: 38, transferedBytes: 6, resourceBytes: 6
body: 6, header: 32, total: 38, resourceBytes: 6
});
});
});
Expand Down Expand Up @@ -483,6 +483,17 @@ describe('Response', function () {
expect(response.size().body).to.equal(20);
});

it('must match the content-length of the response if brotli encoded', function () {
var rawResponse = {
code: 200,
body: 'gzipped content xyzxyzxyzxyzxyzxyz',
header: 'Content-Encoding: br\nContent-Length: 20'
},
response = new Response(rawResponse);

expect(response.size().body).to.equal(20);
});

it('must use byteLength from buffer if provided', function () {
var rawResponse = {
code: 200,
Expand All @@ -503,22 +514,19 @@ describe('Response', function () {
},
response = new Response(rawResponse);

expect(response.size().body).to.equal(14);
expect(response.size().transferedBytes).to.equal(10);
expect(response.size().body).to.equal(10);
expect(response.size().resourceBytes).to.equal(14);
});

it('must use content length of stream if header is provided', function () {
it('must use content length of stream if header is provided and downloaded bytes is not present', function () {
var rawResponse = {
code: 200,
stream: Buffer.from('something nice'),
header: 'Transfer-Encoding: chunked\nContent-Length: 20\nContent-Encoding: gzip',
downloadedBytes: 10
header: 'Transfer-Encoding: chunked\nContent-Length: 20\nContent-Encoding: gzip'
},
response = new Response(rawResponse);

expect(response.size().body).to.equal(20);
expect(response.size().transferedBytes).to.equal(10);
expect(response.size().resourceBytes).to.equal(14);
});
});
Expand Down

0 comments on commit aefa2e8

Please sign in to comment.