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