[Pkg-javascript-commits] [sockjs-client] 102/350: Merge branch 'refactor' of https://github.com/sockjs/sockjs-client into refactor

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 aa56c46c542501f4037b1f40c0d738d7efbe92b3
Merge: ea6d035 a459b62
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Thu Oct 9 20:21:04 2014 -0400

    Merge branch 'refactor' of https://github.com/sockjs/sockjs-client into refactor
    
    Conflicts:
    	lib/transport/jsonp-polling.js
    	lib/transport/xhr-polling.js
    	lib/transport/xhr-streaming.js

 .jshintrc                                         |   3 +
 lib/entry.js                                      |   2 +
 lib/main.js                                       |   2 +-
 lib/transport/{sender => browser}/abstract-xhr.js |   8 +
 lib/transport/browser/websocket.js                |   1 +
 lib/transport/driver/websocket.js                 |   1 +
 lib/transport/driver/xhr.js                       |  57 +++++
 lib/transport/iframe-xhr-polling.js               |   3 +-
 lib/transport/jsonp-polling.js                    | 257 ++--------------------
 lib/transport/lib/ajax-based.js                   |   6 +
 lib/transport/lib/polling.js                      |  10 +-
 lib/transport/receiver/jsonp.js                   | 159 +++++++++++++
 lib/transport/sender/jsonp.js                     |  67 ++++++
 lib/transport/sender/xhr-cors.js                  |  15 +-
 lib/transport/sender/xhr-fake.js                  |   1 +
 lib/transport/sender/xhr-local.js                 |   8 +-
 lib/transport/websocket.js                        |   6 +-
 lib/transport/xdr-polling.js                      |   2 +-
 lib/transport/xhr-polling.js                      |   8 +-
 lib/transport/xhr-streaming.js                    |  11 +-
 package.json                                      |  28 ++-
 tests/receivers.js                                | 119 ++++++++++
 22 files changed, 488 insertions(+), 286 deletions(-)

