From 2f729e07b4836b0ad4c660e32c5f805974e435da Mon Sep 17 00:00:00 2001 From: Kevin Roark Date: Thu, 6 Mar 2014 16:52:08 -0500 Subject: [PATCH 1/2] Encoding ack packets as binary So that arbitrary data can be passed back and forth in ack callbacks. --- index.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index f24ed491..cb61588e 100644 --- a/index.js +++ b/index.js @@ -80,6 +80,16 @@ exports.ERROR = 4; exports.BINARY_EVENT = 5; +/** + * Set of types to encode and decode in binary-style. + * + * @api private + */ + +var binaryEncodeTypes = {}; +binaryEncodeTypes[exports.BINARY_EVENT] = true; +binaryEncodeTypes[exports.ACK] = true; + exports.Encoder = Encoder /** @@ -102,7 +112,7 @@ function Encoder() {}; Encoder.prototype.encode = function(obj, callback){ debug('encoding packet %j', obj); - if (obj.type == exports.BINARY_EVENT) { + if (obj.type in binaryEncodeTypes) { encodeAsBinary(obj, callback); } else { @@ -127,7 +137,7 @@ function encodeAsString(obj) { str += obj.type; // attachments if we have them - if (exports.BINARY_EVENT == obj.type) { + if (obj.type in binaryEncodeTypes) { str += obj.attachments; str += '-'; } @@ -213,8 +223,13 @@ Decoder.prototype.add = function(obj) { var packet; if ('string' == typeof obj) { packet = decodeString(obj); - if (packet.type == exports.BINARY_EVENT) { // binary packet's json + if (packet.type in binaryEncodeTypes) { // binary packet's json this.reconstructor = new BinaryReconstructor(packet); + + // no attachments, labeled binary but no binary data to follow + if (this.reconstructor.reconPack.attachments == 0) { + this.emit('decoded', packet); + } } else { // non-binary full packet this.emit('decoded', packet); } @@ -254,7 +269,7 @@ function decodeString(str) { if (null == exports.types[p.type]) return error(); // look up attachments if type binary - if (exports.BINARY_EVENT == p.type) { + if (p.type in binaryEncodeTypes) { p.attachments = ''; while (str.charAt(++i) != '-') { p.attachments += str.charAt(i); From c8cce3e8c0d9c8134450112365a4b96b9fc3eeb6 Mon Sep 17 00:00:00 2001 From: Kevin Roark Date: Thu, 6 Mar 2014 17:24:58 -0500 Subject: [PATCH 2/2] removed the 'in' slowness --- index.js | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index cb61588e..8ae08c4d 100644 --- a/index.js +++ b/index.js @@ -80,16 +80,6 @@ exports.ERROR = 4; exports.BINARY_EVENT = 5; -/** - * Set of types to encode and decode in binary-style. - * - * @api private - */ - -var binaryEncodeTypes = {}; -binaryEncodeTypes[exports.BINARY_EVENT] = true; -binaryEncodeTypes[exports.ACK] = true; - exports.Encoder = Encoder /** @@ -112,7 +102,7 @@ function Encoder() {}; Encoder.prototype.encode = function(obj, callback){ debug('encoding packet %j', obj); - if (obj.type in binaryEncodeTypes) { + if (exports.BINARY_EVENT == obj.type || exports.ACK == obj.type) { encodeAsBinary(obj, callback); } else { @@ -137,7 +127,7 @@ function encodeAsString(obj) { str += obj.type; // attachments if we have them - if (obj.type in binaryEncodeTypes) { + if (exports.BINARY_EVENT == obj.type || exports.ACK == obj.type) { str += obj.attachments; str += '-'; } @@ -223,7 +213,7 @@ Decoder.prototype.add = function(obj) { var packet; if ('string' == typeof obj) { packet = decodeString(obj); - if (packet.type in binaryEncodeTypes) { // binary packet's json + if (exports.BINARY_EVENT == packet.type || exports.ACK == packet.type) { // binary packet's json this.reconstructor = new BinaryReconstructor(packet); // no attachments, labeled binary but no binary data to follow @@ -269,7 +259,7 @@ function decodeString(str) { if (null == exports.types[p.type]) return error(); // look up attachments if type binary - if (p.type in binaryEncodeTypes) { + if (exports.BINARY_EVENT == p.type || exports.ACK == p.type) { p.attachments = ''; while (str.charAt(++i) != '-') { p.attachments += str.charAt(i);