Possibility to disable JSONP. Emitting an error if JSONP is the only available transport.

This commit is contained in:
Tony Kovanen
2014-06-23 01:31:17 +03:00
parent c090f20211
commit 269bdfff23
3 changed files with 44 additions and 2 deletions

View File

@@ -71,6 +71,7 @@ function Socket(uri, opts){
this.upgrade = false !== opts.upgrade;
this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/';
this.forceJSONP = !!opts.forceJSONP;
this.noJSONP = !!opts.noJSONP;
this.forceBase64 = !!opts.forceBase64;
this.timestampParam = opts.timestampParam || 't';
this.timestampRequests = opts.timestampRequests;
@@ -140,6 +141,7 @@ Socket.prototype.createTransport = function (name) {
path: this.path,
query: query,
forceJSONP: this.forceJSONP,
noJSONP: this.noJSONP,
forceBase64: this.forceBase64,
timestampRequests: this.timestampRequests,
timestampParam: this.timestampParam,
@@ -169,11 +171,26 @@ Socket.prototype.open = function () {
var transport;
if (this.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf('websocket') != -1) {
transport = 'websocket';
} else if (0 == this.transports.length) {
// Emit error on next tick so it can be listened to
var self = this;
setTimeout(function() {
self.emit('error', 'No transports available');
}, 0);
return;
} else {
transport = this.transports[0];
}
this.readyState = 'opening';
var transport = this.createTransport(transport);
// If polling and JSONP disabled (constructor returns null)
if (!transport.name) {
this.transports.shift();
this.open();
return;
}
transport.open();
this.setTransport(transport);
};

View File

@@ -43,6 +43,6 @@ function polling(opts){
if ('open' in xhr && !opts.forceJSONP) {
return new XHR(opts);
} else {
return new JSONP(opts);
return opts.noJSONP ? null : new JSONP(opts);
}
}

View File

@@ -1,6 +1,12 @@
var expect = require('expect.js');
var eio = require('../');
var wsSupport = require('has-cors');
var uagent = navigator.userAgent;
var isOldSimulator = ~uagent.indexOf('iPhone OS 4') || ~uagent.indexOf('iPhone OS 5');
var isIE11 = !!navigator.userAgent.match(/Trident.*rv[ :]*11\./); // ws doesn't work at all in sauce labs
var isAndroid = navigator.userAgent.match(/Android/i);
describe('connection', function() {
this.timeout(20000);
@@ -55,7 +61,7 @@ describe('connection', function() {
}, 1200);
});
});
// no `Worker` on old IE
if (global.Worker) {
it('should work in a worker', function(done){
@@ -66,4 +72,23 @@ describe('connection', function() {
};
});
}
it('should not connect at all when JSONP forced and disabled', function(done) {
var socket = eio.Socket({ transports: ['polling'], forceJSONP: true, noJSONP: true });
socket.on('error', function(msg) {
expect(msg).to.be('No transports available');
done();
});
});
if (wsSupport && !isOldSimulator && !isAndroid && !isIE11) {
it('should connect with ws when JSONP forced and disabled', function(done) {
var socket = eio.Socket({ transports: ['polling', 'websocket'], forceJSONP: true, noJSONP: true });
socket.on('open', function() {
expect(socket.transport.name).to.be('websocket');
done();
});
});
}
});