weightChecker.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Redownloading every files after Phantomas has finished
  3. * Checks weight and every kind of compression
  4. *
  5. */
  6. var debug = require('debug')('ylt:weightChecker');
  7. var Q = require('q');
  8. var http = require('http');
  9. var zlib = require('zlib');
  10. var async = require('async');
  11. var request = require('request');
  12. var imageOptimizer = require('./imageOptimizer');
  13. var fileMinifier = require('./fileMinifier');
  14. var WeightChecker = function() {
  15. var MAX_PARALLEL_DOWNLOADS = 10;
  16. var REQUEST_TIMEOUT = 10000; // 10 seconds
  17. // This function will re-download every asset and check if it could be optimized
  18. function recheckAllFiles(data) {
  19. var startTime = Date.now();
  20. debug('Redownload started');
  21. var deferred = Q.defer();
  22. var requestsList = JSON.parse(data.toolsResults.phantomas.offenders.requestsList);
  23. delete data.toolsResults.phantomas.metrics.requestsList;
  24. delete data.toolsResults.phantomas.offenders.requestsList;
  25. // Transform every request into a download function with a callback when done
  26. var redownloadList = requestsList.map(function(entry) {
  27. return function(callback) {
  28. redownloadEntry(entry)
  29. .then(imageOptimizer.optimizeImage)
  30. .then(fileMinifier.minifyFile)
  31. .then(function(newEntry) {
  32. callback(null, newEntry);
  33. })
  34. .fail(function(err) {
  35. callback(err);
  36. });
  37. };
  38. });
  39. // Lanch all redownload functions and wait for completion
  40. async.parallelLimit(redownloadList, MAX_PARALLEL_DOWNLOADS, function(err, results) {
  41. if (err) {
  42. debug(err);
  43. deferred.reject(err);
  44. } else {
  45. debug('All files checked');
  46. endTime = Date.now();
  47. debug('Redownload took %d ms', endTime - startTime);
  48. // Remove unwanted requests (redirections, about:blank)
  49. results = results.filter(function(result) {
  50. return (result !== null && result.weightCheck && result.weightCheck.bodySize > 0);
  51. });
  52. var metrics = {};
  53. var offenders = {};
  54. // Total weight
  55. offenders.totalWeight = listRequestWeight(results);
  56. metrics.totalWeight = offenders.totalWeight.totalWeight;
  57. // Image compression
  58. offenders.imageOptimization = listImageNotOptimized(results);
  59. metrics.imageOptimization = offenders.imageOptimization.totalGain;
  60. // File minification
  61. offenders.fileMinification = listFilesNotMinified(results);
  62. metrics.fileMinification = offenders.fileMinification.totalGain;
  63. data.toolsResults.weightChecker = {
  64. metrics: metrics,
  65. offenders: offenders
  66. };
  67. deferred.resolve(data);
  68. }
  69. });
  70. return deferred.promise;
  71. }
  72. function listRequestWeight(requests) {
  73. var results = {
  74. totalWeight: 0,
  75. byType: {
  76. html: {
  77. totalWeight: 0,
  78. requests: []
  79. },
  80. css: {
  81. totalWeight: 0,
  82. requests: []
  83. },
  84. js: {
  85. totalWeight: 0,
  86. requests: []
  87. },
  88. json: {
  89. totalWeight: 0,
  90. requests: []
  91. },
  92. image: {
  93. totalWeight: 0,
  94. requests: []
  95. },
  96. video: {
  97. totalWeight: 0,
  98. requests: []
  99. },
  100. webfont: {
  101. totalWeight: 0,
  102. requests: []
  103. },
  104. other: {
  105. totalWeight: 0,
  106. requests: []
  107. }
  108. }
  109. };
  110. requests.forEach(function(req) {
  111. var weight = ((typeof req.weightCheck.bodySize === 'number') ? req.weightCheck.bodySize + req.weightCheck.headersSize : req.contentLength) || 0;
  112. var type = req.type || 'other';
  113. results.totalWeight += weight;
  114. results.byType[type].totalWeight += weight;
  115. results.byType[type].requests.push({
  116. url: req.url,
  117. weight: weight
  118. });
  119. });
  120. return results;
  121. }
  122. function listImageNotOptimized(requests) {
  123. var results = {
  124. totalGain: 0,
  125. images: []
  126. };
  127. requests.forEach(function(req) {
  128. if (req.weightCheck.uncompressedSize && req.weightCheck.isOptimized === false) {
  129. var gain = req.weightCheck.uncompressedSize - req.weightCheck.optimized;
  130. results.totalGain += gain;
  131. results.images.push({
  132. url: req.url,
  133. original: req.weightCheck.uncompressedSize,
  134. optimized: req.weightCheck.optimized,
  135. lossless: req.weightCheck.lossless,
  136. lossy: req.weightCheck.lossy,
  137. gain: gain
  138. });
  139. }
  140. });
  141. return results;
  142. }
  143. function listFilesNotMinified(requests) {
  144. var results = {
  145. totalGain: 0,
  146. files: []
  147. };
  148. requests.forEach(function(req) {
  149. if (req.weightCheck.uncompressedSize && req.weightCheck.isMinified === false) {
  150. var gain = req.weightCheck.uncompressedSize - req.weightCheck.minified;
  151. results.totalGain += gain;
  152. results.files.push({
  153. url: req.url,
  154. original: req.weightCheck.uncompressedSize,
  155. minified: req.weightCheck.minified,
  156. gain: gain
  157. });
  158. }
  159. });
  160. return results;
  161. }
  162. function redownloadEntry(entry) {
  163. var deferred = Q.defer();
  164. function downloadError(message) {
  165. debug('Could not download %s Error: %s', entry.url, message);
  166. entry.weightCheck = {
  167. message: message
  168. };
  169. deferred.resolve(entry);
  170. }
  171. // Not downloaded again but will be counted in totalWeight
  172. function notDownloadableFile(message) {
  173. entry.weightCheck = {
  174. message: message
  175. };
  176. deferred.resolve(entry);
  177. }
  178. // Not counted in totalWeight
  179. function unwantedFile(message) {
  180. debug(message);
  181. deferred.resolve(entry);
  182. }
  183. if (entry.method !== 'GET') {
  184. notDownloadableFile('only downloading GET');
  185. return deferred.promise;
  186. }
  187. if (entry.status !== 200) {
  188. unwantedFile('only downloading requests with status code 200');
  189. return deferred.promise;
  190. }
  191. if (entry.url === 'about:blank') {
  192. unwantedFile('not downloading about:blank');
  193. return deferred.promise;
  194. }
  195. debug('Downloading %s', entry.url);
  196. // Always add a gzip header before sending, in case the server listens to it
  197. var reqHeaders = entry.requestHeaders;
  198. reqHeaders['Accept-Encoding'] = 'gzip, deflate';
  199. var requestOptions = {
  200. method: entry.method,
  201. url: entry.url,
  202. headers: reqHeaders,
  203. timeout: REQUEST_TIMEOUT
  204. };
  205. download(requestOptions, entry.contentType, function(error, result) {
  206. if (error) {
  207. if (error.code === 'ETIMEDOUT') {
  208. downloadError('timeout after ' + REQUEST_TIMEOUT + 'ms');
  209. } else {
  210. downloadError('error while downloading: ' + error.code);
  211. }
  212. return;
  213. }
  214. debug('%s downloaded correctly', entry.url);
  215. entry.weightCheck = result;
  216. deferred.resolve(entry);
  217. });
  218. return deferred.promise;
  219. }
  220. // Inspired by https://github.com/cvan/fastHAR-api/blob/10cec585/app.js
  221. function download(requestOptions, contentType, callback) {
  222. var statusCode;
  223. request(requestOptions)
  224. .on('response', function(res) {
  225. // Raw headers were added in NodeJS v0.12
  226. // (https://github.com/joyent/node/issues/4844), but let's
  227. // reconstruct them for backwards compatibility.
  228. var rawHeaders = ('HTTP/' + res.httpVersion + ' ' + res.statusCode +
  229. ' ' + http.STATUS_CODES[res.statusCode] + '\r\n');
  230. Object.keys(res.headers).forEach(function(headerKey) {
  231. rawHeaders += headerKey + ': ' + res.headers[headerKey] + '\r\n';
  232. });
  233. rawHeaders += '\r\n';
  234. var uncompressedSize = 0; // size after uncompression
  235. var bodySize = 0; // bytes size over the wire
  236. var body = ''; // plain text body (after uncompressing gzip/deflate)
  237. var isCompressed = false;
  238. function tally() {
  239. if (statusCode !== 200) {
  240. callback({code: statusCode});
  241. return;
  242. }
  243. var result = {
  244. body: body,
  245. headersSize: Buffer.byteLength(rawHeaders, 'utf8'),
  246. bodySize: bodySize,
  247. isCompressed: isCompressed,
  248. uncompressedSize: uncompressedSize
  249. };
  250. callback(null, result);
  251. }
  252. switch (res.headers['content-encoding']) {
  253. case 'gzip':
  254. var gzip = zlib.createGunzip();
  255. gzip.on('data', function (data) {
  256. body += data;
  257. uncompressedSize += data.length;
  258. }).on('end', function () {
  259. isCompressed = true;
  260. tally();
  261. }).on('error', function(err) {
  262. debug(err);
  263. });
  264. res.on('data', function (data) {
  265. bodySize += data.length;
  266. }).pipe(gzip);
  267. break;
  268. case 'deflate':
  269. res.setEncoding('utf8');
  270. var deflate = zlib.createInflate();
  271. deflate.on('data', function (data) {
  272. body += data;
  273. uncompressedSize += data.length;
  274. }).on('end', function () {
  275. isCompressed = true;
  276. tally();
  277. }).on('error', function(err) {
  278. debug(err);
  279. });
  280. res.on('data', function (data) {
  281. bodySize += data.length;
  282. }).pipe(deflate);
  283. break;
  284. default:
  285. if (contentType === 'image/jpeg' || contentType === 'image/png') {
  286. res.setEncoding('binary');
  287. }
  288. res.on('data', function (data) {
  289. body += data;
  290. uncompressedSize += data.length;
  291. bodySize += data.length;
  292. }).on('end', function () {
  293. tally();
  294. });
  295. break;
  296. }
  297. })
  298. .on('response', function(response) {
  299. statusCode = response.statusCode;
  300. })
  301. .on('error', function(err) {
  302. callback(err);
  303. });
  304. }
  305. return {
  306. recheckAllFiles: recheckAllFiles,
  307. listRequestWeight: listRequestWeight,
  308. redownloadEntry: redownloadEntry,
  309. download: download
  310. };
  311. };
  312. module.exports = new WeightChecker();