phantomasWrapper.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. var async = require('async');
  2. var Q = require('q');
  3. var ps = require('ps-node');
  4. var path = require('path');
  5. var debug = require('debug')('ylt:phantomaswrapper');
  6. var phantomas = require('phantomas');
  7. var PhantomasWrapper = function() {
  8. 'use strict';
  9. /**
  10. * This is the phantomas launcher. It merges user chosen options into the default options
  11. */
  12. this.execute = function(data) {
  13. var deferred = Q.defer();
  14. var task = data.params;
  15. var viewportOption = null;
  16. // Setting screen dimensions for desktop devices only.
  17. // Phone and tablet dimensions are dealt by Phantomas.
  18. if (task.options.device === 'desktop') {
  19. // Similar to an old non-retina Macbook Air 13"
  20. viewportOption = '1280x800x1';
  21. } else if (task.options.device === 'desktop-hd') {
  22. // Similar to a retina Macbook Pro 16"
  23. viewportOption = '1536x960x2';
  24. }
  25. var options = {
  26. // Cusomizable options
  27. 'timeout': task.options.timeout || 120,
  28. '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,
  29. 'tablet': (task.options.device === 'tablet'),
  30. 'phone': (task.options.device === 'phone'),
  31. 'screenshot': task.options.screenshot || false,
  32. 'viewport': viewportOption,
  33. 'wait-for-network-idle': true,
  34. //'wait-for-selector': task.options.waitForSelector,
  35. 'cookie': task.options.cookie,
  36. 'auth-user': task.options.authUser,
  37. 'auth-pass': task.options.authPass,
  38. 'block-domain': task.options.blockDomain,
  39. 'allow-domain': task.options.allowDomain,
  40. 'no-externals': task.options.noExternals,
  41. // Mandatory
  42. 'analyze-css': true,
  43. 'ignore-ssl-errors': true
  44. };
  45. // Proxy option can't be set to null or undefined...
  46. // this is why it's set now and not in the object above
  47. if (task.options.proxy) {
  48. options.proxy = task.options.proxy;
  49. }
  50. // It's time to launch the test!!!
  51. const promise = phantomas(task.url, options);
  52. // handle the promise
  53. promise.
  54. then(results => {
  55. var json = {
  56. generator: results.getGenerator(),
  57. url: results.getUrl(),
  58. metrics: results.getMetrics(),
  59. offenders: results.getAllOffenders()
  60. };
  61. deferred.resolve(json);
  62. }).
  63. catch(res => {
  64. console.error(res);
  65. deferred.reject('Phantomas failed: ' + res.message);
  66. });
  67. promise.on('milestone', function(event) {
  68. if (event === 'domReady' || event === 'domComplete') {
  69. deferred.notify(event);
  70. }
  71. });
  72. return deferred.promise;
  73. };
  74. };
  75. module.exports = new PhantomasWrapper();