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

Add support for Map/Set property types on Debug

This commit is contained in:
Nick O'Leary 2021-06-29 11:09:30 +01:00
parent 58023b4bf0
commit 48ac50e1c9
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 112 additions and 12 deletions

View File

@ -58,6 +58,10 @@ RED.utils = (function() {
result = $('<span class="red-ui-debug-msg-object-value red-ui-debug-msg-type-meta"></span>').text('buffer['+value.length+']'); result = $('<span class="red-ui-debug-msg-object-value red-ui-debug-msg-type-meta"></span>').text('buffer['+value.length+']');
} else if (value.hasOwnProperty('type') && value.type === 'array' && value.hasOwnProperty('data')) { } else if (value.hasOwnProperty('type') && value.type === 'array' && value.hasOwnProperty('data')) {
result = $('<span class="red-ui-debug-msg-object-value red-ui-debug-msg-type-meta"></span>').text('array['+value.length+']'); result = $('<span class="red-ui-debug-msg-object-value red-ui-debug-msg-type-meta"></span>').text('array['+value.length+']');
} else if (value.hasOwnProperty('type') && value.type === 'set' && value.hasOwnProperty('data')) {
result = $('<span class="red-ui-debug-msg-object-value red-ui-debug-msg-type-meta"></span>').text('set['+value.length+']');
} else if (value.hasOwnProperty('type') && value.type === 'map' && value.hasOwnProperty('data')) {
result = $('<span class="red-ui-debug-msg-object-value red-ui-debug-msg-type-meta"></span>').text('map');
} else if (value.hasOwnProperty('type') && value.type === 'function') { } else if (value.hasOwnProperty('type') && value.type === 'function') {
result = $('<span class="red-ui-debug-msg-object-value red-ui-debug-msg-type-meta"></span>').text('function'); result = $('<span class="red-ui-debug-msg-object-value red-ui-debug-msg-type-meta"></span>').text('function');
} else if (value.hasOwnProperty('type') && (value.type === 'number' || value.type === 'bigint')) { } else if (value.hasOwnProperty('type') && (value.type === 'number' || value.type === 'bigint')) {
@ -350,7 +354,7 @@ RED.utils = (function() {
var isArray = Array.isArray(obj); var isArray = Array.isArray(obj);
var isArrayObject = false; var isArrayObject = false;
if (obj && typeof obj === 'object' && obj.hasOwnProperty('type') && obj.hasOwnProperty('data') && ((obj.__enc__ && obj.type === 'array') || obj.type === 'Buffer')) { if (obj && typeof obj === 'object' && obj.hasOwnProperty('type') && obj.hasOwnProperty('data') && ((obj.__enc__ && obj.type === 'set') || (obj.__enc__ && obj.type === 'array') || obj.type === 'Buffer')) {
isArray = true; isArray = true;
isArrayObject = true; isArrayObject = true;
} }
@ -532,12 +536,18 @@ RED.utils = (function() {
} }
} else if (typeof obj === 'object') { } else if (typeof obj === 'object') {
element.addClass('collapsed'); element.addClass('collapsed');
var keys = Object.keys(obj); var data = obj;
var type = "object";
if (data.__enc__) {
data = data.data;
type = obj.type.toLowerCase();
}
var keys = Object.keys(data);
if (key || keys.length > 0) { if (key || keys.length > 0) {
$('<i class="fa fa-caret-right red-ui-debug-msg-object-handle"></i> ').prependTo(header); $('<i class="fa fa-caret-right red-ui-debug-msg-object-handle"></i> ').prependTo(header);
makeExpandable(header, function() { makeExpandable(header, function() {
if (!key) { if (!key) {
$('<span class="red-ui-debug-msg-type-meta red-ui-debug-msg-object-type-header"></span>').text('object').appendTo(header); $('<span class="red-ui-debug-msg-type-meta red-ui-debug-msg-object-type-header"></span>').text(type).appendTo(header);
} }
for (i=0;i<keys.length;i++) { for (i=0;i<keys.length;i++) {
var row = $('<div class="red-ui-debug-msg-object-entry collapsed"></div>').appendTo(element); var row = $('<div class="red-ui-debug-msg-object-entry collapsed"></div>').appendTo(element);
@ -550,7 +560,7 @@ RED.utils = (function() {
} }
} }
subElements[newPath] = buildMessageElement( subElements[newPath] = buildMessageElement(
obj[keys[i]], data[keys[i]],
{ {
key: keys[i], key: keys[i],
typeHint: false, typeHint: false,
@ -573,7 +583,7 @@ RED.utils = (function() {
checkExpanded(strippedKey,expandPaths)); checkExpanded(strippedKey,expandPaths));
} }
if (key) { if (key) {
$('<span class="red-ui-debug-msg-type-meta"></span>').text('object').appendTo(entryObj); $('<span class="red-ui-debug-msg-type-meta"></span>').text(type).appendTo(entryObj);
} else { } else {
headerHead = $('<span class="red-ui-debug-msg-object-header"></span>').appendTo(entryObj); headerHead = $('<span class="red-ui-debug-msg-object-header"></span>').appendTo(entryObj);
$('<span>{ </span>').appendTo(headerHead); $('<span>{ </span>').appendTo(headerHead);
@ -581,7 +591,7 @@ RED.utils = (function() {
for (i=0;i<keysLength;i++) { for (i=0;i<keysLength;i++) {
$('<span class="red-ui-debug-msg-object-key"></span>').text(keys[i]).appendTo(headerHead); $('<span class="red-ui-debug-msg-object-key"></span>').text(keys[i]).appendTo(headerHead);
$('<span>: </span>').appendTo(headerHead); $('<span>: </span>').appendTo(headerHead);
buildMessageSummaryValue(obj[keys[i]]).appendTo(headerHead); buildMessageSummaryValue(data[keys[i]]).appendTo(headerHead);
if (i < keysLength-1) { if (i < keysLength-1) {
$('<span>, </span>').appendTo(headerHead); $('<span>, </span>').appendTo(headerHead);
} }
@ -1062,7 +1072,7 @@ RED.utils = (function() {
payload = Infinity; payload = Infinity;
} else if ((format === 'number') && (payload === "-Infinity")) { } else if ((format === 'number') && (payload === "-Infinity")) {
payload = -Infinity; payload = -Infinity;
} else if (format === 'Object' || /^array/.test(format) || format === 'boolean' || format === 'number' ) { } else if (format === 'Object' || /^(array|set|map)/.test(format) || format === 'boolean' || format === 'number' ) {
payload = JSON.parse(payload); payload = JSON.parse(payload);
} else if (/error/i.test(format)) { } else if (/error/i.test(format)) {
payload = JSON.parse(payload); payload = JSON.parse(payload);

View File

@ -806,6 +806,7 @@ function encodeObject(msg,opts) {
}); });
} else { } else {
var isArray = util.isArray(msg.msg); var isArray = util.isArray(msg.msg);
var needsStringify = isArray;
if (isArray) { if (isArray) {
msg.format = "array["+msg.msg.length+"]"; msg.format = "array["+msg.msg.length+"]";
if (msg.msg.length > debuglength) { if (msg.msg.length > debuglength) {
@ -817,8 +818,26 @@ function encodeObject(msg,opts) {
length: msg.msg.length length: msg.msg.length
} }
} }
} else if (msg.msg && msg.msg.constructor.name === "Set") {
msg.format = "set["+msg.msg.size+"]";
msg.msg = {
__enc__: true,
type: "set",
data: Array.from(msg.msg).slice(0,debuglength),
length: msg.msg.size
} }
if (isArray || (msg.format === "Object")) { needsStringify = true;
} else if (msg.msg && msg.msg.constructor.name === "Map") {
msg.format = "map";
msg.msg = {
__enc__: true,
type: "map",
data: Object.fromEntries(Array.from(msg.msg.entries()).slice(0,debuglength)),
length: msg.msg.size
}
needsStringify = true;
}
if (needsStringify || (msg.format === "Object")) {
msg.msg = safeJSONStringify(msg.msg, function(key, value) { msg.msg = safeJSONStringify(msg.msg, function(key, value) {
if (key === '_req' || key === '_res') { if (key === '_req' || key === '_res') {
value = { value = {
@ -868,6 +887,20 @@ function encodeObject(msg,opts) {
value = "[internal]" value = "[internal]"
} else if (value.constructor.name === "Socket") { } else if (value.constructor.name === "Socket") {
value = "[internal]" value = "[internal]"
} else if (value.constructor.name === "Set") {
value = {
__enc__: true,
type: "set",
data: Array.from(value).slice(0,debuglength),
length: value.size
}
} else if (value.constructor.name === "Map") {
value = {
__enc__: true,
type: "map",
data: Object.fromEntries(Array.from(value.entries()).slice(0,debuglength)),
length: value.size
}
} }
} else if (value === undefined) { } else if (value === undefined) {
value = { value = {

View File

@ -768,6 +768,36 @@ describe("@node-red/util/util", function() {
result.format.should.eql("string[10]"); result.format.should.eql("string[10]");
result.msg.should.eql('123456...'); result.msg.should.eql('123456...');
}); });
it('encodes Map', function() {
const m = new Map();
m.set("a",1);
m.set("b",2);
var msg = {msg:m};
var result = util.encodeObject(msg);
result.format.should.eql("map");
var resultJson = JSON.parse(result.msg);
resultJson.should.have.property("__enc__",true);
resultJson.should.have.property("type","map");
resultJson.should.have.property("data",{"a":1,"b":2});
resultJson.should.have.property("length",2)
});
it('encodes Set', function() {
const m = new Set();
m.add("a");
m.add("b");
var msg = {msg:m};
var result = util.encodeObject(msg);
result.format.should.eql("set[2]");
var resultJson = JSON.parse(result.msg);
resultJson.should.have.property("__enc__",true);
resultJson.should.have.property("type","set");
resultJson.should.have.property("data",["a","b"]);
resultJson.should.have.property("length",2)
});
describe('encode object', function() { describe('encode object', function() {
it('object', function() { it('object', function() {
var msg = { msg:{"foo":"bar"} }; var msg = { msg:{"foo":"bar"} };
@ -800,7 +830,34 @@ describe("@node-red/util/util", function() {
resultJson.b.should.have.property("__enc__", true); resultJson.b.should.have.property("__enc__", true);
resultJson.b.should.have.property("type", "undefined"); resultJson.b.should.have.property("type", "undefined");
}); });
it('object with Map property', function() {
const m = new Map();
m.set("a",1);
m.set("b",2);
var msg = {msg:{"aMap":m}};
var result = util.encodeObject(msg);
result.format.should.eql("Object");
var resultJson = JSON.parse(result.msg);
resultJson.should.have.property("aMap");
resultJson.aMap.should.have.property("__enc__",true);
resultJson.aMap.should.have.property("type","map");
resultJson.aMap.should.have.property("data",{"a":1,"b":2});
resultJson.aMap.should.have.property("length",2)
});
it('object with Set property', function() {
const m = new Set();
m.add("a");
m.add("b");
var msg = {msg:{"aSet":m}};
var result = util.encodeObject(msg);
result.format.should.eql("Object");
var resultJson = JSON.parse(result.msg);
resultJson.should.have.property("aSet");
resultJson.aSet.should.have.property("__enc__",true);
resultJson.aSet.should.have.property("type","set");
resultJson.aSet.should.have.property("data",["a","b"]);
resultJson.aSet.should.have.property("length",2)
});
it('constructor of IncomingMessage', function() { it('constructor of IncomingMessage', function() {
function IncomingMessage(){}; function IncomingMessage(){};
var msg = { msg:new IncomingMessage() }; var msg = { msg:new IncomingMessage() };