Skip to content

Commit 4349b64

Browse files
devoidfurydarrachequesne
authored andcommitted
[fix] Fix UTF-8 encoding in Firefox WebWorker (#596)
1 parent bed6da6 commit 4349b64

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

lib/transports/polling-xhr.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,6 @@ Request.prototype.create = function () {
220220
xhr.setRequestHeader('Accept', '*/*');
221221
} catch (e) {}
222222

223-
if (this.supportsBinary) {
224-
xhr.responseType = 'arraybuffer';
225-
}
226-
227223
// ie6 check
228224
if ('withCredentials' in xhr) {
229225
xhr.withCredentials = true;
@@ -245,8 +241,8 @@ Request.prototype.create = function () {
245241
if (xhr.readyState === 2) {
246242
try {
247243
var contentType = xhr.getResponseHeader('Content-Type');
248-
if (contentType !== 'application/octet-stream') {
249-
xhr.responseType = 'text';
244+
if (self.supportsBinary && contentType === 'application/octet-stream') {
245+
xhr.responseType = 'arraybuffer';
250246
}
251247
} catch (e) {}
252248
}
@@ -359,8 +355,6 @@ Request.prototype.onLoad = function () {
359355
} catch (e) {}
360356
if (contentType === 'application/octet-stream') {
361357
data = this.xhr.response || this.xhr.responseText;
362-
} else if (this.xhr.responseType === 'arraybuffer') {
363-
data = String.fromCharCode.apply(null, new Uint8Array(this.xhr.response));
364358
} else {
365359
data = this.xhr.responseText;
366360
}

test/connection.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,28 @@ describe('connection', function () {
6262
if (global.Worker) {
6363
it('should work in a worker', function (done) {
6464
var worker = new Worker('/test/support/worker.js');
65+
var msg = 0;
66+
var utf8yay = 'пойду сать всем мпокойной ночи';
6567
worker.onmessage = function (e) {
66-
expect(e.data);
67-
done();
68+
msg++;
69+
if (msg === 1) {
70+
expect(e.data).to.be('hi');
71+
} else if (msg < 11) {
72+
expect(e.data).to.be(utf8yay);
73+
} else if (msg < 20) {
74+
testBinary(e.data);
75+
} else {
76+
testBinary(e.data);
77+
done();
78+
}
6879
};
80+
81+
function testBinary (data) {
82+
var byteArray = new Uint8Array(data);
83+
for (var i = 0; i < byteArray.byteLength; i++) {
84+
expect(byteArray[i]).to.be(i);
85+
}
86+
}
6987
});
7088
}
7189

test/support/public/worker.js

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
importScripts('/test/support/engine.io.js');
44

55
var socket = new eio.Socket();
6+
7+
var count = 0;
68
socket.on('message', function (msg) {
9+
count++;
10+
if (count < 10) {
11+
socket.send('give utf8');
12+
} else if (count < 20) {
13+
socket.send('give binary');
14+
}
715
postMessage(msg);
816
});

test/support/server.js

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ server.on('connection', function (socket) {
3232
}
3333
socket.send(abv);
3434
return;
35+
} else if (data === 'give utf8') {
36+
socket.send('пойду сать всем мпокойной ночи');
37+
return;
3538
}
3639

3740
socket.send(data);

0 commit comments

Comments
 (0)