index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Module dependencies
  3. */
  4. var XMLHttpRequest = require('xmlhttprequest-ssl');
  5. var XHR = require('./polling-xhr');
  6. var JSONP = require('./polling-jsonp');
  7. var websocket = require('./websocket');
  8. /**
  9. * Export transports.
  10. */
  11. exports.polling = polling;
  12. exports.websocket = websocket;
  13. /**
  14. * Polling transport polymorphic constructor.
  15. * Decides on xhr vs jsonp based on feature detection.
  16. *
  17. * @api private
  18. */
  19. function polling (opts) {
  20. var xhr;
  21. var xd = false;
  22. var xs = false;
  23. var jsonp = false !== opts.jsonp;
  24. if (typeof location !== 'undefined') {
  25. var isSSL = 'https:' === location.protocol;
  26. var port = location.port;
  27. // some user agents have empty `location.port`
  28. if (!port) {
  29. port = isSSL ? 443 : 80;
  30. }
  31. xd = opts.hostname !== location.hostname || port !== opts.port;
  32. xs = opts.secure !== isSSL;
  33. }
  34. opts.xdomain = xd;
  35. opts.xscheme = xs;
  36. xhr = new XMLHttpRequest(opts);
  37. if ('open' in xhr && !opts.forceJSONP) {
  38. return new XHR(opts);
  39. } else {
  40. if (!jsonp) throw new Error('JSONP disabled');
  41. return new JSONP(opts);
  42. }
  43. }