[Pkg-javascript-commits] [sockjs-client] 87/350: Start cleanup
tonnerre at ancient-solutions.com
tonnerre at ancient-solutions.com
Fri Aug 5 01:03:45 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 50d4e2136cbc4391239a20371766896f9c69e5ac
Author: Bryce Kahle <bkahle at gmail.com>
Date: Sun Sep 14 17:32:13 2014 -0400
Start cleanup
---
lib/abstract-xhr.js | 163 +++++----
lib/ajax-based.js | 70 ++--
lib/buffered-sender.js | 86 ++---
lib/closeevent.js | 17 +
lib/event.js | 13 +
lib/eventtarget.js | 70 ++++
lib/facade.js | 17 +-
lib/info-receiver-fake.js | 23 +-
lib/info-receiver-iframe.js | 61 ++--
lib/info-receiver.js | 73 ++--
lib/invalidaccesserror.js | 10 +
lib/invalidstateerror.js | 10 +
lib/location.js | 8 +
lib/main.js | 122 +++++++
lib/protocols.js | 20 +-
lib/reventtarget.js | 71 ----
lib/securityerror.js | 10 +
lib/shims.js | 7 +
lib/simpleevent.js | 39 +-
lib/sockjs.js | 2 +-
lib/trans-eventsource.js | 17 +-
lib/trans-htmlfile.js | 18 +-
lib/trans-iframe-eventsource.js | 24 +-
lib/trans-iframe-htmlfile.js | 25 +-
lib/trans-iframe-xhr-polling.js | 24 +-
lib/trans-iframe.js | 126 ++++---
lib/trans-jsonp-polling.js | 474 ++++++++++++------------
lib/trans-polling.js | 65 ++--
lib/trans-receiver-eventsource.js | 65 ++--
lib/trans-receiver-htmlfile.js | 100 +++---
lib/trans-receiver-iframe.js | 21 +-
lib/trans-receiver-xhr.js | 72 ++--
lib/trans-websocket.js | 81 ++---
lib/trans-xdr-polling.js | 25 +-
lib/trans-xdr-streaming.js | 35 +-
lib/trans-xhr-polling.js | 35 +-
lib/trans-xhr-streaming.js | 49 +--
lib/utils.js | 739 +++++++++++++++++++-------------------
lib/xdr.js | 85 ++---
lib/xhr-cors.js | 17 +-
lib/xhr-local.js | 21 +-
lib/xhr-polling-iframe.js | 16 +-
package.json | 8 +-
tests/html/lib/unittests.js | 20 +-
tests/main.js | 85 +++++
45 files changed, 1684 insertions(+), 1455 deletions(-)
diff --git a/lib/abstract-xhr.js b/lib/abstract-xhr.js
index 57b3463..f5dc3a2 100644
--- a/lib/abstract-xhr.js
+++ b/lib/abstract-xhr.js
@@ -11,98 +11,105 @@ function AbstractXHRObject() {
util.inherits(AbstractXHRObject, EventEmitter);
AbstractXHRObject.prototype._start = function(method, url, payload, opts) {
- var that = this;
+ var self = this;
- try {
- that.xhr = new XMLHttpRequest();
- } catch(x) {}
+ try {
+ this.xhr = new XMLHttpRequest();
+ } catch(x) {}
- if (!that.xhr) {
- try {
- that.xhr = new window.ActiveXObject('Microsoft.XMLHTTP');
- } catch(x) {}
- }
- if (window.ActiveXObject || window.XDomainRequest) {
- // IE8 caches even POSTs
- url += ((url.indexOf('?') === -1) ? '?' : '&') + 't='+(+new Date());
- }
-
- // Explorer tends to keep connection open, even after the
- // tab gets closed: http://bugs.jquery.com/ticket/5280
- that.unload_ref = utils.unload_add(function(){that._cleanup(true);});
+ if (!this.xhr) {
try {
- that.xhr.open(method, url, true);
- } catch(e) {
- // IE raises an exception on wrong port.
- that.emit('finish', 0, '');
- that._cleanup();
- return;
- }
-
- if (!opts || !opts.no_credentials) {
- // Mozilla docs says https://developer.mozilla.org/en/XMLHttpRequest :
- // "This never affects same-site requests."
- that.xhr.withCredentials = 'true';
+ this.xhr = new global.ActiveXObject('Microsoft.XMLHTTP');
+ } catch(x) {}
+ }
+ if (global.ActiveXObject || global.XDomainRequest) {
+ // IE8 caches even POSTs
+ url += ((url.indexOf('?') === -1) ? '?' : '&') + 't=' + Date.now();
+ }
+
+ // Explorer tends to keep connection open, even after the
+ // tab gets closed: http://bugs.jquery.com/ticket/5280
+ this.unloadRef = utils.unloadAdd(function(){
+ self._cleanup(true);
+ });
+ try {
+ this.xhr.open(method, url, true);
+ } catch(e) {
+ // IE raises an exception on wrong port.
+ this.emit('finish', 0, '');
+ this._cleanup();
+ return;
+ }
+
+ if (!opts || !opts.noCredentials) {
+ // Mozilla docs says https://developer.mozilla.org/en/XMLHttpRequest :
+ // "This never affects same-site requests."
+ this.xhr.withCredentials = 'true';
+ }
+ if (opts && opts.headers) {
+ for(var key in opts.headers) {
+ this.xhr.setRequestHeader(key, opts.headers[key]);
}
- if (opts && opts.headers) {
- for(var key in opts.headers) {
- that.xhr.setRequestHeader(key, opts.headers[key]);
+ }
+
+ this.xhr.onreadystatechange = function() {
+ if (self.xhr) {
+ var x = self.xhr;
+ var text, status;
+ switch (x.readyState) {
+ case 3:
+ // IE doesn't like peeking into responseText or status
+ // on Microsoft.XMLHTTP and readystate=3
+ try {
+ status = x.status;
+ text = x.responseText;
+ } catch (e) {}
+ // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450
+ if (status === 1223) {
+ status = 204;
}
- }
- that.xhr.onreadystatechange = function() {
- if (that.xhr) {
- var x = that.xhr;
- var text, status;
- switch (x.readyState) {
- case 3:
- // IE doesn't like peeking into responseText or status
- // on Microsoft.XMLHTTP and readystate=3
- try {
- status = x.status;
- text = x.responseText;
- } catch (e) {}
- // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450
- if (status === 1223) status = 204;
-
- // IE does return readystate == 3 for 404 answers.
- if (text && text.length > 0) {
- that.emit('chunk', status, text);
- }
- break;
- case 4:
- status = x.status;
- // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450
- if (status === 1223) status = 204;
-
- that.emit('finish', status, x.responseText);
- that._cleanup(false);
- break;
- }
+ // IE does return readystate == 3 for 404 answers.
+ if (text && text.length > 0) {
+ self.emit('chunk', status, text);
+ }
+ break;
+ case 4:
+ status = x.status;
+ // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450
+ if (status === 1223) {
+ status = 204;
}
- };
- that.xhr.send(payload);
+
+ self.emit('finish', status, x.responseText);
+ self._cleanup(false);
+ break;
+ }
+ }
+ };
+ self.xhr.send(payload);
};
AbstractXHRObject.prototype._cleanup = function(abort) {
- var that = this;
- if (!that.xhr) return;
- utils.unload_del(that.unload_ref);
+ if (!this.xhr) {
+ return;
+ }
+ utils.unloadDel(this.unloadRef);
- // IE needs this field to be a function
- that.xhr.onreadystatechange = function(){};
+ // IE needs this field to be a function
+ this.xhr.onreadystatechange = function(){};
- if (abort) {
- try {
- that.xhr.abort();
- } catch(x) {}
- }
- that.unload_ref = that.xhr = null;
+ if (abort) {
+ try {
+ this.xhr.abort();
+ } catch(x) {}
+ }
+ this.unloadRef = this.xhr = null;
};
AbstractXHRObject.prototype.close = function() {
- this.removeAllListeners();
- this._cleanup(true);
+ this.removeAllListeners();
+ this._cleanup(true);
};
-module.exports = AbstractXHRObject;
\ No newline at end of file
+module.exports = AbstractXHRObject;
diff --git a/lib/ajax-based.js b/lib/ajax-based.js
index 443a465..fa5c72d 100644
--- a/lib/ajax-based.js
+++ b/lib/ajax-based.js
@@ -1,51 +1,43 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-var BufferedSender = require('./buffered-sender');
-var Polling = require('./trans-polling');
+var util = require('util')
+ , BufferedSender = require('./buffered-sender')
+ , Polling = require('./trans-polling')
+ ;
+
+function createAjaxSender(AjaxObject) {
+ return function(url, payload, callback) {
+ var opt = {};
+ if (typeof payload === 'string') {
+ opt.headers = {'Content-type':'text/plain'};
+ }
+ var xo = new AjaxObject('POST', url + '/xhr_send', payload, opt);
+ xo.on('finish', function(status) {
+ callback(status === 200 || status === 204, 'http status ' + status);
+ });
+ return function(abortReason) {
+ callback(false, abortReason);
+ };
+ };
+}
function AjaxBasedTransport() {
}
-AjaxBasedTransport.prototype = new BufferedSender();
+util.inherits(AjaxBasedTransport, BufferedSender);
-AjaxBasedTransport.prototype.run = function(ri, trans_url,
- url_suffix, Receiver, AjaxObject) {
- var that = this;
- that.ri = ri;
- that.trans_url = trans_url;
- that.send_constructor(createAjaxSender(AjaxObject));
- that.poll = new Polling(ri, Receiver,
- trans_url + url_suffix, AjaxObject);
+AjaxBasedTransport.prototype.run = function(ri, transUrl, urlSuffix, Receiver, AjaxObject) {
+ this.ri = ri;
+ this.transUrl = transUrl;
+ this.sendConstructor(createAjaxSender(AjaxObject));
+ this.poll = new Polling(ri, Receiver, transUrl + urlSuffix, AjaxObject);
};
AjaxBasedTransport.prototype.doCleanup = function() {
- var that = this;
- if (that.poll) {
- that.poll.abort();
- that.poll = null;
- }
+ if (this.poll) {
+ this.poll.abort();
+ this.poll = null;
+ }
};
-function createAjaxSender(AjaxObject) {
- return function(url, payload, callback) {
- var opt = {};
- if (typeof payload === 'string') opt.headers = {'Content-type':'text/plain'};
- var xo = new AjaxObject('POST', url + '/xhr_send', payload, opt);
- xo.on('finish', function(status, text) {
- callback(status === 200 || status === 204,
- 'http status ' + status);
- });
- return function(abort_reason) {
- callback(false, abort_reason);
- };
- };
-}
-
-module.exports = AjaxBasedTransport;
\ No newline at end of file
+module.exports = AjaxBasedTransport;
diff --git a/lib/buffered-sender.js b/lib/buffered-sender.js
index 3a4a1b5..768df4c 100644
--- a/lib/buffered-sender.js
+++ b/lib/buffered-sender.js
@@ -1,24 +1,15 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
function BufferedSender() {}
-BufferedSender.prototype.send_constructor = function(sender) {
- var that = this;
- that.send_buffer = [];
- that.sender = sender;
+BufferedSender.prototype.sendConstructor = function(sender) {
+ this.sendBuffer = [];
+ this.sender = sender;
};
BufferedSender.prototype.doSend = function(message) {
- var that = this;
- that.send_buffer.push(message);
- if (!that.send_stop) {
- that.send_schedule();
- }
+ this.sendBuffer.push(message);
+ if (!this.sendStop) {
+ this.sendSchedule();
+ }
};
// For polling transports in a situation when in the message callback,
@@ -29,41 +20,40 @@ BufferedSender.prototype.doSend = function(message) {
// connection be started beforehand. This is only a halfmeasure and
// does not fix the big problem, but it does make the tests go more
// stable on slow networks.
-BufferedSender.prototype.send_schedule_wait = function() {
- var that = this;
- var tref;
- that.send_stop = function() {
- that.send_stop = null;
- clearTimeout(tref);
- };
- tref = setTimeout(function() {
- that.send_stop = null;
- that.send_schedule();
- }, 25);
+BufferedSender.prototype.sendScheduleWait = function() {
+ var self = this;
+ var tref;
+ this.sendStop = function() {
+ self.sendStop = null;
+ clearTimeout(tref);
+ };
+ tref = setTimeout(function() {
+ self.sendStop = null;
+ self.sendSchedule();
+ }, 25);
};
-BufferedSender.prototype.send_schedule = function() {
- var that = this;
- if (that.send_buffer.length > 0) {
- var payload = '[' + that.send_buffer.join(',') + ']';
- that.send_stop = that.sender(that.trans_url, payload, function(success, abort_reason) {
- that.send_stop = null;
- if (success === false) {
- that.ri._didClose(1006, 'Sending error ' + abort_reason);
- } else {
- that.send_schedule_wait();
- }
- });
- that.send_buffer = [];
- }
+BufferedSender.prototype.sendSchedule = function() {
+ var self = this;
+ if (this.sendBuffer.length > 0) {
+ var payload = '[' + this.sendBuffer.join(',') + ']';
+ this.sendStop = this.sender(this.transUrl, payload, function(success, abortReason) {
+ self.sendStop = null;
+ if (success === false) {
+ self.ri._didClose(1006, 'Sending error ' + abortReason);
+ } else {
+ self.sendScheduleWait();
+ }
+ });
+ this.sendBuffer = [];
+ }
};
-BufferedSender.prototype.send_destructor = function() {
- var that = this;
- if (that._send_stop) {
- that._send_stop();
- }
- that._send_stop = null;
+BufferedSender.prototype.sendDestructor = function() {
+ if (this._sendStop) {
+ this._sendStop();
+ }
+ this._sendStop = null;
};
-module.exports = BufferedSender;
\ No newline at end of file
+module.exports = BufferedSender;
diff --git a/lib/closeevent.js b/lib/closeevent.js
new file mode 100644
index 0000000..31beb6b
--- /dev/null
+++ b/lib/closeevent.js
@@ -0,0 +1,17 @@
+'use strict';
+
+var util = require('util')
+ , Event = require('./event')
+ ;
+
+function CloseEvent() {
+ Event.call(this);
+ this.initEvent('close', false, false);
+ this.wasClean = false;
+ this.code = 0;
+ this.reason = '';
+}
+
+util.inherits(CloseEvent, Event);
+
+module.exports = CloseEvent;
diff --git a/lib/event.js b/lib/event.js
new file mode 100644
index 0000000..d2d9844
--- /dev/null
+++ b/lib/event.js
@@ -0,0 +1,13 @@
+'use strict';
+
+function Event() {}
+
+Event.prototype.initEvent = function(eventType, canBubble, cancelable) {
+ this.type = eventType;
+ this.bubbles = canBubble;
+ this.cancelable = cancelable;
+ this.timeStamp = Date.now();
+ return this;
+};
+
+module.exports = global.Event || Event;
diff --git a/lib/eventtarget.js b/lib/eventtarget.js
new file mode 100644
index 0000000..f4d85ab
--- /dev/null
+++ b/lib/eventtarget.js
@@ -0,0 +1,70 @@
+'use strict';
+
+/* Simplified implementation of DOM2 EventTarget.
+ * http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
+ */
+
+function arrayIndexOf(arr, item) {
+ for (var i = 0; i < arr.length; i++) {
+ if (arr[i] === item) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+var REventTarget = function() {
+ this._listeners = {};
+};
+
+REventTarget.prototype.addEventListener = function (eventType, listener) {
+ if (!(eventType in this._listeners)) {
+ this._listeners[eventType] = [];
+ }
+ var arr = this._listeners[eventType];
+ if (arrayIndexOf(arr, listener) === -1) {
+ // Make a copy so as not to interfere with a current dispatchEvent.
+ arr = arr.concat([listener]);
+ }
+ this._listeners[eventType] = arr;
+ return;
+};
+
+REventTarget.prototype.removeEventListener = function (eventType, listener) {
+ if (!(eventType in this._listeners)) {
+ return;
+ }
+ var arr = this._listeners[eventType];
+ var idx = arrayIndexOf(arr, listener);
+ if (idx !== -1) {
+ if (arr.length > 1) {
+ // Make a copy so as not to interfer with a current dispatchEvent.
+ this._listeners[eventType] = arr.slice(0, idx).concat(arr.slice(idx + 1));
+ } else {
+ delete this._listeners[eventType];
+ }
+ return;
+ }
+ return;
+};
+
+REventTarget.prototype.dispatchEvent = function (event) {
+ var t = event.type;
+ var args = Array.prototype.slice.call(arguments, 0);
+ // TODO: This doesn't match the real behavior; per spec, onfoo get
+ // their place in line from the /first/ time they're set from
+ // non-null. Although WebKit bumps it to the end every time it's
+ // set.
+ if (this['on' + t]) {
+ this['on' + t].apply(this, args);
+ }
+ if (t in this._listeners) {
+ // Grab a reference to the listeners list. removeEventListener may alter the list.
+ var listeners = this._listeners[t];
+ for (var i = 0; i < listeners.length; i++) {
+ listeners[i].apply(this, args);
+ }
+ }
+};
+
+module.exports = global.EventTarget || REventTarget;
diff --git a/lib/facade.js b/lib/facade.js
index 0e45ac2..512d905 100644
--- a/lib/facade.js
+++ b/lib/facade.js
@@ -1,26 +1,19 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
var utils = require('./utils');
function FacadeJS() {}
FacadeJS.prototype._didClose = function (code, reason) {
- utils.postMessage('t', utils.closeFrame(code, reason));
+ utils.postMessage('t', utils.closeFrame(code, reason));
};
FacadeJS.prototype._didMessage = function (frame) {
- utils.postMessage('t', frame);
+ utils.postMessage('t', frame);
};
FacadeJS.prototype._doSend = function (data) {
- this._transport.doSend(data);
+ this._transport.doSend(data);
};
FacadeJS.prototype._doCleanup = function () {
- this._transport.doCleanup();
+ this._transport.doCleanup();
};
-module.exports = FacadeJS;
\ No newline at end of file
+module.exports = FacadeJS;
diff --git a/lib/info-receiver-fake.js b/lib/info-receiver-fake.js
index 2c48efc..d64a223 100644
--- a/lib/info-receiver-fake.js
+++ b/lib/info-receiver-fake.js
@@ -1,26 +1,19 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
var EventEmitter = require('events').EventEmitter
, util = require('util')
;
var InfoReceiverFake = function() {
- // It may not be possible to do cross domain AJAX to get the info
- // data, for example for IE7. But we want to run JSONP, so let's
- // fake the response, with rtt=2s (rto=6s).
- var that = this;
- setTimeout(function() {
- that.emit('finish', {}, 2000);
- }, 0);
+ // It may not be possible to do cross domain AJAX to get the info
+ // data, for example for IE7. But we want to run JSONP, so let's
+ // fake the response, with rtt=2s (rto=6s).
+ var self = this;
+ process.nextTick(function() {
+ self.emit('finish', {}, 2000);
+ });
};
util.inherits(InfoReceiverFake, EventEmitter);
-module.exports = InfoReceiverFake;
\ No newline at end of file
+module.exports = InfoReceiverFake;
diff --git a/lib/info-receiver-iframe.js b/lib/info-receiver-iframe.js
index b95b237..a0562ff 100644
--- a/lib/info-receiver-iframe.js
+++ b/lib/info-receiver-iframe.js
@@ -1,11 +1,4 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
var EventEmitter = require('events').EventEmitter
, util = require('util')
@@ -14,35 +7,35 @@ var EventEmitter = require('events').EventEmitter
, IframeTransport = require('./trans-iframe')
;
-function InfoReceiverIframe(base_url) {
- var that = this;
- var go = function() {
- var ifr = new IframeTransport();
- ifr.protocol = 'w-iframe-info-receiver';
- var fun = function(r) {
- if (typeof r === 'string' && r.substr(0,1) === 'm') {
- var d = JSON3.parse(r.substr(1));
- var info = d[0], rtt = d[1];
- that.emit('finish', info, rtt);
- } else {
- that.emit('finish');
- }
- ifr.doCleanup();
- ifr = null;
- };
- var mock_ri = {
- _didClose: fun,
- _didMessage: fun
- };
- ifr.i_constructor(mock_ri, base_url, base_url);
+function InfoReceiverIframe(baseUrl) {
+ var self = this;
+ var go = function() {
+ var ifr = new IframeTransport();
+ ifr.protocol = 'w-iframe-info-receiver';
+ var fun = function(r) {
+ if (typeof r === 'string' && r.substr(0,1) === 'm') {
+ var d = JSON3.parse(r.substr(1));
+ var info = d[0], rtt = d[1];
+ self.emit('finish', info, rtt);
+ } else {
+ self.emit('finish');
+ }
+ ifr.doCleanup();
+ ifr = null;
};
- if(!document.body) {
- utils.attachEvent('load', go);
- } else {
- go();
- }
+ var mockRi = {
+ _didClose: fun,
+ _didMessage: fun
+ };
+ ifr.start(mockRi, baseUrl, baseUrl);
+ };
+ if (!global.document.body) {
+ utils.attachEvent('load', go);
+ } else {
+ go();
+ }
}
util.inherits(InfoReceiverIframe, EventEmitter);
-module.exports = InfoReceiverIframe;
\ No newline at end of file
+module.exports = InfoReceiverIframe;
diff --git a/lib/info-receiver.js b/lib/info-receiver.js
index 369db6e..edee3e4 100644
--- a/lib/info-receiver.js
+++ b/lib/info-receiver.js
@@ -1,55 +1,50 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
var EventEmitter = require('events').EventEmitter
, util = require('util')
, JSON3 = require('json3')
;
-function InfoReceiver(base_url, AjaxObject) {
- var that = this;
- setTimeout(function(){
- that.doXhr(base_url, AjaxObject);
- }, 0);
+function InfoReceiver(baseUrl, AjaxObject) {
+ var self = this;
+ process.nextTick(function(){
+ self.doXhr(baseUrl, AjaxObject);
+ });
}
util.inherits(InfoReceiver, EventEmitter);
-InfoReceiver.prototype.doXhr = function(base_url, AjaxObject) {
- var that = this;
- var t0 = (new Date()).getTime();
- var xo = new AjaxObject('GET', base_url + '/info');
+InfoReceiver.prototype.doXhr = function(baseUrl, AjaxObject) {
+ var self = this;
+ var t0 = Date.now();
+ var xo = new AjaxObject('GET', baseUrl + '/info');
- var tref = setTimeout(function(){xo.ontimeout();}, 8000);
+ var tref = setTimeout(function(){xo.ontimeout();}, 8000);
- xo.on('finish', function(status, text) {
- clearTimeout(tref);
- tref = null;
- if (status === 200) {
- var rtt = (new Date()).getTime() - t0;
- var info;
- if (text) {
- try {
- info = JSON3.parse(text);
- }
- catch (e) {}
- }
- if (typeof info !== 'object') info = {};
- that.emit('finish', info, rtt);
- } else {
- that.emit('finish');
+ xo.on('finish', function(status, text) {
+ clearTimeout(tref);
+ tref = null;
+ if (status === 200) {
+ var rtt = Date.now() - t0;
+ var info;
+ if (text) {
+ try {
+ info = JSON3.parse(text);
}
- });
- xo.ontimeout = function() {
- xo.close();
- that.emit('finish');
- };
+ catch (e) {}
+ }
+ if (typeof info !== 'object') {
+ info = {};
+ }
+ self.emit('finish', info, rtt);
+ } else {
+ self.emit('finish');
+ }
+ });
+ xo.ontimeout = function() {
+ xo.close();
+ self.emit('finish');
+ };
};
-module.exports = InfoReceiver;
\ No newline at end of file
+module.exports = InfoReceiver;
diff --git a/lib/invalidaccesserror.js b/lib/invalidaccesserror.js
new file mode 100644
index 0000000..56cd232
--- /dev/null
+++ b/lib/invalidaccesserror.js
@@ -0,0 +1,10 @@
+'use strict';
+
+var util = require('util');
+
+function InvalidAccessError() {
+ Error.call(this);
+}
+util.inherits(InvalidAccessError, Error);
+
+module.exports = InvalidAccessError;
diff --git a/lib/invalidstateerror.js b/lib/invalidstateerror.js
new file mode 100644
index 0000000..daead51
--- /dev/null
+++ b/lib/invalidstateerror.js
@@ -0,0 +1,10 @@
+'use strict';
+
+var util = require('util');
+
+function InvalidStateError() {
+ Error.call(this);
+}
+util.inherits(InvalidStateError, Error);
+
+module.exports = InvalidStateError;
diff --git a/lib/location.js b/lib/location.js
new file mode 100644
index 0000000..9920ade
--- /dev/null
+++ b/lib/location.js
@@ -0,0 +1,8 @@
+'use strict';
+
+module.exports = global.location || {
+ origin: 'http://localhost:80'
+, protocol: 'http'
+, host: 'localhost'
+, port: 80
+};
diff --git a/lib/main.js b/lib/main.js
new file mode 100644
index 0000000..2789c45
--- /dev/null
+++ b/lib/main.js
@@ -0,0 +1,122 @@
+'use strict';
+
+require('./shims');
+
+var u = require('url')
+ , util = require('util')
+ , SecurityError = require('./securityerror')
+ , InvalidAccessError = require('./invalidaccesserror')
+ , InvalidStateError = require('./invalidstateerror')
+ , CloseEvent = require('./closeevent')
+ , EventTarget = require('./eventtarget')
+ , loc = require('./location')
+ ;
+
+// follow constructor steps defined at http://dev.w3.org/html5/websockets/#the-websocket-interface
+function SockJS(url, protocols) {
+ if (!(this instanceof SockJS)) {
+ return new SockJS(url, protocols);
+ }
+
+ this.readyState = SockJS.CONNECTING;
+ this.extensions = '';
+ this.protocol = '';
+
+ // Step 1 of WS spec - parse and validate the url
+ var parsedUrl = u.parse(url);
+ if (!parsedUrl.host || !parsedUrl.pathname || !parsedUrl.protocol) {
+ throw new SyntaxError("The URL '" + url + "' is invalid");
+ } else if (parsedUrl.search || parsedUrl.hash) {
+ throw new SyntaxError('The URL must not contain a query string or fragment');
+ } else if (parsedUrl.protocol !== 'http:' && parsedUrl !== 'https:') {
+ throw new SyntaxError("The URL's scheme must be either 'http:' or 'https:'. '" + parsedUrl.protocol + "' is not allowed.");
+ }
+
+ var secure = parsedUrl.protocol === 'https:';
+ // Step 2 - don't allow secure origin with an insecure protocol
+ if (loc.protocol === 'https' && !secure) {
+ throw new SecurityError('An insecure SockJS connection may not be initiated from a page loaded over HTTPS');
+ }
+
+ // Step 3 - check port access - no need here
+ // Step 4 - parse protocols argument
+ if (typeof protocols === 'undefined') {
+ protocols = [];
+ } else if (!Array.isArray(protocols)) {
+ protocols = [protocols];
+ }
+
+ // Step 5 - check protocols argument
+ var sortedProtocols = protocols.sort();
+ for (var i = 0; i < sortedProtocols.length; i++) {
+ var proto = sortedProtocols[i];
+ if (!proto) {
+ throw new SyntaxError("The protocols entry '" + proto + "' is invalid.");
+ }
+ if (i < (sortedProtocols.length - 1) && proto === sortedProtocols[i + 1]) {
+ throw new SyntaxError("The protocols entry '" + proto + "' is duplicated.");
+ }
+ }
+
+ // Step 6 - convert origin
+ this._origin = loc.origin ? loc.origin.toLowerCase() : null;
+
+ // Step 7 - start connection in background
+ this.url = parsedUrl.href;
+
+ // TODO initiate info request
+}
+
+util.inherits(SockJS, EventTarget);
+
+SockJS.prototype.close = function(code, reason) {
+ // Step 1
+ if (code && code !== 1000 && (code < 3000 || code > 4999)) {
+ throw new InvalidAccessError('Invalid code');
+ }
+ // Step 2.4 states the max is 123 bytes, but we are just checking length
+ if (reason && reason.length > 123) {
+ throw new SyntaxError('reason argument has an invalid length');
+ }
+
+ // Step 3.1
+ if (this.readyState === SockJS.CLOSING || this.readyState === SockJS.CLOSED) {
+ return;
+ }
+
+ var wasClean = true;
+ // TODO if connection not established, stop it
+
+ // TODO start closing handshake
+ this.readyState = SockJS.CLOSING;
+ process.nextTick(function () {
+ this.readyState = SockJS.CLOSED;
+
+ // TODO if required to fail the connection, fire error event
+
+ var e = new CloseEvent();
+ e.wasClean = wasClean;
+ e.code = code;
+ e.reason = reason;
+
+ this.dispatchEvent(e);
+ }.bind(this));
+};
+
+SockJS.prototype.send = function(/* data */) {
+ if (this.readyState === SockJS.CONNECTING) {
+ throw new InvalidStateError('connection has not been established yet');
+ }
+ if (this.readyState !== SockJS.OPEN) {
+ return;
+ }
+
+ // TODO send data
+};
+
+SockJS.CONNECTING = 0;
+SockJS.OPEN = 1;
+SockJS.CLOSING = 2;
+SockJS.CLOSED = 3;
+
+module.exports = SockJS;
diff --git a/lib/protocols.js b/lib/protocols.js
index f4e7125..c626fcd 100644
--- a/lib/protocols.js
+++ b/lib/protocols.js
@@ -24,17 +24,25 @@ var allProtocols = {
, 'xhr-streaming': require('./trans-xhr-streaming')
};
-module.exports = function (url, protocols_whitelist, info) {
+module.exports = function (url, protocolsWhitelist, info) {
var protocols = [];
- if (!protocols_whitelist) protocols_whitelist = [];
- if (!protocols_whitelist.length) protocols_whitelist = protocolOrdering;
+ if (!protocolsWhitelist) {
+ protocolsWhitelist = [];
+ }
+ if (!protocolsWhitelist.length) {
+ protocolsWhitelist = protocolOrdering;
+ }
for (var i = 0; i < protocolOrdering.length; i++) {
var protoName = protocolOrdering[i];
- if (protocols_whitelist.indexOf(protoName) === -1) continue;
+ if (protocolsWhitelist.indexOf(protoName) === -1) {
+ continue;
+ }
var proto = allProtocols[protoName];
- if (proto && proto.enabled(url, info)) protocols.push(proto);
+ if (proto && proto.enabled(url, info)) {
+ protocols.push(proto);
+ }
}
return protocols;
-};
\ No newline at end of file
+};
diff --git a/lib/reventtarget.js b/lib/reventtarget.js
deleted file mode 100644
index 68f99b1..0000000
--- a/lib/reventtarget.js
+++ /dev/null
@@ -1,71 +0,0 @@
-'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-/* Simplified implementation of DOM2 EventTarget.
- * http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget
- */
-
-var utils = require('./utils');
-
-var REventTarget = function() {};
-REventTarget.prototype.addEventListener = function (eventType, listener) {
- if(!this._listeners) {
- this._listeners = {};
- }
- if(!(eventType in this._listeners)) {
- this._listeners[eventType] = [];
- }
- var arr = this._listeners[eventType];
- if(utils.arrIndexOf(arr, listener) === -1) {
- // Make a copy so as not to interfere with a current dispatchEvent.
- arr = arr.concat([listener]);
- }
- this._listeners[eventType] = arr;
- return;
-};
-
-REventTarget.prototype.removeEventListener = function (eventType, listener) {
- if(!(this._listeners && (eventType in this._listeners))) {
- return;
- }
- var arr = this._listeners[eventType];
- var idx = utils.arrIndexOf(arr, listener);
- if (idx !== -1) {
- if(arr.length > 1) {
- // Make a copy so as not to interfer with a current dispatchEvent.
- this._listeners[eventType] = arr.slice(0, idx).concat( arr.slice(idx+1) );
- } else {
- delete this._listeners[eventType];
- }
- return;
- }
- return;
-};
-
-REventTarget.prototype.dispatchEvent = function (event) {
- var t = event.type;
- var args = Array.prototype.slice.call(arguments, 0);
- // TODO: This doesn't match the real behavior; per spec, onfoo get
- // their place in line from the /first/ time they're set from
- // non-null. Although WebKit bumps it to the end every time it's
- // set.
- if (this['on'+t]) {
- this['on'+t].apply(this, args);
- }
- if (this._listeners && t in this._listeners) {
- // Grab a reference to the listeners list. removeEventListener may
- // remove the list.
- var listeners = this._listeners[t];
- for(var i=0; i < listeners.length; i++) {
- listeners[i].apply(this, args);
- }
- }
-};
-
-module.exports = REventTarget;
diff --git a/lib/securityerror.js b/lib/securityerror.js
new file mode 100644
index 0000000..44561c8
--- /dev/null
+++ b/lib/securityerror.js
@@ -0,0 +1,10 @@
+'use strict';
+
+var util = require('util');
+
+function SecurityError() {
+ Error.call(this);
+}
+util.inherits(SecurityError, Error);
+
+module.exports = SecurityError;
diff --git a/lib/shims.js b/lib/shims.js
new file mode 100644
index 0000000..2f994c5
--- /dev/null
+++ b/lib/shims.js
@@ -0,0 +1,7 @@
+'use strict';
+
+if (!Date.now) {
+ Date.now = function now() {
+ return new Date().getTime();
+ };
+}
diff --git a/lib/simpleevent.js b/lib/simpleevent.js
index 35124ae..4bcd8b5 100644
--- a/lib/simpleevent.js
+++ b/lib/simpleevent.js
@@ -1,31 +1,30 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
var SimpleEvent = function(type, obj) {
- this.type = type;
- if (typeof obj !== 'undefined') {
- for(var k in obj) {
- if (!obj.hasOwnProperty(k)) continue;
- this[k] = obj[k];
- }
+ this.type = type;
+ if (typeof obj !== 'undefined') {
+ for (var k in obj) {
+ if (!obj.hasOwnProperty(k)) {
+ continue;
+ }
+ this[k] = obj[k];
}
+ }
};
SimpleEvent.prototype.toString = function() {
- var r = [];
- for(var k in this) {
- if (!this.hasOwnProperty(k)) continue;
- var v = this[k];
- if (typeof v === 'function') v = '[function]';
- r.push(k + '=' + v);
+ var r = [];
+ for (var k in this) {
+ if (!this.hasOwnProperty(k)) {
+ continue;
}
- return 'SimpleEvent(' + r.join(', ') + ')';
+ var v = this[k];
+ if (typeof v === 'function') {
+ v = '[function]';
+ }
+ r.push(k + '=' + v);
+ }
+ return 'SimpleEvent(' + r.join(', ') + ')';
};
module.exports = SimpleEvent;
diff --git a/lib/sockjs.js b/lib/sockjs.js
index 62da157..8691425 100644
--- a/lib/sockjs.js
+++ b/lib/sockjs.js
@@ -338,7 +338,7 @@ function createInfoReceiver(base_url) {
// need to start up the complex machinery. Just use ajax.
return new InfoReceiver(base_url, XHRCorsObject);
}
- if (utils.isXHRCorsCapable()) {
+ if (utils.isXHRCorsCapable() === 1) {
// XHRLocalObject -> no_credentials=true
return new InfoReceiver(base_url, XHRLocalObject);
}
diff --git a/lib/trans-eventsource.js b/lib/trans-eventsource.js
index 7ca18ae..18d85fe 100644
--- a/lib/trans-eventsource.js
+++ b/lib/trans-eventsource.js
@@ -1,12 +1,15 @@
'use strict';
-var AjaxBasedTransport = require('./ajax-based');
-var EventSourceReceiver = require('./trans-receiver-eventsource');
-var XHRLocalObject = require('./xhr-local');
+var util = require('util')
+ , AjaxBasedTransport = require('./ajax-based')
+ , EventSourceReceiver = require('./trans-receiver-eventsource')
+ , XHRLocalObject = require('./xhr-local')
+ ;
-function EventSourceTransport(ri, trans_url) {
- this.run(ri, trans_url, '/eventsource', EventSourceReceiver, XHRLocalObject);
+function EventSourceTransport(ri, transUrl) {
+ this.run(ri, transUrl, '/eventsource', EventSourceReceiver, XHRLocalObject);
}
-EventSourceTransport.prototype = new AjaxBasedTransport();
-module.exports = EventSourceTransport;
\ No newline at end of file
+util.inherits(EventSourceTransport, AjaxBasedTransport);
+
+module.exports = EventSourceTransport;
diff --git a/lib/trans-htmlfile.js b/lib/trans-htmlfile.js
index ff60c99..fc85aea 100644
--- a/lib/trans-htmlfile.js
+++ b/lib/trans-htmlfile.js
@@ -1,13 +1,15 @@
'use strict';
-var HtmlfileReceiver = require('./trans-receiver-htmlfile');
-var XHRLocalObject = require('./xhr-local');
-var AjaxBasedTransport = require('./ajax-based');
-
-function HtmlFileTransport(ri, trans_url) {
- this.run(ri, trans_url, '/htmlfile', HtmlfileReceiver, XHRLocalObject);
+var util = require('util')
+ , HtmlfileReceiver = require('./trans-receiver-htmlfile')
+ , XHRLocalObject = require('./xhr-local')
+ , AjaxBasedTransport = require('./ajax-based')
+ ;
+
+function HtmlFileTransport(ri, transUrl) {
+ this.run(ri, transUrl, '/htmlfile', HtmlfileReceiver, XHRLocalObject);
}
-HtmlFileTransport.prototype = new AjaxBasedTransport();
+util.inherits(HtmlFileTransport, AjaxBasedTransport);
-module.exports = HtmlFileTransport;
\ No newline at end of file
+module.exports = HtmlFileTransport;
diff --git a/lib/trans-iframe-eventsource.js b/lib/trans-iframe-eventsource.js
index ed0bc59..941539c 100644
--- a/lib/trans-iframe-eventsource.js
+++ b/lib/trans-iframe-eventsource.js
@@ -1,28 +1,22 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-var IframeTransport = require('./trans-iframe');
+var util = require('util')
+ , IframeTransport = require('./trans-iframe')
+ ;
function EventSourceIframeTransport() {
- var that = this;
- that.protocol = 'w-iframe-eventsource';
- that.i_constructor.apply(that, arguments);
+ this.protocol = 'w-iframe-eventsource';
+ this.start.apply(this, arguments);
}
-EventSourceIframeTransport.prototype = new IframeTransport();
+util.inherits(EventSourceIframeTransport, IframeTransport);
EventSourceIframeTransport.enabled = function () {
- return ('EventSource' in window) && IframeTransport.enabled();
+ return ('EventSource' in window) && IframeTransport.enabled();
};
EventSourceIframeTransport.transportName = 'iframe-eventsource';
-EventSourceIframeTransport.need_body = true;
+EventSourceIframeTransport.needBody = true;
EventSourceIframeTransport.roundTrips = 3; // html, javascript, eventsource
-module.exports = EventSourceIframeTransport;
\ No newline at end of file
+module.exports = EventSourceIframeTransport;
diff --git a/lib/trans-iframe-htmlfile.js b/lib/trans-iframe-htmlfile.js
index 0b25ae5..11ebff1 100644
--- a/lib/trans-iframe-htmlfile.js
+++ b/lib/trans-iframe-htmlfile.js
@@ -1,34 +1,27 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
// This transport generally works in any browser, but will cause a
// spinning cursor to appear in any browser other than IE.
// We may test this transport in all browsers - why not, but in
// production it should be only run in IE.
-var IframeTransport = require('./trans-iframe');
+var util = require('util')
+ , IframeTransport = require('./trans-iframe')
+ ;
function HtmlFileIframeTransport() {
- var that = this;
- that.protocol = 'w-iframe-htmlfile';
- that.i_constructor.apply(that, arguments);
+ this.protocol = 'w-iframe-htmlfile';
+ this.start.apply(this, arguments);
}
-// Inheritance.
-HtmlFileIframeTransport.prototype = new IframeTransport();
+util.inherits(HtmlFileIframeTransport, IframeTransport);
HtmlFileIframeTransport.enabled = function() {
- return IframeTransport.enabled();
+ return IframeTransport.enabled();
};
HtmlFileIframeTransport.transportName = 'iframe-htmlfile';
-HtmlFileIframeTransport.need_body = true;
+HtmlFileIframeTransport.needBody = true;
HtmlFileIframeTransport.roundTrips = 3; // html, javascript, htmlfile
-module.exports = HtmlFileIframeTransport;
\ No newline at end of file
+module.exports = HtmlFileIframeTransport;
diff --git a/lib/trans-iframe-xhr-polling.js b/lib/trans-iframe-xhr-polling.js
index d5137a3..04faf6d 100644
--- a/lib/trans-iframe-xhr-polling.js
+++ b/lib/trans-iframe-xhr-polling.js
@@ -1,28 +1,22 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-var IframeTransport = require('./trans-iframe');
+var util = require('util')
+ , IframeTransport = require('./trans-iframe')
+ ;
function XhrPollingIframeTransport() {
- var that = this;
- that.protocol = 'w-iframe-xhr-polling';
- that.i_constructor.apply(that, arguments);
+ this.protocol = 'w-iframe-xhr-polling';
+ this.start.apply(this, arguments);
}
-XhrPollingIframeTransport.prototype = new IframeTransport();
+util.inherits(XhrPollingIframeTransport, IframeTransport);
XhrPollingIframeTransport.enabled = function () {
- return window.XMLHttpRequest && IframeTransport.enabled();
+ return window.XMLHttpRequest && IframeTransport.enabled();
};
XhrPollingIframeTransport.transportName = 'iframe-xhr-polling';
-XhrPollingIframeTransport.need_body = true;
+XhrPollingIframeTransport.needBody = true;
XhrPollingIframeTransport.roundTrips = 3; // html, javascript, xhr
-module.exports = XhrPollingIframeTransport;
\ No newline at end of file
+module.exports = XhrPollingIframeTransport;
diff --git a/lib/trans-iframe.js b/lib/trans-iframe.js
index 5f8b446..fe625f2 100644
--- a/lib/trans-iframe.js
+++ b/lib/trans-iframe.js
@@ -1,11 +1,4 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
// Few cool transports do work only for same-origin. In order to make
// them working cross-domain we shall use iframe, served form the
@@ -20,80 +13,81 @@ var utils = require('./utils');
function IframeTransport() {}
-IframeTransport.prototype.i_constructor = function(ri, trans_url, base_url) {
- var that = this;
- that.ri = ri;
- that.origin = utils.getOrigin(base_url);
- that.base_url = base_url;
- that.trans_url = trans_url;
-
- var iframe_url = base_url + '/iframe.html';
- if (that.ri._devel) {
- iframe_url += '?t=' + (+new Date());
- }
- that.window_id = utils.random_string(8);
- iframe_url += '#' + that.window_id;
-
- that.iframeObj = utils.createIframe(iframe_url, function(r) {
- that.ri._didClose(1006, "Unable to load an iframe (" + r + ")");
- });
-
- that.onmessage_cb = utils.bind(that.onmessage, that);
- utils.attachMessage(that.onmessage_cb);
+IframeTransport.prototype.start = function(ri, transUrl, baseUrl) {
+ var self = this;
+ this.ri = ri;
+ this.origin = utils.getOrigin(baseUrl);
+ this.baseUrl = baseUrl;
+ this.transUrl = transUrl;
+
+ var iframeUrl = baseUrl + '/iframe.html';
+ if (this.ri._devel) {
+ iframeUrl += '?t=' + Date.now();
+ }
+ this.windowId = utils.randomString(8);
+ iframeUrl += '#' + this.windowId;
+
+ this.iframeObj = utils.createIframe(iframeUrl, function(r) {
+ self.ri._didClose(1006, 'Unable to load an iframe (' + r + ')');
+ });
+
+ this.onmessageCallback = utils.bind(this.onmessage, this);
+ utils.attachMessage(this.onmessageCallback);
};
IframeTransport.prototype.doCleanup = function() {
- var that = this;
- if (that.iframeObj) {
- utils.detachMessage(that.onmessage_cb);
- try {
- // When the iframe is not loaded, IE raises an exception
- // on 'contentWindow'.
- that.postMessage('c');
- } catch (x) {}
- that.iframeObj.cleanup();
- that.iframeObj = null;
- that.onmessage_cb = that.iframeObj = null;
- }
+ if (this.iframeObj) {
+ utils.detachMessage(this.onmessageCallback);
+ try {
+ // When the iframe is not loaded, IE raises an exception
+ // on 'contentWindow'.
+ this.postMessage('c');
+ } catch (x) {}
+ this.iframeObj.cleanup();
+ this.iframeObj = null;
+ this.onmessageCallback = this.iframeObj = null;
+ }
};
IframeTransport.prototype.onmessage = function(e) {
- var that = this;
-
- if (!utils.isSameOriginUrl(e.origin, that.origin)) return;
- var window_id = e.data.slice(0, 8);
- var type = e.data.slice(8, 9);
- var data = e.data.slice(9);
-
- if (window_id !== that.window_id) return console.log('Mismatched window id');
-
- switch(type) {
- case 's':
- that.iframeObj.loaded();
- // window global dependency
- that.postMessage('s', JSON3.stringify([window.SockJS.version, that.protocol, that.trans_url, that.base_url]));
- break;
- case 't':
- that.ri._didMessage(data);
- break;
- }
+ if (!utils.isSameOriginUrl(e.origin, this.origin)) {
+ return;
+ }
+ var windowId = e.data.slice(0, 8);
+ var type = e.data.slice(8, 9);
+ var data = e.data.slice(9);
+
+ if (windowId !== this.windowId) {
+ console.log('Mismatched window id');
+ return;
+ }
+
+ switch(type) {
+ case 's':
+ this.iframeObj.loaded();
+ // window global dependency
+ this.postMessage('s', JSON3.stringify([window.SockJS.version, this.protocol, this.transUrl, this.baseUrl]));
+ break;
+ case 't':
+ this.ri._didMessage(data);
+ break;
+ }
};
IframeTransport.prototype.postMessage = function(type, data) {
- var that = this;
- that.iframeObj.post(that.window_id + type + (data || ''), that.origin);
+ this.iframeObj.post(this.windowId + type + (data || ''), this.origin);
};
IframeTransport.prototype.doSend = function (message) {
- this.postMessage('m', message);
+ this.postMessage('m', message);
};
IframeTransport.enabled = function() {
- // postMessage misbehaves in konqueror 4.6.5 - the messages are delivered with
- // huge delay, or not at all.
- var konqueror = navigator && navigator.userAgent && navigator.userAgent.indexOf('Konqueror') !== -1;
- return ((typeof window.postMessage === 'function' ||
- typeof window.postMessage === 'object') && (!konqueror));
+ // postMessage misbehaves in konqueror 4.6.5 - the messages are delivered with
+ // huge delay, or not at all.
+ var konqueror = navigator && navigator.userAgent && navigator.userAgent.indexOf('Konqueror') !== -1;
+ return ((typeof window.postMessage === 'function' ||
+ typeof window.postMessage === 'object') && (!konqueror));
};
module.exports = IframeTransport;
diff --git a/lib/trans-jsonp-polling.js b/lib/trans-jsonp-polling.js
index 0dc55c8..560af91 100644
--- a/lib/trans-jsonp-polling.js
+++ b/lib/trans-jsonp-polling.js
@@ -1,11 +1,4 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
// The simplest and most robust transport, using the well-know cross
// domain hack - JSONP. This transport is quite inefficient - one
@@ -15,157 +8,113 @@
// o you will get a spinning cursor
// o for Konqueror a dumb timer is needed to detect errors
-var utils = require('./utils');
-var BufferedSender = require('./buffered-sender');
+var util = require('util')
+ , utils = require('./utils')
+ , BufferedSender = require('./buffered-sender')
+ ;
-function JsonPTransport(ri, trans_url) {
- utils.polluteGlobalNamespace();
- var that = this;
- that.ri = ri;
- that.trans_url = trans_url;
- that.send_constructor(jsonPGenericSender);
- that._schedule_recv();
-}
+// Abstract away code that handles global namespace pollution.
+var jsonPReceiverWrapper = function(url, constructReceiver, userCallback) {
+ var id = 'a' + utils.randomString(6);
+ var urlId = url + '?c=' + encodeURIComponent(utils.WPrefix + '.' + id);
-// Inheritnace
-JsonPTransport.prototype = new BufferedSender();
+ // Unfortunately it is not possible to abort loading of the
+ // script. We need to keep track of frake close frames.
+ var aborting = 0;
-JsonPTransport.prototype._schedule_recv = function() {
- var that = this;
- var callback = function(data) {
- that._recv_stop = null;
- if (data) {
- // no data - heartbeat;
- if (!that._is_closing) {
- that.ri._didMessage(data);
- }
- }
- // The message can be a close message, and change is_closing state.
- if (!that._is_closing) {
- that._schedule_recv();
- }
- };
- that._recv_stop = jsonPReceiverWrapper(that.trans_url + '/jsonp',
- jsonPGenericReceiver, callback);
-};
-
-JsonPTransport.enabled = function() {
- return true;
-};
-
-JsonPTransport.transportName = 'jsonp-polling';
-JsonPTransport.need_body = true;
+ // 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;
+ }
+ };
-JsonPTransport.prototype.doCleanup = function() {
- var that = this;
- that._is_closing = true;
- if (that._recv_stop) {
- that._recv_stop();
+ var closeScript = constructReceiver(urlId, callback);
+ window[utils.WPrefix][id] = closeScript;
+ var stop = function() {
+ if (window[utils.WPrefix][id]) {
+ aborting = 1;
+ window[utils.WPrefix][id](utils.closeFrame(1000, 'JSONP user aborted read'));
}
- that.ri = that._recv_stop = null;
- that.send_destructor();
+ };
+ return stop;
};
-module.exports = JsonPTransport;
-
-// Abstract away code that handles global namespace pollution.
-var jsonPReceiverWrapper = function(url, constructReceiver, user_callback) {
- var id = 'a' + utils.random_string(6);
- var url_id = url + '?c=' + encodeURIComponent(utils.WPrefix + '.' + id);
+function jsonPGenericSender(url, payload, callback) {
+ var form = window._sendForm;
+ var area = window._sendArea;
- // Unfortunately it is not possible to abort loading of the
- // script. We need to keep track of frake close frames.
- var aborting = 0;
+ 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' + utils.randomString(8);
+ form.target = id;
+ form.action = url + '/jsonp_send?i=' + id;
- // 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];
- user_callback(frame);
- break;
- case 1:
- // Fake close frame - emit but don't delete hook.
- user_callback(frame);
- aborting = 2;
- break;
- case 2:
- // Got frame after connection was closed, delete hook, don't emit.
- delete window[utils.WPrefix][id];
- break;
- }
- };
+ 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';
- var close_script = constructReceiver(url_id, callback);
- window[utils.WPrefix][id] = close_script;
- var stop = function() {
- if (window[utils.WPrefix][id]) {
- aborting = 1;
- window[utils.WPrefix][id](utils.closeFrame(1000, "JSONP user aborted read"));
- }
- };
- return stop;
-};
+ try {
+ area.value = payload;
+ } catch(e) {
+ utils.log('Your browser is seriously broken. Go home! ' + e.message);
+ }
+ form.submit();
-function jsonPGenericSender(url, payload, callback) {
- var form = window._send_form;
- var area = window._send_area;
-
- if (!form) {
- form = window._send_form = document.createElement('form');
- area = window._send_area = 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' + utils.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;
+ var completed = function() {
+ if (!iframe.onerror) {
+ return;
}
- 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);
+ 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(true);
+ };
+ iframe.onerror = iframe.onload = completed;
+ iframe.onreadystatechange = function() {
+ if (iframe.readyState === 'complete') {
+ completed();
}
- form.submit();
-
- var completed = function(e) {
- 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(true);
- };
- iframe.onerror = iframe.onload = completed;
- iframe.onreadystatechange = function(e) {
- if (iframe.readyState == 'complete') completed();
- };
- return completed;
+ };
+ return completed;
}
// Parts derived from Socket.io:
@@ -173,106 +122,153 @@ function jsonPGenericSender(url, payload, callback) {
// 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 = document.createElement('script');
- var script2; // Opera synchronous load trick.
- var close_script = function(frame) {
- 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(frame);
- callback = null;
- }
- };
+ var tref;
+ var script = document.createElement('script');
+ var script2; // Opera synchronous load trick.
+ var closeScript = function(frame) {
+ 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(frame);
+ callback = null;
+ }
+ };
- // IE9 fires 'error' event after orsc or before, in random order.
- var loaded_okay = false;
- var error_timer = null;
+ // IE9 fires 'error' event after orsc or before, in random order.
+ var loadedOkay = false;
+ var errorTimer = null;
- script.id = 'a' + utils.random_string(8);
- script.src = url;
- script.type = 'text/javascript';
- script.charset = 'UTF-8';
- script.onerror = function(e) {
- if (!error_timer) {
- // Delay firing close_script.
- error_timer = setTimeout(function() {
- if (!loaded_okay) {
- close_script(utils.closeFrame(
- 1006,
- "JSONP script loaded abnormally (onerror)"));
- }
- }, 1000);
+ script.id = 'a' + utils.randomString(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(utils.closeFrame(
+ 1006,
+ 'JSONP script loaded abnormally (onerror)'));
}
- };
- script.onload = function(e) {
- close_script(utils.closeFrame(1006, "JSONP script loaded abnormally (onload)"));
- };
+ }, 1000);
+ }
+ };
+ script.onload = function() {
+ closeScript(utils.closeFrame(1006, 'JSONP script loaded abnormally (onload)'));
+ };
- script.onreadystatechange = function(e) {
- if (/loaded|closed/.test(script.readyState)) {
- if (script && script.htmlFor && script.onclick) {
- loaded_okay = true;
- try {
- // In IE, actually execute the script.
- script.onclick();
- } catch (x) {}
- }
- if (script) {
- close_script(utils.closeFrame(1006, "JSONP script loaded abnormally (onreadystatechange)"));
- }
- }
- };
- // 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;
- }
+ 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(utils.closeFrame(1006, 'JSONP script loaded abnormally (onreadystatechange)'));
+ }
}
- if (typeof script.async !== 'undefined') {
- script.async = true;
+ };
+ // 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() {
- close_script(utils.closeFrame(1006, "JSONP script loaded abnormally (timeout)"));
- }, 35000);
+ // Fallback mostly for Konqueror - stupid timer, 35 seconds shall be plenty.
+ tref = setTimeout(function() {
+ closeScript(utils.closeFrame(1006, 'JSONP script loaded abnormally (timeout)'));
+ }, 35000);
- var head = document.getElementsByTagName('head')[0];
- head.insertBefore(script, head.firstChild);
- if (script2) {
- head.insertBefore(script2, head.firstChild);
+ var head = document.getElementsByTagName('head')[0];
+ head.insertBefore(script, head.firstChild);
+ if (script2) {
+ head.insertBefore(script2, head.firstChild);
+ }
+ return closeScript;
+}
+
+function JsonPTransport(ri, transUrl) {
+ utils.polluteGlobalNamespace();
+ this.ri = ri;
+ this.transUrl = transUrl;
+ this.sendConstructor(jsonPGenericSender);
+ this._scheduleReceiver();
+}
+
+util.inherits(JsonPTransport, BufferedSender);
+
+JsonPTransport.prototype._scheduleReceiver = function() {
+ var self = this;
+ var callback = function(data) {
+ self._receiveStop = null;
+ if (data) {
+ // no data - heartbeat;
+ if (!self._isClosing) {
+ self.ri._didMessage(data);
+ }
}
- return close_script;
-}
\ No newline at end of file
+ // The message can be a close message, and change is_closing state.
+ if (!self._isClosing) {
+ self._scheduleReceiver();
+ }
+ };
+ this._receiveStop = jsonPReceiverWrapper(this.transUrl + '/jsonp',
+ jsonPGenericReceiver, callback);
+};
+
+JsonPTransport.enabled = function() {
+ return true;
+};
+
+JsonPTransport.transportName = 'jsonp-polling';
+JsonPTransport.needBody = true;
+
+JsonPTransport.prototype.doCleanup = function() {
+ this._isClosing = true;
+ if (this._receiveStop) {
+ this._receiveStop();
+ }
+ this.ri = this._receiveStop = null;
+ this.sendDestructor();
+};
+
+module.exports = JsonPTransport;
diff --git a/lib/trans-polling.js b/lib/trans-polling.js
index ea183b2..47459e3 100644
--- a/lib/trans-polling.js
+++ b/lib/trans-polling.js
@@ -1,47 +1,36 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-function Polling(ri, Receiver, recv_url, AjaxObject) {
- var that = this;
- that.ri = ri;
- that.Receiver = Receiver;
- that.recv_url = recv_url;
- that.AjaxObject = AjaxObject;
- that._scheduleRecv();
+function Polling(ri, Receiver, receiveUrl, AjaxObject) {
+ this.ri = ri;
+ this.Receiver = Receiver;
+ this.receiveUrl = receiveUrl;
+ this.AjaxObject = AjaxObject;
+ this._scheduleReceiver();
}
-Polling.prototype._scheduleRecv = function() {
- var that = this;
- var poll = that.poll = new that.Receiver(that.recv_url, that.AjaxObject);
- var msg_counter = 0;
- poll.onmessage = function(e) {
- msg_counter += 1;
- that.ri._didMessage(e.data);
- };
- poll.onclose = function(e) {
- that.poll = poll = poll.onmessage = poll.onclose = null;
- if (!that.poll_is_closing) {
- if (e.reason === 'permanent') {
- that.ri._didClose(1006, 'Polling error (' + e.reason + ')');
- } else {
- that._scheduleRecv();
- }
- }
- };
+Polling.prototype._scheduleReceiver = function() {
+ var self = this;
+ var poll = this.poll = new this.Receiver(this.receiveUrl, this.AjaxObject);
+ poll.onmessage = function(e) {
+ self.ri._didMessage(e.data);
+ };
+ poll.onclose = function(e) {
+ self.poll = poll = poll.onmessage = poll.onclose = null;
+ if (!self.pollIsClosing) {
+ if (e.reason === 'permanent') {
+ self.ri._didClose(1006, 'Polling error (' + e.reason + ')');
+ } else {
+ self._scheduleReceiver();
+ }
+ }
+ };
};
Polling.prototype.abort = function() {
- var that = this;
- that.poll_is_closing = true;
- if (that.poll) {
- that.poll.abort();
- }
+ this.pollIsClosing = true;
+ if (this.poll) {
+ this.poll.abort();
+ }
};
-module.exports = Polling;
\ No newline at end of file
+module.exports = Polling;
diff --git a/lib/trans-receiver-eventsource.js b/lib/trans-receiver-eventsource.js
index 2f005cf..0943062 100644
--- a/lib/trans-receiver-eventsource.js
+++ b/lib/trans-receiver-eventsource.js
@@ -1,47 +1,40 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-var SimpleEvent = require('./simpleevent');
-var REventTarget = require('./reventtarget');
+var util = require('util')
+ , SimpleEvent = require('./simpleevent')
+ , REventTarget = require('./reventtarget')
+ ;
function EventSourceReceiver(url) {
- var that = this;
- var es = new EventSource(url);
- es.onmessage = function(e) {
- that.dispatchEvent(new SimpleEvent('message',
- {'data': decodeURI(e.data)}));
- };
- that.es_close = es.onerror = function(e, abort_reason) {
- // ES on reconnection has readyState = 0 or 1.
- // on network error it's CLOSED = 2
- var reason = abort_reason ? 'user' :
- (es.readyState !== 2 ? 'network' : 'permanent');
- that.es_close = es.onmessage = es.onerror = null;
- // EventSource reconnects automatically.
- es.close();
- es = null;
- // Safari and chrome < 15 crash if we close window before
- // waiting for ES cleanup. See:
- // https://code.google.com/p/chromium/issues/detail?id=89155
- setTimeout(function() {
- that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
- }, 200);
- };
+ var self = this;
+ var es = new EventSource(url);
+ es.onmessage = function(e) {
+ self.dispatchEvent(new SimpleEvent('message', {'data': decodeURI(e.data)}));
+ };
+ this.esClose = es.onerror = function(e, abortReason) {
+ // ES on reconnection has readyState = 0 or 1.
+ // on network error it's CLOSED = 2
+ var reason = abortReason ? 'user' :
+ (es.readyState !== 2 ? 'network' : 'permanent');
+ self.esClose = es.onmessage = es.onerror = null;
+ // EventSource reconnects automatically.
+ es.close();
+ es = null;
+ // Safari and chrome < 15 crash if we close window before
+ // waiting for ES cleanup. See:
+ // https://code.google.com/p/chromium/issues/detail?id=89155
+ setTimeout(function() {
+ self.dispatchEvent(new SimpleEvent('close', {reason: reason}));
+ }, 200);
+ };
}
-EventSourceReceiver.prototype = new REventTarget();
+util.inherits(EventSourceReceiver, REventTarget);
EventSourceReceiver.prototype.abort = function() {
- var that = this;
- if (that.es_close) {
- that.es_close({}, true);
- }
+ if (this.esClose) {
+ this.esClose({}, true);
+ }
};
module.exports = EventSourceReceiver;
diff --git a/lib/trans-receiver-htmlfile.js b/lib/trans-receiver-htmlfile.js
index 54270e2..beb0fc4 100644
--- a/lib/trans-receiver-htmlfile.js
+++ b/lib/trans-receiver-htmlfile.js
@@ -1,72 +1,66 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-var utils = require('./utils');
-var SimpleEvent = require('./simpleevent');
-var REventTarget = require('./reventtarget');
+var util = require('util')
+ , utils = require('./utils')
+ , SimpleEvent = require('./simpleevent')
+ , REventTarget = require('./reventtarget')
+ ;
-var _is_ie_htmlfile_capable;
+var _isIeHtmlfileCapable;
var isIeHtmlfileCapable = function() {
- if (_is_ie_htmlfile_capable === undefined) {
- if ('ActiveXObject' in window) {
- try {
- _is_ie_htmlfile_capable = !!new ActiveXObject('htmlfile');
- } catch (x) {}
- } else {
- _is_ie_htmlfile_capable = false;
- }
+ if (_isIeHtmlfileCapable === undefined) {
+ if ('ActiveXObject' in window) {
+ try {
+ _isIeHtmlfileCapable = !!new ActiveXObject('htmlfile');
+ } catch (x) {}
+ } else {
+ _isIeHtmlfileCapable = false;
}
- return _is_ie_htmlfile_capable;
+ }
+ return _isIeHtmlfileCapable;
};
function HtmlfileReceiver(url) {
- var that = this;
- utils.polluteGlobalNamespace();
+ var self = this;
+ utils.polluteGlobalNamespace();
- that.id = 'a' + utils.random_string(6);
- url += ((url.indexOf('?') === -1) ? '?' : '&') +
- 'c=' + decodeURIComponent(utils.WPrefix + '.' + that.id);
+ this.id = 'a' + utils.randomString(6);
+ url += ((url.indexOf('?') === -1) ? '?' : '&') +
+ 'c=' + decodeURIComponent(utils.WPrefix + '.' + this.id);
- var constructor = isIeHtmlfileCapable() ?
- utils.createHtmlfile : utils.createIframe;
+ var constructor = isIeHtmlfileCapable() ?
+ utils.createHtmlfile : utils.createIframe;
- var iframeObj;
- window[utils.WPrefix][that.id] = {
- start: function () {
- iframeObj.loaded();
- },
- message: function (data) {
- that.dispatchEvent(new SimpleEvent('message', {'data': data}));
- },
- stop: function () {
- that.iframe_close({}, 'network');
- }
- };
- that.iframe_close = function(e, abort_reason) {
- iframeObj.cleanup();
- that.iframe_close = iframeObj = null;
- delete window[utils.WPrefix][that.id];
- that.dispatchEvent(new SimpleEvent('close', {reason: abort_reason}));
- };
- iframeObj = constructor(url, function(e) {
- that.iframe_close({}, 'permanent');
- });
+ var iframeObj;
+ window[utils.WPrefix][this.id] = {
+ start: function () {
+ iframeObj.loaded();
+ },
+ message: function (data) {
+ self.dispatchEvent(new SimpleEvent('message', {'data': data}));
+ },
+ stop: function () {
+ self.iframeClose({}, 'network');
+ }
+ };
+ this.iframeClose = function(e, abortReason) {
+ iframeObj.cleanup();
+ self.iframeClose = iframeObj = null;
+ delete window[utils.WPrefix][self.id];
+ self.dispatchEvent(new SimpleEvent('close', {reason: abortReason}));
+ };
+ iframeObj = constructor(url, function() {
+ self.iframeClose({}, 'permanent');
+ });
}
-HtmlfileReceiver.prototype = new REventTarget();
+util.inherits(HtmlfileReceiver, REventTarget);
HtmlfileReceiver.prototype.abort = function() {
- var that = this;
- if (that.iframe_close) {
- that.iframe_close({}, 'user');
- }
+ if (this.iframeClose) {
+ this.iframeClose({}, 'user');
+ }
};
module.exports = HtmlfileReceiver;
diff --git a/lib/trans-receiver-iframe.js b/lib/trans-receiver-iframe.js
index 8490f8d..31dad88 100644
--- a/lib/trans-receiver-iframe.js
+++ b/lib/trans-receiver-iframe.js
@@ -1,17 +1,18 @@
'use strict';
-var XHRLocalObject = require('./xhr-local');
-var JSON3 = require('json3');
-var InfoReceiver = require('./info-receiver');
+var XHRLocalObject = require('./xhr-local')
+ , JSON3 = require('json3')
+ , InfoReceiver = require('./info-receiver')
+ ;
-function WInfoReceiverIframe(ri, _trans_url, base_url) {
- var ir = new InfoReceiver(base_url, XHRLocalObject);
- ir.on('finish', function(info, rtt) {
- ri._didMessage('m'+JSON3.stringify([info, rtt]));
- ri._didClose();
- });
+function WInfoReceiverIframe(ri, transUrl, baseUrl) {
+ var ir = new InfoReceiver(baseUrl, XHRLocalObject);
+ ir.on('finish', function(info, rtt) {
+ ri._didMessage('m' + JSON3.stringify([info, rtt]));
+ ri._didClose();
+ });
}
WInfoReceiverIframe.prototype.doCleanup = function() {};
-module.exports = WInfoReceiverIframe;
\ No newline at end of file
+module.exports = WInfoReceiverIframe;
diff --git a/lib/trans-receiver-xhr.js b/lib/trans-receiver-xhr.js
index c6a3502..8766cd5 100644
--- a/lib/trans-receiver-xhr.js
+++ b/lib/trans-receiver-xhr.js
@@ -1,49 +1,47 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-var SimpleEvent = require('./simpleevent');
-var REventTarget = require('./reventtarget');
+var util = require('util')
+ , SimpleEvent = require('./simpleevent')
+ , REventTarget = require('./reventtarget')
+ ;
function XhrReceiver(url, AjaxObject) {
- var that = this;
- var buf_pos = 0;
+ var self = this;
+ var bufPos = 0;
- that.xo = new AjaxObject('POST', url, null);
- function chunkHandler(status, text) {
- if (status !== 200) return;
- while (1) {
- var buf = text.slice(buf_pos);
- var p = buf.indexOf('\n');
- if (p === -1) break;
- buf_pos += p+1;
- var msg = buf.slice(0, p);
- that.dispatchEvent(new SimpleEvent('message', {data: msg}));
- }
+ this.xo = new AjaxObject('POST', url, null);
+ function chunkHandler(status, text) {
+ if (status !== 200) {
+ return;
}
- that.xo.on('chunk', chunkHandler);
- that.xo.on('finish', function(status, text) {
- chunkHandler(status, text);
- that.xo = null;
- var reason = status === 200 ? 'network' : 'permanent';
- that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
- });
+ for(;;) {
+ var buf = text.slice(bufPos);
+ var p = buf.indexOf('\n');
+ if (p === -1) {
+ break;
+ }
+ bufPos += p + 1;
+ var msg = buf.slice(0, p);
+ self.dispatchEvent(new SimpleEvent('message', {data: msg}));
+ }
+ }
+ this.xo.on('chunk', chunkHandler);
+ this.xo.on('finish', function(status, text) {
+ chunkHandler(status, text);
+ self.xo = null;
+ var reason = status === 200 ? 'network' : 'permanent';
+ self.dispatchEvent(new SimpleEvent('close', {reason: reason}));
+ });
}
-XhrReceiver.prototype = new REventTarget();
+util.inherits(XhrReceiver, REventTarget);
XhrReceiver.prototype.abort = function() {
- var that = this;
- if (that.xo) {
- that.xo.close();
- that.dispatchEvent(new SimpleEvent('close', {reason: 'user'}));
- that.xo = null;
- }
+ if (this.xo) {
+ this.xo.close();
+ this.dispatchEvent(new SimpleEvent('close', {reason: 'user'}));
+ this.xo = null;
+ }
};
-module.exports = XhrReceiver;
\ No newline at end of file
+module.exports = XhrReceiver;
diff --git a/lib/trans-websocket.js b/lib/trans-websocket.js
index 13ca273..b20fe00 100644
--- a/lib/trans-websocket.js
+++ b/lib/trans-websocket.js
@@ -1,60 +1,53 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
+
var utils = require('./utils');
-function WebSocketTransport(ri, trans_url) {
- var that = this;
- var url = trans_url + '/websocket';
- if (url.slice(0, 5) === 'https') {
- url = 'wss' + url.slice(5);
- } else {
- url = 'ws' + url.slice(4);
- }
- that.ri = ri;
- that.url = url;
- var Constructor = window.WebSocket || window.MozWebSocket;
+function WebSocketTransport(ri, transUrl) {
+ var self = this;
+ var url = transUrl + '/websocket';
+ if (url.slice(0, 5) === 'https') {
+ url = 'wss' + url.slice(5);
+ } else {
+ url = 'ws' + url.slice(4);
+ }
+ this.ri = ri;
+ this.url = url;
+ var Constructor = window.WebSocket || window.MozWebSocket;
- that.ws = new Constructor(that.url);
- that.ws.onmessage = function(e) {
- that.ri._didMessage(e.data);
- };
- // Firefox has an interesting bug. If a websocket connection is
- // created after onunload, it stays alive even when user
- // navigates away from the page. In such situation let's lie -
- // let's not open the ws connection at all. See:
- // https://github.com/sockjs/sockjs-client/issues/28
- // https://bugzilla.mozilla.org/show_bug.cgi?id=696085
- that.unload_ref = utils.unload_add(function(){
- that.ws.close();
- });
- that.ws.onclose = that.ws.onerror = function() {
- that.ri._didMessage(utils.closeFrame(1006, "WebSocket connection broken"));
- };
+ this.ws = new Constructor(this.url);
+ this.ws.onmessage = function(e) {
+ self.ri._didMessage(e.data);
+ };
+ // Firefox has an interesting bug. If a websocket connection is
+ // created after onunload, it stays alive even when user
+ // navigates away from the page. In such situation let's lie -
+ // let's not open the ws connection at all. See:
+ // https://github.com/sockjs/sockjs-client/issues/28
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=696085
+ this.unloadRef = utils.unloadAdd(function(){
+ self.ws.close();
+ });
+ this.ws.onclose = this.ws.onerror = function() {
+ self.ri._didMessage(utils.closeFrame(1006, 'WebSocket connection broken'));
+ };
}
WebSocketTransport.prototype.doSend = function(data) {
- this.ws.send('[' + data + ']');
+ this.ws.send('[' + data + ']');
};
WebSocketTransport.prototype.doCleanup = function() {
- var that = this;
- var ws = that.ws;
- if (ws) {
- ws.onmessage = ws.onclose = ws.onerror = null;
- ws.close();
- utils.unload_del(that.unload_ref);
- that.unload_ref = that.ri = that.ws = null;
- }
+ var ws = this.ws;
+ if (ws) {
+ ws.onmessage = ws.onclose = ws.onerror = null;
+ ws.close();
+ utils.unloadDel(this.unloadRef);
+ this.unloadRef = this.ri = this.ws = null;
+ }
};
WebSocketTransport.enabled = function() {
- return !!(window.WebSocket || window.MozWebSocket);
+ return !!(window.WebSocket || window.MozWebSocket);
};
WebSocketTransport.transportName = 'websocket';
diff --git a/lib/trans-xdr-polling.js b/lib/trans-xdr-polling.js
index ceb210c..f544191 100644
--- a/lib/trans-xdr-polling.js
+++ b/lib/trans-xdr-polling.js
@@ -1,25 +1,20 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-var AjaxBasedTransport = require('./ajax-based');
-var XdrStreamingTransport = require('./trans-xdr-streaming');
-var XhrReceiver = require('./trans-receiver-xhr');
-var XDRObject = require('./xdr');
+var util = require('util')
+ , AjaxBasedTransport = require('./ajax-based')
+ , XdrStreamingTransport = require('./trans-xdr-streaming')
+ , XhrReceiver = require('./trans-receiver-xhr')
+ , XDRObject = require('./xdr')
+ ;
-function XdrPollingTransport(ri, trans_url) {
- this.run(ri, trans_url, '/xhr', XhrReceiver, XDRObject);
+function XdrPollingTransport(ri, transUrl) {
+ this.run(ri, transUrl, '/xhr', XhrReceiver, XDRObject);
}
-XdrPollingTransport.prototype = new AjaxBasedTransport();
+util.inherits(XdrPollingTransport, AjaxBasedTransport);
XdrPollingTransport.enabled = XdrStreamingTransport.enabled;
XdrPollingTransport.transportName = 'xdr-polling';
XdrPollingTransport.roundTrips = 2; // preflight, ajax
-module.exports = XdrPollingTransport;
\ No newline at end of file
+module.exports = XdrPollingTransport;
diff --git a/lib/trans-xdr-streaming.js b/lib/trans-xdr-streaming.js
index ed71485..9da1f79 100644
--- a/lib/trans-xdr-streaming.js
+++ b/lib/trans-xdr-streaming.js
@@ -1,35 +1,32 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-var AjaxBasedTransport = require('./ajax-based');
-var XhrReceiver = require('./trans-receiver-xhr');
-var XDRObject = require('./xdr');
-var utils = require('./utils');
+
+var util = require('util')
+ , AjaxBasedTransport = require('./ajax-based')
+ , XhrReceiver = require('./trans-receiver-xhr')
+ , XDRObject = require('./xdr')
+ , utils = require('./utils')
+ ;
// According to:
// http://stackoverflow.com/questions/1641507/detect-browser-support-for-cross-domain-xmlhttprequests
// http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
-function XdrStreamingTransport(ri, trans_url) {
- this.run(ri, trans_url, '/xhr_streaming', XhrReceiver, XDRObject);
+function XdrStreamingTransport(ri, transUrl) {
+ this.run(ri, transUrl, '/xhr_streaming', XhrReceiver, XDRObject);
}
-XdrStreamingTransport.prototype = new AjaxBasedTransport();
+util.inherits(XdrStreamingTransport, AjaxBasedTransport);
XdrStreamingTransport.enabled = function(url, info) {
- if (info.cookie_needed || info.null_origin) return false;
- // IE 8/9 if the request target uses the same scheme - #79
- return !!(window.XDomainRequest && document.domain && utils.isSameOriginScheme(url));
+ if (info.cookie_needed || info.null_origin) {
+ return false;
+ }
+ // IE 8/9 if the request target uses the same scheme - #79
+ return !!(window.XDomainRequest && document.domain && utils.isSameOriginScheme(url));
};
XdrStreamingTransport.transportName = 'xdr-streaming';
XdrStreamingTransport.roundTrips = 2; // preflight, ajax
-module.exports = XdrStreamingTransport;
\ No newline at end of file
+module.exports = XdrStreamingTransport;
diff --git a/lib/trans-xhr-polling.js b/lib/trans-xhr-polling.js
index 4c55ef5..a55eeba 100644
--- a/lib/trans-xhr-polling.js
+++ b/lib/trans-xhr-polling.js
@@ -1,30 +1,29 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-var AjaxBasedTransport = require('./ajax-based');
-var XhrReceiver = require('./trans-receiver-xhr');
-var XHRCorsObject = require('./xhr-cors');
-var utils = require('./utils');
+var util = require('util')
+ , AjaxBasedTransport = require('./ajax-based')
+ , XhrReceiver = require('./trans-receiver-xhr')
+ , XHRCorsObject = require('./xhr-cors')
+ , utils = require('./utils')
+ ;
-function XhrPollingTransport(ri, trans_url) {
- this.run(ri, trans_url, '/xhr', XhrReceiver, XHRCorsObject);
+function XhrPollingTransport(ri, transUrl) {
+ this.run(ri, transUrl, '/xhr', XhrReceiver, XHRCorsObject);
}
-XhrPollingTransport.prototype = new AjaxBasedTransport();
+util.inherits(XhrPollingTransport, AjaxBasedTransport);
XhrPollingTransport.enabled = function(url, info) {
- if (info.null_origin) return false;
- if (window.XMLHttpRequest && utils.isSameOriginUrl(url)) return true;
- return utils.isXHRCorsCapable() === 1;
+ if (info.null_origin) {
+ return false;
+ }
+ if (window.XMLHttpRequest && utils.isSameOriginUrl(url)) {
+ return true;
+ }
+ return utils.isXHRCorsCapable() === 1;
};
XhrPollingTransport.transportName = 'xhr-polling';
XhrPollingTransport.roundTrips = 2; // preflight, ajax
-module.exports = XhrPollingTransport;
\ No newline at end of file
+module.exports = XhrPollingTransport;
diff --git a/lib/trans-xhr-streaming.js b/lib/trans-xhr-streaming.js
index c394c81..12014c8 100644
--- a/lib/trans-xhr-streaming.js
+++ b/lib/trans-xhr-streaming.js
@@ -1,30 +1,31 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-var AjaxBasedTransport = require('./ajax-based');
-var XhrReceiver = require('./trans-receiver-xhr');
-var XHRCorsObject = require('./xhr-cors');
-var utils = require('./utils');
-
-function XhrStreamingTransport(ri, trans_url) {
- this.run(ri, trans_url, '/xhr_streaming', XhrReceiver, XHRCorsObject);
+
+var util = require('util')
+ , AjaxBasedTransport = require('./ajax-based')
+ , XhrReceiver = require('./trans-receiver-xhr')
+ , XHRCorsObject = require('./xhr-cors')
+ , utils = require('./utils')
+ ;
+
+function XhrStreamingTransport(ri, transUrl) {
+ this.run(ri, transUrl, '/xhr_streaming', XhrReceiver, XHRCorsObject);
}
-XhrStreamingTransport.prototype = new AjaxBasedTransport();
+util.inherits(XhrStreamingTransport, AjaxBasedTransport);
XhrStreamingTransport.enabled = function(url, info) {
- if (info.null_origin) return false;
- // Opera doesn't support xhr-streaming
- if (/opera/i.test(navigator.userAgent)) return false;
- if (window.XMLHttpRequest && utils.isSameOriginUrl(url)) return true;
-
- return utils.isXHRCorsCapable() === 1;
+ if (info.null_origin) {
+ return false;
+ }
+ // Opera doesn't support xhr-streaming
+ if (/opera/i.test(navigator.userAgent)) {
+ return false;
+ }
+ if (window.XMLHttpRequest && utils.isSameOriginUrl(url)) {
+ return true;
+ }
+
+ return utils.isXHRCorsCapable() === 1;
};
XhrStreamingTransport.transportName = 'xhr-streaming';
@@ -32,6 +33,6 @@ XhrStreamingTransport.roundTrips = 2; // preflight, ajax
// Safari gets confused when a streaming ajax request is started
// before onload. This causes the load indicator to spin indefinetely.
-XhrStreamingTransport.need_body = true;
+XhrStreamingTransport.needBody = true;
-module.exports = XhrStreamingTransport;
\ No newline at end of file
+module.exports = XhrStreamingTransport;
diff --git a/lib/utils.js b/lib/utils.js
index 5631950..3913cf8 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -1,299 +1,313 @@
'use strict';
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
var JSON3 = require('json3');
+var IframeTransport = require('./trans-iframe');
var utils = {};
var getRandomBytes;
if (window && window.crypto && window.crypto.getRandomValues) {
- getRandomBytes = function (length) {
- var bytes = new Uint8Array(length);
- window.crypto.getRandomValues(bytes);
- return bytes;
- };
+ getRandomBytes = function (length) {
+ var bytes = new Uint8Array(length);
+ window.crypto.getRandomValues(bytes);
+ return bytes;
+ };
} else {
- getRandomBytes = function (length) {
- var bytes = new Uint8Array(length);
- for (var i=0; i < length; i++) bytes[i] = Math.floor(Math.random() * 256);
- return bytes;
- };
+ getRandomBytes = function (length) {
+ var bytes = new Uint8Array(length);
+ for (var i = 0; i < length; i++) {
+ bytes[i] = Math.floor(Math.random() * 256);
+ }
+ return bytes;
+ };
}
// This string has length 32, a power of 2, so the modulus doesn't introduce a
// bias.
-var random_string_chars = 'abcdefghijklmnopqrstuvwxyz012345';
-utils.random_string = function(length) {
- var max = random_string_chars.length;
- var bytes = getRandomBytes(length);
- var ret = [];
- for (var i=0; i < length; i++) {
- ret.push( random_string_chars[bytes[i] % max] );
- }
- return ret.join('');
+var _randomStringChars = 'abcdefghijklmnopqrstuvwxyz012345';
+utils.randomString = function(length) {
+ var max = _randomStringChars.length;
+ var bytes = getRandomBytes(length);
+ var ret = [];
+ for (var i = 0; i < length; i++) {
+ ret.push( _randomStringChars[bytes[i] % max] );
+ }
+ return ret.join('');
};
-utils.random_number = function(max) {
- return Math.floor(Math.random() * max);
+utils.randomNumber = function(max) {
+ return Math.floor(Math.random() * max);
};
-utils.random_number_string = function(max) {
- var t = (''+(max - 1)).length;
- var p = new Array(t+1).join('0');
- return (p + utils.random_number(max)).slice(-t);
+utils.randomNumberString = function(max) {
+ var t = ('' + (max - 1)).length;
+ var p = new Array(t + 1).join('0');
+ return (p + utils.randomNumber(max)).slice(-t);
};
utils.getOrigin = function(url) {
- if (url.match(/^file:\/\//)) {
- // no origin when using file protocol
- return null;
- }
+ if (url.match(/^file:\/\//)) {
+ // no origin when using file protocol
+ return null;
+ }
- var parts = url.split('/');
- var protocol = parts[0];
- var host = parts[2];
- var atSignIndex = host.lastIndexOf('@');
- var hostname;
- var port;
+ var parts = url.split('/');
+ var protocol = parts[0];
+ var host = parts[2];
+ var atSignIndex = host.lastIndexOf('@');
+ var hostname;
+ var port;
- if (~atSignIndex) host = host.slice(atSignIndex + 1);
+ if (~atSignIndex) {
+ host = host.slice(atSignIndex + 1);
+ }
- parts = host.split(':');
- hostname = parts[0];
- port = parts[1];
+ parts = host.split(':');
+ hostname = parts[0];
+ port = parts[1];
- if (!port) port = (protocol === 'https:') ? 443 : 80;
+ if (!port) {
+ port = (protocol === 'https:') ? 443 : 80;
+ }
- return protocol + '//' + hostname + ':' + port;
+ return protocol + '//' + hostname + ':' + port;
};
-utils.isSameOriginUrl = function(url_a, url_b) {
- // location.origin would do, but it's not always available.
- if (!url_b) url_b = window.location.href;
- return utils.getOrigin(url_a) === utils.getOrigin(url_b);
+utils.isSameOriginUrl = function(urlA, urlB) {
+ // location.origin would do, but it's not always available.
+ if (!urlB) {
+ urlB = window.location.href;
+ }
+ return utils.getOrigin(urlA) === utils.getOrigin(urlB);
};
-utils.isSameOriginScheme = function(url_a, url_b) {
- if (!url_b) url_b = window.location.href;
+utils.isSameOriginScheme = function(urlA, urlB) {
+ if (!urlB) {
+ urlB = window.location.href;
+ }
- return (url_a.split(':')[0] === url_b.split(':')[0]);
+ return (urlA.split(':')[0] === urlB.split(':')[0]);
};
utils.getParentDomain = function(url) {
- // ipv4 ip address
- if (/^[0-9.]*$/.test(url)) return url;
- // ipv6 ip address
- if (/^\[/.test(url)) return url;
- // no dots
- if (!(/[.]/.test(url))) return url;
+ // ipv4 ip address
+ if (/^[0-9.]*$/.test(url)) {
+ return url;
+ }
+ // ipv6 ip address
+ if (/^\[/.test(url)) {
+ return url;
+ }
+ // no dots
+ if (!(/[.]/.test(url))) {
+ return url;
+ }
- var parts = url.split('.').slice(1);
- return parts.join('.');
+ var parts = url.split('.').slice(1);
+ return parts.join('.');
};
utils.objectExtend = function(dst, src) {
- for(var k in src) {
- if (src.hasOwnProperty(k)) {
- dst[k] = src[k];
- }
+ for (var k in src) {
+ if (src.hasOwnProperty(k)) {
+ dst[k] = src[k];
}
- return dst;
+ }
+ return dst;
};
var WPrefix = utils.WPrefix = '_jp';
utils.polluteGlobalNamespace = function() {
- if (!(WPrefix in window)) {
- window[WPrefix] = {};
- }
+ if (!(WPrefix in window)) {
+ window[WPrefix] = {};
+ }
};
utils.closeFrame = function (code, reason) {
- return 'c'+JSON3.stringify([code, reason]);
+ return 'c' + JSON3.stringify([code, reason]);
};
utils.userSetCode = function (code) {
- return code === 1000 || (code >= 3000 && code <= 4999);
+ return code === 1000 || (code >= 3000 && code <= 4999);
};
// See: http://www.erg.abdn.ac.uk/~gerrit/dccp/notes/ccid2/rto_estimator/
// and RFC 2988.
utils.countRTO = function (rtt) {
- // In a local environment, when using IE8/9 and the `jsonp-polling`
- // transport the time needed to establish a connection (the time that pass
- // from the opening of the transport to the call of `_dispatchOpen`) is
- // around 200msec (the lower bound used in the article above) and this
- // causes spurious timeouts. For this reason we calculate a value slightly
- // larger than that used in the article.
- if (rtt > 100) return 4 * rtt; // rto > 400msec
- return 300 + rtt; // 300msec < rto <= 400msec
+ // In a local environment, when using IE8/9 and the `jsonp-polling`
+ // transport the time needed to establish a connection (the time that pass
+ // from the opening of the transport to the call of `_dispatchOpen`) is
+ // around 200msec (the lower bound used in the article above) and this
+ // causes spurious timeouts. For this reason we calculate a value slightly
+ // larger than that used in the article.
+ if (rtt > 100) {
+ return 4 * rtt; // rto > 400msec
+ }
+ return 300 + rtt; // 300msec < rto <= 400msec
};
utils.log = function() {
- if (window.console && console.log && console.log.apply) {
- console.log.apply(console, arguments);
- }
+ if (window.console && console.log && console.log.apply) {
+ console.log.apply(console, arguments);
+ }
};
utils.bind = function(fun, that) {
- if (fun.bind) {
- return fun.bind(that);
- } else {
- return function() {
- return fun.apply(that, arguments);
- };
- }
+ if (fun.bind) {
+ return fun.bind(that);
+ } else {
+ return function() {
+ return fun.apply(that, arguments);
+ };
+ }
};
utils.flatUrl = function(url) {
- return url.indexOf('?') === -1 && url.indexOf('#') === -1;
+ return url.indexOf('?') === -1 && url.indexOf('#') === -1;
};
utils.amendUrl = function(url) {
- var dl = document.location;
- if (!url) {
- throw new Error('Wrong url for SockJS');
- }
- if (!utils.flatUrl(url)) {
- throw new Error('Only basic urls are supported in SockJS');
- }
-
- // '//abc' --> 'http://abc'
- if (url.indexOf('//') === 0) {
- url = dl.protocol + url;
- }
- // '/abc' --> 'http://localhost:1234/abc'
- if (url.indexOf('/') === 0) {
- url = dl.protocol + '//' + dl.host + url;
- }
- // strip trailing slashes
- url = url.replace(/[/]+$/,'');
-
- // We have a full url here, with proto and host. For some browsers
- // http://localhost:80/ is not in the same origin as http://localhost/
- // Remove explicit :80 or :443 in such cases. See #74
- var parts = url.split("/");
- if ((parts[0] === "http:" && /:80$/.test(parts[2])) ||
- (parts[0] === "https:" && /:443$/.test(parts[2]))) {
- parts[2] = parts[2].replace(/:(80|443)$/, "");
- }
- url = parts.join("/");
- return url;
+ var dl = document.location;
+ if (!url) {
+ throw new Error('Wrong url for SockJS');
+ }
+ if (!utils.flatUrl(url)) {
+ throw new Error('Only basic urls are supported in SockJS');
+ }
+
+ // '//abc' --> 'http://abc'
+ if (url.indexOf('//') === 0) {
+ url = dl.protocol + url;
+ }
+ // '/abc' --> 'http://localhost:1234/abc'
+ if (url.indexOf('/') === 0) {
+ url = dl.protocol + '//' + dl.host + url;
+ }
+ // strip trailing slashes
+ url = url.replace(/[/]+$/,'');
+
+ // We have a full url here, with proto and host. For some browsers
+ // http://localhost:80/ is not in the same origin as http://localhost/
+ // Remove explicit :80 or :443 in such cases. See #74
+ var parts = url.split('/');
+ if ((parts[0] === 'http:' && /:80$/.test(parts[2])) ||
+ (parts[0] === 'https:' && /:443$/.test(parts[2]))) {
+ parts[2] = parts[2].replace(/:(80|443)$/, '');
+ }
+ url = parts.join('/');
+ return url;
};
// IE doesn't support [].indexOf.
utils.arrIndexOf = function(arr, obj){
- for(var i=0; i < arr.length; i++){
- if(arr[i] === obj){
- return i;
- }
+ for(var i = 0; i < arr.length; i++){
+ if(arr[i] === obj){
+ return i;
}
- return -1;
+ }
+ return -1;
};
utils.arrSkip = function(arr, obj) {
- var idx = utils.arrIndexOf(arr, obj);
- if (idx === -1) {
- return arr.slice();
- } else {
- var dst = arr.slice(0, idx);
- return dst.concat(arr.slice(idx+1));
- }
+ var idx = utils.arrIndexOf(arr, obj);
+ if (idx === -1) {
+ return arr.slice();
+ } else {
+ var dst = arr.slice(0, idx);
+ return dst.concat(arr.slice(idx + 1));
+ }
};
utils.isArray = Array.isArray || function(arg) {
- return Object.prototype.toString.call(arg) === '[object Array]';
+ return Object.prototype.toString.call(arg) === '[object Array]';
};
// Chars worth escaping, as defined by Douglas Crockford:
// https://github.com/douglascrockford/JSON-js/blob/47a9882cddeb1e8529e07af9736218075372b8ac/json2.js#L196
-var json_escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
- json_lookup = {
-"\u0000":"\\u0000","\u0001":"\\u0001","\u0002":"\\u0002","\u0003":"\\u0003",
-"\u0004":"\\u0004","\u0005":"\\u0005","\u0006":"\\u0006","\u0007":"\\u0007",
-"\b":"\\b","\t":"\\t","\n":"\\n","\u000b":"\\u000b","\f":"\\f","\r":"\\r",
-"\u000e":"\\u000e","\u000f":"\\u000f","\u0010":"\\u0010","\u0011":"\\u0011",
-"\u0012":"\\u0012","\u0013":"\\u0013","\u0014":"\\u0014","\u0015":"\\u0015",
-"\u0016":"\\u0016","\u0017":"\\u0017","\u0018":"\\u0018","\u0019":"\\u0019",
-"\u001a":"\\u001a","\u001b":"\\u001b","\u001c":"\\u001c","\u001d":"\\u001d",
-"\u001e":"\\u001e","\u001f":"\\u001f","\"":"\\\"","\\":"\\\\",
-"\u007f":"\\u007f","\u0080":"\\u0080","\u0081":"\\u0081","\u0082":"\\u0082",
-"\u0083":"\\u0083","\u0084":"\\u0084","\u0085":"\\u0085","\u0086":"\\u0086",
-"\u0087":"\\u0087","\u0088":"\\u0088","\u0089":"\\u0089","\u008a":"\\u008a",
-"\u008b":"\\u008b","\u008c":"\\u008c","\u008d":"\\u008d","\u008e":"\\u008e",
-"\u008f":"\\u008f","\u0090":"\\u0090","\u0091":"\\u0091","\u0092":"\\u0092",
-"\u0093":"\\u0093","\u0094":"\\u0094","\u0095":"\\u0095","\u0096":"\\u0096",
-"\u0097":"\\u0097","\u0098":"\\u0098","\u0099":"\\u0099","\u009a":"\\u009a",
-"\u009b":"\\u009b","\u009c":"\\u009c","\u009d":"\\u009d","\u009e":"\\u009e",
-"\u009f":"\\u009f","\u00ad":"\\u00ad","\u0600":"\\u0600","\u0601":"\\u0601",
-"\u0602":"\\u0602","\u0603":"\\u0603","\u0604":"\\u0604","\u070f":"\\u070f",
-"\u17b4":"\\u17b4","\u17b5":"\\u17b5","\u200c":"\\u200c","\u200d":"\\u200d",
-"\u200e":"\\u200e","\u200f":"\\u200f","\u2028":"\\u2028","\u2029":"\\u2029",
-"\u202a":"\\u202a","\u202b":"\\u202b","\u202c":"\\u202c","\u202d":"\\u202d",
-"\u202e":"\\u202e","\u202f":"\\u202f","\u2060":"\\u2060","\u2061":"\\u2061",
-"\u2062":"\\u2062","\u2063":"\\u2063","\u2064":"\\u2064","\u2065":"\\u2065",
-"\u2066":"\\u2066","\u2067":"\\u2067","\u2068":"\\u2068","\u2069":"\\u2069",
-"\u206a":"\\u206a","\u206b":"\\u206b","\u206c":"\\u206c","\u206d":"\\u206d",
-"\u206e":"\\u206e","\u206f":"\\u206f","\ufeff":"\\ufeff","\ufff0":"\\ufff0",
-"\ufff1":"\\ufff1","\ufff2":"\\ufff2","\ufff3":"\\ufff3","\ufff4":"\\ufff4",
-"\ufff5":"\\ufff5","\ufff6":"\\ufff6","\ufff7":"\\ufff7","\ufff8":"\\ufff8",
-"\ufff9":"\\ufff9","\ufffa":"\\ufffa","\ufffb":"\\ufffb","\ufffc":"\\ufffc",
-"\ufffd":"\\ufffd","\ufffe":"\\ufffe","\uffff":"\\uffff"};
+var jsonEscapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
+ jsonLookup = {
+'\u0000':'\\u0000','\u0001':'\\u0001','\u0002':'\\u0002','\u0003':'\\u0003',
+'\u0004':'\\u0004','\u0005':'\\u0005','\u0006':'\\u0006','\u0007':'\\u0007',
+'\b':'\\b','\t':'\\t','\n':'\\n','\u000b':'\\u000b','\f':'\\f','\r':'\\r',
+'\u000e':'\\u000e','\u000f':'\\u000f','\u0010':'\\u0010','\u0011':'\\u0011',
+'\u0012':'\\u0012','\u0013':'\\u0013','\u0014':'\\u0014','\u0015':'\\u0015',
+'\u0016':'\\u0016','\u0017':'\\u0017','\u0018':'\\u0018','\u0019':'\\u0019',
+'\u001a':'\\u001a','\u001b':'\\u001b','\u001c':'\\u001c','\u001d':'\\u001d',
+'\u001e':'\\u001e','\u001f':'\\u001f','\'':'\\\'','\\':'\\\\',
+'\u007f':'\\u007f','\u0080':'\\u0080','\u0081':'\\u0081','\u0082':'\\u0082',
+'\u0083':'\\u0083','\u0084':'\\u0084','\u0085':'\\u0085','\u0086':'\\u0086',
+'\u0087':'\\u0087','\u0088':'\\u0088','\u0089':'\\u0089','\u008a':'\\u008a',
+'\u008b':'\\u008b','\u008c':'\\u008c','\u008d':'\\u008d','\u008e':'\\u008e',
+'\u008f':'\\u008f','\u0090':'\\u0090','\u0091':'\\u0091','\u0092':'\\u0092',
+'\u0093':'\\u0093','\u0094':'\\u0094','\u0095':'\\u0095','\u0096':'\\u0096',
+'\u0097':'\\u0097','\u0098':'\\u0098','\u0099':'\\u0099','\u009a':'\\u009a',
+'\u009b':'\\u009b','\u009c':'\\u009c','\u009d':'\\u009d','\u009e':'\\u009e',
+'\u009f':'\\u009f','\u00ad':'\\u00ad','\u0600':'\\u0600','\u0601':'\\u0601',
+'\u0602':'\\u0602','\u0603':'\\u0603','\u0604':'\\u0604','\u070f':'\\u070f',
+'\u17b4':'\\u17b4','\u17b5':'\\u17b5','\u200c':'\\u200c','\u200d':'\\u200d',
+'\u200e':'\\u200e','\u200f':'\\u200f','\u2028':'\\u2028','\u2029':'\\u2029',
+'\u202a':'\\u202a','\u202b':'\\u202b','\u202c':'\\u202c','\u202d':'\\u202d',
+'\u202e':'\\u202e','\u202f':'\\u202f','\u2060':'\\u2060','\u2061':'\\u2061',
+'\u2062':'\\u2062','\u2063':'\\u2063','\u2064':'\\u2064','\u2065':'\\u2065',
+'\u2066':'\\u2066','\u2067':'\\u2067','\u2068':'\\u2068','\u2069':'\\u2069',
+'\u206a':'\\u206a','\u206b':'\\u206b','\u206c':'\\u206c','\u206d':'\\u206d',
+'\u206e':'\\u206e','\u206f':'\\u206f','\ufeff':'\\ufeff','\ufff0':'\\ufff0',
+'\ufff1':'\\ufff1','\ufff2':'\\ufff2','\ufff3':'\\ufff3','\ufff4':'\\ufff4',
+'\ufff5':'\\ufff5','\ufff6':'\\ufff6','\ufff7':'\\ufff7','\ufff8':'\\ufff8',
+'\ufff9':'\\ufff9','\ufffa':'\\ufffa','\ufffb':'\\ufffb','\ufffc':'\\ufffc',
+'\ufffd':'\\ufffd','\ufffe':'\\ufffe','\uffff':'\\uffff'};
// Some extra characters that Chrome gets wrong, and substitutes with
// something else on the wire.
-var extra_escapable = /[\x00-\x1f\ud800-\udfff\ufffe\uffff\u0300-\u0333\u033d-\u0346\u034a-\u034c\u0350-\u0352\u0357-\u0358\u035c-\u0362\u0374\u037e\u0387\u0591-\u05af\u05c4\u0610-\u0617\u0653-\u0654\u0657-\u065b\u065d-\u065e\u06df-\u06e2\u06eb-\u06ec\u0730\u0732-\u0733\u0735-\u0736\u073a\u073d\u073f-\u0741\u0743\u0745\u0747\u07eb-\u07f1\u0951\u0958-\u095f\u09dc-\u09dd\u09df\u0a33\u0a36\u0a59-\u0a5b\u0a5e\u0b5c-\u0b5d\u0e38-\u0e39\u0f43\u0f4d\u0f52\u0f57\u0f5c\u0f69\u0f72-\u0f76\u0f78\u0 [...]
- extra_lookup;
+var extraEscapable = /[\x00-\x1f\ud800-\udfff\ufffe\uffff\u0300-\u0333\u033d-\u0346\u034a-\u034c\u0350-\u0352\u0357-\u0358\u035c-\u0362\u0374\u037e\u0387\u0591-\u05af\u05c4\u0610-\u0617\u0653-\u0654\u0657-\u065b\u065d-\u065e\u06df-\u06e2\u06eb-\u06ec\u0730\u0732-\u0733\u0735-\u0736\u073a\u073d\u073f-\u0741\u0743\u0745\u0747\u07eb-\u07f1\u0951\u0958-\u095f\u09dc-\u09dd\u09df\u0a33\u0a36\u0a59-\u0a5b\u0a5e\u0b5c-\u0b5d\u0e38-\u0e39\u0f43\u0f4d\u0f52\u0f57\u0f5c\u0f69\u0f72-\u0f76\u0f78\u0f [...]
+ extraLookup;
// JSON Quote string. Use native implementation when possible.
var quoteJSON = (JSON3 && JSON3.stringify) || function(string) {
- json_escapable.lastIndex = 0;
- if (json_escapable.test(string)) {
- string = string.replace(json_escapable, function(a) {
- return json_lookup[a];
- });
- }
- return '"' + string + '"';
+ jsonEscapable.lastIndex = 0;
+ if (jsonEscapable.test(string)) {
+ string = string.replace(jsonEscapable, function(a) {
+ return jsonLookup[a];
+ });
+ }
+ return '"' + string + '"';
};
// This may be quite slow, so let's delay until user actually uses bad
// characters.
-var unroll_lookup = function(escapable) {
- var i;
- var unrolled = {};
- var c = [];
- for(i=0; i<65536; i++) {
- c.push( String.fromCharCode(i) );
- }
- escapable.lastIndex = 0;
- c.join('').replace(escapable, function (a) {
- unrolled[ a ] = '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
- return '';
- });
- escapable.lastIndex = 0;
- return unrolled;
+var unrollLookup = function(escapable) {
+ var i;
+ var unrolled = {};
+ var c = [];
+ for(i = 0; i < 65536; i++) {
+ c.push( String.fromCharCode(i) );
+ }
+ escapable.lastIndex = 0;
+ c.join('').replace(escapable, function (a) {
+ unrolled[ a ] = '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
+ return '';
+ });
+ escapable.lastIndex = 0;
+ return unrolled;
};
// Quote string, also taking care of unicode characters that browsers
// often break. Especially, take care of unicode surrogates:
// http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Surrogates
utils.quote = function(string) {
- var quoted = quoteJSON(string);
+ var quoted = quoteJSON(string);
- // In most cases this should be very fast and good enough.
- extra_escapable.lastIndex = 0;
- if(!extra_escapable.test(quoted)) {
- return quoted;
- }
+ // In most cases this should be very fast and good enough.
+ extraEscapable.lastIndex = 0;
+ if(!extraEscapable.test(quoted)) {
+ return quoted;
+ }
- if(!extra_lookup) extra_lookup = unroll_lookup(extra_escapable);
+ if (!extraLookup) {
+ extraLookup = unrollLookup(extraEscapable);
+ }
- return quoted.replace(extra_escapable, function(a) {
- return extra_lookup[a];
- });
+ return quoted.replace(extraEscapable, function(a) {
+ return extraLookup[a];
+ });
};
// 1. Is natively via XHR
@@ -301,199 +315,203 @@ utils.quote = function(string) {
// 3. Nope, but postMessage is there so it should work via the Iframe.
// 4. Nope, sorry.
utils.isXHRCorsCapable = function() {
- try {
- if (window.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest()) {
- return 1;
- }
- } catch (ignored) {}
- // XDomainRequest doesn't work if page is served from file://
- if (window.XDomainRequest && document.domain) {
- return 2;
- }
- if (IframeTransport.enabled()) {
- return 3;
+ try {
+ if (window.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest()) {
+ return 1;
}
- return 4;
+ } catch (ignored) {}
+ // XDomainRequest doesn't work if page is served from file://
+ if (window.XDomainRequest && document.domain) {
+ return 2;
+ }
+ if (IframeTransport.enabled()) {
+ return 3;
+ }
+ return 4;
};
// May be used by htmlfile jsonp and transports.
var MPrefix = '_sockjs_global';
utils.createHook = function() {
- var window_id = 'a' + utils.random_string(8);
- if (!(MPrefix in window)) {
- var map = {};
- window[MPrefix] = function(window_id) {
- if (!(window_id in map)) {
- map[window_id] = {
- id: window_id,
- del: function() {delete map[window_id];}
- };
- }
- return map[window_id];
+ var windowId = 'a' + utils.randomString(8);
+ if (!(MPrefix in window)) {
+ var map = {};
+ window[MPrefix] = function(windowId) {
+ if (!(windowId in map)) {
+ map[windowId] = {
+ id: windowId,
+ del: function() {delete map[windowId];}
};
- }
- return window[MPrefix](window_id);
+ }
+ return map[windowId];
+ };
+ }
+ return window[MPrefix](windowId);
};
utils.attachMessage = function(listener) {
- utils.attachEvent('message', listener);
+ utils.attachEvent('message', listener);
};
utils.attachEvent = function(event, listener) {
- if (typeof window.addEventListener !== 'undefined') {
- window.addEventListener(event, listener, false);
- } else {
- // 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);
- // I get 'window' for ie8.
- window.attachEvent("on" + event, listener);
- }
+ if (typeof window.addEventListener !== 'undefined') {
+ window.addEventListener(event, listener, false);
+ } else {
+ // 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);
+ // I get 'window' for ie8.
+ window.attachEvent('on' + event, listener);
+ }
};
utils.detachMessage = function(listener) {
- utils.detachEvent('message', listener);
+ utils.detachEvent('message', listener);
};
utils.detachEvent = function(event, listener) {
- if (typeof window.addEventListener !== 'undefined') {
- window.removeEventListener(event, listener, false);
- } else {
- document.detachEvent("on" + event, listener);
- window.detachEvent("on" + event, listener);
- }
+ if (typeof window.addEventListener !== 'undefined') {
+ window.removeEventListener(event, listener, false);
+ } else {
+ document.detachEvent('on' + event, listener);
+ window.detachEvent('on' + event, listener);
+ }
};
-var on_unload = {};
+var onUnload = {};
// Things registered after beforeunload are to be called immediately.
-var after_unload = false;
+var afterUnload = false;
-var trigger_unload_callbacks = function() {
- for(var ref in on_unload) {
- on_unload[ref]();
- delete on_unload[ref];
- }
+var triggerUnloadCallbacks = function() {
+ for(var ref in onUnload) {
+ onUnload[ref]();
+ delete onUnload[ref];
+ }
};
-var unload_triggered = function() {
- if(after_unload) return;
- after_unload = true;
- trigger_unload_callbacks();
+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.
-utils.attachEvent('unload', unload_triggered);
-
-utils.unload_add = function(listener) {
- var ref = utils.random_string(8);
- on_unload[ref] = listener;
- if (after_unload) {
- setTimeout(trigger_unload_callbacks, 0);
- }
- return ref;
-};
-utils.unload_del = function(ref) {
- if (ref in on_unload)
- delete on_unload[ref];
-};
-
-
-utils.createIframe = function (iframe_url, error_callback) {
- var iframe = document.createElement('iframe');
- var tref, unload_ref;
- 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.unload_del(unload_ref);
- }
- };
- var onerror = function(r) {
- if (iframe) {
- cleanup();
- error_callback(r);
+utils.attachEvent('unload', unloadTriggered);
+
+utils.unloadAdd = function(listener) {
+ var ref = utils.randomString(8);
+ onUnload[ref] = listener;
+ if (afterUnload) {
+ setTimeout(triggerUnloadCallbacks, 0);
+ }
+ return ref;
+};
+utils.unloadDel = function(ref) {
+ if (ref in onUnload) {
+ delete onUnload[ref];
+ }
+};
+
+
+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);
}
- };
- 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 = iframe_url;
- 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);
- unload_ref = utils.unload_add(cleanup);
- return {
- post: post,
- cleanup: cleanup,
- loaded: unattach
- };
+ 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) {
+ 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
+ };
};
/* jshint undef: false, newcap: false */
-utils.createHtmlfile = function (iframe_url, error_callback) {
+/* eslint no-undef: [0], new-cap: [0] */
+utils.createHtmlfile = function (iframeUrl, errorCallback) {
var doc = new ActiveXObject('htmlfile');
- var tref, unload_ref;
+ var tref, unloadRef;
var iframe;
var unattach = function() {
clearTimeout(tref);
};
var cleanup = function() {
- if (doc) {
- unattach();
- utils.unload_del(unload_ref);
- iframe.parentNode.removeChild(iframe);
- iframe = doc = null;
- CollectGarbage();
- }
+ if (doc) {
+ unattach();
+ utils.unloadDel(unloadRef);
+ iframe.parentNode.removeChild(iframe);
+ iframe = doc = null;
+ CollectGarbage();
+ }
};
var onerror = function(r) {
- if (doc) {
- cleanup();
- error_callback(r);
- }
+ if (doc) {
+ 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) {
- setTimeout(function() {
- iframe.contentWindow.postMessage(msg, origin);
- }, 0);
- }
- } catch (x) {}
+ 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) {}
};
doc.open();
@@ -506,22 +524,23 @@ utils.createHtmlfile = function (iframe_url, error_callback) {
doc.body.appendChild(c);
iframe = doc.createElement('iframe');
c.appendChild(iframe);
- iframe.src = iframe_url;
+ iframe.src = iframeUrl;
tref = setTimeout(function(){onerror('timeout');}, 15000);
- unload_ref = utils.unload_add(cleanup);
+ unloadRef = utils.unloadAdd(cleanup);
return {
- post: post,
- cleanup: cleanup,
- loaded: unattach
+ post: post,
+ cleanup: cleanup,
+ loaded: unattach
};
};
+// curr_window_id comes from SockJS.bootstrap_iframe()
utils.postMessage = function (type, data) {
- if (parent !== window) {
- parent.postMessage(utils.curr_window_id + type + (data || ''), '*');
- } else {
- utils.log("Can't postMessage, no parent window.", type, data);
- }
+ if (parent !== window) {
+ parent.postMessage(utils.curr_window_id + type + (data || ''), '*');
+ } else {
+ utils.log("Can't postMessage, no parent window.", type, data);
+ }
};
module.exports = utils;
diff --git a/lib/xdr.js b/lib/xdr.js
index f684e0a..6ba39a7 100644
--- a/lib/xdr.js
+++ b/lib/xdr.js
@@ -10,58 +10,63 @@ var EventEmitter = require('events').EventEmitter
// http://msdn.microsoft.com/en-us/library/cc288060(v=VS.85).aspx
function XDRObject(method, url, payload) {
- var that = this;
- setTimeout(function(){that._start(method, url, payload);}, 0);
+ var self = this;
+ process.nextTick(function(){
+ self._start(method, url, payload);
+ });
}
util.inherits(XDRObject, EventEmitter);
XDRObject.prototype._start = function(method, url, payload) {
- var that = this;
- var xdr = new XDomainRequest();
- // IE caches even POSTs
- url += ((url.indexOf('?') === -1) ? '?' : '&') + 't='+(+new Date());
+ var self = this;
+ var xdr = new XDomainRequest();
+ // IE caches even POSTs
+ url += ((url.indexOf('?') === -1) ? '?' : '&') + 't=' + Date.now();
- var onerror = xdr.ontimeout = xdr.onerror = function() {
- that.emit('finish', 0, '');
- that._cleanup(false);
- };
- xdr.onprogress = function() {
- that.emit('chunk', 200, xdr.responseText);
- };
- xdr.onload = function() {
- that.emit('finish', 200, xdr.responseText);
- that._cleanup(false);
- };
- that.xdr = xdr;
- that.unload_ref = utils.unload_add(function(){that._cleanup(true);});
- try {
- // Fails with AccessDenied if port number is bogus
- that.xdr.open(method, url);
- that.xdr.send(payload);
- } catch(x) {
- onerror();
- }
+ var onerror = xdr.ontimeout = xdr.onerror = function() {
+ self.emit('finish', 0, '');
+ self._cleanup(false);
+ };
+ xdr.onprogress = function() {
+ self.emit('chunk', 200, xdr.responseText);
+ };
+ xdr.onload = function() {
+ self.emit('finish', 200, xdr.responseText);
+ self._cleanup(false);
+ };
+ this.xdr = xdr;
+ this.unloadRef = utils.unloadAdd(function(){
+ self._cleanup(true);
+ });
+ try {
+ // Fails with AccessDenied if port number is bogus
+ this.xdr.open(method, url);
+ this.xdr.send(payload);
+ } catch(x) {
+ onerror();
+ }
};
XDRObject.prototype._cleanup = function(abort) {
- var that = this;
- if (!that.xdr) return;
- utils.unload_del(that.unload_ref);
+ if (!this.xdr) {
+ return;
+ }
+ utils.unloadDel(this.unloadRef);
- that.xdr.ontimeout = that.xdr.onerror = that.xdr.onprogress =
- that.xdr.onload = null;
- if (abort) {
- try {
- that.xdr.abort();
- } catch(x) {}
- }
- that.unload_ref = that.xdr = null;
+ this.xdr.ontimeout = this.xdr.onerror = this.xdr.onprogress =
+ this.xdr.onload = null;
+ if (abort) {
+ try {
+ this.xdr.abort();
+ } catch(x) {}
+ }
+ this.unloadRef = this.xdr = null;
};
XDRObject.prototype.close = function() {
- this.removeAllListeners();
- this._cleanup(true);
+ this.removeAllListeners();
+ this._cleanup(true);
};
-module.exports = XDRObject;
\ No newline at end of file
+module.exports = XDRObject;
diff --git a/lib/xhr-cors.js b/lib/xhr-cors.js
index b9fa93b..ef79e5f 100644
--- a/lib/xhr-cors.js
+++ b/lib/xhr-cors.js
@@ -1,11 +1,18 @@
'use strict';
-var AbstractXHRObject = require('./abstract-xhr');
+var util = require('util')
+ , AbstractXHRObject = require('./abstract-xhr')
+ ;
function XHRCorsObject() {
- var that = this, args = arguments;
- setTimeout(function(){that._start.apply(that, args);}, 0);
+ var self = this
+ , args = arguments
+ ;
+ process.nextTick(function(){
+ self._start.apply(self, args);
+ });
}
-XHRCorsObject.prototype = new AbstractXHRObject();
-module.exports = XHRCorsObject;
\ No newline at end of file
+util.inherits(XHRCorsObject, AbstractXHRObject);
+
+module.exports = XHRCorsObject;
diff --git a/lib/xhr-local.js b/lib/xhr-local.js
index 2854d94..5ab3b6d 100644
--- a/lib/xhr-local.js
+++ b/lib/xhr-local.js
@@ -1,15 +1,18 @@
'use strict';
-var AbstractXHRObject = require('./abstract-xhr');
+var util = require('util')
+ , AbstractXHRObject = require('./abstract-xhr')
+ ;
function XHRLocalObject(method, url, payload) {
- var that = this;
- setTimeout(function(){
- that._start(method, url, payload, {
- no_credentials: true
- });
- }, 0);
+ var self = this;
+ process.nextTick(function(){
+ self._start(method, url, payload, {
+ noCredentials: true
+ });
+ });
}
-XHRLocalObject.prototype = new AbstractXHRObject();
-module.exports = XHRLocalObject;
\ No newline at end of file
+util.inherits(XHRLocalObject, AbstractXHRObject);
+
+module.exports = XHRLocalObject;
diff --git a/lib/xhr-polling-iframe.js b/lib/xhr-polling-iframe.js
index ca44568..e437860 100644
--- a/lib/xhr-polling-iframe.js
+++ b/lib/xhr-polling-iframe.js
@@ -1,14 +1,16 @@
'use strict';
-var XhrReceiver = require('./trans-receiver-xhr');
-var XHRLocalObject = require('./xhr-local');
-var AjaxBasedTransport = require('./ajax-based');
+var util = require('util')
+ , XhrReceiver = require('./trans-receiver-xhr')
+ , XHRLocalObject = require('./xhr-local')
+ , AjaxBasedTransport = require('./ajax-based')
+ ;
// w-iframe-xhr-polling
-function XhrPollingITransport(ri, trans_url) {
- this.run(ri, trans_url, '/xhr', XhrReceiver, XHRLocalObject);
+function XhrPollingITransport(ri, transUrl) {
+ this.run(ri, transUrl, '/xhr', XhrReceiver, XHRLocalObject);
}
-XhrPollingITransport.prototype = new AjaxBasedTransport();
+util.inherits(XhrPollingITransport, AjaxBasedTransport);
-module.exports = XhrPollingITransport;
\ No newline at end of file
+module.exports = XhrPollingITransport;
diff --git a/package.json b/package.json
index b89038f..b62ae2d 100644
--- a/package.json
+++ b/package.json
@@ -25,7 +25,12 @@
"node-static": "^0.5.9",
"sockjs": "^0.3.9",
"vinyl-source-stream": "^0.1.1",
- "zuul": "^1.10.1"
+ "zuul": "^1.10.1",
+ "expect.js": "~0.3.1",
+ "proxyquire": "~1.0.1",
+ "gulp-mocha": "~1.1.0",
+ "gulp-jsbeautifier": "0.0.2",
+ "gulp-eslint": "~0.1.8"
},
"homepage": "http://sockjs.org",
"keywords": [
@@ -40,6 +45,7 @@
"url": "https://github.com/sockjs/sockjs-client.git"
},
"scripts": {
+ "t": "node ./node_modules/mocha/bin/mocha tests/main.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"
diff --git a/tests/html/lib/unittests.js b/tests/html/lib/unittests.js
index 0cc9bb0..1954d03 100644
--- a/tests/html/lib/unittests.js
+++ b/tests/html/lib/unittests.js
@@ -3,25 +3,25 @@
var assert = require('assert');
var u = require('../../../lib/utils');
-var protocols = require('../../../lib/protocols')
+var protocols = require('../../../lib/protocols');
-test('random_string', function() {
+test('randomString', function() {
var i, _i, _len, _ref;
- assert.notEqual(u.random_string(8), u.random_string(8));
+ assert.notEqual(u.randomString(8), u.randomString(8));
_ref = [1, 2, 3, 128];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
i = _ref[_i];
- assert.equal(u.random_string(i).length, i);
+ assert.equal(u.randomString(i).length, i);
}
});
-test('random_number_string', function() {
+test('randomNumberString', function() {
for (var i = 0; i <= 10; i++) {
- assert.equal(u.random_number_string(10).length, 1);
- assert.equal(u.random_number_string(100).length, 2);
- assert.equal(u.random_number_string(1000).length, 3);
- assert.equal(u.random_number_string(10000).length, 4);
- assert.equal(u.random_number_string(100000).length, 5);
+ assert.equal(u.randomNumberString(10).length, 1);
+ assert.equal(u.randomNumberString(100).length, 2);
+ assert.equal(u.randomNumberString(1000).length, 3);
+ assert.equal(u.randomNumberString(10000).length, 4);
+ assert.equal(u.randomNumberString(100000).length, 5);
}
});
diff --git a/tests/main.js b/tests/main.js
new file mode 100644
index 0000000..4ef1ce6
--- /dev/null
+++ b/tests/main.js
@@ -0,0 +1,85 @@
+'use strict';
+
+var expect = require('expect.js')
+ , proxyquire = require('proxyquire')
+ , SecurityError = require('../lib/securityerror')
+ , SockJS = require('../lib/main')
+ ;
+
+describe('SockJS', function() {
+ describe('Constructor', function () {
+ it('should support construction without new', function () {
+ var s = SockJS('http://sockjs.org');
+ expect(s).to.be.a(SockJS);
+ });
+
+ it('create a valid WebSocket object', function () {
+ var s = new SockJS('http://sockjs.org');
+ expect(s).to.have.property('url', 'http://sockjs.org/');
+ expect(s).to.have.property('readyState', SockJS.CONNECTING);
+ expect(s).to.have.property('extensions', '');
+ expect(s).to.have.property('protocol', '');
+ });
+
+ describe('WebSocket specification step #1', function () {
+ it('should throw SyntaxError for an invalid url', function () {
+ expect(function () {
+ SockJS('//sockjs.org')
+ }).to.throwException(function (e) {
+ expect(e).to.be.a(SyntaxError);
+ });
+
+ expect(function () {
+ SockJS('http://')
+ }).to.throwException(function (e) {
+ expect(e).to.be.a(SyntaxError);
+ });
+ });
+
+ it('should throw SyntaxError when the url contains a querystring or fragment', function () {
+ expect(function () {
+ SockJS('http://sockjs.org/?test')
+ }).to.throwException(function (e) {
+ expect(e).to.be.a(SyntaxError);
+ });
+
+ expect(function () {
+ SockJS('http://sockjs.org/#test')
+ }).to.throwException(function (e) {
+ expect(e).to.be.a(SyntaxError);
+ });
+ });
+
+ it('should throw SyntaxError for an invalid protocol', function () {
+ expect(function () {
+ SockJS('ftp://sockjs.org')
+ }).to.throwException(function (e) {
+ expect(e).to.be.a(SyntaxError);
+ });
+ });
+ });
+
+ describe('WebSocket specification step #2', function () {
+ it('should throw SecurityError for an insecure url from a secure page', function () {
+ var sjs = proxyquire('../lib/main', { './location': {
+ protocol: 'https'
+ }});
+ expect(function () {
+ sjs('http://sockjs.org')
+ }).to.throwException(function (e) {
+ expect(e).to.be.a(SecurityError);
+ });
+ });
+ });
+
+ describe('WebSocket specification step #5', function () {
+ it('should throw SyntaxError for duplicated protocols', function () {
+ expect(function () {
+ SockJS('http://sockjs.org', ['test', 'test'])
+ }).to.throwException(function (e) {
+ expect(e).to.be.a(SyntaxError);
+ });
+ });
+ });
+ });
+});
\ No newline at end of file
--
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