Skip to content

Instantly share code, notes, and snippets.

@cvan
Created April 3, 2014 23:15
Show Gist options
  • Save cvan/9964743 to your computer and use it in GitHub Desktop.
Save cvan/9964743 to your computer and use it in GitHub Desktop.
get header size and file size (compressed, uncompressed) of node HTTP requests (with gzip, deflate, or no compression)
var http = require('http');
var request = require('request');
var zlib = require('zlib');
var opts = {
method: 'GET',
url: '<url>',
// headers: {'Accept-Encoding': 'gzip, deflate'}
};
var req = request(opts).on('response', function(res) {
// Raw headers were added in v0.12 (https://github.com/joyent/node/issues/4844),
// but let's reconstruct them.
var rawHeaders = ('HTTP/' + res.httpVersion + ' ' + res.statusCode +
' ' + http.STATUS_CODES[res.statusCode] + '\r\n');
Object.keys(res.headers).forEach(function(headerKey) {
rawHeaders += headerKey + ': ' + res.headers[headerKey] + '\r\n';
});
rawHeaders += '\r\n';
console.log(res.headers['content-encoding']);
switch (res.headers['content-encoding']) {
case 'gzip':
var gzip = zlib.createGunzip();
var uncompressedSize = 0; // size after uncompression
var bodySize = 0; // bytes size over the wire
gzip.on('data', function (data) {
uncompressedSize += data.length;
}).on('end', function () {
console.log('headersSize:', Buffer.byteLength(rawHeaders, 'utf8'));
console.log('content.size uncompressed:', uncompressedSize);
console.log('bodySize compressed:', bodySize);
console.log('compression:', uncompressedSize - bodySize);
});
res.on('data', function (data) {
bodySize += data.length;
}).pipe(gzip);
break;
case 'deflate':
var deflate = zlib.createInflate();
var uncompressedSize = 0; // size after uncompression
var bodySize = 0; // bytes size over the wire
deflate.on('data', function (data) {
uncompressedSize += data.length;
}).on('end', function () {
console.log('headersSize:', Buffer.byteLength(rawHeaders, 'utf8'));
console.log('content.size uncompressed:', uncompressedSize);
console.log('bodySize compressed:', bodySize);
console.log('compression:', uncompressedSize - bodySize);
});
res.on('data', function (data) {
bodySize += data.length;
}).pipe(deflate);
break;
default:
var uncompressedSize = 0; // size after uncompression
var bodySize = 0; // bytes size over the wire
res.on('data', function (data) {
uncompressedSize += bodySize += data.length;
}).on('end', function () {
console.log('headersSize:', Buffer.byteLength(rawHeaders, 'utf8'));
console.log('content.size uncompressed:', uncompressedSize);
console.log('bodySize compressed:', bodySize);
console.log('compression:', uncompressedSize - bodySize);
});
break;
}
});
@davidfarinha
Copy link

For uncompressed requests you have "uncompressedSize += bodySize += data.length" to calculate compressed/uncompressed sizes. Is that correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment