Skip to content

Commit

Permalink
[fix] Enable to utf8-decode string payloads (#88)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
darrachequesne authored Mar 21, 2017
1 parent 6c59795 commit 278a7e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ function map(ary, each, done) {
* @api public
*/

exports.decodePayload = function (data, binaryType, callback) {
exports.decodePayload = function (data, binaryType, utf8decode, callback) {
if (typeof data !== 'string') {
return exports.decodePayloadAsBinary(data, binaryType, callback);
}
Expand All @@ -379,12 +379,24 @@ exports.decodePayload = function (data, binaryType, callback) {
binaryType = null;
}

if (typeof utf8decode === 'function') {
callback = utf8decode;
utf8decode = null;
}

var packet;
if (data === '') {
// parser error - ignoring payload
return callback(err, 0, 1);
}

if (utf8decode) {
data = tryDecode(data);
if (data === false) {
return callback(err, 0, 1);
}
}

var length = '', n, msg;

for (var i = 0, l = data.length; i < l; i++) {
Expand Down
14 changes: 13 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function map(ary, each, done) {
* @api public
*/

exports.decodePayload = function (data, binaryType, callback) {
exports.decodePayload = function (data, binaryType, utf8decode, callback) {
if (typeof data !== 'string') {
return exports.decodePayloadAsBinary(data, binaryType, callback);
}
Expand All @@ -275,11 +275,23 @@ exports.decodePayload = function (data, binaryType, callback) {
binaryType = null;
}

if (typeof utf8decode === 'function') {
callback = utf8decode;
utf8decode = null;
}

if (data === '') {
// parser error - ignoring payload
return callback(err, 0, 1);
}

if (utf8decode) {
data = tryDecode(data);
if (data === false) {
return callback(err, 0, 1);
}
}

var length = '', n, msg, packet;

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

0 comments on commit 278a7e4

Please sign in to comment.