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

try to fix debug node non-standard object type handling.

This commit is contained in:
Dave Conway-Jones 2015-10-10 22:41:07 +01:00
parent 1bf72a0bc3
commit 61045ddd7f
2 changed files with 11 additions and 11 deletions

View File

@ -86,15 +86,12 @@ module.exports = function(RED) {
msg.msg = msg.msg.toString('hex');
} else if (typeof msg.msg === 'object') {
var seen = [];
msg.format = "object";
msg.format = msg.msg.constructor.name || "Object";
var isArray = util.isArray(msg.msg);
if (!isArray && msg.msg.toString !== Object.prototype.toString) {
msg.format = msg.msg.constructor.name || "object";
msg.msg = msg.msg.toString();
} else {
if (isArray) {
msg.format = "array ["+msg.msg.length+"]";
}
if (isArray || (msg.format === "Object")) {
msg.msg = JSON.stringify(msg.msg, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (seen.indexOf(value) !== -1) { return "[circular]"; }
@ -102,6 +99,9 @@ module.exports = function(RED) {
}
return value;
}," ");
} else {
try { msg.msg = msg.msg.toString(); }
catch(e) { msg.msg = "[Type not printable]"; }
}
seen = null;
} else if (typeof msg.msg === "boolean") {

View File

@ -94,7 +94,7 @@ describe('debug node', function() {
}, function(msg) {
JSON.parse(msg).should.eql({
topic:"debug",
data:{id:"n1",msg:'{\n "payload": "test"\n}',format:"object"}
data:{id:"n1",msg:'{\n "payload": "test"\n}',format:"Object"}
});
}, done);
});
@ -179,7 +179,7 @@ describe('debug node', function() {
}, function(msg) {
JSON.parse(msg).should.eql({
topic:"debug",
data:{id:"n1",msg:'{\n "type": "foo"\n}',property:"payload",format:"object"}
data:{id:"n1",msg:'{\n "type": "foo"\n}',property:"payload",format:"Object"}
});
}, done);
});
@ -215,7 +215,7 @@ describe('debug node', function() {
data:{
id:"n1",
msg:'{\n "name": "bar",\n "o": "[circular]"\n}',
property:"payload",format:"object"
property:"payload",format:"Object"
}
});
}, done);