[Pkg-javascript-commits] [sockjs-client] 298/434: New protocol detection algorithm.
Tonnerre Lombard
tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:20 UTC 2014
This is an automated email from the git hooks/post-receive script.
tonnerre-guest pushed a commit to branch master
in repository sockjs-client.
commit 5dac751a5f0fc3d40df142532852ab0b9e649e09
Author: Marek Majkowski <majek04 at gmail.com>
Date: Fri Jan 13 15:32:29 2012 +0000
New protocol detection algorithm.
---
lib/utils.js | 64 +++++++++++++++++++++++++++++++++++++++++
tests/html/src/unittests.coffee | 61 +++++++++++++++++++++++++++++++++++++++
2 files changed, 125 insertions(+)
diff --git a/lib/utils.js b/lib/utils.js
index ca317b7..fafb93d 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -219,3 +219,67 @@ utils.quote = function(string) {
});
}
+var _all_protocols = ['websocket',
+ 'xdr-streaming',
+ 'xhr-streaming',
+ 'iframe-eventsource',
+ 'iframe-htmlfile',
+ 'xdr-polling',
+ 'xhr-polling',
+ 'iframe-xhr-polling',
+ 'jsonp-polling'];
+
+utils.probeProtocols = function() {
+ var probed = {};
+ for(var i=0; i<_all_protocols.length; i++) {
+ var protocol = _all_protocols[i];
+ // User can have a typo in protocol name.
+ probed[protocol] = SockJS[protocol] &&
+ SockJS[protocol].enabled();
+ }
+ return probed;
+};
+
+utils.detectProtocols = function(probed, protocols_whitelist, info) {
+ var pe = {},
+ protocols = [];
+ if (!protocols_whitelist) protocols_whitelist = _all_protocols;
+ for(var i=0; i<protocols_whitelist.length; i++) {
+ var protocol = protocols_whitelist[i];
+ pe[protocol] = probed[protocol];
+ }
+ var maybe_push = function(protos) {
+ var proto = protos.shift();
+ if (pe[proto]) {
+ protocols.push(proto);
+ } else {
+ if (protos.length > 0) {
+ maybe_push(protos);
+ }
+ }
+ }
+
+ // 1. Websocket
+ if (info.websocket !== false) {
+ maybe_push(['websocket']);
+ }
+
+ // 2. Streaming
+ if (pe['xdr-streaming'] && !info.cookie_needed) {
+ protocols.push('xdr-streaming');
+ } else {
+ maybe_push(['xhr-streaming',
+ 'iframe-eventsource',
+ 'iframe-htmlfile']);
+ }
+
+ // 3. Polling
+ if (pe['xdr-polling'] && !info.cookie_needed) {
+ protocols.push('xdr-polling');
+ } else {
+ maybe_push(['xhr-polling',
+ 'iframe-xhr-polling',
+ 'jsonp-polling']);
+ }
+ return protocols;
+}
diff --git a/tests/html/src/unittests.coffee b/tests/html/src/unittests.coffee
index b102c2c..cab6ba7 100644
--- a/tests/html/src/unittests.coffee
+++ b/tests/html/src/unittests.coffee
@@ -84,3 +84,64 @@ test 'quote', ->
all_chars = c.join('')
ok(JSON.parse(u.quote(all_chars)) is all_chars, "Quote/unquote all 64K chars.")
+test 'detectProtocols', ->
+ chrome_probed = {
+ 'websocket': true
+ 'xdr-streaming': false
+ 'xhr-streaming': true
+ 'iframe-eventsource': true
+ 'iframe-htmlfile': true
+ 'xdr-polling': false
+ 'xhr-polling': true
+ 'iframe-xhr-polling': true
+ 'jsonp-polling': true
+ }
+ # Chrome
+ deepEqual(u.detectProtocols(chrome_probed, null, {}),
+ ['websocket', 'xhr-streaming', 'xhr-polling'])
+ deepEqual(u.detectProtocols(chrome_probed, null, {websocket:false}),
+ ['xhr-streaming', 'xhr-polling'])
+ # Opera
+ opera_probed = {
+ 'websocket': false
+ 'xdr-streaming': false
+ 'xhr-streaming': false
+ 'iframe-eventsource': true
+ 'iframe-htmlfile': true
+ 'xdr-polling': false
+ 'xhr-polling': false
+ 'iframe-xhr-polling': true
+ 'jsonp-polling': true
+ }
+ deepEqual(u.detectProtocols(opera_probed, null, {}),
+ ['iframe-eventsource', 'iframe-xhr-polling'])
+ # IE 6, IE 7
+ ie6_probed = {
+ 'websocket': false
+ 'xdr-streaming': false
+ 'xhr-streaming': false
+ 'iframe-eventsource': false
+ 'iframe-htmlfile': false
+ 'xdr-polling': false
+ 'xhr-polling': false
+ 'iframe-xhr-polling': false
+ 'jsonp-polling': true
+ }
+ deepEqual(u.detectProtocols(ie6_probed, null, {}),
+ ['jsonp-polling'])
+ # IE 8, IE 9
+ ie8_probed = {
+ 'websocket': false
+ 'xdr-streaming': true
+ 'xhr-streaming': false
+ 'iframe-eventsource': false
+ 'iframe-htmlfile': true
+ 'xdr-polling': true
+ 'xhr-polling': false
+ 'iframe-xhr-polling': true
+ 'jsonp-polling': true
+ }
+ deepEqual(u.detectProtocols(ie8_probed, null, {}),
+ ['xdr-streaming', 'xdr-polling'])
+ deepEqual(u.detectProtocols(ie8_probed, null, {cookie_needed:true}),
+ ['iframe-htmlfile', 'iframe-xhr-polling'])
--
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