[Pkg-javascript-commits] [sockjs-client] 93/350: Add iframe bootstrap function back

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:46 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 ad35425123db12edb1914e1aa77c770469f77607
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Wed Oct 8 00:39:25 2014 -0400

    Add iframe bootstrap function back
---
 lib/facade.js                      | 15 +++++---
 lib/facade/info-receiver-iframe.js | 14 ++++++--
 lib/iframe-bootstrap.js            | 73 ++++++++++++++++++++++++++++++++++++++
 lib/info-receiver-iframe.js        |  7 ++--
 lib/main.js                        |  1 +
 lib/utils.js                       |  2 +-
 6 files changed, 98 insertions(+), 14 deletions(-)

diff --git a/lib/facade.js b/lib/facade.js
index d6ceb76..75e4904 100644
--- a/lib/facade.js
+++ b/lib/facade.js
@@ -2,17 +2,22 @@
 
 var utils = require('./utils');
 
-function FacadeJS() {}
-FacadeJS.prototype._didClose = function (code, reason) {
+function FacadeJS(transport) {
+  this._transport = transport;
+  transport.onmessage = this._transportMessage;
+  transport.onclose = this._transportClose;
+}
+
+FacadeJS.prototype._transportClose = function (code, reason) {
   utils.postMessage('t', utils.closeFrame(code, reason));
 };
-FacadeJS.prototype._didMessage = function (frame) {
+FacadeJS.prototype._transportMessage = function (frame) {
   utils.postMessage('t', frame);
 };
-FacadeJS.prototype._doSend = function (data) {
+FacadeJS.prototype._send = function (data) {
   this._transport.send(data);
 };
-FacadeJS.prototype._doCleanup = function () {
+FacadeJS.prototype._close = function () {
   this._transport.close();
 };
 
diff --git a/lib/facade/info-receiver-iframe.js b/lib/facade/info-receiver-iframe.js
index 6166f0e..fc5701c 100644
--- a/lib/facade/info-receiver-iframe.js
+++ b/lib/facade/info-receiver-iframe.js
@@ -3,16 +3,24 @@
 var XHRLocalObject = require('../xhr-local')
   , JSON3 = require('json3')
   , InfoReceiver = require('../info-receiver')
+  , util = require('util')
+  , EventTarget = require('../polyfills/eventtarget')
+  , TransportMessageEvent = require('../trans-message-event')
+  , CloseEvent = require('../closeevent')
   ;
 
-function WInfoReceiverIframe(ri, transUrl, baseUrl) {
+function WInfoReceiverIframe(transUrl, baseUrl) {
+  EventTarget.call(this);
+
   var ir = new InfoReceiver(baseUrl, XHRLocalObject);
   ir.on('finish', function(info, rtt) {
-    ri._didMessage('m' + JSON3.stringify([info, rtt]));
-    ri._didClose();
+    self.dispatchEvent(new TransportMessageEvent('m' + JSON3.stringify([info, rtt])));
+    self.dispatchEvent(new CloseEvent());
   });
 }
 
+util.inherits(WInfoReceiverIframe, EventTarget);
+
 WInfoReceiverIframe.prototype.close = function() {};
 
 module.exports = WInfoReceiverIframe;
diff --git a/lib/iframe-bootstrap.js b/lib/iframe-bootstrap.js
new file mode 100644
index 0000000..9e49649
--- /dev/null
+++ b/lib/iframe-bootstrap.js
@@ -0,0 +1,73 @@
+'use strict';
+
+var utils = require('./utils')
+  , JSON3 = require('json3')
+  , FacadeJS = require('./facade')
+  ;
+
+module.exports = function (SockJS) {
+  SockJS.bootstrap_iframe = function() {
+    var facade;
+    utils.curr_window_id = global.location.hash.slice(1);
+    var onMessage = function(e) {
+      if (e.source !== parent) {
+        return;
+      }
+      if (typeof utils.parent_origin === 'undefined') {
+        utils.parent_origin = e.origin;
+      }
+      if (e.origin !== utils.parent_origin) {
+        return;
+      }
+
+      var windowId = e.data.slice(0, 8);
+      var type = e.data.slice(8, 9);
+      var data = e.data.slice(9);
+      if (windowId !== utils.curr_window_id) {
+        return;
+      }
+      switch(type) {
+      case 's':
+        var p = JSON3.parse(data);
+        var version = p[0];
+        var protocol = p[1];
+        var transUrl = p[2];
+        var baseUrl = p[3];
+        // change this to semver logic
+        if (version !== SockJS.version) {
+          utils.log('Incompatibile SockJS! Main site uses:' +
+                    ' "' + version + '", the iframe:' +
+                    ' "' + SockJS.version + '".');
+        }
+        if (!utils.flatUrl(transUrl) || !utils.flatUrl(baseUrl)) {
+          utils.log('Only basic urls are supported in SockJS');
+          return;
+        }
+
+        if (!utils.isSameOriginUrl(transUrl) ||
+            !utils.isSameOriginUrl(baseUrl)) {
+          utils.log('Can\'t connect to different domain from within an ' +
+                    'iframe. (' + JSON3.stringify([global.location.href, transUrl, baseUrl]) +
+                    ')');
+          return;
+        }
+        facade = new FacadeJS(new FacadeJS[protocol](transUrl, baseUrl));
+        break;
+      case 'm':
+        facade._send(data);
+        break;
+      case 'c':
+        if (facade) {
+          facade._close();
+        }
+        facade = null;
+        break;
+      }
+    };
+
+    utils.attachMessage(onMessage);
+
+    // Start
+    utils.postMessage('s');
+  };
+};
diff --git a/lib/info-receiver-iframe.js b/lib/info-receiver-iframe.js
index 61780de..86939b5 100644
--- a/lib/info-receiver-iframe.js
+++ b/lib/info-receiver-iframe.js
@@ -25,11 +25,8 @@ function InfoReceiverIframe(baseUrl) {
       ifr.doCleanup();
       ifr = null;
     };
-    var mockRi = {
-      _didClose: fun,
-      _didMessage: fun
-    };
-    ifr.start(mockRi, baseUrl, baseUrl);
+    ifr.onmessage = ifr.onclose = fun;
+    ifr.start(baseUrl, baseUrl);
   };
   if (!global.document.body) {
     utils.attachEvent('load', go);
diff --git a/lib/main.js b/lib/main.js
index 610fb74..df6b245 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -261,5 +261,6 @@ SockJS.prototype._close = function(code, reason, wasClean) {
 
 module.exports = function (availableTransports) {
   transports = require('./transports')(availableTransports);
+  require('./iframe-bootstrap')(SockJS);
   return SockJS;
 };
diff --git a/lib/utils.js b/lib/utils.js
index 2644a51..c791c04 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -359,7 +359,7 @@ utils.attachEvent = function(event, listener) {
     // IE quirks.
     // According to: http://stevesouders.com/misc/test-postmessage.php
     // the message gets delivered only to 'document', not 'window'.
-    document.attachEvent('on' + event, listener);
+    global.document.attachEvent('on' + event, listener);
     // I get 'window' for ie8.
     global.attachEvent('on' + event, listener);
   }

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