[Pkg-javascript-commits] [sockjs-client] 103/350: Make things happy again

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:47 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 31d3ed6737026d8a977917369b70c6d3862f5721
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Thu Oct 9 20:34:40 2014 -0400

    Make things happy again
---
 lib/info-receiver-iframe.js        |   2 +-
 lib/info-receiver.js               |   4 +-
 lib/main.js                        |   2 +-
 lib/transport/driver/xhr.js        |   6 +-
 lib/transport/lib/iframe.js        |  14 ++--
 lib/transport/receiver/htmlfile.js |   2 +-
 lib/transport/receiver/jsonp.js    |  11 ++--
 lib/transport/sender/jsonp.js      |   6 +-
 lib/transport/sender/xdr.js        |   2 +-
 lib/transport/websocket.js         |   2 +-
 lib/transport/xdr-streaming.js     |   2 +-
 lib/transport/xhr-polling.js       |   2 +-
 lib/transport/xhr-streaming.js     |   2 +-
 lib/utils/event.js                 |  58 +++++++++--------
 lib/utils/iframe.js                | 128 +++++++++++++++++++------------------
 lib/utils/log.js                   |   8 ++-
 tests/receivers.js                 |   2 +-
 17 files changed, 134 insertions(+), 119 deletions(-)

diff --git a/lib/info-receiver-iframe.js b/lib/info-receiver-iframe.js
index a537f3c..f3aa2a6 100644
--- a/lib/info-receiver-iframe.js
+++ b/lib/info-receiver-iframe.js
@@ -3,7 +3,7 @@
 var EventEmitter = require('events').EventEmitter
   , util = require('util')
   , JSON3 = require('json3')
-  , utils = require('./utils')
+  , utils = require('./utils/event')
   , IframeTransport = require('./transport/lib/iframe')
   ;
 
diff --git a/lib/info-receiver.js b/lib/info-receiver.js
index 9c542f1..6a48fb1 100644
--- a/lib/info-receiver.js
+++ b/lib/info-receiver.js
@@ -2,7 +2,7 @@
 
 var EventEmitter = require('events').EventEmitter
   , util = require('util')
-  , utils = require('./utils')
+  , utils = require('./utils/origin')
   , JSON3 = require('json3')
   , loc = require('./polyfills/location')
   , XHRCors = require('./transport/sender/xhr-cors')
