From ba5d0c42e8b3248446c67dbac0ea3e379b667d86 Mon Sep 17 00:00:00 2001 From: Mike Bush Date: Thu, 14 Jul 2016 18:24:50 +0200 Subject: [PATCH] Throw on non-buffer, non-string messages rather than converting them to strings implicitly --- lib/index.js | 4 +++- test/socket.messages.js | 22 ++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/index.js b/lib/index.js index ea67db9..704056d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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); diff --git a/test/socket.messages.js b/test/socket.messages.js index 69e8210..74d8f9e 100644 --- a/test/socket.messages.js +++ b/test/socket.messages.js @@ -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(); @@ -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(); @@ -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')]); }); }); @@ -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(); @@ -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'); });