phantomasWrapper.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. var Q = require('q');
  2. var path = require('path');
  3. var debug = require('debug')('ylt:phantomaswrapper');
  4. var phantomas = require('phantomas');
  5. var PhantomasWrapper = function() {
  6. 'use strict';
  7. /**
  8. * This is the phantomas launcher. It merges user chosen options into the default options
  9. */
  10. this.execute = function(data) {
  11. var deferred = Q.defer();
  12. var task = data.params;
  13. var viewportOption = null;
  14. // Setting screen dimensions for desktop devices only.
  15. // Phone and tablet dimensions are dealt by Phantomas.
  16. if (task.options.device === 'desktop') {
  17. // Similar to an old non-retina Macbook Air 13"
  18. viewportOption = '1280x800x1';
  19. } else if (task.options.device === 'desktop-hd') {
  20. // Similar to a retina Macbook Pro 16"
  21. viewportOption = '1536x960x2';
  22. }
  23. var options = {
  24. // Cusomizable options
  25. 'timeout': task.options.timeout || 120,
  26. 'user-agent': (task.options.device === 'desktop') ? 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) YLT Chrome/85.0.4183.121 Safari/537.36' : null,
  27. 'tablet': (task.options.device === 'tablet'),
  28. 'phone': (task.options.device === 'phone'),
  29. 'screenshot': task.options.screenshot || false,
  30. 'viewport': viewportOption,
  31. 'wait-for-network-idle': true,
  32. 'cookie': task.options.cookie,
  33. 'auth-user': task.options.authUser,
  34. 'auth-pass': task.options.authPass,
  35. 'block-domain': task.options.blockDomain,
  36. 'allow-domain': task.options.allowDomain,
  37. 'no-externals': task.options.noExternals,
  38. 'local-storage': task.options.localStorage,
  39. 'session-storage': task.options.sessionStorage,
  40. // Mandatory
  41. 'analyze-css': true,
  42. 'analyze-images': true,
  43. 'ignoreSslErrors': true, // until Phantomas 2.1
  44. 'ignore-ssl-errors': true // for Phantomas >= 2.2
  45. };
  46. // Proxy option can't be set to null or undefined...
  47. // this is why it's set now and not in the object above
  48. if (task.options.proxy) {
  49. options.proxy = task.options.proxy;
  50. }
  51. var debugCmd = 'DEBUG=* node node_modules/phantomas/bin/phantomas.js --url ' + task.url;
  52. Object.keys(options).forEach(function(key) {
  53. if (key !== 'wait-for-network-idle' && options[key] !== null) {
  54. debugCmd += ' --' + key + ' ' + options[key];
  55. }
  56. });
  57. debug('If you want to run Phantomas alone for debugging purpose, this is the command: %s', debugCmd);
  58. // It's time to launch the test!!!
  59. const promise = phantomas(task.url, options);
  60. // handle the promise
  61. promise.
  62. then(results => {
  63. var json = {
  64. generator: results.getGenerator(),
  65. url: results.getUrl(),
  66. metrics: results.getMetrics(),
  67. offenders: results.getAllOffenders()
  68. };
  69. // Special rules here
  70. if (task.options.device !== 'phone') {
  71. delete json.metrics.imagesExcessiveDensity;
  72. delete json.offenders.imagesExcessiveDensity;
  73. }
  74. deferred.resolve(json);
  75. }).
  76. catch(res => {
  77. console.error(res);
  78. deferred.reject('Phantomas failed: ' + res.message);
  79. });
  80. promise.on('milestone', function(event) {
  81. if (event === 'domReady' || event === 'domComplete') {
  82. deferred.notify(event);
  83. }
  84. });
  85. return deferred.promise;
  86. };
  87. };
  88. module.exports = new PhantomasWrapper();