[Pkg-javascript-commits] [sockjs-client] 72/350: Put transport feature detection in proper places and re-use for info request

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:43 UTC 2016


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.

commit 977a03f53d159cd466b0b559d01c52aeeaac5788
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Fri Jun 6 18:47:47 2014 -0400

    Put transport feature detection in proper places and re-use for info request
---
 lib/sockjs.js                   | 26 +++++++++++---------------
 lib/trans-xdr-streaming.js      |  7 +++++--
 lib/trans-xhr-polling.js        |  4 ++--
 lib/trans-xhr-streaming.js      |  4 ++--
 lib/utils.js                    | 12 +++++++-----
 tests/html/lib/endtoendtests.js | 40 +---------------------------------------
 tests/html/lib/tests.js         |  2 +-
 7 files changed, 29 insertions(+), 66 deletions(-)

diff --git a/lib/sockjs.js b/lib/sockjs.js
index 58fbaf9..0b54013 100644
--- a/lib/sockjs.js
+++ b/lib/sockjs.js
@@ -18,6 +18,8 @@ var JSON3 = require('json3');
 var XHRLocalObject = require('./xhr-local');
 var XHRCorsObject = require('./xhr-cors');
 var XDRObject = require('./xdr');
+var XDRPolling = require('./trans-xdr-polling');
+var IframeTransport = require('./trans-iframe');
 
 function SockJS(url, dep_protocols_whitelist, options) {
     if (!(this instanceof SockJS)) {
@@ -209,8 +211,6 @@ SockJS.prototype._try_next_protocol = function(close_event) {
         that._try_next_protocol();
     }
 
-    var isSameOrigin = utils.isSameOriginUrl(that._base_url);
-
     while(1) {
         var protocol = that.protocol = that._protocols.shift();
         if (!protocol) {
@@ -230,7 +230,7 @@ SockJS.prototype._try_next_protocol = function(close_event) {
         }
 
         if (!SockJS[protocol] ||
-              !SockJS[protocol].enabled(that._options, isSameOrigin)) {
+              !SockJS[protocol].enabled(that._base_url)) {
             that._debug('Skipping transport:', protocol);
         } else {
             var roundTrips = SockJS[protocol].roundTrips || 1;
@@ -281,7 +281,7 @@ SockJS.prototype._applyInfo = function(info, rtt, protocols_whitelist) {
     // avoid browser per-domain connection limits.
     if (info.base_url)
       that._base_url = utils.amendUrl(info.base_url);
-    var probed = utils.probeProtocols();
+    var probed = utils.probeProtocols(that._base_url);
     that._protocols = utils.detectProtocols(probed, protocols_whitelist, info);
 };
 
@@ -350,7 +350,7 @@ SockJS['iframe-eventsource'] = require('./trans-iframe-eventsource');
 SockJS['iframe-htmlfile'] = require('./trans-iframe-htmlfile');
 SockJS['iframe-xhr-polling'] = require('./trans-iframe-xhr-polling');
 SockJS['jsonp-polling'] = require('./trans-jsonp-polling');
-SockJS['xdr-polling'] = require('./trans-xdr-polling');
+SockJS['xdr-polling'] = XDRPolling;
 SockJS['xdr-streaming'] = require('./trans-xdr-streaming');
 SockJS['xhr-polling'] = require('./trans-xhr-polling');
 SockJS['xhr-streaming'] = require('./trans-xhr-streaming');
@@ -374,18 +374,14 @@ function createInfoReceiver(base_url) {
         // need to start up the complex machinery. Just use ajax.
         return new InfoReceiver(base_url, XHRCorsObject);
     }
-    switch (utils.isXHRCorsCapable()) {
-    case 1:
+    if (utils.isXHRCorsCapable()) {
         // XHRLocalObject -> no_credentials=true
         return new InfoReceiver(base_url, XHRLocalObject);
-    case 2:
-        // IE 8/9 if the request target uses the same scheme
-        if (utils.isSameOriginScheme(base_url)) {
-            return new InfoReceiver(base_url, XDRObject);
-        }
-        break;
-    case 3:
-        // Opera
+    }
+    if (XDRPolling.enabled(base_url)) {
+        return new InfoReceiver(base_url, XDRObject);
+    }
+    if (IframeTransport.enabled()) {
         return new InfoReceiverIframe(base_url);
     }
 
diff --git a/lib/trans-xdr-streaming.js b/lib/trans-xdr-streaming.js
index 1d69965..2b0de41 100644
--- a/lib/trans-xdr-streaming.js
+++ b/lib/trans-xdr-streaming.js
@@ -10,6 +10,7 @@
 var AjaxBasedTransport = require('./ajax-based');
 var XhrReceiver = require('./trans-receiver-xhr');
 var XDRObject = require('./xdr');
+var utils = require('./utils');
 
 // According to:
 //   http://stackoverflow.com/questions/1641507/detect-browser-support-for-cross-domain-xmlhttprequests
@@ -21,9 +22,11 @@ function XdrStreamingTransport(ri, trans_url) {
 
 XdrStreamingTransport.prototype = new AjaxBasedTransport();
 
-XdrStreamingTransport.enabled = function() {
-    return !!window.XDomainRequest;
+XdrStreamingTransport.enabled = function(url) {
+    // IE 8/9 if the request target uses the same scheme - #79
+    return !!(window.XDomainRequest && document.domain && utils.isSameOriginScheme(url));
 };
+
 XdrStreamingTransport.roundTrips = 2; // preflight, ajax
 
 module.exports = XdrStreamingTransport;
\ No newline at end of file
diff --git a/lib/trans-xhr-polling.js b/lib/trans-xhr-polling.js
index 506eb09..1656908 100644
--- a/lib/trans-xhr-polling.js
+++ b/lib/trans-xhr-polling.js
@@ -18,8 +18,8 @@ function XhrPollingTransport(ri, trans_url) {
 
 XhrPollingTransport.prototype = new AjaxBasedTransport();
 
-XhrPollingTransport.enabled = function(options, isSameOrigin) {
-    if (window.XMLHttpRequest && isSameOrigin) return true;
+XhrPollingTransport.enabled = function(url) {
+    if (window.XMLHttpRequest && utils.isSameOriginUrl(url)) return true;
     return utils.isXHRCorsCapable() === 1;
 };
 
diff --git a/lib/trans-xhr-streaming.js b/lib/trans-xhr-streaming.js
index ad6cfc0..b51f16c 100644
--- a/lib/trans-xhr-streaming.js
+++ b/lib/trans-xhr-streaming.js
@@ -18,10 +18,10 @@ function XhrStreamingTransport(ri, trans_url) {
 
 XhrStreamingTransport.prototype = new AjaxBasedTransport();
 
-XhrStreamingTransport.enabled = function(options, isSameOrigin) {
+XhrStreamingTransport.enabled = function(url) {
     // Opera doesn't support xhr-streaming
     if (/opera/i.test(navigator.userAgent)) return false;
-    if (window.XMLHttpRequest && isSameOrigin) return true;
+    if (window.XMLHttpRequest && utils.isSameOriginUrl(url)) return true;
 
     return utils.isXHRCorsCapable() === 1;
 };
diff --git a/lib/utils.js b/lib/utils.js
index b504ea8..89b67e4 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -316,13 +316,13 @@ var _all_protocols = ['websocket',
                       'iframe-xhr-polling',
                       'jsonp-polling'];
 
-utils.probeProtocols = function() {
+utils.probeProtocols = function(url) {
     var probed = {};
     for(var i=0; i<_all_protocols.length; i++) {
         var protocol = _all_protocols[i];
         // User can have a typo in protocol name.
         probed[protocol] = SockJS[protocol] &&
-                           SockJS[protocol].enabled();
+                           SockJS[protocol].enabled(url);
     }
     return probed;
 };
@@ -382,9 +382,11 @@ utils.detectProtocols = function(probed, protocols_whitelist, info) {
 // 3. Nope, but postMessage is there so it should work via the Iframe.
 // 4. Nope, sorry.
 utils.isXHRCorsCapable = function() {
-    if (window.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest()) {
-        return 1;
-    }
+    try {
+        if (window.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest()) {
+            return 1;
+        }
+    } catch (ignored) {}
     // XDomainRequest doesn't work if page is served from file://
     if (window.XDomainRequest && document.domain) {
         return 2;
diff --git a/tests/html/lib/endtoendtests.js b/tests/html/lib/endtoendtests.js
index 213bca7..f687650 100644
--- a/tests/html/lib/endtoendtests.js
+++ b/tests/html/lib/endtoendtests.js
@@ -1,49 +1,11 @@
 'use strict';
-/* global expect, ok, QUnit, start, test, asyncTest, SockJS, equal, client_opts */
-var factory_body_check;
+/* global expect, ok, QUnit, start, asyncTest, equal */
 
 var u = require('../../../lib/utils');
 var testutils = require('./testutils');
 
 QUnit.module('End to End');
 
-factory_body_check = function(protocol) {
-  var n;
-  if (!SockJS[protocol] || !SockJS[protocol].enabled(client_opts.sockjs_opts)) {
-    n = " " + protocol + " [unsupported by client]";
-    test(n, function() {
-      u.log('Unsupported protocol (by client): "' + protocol + '"');
-    });
-  } else {
-    asyncTest(protocol, function() {
-      var code, hook, url;
-      expect(5);
-      url = client_opts.url + '/echo';
-      code = "hook.test_body(!!document.body, typeof document.body);\n\nvar sock = new SockJS('" + url + "', null,\n{protocols_whitelist:['" + protocol + "']});\nsock.onopen = function() {\n    var m = hook.onopen();\n    sock.send(m);\n};\nsock.onmessage = function(e) {\n    hook.onmessage(e.data);\n    sock.close();\n};";
-      hook = testutils.newIframe('sockjs-in-head.html');
-      hook.open = function() {
-        hook.iobj.loaded();
-        ok(true, 'open');
-        hook.callback(code);
-      };
-      hook.test_body = function(is_body, type) {
-        equal(is_body, false, 'body not yet loaded ' + type);
-      };
-      hook.onopen = function() {
-        ok(true, 'onopen');
-        return 'a';
-      };
-      hook.onmessage = function(m) {
-        equal(m, 'a');
-        ok(true, 'onmessage');
-        hook.iobj.cleanup();
-        hook.del();
-        start();
-      };
-    });
-  }
-};
-
 QUnit.module('connection errors');
 
 asyncTest("invalid url 404", function() {
diff --git a/tests/html/lib/tests.js b/tests/html/lib/tests.js
index dcae965..01d8da8 100644
--- a/tests/html/lib/tests.js
+++ b/tests/html/lib/tests.js
@@ -297,7 +297,7 @@ arrIndexOf = function(arr, obj) {
 
 test_protocol_messages = function(protocol) {
   QUnit.module(protocol);
-  if (!SockJS[protocol] || !SockJS[protocol].enabled()) {
+  if (!SockJS[protocol] || !SockJS[protocol].enabled(client_opts.url)) {
     test("[unsupported by client]", function() {
       ok(true, 'Unsupported protocol (by client): "' + protocol + '"');
     });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list