Skip to content

Commit 278a7e4

Browse files
[fix] Enable to utf8-decode string payloads (#88)
That will allow clients receiving the xhr payload with responseType = 'arraybuffer' to properly decode the message, which is not sent as binary by the backend anymore since 292c00c (#85).
1 parent 6c59795 commit 278a7e4

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/browser.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ function map(ary, each, done) {
369369
* @api public
370370
*/
371371

372-
exports.decodePayload = function (data, binaryType, callback) {
372+
exports.decodePayload = function (data, binaryType, utf8decode, callback) {
373373
if (typeof data !== 'string') {
374374
return exports.decodePayloadAsBinary(data, binaryType, callback);
375375
}
@@ -379,12 +379,24 @@ exports.decodePayload = function (data, binaryType, callback) {
379379
binaryType = null;
380380
}
381381

382+
if (typeof utf8decode === 'function') {
383+
callback = utf8decode;
384+
utf8decode = null;
385+
}
386+
382387
var packet;
383388
if (data === '') {
384389
// parser error - ignoring payload
385390
return callback(err, 0, 1);
386391
}
387392

393+
if (utf8decode) {
394+
data = tryDecode(data);
395+
if (data === false) {
396+
return callback(err, 0, 1);
397+
}
398+
}
399+
388400
var length = '', n, msg;
389401

390402
for (var i = 0, l = data.length; i < l; i++) {

lib/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function map(ary, each, done) {
265265
* @api public
266266
*/
267267

268-
exports.decodePayload = function (data, binaryType, callback) {
268+
exports.decodePayload = function (data, binaryType, utf8decode, callback) {
269269
if (typeof data !== 'string') {
270270
return exports.decodePayloadAsBinary(data, binaryType, callback);
271271
}
@@ -275,11 +275,23 @@ exports.decodePayload = function (data, binaryType, callback) {
275275
binaryType = null;
276276
}
277277

278+
if (typeof utf8decode === 'function') {
279+
callback = utf8decode;
280+
utf8decode = null;
281+
}
282+
278283
if (data === '') {
279284
// parser error - ignoring payload
280285
return callback(err, 0, 1);
281286
}
282287

288+
if (utf8decode) {
289+
data = tryDecode(data);
290+
if (data === false) {
291+
return callback(err, 0, 1);
292+
}
293+
}
294+
283295
var length = '', n, msg, packet;
284296

285297
for (var i = 0, l = data.length; i < l; i++) {

0 commit comments

Comments
 (0)