Skip to content

Commit

Permalink
[fix] Fix UTF-8 encoding in Firefox WebWorker (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
devoidfury authored and darrachequesne committed Mar 9, 2018
1 parent bed6da6 commit 4349b64
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
10 changes: 2 additions & 8 deletions lib/transports/polling-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ Request.prototype.create = function () {
xhr.setRequestHeader('Accept', '*/*');
} catch (e) {}

if (this.supportsBinary) {
xhr.responseType = 'arraybuffer';
}

// ie6 check
if ('withCredentials' in xhr) {
xhr.withCredentials = true;
Expand All @@ -245,8 +241,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) {}
}
Expand Down Expand Up @@ -359,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;
}
Expand Down
22 changes: 20 additions & 2 deletions test/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,28 @@ 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();
msg++;
if (msg === 1) {
expect(e.data).to.be('hi');
} else if (msg < 11) {
expect(e.data).to.be(utf8yay);
} else if (msg < 20) {
testBinary(e.data);
} else {
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);
}
}
});
}

Expand Down
8 changes: 8 additions & 0 deletions test/support/public/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
importScripts('/test/support/engine.io.js');

var socket = new eio.Socket();

var count = 0;
socket.on('message', function (msg) {
count++;
if (count < 10) {
socket.send('give utf8');
} else if (count < 20) {
socket.send('give binary');
}
postMessage(msg);
});
3 changes: 3 additions & 0 deletions test/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ server.on('connection', function (socket) {
}
socket.send(abv);
return;
} else if (data === 'give utf8') {
socket.send('пойду сать всем мпокойной ночи');
return;
}

socket.send(data);
Expand Down

0 comments on commit 4349b64

Please sign in to comment.