diff --cc lib/main.js
index f7f1411,ddc2d1e..b324879
--- a/lib/main.js
+++ b/lib/main.js
@@@ -141,10 -131,10 +141,10 @@@ SockJS.prototype._receiveInfo = functio
  
    // establish a round-trip timeout (RTO) based on the
    // round-trip time (RTT)
 -  this._rto = utils.countRTO(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 = !document.domain;
+   info.nullOrigin = global.document && !global.document.domain;
    // determine list of desired and supported transports
    var enabledTransports = transports(this.url, this._transportsWhitelist, info);
    this._transports = enabledTransports.main;
diff --cc lib/transport/jsonp-polling.js
index 5320986,b713dcf..06c0e27
--- a/lib/transport/jsonp-polling.js
+++ b/lib/transport/jsonp-polling.js
@@@ -9,230 -9,20 +9,20 @@@
  //   o for Konqueror a dumb timer is needed to detect errors
  
  var util = require('util')
-   , utils = require('../utils')
-   , random = require('../utils/random')
    , BufferedSender = require('./lib/buffered-sender')
-   , TransportMessageEvent = require('./lib/trans-message-event')
+   , Polling = require('./lib/polling')
+   , JsonpReceiver = require('./receiver/jsonp')
+   , jsonpSender = require('./sender/jsonp')
    ;
  
- // Abstract away code that handles global namespace pollution.
- var jsonPReceiverWrapper = function(url, constructReceiver, userCallback) {
-   var id = 'a' + random.string(6);
-   var urlId = url + '?c=' + encodeURIComponent(utils.WPrefix + '.' + id);
- 
-   // Unfortunately it is not possible to abort loading of the
-   // script. We need to keep track of frake close frames.
-   var aborting = 0;
- 
-   // Callback will be called exactly once.
-   var callback = function(frame) {
-     switch(aborting) {
-     case 0:
-       // Normal behaviour - delete hook _and_ emit message.
-       delete window[utils.WPrefix][id];
-       userCallback(frame);
-       break;
-     case 1:
-       // Fake close frame - emit but don't delete hook.
-       userCallback(frame);
-       aborting = 2;
-       break;
-     case 2:
-       // Got frame after connection was closed, delete hook, don't emit.
-       delete window[utils.WPrefix][id];
-       break;
-     }
-   };
- 
-   var closeScript = constructReceiver(urlId, callback);
-   window[utils.WPrefix][id] = closeScript;
-   var stop = function() {
-     if (window[utils.WPrefix][id]) {
-       aborting = 1;
-       var err = new Error('JSONP user aborted read');
-       err.code = 1000;
-       window[utils.WPrefix][id](err);
-     }
-   };
-   return stop;
- };
- 
- function jsonPGenericSender(url, payload, callback) {
-   var form = window._sendForm;
-   var area = window._sendArea;
- 
-   if (!form) {
-     form = window._sendForm = document.createElement('form');
-     area = window._sendArea = document.createElement('textarea');
-     area.name = 'd';
-     form.style.display = 'none';
-     form.style.position = 'absolute';
-     form.method = 'POST';
-     form.enctype = 'application/x-www-form-urlencoded';
-     form.acceptCharset = 'UTF-8';
-     form.appendChild(area);
-     document.body.appendChild(form);
-   }
-   var id = 'a' + random.string(8);
-   form.target = id;
-   form.action = url + '/jsonp_send?i=' + id;
- 
-   var iframe;
-   try {
-     // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
-     iframe = document.createElement('<iframe name="' + id + '">');
-   } catch(x) {
-     iframe = document.createElement('iframe');
-     iframe.name = id;
-   }
-   iframe.id = id;
-   form.appendChild(iframe);
-   iframe.style.display = 'none';
- 
-   try {
-     area.value = payload;
-   } catch(e) {
-     utils.log('Your browser is seriously broken. Go home! ' + e.message);
-   }
-   form.submit();
- 
-   var completed = function() {
-     if (!iframe.onerror) {
-       return;
-     }
-     iframe.onreadystatechange = iframe.onerror = iframe.onload = null;
-     // Opera mini doesn't like if we GC iframe
-     // immediately, thus this timeout.
-     setTimeout(function() {
-                    iframe.parentNode.removeChild(iframe);
-                    iframe = null;
-                }, 500);
-     area.value = '';
-     // It is not possible to detect if the iframe succeeded or
-     // failed to submit our form.
-     callback();
-   };
-   iframe.onerror = iframe.onload = completed;
-   iframe.onreadystatechange = function() {
-     if (iframe.readyState === 'complete') {
-       completed();
-     }
-   };
-   return completed;
- }
- 
- // Parts derived from Socket.io:
- //    https://github.com/LearnBoost/socket.io/blob/0.6.17/lib/socket.io/transports/jsonp-polling.js
- // and jQuery-JSONP:
- //    https://code.google.com/p/jquery-jsonp/source/browse/trunk/core/jquery.jsonp.js
- function jsonPGenericReceiver(url, callback) {
-   var tref;
-   var script = global.document.createElement('script');
-   var script2;  // Opera synchronous load trick.
-   var closeScript = function(err) {
-     if (script2) {
-       script2.parentNode.removeChild(script2);
-       script2 = null;
-     }
-     if (script) {
-       clearTimeout(tref);
-       // Unfortunately, you can't really abort script loading of
-       // the script.
-       script.parentNode.removeChild(script);
-       script.onreadystatechange = script.onerror =
-           script.onload = script.onclick = null;
-       script = null;
-       callback(err);
-       callback = null;
-     }
-   };
- 
-   // IE9 fires 'error' event after orsc or before, in random order.
-   var loadedOkay = false;
-   var errorTimer = null;
- 
-   script.id = 'a' + random.string(8);
-   script.src = url;
-   script.type = 'text/javascript';
-   script.charset = 'UTF-8';
-   script.onerror = function() {
-     if (!errorTimer) {
-       // Delay firing closeScript.
-       errorTimer = setTimeout(function() {
-         if (!loadedOkay) {
-           closeScript(new Error('JSONP script loaded abnormally (onerror)'));
-         }
-       }, 1000);
-     }
-   };
-   script.onload = function() {
-     closeScript(new Error('JSONP script loaded abnormally (onload)'));
-   };
+ function JsonPTransport(transUrl) {
+   var self = this;
+   BufferedSender.call(this, transUrl, jsonpSender);
 -  
 +
-   script.onreadystatechange = function() {
-     if (/loaded|closed/.test(script.readyState)) {
-       if (script && script.htmlFor && script.onclick) {
-         loadedOkay = true;
-         try {
-             // In IE, actually execute the script.
-             script.onclick();
-         } catch (x) {}
-       }
-       if (script) {
-         closeScript(new Error('JSONP script loaded abnormally (onreadystatechange)'));
-       }
-     }
+   this.poll = new Polling(JsonpReceiver, this.transUrl + '/jsonp');
+   this.poll.onmessage = this.poll.onclose = function (e) {
+     self.dispatchEvent(e);
    };
-   // IE: event/htmlFor/onclick trick.
-   // One can't rely on proper order for onreadystatechange. In order to
-   // make sure, set a 'htmlFor' and 'event' properties, so that
-   // script code will be installed as 'onclick' handler for the
-   // script object. Later, onreadystatechange, manually execute this
-   // code. FF and Chrome doesn't work with 'event' and 'htmlFor'
-   // set. For reference see:
-   //   http://jaubourg.net/2010/07/loading-script-as-onclick-handler-of.html
-   // Also, read on that about script ordering:
-   //   http://wiki.whatwg.org/wiki/Dynamic_Script_Execution_Order
-   if (typeof script.async === 'undefined' && document.attachEvent) {
-     // According to mozilla docs, in recent browsers script.async defaults
-     // to 'true', so we may use it to detect a good browser:
-     // https://developer.mozilla.org/en/HTML/Element/script
-     if (!/opera/i.test(navigator.userAgent)) {
-       // Naively assume we're in IE
-       try {
-           script.htmlFor = script.id;
-           script.event = 'onclick';
-       } catch (x) {}
-       script.async = true;
-     } else {
-       // Opera, second sync script hack
-       script2 = document.createElement('script');
-       script2.text = "try{var a = document.getElementById('" + script.id + "'); if(a)a.onerror();}catch(x){};";
-       script.async = script2.async = false;
-     }
-   }
-   if (typeof script.async !== 'undefined') {
-     script.async = true;
-   }
- 
-   // Fallback mostly for Konqueror - stupid timer, 35 seconds shall be plenty.
-   tref = setTimeout(function() {
-     closeScript(new Error('JSONP script loaded abnormally (timeout)'));
-   }, 35000);
- 
-   var head = document.getElementsByTagName('head')[0];
-   head.insertBefore(script, head.firstChild);
-   if (script2) {
-     head.insertBefore(script2, head.firstChild);
-   }
-   return closeScript;
- }
- 
- function JsonPTransport(transUrl) {
-   utils.polluteGlobalNamespace();
-   this.transUrl = transUrl;
-   BufferedSender.call(this, transUrl, jsonPGenericSender);
-   this._scheduleReceiver();
  }
  
  util.inherits(JsonPTransport, BufferedSender);
diff --cc lib/transport/xhr-polling.js
index 355b12e,6202b60..b5d602d
--- a/lib/transport/xhr-polling.js
+++ b/lib/transport/xhr-polling.js
@@@ -4,11 -4,11 +4,12 @@@ var util = require('util'
    , AjaxBasedTransport = require('./lib/ajax-based')
    , XhrReceiver = require('./receiver/xhr')
    , XHRCorsObject = require('./sender/xhr-cors')
+   , XHRLocalObject = require('./sender/xhr-local')
    , utils = require('../utils')
 +  , loc = require('../polyfills/location')
    ;
  
- function XhrPollingTransport(ri, transUrl) {
+ function XhrPollingTransport(transUrl) {
    AjaxBasedTransport.call(this, transUrl, '/xhr', XhrReceiver, XHRCorsObject);
  }
  
@@@ -18,10 -18,11 +19,11 @@@ XhrPollingTransport.enabled = function(
    if (info.nullOrigin) {
      return false;
    }
-   if (global.XMLHttpRequest && utils.isSameOriginUrl(url, loc.href)) {
+ 
 -  if (XHRLocalObject.enabled && utils.isSameOriginUrl(url)) {
++  if (XHRLocalObject.enabled && utils.isSameOriginUrl(url, loc.href)) {
      return true;
    }
-   return XHRCorsObject.capable();
+   return XHRCorsObject.enabled;
  };
  
  XhrPollingTransport.transportName = 'xhr-polling';
diff --cc lib/transport/xhr-streaming.js
index 630b979,6cc2f3e..c903fb1
--- a/lib/transport/xhr-streaming.js
+++ b/lib/transport/xhr-streaming.js
@@@ -4,11 -4,11 +4,12 @@@ var util = require('util'
    , AjaxBasedTransport = require('./lib/ajax-based')
    , XhrReceiver = require('./receiver/xhr')
    , XHRCorsObject = require('./sender/xhr-cors')
+   , XHRLocalObject = require('./sender/xhr-local')
    , utils = require('../utils')
 +  , loc = require('../polyfills/location')
    ;
  
- function XhrStreamingTransport(ri, transUrl) {
+ function XhrStreamingTransport(transUrl) {
    AjaxBasedTransport.call(this, transUrl, '/xhr_streaming', XhrReceiver, XHRCorsObject);
  }
  
@@@ -19,14 -19,14 +20,14 @@@ XhrStreamingTransport.enabled = functio
      return false;
    }
    // Opera doesn't support xhr-streaming
-   if (/opera/i.test(global.navigator.userAgent)) {
+   if (global.navigator && /opera/i.test(global.navigator.userAgent)) {
      return false;
    }
-   if (global.XMLHttpRequest && utils.isSameOriginUrl(url, loc.href)) {
 -  
 -  if (XHRLocalObject.enabled && utils.isSameOriginUrl(url)) {
++
++  if (XHRLocalObject.enabled && utils.isSameOriginUrl(url, loc.href)) {
      return true;
    }
- 
-   return XHRCorsObject.capable();
+   return XHRCorsObject.enabled;
  };
  
  XhrStreamingTransport.transportName = 'xhr-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