isHttp2.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var debug = require('debug')('ylt:isHttp2');
  2. var url = require('url');
  3. var Q = require('q');
  4. var http2 = require('is-http2');
  5. var isHttp2 = function() {
  6. 'use strict';
  7. this.check = function(data) {
  8. debug('Starting to check for HTTP2 support...');
  9. return this.checkHttp2(data)
  10. .then(function(result) {
  11. if (result.isHttp2) {
  12. debug('HTTP/2 (or SPDY) is supported');
  13. data.toolsResults.http2 = {
  14. metrics: {
  15. http2: true
  16. }
  17. };
  18. } else {
  19. debug('HTTP/2 is not supported');
  20. data.toolsResults.http2 = {
  21. metrics: {
  22. http2: false
  23. }
  24. };
  25. }
  26. // Add the supported protocols as offenders
  27. if (result.supportedProtocols) {
  28. debug('Supported protocols: ' + result.supportedProtocols.join(' '));
  29. data.toolsResults.http2.offenders = {
  30. http2: result.supportedProtocols
  31. };
  32. }
  33. debug('End of HTTP2 support check');
  34. return data;
  35. })
  36. .fail(function() {
  37. return data;
  38. });
  39. };
  40. this.getParsedUrl = function(data) {
  41. return url.parse(data.toolsResults.phantomas.url);
  42. };
  43. this.getProtocol = function(data) {
  44. return this.getParsedUrl(data).protocol;
  45. };
  46. this.getDomain = function(data) {
  47. return this.getParsedUrl(data).hostname;
  48. };
  49. this.checkHttp2 = function(data) {
  50. var deferred = Q.defer();
  51. var domain = this.getDomain(data);
  52. // To make is-http2 work, you need to have openssl in a version greater than 1.0.0 installed and available in your $path.
  53. http2(domain, {includeSpdy: true})
  54. .then(function(result) {
  55. deferred.resolve(result);
  56. })
  57. .catch(function(error) {
  58. debug('Error while checking HTTP2 support:');
  59. debug(error);
  60. deferred.reject('Error while checking for HTTP2 support');
  61. });
  62. return deferred.promise;
  63. };
  64. };
  65. module.exports = new isHttp2();