From 3e235ecc0b219f668cd28f3401dfbe5ee1aa5bcc Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 6 Nov 2014 11:39:30 +0000 Subject: [PATCH] Move cloneMessage to RED.util.cloneMessage --- red/nodes/Node.js | 24 ++++-------------------- red/util.js | 22 ++++++++++++++++++++++ test/red/util_spec.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 20 deletions(-) diff --git a/red/nodes/Node.js b/red/nodes/Node.js index 1b1300379..a24c3490f 100644 --- a/red/nodes/Node.js +++ b/red/nodes/Node.js @@ -16,9 +16,10 @@ var util = require("util"); var EventEmitter = require("events").EventEmitter; -var clone = require("clone"); var when = require("when"); +var redUtil = require("../util"); + var flows = require("./flows"); var comms = require("../comms"); @@ -71,24 +72,7 @@ Node.prototype.on = function(event, callback) { Node.prototype.close = function() {}; -function cloneMessage(msg) { - // Temporary fix for #97 - // TODO: remove this http-node-specific fix somehow - var req = msg.req; - var res = msg.res; - delete msg.req; - delete msg.res; - var m = clone(msg); - if (req) { - m.req = req; - msg.req = req; - } - if (res) { - m.res = res; - msg.res = res; - } - return m; -} + Node.prototype.send = function(msg) { var msgSent = false; @@ -134,7 +118,7 @@ Node.prototype.send = function(msg) { // for each msg to send eg. [[m1, m2, ...], ...] for (k = 0; k < msgs.length; k++) { if (msgSent) { - sendEvents.push({n:node,m:cloneMessage(msgs[k])}); + sendEvents.push({n:node,m:redUtil.cloneMessage(msgs[k])}); } else { // first msg sent so don't clone sendEvents.push({n:node,m:msgs[k]}); diff --git a/red/util.js b/red/util.js index 7ca72b0e1..7f7d356e3 100644 --- a/red/util.js +++ b/red/util.js @@ -14,6 +14,8 @@ * limitations under the License. **/ +var clone = require("clone"); + function ensureString(o) { if (Buffer.isBuffer(o)) { return o.toString(); @@ -36,8 +38,28 @@ function ensureBuffer(o) { return new Buffer(o); } +function cloneMessage(msg) { + // Temporary fix for #97 + // TODO: remove this http-node-specific fix somehow + var req = msg.req; + var res = msg.res; + delete msg.req; + delete msg.res; + var m = clone(msg); + if (req) { + m.req = req; + msg.req = req; + } + if (res) { + m.res = res; + msg.res = res; + } + return m; +} + module.exports = { ensureString: ensureString, ensureBuffer: ensureBuffer, + cloneMessage: cloneMessage }; diff --git a/test/red/util_spec.js b/test/red/util_spec.js index 5200ef122..cd65a933d 100644 --- a/test/red/util_spec.js +++ b/test/red/util_spec.js @@ -66,5 +66,35 @@ describe("red/util", function() { } }); }); + + describe('cloneMessage', function() { + it('clones a simple message', function() { + var msg = {string:"hi",array:[1,2,3],object:{a:1,subobject:{b:2}}}; + + var cloned = util.cloneMessage(msg); + + cloned.should.eql(msg); + + cloned.should.not.equal(msg); + cloned.array.should.not.equal(msg.string); + cloned.object.should.not.equal(msg.object); + cloned.object.subobject.should.not.equal(msg.object.subobject); + + cloned.should.not.have.property("req"); + cloned.should.not.have.property("res"); + }); + it('does not clone http req/res properties', function() { + var msg = {req:{a:1},res:{b:2}}; + + var cloned = util.cloneMessage(msg); + + cloned.should.eql(msg); + cloned.should.not.equal(msg); + + cloned.req.should.equal(msg.req); + cloned.res.should.equal(msg.res); + }); + + }); });