Skip to content

Commit

Permalink
[fix] Fix double utf8 encoding for payloads (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne authored Dec 21, 2016
1 parent 339d367 commit 181acef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ exports.encodePayload = function (packets, supportsBinary, callback) {
}

function encodeOne(packet, doneCallback) {
exports.encodePacket(packet, !isBinary ? false : supportsBinary, true, function(message) {
exports.encodePacket(packet, !isBinary ? false : supportsBinary, false, function(message) {
doneCallback(null, setLengthHeader(message));
});
}
Expand Down Expand Up @@ -407,7 +407,7 @@ exports.decodePayload = function (data, binaryType, callback) {
}

if (msg.length) {
packet = exports.decodePacket(msg, binaryType, true);
packet = exports.decodePacket(msg, binaryType, false);

if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ exports.encodePayload = function (packets, supportsBinary, callback) {
}

function encodeOne(packet, doneCallback) {
exports.encodePacket(packet, supportsBinary, true, function(message) {
exports.encodePacket(packet, supportsBinary, false, function(message) {
doneCallback(null, setLengthHeader(message));
});
}
Expand Down Expand Up @@ -302,7 +302,7 @@ exports.decodePayload = function (data, binaryType, callback) {
}

if (msg.length) {
packet = exports.decodePacket(msg, binaryType, true);
packet = exports.decodePacket(msg, binaryType, false);

if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
Expand Down
13 changes: 6 additions & 7 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ module.exports = function(parser) {
});
});
});

it('should not utf8 encode when dealing with strings only', function() {
encPayload([{ type: 'message', data: '€€€' }, { type: 'message', data: 'α' }], function(data) {
expect(data).to.eql('4:4€€€2:4α');
});
});
});

describe('decoding error handling', function () {
Expand Down Expand Up @@ -255,13 +261,6 @@ module.exports = function(parser) {
});
});

it('should err on invalid utf8', function () {
decPayload('2:4\uffff', function (packet, index, total) {
var isLast = index + 1 == total;
expect(packet).to.eql(err);
expect(isLast).to.eql(true);
});
});
});
});
});
Expand Down

0 comments on commit 181acef

Please sign in to comment.