From 30428b09317abd7d1b38d1614b2b7e5f6495d5e6 Mon Sep 17 00:00:00 2001 From: Mike Bush Date: Thu, 14 Jul 2016 18:24:50 +0200 Subject: [PATCH 1/2] 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 f3d4781..c62a052 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'); }); From 2ad75f5addf902875226b9334b628a6158ee7313 Mon Sep 17 00:00:00 2001 From: Mike Bush Date: Fri, 19 Aug 2016 18:04:14 +0200 Subject: [PATCH 2/2] Remove conversion of strings to string --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index c62a052..d4bfda4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -193,7 +193,7 @@ function OutBatch() { OutBatch.prototype.append = function (buf, flags, cb) { if (typeof buf === 'string') { - buf = new Buffer(String(buf), 'utf8'); + buf = new Buffer(buf, 'utf8'); } else if (!Buffer.isBuffer(buf)) { throw new TypeError('ZeroMQ only supports Buffers or Strings as messages'); }