Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…261l3A1m3y <--- after ;o
  • Loading branch information
3rd-Eden committed Oct 12, 2011
1 parent 6d5ffa0 commit 709c172
Showing 1 changed file with 59 additions and 71 deletions.
130 changes: 59 additions & 71 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,38 @@
* Packet types.
*/

var packets = exports.packets = [
'disconnect'
, 'connect'
, 'heartbeat'
, 'message'
, 'json'
, 'event'
, 'ack'
, 'error'
, 'noop'
];
var packets = exports.packets = {
'disconnect': 0
, 'connect': 1
, 'heartbeat': 2
, 'message': 3
, 'json': 4
, 'event': 5
, 'ack': 6
, 'error': 7
, 'noop': 8
}
, packetslist = Object.keys(packets);

/**
* Errors reasons.
*/

var reasons = exports.reasons = [
'transport not supported'
, 'client not handshaken'
, 'unauthorized'
];
var reasons = exports.reasons = {
'transport not supported': 0
, 'client not handshaken': 1
, 'unauthorized': 2
}
, reasonslist = Object.keys(reasons);

/**
* Errors advice.
*/

var advice = exports.advice = [
'reconnect'
];
var advice = exports.advice = {
'reconnect': 0
}
, advicelist = Object.keys(advice);

/**
* Encodes a packet.
Expand All @@ -50,16 +53,16 @@ var advice = exports.advice = [
*/

exports.encodePacket = function (packet) {
var type = packets.indexOf(packet.type)
var type = packets[packet.type]
, id = packet.id || ''
, endpoint = packet.endpoint || ''
, ack = packet.ack
, data = null;

switch (packet.type) {
case 'error':
var reason = packet.reason ? reasons.indexOf(packet.reason) : ''
, adv = packet.advice ? advice.indexOf(packet.advice) : ''
var reason = packet.reason ? reasons[packet.reason] : ''
, adv = packet.advice ? advice[packet.advice] : ''

if (reason !== '' || adv !== '')
data = reason + (adv !== '' ? ('+' + adv) : '')
Expand Down Expand Up @@ -141,14 +144,15 @@ exports.encodePayload = function (packets) {
var regexp = /([^:]+):([0-9]+)?(\+)?:([^:]+)?:?([\s\S]*)?/;

exports.decodePacket = function (data) {
var pieces = data.match(regexp);
var pieces = data.match(regexp)
, parse = JSON.parse;

if (!pieces) return {};

var id = pieces[2] || ''
, data = pieces[5] || ''
, packet = {
type: packets[pieces[1]]
type: packetslist[pieces[1]]
, endpoint: pieces[4] || ''
};

Expand All @@ -162,54 +166,38 @@ exports.decodePacket = function (data) {
}

// handle different packet types
switch (packet.type) {
case 'error':
var pieces = data.split('+');
packet.reason = reasons[pieces[0]] || '';
packet.advice = advice[pieces[1]] || '';
break;

case 'message':
packet.data = data || '';
break;

case 'event':
try {
var opts = JSON.parse(data);
packet.name = opts.name;
packet.args = opts.args;
} catch (e) { }

packet.args = packet.args || [];
break;

case 'json':
try {
packet.data = JSON.parse(data);
} catch (e) { }
break;

case 'connect':
packet.qs = data || '';
break;

case 'ack':
var pieces = data.match(/^([0-9]+)(\+)?(.*)/);
if (pieces) {
packet.ackId = pieces[1];
packet.args = [];

if (pieces[3]) {
try {
packet.args = pieces[3] ? JSON.parse(pieces[3]) : [];
} catch (e) { }
}
if (packet.type === 'error') {
pieces = data.split('+');
packet.reason = reasonslist[pieces[0]] || '';
packet.advice = advicelist[pieces[1]] || '';
} else if (packet.type === 'message') {
packet.data = data || '';
} else if (packet.type === 'event') {
try {
pieces = parse(data);
packet.name = pieces.name;
packet.args = pieces.args;
} catch (e) { }

packet.args = packet.args || [];
} else if (packet.type === 'json') {
try {
packet.data = parse(data);
} catch (e) { }
} else if (packet.type === 'connect') {
packet.qs = data || '';
} else if (packet.type === 'ack') {
pieces = data.match(/^([0-9]+)(\+)?(.*)/);
if (pieces) {
packet.ackId = pieces[1];
packet.args = [];

if (pieces[3]) {
try {
packet.args = pieces[3] ? parse(pieces[3]) : [];
} catch (e) { }
}
break;

case 'disconnect':
case 'heartbeat':
break;
}
};

return packet;
Expand Down

0 comments on commit 709c172

Please sign in to comment.