From 751a73279e8b8f4d66aa3a71ba7b3593d49b14a9 Mon Sep 17 00:00:00 2001 From: Tom Hunkapiller Date: Tue, 6 Mar 2018 02:11:49 -0600 Subject: [PATCH 1/5] add test demonstrating the issue in firefox; this fails for me about half the time --- test/connection.js | 14 ++++++++++++-- test/support/public/worker.js | 6 ++++++ test/support/server.js | 3 +++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/test/connection.js b/test/connection.js index 128224c4d..ad9fe4dae 100644 --- a/test/connection.js +++ b/test/connection.js @@ -62,9 +62,19 @@ describe('connection', function () { if (global.Worker) { it('should work in a worker', function (done) { var worker = new Worker('/test/support/worker.js'); + var msg = 0; + var utf8yay = 'пойду сать всем мпокойной ночи'; worker.onmessage = function (e) { - expect(e.data); - done(); + if (msg === 0) { + msg++; + expect(e.data).to.be('hi'); + } else if (msg < 10) { + msg++; + expect(e.data).to.be(utf8yay); + } else { + expect(e.data).to.be(utf8yay); + done(); + } }; }); } diff --git a/test/support/public/worker.js b/test/support/public/worker.js index c11e57b04..286d5b9a1 100644 --- a/test/support/public/worker.js +++ b/test/support/public/worker.js @@ -3,6 +3,12 @@ importScripts('/test/support/engine.io.js'); var socket = new eio.Socket(); + +var count = 0; socket.on('message', function (msg) { + if (count < 10) { + count++ + socket.send('give utf8'); + } postMessage(msg); }); diff --git a/test/support/server.js b/test/support/server.js index 06ba29800..156e6d670 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -32,6 +32,9 @@ server.on('connection', function (socket) { } socket.send(abv); return; + } else if (data === 'give utf8') { + socket.send("пойду сать всем мпокойной ночи"); + return; } socket.send(data); From 4cf841fff42407efd48824013fea5fd6c9896776 Mon Sep 17 00:00:00 2001 From: Tom Hunkapiller Date: Wed, 7 Mar 2018 11:31:42 -0600 Subject: [PATCH 2/5] add test for binary data in worker --- test/connection.js | 18 +++++++++++++----- test/support/public/worker.js | 4 +++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/test/connection.js b/test/connection.js index ad9fe4dae..00ee6c260 100644 --- a/test/connection.js +++ b/test/connection.js @@ -65,17 +65,25 @@ describe('connection', function () { var msg = 0; var utf8yay = 'пойду сать всем мпокойной ночи'; worker.onmessage = function (e) { - if (msg === 0) { - msg++; + msg++; + if (msg === 1) { expect(e.data).to.be('hi'); - } else if (msg < 10) { - msg++; + } else if (msg < 11) { expect(e.data).to.be(utf8yay); + } else if (msg < 20) { + testBinary(e.data); } else { - expect(e.data).to.be(utf8yay); + testBinary(e.data); done(); } }; + + function testBinary(data) { + var byteArray = new Uint8Array(data); + for (var i = 0; i < byteArray.byteLength; i++) { + expect(byteArray[i]).to.be(i); + } + } }); } diff --git a/test/support/public/worker.js b/test/support/public/worker.js index 286d5b9a1..157446bb0 100644 --- a/test/support/public/worker.js +++ b/test/support/public/worker.js @@ -6,9 +6,11 @@ var socket = new eio.Socket(); var count = 0; socket.on('message', function (msg) { + count++ if (count < 10) { - count++ socket.send('give utf8'); + } else if(count < 20) { + socket.send('give binary'); } postMessage(msg); }); From 2ff3a5431ae7b6dffa72bde26cd984707c48d635 Mon Sep 17 00:00:00 2001 From: Tom Hunkapiller Date: Wed, 7 Mar 2018 11:32:55 -0600 Subject: [PATCH 3/5] fix #595 by delaying setting xhr.responseType=arraybuffer until xhr recieved headers state --- lib/transports/polling-xhr.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/transports/polling-xhr.js b/lib/transports/polling-xhr.js index c6f409200..05f307ba7 100755 --- a/lib/transports/polling-xhr.js +++ b/lib/transports/polling-xhr.js @@ -220,9 +220,6 @@ Request.prototype.create = function () { xhr.setRequestHeader('Accept', '*/*'); } catch (e) {} - if (this.supportsBinary) { - xhr.responseType = 'arraybuffer'; - } // ie6 check if ('withCredentials' in xhr) { @@ -245,8 +242,8 @@ Request.prototype.create = function () { if (xhr.readyState === 2) { try { var contentType = xhr.getResponseHeader('Content-Type'); - if (contentType !== 'application/octet-stream') { - xhr.responseType = 'text'; + if (self.supportsBinary && contentType === 'application/octet-stream') { + xhr.responseType = 'arraybuffer'; } } catch (e) {} } From a3125aaa8b0cee002fbf4321230c59cb6556ffe4 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Thu, 8 Mar 2018 12:33:50 +0100 Subject: [PATCH 4/5] fix style --- lib/transports/polling-xhr.js | 1 - test/connection.js | 2 +- test/support/public/worker.js | 4 ++-- test/support/server.js | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/transports/polling-xhr.js b/lib/transports/polling-xhr.js index 05f307ba7..ab0859d67 100755 --- a/lib/transports/polling-xhr.js +++ b/lib/transports/polling-xhr.js @@ -220,7 +220,6 @@ Request.prototype.create = function () { xhr.setRequestHeader('Accept', '*/*'); } catch (e) {} - // ie6 check if ('withCredentials' in xhr) { xhr.withCredentials = true; diff --git a/test/connection.js b/test/connection.js index 00ee6c260..5b4d5a5d1 100644 --- a/test/connection.js +++ b/test/connection.js @@ -78,7 +78,7 @@ describe('connection', function () { } }; - function testBinary(data) { + function testBinary (data) { var byteArray = new Uint8Array(data); for (var i = 0; i < byteArray.byteLength; i++) { expect(byteArray[i]).to.be(i); diff --git a/test/support/public/worker.js b/test/support/public/worker.js index 157446bb0..4a46b570f 100644 --- a/test/support/public/worker.js +++ b/test/support/public/worker.js @@ -6,10 +6,10 @@ var socket = new eio.Socket(); var count = 0; socket.on('message', function (msg) { - count++ + count++; if (count < 10) { socket.send('give utf8'); - } else if(count < 20) { + } else if (count < 20) { socket.send('give binary'); } postMessage(msg); diff --git a/test/support/server.js b/test/support/server.js index 156e6d670..ef9b45f9f 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -33,7 +33,7 @@ server.on('connection', function (socket) { socket.send(abv); return; } else if (data === 'give utf8') { - socket.send("пойду сать всем мпокойной ночи"); + socket.send('пойду сать всем мпокойной ночи'); return; } From 84d1da062b1ba90e4e7709e2ae3003b7419e881f Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Fri, 9 Mar 2018 12:39:49 +0100 Subject: [PATCH 5/5] remove useless case --- lib/transports/polling-xhr.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/transports/polling-xhr.js b/lib/transports/polling-xhr.js index ab0859d67..53f5fcd8d 100755 --- a/lib/transports/polling-xhr.js +++ b/lib/transports/polling-xhr.js @@ -355,8 +355,6 @@ Request.prototype.onLoad = function () { } catch (e) {} if (contentType === 'application/octet-stream') { data = this.xhr.response || this.xhr.responseText; - } else if (this.xhr.responseType === 'arraybuffer') { - data = String.fromCharCode.apply(null, new Uint8Array(this.xhr.response)); } else { data = this.xhr.responseText; }