[Pkg-javascript-commits] [sockjs-client] 167/350: Better way to handle info receiver selection

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:04:08 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 354ad9e9faff497b1b2ae7abcf5cb83f8886613e
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Fri Oct 17 14:49:34 2014 -0400

    Better way to handle info receiver selection
---
 lib/info-receiver.js    | 35 ++++++++++++++++++-----------------
 lib/main.js             | 15 ++++++++++-----
 tests/lib/end-to-end.js |  9 +++++----
 3 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/lib/info-receiver.js b/lib/info-receiver.js
index c5fa93a..84004fb 100644
--- a/lib/info-receiver.js
+++ b/lib/info-receiver.js
@@ -2,8 +2,6 @@
 
 var EventEmitter = require('events').EventEmitter
   , util = require('util')
-  , urlUtils = require('./utils/url')
-  , loc = require('./polyfills/location')
   , XDR = require('./transport/sender/xdr')
   , XHRCors = require('./transport/sender/xhr-cors')
   , XHRLocal = require('./transport/sender/xhr-local')
@@ -13,13 +11,13 @@ var EventEmitter = require('events').EventEmitter
   , debug = require('debug')('sockjs-client:info')
   ;
 
-function InfoReceiver(baseUrl) {
+function InfoReceiver(baseUrl, urlInfo) {
   debug(baseUrl);
   var self = this;
   EventEmitter.call(this);
 
   process.nextTick(function(){
-    self.doXhr(baseUrl, InfoReceiver._getReceiver(baseUrl));
+    self.doXhr(baseUrl, urlInfo);
   });
 }
 
@@ -27,27 +25,30 @@ util.inherits(InfoReceiver, EventEmitter);
 
 // TODO this is currently ignoring the list of available transports and the whitelist
 
-InfoReceiver._getReceiver = function (baseUrl) {
+InfoReceiver._getReceiver = function (url, urlInfo) {
   // determine method of CORS support (if needed)
-  if (urlUtils.isOriginEqual(baseUrl, loc.href)) {
-    return XHRLocal;
-  } else if (XHRCors.enabled) {
-    return XHRCors;
-  } else if (XDR.enabled && urlUtils.isSchemeEqual(baseUrl, loc.href)) {
-    return XDR;
-  } else if (InfoIframe.enabled()) {
-    return null;
+  if (urlInfo.sameOrigin) {
+    return new InfoAjax(url, XHRLocal);
   }
-  return XHRFake;
+  if (XHRCors.enabled) {
+    return new InfoAjax(url, XHRCors);
+  }
+  if (XDR.enabled && urlInfo.sameScheme) {
+    return new InfoAjax(url, XDR);
+  }
+  if (InfoIframe.enabled()) {
+    return new InfoIframe(url);
+  }
+  return new InfoAjax(url, XHRFake);
 };
 
-InfoReceiver.prototype.doXhr = function(baseUrl, AjaxObject) {
-  debug('doXhr', baseUrl, AjaxObject.name);
+InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo) {
+  debug('doXhr', baseUrl);
   var self = this
     , url = baseUrl + '/info'
     ;
 
-  this.xo = AjaxObject ? new InfoAjax(url, AjaxObject) : new InfoIframe(url);
+  this.xo = InfoReceiver._getReceiver(url, urlInfo);
 
   this.timeoutRef = setTimeout(function() {
     debug('timeout');
diff --git a/lib/main.js b/lib/main.js
index 31dc59a..f525fe1 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -11,6 +11,7 @@ var u = require('url')
   , urlUtils = require('./utils/url')
   , eventUtils = require('./utils/event')
   , transport = require('./utils/transport')
+  , objectUtils = require('./utils/object')
   , SecurityError = require('./error/securityerror')
   , InvalidAccessError = require('./error/invalidaccesserror')
   , InvalidStateError = require('./error/invalidstateerror')
@@ -101,7 +102,13 @@ function SockJS(url, protocols, transportsWhitelist) {
   // Step 7 - start connection in background
   // obtain server info
   // http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html#section-26
-  this._ir = new InfoReceiver(this.url);
+  this._urlInfo = {
+    nullOrigin: global.document && !global.document.domain
+  , sameOrigin: urlUtils.isOriginEqual(this.url, loc.href)
+  , sameScheme: urlUtils.isSchemeEqual(this.url, loc.href)
+  };
+
+  this._ir = new InfoReceiver(this.url, this._urlInfo);
   this._ir.once('finish', this._receiveInfo.bind(this));
 }
 
@@ -132,7 +139,7 @@ SockJS.prototype.close = function(code, reason) {
 };
 
 SockJS.prototype.send = function(data) {
-  // TODO seems we should object WS spec and not allow anything but strings
+  // TODO seems we should uphold WS spec and not allow anything but strings
   if (typeof data !== 'string') {
     data = '' + data;
     //throw new TypeError('data must be a string');
@@ -166,9 +173,7 @@ SockJS.prototype._receiveInfo = function(info, rtt) {
   this._rto = this.countRTO(rtt);
   // allow server to override url used for the actual transport
   this._transUrl = info.base_url ? info.base_url : this.url;
-  info.nullOrigin = global.document && !global.document.domain;
-  info.sameOrigin = urlUtils.isOriginEqual(this.url, loc.href);
-  info.sameScheme = urlUtils.isSchemeEqual(this.url, loc.href);
+  info = objectUtils.extend(info, this._urlInfo);
   debug('info: ', info);
   // determine list of desired and supported transports
   var enabledTransports = transports.filterToEnabled(this._transportsWhitelist, info);
diff --git a/tests/lib/end-to-end.js b/tests/lib/end-to-end.js
index 977410f..793d798 100644
--- a/tests/lib/end-to-end.js
+++ b/tests/lib/end-to-end.js
@@ -2,8 +2,9 @@
 
 var expect = require('expect.js')
   , testUtils = require('./test-utils')
-  , InfoReceiver = require('../../lib/info-receiver')
-  , XHRFake = require('../../lib/transport/sender/xhr-fake')
+  , XDR = require('../../lib/transport/sender/xdr')
+  , XHRCors = require('../../lib/transport/sender/xhr-cors')
+  , InfoIframe = require('../../lib/info-iframe')
   ;
 
 describe('End to End', function () {
@@ -32,8 +33,8 @@ describe('End to End', function () {
       } else {
         badUrl = 'http://localhost:1079';
       }
-      // TODO this isn't a greay way to disable this test
-      if (InfoReceiver._getReceiver(badUrl) === XHRFake) {
+      // TODO this isn't a great way to disable this test
+      if (!XHRCors.enabled && !XDR.enabled && !InfoIframe.enabled()) {
         // CORS unsupported, won't actually hit info server
         done();
         return;

-- 
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