[Pkg-javascript-commits] [sockjs-client] 47/350: Move to gulp build system with browserify and json3
tonnerre at ancient-solutions.com
tonnerre at ancient-solutions.com
Fri Aug 5 01:03:40 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 48259a310be57fdbd9ac783fcf5df436ced77a39
Author: Bryce Kahle <bkahle at gmail.com>
Date: Fri May 23 17:49:33 2014 -0400
Move to gulp build system with browserify and json3
---
.jshintrc | 15 ++
gulpfile.js | 20 +++
lib/abstract-xhr.js | 107 +++++++++++++++
lib/ajax-based.js | 51 +++++++
lib/all.js | 9 --
lib/buffered-sender.js | 71 ++++++++++
lib/dom.js | 208 ----------------------------
lib/dom2.js | 198 ---------------------------
lib/eventemitter.js | 10 +-
lib/facade.js | 26 ++++
lib/index.js | 37 -----
lib/info-receiver-fake.js | 24 ++++
lib/info-receiver-iframe.js | 47 +++++++
lib/info-receiver.js | 63 +++++++++
lib/info.js | 125 -----------------
lib/json2.min.js | 1 -
lib/reventtarget.js | 6 +
lib/simpleevent.js | 3 +
lib/sockjs.js | 160 +++++++++++++++++++---
lib/test-hooks.js | 16 ---
lib/trans-eventsource.js | 14 ++
lib/trans-iframe-eventsource.js | 16 +--
lib/trans-iframe-htmlfile.js | 13 +-
lib/trans-iframe-within.js | 94 -------------
lib/trans-iframe-xhr-polling.js | 14 +-
lib/trans-iframe.js | 16 ++-
lib/trans-jsonp-polling.js | 188 ++++++++++++++++++++++++--
lib/trans-jsonp-receiver.js | 116 ----------------
lib/trans-polling.js | 7 +-
lib/trans-receiver-eventsource.js | 13 +-
lib/trans-receiver-htmlfile.js | 19 ++-
lib/trans-receiver-xhr.js | 12 +-
lib/trans-sender.js | 142 -------------------
lib/trans-websocket.js | 16 ++-
lib/trans-xhr.js | 94 -------------
lib/utils.js | 278 ++++++++++++++++++++++++++++++++++----
lib/xdr-polling.js | 24 ++++
lib/xdr-streaming.js | 29 ++++
lib/xdr.js | 65 +++++++++
lib/xhr-cors.js | 12 ++
lib/xhr-local.js | 16 +++
lib/xhr-polling.js | 25 ++++
lib/xhr-streaming.js | 38 ++++++
package.json | 10 +-
44 files changed, 1333 insertions(+), 1135 deletions(-)
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..b31c343
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,15 @@
+{
+ "undef": true
+, "unused": "vars"
+, "newcap": true
+, "laxcomma": true
+, "laxbreak": true
+, "strict": true
+, "node": true
+, "browser": true
+, "predef": [
+ "ActiveXObject"
+ , "XDomainRequest"
+ , "EventSource"
+ ]
+}
\ No newline at end of file
diff --git a/gulpfile.js b/gulpfile.js
new file mode 100644
index 0000000..e855df1
--- /dev/null
+++ b/gulpfile.js
@@ -0,0 +1,20 @@
+'use strict';
+
+var gulp = require('gulp')
+ , browserify = require('browserify')
+ , source = require('vinyl-source-stream')
+ , pkg = require('./package.json')
+ ;
+
+gulp.task('default', function() {
+
+});
+
+gulp.task('browserify', function() {
+ return browserify('./lib/sockjs.js')
+ .bundle({
+ standalone: 'SockJS'
+ })
+ .pipe(source('sockjs.js'))
+ .pipe(gulp.dest('./build/'));
+});
\ No newline at end of file
diff --git a/lib/abstract-xhr.js b/lib/abstract-xhr.js
new file mode 100644
index 0000000..fd2772a
--- /dev/null
+++ b/lib/abstract-xhr.js
@@ -0,0 +1,107 @@
+'use strict';
+
+var EventEmitter = require('./eventemitter');
+var utils = require('./utils');
+
+function AbstractXHRObject() {
+}
+
+AbstractXHRObject.prototype = new EventEmitter(['chunk', 'finish']);
+
+AbstractXHRObject.prototype._start = function(method, url, payload, opts) {
+ var that = this;
+
+ try {
+ that.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);});
+ 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';
+ }
+ if (opts && opts.headers) {
+ for(var key in opts.headers) {
+ that.xhr.setRequestHeader(key, opts.headers[key]);
+ }
+ }
+
+ 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;
+ }
+ }
+ };
+ that.xhr.send(payload);
+};
+
+AbstractXHRObject.prototype._cleanup = function(abort) {
+ var that = this;
+ if (!that.xhr) return;
+ utils.unload_del(that.unload_ref);
+
+ // IE needs this field to be a function
+ that.xhr.onreadystatechange = function(){};
+
+ if (abort) {
+ try {
+ that.xhr.abort();
+ } catch(x) {}
+ }
+ that.unload_ref = that.xhr = null;
+};
+
+AbstractXHRObject.prototype.close = function() {
+ var that = this;
+ that.nuke();
+ that._cleanup(true);
+};
+
+module.exports = AbstractXHRObject;
\ No newline at end of file
diff --git a/lib/ajax-based.js b/lib/ajax-based.js
new file mode 100644
index 0000000..a74dbeb
--- /dev/null
+++ b/lib/ajax-based.js
@@ -0,0 +1,51 @@
+'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');
+
+function AjaxBasedTransport() {
+}
+
+AjaxBasedTransport.prototype = new 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.doCleanup = function() {
+ var that = this;
+ if (that.poll) {
+ that.poll.abort();
+ that.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.onfinish = 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
diff --git a/lib/all.js b/lib/all.js
deleted file mode 100644
index 3391dd7..0000000
--- a/lib/all.js
+++ /dev/null
@@ -1,9 +0,0 @@
-/* SockJS client, version <!-- version -->, http://sockjs.org, MIT License
-
-<!-- include LICENSE -->
-*/
-
-// JSON2 by Douglas Crockford (minified).
-<!-- include lib/json2.min.js -->
-
-<!-- include_and_minify lib/index.js c -->
diff --git a/lib/buffered-sender.js b/lib/buffered-sender.js
new file mode 100644
index 0000000..20e2572
--- /dev/null
+++ b/lib/buffered-sender.js
@@ -0,0 +1,71 @@
+'use strict';
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ * Copyright (c) 2011-2012 VMware, Inc.
+ *
+ * For the license see COPYING.
+ * ***** END LICENSE BLOCK *****
+ */
+
+var utils = require('./utils');
+
+function BufferedSender() {}
+BufferedSender.prototype.send_constructor = function(sender) {
+ var that = this;
+ that.send_buffer = [];
+ that.sender = sender;
+};
+BufferedSender.prototype.doSend = function(message) {
+ var that = this;
+ that.send_buffer.push(message);
+ if (!that.send_stop) {
+ that.send_schedule();
+ }
+};
+
+// For polling transports in a situation when in the message callback,
+// new message is being send. If the sending connection was started
+// before receiving one, it is possible to saturate the network and
+// timeout due to the lack of receiving socket. To avoid that we delay
+// sending messages by some small time, in order to let receiving
+// 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 = utils.delay(25, function() {
+ that.send_stop = null;
+ that.send_schedule();
+ });
+};
+
+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.send_destructor = function() {
+ var that = this;
+ if (that._send_stop) {
+ that._send_stop();
+ }
+ that._send_stop = null;
+};
+
+module.exports = BufferedSender;
\ No newline at end of file
diff --git a/lib/dom.js b/lib/dom.js
deleted file mode 100644
index 4a903e2..0000000
--- a/lib/dom.js
+++ /dev/null
@@ -1,208 +0,0 @@
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-// 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];
- }
- }
- return _window[MPrefix](window_id);
-};
-
-
-
-utils.attachMessage = function(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);
- }
-};
-
-utils.detachMessage = function(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);
- }
-};
-
-
-var on_unload = {};
-// Things registered after beforeunload are to be called immediately.
-var after_unload = false;
-
-var trigger_unload_callbacks = function() {
- for(var ref in on_unload) {
- on_unload[ref]();
- delete on_unload[ref];
- };
-};
-
-var unload_triggered = function() {
- if(after_unload) return;
- after_unload = true;
- trigger_unload_callbacks();
-};
-
-// '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) {
- utils.delay(trigger_unload_callbacks);
- }
- 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);
- }
- };
- 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
- };
-};
-
-utils.createHtmlfile = function (iframe_url, error_callback) {
- var doc = new ActiveXObject('htmlfile');
- var tref, unload_ref;
- 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();
- }
- };
- var onerror = function(r) {
- if (doc) {
- cleanup();
- error_callback(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) {};
- };
-
- doc.open();
- doc.write('<html><s' + 'cript>' +
- 'document.domain="' + document.domain + '";' +
- '</s' + 'cript></html>');
- doc.close();
- doc.parentWindow[WPrefix] = _window[WPrefix];
- var c = doc.createElement('div');
- doc.body.appendChild(c);
- iframe = doc.createElement('iframe');
- c.appendChild(iframe);
- iframe.src = iframe_url;
- tref = setTimeout(function(){onerror('timeout');}, 15000);
- unload_ref = utils.unload_add(cleanup);
- return {
- post: post,
- cleanup: cleanup,
- loaded: unattach
- };
-};
diff --git a/lib/dom2.js b/lib/dom2.js
deleted file mode 100644
index 2183d30..0000000
--- a/lib/dom2.js
+++ /dev/null
@@ -1,198 +0,0 @@
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-var AbstractXHRObject = function(){};
-AbstractXHRObject.prototype = new EventEmitter(['chunk', 'finish']);
-
-AbstractXHRObject.prototype._start = function(method, url, payload, opts) {
- var that = this;
-
- try {
- that.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);});
- 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';
- }
- if (opts && opts.headers) {
- for(var key in opts.headers) {
- that.xhr.setRequestHeader(key, opts.headers[key]);
- }
- }
-
- that.xhr.onreadystatechange = function() {
- if (that.xhr) {
- var x = that.xhr;
- switch (x.readyState) {
- case 3:
- // IE doesn't like peeking into responseText or status
- // on Microsoft.XMLHTTP and readystate=3
- try {
- var status = x.status;
- var 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:
- var 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;
- }
- }
- };
- that.xhr.send(payload);
-};
-
-AbstractXHRObject.prototype._cleanup = function(abort) {
- var that = this;
- if (!that.xhr) return;
- utils.unload_del(that.unload_ref);
-
- // IE needs this field to be a function
- that.xhr.onreadystatechange = function(){};
-
- if (abort) {
- try {
- that.xhr.abort();
- } catch(x) {};
- }
- that.unload_ref = that.xhr = null;
-};
-
-AbstractXHRObject.prototype.close = function() {
- var that = this;
- that.nuke();
- that._cleanup(true);
-};
-
-var XHRCorsObject = utils.XHRCorsObject = function() {
- var that = this, args = arguments;
- utils.delay(function(){that._start.apply(that, args);});
-};
-XHRCorsObject.prototype = new AbstractXHRObject();
-
-var XHRLocalObject = utils.XHRLocalObject = function(method, url, payload) {
- var that = this;
- utils.delay(function(){
- that._start(method, url, payload, {
- no_credentials: true
- });
- });
-};
-XHRLocalObject.prototype = new AbstractXHRObject();
-
-
-
-// References:
-// http://ajaxian.com/archives/100-line-ajax-wrapper
-// http://msdn.microsoft.com/en-us/library/cc288060(v=VS.85).aspx
-var XDRObject = utils.XDRObject = function(method, url, payload) {
- var that = this;
- utils.delay(function(){that._start(method, url, payload);});
-};
-XDRObject.prototype = new EventEmitter(['chunk', 'finish']);
-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 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();
- }
-};
-
-XDRObject.prototype._cleanup = function(abort) {
- var that = this;
- if (!that.xdr) return;
- utils.unload_del(that.unload_ref);
-
- 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;
-};
-
-XDRObject.prototype.close = function() {
- var that = this;
- that.nuke();
- that._cleanup(true);
-};
-
-// 1. Is natively via XHR
-// 2. Is natively via XDR
-// 3. Nope, but postMessage is there so it should work via the Iframe.
-// 4. Nope, sorry.
-utils.isXHRCorsCapable = function() {
- if (_window.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest()) {
- return 1;
- }
- // XDomainRequest doesn't work if page is served from file://
- if (_window.XDomainRequest && _document.domain) {
- return 2;
- }
- if (IframeTransport.enabled()) {
- return 3;
- }
- return 4;
-};
diff --git a/lib/eventemitter.js b/lib/eventemitter.js
index f0c9206..ad43524 100644
--- a/lib/eventemitter.js
+++ b/lib/eventemitter.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -6,6 +7,9 @@
* ***** END LICENSE BLOCK *****
*/
+var utils = require('./utils');
+var JSON3 = require('json3').noConflict();
+
var EventEmitter = function(events) {
var that = this;
that._events = events || [];
@@ -41,8 +45,8 @@ EventEmitter.prototype.on = function(type, callback) {
EventEmitter.prototype._verifyType = function(type) {
var that = this;
if (utils.arrIndexOf(that._events, type) === -1) {
- utils.log('Event ' + JSON.stringify(type) +
- ' not listed ' + JSON.stringify(that._events) +
+ utils.log('Event ' + JSON3.stringify(type) +
+ ' not listed ' + JSON3.stringify(that._events) +
' in ' + that);
}
};
@@ -55,3 +59,5 @@ EventEmitter.prototype.nuke = function() {
}
that._listeners = {};
};
+
+module.exports = EventEmitter;
diff --git a/lib/facade.js b/lib/facade.js
new file mode 100644
index 0000000..0e45ac2
--- /dev/null
+++ b/lib/facade.js
@@ -0,0 +1,26 @@
+'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));
+};
+FacadeJS.prototype._didMessage = function (frame) {
+ utils.postMessage('t', frame);
+};
+FacadeJS.prototype._doSend = function (data) {
+ this._transport.doSend(data);
+};
+FacadeJS.prototype._doCleanup = function () {
+ this._transport.doCleanup();
+};
+
+module.exports = FacadeJS;
\ No newline at end of file
diff --git a/lib/index.js b/lib/index.js
deleted file mode 100644
index 96262d3..0000000
--- a/lib/index.js
+++ /dev/null
@@ -1,37 +0,0 @@
-// Public object
-SockJS = (function(){
- var _document = document;
- var _window = window;
- var utils = {};
-
-<!-- include lib/reventtarget.js -->
-<!-- include lib/simpleevent.js -->
-<!-- include lib/eventemitter.js -->
-<!-- include lib/utils.js -->
-<!-- include lib/dom.js -->
-<!-- include lib/dom2.js -->
-<!-- include lib/sockjs.js -->
-<!-- include lib/trans-websocket.js -->
-<!-- include lib/trans-sender.js -->
-<!-- include lib/trans-jsonp-receiver.js -->
-<!-- include lib/trans-jsonp-polling.js -->
-<!-- include lib/trans-xhr.js -->
-<!-- include lib/trans-iframe.js -->
-<!-- include lib/trans-iframe-within.js -->
-<!-- include lib/info.js -->
-<!-- include lib/trans-iframe-eventsource.js -->
-<!-- include lib/trans-iframe-xhr-polling.js -->
-<!-- include lib/trans-iframe-htmlfile.js -->
-<!-- include lib/trans-polling.js -->
-<!-- include lib/trans-receiver-eventsource.js -->
-<!-- include lib/trans-receiver-htmlfile.js -->
-<!-- include lib/trans-receiver-xhr.js -->
-<!-- include lib/test-hooks.js -->
- return SockJS;
- })();
-if ('_sockjs_onload' in window) setTimeout(_sockjs_onload, 1);
-
-// AMD compliance
-if (typeof define === 'function' && define.amd) {
- define([], function(){return SockJS;});
-}
diff --git a/lib/info-receiver-fake.js b/lib/info-receiver-fake.js
new file mode 100644
index 0000000..b85d7d1
--- /dev/null
+++ b/lib/info-receiver-fake.js
@@ -0,0 +1,24 @@
+'use strict';
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ * Copyright (c) 2011-2012 VMware, Inc.
+ *
+ * For the license see COPYING.
+ * ***** END LICENSE BLOCK *****
+ */
+
+var utils = require('./utils');
+var EventEmitter = require('./eventemitter');
+
+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;
+ utils.delay(function() {
+ that.emit('finish', {}, 2000);
+ });
+};
+InfoReceiverFake.prototype = new EventEmitter(['finish']);
+
+module.exports = InfoReceiverFake;
\ No newline at end of file
diff --git a/lib/info-receiver-iframe.js b/lib/info-receiver-iframe.js
new file mode 100644
index 0000000..13d4873
--- /dev/null
+++ b/lib/info-receiver-iframe.js
@@ -0,0 +1,47 @@
+'use strict';
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ * Copyright (c) 2011-2012 VMware, Inc.
+ *
+ * For the license see COPYING.
+ * ***** END LICENSE BLOCK *****
+ */
+
+var utils = require('./utils');
+var EventEmitter = require('./eventemitter');
+var IframeTransport = require('./trans-iframe');
+var JSON3 = require('json3').noConflict();
+
+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 = {
+ _options: {},
+ _didClose: fun,
+ _didMessage: fun
+ };
+ ifr.i_constructor(mock_ri, base_url, base_url);
+ };
+ if(!document.body) {
+ utils.attachEvent('load', go);
+ } else {
+ go();
+ }
+}
+
+InfoReceiverIframe.prototype = new EventEmitter(['finish']);
+
+module.exports = InfoReceiverIframe;
\ No newline at end of file
diff --git a/lib/info-receiver.js b/lib/info-receiver.js
new file mode 100644
index 0000000..b900c9b
--- /dev/null
+++ b/lib/info-receiver.js
@@ -0,0 +1,63 @@
+'use strict';
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ * Copyright (c) 2011-2012 VMware, Inc.
+ *
+ * For the license see COPYING.
+ * ***** END LICENSE BLOCK *****
+ */
+
+var utils = require('./utils');
+var EventEmitter = require('./eventemitter');
+var FacadeJS = require('./facade');
+var JSON3 = require('json3').noConflict();
+
+function InfoReceiver(base_url, AjaxObject) {
+ var that = this;
+ utils.delay(function(){that.doXhr(base_url, AjaxObject);});
+}
+
+InfoReceiver.prototype = new EventEmitter(['finish']);
+
+InfoReceiver.prototype.doXhr = function(base_url, AjaxObject) {
+ var that = this;
+ var t0 = (new Date()).getTime();
+ var xo = new AjaxObject('GET', base_url + '/info');
+
+ var tref = utils.delay(8000,
+ function(){xo.ontimeout();});
+
+ xo.onfinish = 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.ontimeout = function() {
+ xo.close();
+ that.emit('finish');
+ };
+};
+
+module.exports = InfoReceiver;
+
+var WInfoReceiverIframe = FacadeJS['w-iframe-info-receiver'] = function(ri, _trans_url, base_url) {
+ var ir = new InfoReceiver(base_url, utils.XHRLocalObject);
+ ir.onfinish = function(info, rtt) {
+ ri._didMessage('m'+JSON3.stringify([info, rtt]));
+ ri._didClose();
+ };
+};
+WInfoReceiverIframe.prototype.doCleanup = function() {};
\ No newline at end of file
diff --git a/lib/info.js b/lib/info.js
deleted file mode 100644
index fc5b662..0000000
--- a/lib/info.js
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-var InfoReceiver = function(base_url, AjaxObject) {
- var that = this;
- utils.delay(function(){that.doXhr(base_url, AjaxObject);});
-};
-
-InfoReceiver.prototype = new EventEmitter(['finish']);
-
-InfoReceiver.prototype.doXhr = function(base_url, AjaxObject) {
- var that = this;
- var t0 = (new Date()).getTime();
- var xo = new AjaxObject('GET', base_url + '/info');
-
- var tref = utils.delay(8000,
- function(){xo.ontimeout();});
-
- xo.onfinish = function(status, text) {
- clearTimeout(tref);
- tref = null;
- if (status === 200) {
- var rtt = (new Date()).getTime() - t0;
- var info;
- if (text) {
- try {
- info = JSON.parse(text);
- }
- catch (e) {}
- }
- if (typeof info !== 'object') info = {};
- that.emit('finish', info, rtt);
- } else {
- that.emit('finish');
- }
- };
- xo.ontimeout = function() {
- xo.close();
- that.emit('finish');
- };
-};
-
-var InfoReceiverIframe = function(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 = JSON.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 = {
- _options: {},
- _didClose: fun,
- _didMessage: fun
- };
- ifr.i_constructor(mock_ri, base_url, base_url);
- }
- if(!_document.body) {
- utils.attachEvent('load', go);
- } else {
- go();
- }
-};
-InfoReceiverIframe.prototype = new EventEmitter(['finish']);
-
-
-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;
- utils.delay(function() {
- that.emit('finish', {}, 2000);
- });
-};
-InfoReceiverFake.prototype = new EventEmitter(['finish']);
-
-var createInfoReceiver = function(base_url) {
- if (utils.isSameOriginUrl(base_url)) {
- // If, for some reason, we have SockJS locally - there's no
- // need to start up the complex machinery. Just use ajax.
- return new InfoReceiver(base_url, utils.XHRLocalObject);
- }
- switch (utils.isXHRCorsCapable()) {
- case 1:
- // XHRLocalObject -> no_credentials=true
- return new InfoReceiver(base_url, utils.XHRLocalObject);
- case 2:
- // IE 8/9 if the request target uses the same scheme
- if (utils.isSameOriginScheme(base_url)) {
- return new InfoReceiver(base_url, utils.XDRObject);
- }
- break;
- case 3:
- // Opera
- return new InfoReceiverIframe(base_url);
- };
-
- // IE 7, and IE 8/9 if requesting across schemes (e.g. http -> https)
- return new InfoReceiverFake();
-};
-
-
-var WInfoReceiverIframe = FacadeJS['w-iframe-info-receiver'] = function(ri, _trans_url, base_url) {
- var ir = new InfoReceiver(base_url, utils.XHRLocalObject);
- ir.onfinish = function(info, rtt) {
- ri._didMessage('m'+JSON.stringify([info, rtt]));
- ri._didClose();
- }
-};
-WInfoReceiverIframe.prototype.doCleanup = function() {};
-
diff --git a/lib/json2.min.js b/lib/json2.min.js
deleted file mode 100644
index 04e8d93..0000000
--- a/lib/json2.min.js
+++ /dev/null
@@ -1 +0,0 @@
-var JSON;JSON||(JSON={}),function(){function str(a,b){var c,d,e,f,g=gap,h,i=b[a];i&&typeof i=="object"&&typeof i.toJSON=="function"&&(i=i.toJSON(a)),typeof rep=="function"&&(i=rep.call(b,a,i));switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i)return"null";gap+=indent,h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1)h[c]=str(c,i)||"null";e=h.l [...]
\ No newline at end of file
diff --git a/lib/reventtarget.js b/lib/reventtarget.js
index aed5f85..68f99b1 100644
--- a/lib/reventtarget.js
+++ b/lib/reventtarget.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -9,6 +10,9 @@
/* 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) {
@@ -63,3 +67,5 @@ REventTarget.prototype.dispatchEvent = function (event) {
}
}
};
+
+module.exports = REventTarget;
diff --git a/lib/simpleevent.js b/lib/simpleevent.js
index 47b5938..35124ae 100644
--- a/lib/simpleevent.js
+++ b/lib/simpleevent.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -26,3 +27,5 @@ SimpleEvent.prototype.toString = function() {
}
return 'SimpleEvent(' + r.join(', ') + ')';
};
+
+module.exports = SimpleEvent;
diff --git a/lib/sockjs.js b/lib/sockjs.js
index 12de0f7..bd14725 100644
--- a/lib/sockjs.js
+++ b/lib/sockjs.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -6,12 +7,21 @@
* ***** END LICENSE BLOCK *****
*/
-var SockJS = function(url, dep_protocols_whitelist, options) {
+var utils = require('./utils');
+var REventTarget = require('./reventtarget');
+var SimpleEvent = require('./SimpleEvent');
+var InfoReceiver = require('./info-receiver');
+var InfoReceiverIframe = require('./info-receiver-iframe');
+var InfoReceiverFake = require('./info-receiver-fake');
+var FacadeJS = require('./facade');
+var JSON3 = require('json3').noConflict();
+
+function SockJS(url, dep_protocols_whitelist, options) {
if (!(this instanceof SockJS)) {
// makes `new` optional
return new SockJS(url, dep_protocols_whitelist, options);
}
-
+
var that = this, protocols_whitelist;
that._options = {devel: false, debug: false, protocols_whitelist: [],
info: undefined, rtt: undefined};
@@ -29,7 +39,7 @@ var SockJS = function(url, dep_protocols_whitelist, options) {
dep_protocols_whitelist.length > 0) {
protocols_whitelist = [dep_protocols_whitelist];
} else if (utils.isArray(dep_protocols_whitelist)) {
- protocols_whitelist = dep_protocols_whitelist
+ protocols_whitelist = dep_protocols_whitelist;
} else {
protocols_whitelist = null;
}
@@ -59,7 +69,8 @@ var SockJS = function(url, dep_protocols_whitelist, options) {
that._didClose(1002, 'Can\'t connect to server', true);
}
};
-};
+}
+
// Inheritance
SockJS.prototype = new REventTarget();
@@ -146,22 +157,23 @@ SockJS.prototype._didClose = function(code, reason, force) {
SockJS.prototype._didMessage = function(data) {
var that = this;
var type = data.slice(0, 1);
+ var payload;
switch(type) {
case 'o':
that._dispatchOpen();
break;
case 'a':
- var payload = JSON.parse(data.slice(1) || '[]');
+ payload = JSON3.parse(data.slice(1) || '[]');
for(var i=0; i < payload.length; i++){
that._dispatchMessage(payload[i]);
}
break;
case 'm':
- var payload = JSON.parse(data.slice(1) || 'null');
+ payload = JSON3.parse(data.slice(1) || 'null');
that._dispatchMessage(payload);
break;
case 'c':
- var payload = JSON.parse(data.slice(1) || '[]');
+ payload = JSON3.parse(data.slice(1) || '[]');
that._didClose(payload[0], payload[1]);
break;
case 'h':
@@ -181,6 +193,19 @@ SockJS.prototype._try_next_protocol = function(close_event) {
that._transport_tref = null;
}
+ function timeoutFunction() {
+ if (that.readyState === SockJS.CONNECTING) {
+ // I can't understand how it is possible to run
+ // this timer, when the state is CLOSED, but
+ // apparently in IE everythin is possible.
+ that._didClose(2007, "Transport timeouted");
+ }
+ }
+
+ function tryNextProtocol() {
+ that._try_next_protocol();
+ }
+
while(1) {
var protocol = that.protocol = that._protocols.shift();
if (!protocol) {
@@ -190,14 +215,12 @@ SockJS.prototype._try_next_protocol = function(close_event) {
// the `head`?
if (SockJS[protocol] &&
SockJS[protocol].need_body === true &&
- (!_document.body ||
- (typeof _document.readyState !== 'undefined'
- && _document.readyState !== 'complete'))) {
+ (!document.body ||
+ (typeof document.readyState !== 'undefined'
+ && document.readyState !== 'complete'))) {
that._protocols.unshift(protocol);
that.protocol = 'waiting-for-load';
- utils.attachEvent('load', function(){
- that._try_next_protocol();
- });
+ utils.attachEvent('load', tryNextProtocol);
return true;
}
@@ -207,14 +230,7 @@ SockJS.prototype._try_next_protocol = function(close_event) {
} else {
var roundTrips = SockJS[protocol].roundTrips || 1;
var to = ((that._options.rto || 0) * roundTrips) || 5000;
- that._transport_tref = utils.delay(to, function() {
- if (that.readyState === SockJS.CONNECTING) {
- // I can't understand how it is possible to run
- // this timer, when the state is CLOSED, but
- // apparently in IE everythin is possible.
- that._didClose(2007, "Transport timeouted");
- }
- });
+ that._transport_tref = utils.delay(to, timeoutFunction);
var connid = utils.random_string(8);
var trans_url = that._base_url + '/' + that._server + '/' + connid;
@@ -255,7 +271,7 @@ SockJS.prototype._applyInfo = function(info, rtt, protocols_whitelist) {
that._options.info = info;
that._options.rtt = rtt;
that._options.rto = utils.countRTO(rtt);
- that._options.info.null_origin = !_document.domain;
+ that._options.info.null_origin = !document.domain;
// Servers can override base_url, eg to provide a randomized domain name and
// avoid browser per-domain connection limits.
if (info.base_url)
@@ -263,3 +279,103 @@ SockJS.prototype._applyInfo = function(info, rtt, protocols_whitelist) {
var probed = utils.probeProtocols();
that._protocols = utils.detectProtocols(probed, protocols_whitelist, info);
};
+
+utils.parent_origin = undefined;
+
+SockJS.bootstrap_iframe = function() {
+ var facade;
+ utils.curr_window_id = document.location.hash.slice(1);
+ var onMessage = function(e) {
+ if(e.source !== parent) return;
+ if(typeof utils.parent_origin === 'undefined')
+ utils.parent_origin = e.origin;
+ if (e.origin !== utils.parent_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 !== utils.curr_window_id) return;
+ switch(type) {
+ case 's':
+ var p = JSON3.parse(data);
+ var version = p[0];
+ var protocol = p[1];
+ var trans_url = p[2];
+ var base_url = p[3];
+ if (version !== SockJS.version) {
+ utils.log("Incompatibile SockJS! Main site uses:" +
+ " \"" + version + "\", the iframe:" +
+ " \"" + SockJS.version + "\".");
+ }
+ if (!utils.flatUrl(trans_url) || !utils.flatUrl(base_url)) {
+ utils.log("Only basic urls are supported in SockJS");
+ return;
+ }
+
+ if (!utils.isSameOriginUrl(trans_url) ||
+ !utils.isSameOriginUrl(base_url)) {
+ utils.log("Can't connect to different domain from within an " +
+ "iframe. (" + JSON3.stringify([window.location.href, trans_url, base_url]) +
+ ")");
+ return;
+ }
+ facade = new FacadeJS();
+ facade._transport = new FacadeJS[protocol](facade, trans_url, base_url);
+ break;
+ case 'm':
+ facade._doSend(data);
+ break;
+ case 'c':
+ if (facade)
+ facade._doCleanup();
+ facade = null;
+ break;
+ }
+ };
+
+ // alert('test ticker');
+ // facade = new FacadeJS();
+ // facade._transport = new FacadeJS['w-iframe-xhr-polling'](facade, 'http://host.com:9999/ticker/12/basd');
+
+ utils.attachMessage(onMessage);
+
+ // Start
+ utils.postMessage('s');
+};
+
+SockJS.websocket = require('./trans-websocket');
+SockJS['iframe-eventsource'] = require('./trans-iframe-eventsource');
+SockJS['iframe-htmlfile'] = require('./trans-iframe-htmlfile');
+SockJS['iframe-xhr-polling'] = require('./trans-iframe-xhr-polling');
+SockJS['jsonp-polling'] = require('./trans-jsonp-polling');
+SockJS['xdr-polling'] = require('./xdr-polling');
+SockJS['xdr-streaming'] = require('./xdr-streaming');
+SockJS['xhr-polling'] = require('./xhr-polling');
+SockJS['xhr-streaming'] = require('./xhr-streaming');
+
+module.exports = SockJS;
+
+function createInfoReceiver(base_url) {
+ if (utils.isSameOriginUrl(base_url)) {
+ // If, for some reason, we have SockJS locally - there's no
+ // need to start up the complex machinery. Just use ajax.
+ return new InfoReceiver(base_url, utils.XHRLocalObject);
+ }
+ switch (utils.isXHRCorsCapable()) {
+ case 1:
+ // XHRLocalObject -> no_credentials=true
+ return new InfoReceiver(base_url, utils.XHRLocalObject);
+ case 2:
+ // IE 8/9 if the request target uses the same scheme
+ if (utils.isSameOriginScheme(base_url)) {
+ return new InfoReceiver(base_url, utils.XDRObject);
+ }
+ break;
+ case 3:
+ // Opera
+ return new InfoReceiverIframe(base_url);
+ }
+
+ // IE 7, and IE 8/9 if requesting across schemes (e.g. http -> https)
+ return new InfoReceiverFake();
+}
diff --git a/lib/test-hooks.js b/lib/test-hooks.js
deleted file mode 100644
index 706e01b..0000000
--- a/lib/test-hooks.js
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-// For testing
-SockJS.getUtils = function(){
- return utils;
-};
-
-SockJS.getIframeTransport = function(){
- return IframeTransport;
-};
diff --git a/lib/trans-eventsource.js b/lib/trans-eventsource.js
new file mode 100644
index 0000000..bb71161
--- /dev/null
+++ b/lib/trans-eventsource.js
@@ -0,0 +1,14 @@
+'use strict';
+
+var AjaxBasedTransport = require('./ajax-based');
+var EventSourceReceiver = require('./trans-receiver-eventsource');
+var FacadeJS = require('./facade');
+var utils = require('./utils');
+
+// w-iframe-eventsource
+var EventSourceTransport = FacadeJS['w-iframe-eventsource'] = function EventSourceTransport(ri, trans_url) {
+ this.run(ri, trans_url, '/eventsource', EventSourceReceiver, utils.XHRLocalObject);
+};
+EventSourceTransport.prototype = new AjaxBasedTransport();
+
+module.exports = EventSourceTransport;
\ No newline at end of file
diff --git a/lib/trans-iframe-eventsource.js b/lib/trans-iframe-eventsource.js
index 3eeb9dd..9fb261e 100644
--- a/lib/trans-iframe-eventsource.js
+++ b/lib/trans-iframe-eventsource.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -6,24 +7,21 @@
* ***** END LICENSE BLOCK *****
*/
-var EventSourceIframeTransport = SockJS['iframe-eventsource'] = function () {
+var IframeTransport = require('./trans-iframe');
+
+function EventSourceIframeTransport() {
var that = this;
that.protocol = 'w-iframe-eventsource';
that.i_constructor.apply(that, arguments);
-};
+}
EventSourceIframeTransport.prototype = new IframeTransport();
EventSourceIframeTransport.enabled = function () {
- return ('EventSource' in _window) && IframeTransport.enabled();
+ return ('EventSource' in window) && IframeTransport.enabled();
};
EventSourceIframeTransport.need_body = true;
EventSourceIframeTransport.roundTrips = 3; // html, javascript, eventsource
-
-// w-iframe-eventsource
-var EventSourceTransport = FacadeJS['w-iframe-eventsource'] = function(ri, trans_url) {
- this.run(ri, trans_url, '/eventsource', EventSourceReceiver, utils.XHRLocalObject);
-}
-EventSourceTransport.prototype = new AjaxBasedTransport();
+module.exports = EventSourceIframeTransport;
\ No newline at end of file
diff --git a/lib/trans-iframe-htmlfile.js b/lib/trans-iframe-htmlfile.js
index 3b1ee25..25de7af 100644
--- a/lib/trans-iframe-htmlfile.js
+++ b/lib/trans-iframe-htmlfile.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -11,11 +12,17 @@
// We may test this transport in all browsers - why not, but in
// production it should be only run in IE.
-var HtmlFileIframeTransport = SockJS['iframe-htmlfile'] = function () {
+var IframeTransport = require('./trans-iframe');
+var utils = require('./utils');
+var AjaxBasedTransport = require('./ajax-based');
+var HtmlfileReceiver = require('./trans-receiver-htmlfile');
+var FacadeJS = require('./facade');
+
+function HtmlFileIframeTransport() {
var that = this;
that.protocol = 'w-iframe-htmlfile';
that.i_constructor.apply(that, arguments);
-};
+}
// Inheritance.
HtmlFileIframeTransport.prototype = new IframeTransport();
@@ -27,6 +34,8 @@ HtmlFileIframeTransport.enabled = function() {
HtmlFileIframeTransport.need_body = true;
HtmlFileIframeTransport.roundTrips = 3; // html, javascript, htmlfile
+module.exports = HtmlFileIframeTransport;
+
// w-iframe-htmlfile
var HtmlFileTransport = FacadeJS['w-iframe-htmlfile'] = function(ri, trans_url) {
diff --git a/lib/trans-iframe-within.js b/lib/trans-iframe-within.js
deleted file mode 100644
index 9a642cf..0000000
--- a/lib/trans-iframe-within.js
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-var curr_window_id;
-
-var postMessage = function (type, data) {
- if(parent !== _window) {
- parent.postMessage(curr_window_id + type + (data || ''), '*');
- } else {
- utils.log("Can't postMessage, no parent window.", type, data);
- }
-};
-
-var FacadeJS = function() {};
-FacadeJS.prototype._didClose = function (code, reason) {
- postMessage('t', utils.closeFrame(code, reason));
-};
-FacadeJS.prototype._didMessage = function (frame) {
- postMessage('t', frame);
-};
-FacadeJS.prototype._doSend = function (data) {
- this._transport.doSend(data);
-};
-FacadeJS.prototype._doCleanup = function () {
- this._transport.doCleanup();
-};
-
-utils.parent_origin = undefined;
-
-SockJS.bootstrap_iframe = function() {
- var facade;
- curr_window_id = _document.location.hash.slice(1);
- var onMessage = function(e) {
- if(e.source !== parent) return;
- if(typeof utils.parent_origin === 'undefined')
- utils.parent_origin = e.origin;
- if (e.origin !== utils.parent_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 !== curr_window_id) return;
- switch(type) {
- case 's':
- var p = JSON.parse(data);
- var version = p[0];
- var protocol = p[1];
- var trans_url = p[2];
- var base_url = p[3];
- if (version !== SockJS.version) {
- utils.log("Incompatibile SockJS! Main site uses:" +
- " \"" + version + "\", the iframe:" +
- " \"" + SockJS.version + "\".");
- }
- if (!utils.flatUrl(trans_url) || !utils.flatUrl(base_url)) {
- utils.log("Only basic urls are supported in SockJS");
- return;
- }
-
- if (!utils.isSameOriginUrl(trans_url) ||
- !utils.isSameOriginUrl(base_url)) {
- utils.log("Can't connect to different domain from within an " +
- "iframe. (" + JSON.stringify([_window.location.href, trans_url, base_url]) +
- ")");
- return;
- }
- facade = new FacadeJS();
- facade._transport = new FacadeJS[protocol](facade, trans_url, base_url);
- break;
- case 'm':
- facade._doSend(data);
- break;
- case 'c':
- if (facade)
- facade._doCleanup();
- facade = null;
- break;
- }
- };
-
- // alert('test ticker');
- // facade = new FacadeJS();
- // facade._transport = new FacadeJS['w-iframe-xhr-polling'](facade, 'http://host.com:9999/ticker/12/basd');
-
- utils.attachMessage(onMessage);
-
- // Start
- postMessage('s');
-};
diff --git a/lib/trans-iframe-xhr-polling.js b/lib/trans-iframe-xhr-polling.js
index eadace1..969832a 100644
--- a/lib/trans-iframe-xhr-polling.js
+++ b/lib/trans-iframe-xhr-polling.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -6,21 +7,28 @@
* ***** END LICENSE BLOCK *****
*/
-var XhrPollingIframeTransport = SockJS['iframe-xhr-polling'] = function () {
+var IframeTransport = require('./trans-iframe');
+var utils = require('./utils');
+var AjaxBasedTransport = require('./ajax-based');
+var XhrReceiver = require('./trans-receiver-xhr');
+var FacadeJS = require('./facade');
+
+function XhrPollingIframeTransport() {
var that = this;
that.protocol = 'w-iframe-xhr-polling';
that.i_constructor.apply(that, arguments);
-};
+}
XhrPollingIframeTransport.prototype = new IframeTransport();
XhrPollingIframeTransport.enabled = function () {
- return _window.XMLHttpRequest && IframeTransport.enabled();
+ return window.XMLHttpRequest && IframeTransport.enabled();
};
XhrPollingIframeTransport.need_body = true;
XhrPollingIframeTransport.roundTrips = 3; // html, javascript, xhr
+module.exports = XhrPollingIframeTransport;
// w-iframe-xhr-polling
var XhrPollingITransport = FacadeJS['w-iframe-xhr-polling'] = function(ri, trans_url) {
diff --git a/lib/trans-iframe.js b/lib/trans-iframe.js
index ec1fd09..8063994 100644
--- a/lib/trans-iframe.js
+++ b/lib/trans-iframe.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -14,7 +15,10 @@
// http://msdn.microsoft.com/en-us/library/cc197015(v=VS.85).aspx
// http://stevesouders.com/misc/test-postmessage.php
-var IframeTransport = function() {};
+var JSON3 = require('json3').noConflict();
+var utils = require('./utils');
+
+function IframeTransport() {}
IframeTransport.prototype.i_constructor = function(ri, trans_url, base_url) {
var that = this;
@@ -25,7 +29,7 @@ IframeTransport.prototype.i_constructor = function(ri, trans_url, base_url) {
var iframe_url = base_url + '/iframe.html';
if (that.ri._options.devel) {
- iframe_url += '?t=' + (+new Date);
+ iframe_url += '?t=' + (+new Date());
}
that.window_id = utils.random_string(8);
iframe_url += '#' + that.window_id;
@@ -65,7 +69,7 @@ IframeTransport.prototype.onmessage = function(e) {
switch(type) {
case 's':
that.iframeObj.loaded();
- that.postMessage('s', JSON.stringify([SockJS.version, that.protocol, that.trans_url, that.base_url]));
+ that.postMessage('s', JSON3.stringify([window.SockJS.version, that.protocol, that.trans_url, that.base_url]));
break;
case 't':
that.ri._didMessage(data);
@@ -86,6 +90,8 @@ 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));
+ 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 bfb37b2..4dfb425 100644
--- a/lib/trans-jsonp-polling.js
+++ b/lib/trans-jsonp-polling.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -14,15 +15,17 @@
// 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 JsonPTransport = SockJS['jsonp-polling'] = function(ri, trans_url) {
+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();
-};
+}
// Inheritnace
JsonPTransport.prototype = new BufferedSender();
@@ -67,7 +70,7 @@ JsonPTransport.prototype.doCleanup = function() {
// 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=' + escape(WPrefix + '.' + id);
+ var url_id = url + '?c=' + encodeURIComponent(utils.WPrefix + '.' + id);
// Unfortunately it is not possible to abort loading of the
// script. We need to keep track of frake close frames.
@@ -78,7 +81,7 @@ var jsonPReceiverWrapper = function(url, constructReceiver, user_callback) {
switch(aborting) {
case 0:
// Normal behaviour - delete hook _and_ emit message.
- delete _window[WPrefix][id];
+ delete window[utils.WPrefix][id];
user_callback(frame);
break;
case 1:
@@ -88,18 +91,187 @@ var jsonPReceiverWrapper = function(url, constructReceiver, user_callback) {
break;
case 2:
// Got frame after connection was closed, delete hook, don't emit.
- delete _window[WPrefix][id];
+ delete window[utils.WPrefix][id];
break;
}
};
var close_script = constructReceiver(url_id, callback);
- _window[WPrefix][id] = close_script;
+ window[utils.WPrefix][id] = close_script;
var stop = function() {
- if (_window[WPrefix][id]) {
+ if (window[utils.WPrefix][id]) {
aborting = 1;
- _window[WPrefix][id](utils.closeFrame(1000, "JSONP user aborted read"));
+ window[utils.WPrefix][id](utils.closeFrame(1000, "JSONP user aborted read"));
}
};
return stop;
};
+
+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;
+ }
+ iframe.id = id;
+ form.appendChild(iframe);
+ iframe.style.display = 'none';
+
+ try {
+ area.value = payload;
+ } catch(e) {
+ utils.log('Your browser is seriously broken. Go home! ' + e.message);
+ }
+ form.submit();
+
+ var completed = function(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.
+ utils.delay(500, function() {
+ iframe.parentNode.removeChild(iframe);
+ iframe = null;
+ });
+ 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;
+}
+
+// Parts derived from Socket.io:
+// https://github.com/LearnBoost/socket.io/blob/0.6.17/lib/socket.io/transports/jsonp-polling.js
+// and jQuery-JSONP:
+// https://code.google.com/p/jquery-jsonp/source/browse/trunk/core/jquery.jsonp.js
+function jsonPGenericReceiver(url, callback) {
+ var tref;
+ var script = 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;
+ }
+ };
+
+ // IE9 fires 'error' event after orsc or before, in random order.
+ var loaded_okay = false;
+ var error_timer = 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.onload = function(e) {
+ close_script(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;
+ }
+ }
+ 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);
+
+ var head = document.getElementsByTagName('head')[0];
+ head.insertBefore(script, head.firstChild);
+ if (script2) {
+ head.insertBefore(script2, head.firstChild);
+ }
+ return close_script;
+}
\ No newline at end of file
diff --git a/lib/trans-jsonp-receiver.js b/lib/trans-jsonp-receiver.js
deleted file mode 100644
index 3e26998..0000000
--- a/lib/trans-jsonp-receiver.js
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-// Parts derived from Socket.io:
-// https://github.com/LearnBoost/socket.io/blob/0.6.17/lib/socket.io/transports/jsonp-polling.js
-// and jQuery-JSONP:
-// https://code.google.com/p/jquery-jsonp/source/browse/trunk/core/jquery.jsonp.js
-var jsonPGenericReceiver = function(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;
- }
- };
-
- // IE9 fires 'error' event after orsc or before, in random order.
- var loaded_okay = false;
- var error_timer = 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.onload = function(e) {
- close_script(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;
- }
- }
- 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);
-
- var head = _document.getElementsByTagName('head')[0];
- head.insertBefore(script, head.firstChild);
- if (script2) {
- head.insertBefore(script2, head.firstChild);
- }
- return close_script;
-};
diff --git a/lib/trans-polling.js b/lib/trans-polling.js
index 24cb95f..ea183b2 100644
--- a/lib/trans-polling.js
+++ b/lib/trans-polling.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -6,14 +7,14 @@
* ***** END LICENSE BLOCK *****
*/
-var Polling = function(ri, Receiver, recv_url, AjaxObject) {
+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();
-};
+}
Polling.prototype._scheduleRecv = function() {
var that = this;
@@ -42,3 +43,5 @@ Polling.prototype.abort = function() {
that.poll.abort();
}
};
+
+module.exports = Polling;
\ No newline at end of file
diff --git a/lib/trans-receiver-eventsource.js b/lib/trans-receiver-eventsource.js
index 9edbdd2..eee45c9 100644
--- a/lib/trans-receiver-eventsource.js
+++ b/lib/trans-receiver-eventsource.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -6,12 +7,16 @@
* ***** END LICENSE BLOCK *****
*/
-var EventSourceReceiver = function(url) {
+var SimpleEvent = require('./simpleevent');
+var REventTarget = require('./reventtarget');
+var utils = require('./utils');
+
+function EventSourceReceiver(url) {
var that = this;
var es = new EventSource(url);
es.onmessage = function(e) {
that.dispatchEvent(new SimpleEvent('message',
- {'data': unescape(e.data)}));
+ {'data': decodeURI(e.data)}));
};
that.es_close = es.onerror = function(e, abort_reason) {
// ES on reconnection has readyState = 0 or 1.
@@ -29,7 +34,7 @@ var EventSourceReceiver = function(url) {
that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
});
};
-};
+}
EventSourceReceiver.prototype = new REventTarget();
@@ -39,3 +44,5 @@ EventSourceReceiver.prototype.abort = function() {
that.es_close({}, true);
}
};
+
+module.exports = EventSourceReceiver;
diff --git a/lib/trans-receiver-htmlfile.js b/lib/trans-receiver-htmlfile.js
index e2e3dd3..54270e2 100644
--- a/lib/trans-receiver-htmlfile.js
+++ b/lib/trans-receiver-htmlfile.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -6,10 +7,14 @@
* ***** END LICENSE BLOCK *****
*/
+var utils = require('./utils');
+var SimpleEvent = require('./simpleevent');
+var REventTarget = require('./reventtarget');
+
var _is_ie_htmlfile_capable;
var isIeHtmlfileCapable = function() {
if (_is_ie_htmlfile_capable === undefined) {
- if ('ActiveXObject' in _window) {
+ if ('ActiveXObject' in window) {
try {
_is_ie_htmlfile_capable = !!new ActiveXObject('htmlfile');
} catch (x) {}
@@ -21,19 +26,19 @@ var isIeHtmlfileCapable = function() {
};
-var HtmlfileReceiver = function(url) {
+function HtmlfileReceiver(url) {
var that = this;
utils.polluteGlobalNamespace();
that.id = 'a' + utils.random_string(6);
url += ((url.indexOf('?') === -1) ? '?' : '&') +
- 'c=' + escape(WPrefix + '.' + that.id);
+ 'c=' + decodeURIComponent(utils.WPrefix + '.' + that.id);
var constructor = isIeHtmlfileCapable() ?
utils.createHtmlfile : utils.createIframe;
var iframeObj;
- _window[WPrefix][that.id] = {
+ window[utils.WPrefix][that.id] = {
start: function () {
iframeObj.loaded();
},
@@ -47,13 +52,13 @@ var HtmlfileReceiver = function(url) {
that.iframe_close = function(e, abort_reason) {
iframeObj.cleanup();
that.iframe_close = iframeObj = null;
- delete _window[WPrefix][that.id];
+ delete window[utils.WPrefix][that.id];
that.dispatchEvent(new SimpleEvent('close', {reason: abort_reason}));
};
iframeObj = constructor(url, function(e) {
that.iframe_close({}, 'permanent');
});
-};
+}
HtmlfileReceiver.prototype = new REventTarget();
@@ -63,3 +68,5 @@ HtmlfileReceiver.prototype.abort = function() {
that.iframe_close({}, 'user');
}
};
+
+module.exports = HtmlfileReceiver;
diff --git a/lib/trans-receiver-xhr.js b/lib/trans-receiver-xhr.js
index d114ebc..9343ec5 100644
--- a/lib/trans-receiver-xhr.js
+++ b/lib/trans-receiver-xhr.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -6,7 +7,10 @@
* ***** END LICENSE BLOCK *****
*/
-var XhrReceiver = function(url, AjaxObject) {
+var SimpleEvent = require('./simpleevent');
+var REventTarget = require('./reventtarget');
+
+function XhrReceiver(url, AjaxObject) {
var that = this;
var buf_pos = 0;
@@ -27,8 +31,8 @@ var XhrReceiver = function(url, AjaxObject) {
that.xo = null;
var reason = status === 200 ? 'network' : 'permanent';
that.dispatchEvent(new SimpleEvent('close', {reason: reason}));
- }
-};
+ };
+}
XhrReceiver.prototype = new REventTarget();
@@ -40,3 +44,5 @@ XhrReceiver.prototype.abort = function() {
that.xo = null;
}
};
+
+module.exports = XhrReceiver;
\ No newline at end of file
diff --git a/lib/trans-sender.js b/lib/trans-sender.js
deleted file mode 100644
index 22762af..0000000
--- a/lib/trans-sender.js
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-var BufferedSender = function() {};
-BufferedSender.prototype.send_constructor = function(sender) {
- var that = this;
- that.send_buffer = [];
- that.sender = sender;
-};
-BufferedSender.prototype.doSend = function(message) {
- var that = this;
- that.send_buffer.push(message);
- if (!that.send_stop) {
- that.send_schedule();
- }
-};
-
-// For polling transports in a situation when in the message callback,
-// new message is being send. If the sending connection was started
-// before receiving one, it is possible to saturate the network and
-// timeout due to the lack of receiving socket. To avoid that we delay
-// sending messages by some small time, in order to let receiving
-// 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 = utils.delay(25, function() {
- that.send_stop = null;
- that.send_schedule();
- });
-};
-
-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.send_destructor = function() {
- var that = this;
- if (that._send_stop) {
- that._send_stop();
- }
- that._send_stop = null;
-};
-
-var jsonPGenericSender = function(url, payload, callback) {
- var that = this;
-
- if (!('_send_form' in that)) {
- var form = that._send_form = _document.createElement('form');
- var area = that._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 form = that._send_form;
- var area = that._send_area;
- 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;
- }
- iframe.id = id;
- form.appendChild(iframe);
- iframe.style.display = 'none';
-
- try {
- area.value = payload;
- } catch(e) {
- utils.log('Your browser is seriously broken. Go home! ' + e.message);
- }
- form.submit();
-
- var completed = function(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.
- utils.delay(500, function() {
- iframe.parentNode.removeChild(iframe);
- iframe = null;
- });
- 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;
-};
-
-var createAjaxSender = function(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.onfinish = function(status, text) {
- callback(status === 200 || status === 204,
- 'http status ' + status);
- };
- return function(abort_reason) {
- callback(false, abort_reason);
- };
- };
-};
diff --git a/lib/trans-websocket.js b/lib/trans-websocket.js
index 7f6362a..9805760 100644
--- a/lib/trans-websocket.js
+++ b/lib/trans-websocket.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -5,8 +6,9 @@
* For the license see COPYING.
* ***** END LICENSE BLOCK *****
*/
+var utils = require('./utils');
-var WebSocketTransport = SockJS.websocket = function(ri, trans_url) {
+function WebSocketTransport(ri, trans_url) {
var that = this;
var url = trans_url + '/websocket';
if (url.slice(0, 5) === 'https') {
@@ -16,7 +18,7 @@ var WebSocketTransport = SockJS.websocket = function(ri, trans_url) {
}
that.ri = ri;
that.url = url;
- var Constructor = _window.WebSocket || _window.MozWebSocket;
+ var Constructor = window.WebSocket || window.MozWebSocket;
that.ws = new Constructor(that.url);
that.ws.onmessage = function(e) {
@@ -28,11 +30,13 @@ var WebSocketTransport = SockJS.websocket = function(ri, trans_url) {
// 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.unload_ref = utils.unload_add(function(){
+ that.ws.close();
+ });
that.ws.onclose = function() {
that.ri._didMessage(utils.closeFrame(1006, "WebSocket connection broken"));
};
-};
+}
WebSocketTransport.prototype.doSend = function(data) {
this.ws.send('[' + data + ']');
@@ -50,7 +54,7 @@ WebSocketTransport.prototype.doCleanup = function() {
};
WebSocketTransport.enabled = function() {
- return !!(_window.WebSocket || _window.MozWebSocket);
+ return !!(window.WebSocket || window.MozWebSocket);
};
// In theory, ws should require 1 round trip. But in chrome, this is
@@ -58,3 +62,5 @@ WebSocketTransport.enabled = function() {
// separate SSL connection, in which case 2 round trips are an
// absolute minumum.
WebSocketTransport.roundTrips = 2;
+
+module.exports = WebSocketTransport;
diff --git a/lib/trans-xhr.js b/lib/trans-xhr.js
deleted file mode 100644
index 417a9c9..0000000
--- a/lib/trans-xhr.js
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * ***** BEGIN LICENSE BLOCK *****
- * Copyright (c) 2011-2012 VMware, Inc.
- *
- * For the license see COPYING.
- * ***** END LICENSE BLOCK *****
- */
-
-var AjaxBasedTransport = function() {};
-AjaxBasedTransport.prototype = new 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.doCleanup = function() {
- var that = this;
- if (that.poll) {
- that.poll.abort();
- that.poll = null;
- }
-};
-
-// xhr-streaming
-var XhrStreamingTransport = SockJS['xhr-streaming'] = function(ri, trans_url) {
- this.run(ri, trans_url, '/xhr_streaming', XhrReceiver, utils.XHRCorsObject);
-};
-
-XhrStreamingTransport.prototype = new AjaxBasedTransport();
-
-XhrStreamingTransport.enabled = function() {
- // Support for CORS Ajax aka Ajax2? Opera 12 claims CORS but
- // doesn't do streaming.
- try {
- return (_window.XMLHttpRequest &&
- 'withCredentials' in new XMLHttpRequest() &&
- (!/opera/i.test(navigator.userAgent)));
- }
- catch (ignored) {
- return false;
- }
-};
-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;
-
-
-// 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/
-
-
-// xdr-streaming
-var XdrStreamingTransport = SockJS['xdr-streaming'] = function(ri, trans_url) {
- this.run(ri, trans_url, '/xhr_streaming', XhrReceiver, utils.XDRObject);
-};
-
-XdrStreamingTransport.prototype = new AjaxBasedTransport();
-
-XdrStreamingTransport.enabled = function() {
- return !!_window.XDomainRequest;
-};
-XdrStreamingTransport.roundTrips = 2; // preflight, ajax
-
-
-
-// xhr-polling
-var XhrPollingTransport = SockJS['xhr-polling'] = function(ri, trans_url) {
- this.run(ri, trans_url, '/xhr', XhrReceiver, utils.XHRCorsObject);
-};
-
-XhrPollingTransport.prototype = new AjaxBasedTransport();
-
-XhrPollingTransport.enabled = XhrStreamingTransport.enabled;
-XhrPollingTransport.roundTrips = 2; // preflight, ajax
-
-
-// xdr-polling
-var XdrPollingTransport = SockJS['xdr-polling'] = function(ri, trans_url) {
- this.run(ri, trans_url, '/xhr', XhrReceiver, utils.XDRObject);
-};
-
-XdrPollingTransport.prototype = new AjaxBasedTransport();
-
-XdrPollingTransport.enabled = XdrStreamingTransport.enabled;
-XdrPollingTransport.roundTrips = 2; // preflight, ajax
diff --git a/lib/utils.js b/lib/utils.js
index 49ae47c..3c233f5 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -1,3 +1,4 @@
+'use strict';
/*
* ***** BEGIN LICENSE BLOCK *****
* Copyright (c) 2011-2012 VMware, Inc.
@@ -6,17 +7,20 @@
* ***** END LICENSE BLOCK *****
*/
+var JSON3 = require('json3').noConflict();
+var utils = {};
+
// This string has length 32, a power of 2, so the modulus doesn't introduce a
// bias.
var random_string_chars = 'abcdefghijklmnopqrstuvwxyz012345';
// Use real randomness when available.
-if (_window.crypto && _window.crypto.getRandomValues) {
+if (window.crypto && window.crypto.getRandomValues) {
utils.random_string = function(length) {
var max = random_string_chars.length;
var ret = [];
var bytes = new Uint8Array(length);
- _window.crypto.getRandomValues(bytes);
+ window.crypto.getRandomValues(bytes);
for(var i=0; i < length; i++) {
ret.push( random_string_chars[bytes[i] % max] );
}
@@ -37,7 +41,7 @@ utils.random_number = function(max) {
};
utils.random_number_string = function(max) {
var t = (''+(max - 1)).length;
- var p = Array(t+1).join('0');
+ var p = new Array(t+1).join('0');
return (p + utils.random_number(max)).slice(-t);
};
@@ -50,7 +54,7 @@ utils.getOrigin = function(url) {
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;
+ if (!url_b) url_b = window.location.href;
return (url_a.split('/').slice(0,3).join('/')
===
@@ -58,7 +62,7 @@ utils.isSameOriginUrl = function(url_a, url_b) {
};
utils.isSameOriginScheme = function(url_a, url_b) {
- if (!url_b) url_b = _window.location.href;
+ if (!url_b) url_b = window.location.href;
return (url_a.split(':')[0] === url_b.split(':')[0]);
};
@@ -84,16 +88,16 @@ utils.objectExtend = function(dst, src) {
return dst;
};
-var WPrefix = '_jp';
+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'+JSON.stringify([code, reason]);
+ return 'c'+JSON3.stringify([code, reason]);
};
utils.userSetCode = function (code) {
@@ -114,7 +118,7 @@ utils.countRTO = function (rtt) {
};
utils.log = function() {
- if (_window.console && console.log && console.log.apply) {
+ if (window.console && console.log && console.log.apply) {
console.log.apply(console, arguments);
}
};
@@ -134,7 +138,7 @@ utils.flatUrl = function(url) {
};
utils.amendUrl = function(url) {
- var dl = _document.location;
+ var dl = document.location;
if (!url) {
throw new Error('Wrong url for SockJS');
}
@@ -155,12 +159,12 @@ utils.amendUrl = function(url) {
// 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
+ // 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)$/, "");
- }
+ (parts[0] === "https:" && /:443$/.test(parts[2]))) {
+ parts[2] = parts[2].replace(/:(80|443)$/, "");
+ }
url = parts.join("/");
return url;
};
@@ -187,7 +191,7 @@ utils.arrSkip = function(arr, obj) {
// Via: https://gist.github.com/1133122/2121c601c5549155483f50be3da5305e83b8c5df
utils.isArray = Array.isArray || function(value) {
- return {}.toString.call(value).indexOf('Array') >= 0
+ return {}.toString.call(value).indexOf('Array') >= 0;
};
utils.delay = function(t, fun) {
@@ -240,7 +244,7 @@ var extra_escapable = /[\x00-\x1f\ud800-\udfff\ufffe\uffff\u0300-\u0333\u033d-\u
extra_lookup;
// JSON Quote string. Use native implementation when possible.
-var JSONQuote = (JSON && JSON.stringify) || function(string) {
+var quoteJSON = (JSON3 && JSON3.stringify) || function(string) {
json_escapable.lastIndex = 0;
if (json_escapable.test(string)) {
string = string.replace(json_escapable, function(a) {
@@ -254,8 +258,8 @@ var JSONQuote = (JSON && JSON.stringify) || function(string) {
// characters.
var unroll_lookup = function(escapable) {
var i;
- var unrolled = {}
- var c = []
+ var unrolled = {};
+ var c = [];
for(i=0; i<65536; i++) {
c.push( String.fromCharCode(i) );
}
@@ -272,7 +276,7 @@ var unroll_lookup = function(escapable) {
// often break. Especially, take care of unicode surrogates:
// http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Surrogates
utils.quote = function(string) {
- var quoted = JSONQuote(string);
+ var quoted = quoteJSON(string);
// In most cases this should be very fast and good enough.
extra_escapable.lastIndex = 0;
@@ -285,7 +289,7 @@ utils.quote = function(string) {
return quoted.replace(extra_escapable, function(a) {
return extra_lookup[a];
});
-}
+};
var _all_protocols = ['websocket',
'xdr-streaming',
@@ -325,7 +329,7 @@ utils.detectProtocols = function(probed, protocols_whitelist, info) {
maybe_push(protos);
}
}
- }
+ };
// 1. Websocket
if (info.websocket !== false) {
@@ -356,4 +360,232 @@ utils.detectProtocols = function(probed, protocols_whitelist, info) {
}
}
return protocols;
-}
+};
+
+// 1. Is natively via XHR
+// 2. Is natively via XDR
+// 3. Nope, but postMessage is there so it should work via the Iframe.
+// 4. Nope, sorry.
+utils.isXHRCorsCapable = function() {
+ if (window.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest()) {
+ return 1;
+ }
+ // 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];
+ };
+ }
+ return window[MPrefix](window_id);
+};
+
+utils.attachMessage = function(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);
+ }
+};
+
+utils.detachMessage = function(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);
+ }
+};
+
+
+var on_unload = {};
+// Things registered after beforeunload are to be called immediately.
+var after_unload = false;
+
+var trigger_unload_callbacks = function() {
+ for(var ref in on_unload) {
+ on_unload[ref]();
+ delete on_unload[ref];
+ }
+};
+
+var unload_triggered = function() {
+ if(after_unload) return;
+ after_unload = true;
+ trigger_unload_callbacks();
+};
+
+// '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) {
+ utils.delay(trigger_unload_callbacks);
+ }
+ 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);
+ }
+ };
+ 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
+ };
+};
+
+/* jshint undef: false, newcap: false */
+utils.createHtmlfile = function (iframe_url, error_callback) {
+ var doc = new ActiveXObject('htmlfile');
+ var tref, unload_ref;
+ 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();
+ }
+ };
+ var onerror = function(r) {
+ if (doc) {
+ cleanup();
+ error_callback(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) {}
+ };
+
+ doc.open();
+ doc.write('<html><s' + 'cript>' +
+ 'document.domain="' + document.domain + '";' +
+ '</s' + 'cript></html>');
+ doc.close();
+ doc.parentWindow[utils.WPrefix] = window[utils.WPrefix];
+ var c = doc.createElement('div');
+ doc.body.appendChild(c);
+ iframe = doc.createElement('iframe');
+ c.appendChild(iframe);
+ iframe.src = iframe_url;
+ tref = setTimeout(function(){onerror('timeout');}, 15000);
+ unload_ref = utils.unload_add(cleanup);
+ return {
+ post: post,
+ cleanup: cleanup,
+ loaded: unattach
+ };
+};
+
+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);
+ }
+};
+
+module.exports = utils;
\ No newline at end of file
diff --git a/lib/xdr-polling.js b/lib/xdr-polling.js
new file mode 100644
index 0000000..8e29867
--- /dev/null
+++ b/lib/xdr-polling.js
@@ -0,0 +1,24 @@
+'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('./xdr-streaming');
+var utils = require('./utils');
+var XhrReceiver = require('./trans-receiver-xhr');
+
+function XdrPollingTransport(ri, trans_url) {
+ this.run(ri, trans_url, '/xhr', XhrReceiver, utils.XDRObject);
+}
+
+XdrPollingTransport.prototype = new AjaxBasedTransport();
+
+XdrPollingTransport.enabled = XdrStreamingTransport.enabled;
+XdrPollingTransport.roundTrips = 2; // preflight, ajax
+
+module.exports = XdrPollingTransport;
\ No newline at end of file
diff --git a/lib/xdr-streaming.js b/lib/xdr-streaming.js
new file mode 100644
index 0000000..7e386d8
--- /dev/null
+++ b/lib/xdr-streaming.js
@@ -0,0 +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 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, utils.XDRObject);
+}
+
+XdrStreamingTransport.prototype = new AjaxBasedTransport();
+
+XdrStreamingTransport.enabled = function() {
+ return !!window.XDomainRequest;
+};
+XdrStreamingTransport.roundTrips = 2; // preflight, ajax
+
+module.exports = XdrStreamingTransport;
\ No newline at end of file
diff --git a/lib/xdr.js b/lib/xdr.js
new file mode 100644
index 0000000..a75d250
--- /dev/null
+++ b/lib/xdr.js
@@ -0,0 +1,65 @@
+'use strict';
+
+var utils = require('./utils');
+var EventEmitter = require('./eventemitter');
+
+// References:
+// http://ajaxian.com/archives/100-line-ajax-wrapper
+// http://msdn.microsoft.com/en-us/library/cc288060(v=VS.85).aspx
+
+function XDRObject(method, url, payload) {
+ var that = this;
+ utils.delay(function(){that._start(method, url, payload);});
+}
+
+XDRObject.prototype = new EventEmitter(['chunk', 'finish']);
+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 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();
+ }
+};
+
+XDRObject.prototype._cleanup = function(abort) {
+ var that = this;
+ if (!that.xdr) return;
+ utils.unload_del(that.unload_ref);
+
+ 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;
+};
+
+XDRObject.prototype.close = function() {
+ var that = this;
+ that.nuke();
+ that._cleanup(true);
+};
+
+module.exports = XDRObject;
\ No newline at end of file
diff --git a/lib/xhr-cors.js b/lib/xhr-cors.js
new file mode 100644
index 0000000..c34dd77
--- /dev/null
+++ b/lib/xhr-cors.js
@@ -0,0 +1,12 @@
+'use strict';
+
+var AbstractXHRObject = require('./abstract-xhr');
+var utils = require('./utils');
+
+function XHRCorsObject() {
+ var that = this, args = arguments;
+ utils.delay(function(){that._start.apply(that, args);});
+}
+XHRCorsObject.prototype = new AbstractXHRObject();
+
+module.exports = XHRCorsObject;
\ No newline at end of file
diff --git a/lib/xhr-local.js b/lib/xhr-local.js
new file mode 100644
index 0000000..a729a9b
--- /dev/null
+++ b/lib/xhr-local.js
@@ -0,0 +1,16 @@
+'use strict';
+
+var utils = require('./utils');
+var AbstractXHRObject = require('./abstract-xhr');
+
+function XHRLocalObject(method, url, payload) {
+ var that = this;
+ utils.delay(function(){
+ that._start(method, url, payload, {
+ no_credentials: true
+ });
+ });
+}
+XHRLocalObject.prototype = new AbstractXHRObject();
+
+module.exports = XHRLocalObject;
\ No newline at end of file
diff --git a/lib/xhr-polling.js b/lib/xhr-polling.js
new file mode 100644
index 0000000..b57ac07
--- /dev/null
+++ b/lib/xhr-polling.js
@@ -0,0 +1,25 @@
+'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 XhrStreamingTransport = require('./xhr-streaming');
+var XhrReceiver = require('./trans-receiver-xhr');
+var utils = require('./utils');
+
+
+function XhrPollingTransport(ri, trans_url) {
+ this.run(ri, trans_url, '/xhr', XhrReceiver, utils.XHRCorsObject);
+}
+
+XhrPollingTransport.prototype = new AjaxBasedTransport();
+
+XhrPollingTransport.enabled = XhrStreamingTransport.enabled;
+XhrPollingTransport.roundTrips = 2; // preflight, ajax
+
+module.exports = XhrPollingTransport;
\ No newline at end of file
diff --git a/lib/xhr-streaming.js b/lib/xhr-streaming.js
new file mode 100644
index 0000000..0a98429
--- /dev/null
+++ b/lib/xhr-streaming.js
@@ -0,0 +1,38 @@
+'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 utils = require('./utils');
+
+function XhrStreamingTransport(ri, trans_url) {
+ this.run(ri, trans_url, '/xhr_streaming', XhrReceiver, utils.XHRCorsObject);
+}
+
+XhrStreamingTransport.prototype = new AjaxBasedTransport();
+
+XhrStreamingTransport.enabled = function() {
+ // Support for CORS Ajax aka Ajax2? Opera 12 claims CORS but
+ // doesn't do streaming.
+ try {
+ return (window.XMLHttpRequest &&
+ 'withCredentials' in new XMLHttpRequest() &&
+ (!/opera/i.test(navigator.userAgent)));
+ }
+ catch (ignored) {
+ return false;
+ }
+};
+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;
+
+module.exports = XhrStreamingTransport;
\ No newline at end of file
diff --git a/package.json b/package.json
index c999ebc..883565e 100644
--- a/package.json
+++ b/package.json
@@ -12,13 +12,19 @@
"email": "bkahle at gmail.com"
}
],
+ "dependencies": {
+ "json3": "^3.3.1"
+ },
"devDependencies": {
+ "browserify": "^4.1.5",
"coffee-script": "1.2.x",
+ "gulp": "^3.6.2",
+ "mocha": "^1.18.2",
"node-static": "0.5.9",
"optimist": "0.3.5",
"sockjs": "^0.3.8",
"uglify-js": "1.2.5",
- "mocha": "^1.18.2",
+ "vinyl-source-stream": "^0.1.1",
"zuul": "^1.6.4"
},
"homepage": "http://sockjs.org",
@@ -36,4 +42,4 @@
"test": "./node_modules/zuul/bin/zuul -- tests/html/lib/unittests.js tests/html/lib/domtests.js tests/html/lib/endtoendtests.js tests/html/lib/protocols.js",
"test_local": "./node_modules/zuul/bin/zuul --local 9090 --ui qunit -- tests/html/lib/unittests.js tests/html/lib/domtests.js tests/html/lib/endtoendtests.js tests/html/lib/protocols.js"
}
-}
+}
\ 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