[Pkg-javascript-commits] [sockjs-client] 130/350: Cleanup info iframe.

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:54 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 9c9604f52d5208a5b16a7b1c10a337a962f5947d
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Mon Oct 13 13:06:02 2014 -0400

    Cleanup info iframe.
---
 lib/facade.js                                |  2 +-
 lib/iframe-bootstrap.js                      | 19 +++++++---------
 lib/info-iframe-receiver.js                  | 32 ++++++++++++++++++++++++++
 lib/info-iframe.js                           |  3 ++-
 lib/info-receiver.js                         |  6 ++---
 lib/main.js                                  | 23 +++++++++++--------
 lib/transport-list.js                        | 17 +++++---------
 lib/transport/facade/info-receiver-iframe.js | 26 ---------------------
 lib/transport/iframe.js                      | 28 +++++++++++------------
 lib/transport/lib/iframe-utils.js            | 13 -----------
 lib/transport/websocket.js                   | 16 +++++++++----
 lib/transports.js                            | 32 --------------------------
 lib/utils/iframe.js                          | 17 +++++++++++++-
 lib/utils/transport.js                       | 34 ++++++++++++++++++++++++++++
 14 files changed, 140 insertions(+), 128 deletions(-)

diff --git a/lib/facade.js b/lib/facade.js
index f02703a..d12a28b 100644
--- a/lib/facade.js
+++ b/lib/facade.js
@@ -1,7 +1,7 @@
 'use strict';
 
 var frameDefs = require('./transport/lib/frames')
