Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small optimisations #57

Merged
merged 2 commits into from
Dec 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 44 additions & 46 deletions binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,38 @@ var isBuf = require('./is-buffer');
* @api public
*/

exports.deconstructPacket = function(packet){
exports.deconstructPacket = function(packet) {
var buffers = [];
var packetData = packet.data;

function _deconstructPacket(data) {
if (!data) return data;

if (isBuf(data)) {
var placeholder = { _placeholder: true, num: buffers.length };
buffers.push(data);
return placeholder;
} else if (isArray(data)) {
var newData = new Array(data.length);
for (var i = 0; i < data.length; i++) {
newData[i] = _deconstructPacket(data[i]);
}
return newData;
} else if (typeof data === 'object' && !(data instanceof Date)) {
var newData = {};
for (var key in data) {
newData[key] = _deconstructPacket(data[key]);
}
return newData;
}
return data;
}

var pack = packet;
pack.data = _deconstructPacket(packetData);
pack.data = _deconstructPacket(packetData, buffers);
pack.attachments = buffers.length; // number of binary 'attachments'
return {packet: pack, buffers: buffers};
};

function _deconstructPacket(data, buffers) {
if (!data) return data;

if (isBuf(data)) {
var placeholder = { _placeholder: true, num: buffers.length };
buffers.push(data);
return placeholder;
} else if (isArray(data)) {
var newData = new Array(data.length);
for (var i = 0; i < data.length; i++) {
newData[i] = _deconstructPacket(data[i], buffers);
}
return newData;
} else if (typeof data === 'object' && !(data instanceof Date)) {
var newData = {};
for (var key in data) {
newData[key] = _deconstructPacket(data[key], buffers);
}
return newData;
}
return data;
}

/**
* Reconstructs a binary packet from its placeholder packet and buffers
*
Expand All @@ -60,29 +59,28 @@ exports.deconstructPacket = function(packet){
*/

exports.reconstructPacket = function(packet, buffers) {
packet.data = _reconstructPacket(packet.data, buffers);
packet.attachments = undefined; // no longer useful
return packet;
};

function _reconstructPacket(data) {
if (data && data._placeholder) {
var buf = buffers[data.num]; // appropriate buffer (should be natural order anyway)
return buf;
} else if (isArray(data)) {
for (var i = 0; i < data.length; i++) {
data[i] = _reconstructPacket(data[i]);
}
return data;
} else if (data && 'object' === typeof data) {
for (var key in data) {
data[key] = _reconstructPacket(data[key]);
}
return data;
function _reconstructPacket(data, buffers) {
if (!data) return data;

if (data && data._placeholder) {
return buffers[data.num]; // appropriate buffer (should be natural order anyway)
} else if (isArray(data)) {
for (var i = 0; i < data.length; i++) {
data[i] = _reconstructPacket(data[i], buffers);
}
} else if (typeof data === 'object') {
for (var key in data) {
data[key] = _reconstructPacket(data[key], buffers);
}
return data;
}

packet.data = _reconstructPacket(packet.data);
packet.attachments = undefined; // no longer useful
return packet;
};
return data;
}

/**
* Asynchronously removes Blobs or Files from data via
Expand Down Expand Up @@ -124,7 +122,7 @@ exports.removeBlobs = function(data, callback) {
for (var i = 0; i < obj.length; i++) {
_removeBlobs(obj[i], i, obj);
}
} else if (obj && 'object' === typeof obj && !isBuf(obj)) { // and object
} else if (typeof obj === 'object' && !isBuf(obj)) { // and object
for (var key in obj) {
_removeBlobs(obj[key], key, obj);
}
Expand Down
20 changes: 9 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,19 @@ Encoder.prototype.encode = function(obj, callback){
*/

function encodeAsString(obj) {
var str = '';

// first is type
str += obj.type;
var str = '' + obj.type;

// attachments if we have them
if (exports.BINARY_EVENT === obj.type || exports.BINARY_ACK === obj.type) {
str += obj.attachments;
str += '-';
str += obj.attachments + '-';
}

// if we have a namespace other than `/`
// we append it followed by a comma `,`
if (obj.nsp && '/' !== obj.nsp) {
str += obj.nsp;
str += ',';
str += obj.nsp + ',';
}

// immediately followed by the id
Expand Down Expand Up @@ -265,15 +262,16 @@ Decoder.prototype.add = function(obj) {
*/

function decodeString(str) {
var p = {};
var i = 0;

// look up type
p.type = Number(str.charAt(0));
var p = {
type: Number(str.charAt(0))
};

if (null == exports.types[p.type]) return error();

// look up attachments if type binary
if (exports.BINARY_EVENT == p.type || exports.BINARY_ACK == p.type) {
if (exports.BINARY_EVENT === p.type || exports.BINARY_ACK === p.type) {
var buf = '';
while (str.charAt(++i) !== '-') {
buf += str.charAt(i);
Expand Down Expand Up @@ -390,7 +388,7 @@ BinaryReconstructor.prototype.finishedReconstruction = function() {
this.buffers = [];
};

function error(data){
function error() {
return {
type: exports.ERROR,
data: 'parser error'
Expand Down