Skip to content

Commit

Permalink
Adapted server encoder/decoder for event spec change.
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Jun 11, 2011
1 parent baa8056 commit 1db340a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
26 changes: 14 additions & 12 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ exports.encodePacket = function (packet) {
break;

case 'event':
var params = packet.args && packet.args.length
? JSON.stringify(packet.args) : '';
data = packet.name + (params !== '' ? ('\ufffd' + params) : '');
var ev = { name: packet.name };

if (packet.args && packet.args.length) {
ev.args = packet.args;
}

data = JSON.stringify(ev);
break;

case 'json':
Expand Down Expand Up @@ -173,15 +177,13 @@ exports.decodePacket = function (data) {
break;

case 'event':
var pieces = data.match(/([^\ufffd]+)(\ufffd)?(.*)/);
packet.name = pieces[1] || '';
packet.args = [];

if (pieces[3]) {
try {
packet.args = JSON.parse(pieces[3]);
} catch (e) { }
}
try {
var opts = JSON.parse(data);
packet.name = opts.name;
packet.args = opts.args;
} catch (e) { }

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

case 'json':
Expand Down
17 changes: 9 additions & 8 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = {
},

'decoding ack packet with args': function () {
parser.decodePacket('6:::12+' + JSON.stringify(['woot', 'wa'])).should.eql({
parser.decodePacket('6:::12+["woot","wa"]').should.eql({
type: 'ack'
, ackId: '12'
, endpoint: ''
Expand Down Expand Up @@ -103,7 +103,7 @@ module.exports = {
},

'decoding an event packet': function () {
parser.decodePacket('5:::woot').should.eql({
parser.decodePacket('5:::{"name":"woot"}').should.eql({
type: 'event'
, name: 'woot'
, endpoint: ''
Expand All @@ -112,7 +112,7 @@ module.exports = {
},

'decoding an event packet with message id and ack': function () {
parser.decodePacket('5:1+::tobi').should.eql({
parser.decodePacket('5:1+::{"name":"tobi"}').should.eql({
type: 'event'
, id: 1
, ack: 'data'
Expand All @@ -123,7 +123,8 @@ module.exports = {
},

'decoding an event packet with data': function () {
parser.decodePacket('5:::edwald\ufffd[{"a": "b"},2,"3"]').should.eql({
parser.decodePacket('5:::{"name":"edwald","args":[{"a": "b"},2,"3"]}')
.should.eql({
type: 'event'
, name: 'edwald'
, endpoint: ''
Expand Down Expand Up @@ -230,7 +231,7 @@ module.exports = {
, ackId: '12'
, endpoint: ''
, args: ['woot', 'wa']
}).should.eql('6:::12+' + JSON.stringify(['woot', 'wa']));
}).should.eql('6:::12+["woot","wa"]');
},

'encoding json packet': function () {
Expand All @@ -257,7 +258,7 @@ module.exports = {
, name: 'woot'
, endpoint: ''
, args: []
}).should.eql('5:::woot');
}).should.eql('5:::{"name":"woot"}');
},

'encoding an event packet with message id and ack': function () {
Expand All @@ -268,7 +269,7 @@ module.exports = {
, endpoint: ''
, name: 'tobi'
, args: []
}).should.eql('5:1+::tobi');
}).should.eql('5:1+::{"name":"tobi"}');
},

'encoding an event packet with data': function () {
Expand All @@ -277,7 +278,7 @@ module.exports = {
, name: 'edwald'
, endpoint: ''
, args: [{a: 'b'}, 2, '3']
}).should.eql('5:::edwald\ufffd[{"a":"b"},2,"3"]');
}).should.eql('5:::{"name":"edwald","args":[{"a":"b"},2,"3"]}');
},

'encoding a message packet': function () {
Expand Down

0 comments on commit 1db340a

Please sign in to comment.