[Pkg-javascript-commits] [sockjs-client] 119/350: Cleanup info receiver and abstract underlying senders.

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:50 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 9d6acf81965b549a030b445ed0cfc8015c4073bc
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Fri Oct 10 17:37:40 2014 -0400

    Cleanup info receiver and abstract underlying senders.
---
 lib/info-ajax.js                                | 43 +++++++++++++++
 lib/{info-receiver-iframe.js => info-iframe.js} | 16 +++---
 lib/info-receiver.js                            | 69 ++++++++++---------------
 lib/transport/sender/xdr.js                     |  9 ++--
 lib/transport/xdr-streaming.js                  |  4 +-
 5 files changed, 88 insertions(+), 53 deletions(-)

diff --git a/lib/info-ajax.js b/lib/info-ajax.js
new file mode 100644
index 0000000..2248972
--- /dev/null
+++ b/lib/info-ajax.js
@@ -0,0 +1,43 @@
+'use strict';
+
+var EventEmitter = require('events').EventEmitter
+  , util = require('util')
+  , JSON3 = require('json3')
+  , objectUtils = require('./utils/object')
+  ;
+
+function InfoAjax(url, AjaxObject) {
+  EventEmitter.call(this);
+
+  var self = this;
+  var t0 = Date.now();
+  this.xo = new AjaxObject('GET', url);
+
+  this.xo.once('finish', function(status, text) {
+    var info, rtt;
+    if (status === 200) {
+      rtt = Date.now() - t0;
+      if (text) {
+        try {
+          info = JSON3.parse(text);
+        }
+        catch (e) {}
+      }
+
+      if (!objectUtils.isObject(info)) {
+        info = {};
+      }
+    }
+    self.emit('finish', info, rtt);
+    self.removeAllListeners();
+  });
+}
+
+util.inherits(InfoAjax, EventEmitter);
+
+InfoAjax.prototype.close = function () {
+  this.removeAllListeners();
+  this.xo.close();
+};
+
+module.exports = InfoAjax;
diff --git a/lib/info-receiver-iframe.js b/lib/info-iframe.js
similarity index 72%
rename from lib/info-receiver-iframe.js
rename to lib/info-iframe.js
index de74847..1d8865f 100644
--- a/lib/info-receiver-iframe.js
+++ b/lib/info-iframe.js
@@ -7,7 +7,7 @@ var EventEmitter = require('events').EventEmitter
   , IframeTransport = require('./transport/iframe')
   ;
 
