Skip to content

Commit

Permalink
To wrap the grunt work involved in encoding and decoding JSON message…
Browse files Browse the repository at this point in the history
…s a new attribute has been added to the client options named json.

Unit tests have also been provided.

Addressed review comments:

nats-io#1 - Validate message payload when JSON switch is turned on before publishing
nats-io#2 - Defaults to utf8 or encoding type set by the user
  • Loading branch information
diogogmt committed Feb 2, 2016
1 parent 6e1bb67 commit 1a1b7f9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 15 deletions.
30 changes: 20 additions & 10 deletions lib/nats.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var VERSION = '0.5.4',
BAD_MSG_ERR = new Error('Message can\'t be a function'),
BAD_REPLY_ERR = new Error('Reply can\'t be a function'),
CONN_CLOSED_ERR = new Error('Connection closed'),
BAD_JSON_MSG_ERR = new Error('Message should be a JSON object'),

// Pedantic Mode support
//Q_SUB = /^([^\.\*>\s]+|>$|\*)(\.([^\.\*>\s]+|>$|\*))*$/, // TODO: remove / never used
Expand Down Expand Up @@ -253,12 +254,6 @@ Client.prototype.parseOptions = function(opts) {
if (options.noRandomize !== true) {
shuffle(client.servers);
}

// For compatibilty with the golang client we need to use
// a base64 encoder when dealing with json
if (options.json) {
client.encoding = 'base64';
}
};

/**
Expand Down Expand Up @@ -849,8 +844,14 @@ Client.prototype.processMsg = function() {
}

if (sub.callback) {
// Create an ascii string out of the base64 msg before parsing to a json object
var msg = this.options.json ? JSON.parse(new Buffer(this.payload.msg, 'base64').toString('ascii')) : this.payload.msg;
var msg = this.payload.msg;
if (this.options.json) {
try {
msg = JSON.parse(new Buffer(this.payload.msg, this.options.encoding).toString())
} catch (e) {
msg = e;
}
}
sub.callback(msg, this.payload.reply, this.payload.subj, this.payload.sid);
}
}
Expand Down Expand Up @@ -931,9 +932,18 @@ Client.prototype.publish = function(subject, msg, opt_reply, opt_callback) {
}

// Need to treat sending buffers different.

if (!Buffer.isBuffer(msg)) {
var str = this.options.json ? JSON.stringify(msg) : msg;
var str = msg;
if (this.options.json) {
if (typeof msg !== 'object' || Array.isArray(msg)) {
throw(BAD_JSON_MSG_ERR);
}
try {
str = JSON.stringify(msg)
} catch (e) {
throw(BAD_JSON_MSG_ERR);
}
}
this.sendCommand(psub + Buffer.byteLength(str) + CR_LF + str + CR_LF);
} else {
var b = new Buffer(psub.length + msg.length + (2 * CR_LF_LEN) + msg.length.toString().length);
Expand Down
74 changes: 69 additions & 5 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,81 @@ describe('Basics', function() {
it('should parse json messages', function(done) {
var config = {
port: PORT,
json: true
json: true,
};
var nc = NATS.connect(config);
var jsonMsg = {
json: true
key: true
};
nc.subscribe('foo', function(msg) {
msg.should.have.property('json').and.be.a.Boolean();
nc.subscribe('foo1', function(msg) {
msg.should.have.property('key').and.be.a.Boolean();
done();
});
nc.publish('foo1', jsonMsg);
});

it('should parse UTF8 json messages', function(done) {
var config = {
port: PORT,
json: true,
};
var nc = NATS.connect(config);
var utf8msg = {
key: 'CEDILA-Ç'
};
nc.subscribe('foo2', function(msg) {
msg.should.have.property('key');
msg.key.should.equal('CEDILA-Ç')
done();
});
nc.publish('foo', jsonMsg);
nc.publish('foo2', utf8msg);
});

it('should validate json messages before publishing', function(done) {
var config = {
port: PORT,
json: true,
};
var nc = NATS.connect(config);
var error;

try {
nc.publish('foo3', 'not JSON');
} catch (e) {
error = e;
}
if (!error) {
done('Should not accept string as message when JSON switch is turned on');
}

try {
nc.publish('foo3', 1);
} catch (e) {
error = e;
}
if (!error) {
done('Should not accept number as message when JSON switch is turned on');
}

try {
nc.publish('foo3', false);
} catch (e) {
error = e;
}
if (!error) {
done('Should not accept boolean as message when JSON switch is turned on');
}

try {
nc.publish('foo3', []);
} catch (e) {
error = e;
}
if (!error) {
done('Should not accept array as message when JSON switch is turned on');
}

done();
});

});

0 comments on commit 1a1b7f9

Please sign in to comment.