1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Move cloneMessage to RED.util.cloneMessage

This commit is contained in:
Nick O'Leary 2014-11-06 11:39:30 +00:00
parent 9c92eeb9f5
commit 3e235ecc0b
3 changed files with 56 additions and 20 deletions

View File

@ -16,9 +16,10 @@
var util = require("util"); var util = require("util");
var EventEmitter = require("events").EventEmitter; var EventEmitter = require("events").EventEmitter;
var clone = require("clone");
var when = require("when"); var when = require("when");
var redUtil = require("../util");
var flows = require("./flows"); var flows = require("./flows");
var comms = require("../comms"); var comms = require("../comms");
@ -71,24 +72,7 @@ Node.prototype.on = function(event, callback) {
Node.prototype.close = function() {}; 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) { Node.prototype.send = function(msg) {
var msgSent = false; var msgSent = false;
@ -134,7 +118,7 @@ Node.prototype.send = function(msg) {
// for each msg to send eg. [[m1, m2, ...], ...] // for each msg to send eg. [[m1, m2, ...], ...]
for (k = 0; k < msgs.length; k++) { for (k = 0; k < msgs.length; k++) {
if (msgSent) { if (msgSent) {
sendEvents.push({n:node,m:cloneMessage(msgs[k])}); sendEvents.push({n:node,m:redUtil.cloneMessage(msgs[k])});
} else { } else {
// first msg sent so don't clone // first msg sent so don't clone
sendEvents.push({n:node,m:msgs[k]}); sendEvents.push({n:node,m:msgs[k]});

View File

@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
**/ **/
var clone = require("clone");
function ensureString(o) { function ensureString(o) {
if (Buffer.isBuffer(o)) { if (Buffer.isBuffer(o)) {
return o.toString(); return o.toString();
@ -36,8 +38,28 @@ function ensureBuffer(o) {
return new Buffer(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 = { module.exports = {
ensureString: ensureString, ensureString: ensureString,
ensureBuffer: ensureBuffer, ensureBuffer: ensureBuffer,
cloneMessage: cloneMessage
}; };

View File

@ -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);
});
});
}); });