-function InfoReceiverIframe(method, url) {
+function InfoIframe(url) {
   var self = this;
   EventEmitter.call(this);
 
@@ -23,13 +23,13 @@ function InfoReceiverIframe(method, url) {
       self.close();
     });
 
-    ifr.on('close', function () {
+    ifr.once('close', function () {
       self.emit('finish');
       self.close();
     });
   };
 
-  // this seems the same as the 'needBody' from transports
+  // TODO this seems the same as the 'needBody' from transports
   if (!global.document.body) {
     utils.attachEvent('load', go);
   } else {
@@ -37,9 +37,13 @@ function InfoReceiverIframe(method, url) {
   }
 }
 
-util.inherits(InfoReceiverIframe, EventEmitter);
+util.inherits(InfoIframe, EventEmitter);
 
-InfoReceiverIframe.prototype.close = function () {
+InfoIframe.enabled = function (url) {
+  return IframeTransport.enabled(url);
+};
+
+InfoIframe.prototype.close = function () {
   if (this.ifr) {
     this.ifr.close();
   }
@@ -47,4 +51,4 @@ InfoReceiverIframe.prototype.close = function () {
   this.ifr = null;
 };
 
-module.exports = InfoReceiverIframe;
+module.exports = InfoIframe;
diff --git a/lib/info-receiver.js b/lib/info-receiver.js
index 0b9baaf..80dc375 100644
--- a/lib/info-receiver.js
+++ b/lib/info-receiver.js
@@ -3,33 +3,20 @@
 var EventEmitter = require('events').EventEmitter
   , util = require('util')
   , origin = require('./utils/origin')
-  , JSON3 = require('json3')
   , loc = require('./polyfills/location')
+  , XDR = require('./transport/sender/xdr')
   , XHRCors = require('./transport/sender/xhr-cors')
   , XHRLocal = require('./transport/sender/xhr-local')
-  , XDR = require('./transport/sender/xdr')
   , XHRFake = require('./transport/sender/xhr-fake')
-  // it seems odd to include these just for the 'enabled' function
-  , XDRPolling = require('./transport/xdr-polling')
-  , IframeTransport = require('./transport/iframe')
-  , InfoIframe = require('./info-receiver-iframe')
+  , InfoIframe = require('./info-iframe')
+  , InfoAjax = require('./info-ajax')
   ;
 
 function InfoReceiver(baseUrl) {
   var self = this;
   EventEmitter.call(this);
 
-  var AjaxObject = XHRFake;
-  // determine method of CORS support (if needed)
-  if (origin.isSameOriginUrl(baseUrl, loc.href)) {
-    AjaxObject = XHRLocal;
-  } else if (XHRCors.enabled) {
-    AjaxObject = XHRCors;
-  } else if (XDRPolling.enabled(baseUrl)) {
-    AjaxObject = XDR;
-  } else if (IframeTransport.enabled()) {
-    AjaxObject = InfoIframe;
-  }
+  var AjaxObject = this._getReceiver(baseUrl);
 
   process.nextTick(function(){
     self.doXhr(baseUrl, AjaxObject);
@@ -38,37 +25,37 @@ function InfoReceiver(baseUrl) {
 
 util.inherits(InfoReceiver, EventEmitter);
 
+// FIXME this is currently ignoring the list of available transports and the whitelist
+
+InfoReceiver.prototype._getReceiver = function (baseUrl) {
+  // determine method of CORS support (if needed)
+  if (origin.isSameOriginUrl(baseUrl, loc.href)) {
+    return XHRLocal;
+  } else if (XHRCors.enabled) {
+    return XHRCors;
+  } else if (XDR.enabled && origin.isSameOriginScheme(baseUrl, loc.href)) {
+    return XDR;
+  } else if (InfoIframe.enabled(baseUrl)) {
+    return null;
+  }
+  return XHRFake;
+};
+
 InfoReceiver.prototype.doXhr = function(baseUrl, AjaxObject) {
-  var self = this;
-  var t0 = Date.now();
-  this.xo = new AjaxObject('GET', baseUrl + '/info');
+  var self = this
+    , url = baseUrl + '/info'
+    ;
+
+  this.xo = AjaxObject ? new InfoAjax(url, AjaxObject) : new InfoIframe(url);
 
   this.timeoutRef = setTimeout(function() {
-    self.xo.close();
+    self._cleanup(false);
     self.emit('finish');
   }, 8000);
 
-  this.xo.on('finish', function(status, text) {
+  this.xo.once('finish', function(info, rtt) {
     self._cleanup(true);
-    if (status === 200) {
-      var rtt = Date.now() - t0;
-      var info;
-      if (text) {
-        try {
-          info = JSON3.parse(text);
-        }
-        catch (e) {}
-      }
-      if (typeof info !== 'object') {
-        info = {};
-      }
-      self.emit('finish', info, rtt);
-    } else if (typeof status === 'object' && typeof text === 'number') {
-      // pass through data
-      self.emit('finish', status, text);
-    } else {
-      self.emit('finish');
-    }
+    self.emit('finish', info, rtt);
   });
 };
 
diff --git a/lib/transport/sender/xdr.js b/lib/transport/sender/xdr.js
index b87fc15..b49b13e 100644
--- a/lib/transport/sender/xdr.js
+++ b/lib/transport/sender/xdr.js
@@ -2,7 +2,7 @@
 
 var EventEmitter = require('events').EventEmitter
   , util = require('util')
-  , utils = require('../../utils/event')
+  , eventUtils = require('../../utils/event')
   ;
 
 // References:
@@ -38,7 +38,7 @@ XDRObject.prototype._start = function(method, url, payload) {
     self._cleanup(false);
   };
   this.xdr = xdr;
-  this.unloadRef = utils.unloadAdd(function(){
+  this.unloadRef = eventUtils.unloadAdd(function(){
     self._cleanup(true);
   });
   try {
@@ -54,7 +54,7 @@ XDRObject.prototype._cleanup = function(abort) {
   if (!this.xdr) {
     return;
   }
-  utils.unloadDel(this.unloadRef);
+  eventUtils.unloadDel(this.unloadRef);
 
   this.xdr.ontimeout = this.xdr.onerror = this.xdr.onprogress =
     this.xdr.onload = null;
@@ -71,4 +71,7 @@ XDRObject.prototype.close = function() {
   this._cleanup(true);
 };
 
+// IE 8/9 if the request target uses the same scheme - #79
+XDRObject.enabled = !!(global.XDomainRequest && global.document && global.document.domain);
+
 module.exports = XDRObject;
diff --git a/lib/transport/xdr-streaming.js b/lib/transport/xdr-streaming.js
index ee5c491..30d850f 100644
--- a/lib/transport/xdr-streaming.js
+++ b/lib/transport/xdr-streaming.js
@@ -22,9 +22,7 @@ XdrStreamingTransport.enabled = function(url, info) {
   if (info && (info.cookie_needed || info.nullOrigin)) {
     return false;
   }
-  // IE 8/9 if the request target uses the same scheme - #79
-  return !!(global.XDomainRequest && global.document &&
-    global.document.domain && origin.isSameOriginScheme(url, loc.href));
+  return XDRObject.enabled && origin.isSameOriginScheme(url, loc.href);
 };
 
 XdrStreamingTransport.transportName = 'xdr-streaming';

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