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

Use json-stringify-safe to detect circular references in debug msgs

This commit is contained in:
Nick O'Leary 2016-12-20 23:16:11 +00:00
parent bba3ca8cc0
commit e7cc42a927
2 changed files with 4 additions and 5 deletions

View File

@ -19,6 +19,7 @@ module.exports = function(RED) {
var util = require("util"); var util = require("util");
var events = require("events"); var events = require("events");
var path = require("path"); var path = require("path");
var safeJSONStringify = require("json-stringify-safe");
var debuglength = RED.settings.debugMaxLength||1000; var debuglength = RED.settings.debugMaxLength||1000;
var useColors = false; var useColors = false;
// util.inspect.styles.boolean = "red"; // util.inspect.styles.boolean = "red";
@ -87,6 +88,7 @@ module.exports = function(RED) {
} }
} else if (msg.msg && typeof msg.msg === 'object') { } else if (msg.msg && typeof msg.msg === 'object') {
var seen = []; var seen = [];
var seenAts = [];
try { try {
msg.format = msg.msg.constructor.name || "Object"; msg.format = msg.msg.constructor.name || "Object";
} catch(err) { } catch(err) {
@ -106,7 +108,7 @@ module.exports = function(RED) {
} }
} }
if (isArray || (msg.format === "Object")) { if (isArray || (msg.format === "Object")) {
msg.msg = JSON.stringify(msg.msg, function(key, value) { msg.msg = safeJSONStringify(msg.msg, function(key, value) {
if (key[0] === '_' && key !== "_msgid") { if (key[0] === '_' && key !== "_msgid") {
return undefined; return undefined;
} }
@ -124,10 +126,6 @@ module.exports = function(RED) {
length: value.length length: value.length
} }
} }
if (typeof value === 'object' && value !== null) {
if (seen.indexOf(value) !== -1) { return "[circular]"; }
seen.push(value);
}
if (typeof value === 'string') { if (typeof value === 'string') {
if (value.length > debuglength) { if (value.length > debuglength) {
return value.substring(0,debuglength)+"..."; return value.substring(0,debuglength)+"...";

View File

@ -41,6 +41,7 @@
"i18next":"1.10.6", "i18next":"1.10.6",
"is-utf8":"0.2.1", "is-utf8":"0.2.1",
"js-yaml": "3.6.1", "js-yaml": "3.6.1",
"json-stringify-safe":"5.0.1",
"jsonata":"1.0.10", "jsonata":"1.0.10",
"media-typer": "0.3.0", "media-typer": "0.3.0",
"mqtt": "1.14.1", "mqtt": "1.14.1",