-  , iframeUtils = require('./transport/lib/iframe-utils')
+  , iframeUtils = require('./utils/iframe')
   ;
 
 function FacadeJS(transport) {
diff --git a/lib/iframe-bootstrap.js b/lib/iframe-bootstrap.js
index ee8ad8f..9bd0642 100644
--- a/lib/iframe-bootstrap.js
+++ b/lib/iframe-bootstrap.js
@@ -4,8 +4,8 @@ var originUtils = require('./utils/origin')
   , eventUtils = require('./utils/event')
   , JSON3 = require('json3')
   , FacadeJS = require('./facade')
-  , InfoIframeFacade = require('./transport/facade/info-receiver-iframe')
-  , iframeUtils = require('./transport/lib/iframe-utils')
+  , InfoIframeReceiver = require('./info-receiver-iframe')
+  , iframeUtils = require('./utils/iframe')
   , loc = require('./polyfills/location')
   ;
 
@@ -17,7 +17,7 @@ module.exports = function (SockJS, facadeTransports) {
 
   // hard-coded for the info iframe
   // TODO see if we can make this more dynamic
-  transportMap[InfoIframeFacade.transportName] = InfoIframeFacade;
+  transportMap[InfoIframeReceiver.transportName] = InfoIframeReceiver;
   var parentOrigin;
 
   /* eslint-disable camelcase */
@@ -35,16 +35,13 @@ module.exports = function (SockJS, facadeTransports) {
       if (e.origin !== parentOrigin) {
         return;
       }
-
-      var windowId = e.data.slice(0, 8);
-      var type = e.data.slice(8, 9);
-      var data = e.data.slice(9);
-      if (windowId !== iframeUtils.currentWindowId) {
+      var iframeMessage = JSON3.parse(e.data);
+      if (iframeMessage.windowId !== iframeUtils.currentWindowId) {
         return;
       }
-      switch(type) {
+      switch (iframeMessage.type) {
       case 's':
-        var p = JSON3.parse(data);
+        var p = JSON3.parse(iframeMessage.data);
         var version = p[0];
         var transport = p[1];
         var transUrl = p[2];
@@ -65,7 +62,7 @@ module.exports = function (SockJS, facadeTransports) {
         facade = new FacadeJS(new transportMap[transport](transUrl, baseUrl));
         break;
       case 'm':
-        facade._send(data);
+        facade._send(iframeMessage.data);
         break;
       case 'c':
         if (facade) {
diff --git a/lib/info-iframe-receiver.js b/lib/info-iframe-receiver.js
new file mode 100644
index 0000000..fb674e2
--- /dev/null
+++ b/lib/info-iframe-receiver.js
@@ -0,0 +1,32 @@
+'use strict';
+
+var util = require('util')
+  , EventEmitter = require('events').EventEmitter
+  , JSON3 = require('json3')
+  , XHRLocalObject = require('./transport/sender/xhr-local')
+  , InfoAjax = require('./info-ajax')
+  ;
+
+function InfoReceiverIframe(transUrl, baseUrl) {
+  var self = this;
+  EventEmitter.call(this);
+
+  this.ir = new InfoAjax(baseUrl, XHRLocalObject);
+  this.ir.once('finish', function(info, rtt) {
+    self.ir = null;
+    self.emit('message', JSON3.stringify([info, rtt]));
+  });
+}
+
+util.inherits(InfoReceiverIframe, EventEmitter);
+
+InfoReceiverIframe.transportName = 'iframe-info-receiver';
+
+InfoReceiverIframe.prototype.close = function() {
+  if (this.ir) {
+    this.ir.close();
+    this.ir = null;
+  }
+};
+
+module.exports = InfoReceiverIframe;
diff --git a/lib/info-iframe.js b/lib/info-iframe.js
index b06231c..67b69dd 100644
--- a/lib/info-iframe.js
+++ b/lib/info-iframe.js
@@ -5,6 +5,7 @@ var EventEmitter = require('events').EventEmitter
   , JSON3 = require('json3')
   , utils = require('./utils/event')
   , IframeTransport = require('./transport/iframe')
+  , InfoReceiverIframe = require('./info-iframe-receiver')
   ;
 
 function InfoIframe(url) {
@@ -12,7 +13,7 @@ function InfoIframe(url) {
   EventEmitter.call(this);
 
   var go = function() {
-    var ifr = self.ifr = new IframeTransport('w-iframe-info-receiver', url, url);
+    var ifr = self.ifr = new IframeTransport(InfoReceiverIframe.transportName, url, url);
 
     ifr.once('message', function (msg) {
       if (msg) {
diff --git a/lib/info-receiver.js b/lib/info-receiver.js
index 80dc375..d054738 100644
--- a/lib/info-receiver.js
+++ b/lib/info-receiver.js
@@ -16,16 +16,14 @@ function InfoReceiver(baseUrl) {
   var self = this;
   EventEmitter.call(this);
 
-  var AjaxObject = this._getReceiver(baseUrl);
-
   process.nextTick(function(){
-    self.doXhr(baseUrl, AjaxObject);
+    self.doXhr(baseUrl, self._getReceiver(baseUrl));
   });
 }
 
 util.inherits(InfoReceiver, EventEmitter);
 
-// FIXME this is currently ignoring the list of available transports and the whitelist
+// TODO this is currently ignoring the list of available transports and the whitelist
 
 InfoReceiver.prototype._getReceiver = function (baseUrl) {
   // determine method of CORS support (if needed)
diff --git a/lib/main.js b/lib/main.js
index 4ad023c..14ac3c1 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -9,6 +9,7 @@ var u = require('url')
   , escape = require('./utils/escape')
   , origin = require('./utils/origin')
   , eventUtils = require('./utils/event')
+  , transport = require('./utils/transport')
   , SecurityError = require('./error/securityerror')
   , InvalidAccessError = require('./error/invalidaccesserror')
   , InvalidStateError = require('./error/invalidstateerror')
@@ -152,7 +153,7 @@ SockJS.prototype._receiveInfo = function(info, rtt) {
   info.nullOrigin = global.document && !global.document.domain;
   info.sameOrigin = origin.isSameOriginUrl(this.url, loc.href);
   // determine list of desired and supported transports
-  var enabledTransports = transports(this.url, this._transportsWhitelist, info);
+  var enabledTransports = transports.filterToAvailable(this.url, this._transportsWhitelist, info);
   this._transports = enabledTransports.main;
 
   // setup iframe bootstrap
@@ -222,6 +223,16 @@ SockJS.prototype._transportMessage = function(msg) {
 };
 
 SockJS.prototype._transportClose = function(code, reason) {
+  if (this._transport) {
+    this._transport.removeAllListeners();
+    this._transport = null;
+  }
+
+  if (!userSetCode(code) && code !== 2000 && this.readyState === SockJS.CONNECTING) {
+    this._connect();
+    return;
+  }
+
   this._close(code, reason);
 };
 
@@ -256,9 +267,8 @@ SockJS.prototype._close = function(code, reason, wasClean) {
     this._ir = null;
   }
   if (this._transport) {
-    this._transport.close();
-    // strictly not necessary because each transport should do this, but good cleanup
     this._transport.removeAllListeners();
+    this._transport.close();
     this._transport = null;
   }
 
@@ -266,11 +276,6 @@ SockJS.prototype._close = function(code, reason, wasClean) {
     throw new InvalidStateError('SockJS has already been closed');
   }
 
-  if (!userSetCode(code) && code !== 2000 && this.readyState === SockJS.CONNECTING) {
-    this._connect();
-    return;
-  }
-
   this.readyState = SockJS.CLOSING;
   process.nextTick(function () {
     this.readyState = SockJS.CLOSED;
@@ -305,6 +310,6 @@ SockJS.prototype.countRTO = function (rtt) {
 };
 
 module.exports = function (availableTransports) {
-  transports = require('./transports')(availableTransports);
+  transports = transport(availableTransports);
   return SockJS;
 };
diff --git a/lib/transport-list.js b/lib/transport-list.js
index 985aa32..c260421 100644
--- a/lib/transport-list.js
+++ b/lib/transport-list.js
@@ -1,23 +1,18 @@
 'use strict';
 
-var EventSourceTransport = require('./transport/eventsource')
-  , HtmlFileTransport = require('./transport/htmlfile')
-  , XhrPollingTransport = require('./transport/xhr-polling')
-  ;
-
 module.exports = [
   // streaming transports
   require('./transport/websocket')
 , require('./transport/xdr-streaming')
 , require('./transport/xhr-streaming')
-, EventSourceTransport
-, require('./transport/lib/iframe-wrap')(EventSourceTransport)
+, require('./transport/eventsource')
+, require('./transport/lib/iframe-wrap')(require('./transport/eventsource'))
 
   // polling transports
-, HtmlFileTransport
-, require('./transport/lib/iframe-wrap')(HtmlFileTransport)
+, require('./transport/htmlfile')
+, require('./transport/lib/iframe-wrap')(require('./transport/htmlfile'))
 , require('./transport/xdr-polling')
-, XhrPollingTransport
-, require('./transport/lib/iframe-wrap')(XhrPollingTransport)
+, require('./transport/xhr-polling')
+, require('./transport/lib/iframe-wrap')(require('./transport/xhr-polling'))
 , require('./transport/jsonp-polling')
 ];
diff --git a/lib/transport/facade/info-receiver-iframe.js b/lib/transport/facade/info-receiver-iframe.js
deleted file mode 100644
index 9a83e15..0000000
--- a/lib/transport/facade/info-receiver-iframe.js
+++ /dev/null
@@ -1,26 +0,0 @@
-'use strict';
-
-var XHRLocalObject = require('../sender/xhr-local')
-  , JSON3 = require('json3')
-  , InfoReceiver = require('../../info-receiver')
-  , util = require('util')
-  , EventEmitter = require('events').EventEmitter
-  ;
-
-function WInfoReceiverIframe(transUrl, baseUrl) {
-  var self = this;
-  EventEmitter.call(this);
-
-  var ir = new InfoReceiver(baseUrl, XHRLocalObject);
-  ir.once('finish', function(info, rtt) {
-    self.emit('message', JSON3.stringify([info, rtt]));
-  });
-}
-
-util.inherits(WInfoReceiverIframe, EventEmitter);
-
-WInfoReceiverIframe.transportName = 'w-iframe-info-receiver';
-
-WInfoReceiverIframe.prototype.close = function() {};
-
-module.exports = WInfoReceiverIframe;
diff --git a/lib/transport/iframe.js b/lib/transport/iframe.js
index 550992e..c53ae9e 100644
--- a/lib/transport/iframe.js
+++ b/lib/transport/iframe.js
@@ -26,14 +26,9 @@ function IframeTransport(transport, transUrl, baseUrl) {
   this.baseUrl = baseUrl;
   this.transUrl = transUrl;
   this.transport = transport;
-
-  var iframeUrl = baseUrl + '/iframe.html';
-  // TODO figure out how to get this info again
-  // if (this.ri._devel) {
-  //   iframeUrl += '?t=' + Date.now();
-  // }
   this.windowId = random.string(8);
-  iframeUrl += '#' + this.windowId;
+
+  var iframeUrl = baseUrl + '/iframe.html#' + this.windowId;
 
   this.iframeObj = iframeUtils.createIframe(iframeUrl, function(r) {
     self.emit('close', 1006, 'Unable to load an iframe (' + r + ')');
@@ -65,16 +60,14 @@ IframeTransport.prototype._message = function(e) {
   if (!originUtils.isSameOriginUrl(e.origin, this.origin)) {
     return;
   }
-  var windowId = e.data.slice(0, 8);
-  var type = e.data.slice(8, 9);
-  var data = e.data.slice(9);
+  var iframeMessage = JSON3.parse(e.data);
 
-  if (windowId !== this.windowId) {
+  if (iframeMessage.windowId !== this.windowId) {
     console.log('Mismatched window id');
     return;
   }
 
-  switch(type) {
+  switch (iframeMessage.type) {
   case 's':
     this.iframeObj.loaded();
     // window global dependency
@@ -86,13 +79,17 @@ IframeTransport.prototype._message = function(e) {
     ]));
     break;
   case 't':
-    this.emit('message', data);
+    this.emit('message', iframeMessage.data);
     break;
   }
 };
 
 IframeTransport.prototype.postMessage = function(type, data) {
-  this.iframeObj.post(this.windowId + type + (data || ''), this.origin);
+  this.iframeObj.post(JSON3.stringify({
+    windowId: this.windowId
+  , type: type
+  , data: data || ''
+  }), this.origin);
 };
 
 IframeTransport.prototype.send = function (message) {
@@ -100,6 +97,9 @@ IframeTransport.prototype.send = function (message) {
 };
 
 IframeTransport.enabled = function() {
+  if (!global.document) {
+    return false;
+  }
   // postMessage misbehaves in konqueror 4.6.5 - the messages are delivered with
   // huge delay, or not at all.
   return ((typeof global.postMessage === 'function' ||
diff --git a/lib/transport/lib/iframe-utils.js b/lib/transport/lib/iframe-utils.js
deleted file mode 100644
index b724cb3..0000000
--- a/lib/transport/lib/iframe-utils.js
+++ /dev/null
@@ -1,13 +0,0 @@
-'use strict';
-
-// currentWindowId comes from SockJS.bootstrap_iframe()
-module.exports = {
-  currentWindowId: null
-, postMessage: function (type, data) {
-    if (global.parent !== global) {
-      global.parent.postMessage(this.currentWindowId + type + (data || ''), '*');
-    } else {
-      console.log('Cannot postMessage, no parent window.', type, data);
-    }
-  }
-};
diff --git a/lib/transport/websocket.js b/lib/transport/websocket.js
index d68518e..ef5edf6 100644
--- a/lib/transport/websocket.js
+++ b/lib/transport/websocket.js
@@ -33,11 +33,11 @@ function WebSocketTransport(transUrl) {
   });
   this.ws.onclose = function(e) {
     self.emit('close', e.code, e.reason);
-    self.removeAllListeners();
+    self._cleanup();
   };
   this.ws.onerror = function() {
     self.emit('close', 1006, 'WebSocket connection broken');
-    self.removeAllListeners();
+    self._cleanup();
   };
 }
 
@@ -48,13 +48,19 @@ WebSocketTransport.prototype.send = function(data) {
 };
 
 WebSocketTransport.prototype.close = function() {
+  if (this.ws) {
+    this.ws.close();
+  }
+  this._cleanup();
+};
+
+WebSocketTransport.prototype._cleanup = function () {
   var ws = this.ws;
   if (ws) {
     ws.onmessage = ws.onclose = ws.onerror = null;
-    ws.close();
-    utils.unloadDel(this.unloadRef);
-    this.unloadRef = this.ws = null;
   }
+  utils.unloadDel(this.unloadRef);
+  this.unloadRef = this.ws = null;
   this.removeAllListeners();
 };
 
diff --git a/lib/transports.js b/lib/transports.js
deleted file mode 100644
index 0364b0c..0000000
--- a/lib/transports.js
+++ /dev/null
@@ -1,32 +0,0 @@
-'use strict';
-
-module.exports = function (availableTransports) {
-  return function (url, transportsWhitelist, info) {
-    var transports = {
-      main: []
-    , facade: []
-    };
-    if (!transportsWhitelist) {
-      transportsWhitelist = [];
-    }
-
-    availableTransports.forEach(function (trans) {
-      if (!trans) {
-        return;
-      }
-
-      if (transportsWhitelist.length &&
-          transportsWhitelist.indexOf(trans.transportName) === -1) {
-        return;
-      }
-
-      if (trans.enabled(url, info)) {
-        transports.main.push(trans);
-        if (trans.facadeTransport) {
-          transports.facade.push(trans.facadeTransport);
-        }
-      }
-    });
-    return transports;
-  };
-};
diff --git a/lib/utils/iframe.js b/lib/utils/iframe.js
index 0ab1f7d..39efb94 100644
--- a/lib/utils/iframe.js
+++ b/lib/utils/iframe.js
@@ -1,9 +1,12 @@
 'use strict';
 
-var eventUtils = require('./event');
+var eventUtils = require('./event')
+  , JSON3 = require('json3')
+  ;
 
 module.exports = {
   WPrefix: '_jp'
+, currentWindowId: null
 
 , polluteGlobalNamespace: function() {
     if (!(this.WPrefix in global)) {
@@ -11,6 +14,18 @@ module.exports = {
     }
   }
 
+, postMessage: function (type, data) {
+    if (global.parent !== global) {
+      global.parent.postMessage(JSON3.stringify({
+        windowId: this.currentWindowId
+      , type: type
+      , data: data || ''
+      }), '*');
+    } else {
+      console.log('Cannot postMessage, no parent window.', type, data);
+    }
+  }
+
 , createIframe: function (iframeUrl, errorCallback) {
     var iframe = global.document.createElement('iframe');
     var tref, unloadRef;
diff --git a/lib/utils/transport.js b/lib/utils/transport.js
new file mode 100644
index 0000000..14ca2d7
--- /dev/null
+++ b/lib/utils/transport.js
@@ -0,0 +1,34 @@
+'use strict';
+
+module.exports = function (availableTransports) {
+  return {
+    filterToEnabled: function (url, transportsWhitelist, info) {
+      var transports = {
+        main: []
+      , facade: []
+      };
+      if (!transportsWhitelist) {
+        transportsWhitelist = [];
+      }
+
+      availableTransports.forEach(function (trans) {
+        if (!trans) {
+          return;
+        }
+
+        if (transportsWhitelist.length &&
+            transportsWhitelist.indexOf(trans.transportName) === -1) {
+          return;
+        }
+
+        if (trans.enabled(url, info)) {
+          transports.main.push(trans);
+          if (trans.facadeTransport) {
+            transports.facade.push(trans.facadeTransport);
+          }
+        }
+      });
+      return transports;
+    }
+  };
+};

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