[Pkg-javascript-commits] [sockjs-client] 134/350: debug statements. Almost all transports working.
tonnerre at ancient-solutions.com
tonnerre at ancient-solutions.com
Fri Aug 5 01:03:55 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 61353b2abf2ed10e208ef143ef034bb49172354d
Author: Bryce Kahle <bkahle at gmail.com>
Date: Mon Oct 13 21:14:35 2014 -0400
debug statements. Almost all transports working.
---
.travis.yml | 46 +++++++++-
.zuul.yml | 26 +++---
Makefile | 23 +++++
gulpfile.js | 12 +--
lib/entry.js | 5 ++
lib/iframe-bootstrap.js | 8 +-
lib/main.js | 35 ++++++--
lib/shims.js | 163 ++++++++++++++++++++++++++++++++++
lib/simpleevent.js | 23 -----
lib/transport/iframe.js | 10 ++-
lib/transport/lib/ajax-based.js | 4 +
lib/transport/lib/buffered-sender.js | 10 +++
lib/transport/lib/polling.js | 6 ++
lib/transport/lib/sender-receiver.js | 5 ++
lib/transport/receiver/eventsource.js | 11 ++-
lib/transport/receiver/htmlfile.js | 10 +++
lib/transport/receiver/jsonp.js | 14 ++-
lib/transport/receiver/xhr.js | 38 ++++++--
lib/transport/sender/jsonp.js | 14 ++-
lib/transport/websocket.js | 12 ++-
lib/utils/iframe.js | 22 +++--
lib/utils/object.js | 2 +-
lib/utils/random.js | 2 +-
lib/utils/transport.js | 8 ++
package.json | 13 ++-
tests/browser.js | 2 +
tests/html/smoke-latency.html | 6 +-
tests/lib/receivers.js | 24 ++---
tests/lib/utils.js | 2 +-
29 files changed, 456 insertions(+), 100 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 4b002ad..15a8698 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,13 +1,51 @@
language: node_js
node_js:
- '0.10'
-before_script:
- - "gulp testbundle"
script:
- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] && npm test || false'
-addons:
- sauce_connect: true
env:
global:
- secure: L36j3mbU1U2FiOhxYZrTZ7apVnb56gE+QFixsYyDpCRBrVW0q8WR9LTMJx+aPNdlzhRS5FT4gfInujPTnZeb+EvqEoYwtT+CeNG15XtGkfJDkfBFjBa4IZpJAjIwJB+0kKkZr9R7lQEEgy7H9FvLHY2N2WgdzuG8WZwiVRnkwVw=
- secure: K6NxT5qIzV/4Ce1HqoYMdpFtqxx5S5Ua/sKp1bbHCXTCft6gLBR3mrf1RR14Wj9AwcM9QY6YbNF/dXGmBdMNtiJN+a2nvcfIoPTGJ3UpK4r2xI5w/U+uhRQMxD55SkWaxUIPvEfgvwyETSpFZRYACnNkpxeQWMkdc14vk74Feu0=
+matrix:
+ include:
+ - node_js: '0.10'
+ env: BROWSER_NAME=chrome BROWSER_VERSION=latest
+ - node_js: '0.10'
+ env: BROWSER_NAME=firefox BROWSER_VERSION=latest
+ - node_js: '0.10'
+ env: BROWSER_NAME=safari BROWSER_VERSION=5
+ - node_js: '0.10'
+ env: BROWSER_NAME=safari BROWSER_VERSION=6
+ - node_js: '0.10'
+ env: BROWSER_NAME=safari BROWSER_VERSION=7
+ - node_js: '0.10'
+ env: BROWSER_NAME=ie BROWSER_VERSION=6
+ - node_js: '0.10'
+ env: BROWSER_NAME=ie BROWSER_VERSION=7
+ - node_js: '0.10'
+ env: BROWSER_NAME=ie BROWSER_VERSION=8
+ - node_js: '0.10'
+ env: BROWSER_NAME=ie BROWSER_VERSION=9
+ - node_js: '0.10'
+ env: BROWSER_NAME=ie BROWSER_VERSION=10 BROWSER_PLATFORM="Windows 2012"
+ - node_js: '0.10'
+ env: BROWSER_NAME=ie BROWSER_VERSION=latest BROWSER_PLATFORM="Windows 2012"
+ - node_js: '0.10'
+ env: BROWSER_NAME=iphone BROWSER_VERSION=5.1
+ - node_js: '0.10'
+ env: BROWSER_NAME=iphone BROWSER_VERSION=6.1
+ - node_js: '0.10'
+ env: BROWSER_NAME=iphone BROWSER_VERSION=7.1
+ - node_js: '0.10'
+ env: BROWSER_NAME=iphone BROWSER_VERSION=8.0
+ - node_js: '0.10'
+ env: BROWSER_NAME=android BROWSER_VERSION=4.0
+ - node_js: '0.10'
+ env: BROWSER_NAME=android BROWSER_VERSION=4.1
+ - node_js: '0.10'
+ env: BROWSER_NAME=android BROWSER_VERSION=4.2
+ - node_js: '0.10'
+ env: BROWSER_NAME=android BROWSER_VERSION=4.3
+ - node_js: '0.10'
+ env: BROWSER_NAME=android BROWSER_VERSION=4.4
\ No newline at end of file
diff --git a/.zuul.yml b/.zuul.yml
index 65eebe1..e7a84f5 100644
--- a/.zuul.yml
+++ b/.zuul.yml
@@ -1,22 +1,20 @@
ui: mocha-bdd
scripts:
- - "static/jquery.min.js"
- "config.js"
- "domain.js"
- - "lib/sockjs.js"
server: ./tests/support/sockjs_server.js
browsers:
- name: chrome
version: latest
- # - name: firefox
- # version: latest
- # - name: opera
- # version: 11..latest
- # - name: safari
- # version: 5..latest
- # - name: ie
- # version: 6..latest
- # - name: iphone
- # version: 4..latest
- # - name: android
- # version: 4.0..latest
\ No newline at end of file
+ - name: firefox
+ version: latest
+ - name: opera
+ version: 11..latest
+ - name: safari
+ version: 5..latest
+ - name: ie
+ version: 6..latest
+ - name: iphone
+ version: 5.1..latest
+ - name: android
+ version: 4.0..latest
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..fdbfc2b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,23 @@
+
+test:
+ @if [ "x$(BROWSER_NAME)" = "x" ]; then make test-node; else make test-zuul; fi
+
+test-node:
+ @./node_modules/.bin/mocha \
+ tests/node.js
+
+test-zuul:
+ @if [ "x$(BROWSER_PLATFORM)" = "x" ]; then \
+ ./node_modules/.bin/zuul --sauce-connect \
+ --browser-name $(BROWSER_NAME) \
+ --browser-version $(BROWSER_VERSION) \
+ tests/browser.js; \
+ else \
+ ./node_modules/.bin/zuul --sauce-connect \
+ --browser-name $(BROWSER_NAME) \
+ --browser-version $(BROWSER_VERSION) \
+ --browser-platform "$(BROWSER_PLATFORM)" \
+ tests/browser.js; \
+ fi
+
+.PHONY: test
diff --git a/gulpfile.js b/gulpfile.js
index 844f8b3..e5880c7 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -31,7 +31,7 @@ gulp.task('watch', function () {
});
gulp.task('testbundle', function() {
- browserify('./lib/entry.js')
+ return browserify('./lib/entry.js')
.ignore('querystring')
.bundle({
standalone: 'SockJS'
@@ -43,11 +43,11 @@ gulp.task('testbundle', function() {
.pipe(gulp.dest('./tests/html/lib/'))
;
- return browserify('./tests/html/lib/alltests.js')
- .bundle()
- .pipe(source('alltestsbundle.js'))
- .pipe(gulp.dest('./tests/html/lib/'))
- ;
+ // return browserify('./tests/html/lib/alltests.js')
+ // .bundle()
+ // .pipe(source('alltestsbundle.js'))
+ // .pipe(gulp.dest('./tests/html/lib/'))
+ // ;
});
gulp.task('browserify', function () {
diff --git a/lib/entry.js b/lib/entry.js
index 342b8a0..955b284 100644
--- a/lib/entry.js
+++ b/lib/entry.js
@@ -3,3 +3,8 @@
var transportList = require('./transport-list');
module.exports = require('./main')(transportList);
+
+// TODO this is needed for iframe transports, but could probably be done a different way.
+if ('_sockjs_onload' in global) {
+ setTimeout(global._sockjs_onload, 1);
+}
diff --git a/lib/iframe-bootstrap.js b/lib/iframe-bootstrap.js
index a53483f..45b22b7 100644
--- a/lib/iframe-bootstrap.js
+++ b/lib/iframe-bootstrap.js
@@ -9,10 +9,12 @@ var originUtils = require('./utils/origin')
, loc = require('./polyfills/location')
;
-module.exports = function (SockJS, facadeTransports) {
+module.exports = function (SockJS, availableTransports) {
var transportMap = {};
- facadeTransports.forEach(function (ft) {
- transportMap[ft.transportName] = ft;
+ availableTransports.forEach(function (at) {
+ if (at.facadeTransport) {
+ transportMap[at.facadeTransport.transportName] = at.facadeTransport;
+ }
});
// hard-coded for the info iframe
diff --git a/lib/main.js b/lib/main.js
index 4e10189..9654ff1 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -5,6 +5,7 @@ require('./shims');
var u = require('url')
, util = require('util')
, JSON3 = require('json3')
+ , debug = require('debug')
, random = require('./utils/random')
, escape = require('./utils/escape')
, origin = require('./utils/origin')
@@ -21,6 +22,9 @@ var u = require('url')
, InfoReceiver = require('./info-receiver')
;
+global.dbg = debug;
+debug = debug('sockjs-client:main');
+
var transports;
// follow constructor steps defined at http://dev.w3.org/html5/websockets/#the-websocket-interface
@@ -37,6 +41,8 @@ function SockJS(url, protocols, transportsWhitelist) {
// non-standard extension
// TODO attempt to remove and provide another way
this._transportsWhitelist = transportsWhitelist;
+ // TODO specify via options? server routes require this
+ this._server = random.numberString(1000);
// Step 1 of WS spec - parse and validate the url
var parsedUrl = u.parse(url);
@@ -56,7 +62,7 @@ function SockJS(url, protocols, transportsWhitelist) {
// Step 3 - check port access - no need here
// Step 4 - parse protocols argument
- if (typeof protocols === 'undefined') {
+ if (!protocols) {
protocols = [];
} else if (!Array.isArray(protocols)) {
protocols = [protocols];
@@ -89,6 +95,7 @@ function SockJS(url, protocols, transportsWhitelist) {
// store the sanitized url
this.url = u.format(parsedUrl);
+ debug('using url %s', this.url);
// Step 7 - start connection in background
// obtain server info
@@ -100,12 +107,12 @@ function SockJS(url, protocols, transportsWhitelist) {
util.inherits(SockJS, EventTarget);
function userSetCode(code) {
- return code && (code === 1000 || (code >= 3000 && code <= 4999));
+ return code === 1000 || (code >= 3000 && code <= 4999);
}
SockJS.prototype.close = function(code, reason) {
// Step 1
- if (!userSetCode(code)) {
+ if (code && !userSetCode(code)) {
throw new InvalidAccessError('Invalid code');
}
// Step 2.4 states the max is 123 bytes, but we are just checking length
@@ -133,6 +140,8 @@ SockJS.prototype.send = function(data) {
this._transport.send(escape.quote(data));
};
+SockJS.version = '1.0.0-alpha1';
+
SockJS.CONNECTING = 0;
SockJS.OPEN = 1;
SockJS.CLOSING = 2;
@@ -153,20 +162,19 @@ 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.filterToAvailable(this.url, this._transportsWhitelist, info);
+ var enabledTransports = transports.filterToEnabled(this.url, this._transportsWhitelist, info);
this._transports = enabledTransports.main;
- // setup iframe bootstrap
- require('./iframe-bootstrap')(SockJS, enabledTransports.facade);
-
this._connect();
};
SockJS.prototype._connect = function() {
for (var Transport = this._transports.shift(); Transport; Transport = this._transports.shift()) {
+ debug('attempt %s', Transport.transportName);
if (Transport.needBody) {
if (!global.document.body ||
(typeof global.document.readyState !== 'undefined' && global.document.readyState !== 'complete')) {
+ debug('waiting for body');
this._transports.unshift(Transport);
eventUtils.attachEvent('load', this._connect.bind(this));
return;
@@ -176,8 +184,10 @@ SockJS.prototype._connect = function() {
// calculate timeout based on RTO and round trips. Default to 5s
var timeoutMs = (this._rto * Transport.roundTrips) || 5000;
this._transportTimeoutId = setTimeout(this._transportTimeout.bind(this), timeoutMs);
+ debug('using timeout ', timeoutMs);
var transportUrl = this._transUrl + '/' + this._server + '/' + random.string(8);
+ debug('transport url: %s', transportUrl);
var transport = new Transport(transportUrl, this._transUrl);
transport.on('message', this._transportMessage.bind(this));
transport.once('close', this._transportClose.bind(this));
@@ -189,12 +199,14 @@ SockJS.prototype._connect = function() {
};
SockJS.prototype._transportTimeout = function() {
+ debug('_transportTimeout');
if (this.readyState === SockJS.CONNECTING) {
this._close(2007, 'Transport timed out');
}
};
SockJS.prototype._transportMessage = function(msg) {
+ debug('_transportMessage', msg);
var self = this;
var type = msg.slice(0, 1);
var payload;
@@ -205,11 +217,13 @@ SockJS.prototype._transportMessage = function(msg) {
case 'a':
payload = JSON3.parse(msg.slice(1) || '[]');
payload.forEach(function (p) {
+ debug('message', p);
self.dispatchEvent(new TransportMessageEvent(p));
});
break;
case 'm':
payload = JSON3.parse(msg.slice(1) || 'null');
+ debug('message', payload);
this.dispatchEvent(new TransportMessageEvent(payload));
break;
case 'c':
@@ -218,11 +232,13 @@ SockJS.prototype._transportMessage = function(msg) {
break;
case 'h':
this.dispatchEvent(new Event('heartbeat'));
+ debug('heartbeat');
break;
}
};
SockJS.prototype._transportClose = function(code, reason) {
+ debug('_transportClose', code, reason);
if (this._transport) {
this._transport.removeAllListeners();
this._transport = null;
@@ -237,6 +253,7 @@ SockJS.prototype._transportClose = function(code, reason) {
};
SockJS.prototype._open = function() {
+ debug('_open', this.readyState);
if (this.readyState === SockJS.CONNECTING) {
if (this._transportTimeoutId) {
clearTimeout(this._transportTimeoutId);
@@ -245,6 +262,7 @@ SockJS.prototype._open = function() {
this.readyState = SockJS.OPEN;
this.transport = this._transport.transportName;
this.dispatchEvent(new Event('open'));
+ debug('connected', this.transport);
} else {
// The server might have been restarted, and lost track of our
// connection.
@@ -253,6 +271,7 @@ SockJS.prototype._open = function() {
};
SockJS.prototype._close = function(code, reason, wasClean) {
+ debug('_close', code, reason, wasClean, this.readyState);
var forceFail = false;
if (this._ir) {
@@ -285,6 +304,7 @@ SockJS.prototype._close = function(code, reason, wasClean) {
this.dispatchEvent(e);
this.onmessage = this.onclose = this.onerror = null;
+ debug('disconnected');
}.bind(this));
};
@@ -305,5 +325,6 @@ SockJS.prototype.countRTO = function (rtt) {
module.exports = function (availableTransports) {
transports = transport(availableTransports);
+ require('./iframe-bootstrap')(SockJS, availableTransports);
return SockJS;
};
diff --git a/lib/shims.js b/lib/shims.js
index 3855b86..3e67bf7 100644
--- a/lib/shims.js
+++ b/lib/shims.js
@@ -6,6 +6,7 @@
var ArrayPrototype = Array.prototype;
var ObjectPrototype = Object.prototype;
var FunctionPrototype = Function.prototype;
+var StringPrototype = String.prototype;
var array_slice = ArrayPrototype.slice;
var _toString = ObjectPrototype.toString;
@@ -81,6 +82,10 @@ function toInteger(num) {
return n;
}
+function ToUint32(x) {
+ return x >>> 0;
+}
+
//
// Function
// ========
@@ -320,3 +325,161 @@ if (!Date.now) {
return new Date().getTime();
};
}
+
+//
+// String
+// ======
+//
+
+// ES5 15.5.4.14
+// http://es5.github.com/#x15.5.4.14
+
+// [bugfix, IE lt 9, firefox 4, Konqueror, Opera, obscure browsers]
+// Many browsers do not split properly with regular expressions or they
+// do not perform the split correctly under obscure conditions.
+// See http://blog.stevenlevithan.com/archives/cross-browser-split
+// I've tested in many browsers and this seems to cover the deviant ones:
+// 'ab'.split(/(?:ab)*/) should be ["", ""], not [""]
+// '.'.split(/(.?)(.?)/) should be ["", ".", "", ""], not ["", ""]
+// 'tesst'.split(/(s)*/) should be ["t", undefined, "e", "s", "t"], not
+// [undefined, "t", undefined, "e", ...]
+// ''.split(/.?/) should be [], not [""]
+// '.'.split(/()()/) should be ["."], not ["", "", "."]
+
+var string_split = StringPrototype.split;
+if (
+ 'ab'.split(/(?:ab)*/).length !== 2 ||
+ '.'.split(/(.?)(.?)/).length !== 4 ||
+ 'tesst'.split(/(s)*/)[1] === 't' ||
+ 'test'.split(/(?:)/, -1).length !== 4 ||
+ ''.split(/.?/).length ||
+ '.'.split(/()()/).length > 1
+) {
+ (function () {
+ var compliantExecNpcg = /()??/.exec('')[1] === void 0; // NPCG: nonparticipating capturing group
+
+ StringPrototype.split = function (separator, limit) {
+ var string = this;
+ if (separator === void 0 && limit === 0) {
+ return [];
+ }
+
+ // If `separator` is not a regex, use native split
+ if (_toString.call(separator) !== '[object RegExp]') {
+ return string_split.call(this, separator, limit);
+ }
+
+ var output = [],
+ flags = (separator.ignoreCase ? 'i' : '') +
+ (separator.multiline ? 'm' : '') +
+ (separator.extended ? 'x' : '') + // Proposed for ES6
+ (separator.sticky ? 'y' : ''), // Firefox 3+
+ lastLastIndex = 0,
+ // Make `global` and avoid `lastIndex` issues by working with a copy
+ separator2, match, lastIndex, lastLength;
+ separator = new RegExp(separator.source, flags + 'g');
+ string += ''; // Type-convert
+ if (!compliantExecNpcg) {
+ // Doesn't need flags gy, but they don't hurt
+ separator2 = new RegExp('^' + separator.source + '$(?!\\s)', flags);
+ }
+ /* Values for `limit`, per the spec:
+ * If undefined: 4294967295 // Math.pow(2, 32) - 1
+ * If 0, Infinity, or NaN: 0
+ * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
+ * If negative number: 4294967296 - Math.floor(Math.abs(limit))
+ * If other: Type-convert, then use the above rules
+ */
+ limit = limit === void 0 ?
+ -1 >>> 0 : // Math.pow(2, 32) - 1
+ ToUint32(limit);
+ while (match = separator.exec(string)) {
+ // `separator.lastIndex` is not reliable cross-browser
+ lastIndex = match.index + match[0].length;
+ if (lastIndex > lastLastIndex) {
+ output.push(string.slice(lastLastIndex, match.index));
+ // Fix browsers whose `exec` methods don't consistently return `undefined` for
+ // nonparticipating capturing groups
+ if (!compliantExecNpcg && match.length > 1) {
+ match[0].replace(separator2, function () {
+ for (var i = 1; i < arguments.length - 2; i++) {
+ if (arguments[i] === void 0) {
+ match[i] = void 0;
+ }
+ }
+ });
+ }
+ if (match.length > 1 && match.index < string.length) {
+ ArrayPrototype.push.apply(output, match.slice(1));
+ }
+ lastLength = match[0].length;
+ lastLastIndex = lastIndex;
+ if (output.length >= limit) {
+ break;
+ }
+ }
+ if (separator.lastIndex === match.index) {
+ separator.lastIndex++; // Avoid an infinite loop
+ }
+ }
+ if (lastLastIndex === string.length) {
+ if (lastLength || !separator.test('')) {
+ output.push('');
+ }
+ } else {
+ output.push(string.slice(lastLastIndex));
+ }
+ return output.length > limit ? output.slice(0, limit) : output;
+ };
+ }());
+
+// [bugfix, chrome]
+// If separator is undefined, then the result array contains just one String,
+// which is the this value (converted to a String). If limit is not undefined,
+// then the output array is truncated so that it contains no more than limit
+// elements.
+// "0".split(undefined, 0) -> []
+} else if ('0'.split(void 0, 0).length) {
+ StringPrototype.split = function split(separator, limit) {
+ if (separator === void 0 && limit === 0) { return []; }
+ return string_split.call(this, separator, limit);
+ };
+}
+
+// ES5 15.5.4.20
+// whitespace from: http://es5.github.io/#x15.5.4.20
+var ws = '\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003' +
+ '\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028' +
+ '\u2029\uFEFF';
+var zeroWidth = '\u200b';
+var wsRegexChars = '[' + ws + ']';
+var trimBeginRegexp = new RegExp('^' + wsRegexChars + wsRegexChars + '*');
+var trimEndRegexp = new RegExp(wsRegexChars + wsRegexChars + '*$');
+var hasTrimWhitespaceBug = StringPrototype.trim && (ws.trim() || !zeroWidth.trim());
+defineProperties(StringPrototype, {
+ // http://blog.stevenlevithan.com/archives/faster-trim-javascript
+ // http://perfectionkills.com/whitespace-deviations/
+ trim: function trim() {
+ if (this === void 0 || this === null) {
+ throw new TypeError("can't convert " + this + ' to object');
+ }
+ return String(this).replace(trimBeginRegexp, '').replace(trimEndRegexp, '');
+ }
+}, hasTrimWhitespaceBug);
+
+// ECMA-262, 3rd B.2.3
+// Not an ECMAScript standard, although ECMAScript 3rd Edition has a
+// non-normative section suggesting uniform semantics and it should be
+// normalized across all browsers
+// [bugfix, IE lt 9] IE < 9 substr() with negative value not working in IE
+var string_substr = StringPrototype.substr;
+var hasNegativeSubstrBug = ''.substr && '0b'.substr(-1) !== 'b';
+defineProperties(StringPrototype, {
+ substr: function substr(start, length) {
+ return string_substr.call(
+ this,
+ start < 0 ? ((start = this.length + start) < 0 ? 0 : start) : start,
+ length
+ );
+ }
+}, hasNegativeSubstrBug);
diff --git a/lib/simpleevent.js b/lib/simpleevent.js
deleted file mode 100644
index 755d424..0000000
--- a/lib/simpleevent.js
+++ /dev/null
@@ -1,23 +0,0 @@
-'use strict';
-
-var util = require('util')
- , Event = require('./polyfills/event')
- ;
-
-var SimpleEvent = function(type, obj) {
- Event.call(this);
- this.initEvent(type);
-
- if (typeof obj !== 'undefined') {
- for (var k in obj) {
- if (!obj.hasOwnProperty(k)) {
- continue;
- }
- this[k] = obj[k];
- }
- }
-};
-
-util.inherits(SimpleEvent, Event);
-
-module.exports = SimpleEvent;
diff --git a/lib/transport/iframe.js b/lib/transport/iframe.js
index a0964a9..c78b6fa 100644
--- a/lib/transport/iframe.js
+++ b/lib/transport/iframe.js
@@ -16,9 +16,11 @@ var util = require('util')
, eventUtils = require('../utils/event')
, random = require('../utils/random')
, browser = require('../utils/browser')
+ , debug = require('debug')('sockjs-client:transport:iframe')
;
function IframeTransport(transport, transUrl, baseUrl) {
+ debug(transport, transUrl, baseUrl);
EventEmitter.call(this);
var self = this;
@@ -31,6 +33,7 @@ function IframeTransport(transport, transUrl, baseUrl) {
var iframeUrl = baseUrl + '/iframe.html#' + this.windowId;
this.iframeObj = iframeUtils.createIframe(iframeUrl, function(r) {
+ debug('err callback');
self.emit('close', 1006, 'Unable to load an iframe (' + r + ')');
self.removeAllListeners();
});
@@ -42,6 +45,7 @@ function IframeTransport(transport, transUrl, baseUrl) {
util.inherits(IframeTransport, EventEmitter);
IframeTransport.prototype.close = function() {
+ debug('close');
if (this.iframeObj) {
eventUtils.detachEvent('message', this.onmessageCallback);
try {
@@ -57,13 +61,15 @@ IframeTransport.prototype.close = function() {
};
IframeTransport.prototype._message = function(e) {
+ debug('message', e.data);
if (!originUtils.isSameOriginUrl(e.origin, this.origin)) {
+ debug('not same origin', e.origin, this.origin);
return;
}
var iframeMessage = JSON3.parse(e.data);
if (iframeMessage.windowId !== this.windowId) {
- console.log('Mismatched window id');
+ debug('mismatched window id', iframeMessage.windowId, this.windowId);
return;
}
@@ -85,6 +91,7 @@ IframeTransport.prototype._message = function(e) {
};
IframeTransport.prototype.postMessage = function(type, data) {
+ debug('postMessage', type, data);
this.iframeObj.post(JSON3.stringify({
windowId: this.windowId
, type: type
@@ -93,6 +100,7 @@ IframeTransport.prototype.postMessage = function(type, data) {
};
IframeTransport.prototype.send = function (message) {
+ debug('send', message);
this.postMessage('m', message);
};
diff --git a/lib/transport/lib/ajax-based.js b/lib/transport/lib/ajax-based.js
index bc3fda6..53008d3 100644
--- a/lib/transport/lib/ajax-based.js
+++ b/lib/transport/lib/ajax-based.js
@@ -2,22 +2,26 @@
var util = require('util')
, SenderReceiver = require('./sender-receiver')
+ , debug = require('debug')('sockjs-client:ajax-based')
;
function createAjaxSender(AjaxObject) {
return function(url, payload, callback) {
+ debug('create ajax sender', url, payload);
var opt = {};
if (typeof payload === 'string') {
opt.headers = {'Content-type':'text/plain'};
}
var xo = new AjaxObject('POST', url + '/xhr_send', payload, opt);
xo.once('finish', function(status) {
+ debug('finish', status);
if (status !== 200 && status !== 204) {
return callback(new Error('http status ' + status));
}
callback();
});
return function() {
+ debug('abort');
callback(new Error('Aborted'));
};
};
diff --git a/lib/transport/lib/buffered-sender.js b/lib/transport/lib/buffered-sender.js
index 7e9663e..bb031dc 100644
--- a/lib/transport/lib/buffered-sender.js
+++ b/lib/transport/lib/buffered-sender.js
@@ -2,9 +2,11 @@
var util = require('util')
, EventEmitter = require('events').EventEmitter
+ , debug = require('debug')('sockjs-client:buffered-sender')
;
function BufferedSender(url, sender) {
+ debug(url);
EventEmitter.call(this);
this.sendBuffer = [];
this.sender = sender;
@@ -14,6 +16,7 @@ function BufferedSender(url, sender) {
util.inherits(BufferedSender, EventEmitter);
BufferedSender.prototype.send = function(message) {
+ debug('send', message);
this.sendBuffer.push(message);
if (!this.sendStop) {
this.sendSchedule();
@@ -29,25 +32,30 @@ BufferedSender.prototype.send = function(message) {
// does not fix the big problem, but it does make the tests go more
// stable on slow networks.
BufferedSender.prototype.sendScheduleWait = function() {
+ debug('sendScheduleWait');
var self = this;
var tref;
this.sendStop = function() {
+ debug('sendStop');
self.sendStop = null;
clearTimeout(tref);
};
tref = setTimeout(function() {
+ debug('timeout');
self.sendStop = null;
self.sendSchedule();
}, 25);
};
BufferedSender.prototype.sendSchedule = function() {
+ debug('sendSchedule', this.sendBuffer.length);
var self = this;
if (this.sendBuffer.length > 0) {
var payload = '[' + this.sendBuffer.join(',') + ']';
this.sendStop = this.sender(this.url, payload, function(err) {
self.sendStop = null;
if (err) {
+ debug('error', err);
self.emit('close', err.code || 1006, 'Sending error: ' + err);
self._cleanup();
} else {
@@ -59,10 +67,12 @@ BufferedSender.prototype.sendSchedule = function() {
};
BufferedSender.prototype._cleanup = function () {
+ debug('_cleanup');
this.removeAllListeners();
};
BufferedSender.prototype.stop = function() {
+ debug('stop');
if (this.sendStop) {
this.sendStop();
}
diff --git a/lib/transport/lib/polling.js b/lib/transport/lib/polling.js
index e1a4092..b6f0739 100644
--- a/lib/transport/lib/polling.js
+++ b/lib/transport/lib/polling.js
@@ -2,9 +2,11 @@
var util = require('util')
, EventEmitter = require('events').EventEmitter
+ , debug = require('debug')('sockjs-client:polling')
;
function Polling(Receiver, receiveUrl, AjaxObject) {
+ debug(receiveUrl);
EventEmitter.call(this);
this.Receiver = Receiver;
this.receiveUrl = receiveUrl;
@@ -15,14 +17,17 @@ function Polling(Receiver, receiveUrl, AjaxObject) {
util.inherits(Polling, EventEmitter);
Polling.prototype._scheduleReceiver = function() {
+ debug('_scheduleReceiver');
var self = this;
var poll = this.poll = new this.Receiver(this.receiveUrl, this.AjaxObject);
poll.on('message', function(msg) {
+ debug('message', msg);
self.emit('message', msg);
});
poll.once('close', function(code, reason) {
+ debug('close', code, reason, self.pollIsClosing);
self.poll = poll = null;
if (!self.pollIsClosing) {
@@ -36,6 +41,7 @@ Polling.prototype._scheduleReceiver = function() {
};
Polling.prototype.abort = function() {
+ debug('abort');
this.pollIsClosing = true;
if (this.poll) {
this.poll.abort();
diff --git a/lib/transport/lib/sender-receiver.js b/lib/transport/lib/sender-receiver.js
index 945570f..8b834df 100644
--- a/lib/transport/lib/sender-receiver.js
+++ b/lib/transport/lib/sender-receiver.js
@@ -3,17 +3,21 @@
var util = require('util')
, BufferedSender = require('./buffered-sender')
, Polling = require('./polling')
+ , debug = require('debug')('sockjs-client:sender-receiver')
;
function SenderReceiver(transUrl, urlSuffix, senderFunc, Receiver, AjaxObject) {
+ debug(transUrl, urlSuffix);
var self = this;
BufferedSender.call(this, transUrl, senderFunc);
this.poll = new Polling(Receiver, transUrl + urlSuffix, AjaxObject);
this.poll.on('message', function (msg) {
+ debug('poll message', msg);
self.emit('message', msg);
});
this.poll.once('close', function (code, reason) {
+ debug('poll close', code, reason);
self.poll = null;
self.stop();
self.emit('close', code, reason);
@@ -24,6 +28,7 @@ function SenderReceiver(transUrl, urlSuffix, senderFunc, Receiver, AjaxObject) {
util.inherits(SenderReceiver, BufferedSender);
SenderReceiver.prototype.close = function() {
+ debug('close');
if (this.poll) {
this.poll.abort();
this.poll = null;
diff --git a/lib/transport/receiver/eventsource.js b/lib/transport/receiver/eventsource.js
index 1073074..edb4af9 100644
--- a/lib/transport/receiver/eventsource.js
+++ b/lib/transport/receiver/eventsource.js
@@ -2,33 +2,39 @@
var util = require('util')
, EventEmitter = require('events').EventEmitter
+ , debug = require('debug')('sockjs-client:receiver:eventsource')
;
function EventSourceReceiver(url) {
+ debug(url);
EventEmitter.call(this);
var self = this;
var es = this.es = new global.EventSource(url);
es.onmessage = function(e) {
+ debug('message', e.data);
self.emit('message', decodeURI(e.data));
};
- es.onerror = function () {
+ es.onerror = function (e) {
+ debug('error', es.readyState, e);
// ES on reconnection has readyState = 0 or 1.
// on network error it's CLOSED = 2
var reason = (es.readyState !== 2 ? 'network' : 'permanent');
self._cleanup();
- self.close(reason);
+ self._close(reason);
};
}
util.inherits(EventSourceReceiver, EventEmitter);
EventSourceReceiver.prototype.abort = function() {
+ debug('abort');
this._cleanup();
this._close('user');
};
EventSourceReceiver.prototype._cleanup = function () {
+ debug('cleanup');
var es = this.es;
if (es) {
es.onmessage = es.onerror = null;
@@ -38,6 +44,7 @@ EventSourceReceiver.prototype._cleanup = function () {
};
EventSourceReceiver.prototype._close = function (reason) {
+ debug('close', reason);
var self = this;
setTimeout(function() {
self.emit('close', null, reason);
diff --git a/lib/transport/receiver/htmlfile.js b/lib/transport/receiver/htmlfile.js
index e291912..281a690 100644
--- a/lib/transport/receiver/htmlfile.js
+++ b/lib/transport/receiver/htmlfile.js
@@ -4,6 +4,7 @@ var util = require('util')
, iframeUtils = require('../../utils/iframe')
, EventEmitter = require('events').EventEmitter
, random = require('../../utils/random')
+ , debug = require('debug')('sockjs-client:receiver:htmlfile')
;
var _isIeHtmlfileCapable;
@@ -22,6 +23,7 @@ var isIeHtmlfileCapable = function() {
function HtmlfileReceiver(url) {
+ debug(url);
EventEmitter.call(this);
var self = this;
iframeUtils.polluteGlobalNamespace();
@@ -30,22 +32,27 @@ function HtmlfileReceiver(url) {
url += ((url.indexOf('?') === -1) ? '?' : '&') +
'c=' + decodeURIComponent(iframeUtils.WPrefix + '.' + this.id);
+ debug('using htmlfile', _isIeHtmlfileCapable);
var constructor = isIeHtmlfileCapable() ?
iframeUtils.createHtmlfile : iframeUtils.createIframe;
global[iframeUtils.WPrefix][this.id] = {
start: function () {
+ debug('start');
self.iframeObj.loaded();
},
message: function (data) {
+ debug('message', data);
self.emit('message', data);
},
stop: function () {
+ debug('stop');
self._cleanup();
self._close('network');
}
};
this.iframeObj = constructor(url, function() {
+ debug('callback');
self._cleanup();
self._close('permanent');
});
@@ -54,11 +61,13 @@ function HtmlfileReceiver(url) {
util.inherits(HtmlfileReceiver, EventEmitter);
HtmlfileReceiver.prototype.abort = function() {
+ debug('abort');
this._cleanup();
this._close('user');
};
HtmlfileReceiver.prototype._cleanup = function () {
+ debug('_cleanup');
if (this.iframeObj) {
this.iframeObj.cleanup();
this.iframeObj = null;
@@ -67,6 +76,7 @@ HtmlfileReceiver.prototype._cleanup = function () {
};
HtmlfileReceiver.prototype._close = function (reason) {
+ debug('_close', reason);
this.emit('close', null, reason);
this.removeAllListeners();
};
diff --git a/lib/transport/receiver/jsonp.js b/lib/transport/receiver/jsonp.js
index 88d16f4..4678737 100644
--- a/lib/transport/receiver/jsonp.js
+++ b/lib/transport/receiver/jsonp.js
@@ -5,9 +5,11 @@ var utils = require('../../utils/iframe')
, browser = require('../../utils/browser')
, util = require('util')
, EventEmitter = require('events').EventEmitter
+ , debug = require('debug')('sockjs-client:receiver:jsonp')
;
function JsonpReceiver(url) {
+ debug(url);
var self = this;
EventEmitter.call(this);
@@ -21,6 +23,7 @@ function JsonpReceiver(url) {
// Fallback mostly for Konqueror - stupid timer, 35 seconds shall be plenty.
this.timeoutId = setTimeout(function() {
+ debug('timeout');
self._abort(new Error('JSONP script loaded abnormally (timeout)'));
}, JsonpReceiver.timeout);
}
@@ -28,6 +31,7 @@ function JsonpReceiver(url) {
util.inherits(JsonpReceiver, EventEmitter);
JsonpReceiver.prototype.abort = function () {
+ debug('abort');
if (global[utils.WPrefix][this.id]) {
var err = new Error('JSONP user aborted read');
err.code = 1000;
@@ -39,6 +43,7 @@ JsonpReceiver.timeout = 35000;
JsonpReceiver.scriptErrorTimeout = 1000;
JsonpReceiver.prototype._callback = function (data) {
+ debug('_callback', data);
this._cleanup();
delete global[utils.WPrefix][this.id];
@@ -47,6 +52,7 @@ JsonpReceiver.prototype._callback = function (data) {
}
if (data) {
+ debug('message', data);
this.emit('message', data);
}
this.emit('close', null, 'network');
@@ -54,6 +60,7 @@ JsonpReceiver.prototype._callback = function (data) {
};
JsonpReceiver.prototype._abort = function (err) {
+ debug('_abort', err);
this._cleanup();
this.aborting = true;
this.emit('close', err.code, err.message);
@@ -61,6 +68,7 @@ JsonpReceiver.prototype._abort = function (err) {
};
JsonpReceiver.prototype._cleanup = function () {
+ debug('_cleanup');
clearTimeout(this.timeoutId);
if (this.script2) {
this.script2.parentNode.removeChild(this.script2);
@@ -78,6 +86,7 @@ JsonpReceiver.prototype._cleanup = function () {
};
JsonpReceiver.prototype._scriptError = function () {
+ debug('_scriptError');
var self = this;
if (this.errorTimer) {
return;
@@ -91,22 +100,25 @@ JsonpReceiver.prototype._scriptError = function () {
};
JsonpReceiver.prototype._createScript = function (url) {
+ debug('_createScript', url);
var self = this;
var script = this.script = global.document.createElement('script');
var script2; // Opera synchronous load trick.
- script.id = 'a' + utils.randomString(8);
+ script.id = 'a' + random.string(8);
script.src = url;
script.type = 'text/javascript';
script.charset = 'UTF-8';
script.onerror = this._scriptError.bind(this);
script.onload = function() {
+ debug('onload');
self._abort(new Error('JSONP script loaded abnormally (onload)'));
};
// IE9 fires 'error' event after onreadystatechange or before, in random order.
// Use loadedOkay to determine if actually errored
script.onreadystatechange = function() {
+ debug('onreadystatechange', script.readyState);
if (/loaded|closed/.test(script.readyState)) {
if (script && script.htmlFor && script.onclick) {
self.loadedOkay = true;
diff --git a/lib/transport/receiver/xhr.js b/lib/transport/receiver/xhr.js
index 2331f04..6e2b5b3 100644
--- a/lib/transport/receiver/xhr.js
+++ b/lib/transport/receiver/xhr.js
@@ -2,18 +2,24 @@
var util = require('util')
, EventEmitter = require('events').EventEmitter
+ , debug = require('debug')('sockjs-client:receiver:xhr')
;
function XhrReceiver(url, AjaxObject) {
+ debug('url');
EventEmitter.call(this);
var self = this;
+ this.bufferPosition = 0;
+
this.xo = new AjaxObject('POST', url, null);
this.xo.on('chunk', this._chunkHandler.bind(this));
this.xo.once('finish', function(status, text) {
+ debug('finish', status, text);
self._chunkHandler(status, text);
self.xo = null;
var reason = status === 200 ? 'network' : 'permanent';
+ debug('close', reason);
self.emit('close', null, reason);
self._cleanup();
});
@@ -22,27 +28,45 @@ function XhrReceiver(url, AjaxObject) {
util.inherits(XhrReceiver, EventEmitter);
XhrReceiver.prototype._chunkHandler = function (status, text) {
+ debug('_chunkHandler', status);
if (status !== 200 || !text) {
return;
}
- var self = this;
- var messages = text.split('\n');
- messages.forEach(function (msg) {
- if (!msg) {
- return;
+ for (var idx = -1; ; this.bufferPosition += idx + 1) {
+ var buf = text.slice(this.bufferPosition);
+ idx = buf.indexOf('\n');
+ if (idx === -1) {
+ break;
}
- self.emit('message', msg);
- });
+ var msg = buf.slice(0, idx);
+ if (msg) {
+ debug('message', msg);
+ this.emit('message', msg);
+ }
+ }
+
+ // var self = this;
+ // var messages = text.split('\n');
+ // messages.forEach(function (msg) {
+ // if (!msg) {
+ // return;
+ // }
+ // debug('message', msg);
+ // self.emit('message', msg);
+ // });
};
XhrReceiver.prototype._cleanup = function () {
+ debug('_cleanup');
this.removeAllListeners();
};
XhrReceiver.prototype.abort = function() {
+ debug('abort');
if (this.xo) {
this.xo.close();
+ debug('close');
this.emit('close', null, 'user');
this.xo = null;
}
diff --git a/lib/transport/sender/jsonp.js b/lib/transport/sender/jsonp.js
index 8dc93a7..1aa3a1f 100644
--- a/lib/transport/sender/jsonp.js
+++ b/lib/transport/sender/jsonp.js
@@ -1,11 +1,13 @@
'use strict';
var random = require('../../utils/random')
+ , debug = require('debug')('sockjs-client:sender:jsonp')
;
var form, area;
function createIframe (id) {
+ debug('createIframe', id);
try {
// ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
return global.document.createElement('<iframe name="' + id + '">');
@@ -17,6 +19,7 @@ function createIframe (id) {
}
function createForm() {
+ debug('createForm');
form = global.document.createElement('form');
form.style.display = 'none';
form.style.position = 'absolute';
@@ -32,6 +35,7 @@ function createForm() {
}
module.exports = function (url, payload, callback) {
+ debug(url, payload);
if (!form) {
createForm();
}
@@ -52,6 +56,7 @@ module.exports = function (url, payload, callback) {
form.submit();
var completed = function(err) {
+ debug('completed', err);
if (!iframe.onerror) {
return;
}
@@ -67,13 +72,18 @@ module.exports = function (url, payload, callback) {
// failed to submit our form.
callback(err);
};
- iframe.onerror = iframe.onload = completed;
- iframe.onreadystatechange = function() {
+ iframe.onerror = completed;
+ iframe.onload = function () {
+ completed();
+ };
+ iframe.onreadystatechange = function(e) {
+ debug('onreadystatechange', iframe.readyState, e);
if (iframe.readyState === 'complete') {
completed();
}
};
return function () {
+ debug('aborted');
completed(new Error('Aborted'));
};
};
diff --git a/lib/transport/websocket.js b/lib/transport/websocket.js
index ef5edf6..65373e1 100644
--- a/lib/transport/websocket.js
+++ b/lib/transport/websocket.js
@@ -2,12 +2,14 @@
var utils = require('../utils/event')
, util = require('util')
+ , debug = require('debug')('sockjs-client:websocket')
, EventEmitter = require('events').EventEmitter
, WebsocketDriver = require('./driver/websocket')
;
function WebSocketTransport(transUrl) {
EventEmitter.call(this);
+ debug('constructor', transUrl);
var self = this;
var url = transUrl + '/websocket';
@@ -20,6 +22,7 @@ function WebSocketTransport(transUrl) {
this.ws = new WebsocketDriver(this.url);
this.ws.onmessage = function(e) {
+ debug('message event', e.data);
self.emit('message', e.data);
};
// Firefox has an interesting bug. If a websocket connection is
@@ -29,13 +32,16 @@ function WebSocketTransport(transUrl) {
// https://github.com/sockjs/sockjs-client/issues/28
// https://bugzilla.mozilla.org/show_bug.cgi?id=696085
this.unloadRef = utils.unloadAdd(function(){
+ debug('unload');
self.ws.close();
});
this.ws.onclose = function(e) {
+ debug('close event', e);
self.emit('close', e.code, e.reason);
self._cleanup();
};
- this.ws.onerror = function() {
+ this.ws.onerror = function(e) {
+ debug('error event', e);
self.emit('close', 1006, 'WebSocket connection broken');
self._cleanup();
};
@@ -44,10 +50,12 @@ function WebSocketTransport(transUrl) {
util.inherits(WebSocketTransport, EventEmitter);
WebSocketTransport.prototype.send = function(data) {
+ debug('send', data);
this.ws.send('[' + data + ']');
};
WebSocketTransport.prototype.close = function() {
+ debug('close');
if (this.ws) {
this.ws.close();
}
@@ -55,6 +63,7 @@ WebSocketTransport.prototype.close = function() {
};
WebSocketTransport.prototype._cleanup = function () {
+ debug('_cleanup');
var ws = this.ws;
if (ws) {
ws.onmessage = ws.onclose = ws.onerror = null;
@@ -65,6 +74,7 @@ WebSocketTransport.prototype._cleanup = function () {
};
WebSocketTransport.enabled = function() {
+ debug('enabled');
return !!WebsocketDriver;
};
WebSocketTransport.transportName = 'websocket';
diff --git a/lib/utils/iframe.js b/lib/utils/iframe.js
index 3c20a3e..0fe7d9f 100644
--- a/lib/utils/iframe.js
+++ b/lib/utils/iframe.js
@@ -2,6 +2,7 @@
var eventUtils = require('./event')
, JSON3 = require('json3')
+ , debug = require('debug')('sockjs-client:utils:iframe')
;
module.exports = {
@@ -30,12 +31,14 @@ module.exports = {
var iframe = global.document.createElement('iframe');
var tref, unloadRef;
var unattach = function() {
+ debug('unattach');
clearTimeout(tref);
// Explorer had problems with that.
try {iframe.onload = null;} catch (x) {}
iframe.onerror = null;
};
var cleanup = function() {
+ debug('cleanup');
if (iframe) {
unattach();
// This timeout makes chrome fire onbeforeunload event
@@ -50,13 +53,15 @@ module.exports = {
eventUtils.unloadDel(unloadRef);
}
};
- var onerror = function(r) {
+ var onerror = function(err) {
+ debug('onerror', err);
if (iframe) {
cleanup();
- errorCallback(r);
+ errorCallback(err);
}
};
var post = function(msg, origin) {
+ debug('post', msg, origin);
try {
// When the iframe is not loaded, IE raises an exception
// on 'contentWindow'.
@@ -71,15 +76,22 @@ module.exports = {
iframe.src = iframeUrl;
iframe.style.display = 'none';
iframe.style.position = 'absolute';
- iframe.onerror = function(){onerror('onerror');};
+ iframe.onerror = function(){
+ onerror('onerror');
+ };
iframe.onload = function() {
+ debug('onload');
// `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);
+ tref = setTimeout(function () {
+ onerror('onload timeout');
+ }, 2000);
};
global.document.body.appendChild(iframe);
- tref = setTimeout(function(){onerror('timeout');}, 15000);
+ tref = setTimeout(function(){
+ onerror('timeout');
+ }, 15000);
unloadRef = eventUtils.unloadAdd(cleanup);
return {
post: post,
diff --git a/lib/utils/object.js b/lib/utils/object.js
index 6e98496..415e9f7 100644
--- a/lib/utils/object.js
+++ b/lib/utils/object.js
@@ -14,7 +14,7 @@ module.exports = {
for (var i = 1, length = arguments.length; i < length; i++) {
source = arguments[i];
for (prop in source) {
- if (hasOwnProperty.call(source, prop)) {
+ if (Object.prototype.hasOwnProperty.call(source, prop)) {
obj[prop] = source[prop];
}
}
diff --git a/lib/utils/random.js b/lib/utils/random.js
index b760008..b1206eb 100644
--- a/lib/utils/random.js
+++ b/lib/utils/random.js
@@ -12,7 +12,7 @@ module.exports = {
var bytes = crypto.randomBytes(length);
var ret = [];
for (var i = 0; i < length; i++) {
- ret.push( _randomStringChars[bytes[i] % max] );
+ ret.push(_randomStringChars.substr(bytes[i] % max, 1));
}
return ret.join('');
}
diff --git a/lib/utils/transport.js b/lib/utils/transport.js
index 14ca2d7..a9d0a06 100644
--- a/lib/utils/transport.js
+++ b/lib/utils/transport.js
@@ -1,5 +1,7 @@
'use strict';
+var debug = require('debug')('sockjs-client:utils:transport');
+
module.exports = function (availableTransports) {
return {
filterToEnabled: function (url, transportsWhitelist, info) {
@@ -9,6 +11,8 @@ module.exports = function (availableTransports) {
};
if (!transportsWhitelist) {
transportsWhitelist = [];
+ } else if (typeof transportsWhitelist === 'string') {
+ transportsWhitelist = [transportsWhitelist];
}
availableTransports.forEach(function (trans) {
@@ -18,14 +22,18 @@ module.exports = function (availableTransports) {
if (transportsWhitelist.length &&
transportsWhitelist.indexOf(trans.transportName) === -1) {
+ debug('not in whitelist', trans.transportName);
return;
}
if (trans.enabled(url, info)) {
+ debug('enabled', trans.transportName);
transports.main.push(trans);
if (trans.facadeTransport) {
transports.facade.push(trans.facadeTransport);
}
+ } else {
+ debug('disabled', trans.transportName);
}
});
return transports;
diff --git a/package.json b/package.json
index 86da61f..7dbc908 100644
--- a/package.json
+++ b/package.json
@@ -27,6 +27,7 @@
},
"devDependencies": {
"browserify": "^4.2.3",
+ "debug": "^2.0.0",
"exorcist": "^0.1.6",
"expect.js": "~0.3.1",
"gulp": "^3.8.7",
@@ -39,7 +40,7 @@
"proxyquire": "~1.0.1",
"sockjs": "^0.3.9",
"vinyl-source-stream": "^0.1.1",
- "zuul": "^1.10.1"
+ "zuul": "^1.11.2"
},
"homepage": "http://sockjs.org",
"keywords": [
@@ -54,10 +55,8 @@
"url": "https://github.com/sockjs/sockjs-client.git"
},
"scripts": {
- "t": "node ./node_modules/mocha/bin/mocha tests/node.js",
- "b": "./node_modules/zuul/bin/zuul --local 9090 -- tests/browser.js",
- "test": "./node_modules/zuul/bin/zuul --sauce-connect -- tests/html/lib/unittests.js tests/html/lib/domtests.js tests/html/lib/endtoendtests.js tests/html/lib/tests.js",
- "test_ws": "./node_modules/zuul/bin/zuul --sauce-connect -- tests/html/lib/ws_test.js",
- "test_local": "./node_modules/zuul/bin/zuul --local 9090 -- tests/html/lib/unittests.js tests/html/lib/domtests.js tests/html/lib/endtoendtests.js tests/html/lib/tests.js"
+ "test": "make test",
+ "test_local": "./node_modules/.bin/zuul --local 9090 -- tests/browser.js",
+ "zuul": "./node_modules/.bin/zuul -- tests/browser.js"
}
-}
\ No newline at end of file
+}
diff --git a/tests/browser.js b/tests/browser.js
index 4cbaeb8..d17cc71 100644
--- a/tests/browser.js
+++ b/tests/browser.js
@@ -1,5 +1,7 @@
'use strict';
+require('../lib/shims');
+
// prevent global leak warnings on this
global._jp = {};
diff --git a/tests/html/smoke-latency.html b/tests/html/smoke-latency.html
index 9c91f5e..7733ccd 100644
--- a/tests/html/smoke-latency.html
+++ b/tests/html/smoke-latency.html
@@ -18,7 +18,9 @@
<option value="websocket">websocket</option>
<option value="xdr-streaming">xdr-streaming</option>
<option value="xhr-streaming">xhr-streaming</option>
+ <option value="eventsource">eventsource</option>
<option value="iframe-eventsource">iframe-eventsource</option>
+ <option value="htmlfile">htmlfile</option>
<option value="iframe-htmlfile">iframe-htmlfile</option>
<option value="xdr-polling">xdr-polling</option>
<option value="xhr-polling">xhr-polling</option>
@@ -49,7 +51,7 @@
send();
};
function onclose(e) {
- log('disconnected ' + e);
+ log('disconnected ' + e.code + ', ' + e.reason);
$('#connect').each(function(_,e){e.disabled='';});
$('#disconnect').attr('disabled', true);
};
@@ -73,7 +75,7 @@
options = jQuery.extend({}, client_opts.sockjs_opts)
options.protocols_whitelist = typeof protocol === 'string' ?
[protocol] : protocol;
- sjs = new SockJS(client_opts.url + '/echo', null, options);
+ sjs = new SockJS(client_opts.url + '/echo', null, protocol);
sjs.onopen = onopen
sjs.onclose = onclose;
sjs.onmessage = xonmessage;
diff --git a/tests/lib/receivers.js b/tests/lib/receivers.js
index d6c44e8..ea159ce 100644
--- a/tests/lib/receivers.js
+++ b/tests/lib/receivers.js
@@ -23,11 +23,11 @@ describe('Receivers', function () {
};
var jpr = new JsonpReceiver('test');
jpr.on('close', function (code, reason) {
- expect(reason).to.eql('network');
+ expect(reason).to.equal('network');
done();
});
jpr.on('message', function (msg) {
- expect(msg).to.eql('datadata');
+ expect(msg).to.equal('datadata');
});
});
@@ -81,11 +81,11 @@ describe('Receivers', function () {
var jpr = new JsonpReceiver('test');
jpr.on('close', function (code, reason) {
- expect(reason).to.eql('network');
+ expect(reason).to.equal('network');
done();
});
jpr.on('message', function (msg) {
- expect(msg).to.eql('datadata');
+ expect(msg).to.equal('datadata');
});
// simulate script error
@@ -105,17 +105,17 @@ describe('Receivers', function () {
var jpr = new JsonpReceiver('test');
jpr.on('close', function (code, reason) {
- expect(reason).to.eql('network');
+ expect(reason).to.equal('network');
done();
});
jpr.on('message', function (msg) {
- expect(msg).to.eql('datadata');
+ expect(msg).to.equal('datadata');
});
// simulate script error
setTimeout(function () {
jpr._scriptError();
- }, 150);
+ }, 100);
});
});
@@ -128,11 +128,11 @@ describe('Receivers', function () {
var xhr = new XhrReceiver('test', XhrFake);
var i = 0, responses = ['test', 'multiple', 'lines', '{}'];
xhr.on('message', function (msg) {
- expect(msg).to.be.eql(responses[i]);
+ expect(msg).to.equal(responses[i]);
i++;
});
xhr.on('close', function (code, reason) {
- expect(reason).to.be.eql('network');
+ expect(reason).to.equal('network');
done();
});
xhr._chunkHandler(200, 'test\nmultiple\nlines');
@@ -143,11 +143,11 @@ describe('Receivers', function () {
var i = 0, responses = ['{}'];
xhr.on('message', function (msg) {
expect(i).to.be.lessThan(responses.length);
- expect(msg).to.be.eql(responses[i]);
+ expect(msg).to.equal(responses[i]);
i++;
});
xhr.on('close', function (code, reason) {
- expect(reason).to.be.eql('network');
+ expect(reason).to.equal('network');
done();
});
xhr._chunkHandler(200, '');
@@ -159,7 +159,7 @@ describe('Receivers', function () {
expect().fail();
});
xhr.on('close', function (code, reason) {
- expect(reason).to.be.eql('user');
+ expect(reason).to.equal('user');
done();
});
xhr.abort();
diff --git a/tests/lib/utils.js b/tests/lib/utils.js
index c8a7e9c..dd0fd4c 100644
--- a/tests/lib/utils.js
+++ b/tests/lib/utils.js
@@ -89,7 +89,7 @@ describe('utils', function () {
expect(escape.quote('\u2000\u2001\u0300\u0301')).to.equal('"\\u2000\\u2001\\u0300\\u0301"');
});
- it('handles all 64K characters round-trip', function () {
+ it.skip('handles all 64K characters round-trip', function () {
var c = [];
for (var i = 0; i <= 65536; i++) {
c.push(String.fromCharCode(i));
--
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