@@ -23,7 +23,7 @@ function InfoReceiver(baseUrl) {
   // determine method of CORS support (if needed)
   if (utils.isSameOriginUrl(baseUrl, loc.href)) {
     AjaxObject = XHRLocal;
-  } else if (XHRCors.capable()) {
+  } else if (XHRCors.enabled) {
     AjaxObject = XHRCors;
   } else if (XDRPolling.enabled(baseUrl)) {
     AjaxObject = XDR;
diff --git a/lib/main.js b/lib/main.js
index b324879..3f445d0 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -86,7 +86,7 @@ function SockJS(url, protocols, transportsWhitelist) {
   parsedUrl.pathname = parsedUrl.pathname.replace(/[/]+$/, '');
 
   // store the sanitized url
-  this.url = url.format(parsedUrl);
+  this.url = u.format(parsedUrl);
 
   // Step 7 - start connection in background
   // obtain server info
diff --git a/lib/transport/driver/xhr.js b/lib/transport/driver/xhr.js
index 87b4580..e459e97 100644
--- a/lib/transport/driver/xhr.js
+++ b/lib/transport/driver/xhr.js
@@ -37,7 +37,9 @@ function XhrDriver(method, url, payload, opts) {
     self.emit('finish', 0, e.message);
   });
 
-  this.req.write(payload);
+  if (payload) {
+    this.req.write(payload);
+  }
   this.req.end();
 }
 
@@ -54,4 +56,4 @@ XhrDriver.prototype.close = function() {
 XhrDriver.enabled = true;
 XhrDriver.supportsCORS = true;
 
-module.exports = XhrDriver;
\ No newline at end of file
+module.exports = XhrDriver;
diff --git a/lib/transport/lib/iframe.js b/lib/transport/lib/iframe.js
index c278f10..6ac4442 100644
--- a/lib/transport/lib/iframe.js
+++ b/lib/transport/lib/iframe.js
@@ -13,7 +13,9 @@ var util = require('util')
   , CloseEvent = require('./closeevent')
   , TransportMessageEvent = require('./trans-message-event')
   , JSON3 = require('json3')
-  , utils = require('../../utils')
+  , originUtils = require('../../utils/origin')
+  , iframeUtils = require('../../utils/iframe')
+  , eventUtils = require('../../utils/event')
   , random = require('../../utils/random')
   ;
 
@@ -21,7 +23,7 @@ function IframeTransport(transport, transUrl, baseUrl) {
   EventTarget.call(this);
 
   var self = this;
-  this.origin = utils.getOrigin(baseUrl);
+  this.origin = originUtils.getOrigin(baseUrl);
   this.baseUrl = baseUrl;
   this.transUrl = transUrl;
   this.transport = transport;
@@ -34,7 +36,7 @@ function IframeTransport(transport, transUrl, baseUrl) {
   this.windowId = random.string(8);
   iframeUrl += '#' + this.windowId;
 
-  this.iframeObj = utils.createIframe(iframeUrl, function(r) {
+  this.iframeObj = iframeUtils.createIframe(iframeUrl, function(r) {
     var closeEvent = new CloseEvent();
     closeEvent.code = 1006;
     closeEvent.reason = 'Unable to load an iframe (' + r + ')';
@@ -42,14 +44,14 @@ function IframeTransport(transport, transUrl, baseUrl) {
   });
 
   this.onmessageCallback = this._message.bind(this);
-  utils.attachMessage(this.onmessageCallback);
+  eventUtils.attachMessage(this.onmessageCallback);
 }
 
 util.inherits(IframeTransport, EventTarget);
 
 IframeTransport.prototype.close = function() {
   if (this.iframeObj) {
-    utils.detachMessage(this.onmessageCallback);
+    eventUtils.detachMessage(this.onmessageCallback);
     try {
       // When the iframe is not loaded, IE raises an exception
       // on 'contentWindow'.
@@ -62,7 +64,7 @@ IframeTransport.prototype.close = function() {
 };
 
 IframeTransport.prototype._message = function(e) {
-  if (!utils.isSameOriginUrl(e.origin, this.origin)) {
+  if (!originUtils.isSameOriginUrl(e.origin, this.origin)) {
     return;
   }
   var windowId = e.data.slice(0, 8);
diff --git a/lib/transport/receiver/htmlfile.js b/lib/transport/receiver/htmlfile.js
index e23dc54..db91d85 100644
--- a/lib/transport/receiver/htmlfile.js
+++ b/lib/transport/receiver/htmlfile.js
@@ -1,7 +1,7 @@
 'use strict';
 
 var util = require('util')
-  , utils = require('../../utils')
+  , utils = require('../../utils/iframe')
   , SimpleEvent = require('../../simpleevent')
   , EventTarget = require('../../polyfills/eventtarget')
   , random = require('../../utils/random')
diff --git a/lib/transport/receiver/jsonp.js b/lib/transport/receiver/jsonp.js
index a6f5a72..b5d5385 100644
--- a/lib/transport/receiver/jsonp.js
+++ b/lib/transport/receiver/jsonp.js
@@ -1,6 +1,7 @@
 'use strict';
 
-var utils = require('../../utils')
+var utils = require('../../utils/iframe')
+  , random = require('../../utils/random')
   , util = require('util')
   , SimpleEvent = require('../../simpleevent')
   , EventTarget = require('../../polyfills/eventtarget')
@@ -12,7 +13,7 @@ function JsonpReceiver(url) {
 
   utils.polluteGlobalNamespace();
 
-  this.id = 'a' + utils.randomString(6);
+  this.id = 'a' + random.string(6);
   var urlWithId = url + '?c=' + encodeURIComponent(utils.WPrefix + '.' + this.id);
 
   global[utils.WPrefix][this.id] = this._callback.bind(this);
@@ -40,7 +41,7 @@ JsonpReceiver.scriptErrorTimeout = 1000;
 JsonpReceiver.prototype._callback = function (data) {
   this._cleanup();
   delete global[utils.WPrefix][this.id];
-  
+
   if (this.aborting) {
     return;
   }
@@ -78,7 +79,9 @@ JsonpReceiver.prototype._cleanup = function () {
 
 JsonpReceiver.prototype._scriptError = function () {
   var self = this;
-  if (this.errorTimer) return;
+  if (this.errorTimer) {
+    return;
+  }
 
   this.errorTimer = setTimeout(function() {
     if (!self.loadedOkay) {
diff --git a/lib/transport/sender/jsonp.js b/lib/transport/sender/jsonp.js
index 75a5bce..245ed46 100644
--- a/lib/transport/sender/jsonp.js
+++ b/lib/transport/sender/jsonp.js
@@ -1,6 +1,8 @@
 'use strict';
 
-var utils = require('../../utils');
+var random = require('../../utils/random')
+  , utils = require('../../utils/log')
+  ;
 
 module.exports = function (url, payload, callback) {
   var form = window._sendForm;
@@ -18,7 +20,7 @@ module.exports = function (url, payload, callback) {
     form.appendChild(area);
     document.body.appendChild(form);
   }
-  var id = 'a' + utils.randomString(8);
+  var id = 'a' + random.string(8);
   form.target = id;
   form.action = url + '/jsonp_send?i=' + id;
 
diff --git a/lib/transport/sender/xdr.js b/lib/transport/sender/xdr.js
index fd5bd32..b87fc15 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')
+  , utils = require('../../utils/event')
   ;
 
 // References:
diff --git a/lib/transport/websocket.js b/lib/transport/websocket.js
index 166ac3f..154e761 100644
--- a/lib/transport/websocket.js
+++ b/lib/transport/websocket.js
@@ -1,6 +1,6 @@
 'use strict';
 
-var utils = require('../utils')
+var utils = require('../utils/event')
   , util = require('util')
   , EventTarget = require('../polyfills/eventtarget')
   , TransportMessageEvent = require('./lib/trans-message-event')
diff --git a/lib/transport/xdr-streaming.js b/lib/transport/xdr-streaming.js
index 2c934de..8c42d82 100644
--- a/lib/transport/xdr-streaming.js
+++ b/lib/transport/xdr-streaming.js
@@ -4,7 +4,7 @@ var util = require('util')
   , AjaxBasedTransport = require('./lib/ajax-based')
   , XhrReceiver = require('./receiver/xhr')
   , XDRObject = require('./sender/xdr')
-  , utils = require('../utils')
+  , utils = require('../utils/origin')
   , loc = require('../polyfills/location')
   ;
 
diff --git a/lib/transport/xhr-polling.js b/lib/transport/xhr-polling.js
index b5d602d..cbc2fcd 100644
--- a/lib/transport/xhr-polling.js
+++ b/lib/transport/xhr-polling.js
@@ -5,7 +5,7 @@ var util = require('util')
   , XhrReceiver = require('./receiver/xhr')
   , XHRCorsObject = require('./sender/xhr-cors')
   , XHRLocalObject = require('./sender/xhr-local')
-  , utils = require('../utils')
+  , utils = require('../utils/origin')
   , loc = require('../polyfills/location')
   ;
 
diff --git a/lib/transport/xhr-streaming.js b/lib/transport/xhr-streaming.js
index c903fb1..ae75544 100644
--- a/lib/transport/xhr-streaming.js
+++ b/lib/transport/xhr-streaming.js
@@ -5,7 +5,7 @@ var util = require('util')
   , XhrReceiver = require('./receiver/xhr')
   , XHRCorsObject = require('./sender/xhr-cors')
   , XHRLocalObject = require('./sender/xhr-local')
-  , utils = require('../utils')
+  , utils = require('../utils/origin')
   , loc = require('../polyfills/location')
   ;
 
diff --git a/lib/utils/event.js b/lib/utils/event.js
index cf2cec9..9ae85d4 100644
--- a/lib/utils/event.js
+++ b/lib/utils/event.js
@@ -1,5 +1,7 @@
 'use strict';
 
+var random = require('./random');
+
 module.exports = {
   attachMessage: function(listener) {
     this.attachEvent('message', listener);
@@ -30,44 +32,44 @@ module.exports = {
       global.detachEvent('on' + event, listener);
     }
   }
-};
 
-
-var onUnload = {};
+, onUnload: {}
 // Things registered after beforeunload are to be called immediately.
-var afterUnload = false;
+, afterUnload: false
 
-var triggerUnloadCallbacks = function() {
-  for(var ref in onUnload) {
-    onUnload[ref]();
-    delete onUnload[ref];
+, unloadAdd: function(listener) {
+    var ref = random.string(8);
+    this.onUnload[ref] = listener;
+    if (this.afterUnload) {
+      process.nextTick(this.triggerUnloadCallbacks);
+    }
+    return ref;
   }
-};
 
-var unloadTriggered = function() {
-  if (afterUnload) {
-    return;
+, unloadDel: function(ref) {
+    if (ref in this.onUnload) {
+      delete this.onUnload[ref];
+    }
+  }
+
+, triggerUnloadCallbacks: function() {
+    for(var ref in this.onUnload) {
+      this.onUnload[ref]();
+      delete this.onUnload[ref];
+    }
   }
-  afterUnload = true;
-  triggerUnloadCallbacks();
 };
 
+// var unloadTriggered = function() {
+//   if (afterUnload) {
+//     return;
+//   }
+//   afterUnload = true;
+//   triggerUnloadCallbacks();
+// };
+
 // 'unload' alone is not reliable in opera within an iframe, but we
 // can't use `beforeunload` as IE fires it on javascript: links.
 
 // TODO see if we need to uncomment this
 //utils.attachEvent('unload', unloadTriggered);
-
-utils.unloadAdd = function(listener) {
-  var ref = utils.randomString(8);
-  onUnload[ref] = listener;
-  if (afterUnload) {
-    process.nextTick(triggerUnloadCallbacks);
-  }
-  return ref;
-};
-utils.unloadDel = function(ref) {
-  if (ref in onUnload) {
-    delete onUnload[ref];
-  }
-};
diff --git a/lib/utils/iframe.js b/lib/utils/iframe.js
index d5fad06..28eaeb8 100644
--- a/lib/utils/iframe.js
+++ b/lib/utils/iframe.js
@@ -1,78 +1,79 @@
 'use strict';
 
-var WPrefix = utils.WPrefix = '_jp';
+module.exports = {
+  WPrefix: '_jp'
 
-utils.polluteGlobalNamespace = function() {
-  if (!(WPrefix in global)) {
-    global[WPrefix] = {};
+, polluteGlobalNamespace: function() {
+    if (!(this.WPrefix in global)) {
+      global[this.WPrefix] = {};
+    }
   }
-};
 
-utils.createIframe = function (iframeUrl, errorCallback) {
-  var iframe = document.createElement('iframe');
-  var tref, unloadRef;
-  var unattach = function() {
-    clearTimeout(tref);
-    // Explorer had problems with that.
-    try {iframe.onload = null;} catch (x) {}
-    iframe.onerror = null;
-  };
-  var cleanup = function() {
-    if (iframe) {
-      unattach();
-      // This timeout makes chrome fire onbeforeunload event
-      // within iframe. Without the timeout it goes straight to
-      // onunload.
-      setTimeout(function() {
-        if(iframe) {
-            iframe.parentNode.removeChild(iframe);
-        }
-        iframe = null;
-      }, 0);
-      utils.unloadDel(unloadRef);
-    }
-  };
-  var onerror = function(r) {
-    if (iframe) {
-      cleanup();
-      errorCallback(r);
-    }
-  };
-  var post = function(msg, origin) {
-    try {
-      // When the iframe is not loaded, IE raises an exception
-      // on 'contentWindow'.
-      if (iframe && iframe.contentWindow) {
+, createIframe: function (iframeUrl, errorCallback) {
+    var iframe = document.createElement('iframe');
+    var tref, unloadRef;
+    var unattach = function() {
+      clearTimeout(tref);
+      // Explorer had problems with that.
+      try {iframe.onload = null;} catch (x) {}
+      iframe.onerror = null;
+    };
+    var cleanup = function() {
+      if (iframe) {
+        unattach();
+        // This timeout makes chrome fire onbeforeunload event
+        // within iframe. Without the timeout it goes straight to
+        // onunload.
         setTimeout(function() {
-          iframe.contentWindow.postMessage(msg, origin);
+          if(iframe) {
+              iframe.parentNode.removeChild(iframe);
+          }
+          iframe = null;
         }, 0);
+        utils.unloadDel(unloadRef);
+      }
+    };
+    var onerror = function(r) {
+      if (iframe) {
+        cleanup();
+        errorCallback(r);
       }
-    } catch (x) {}
-  };
+    };
+    var post = function(msg, origin) {
+      try {
+        // When the iframe is not loaded, IE raises an exception
+        // on 'contentWindow'.
+        if (iframe && iframe.contentWindow) {
+          setTimeout(function() {
+            iframe.contentWindow.postMessage(msg, origin);
+          }, 0);
+        }
+      } catch (x) {}
+    };
 
-  iframe.src = iframeUrl;
-  iframe.style.display = 'none';
-  iframe.style.position = 'absolute';
-  iframe.onerror = function(){onerror('onerror');};
-  iframe.onload = function() {
-    // `onload` is triggered before scripts on the iframe are
-    // executed. Give it few seconds to actually load stuff.
-    clearTimeout(tref);
-    tref = setTimeout(function(){onerror('onload timeout');}, 2000);
-  };
-  document.body.appendChild(iframe);
-  tref = setTimeout(function(){onerror('timeout');}, 15000);
-  unloadRef = utils.unloadAdd(cleanup);
-  return {
-    post: post,
-    cleanup: cleanup,
-    loaded: unattach
-  };
-};
+    iframe.src = iframeUrl;
+    iframe.style.display = 'none';
+    iframe.style.position = 'absolute';
+    iframe.onerror = function(){onerror('onerror');};
+    iframe.onload = function() {
+      // `onload` is triggered before scripts on the iframe are
+      // executed. Give it few seconds to actually load stuff.
+      clearTimeout(tref);
+      tref = setTimeout(function(){onerror('onload timeout');}, 2000);
+    };
+    document.body.appendChild(iframe);
+    tref = setTimeout(function(){onerror('timeout');}, 15000);
+    unloadRef = utils.unloadAdd(cleanup);
+    return {
+      post: post,
+      cleanup: cleanup,
+      loaded: unattach
+    };
+  }
 
 /* jshint undef: false, newcap: false */
 /* eslint no-undef: [0], new-cap: [0] */
-utils.createHtmlfile = function (iframeUrl, errorCallback) {
+, createHtmlfile: function (iframeUrl, errorCallback) {
     var doc = new ActiveXObject('htmlfile');
     var tref, unloadRef;
     var iframe;
@@ -124,4 +125,5 @@ utils.createHtmlfile = function (iframeUrl, errorCallback) {
       cleanup: cleanup,
       loaded: unattach
     };
+  }
 };
diff --git a/lib/utils/log.js b/lib/utils/log.js
index c4fc960..a536915 100644
--- a/lib/utils/log.js
+++ b/lib/utils/log.js
@@ -1,7 +1,9 @@
 'use strict';
 
-utils.log = function() {
-  if (global.console && console.log && console.log.apply) {
-    console.log.apply(console, arguments);
+module.exports = {
+  log: function() {
+    if (global.console && console.log && console.log.apply) {
+      console.log.apply(console, arguments);
+    }
   }
 };
diff --git a/tests/receivers.js b/tests/receivers.js
index 4cb8393..d501cc6 100644
--- a/tests/receivers.js
+++ b/tests/receivers.js
@@ -2,7 +2,7 @@
 
 var expect = require('expect.js')
   , JsonpReceiver = require('../lib/transport/receiver/jsonp')
-  , utils = require('../lib/utils')
+  , utils = require('../lib/utils/iframe')
   ;
 
 describe('Receivers', function () {

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