Skip to content

Commit

Permalink
Don't UTF-8 encode packets unless asked.
Browse files Browse the repository at this point in the history
Still done in payload encoding for polling, but no need with WebSockets since it deals with UTF-8 itself.
  • Loading branch information
Tony Kovanen committed Jul 16, 2014
1 parent 952b466 commit 95840ca
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
35 changes: 21 additions & 14 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ var Blob = require('blob');
* @api private
*/

exports.encodePacket = function (packet, supportsBinary, callback) {
if (typeof supportsBinary == 'function') {
exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
if ('function' == typeof supportsBinary) {
callback = supportsBinary;
supportsBinary = false;
}

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

var data = (packet.data === undefined)
? undefined
: packet.data.buffer || packet.data;
Expand All @@ -88,7 +93,7 @@ exports.encodePacket = function (packet, supportsBinary, callback) {

// data fragment is optional
if (undefined !== packet.data) {
encoded += utf8.encode(String(packet.data));
encoded += utf8encode ? utf8.encode(String(packet.data)) : String(packet.data);
}

return callback('' + encoded);
Expand Down Expand Up @@ -124,7 +129,7 @@ function encodeBlobAsArrayBuffer(packet, supportsBinary, callback) {
var fr = new FileReader();
fr.onload = function() {
packet.data = fr.result;
exports.encodePacket(packet, supportsBinary, callback);
exports.encodePacket(packet, supportsBinary, true, callback);
};
return fr.readAsArrayBuffer(packet.data);
}
Expand Down Expand Up @@ -186,17 +191,19 @@ exports.encodeBase64Packet = function(packet, callback) {
* @api private
*/

exports.decodePacket = function (data, binaryType) {
exports.decodePacket = function (data, binaryType, utf8decode) {
// String data
if (typeof data == 'string' || data === undefined) {
if (data.charAt(0) == 'b') {
return exports.decodeBase64Packet(data.substr(1), binaryType);
}

try {
data = utf8.decode(data);
} catch (e) {
return err;
if (utf8decode) {
try {
data = utf8.decode(data);
} catch (e) {
return err;
}
}
var type = data.charAt(0);

Expand Down Expand Up @@ -281,7 +288,7 @@ exports.encodePayload = function (packets, supportsBinary, callback) {
}

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

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

if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
Expand Down Expand Up @@ -401,7 +408,7 @@ exports.encodePayloadAsArrayBuffer = function(packets, callback) {
}

function encodeOne(packet, doneCallback) {
exports.encodePacket(packet, true, function(data) {
exports.encodePacket(packet, true, true, function(data) {
return doneCallback(null, data);
});
}
Expand Down Expand Up @@ -459,7 +466,7 @@ exports.encodePayloadAsArrayBuffer = function(packets, callback) {

exports.encodePayloadAsBlob = function(packets, callback) {
function encodeOne(packet, doneCallback) {
exports.encodePacket(packet, true, function(encoded) {
exports.encodePacket(packet, true, true, function(encoded) {
var binaryIdentifier = new Uint8Array(1);
binaryIdentifier[0] = 1;
if (typeof encoded === 'string') {
Expand Down Expand Up @@ -554,6 +561,6 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {

var total = buffers.length;
buffers.forEach(function(buffer, i) {
callback(exports.decodePacket(buffer, binaryType), i, total);
callback(exports.decodePacket(buffer, binaryType, true), i, total);
});
};
31 changes: 19 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ var err = { type: 'error', data: 'parser error' };
* @api private
*/

exports.encodePacket = function (packet, supportsBinary, callback) {
if (typeof supportsBinary == 'function') {
exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
if ('function' == typeof supportsBinary) {
callback = supportsBinary;
supportsBinary = null;
}

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

var data = (packet.data === undefined)
? undefined
: packet.data.buffer || packet.data;
Expand All @@ -70,7 +75,7 @@ exports.encodePacket = function (packet, supportsBinary, callback) {

// data fragment is optional
if (undefined !== packet.data) {
encoded += utf8.encode(String(packet.data));
encoded += utf8encode ? utf8.encode(String(packet.data)) : String(packet.data);
}

return callback('' + encoded);
Expand Down Expand Up @@ -139,18 +144,20 @@ exports.encodeBase64Packet = function(packet, callback){
* @api private
*/

exports.decodePacket = function (data, binaryType) {
exports.decodePacket = function (data, binaryType, utf8decode) {
// String data
if (typeof data == 'string' || data === undefined) {
if (data.charAt(0) == 'b') {
return exports.decodeBase64Packet(data.substr(1), binaryType);
}

var type = data.charAt(0);
try {
data = utf8.decode(data);
} catch (e) {
return err;
if (utf8decode) {
try {
data = utf8.decode(data);
} catch (e) {
return err;
}
}

if (Number(type) != type || !packetslist[type]) {
Expand Down Expand Up @@ -232,7 +239,7 @@ exports.encodePayload = function (packets, supportsBinary, callback) {
}

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

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

if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
Expand Down Expand Up @@ -382,7 +389,7 @@ exports.encodePayloadAsBinary = function (packets, callback) {
}

function encodeOne(p, doneCallback) {
exports.encodePacket(p, true, function(packet) {
exports.encodePacket(p, true, true, function(packet) {

if (typeof packet === 'string') {
var encodingLength = '' + packet.length;
Expand Down Expand Up @@ -455,6 +462,6 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {

var total = buffers.length;
buffers.forEach(function(buffer, i) {
callback(exports.decodePacket(buffer, binaryType), i, total);
callback(exports.decodePacket(buffer, binaryType, true), i, total);
});
};
2 changes: 1 addition & 1 deletion test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ module.exports = function(parser) {
});

it('should disallow invalid utf8', function () {
expect(decode('4\uffff')).to.eql(err);
expect(decode('4\uffff', false, true)).to.eql(err);
});
});
});
Expand Down

0 comments on commit 95840ca

Please sign in to comment.