[Pkg-javascript-commits] [sockjs-client] 73/350: Fix up options checking. Document server option
tonnerre at ancient-solutions.com
tonnerre at ancient-solutions.com
Fri Aug 5 01:03:43 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 96e4d2efe78d3ffc0092333aaea4276e7d64bc0b
Author: Bryce Kahle <bkahle at gmail.com>
Date: Fri Jun 6 19:25:10 2014 -0400
Fix up options checking. Document server option
---
Changelog | 1 +
README.md | 4 +++
lib/info-receiver-iframe.js | 1 -
lib/sockjs.js | 75 ++++++++++++++++++---------------------------
lib/trans-iframe.js | 2 +-
lib/utils.js | 7 ++---
6 files changed, 38 insertions(+), 52 deletions(-)
diff --git a/Changelog b/Changelog
index 59f5b57..36d79b5 100644
--- a/Changelog
+++ b/Changelog
@@ -1,6 +1,7 @@
1.0.0 (unreleased)
=====
+ * Relax requirements when using same origin XHR - #80
* Upgrade to JSON3 from JSON2 - #123
* Package library with browserify supporting the UMD pattern
* Move tests to JavaScript
diff --git a/README.md b/README.md
index 2c09610..2aa7cb1 100644
--- a/README.md
+++ b/README.md
@@ -127,6 +127,10 @@ Where `options` is a hash which can contain:
Development mode. Currently setting it disables caching of the
'iframe.html'.
+ * **server (string)**
+
+ String to append to url for actual data connection. Defaults to random 4 digit number.
+
* **protocols_whitelist (list of strings)**
Sometimes it is useful to disable some fallback protocols. This
diff --git a/lib/info-receiver-iframe.js b/lib/info-receiver-iframe.js
index ad32ffd..1c64e56 100644
--- a/lib/info-receiver-iframe.js
+++ b/lib/info-receiver-iframe.js
@@ -29,7 +29,6 @@ function InfoReceiverIframe(base_url) {
ifr = null;
};
var mock_ri = {
- _options: {},
_didClose: fun,
_didMessage: fun
};
diff --git a/lib/sockjs.js b/lib/sockjs.js
index 0b54013..403e622 100644
--- a/lib/sockjs.js
+++ b/lib/sockjs.js
@@ -21,39 +21,29 @@ var XDRObject = require('./xdr');
var XDRPolling = require('./trans-xdr-polling');
var IframeTransport = require('./trans-iframe');
-function SockJS(url, dep_protocols_whitelist, options) {
+function SockJS(url, _reserved, options) {
if (!(this instanceof SockJS)) {
// makes `new` optional
- return new SockJS(url, dep_protocols_whitelist, options);
+ return new SockJS(url, _reserved, options);
}
- var that = this, protocols_whitelist;
- that._options = {devel: false, debug: false, protocols_whitelist: [],
- info: undefined, rtt: undefined};
- if (options) {
- utils.objectExtend(that._options, options);
- }
+ var that = this;
that._base_url = utils.amendUrl(url);
- that._server = that._options.server || utils.random_number_string(1000);
- if (that._options.protocols_whitelist &&
- that._options.protocols_whitelist.length) {
- protocols_whitelist = that._options.protocols_whitelist;
+
+ that._server = 'server' in options ? options.server : utils.random_number_string(1000);
+ that._devel = 'devel' in options ? options.devel : false;
+ that._debug = 'debug' in options ? options.debug : false;
+
+ // only allow whitelist if it is valid
+ if (options.protocols_whitelist && options.protocols_whitelist.length) {
+ that._protocols_whitelist = utils.isArray(options.protocols_whitelist)
+ ? options.protocols_whitelist
+ : [options.protocols_whitelist]
+ ;
} else {
- // Deprecated API
- if (typeof dep_protocols_whitelist === 'string' &&
- dep_protocols_whitelist.length > 0) {
- protocols_whitelist = [dep_protocols_whitelist];
- } else if (utils.isArray(dep_protocols_whitelist)) {
- protocols_whitelist = dep_protocols_whitelist;
- } else {
- protocols_whitelist = null;
- }
- if (protocols_whitelist) {
- that._debug('Deprecated API: Use "protocols_whitelist" option ' +
- 'instead of supplying protocol list as a second ' +
- 'parameter to SockJS constructor.');
- }
+ that._protocols_whitelist = [];
}
+
that._protocols = [];
that.protocol = null;
that.readyState = SockJS.CONNECTING;
@@ -61,17 +51,10 @@ function SockJS(url, dep_protocols_whitelist, options) {
that._ir.onfinish = function(info, rtt) {
that._ir = null;
if (info) {
- if (that._options.info) {
- // Override if user supplies the option
- info = utils.objectExtend(info, that._options.info);
- }
- if (that._options.rtt) {
- rtt = that._options.rtt;
- }
- that._applyInfo(info, rtt, protocols_whitelist);
+ that._applyInfo(info, rtt, that._protocols_whitelist);
that._didClose();
} else {
- that._didClose(1002, 'Can\'t connect to server', true);
+ that._didClose(1002, "Can't connect to server", true);
}
};
}
@@ -86,8 +69,8 @@ SockJS.prototype.OPEN = SockJS.OPEN = 1;
SockJS.prototype.CLOSING = SockJS.CLOSING = 2;
SockJS.prototype.CLOSED = SockJS.CLOSED = 3;
-SockJS.prototype._debug = function() {
- if (this._options.debug)
+SockJS.prototype._log = function() {
+ if (this._debug)
utils.log.apply(utils, arguments);
};
@@ -190,7 +173,7 @@ SockJS.prototype._didMessage = function(data) {
SockJS.prototype._try_next_protocol = function(close_event) {
var that = this;
if (that.protocol) {
- that._debug('Closed transport:', that.protocol, ''+close_event);
+ that._log('Closed transport:', that.protocol, ''+close_event);
that.protocol = null;
}
if (that._transport_tref) {
@@ -231,16 +214,16 @@ SockJS.prototype._try_next_protocol = function(close_event) {
if (!SockJS[protocol] ||
!SockJS[protocol].enabled(that._base_url)) {
- that._debug('Skipping transport:', protocol);
+ that._log('Skipping transport:', protocol);
} else {
var roundTrips = SockJS[protocol].roundTrips || 1;
- var to = ((that._options.rto || 0) * roundTrips) || 5000;
+ var to = ((that._rto || 0) * roundTrips) || 5000;
that._transport_tref = utils.delay(to, timeoutFunction);
var connid = utils.random_string(8);
var trans_url = that._base_url + '/' + that._server + '/' + connid;
- that._debug('Opening transport:', protocol, ' url:'+trans_url,
- ' RTO:'+that._options.rto);
+ that._log('Opening transport:', protocol, ' url:'+trans_url,
+ ' RTO:'+that._rto);
that._transport = new SockJS[protocol](that, trans_url,
that._base_url);
return true;
@@ -273,10 +256,10 @@ SockJS.prototype.send = function(data) {
SockJS.prototype._applyInfo = function(info, rtt, protocols_whitelist) {
var that = this;
- that._options.info = info;
- that._options.rtt = rtt;
- that._options.rto = utils.countRTO(rtt);
- that._options.info.null_origin = !document.domain;
+ that._rtt = rtt;
+ that._rto = utils.countRTO(rtt);
+
+ 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)
diff --git a/lib/trans-iframe.js b/lib/trans-iframe.js
index 1925eea..5f8b446 100644
--- a/lib/trans-iframe.js
+++ b/lib/trans-iframe.js
@@ -28,7 +28,7 @@ IframeTransport.prototype.i_constructor = function(ri, trans_url, base_url) {
that.trans_url = trans_url;
var iframe_url = base_url + '/iframe.html';
- if (that.ri._options.devel) {
+ if (that.ri._devel) {
iframe_url += '?t=' + (+new Date());
}
that.window_id = utils.random_string(8);
diff --git a/lib/utils.js b/lib/utils.js
index 89b67e4..6c015a2 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -204,9 +204,8 @@ 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;
+utils.isArray = Array.isArray || function(arg) {
+ return Object.prototype.toString.call(arg) === '[object Array]';
};
utils.delay = function(t, fun) {
@@ -330,7 +329,7 @@ utils.probeProtocols = function(url) {
utils.detectProtocols = function(probed, protocols_whitelist, info) {
var pe = {},
protocols = [];
- if (!protocols_whitelist) protocols_whitelist = _all_protocols;
+ if (!protocols_whitelist || !protocols_whitelist.length) protocols_whitelist = _all_protocols;
for(var i=0; i<protocols_whitelist.length; i++) {
var protocol = protocols_whitelist[i];
pe[protocol] = probed[protocol];
--
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