From 95840ca6b6d393d9f9140b105405809094af643b Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Thu, 17 Jul 2014 00:45:48 +0300 Subject: [PATCH] Don't UTF-8 encode packets unless asked. Still done in payload encoding for polling, but no need with WebSockets since it deals with UTF-8 itself. --- lib/browser.js | 35 +++++++++++++++++++++-------------- lib/index.js | 31 +++++++++++++++++++------------ test/parser.js | 2 +- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/lib/browser.js b/lib/browser.js index d35a567..aa9d877 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -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; @@ -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); @@ -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); } @@ -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); @@ -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)); }); } @@ -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 @@ -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); }); } @@ -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') { @@ -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); }); }; diff --git a/lib/index.js b/lib/index.js index bdb1da1..bd7ea9a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; @@ -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); @@ -139,7 +144,7 @@ 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') { @@ -147,10 +152,12 @@ exports.decodePacket = function (data, 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]) { @@ -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)); }); } @@ -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 @@ -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; @@ -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); }); }; diff --git a/test/parser.js b/test/parser.js index 25b43fd..9dc0c37 100644 --- a/test/parser.js +++ b/test/parser.js @@ -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); }); }); });