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

cloneMessage should handle undefined without throwing err

Fixes #2399
This commit is contained in:
Nick O'Leary 2020-02-06 10:04:55 +00:00
parent 62fc554d25
commit 0f1ca1c7cf
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 27 additions and 20 deletions

View File

@ -81,22 +81,25 @@ function ensureBuffer(o) {
* @memberof @node-red/util_util * @memberof @node-red/util_util
*/ */
function cloneMessage(msg) { function cloneMessage(msg) {
// Temporary fix for #97 if (typeof msg !== "undefined") {
// TODO: remove this http-node-specific fix somehow // Temporary fix for #97
var req = msg.req; // TODO: remove this http-node-specific fix somehow
var res = msg.res; var req = msg.req;
delete msg.req; var res = msg.res;
delete msg.res; delete msg.req;
var m = clone(msg); delete msg.res;
if (req) { var m = clone(msg);
m.req = req; if (req) {
msg.req = req; m.req = req;
msg.req = req;
}
if (res) {
m.res = res;
msg.res = res;
}
return m;
} }
if (res) { return msg;
m.res = res;
msg.res = res;
}
return m;
} }
/** /**

View File

@ -143,6 +143,10 @@ describe("@node-red/util/util", function() {
cloned.req.should.equal(msg.req); cloned.req.should.equal(msg.req);
cloned.res.should.equal(msg.res); cloned.res.should.equal(msg.res);
}); });
it('handles undefined values without throwing an error', function() {
var result = util.cloneMessage(undefined);
should.not.exist(result);
})
}); });
describe('getObjectProperty', function() { describe('getObjectProperty', function() {
it('gets a property beginning with "msg."', function() { it('gets a property beginning with "msg."', function() {
@ -840,11 +844,11 @@ describe("@node-red/util/util", function() {
}, },
} }
}; };
for (var i = 0; i < 1000; i++) { for (var i = 0; i < 1000; i++) {
msg.msg.obj.big += 'some more string '; msg.msg.obj.big += 'some more string ';
} }
var result = util.encodeObject(msg); var result = util.encodeObject(msg);
result.format.should.eql("error"); result.format.should.eql("error");
var resultJson = JSON.parse(result.msg); var resultJson = JSON.parse(result.msg);
@ -862,7 +866,7 @@ describe("@node-red/util/util", function() {
throw new Error('Exception in toString - should have been caught'); throw new Error('Exception in toString - should have been caught');
} }
msg.msg.constructor = { name: "strangeobj" }; msg.msg.constructor = { name: "strangeobj" };
var result = util.encodeObject(msg); var result = util.encodeObject(msg);
var success = (result.msg.indexOf('[Type not printable]') >= 0); var success = (result.msg.indexOf('[Type not printable]') >= 0);
success.should.eql(true); success.should.eql(true);
@ -872,11 +876,11 @@ describe("@node-red/util/util", function() {
var msg = { var msg = {
msg: { msg: {
mystrangeobj:"hello", mystrangeobj:"hello",
constructor: { constructor: {
get name(){ get name(){
throw new Error('Exception in constructor name'); throw new Error('Exception in constructor name');
} }
} }
}, },
}; };
var result = util.encodeObject(msg); var result = util.encodeObject(msg);