Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

Commit

Permalink
Throw on non-buffer, non-string messages rather than converting them …
Browse files Browse the repository at this point in the history
…to strings implicitly
  • Loading branch information
platy committed Jul 14, 2016
1 parent b7dc94d commit ba5d0c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ function OutBatch() {
}

OutBatch.prototype.append = function (buf, flags, cb) {
if (!Buffer.isBuffer(buf)) {
if (typeof buf === 'string') {
buf = new Buffer(String(buf), 'utf8');
} else if (!Buffer.isBuffer(buf)) {
throw new TypeError('ZeroMQ only supports Buffers or Strings as messages');
}

this.content.push(buf, flags);
Expand Down
22 changes: 10 additions & 12 deletions test/socket.messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ describe('socket.messages', function(){
msg.should.equal('string');
break;
case 1:
msg.should.equal('15.99');
break;
case 2:
msg.should.equal('buffer');
push.close();
pull.close();
Expand All @@ -35,16 +32,21 @@ describe('socket.messages', function(){
if (error) throw error;
push.connect('inproc://stuff_ssm');
push.send('string');
push.send(15.99);
push.send(new Buffer('buffer'));
});
});

it('should not support messages other than string and Buffer', function(){
push.connect('inproc://stuff_ssm');
should.throws(function () {
push.send(15.99);
}, TypeError);
});

it('should support multipart messages', function(done){
pull.on('message', function(msg1, msg2, msg3){
pull.on('message', function(msg1, msg2){
msg1.toString().should.equal('string');
msg2.toString().should.equal('15.99');
msg3.toString().should.equal('buffer');
msg2.toString().should.equal('buffer');
push.close();
pull.close();
done();
Expand All @@ -53,7 +55,7 @@ describe('socket.messages', function(){
pull.bind('inproc://stuff_ssmm', function (error) {
if (error) throw error;
push.connect('inproc://stuff_ssmm');
push.send(['string', 15.99, new Buffer('buffer')]);
push.send(['string', new Buffer('buffer')]);
});
});

Expand Down Expand Up @@ -88,9 +90,6 @@ describe('socket.messages', function(){
msg.should.equal('string');
break;
case 1:
msg.should.equal('15.99');
break;
case 2:
msg.should.equal('buffer');
push.close();
pull.close();
Expand All @@ -110,7 +109,6 @@ describe('socket.messages', function(){
push.bind('tcp://127.0.0.1:12345', function (error) {
if (error) throw error;
push.send('string');
push.send(15.99);
push.send(new Buffer('buffer'));
pull.connect('tcp://127.0.0.1:12345');
});
Expand Down

0 comments on commit ba5d0c4

Please sign in to comment.