Fix util.encodeObject

This commit is contained in:
Alexandre Alapetite
2022-02-03 15:59:25 +01:00
parent e55cbb3e3d
commit 280d63fde7
4 changed files with 83 additions and 20 deletions

View File

@@ -24,6 +24,18 @@ const jsonata = require("jsonata");
const moment = require("moment-timezone");
const safeJSONStringify = require("json-stringify-safe");
const util = require("util");
const { hasOwnProperty } = Object.prototype;
/**
* Safely returns the object construtor name.
* @return {String} the name of the object constructor if it exists, empty string otherwise.
* @memberof @node-red/util_util
*/
function constructorName(obj) {
// Note: This function could be replaced by optional chaining in Node.js 14+:
// obj?.constructor?.name
return obj && obj.constructor ? obj.constructor.name : '';
}
/**
* Generates a psuedo-unique-random id.
@@ -171,7 +183,7 @@ function compareObjects(obj1,obj2) {
}
for (var k in obj1) {
/* istanbul ignore else */
if (obj1.hasOwnProperty(k)) {
if (hasOwnProperty.call(obj1, k)) {
if (!compareObjects(obj1[k],obj2[k])) {
return false;
}
@@ -462,7 +474,7 @@ function setObjectProperty(msg,prop,value,createMissing) {
for (var i=0;i<length-1;i++) {
key = msgPropParts[i];
if (typeof key === 'string' || (typeof key === 'number' && !Array.isArray(obj))) {
if (obj.hasOwnProperty(key)) {
if (hasOwnProperty.call(obj, key)) {
if (length > 1 && ((typeof obj[key] !== "object" && typeof obj[key] !== "function") || obj[key] === null)) {
// Break out early as we cannot create a property beneath
// this type of value
@@ -561,7 +573,7 @@ function getSetting(node, name, flow_) {
* @memberof @node-red/util_util
*/
function evaluateEnvProperty(value, node) {
var flow = (node && node.hasOwnProperty("_flow")) ? node._flow : null;
var flow = (node && hasOwnProperty.call(node, "_flow")) ? node._flow : null;
var result;
if (/^\${[^}]+}$/.test(value)) {
// ${ENV_VAR}
@@ -785,7 +797,7 @@ function normaliseNodeTypeName(name) {
function encodeObject(msg,opts) {
try {
var debuglength = 1000;
if (opts && opts.hasOwnProperty('maxLength')) {
if (opts && hasOwnProperty.call(opts, 'maxLength')) {
debuglength = opts.maxLength;
}
var msgType = typeof msg.msg;
@@ -795,7 +807,7 @@ function encodeObject(msg,opts) {
if (msg.msg.name) {
errorMsg.name = msg.msg.name;
}
if (msg.msg.hasOwnProperty('message')) {
if (hasOwnProperty.call(msg.msg, 'message')) {
errorMsg.message = msg.msg.message;
} else {
errorMsg.message = msg.msg.toString();
@@ -809,7 +821,7 @@ function encodeObject(msg,opts) {
}
} else if (msg.msg && msgType === 'object') {
try {
msg.format = msg.msg.constructor.name || "Object";
msg.format = constructorName(msg.msg) || "Object";
// Handle special case of msg.req/res objects from HTTP In node
if (msg.format === "IncomingMessage" || msg.format === "ServerResponse") {
msg.format = "Object";
@@ -836,7 +848,7 @@ function encodeObject(msg,opts) {
length: msg.msg.length
}
}
} else if (msg.msg && msg.msg.constructor.name === "Set") {
} else if (constructorName(msg.msg) === "Set") {
msg.format = "set["+msg.msg.size+"]";
msg.msg = {
__enc__: true,
@@ -845,7 +857,7 @@ function encodeObject(msg,opts) {
length: msg.msg.size
}
needsStringify = true;
} else if (msg.msg && msg.msg.constructor.name === "Map") {
} else if (constructorName(msg.msg) === "Map") {
msg.format = "map";
msg.msg = {
__enc__: true,
@@ -854,7 +866,7 @@ function encodeObject(msg,opts) {
length: msg.msg.size
}
needsStringify = true;
} else if (msg.msg && msg.msg.constructor.name === "RegExp") {
} else if (constructorName(msg.msg) === "RegExp") {
msg.format = 'regexp';
msg.msg = msg.msg.toString();
}
@@ -904,25 +916,25 @@ function encodeObject(msg,opts) {
if (value.length > debuglength) {
value.data = value.data.slice(0,debuglength);
}
} else if (value.constructor.name === "ServerResponse") {
} else if (constructorName(value) === "ServerResponse") {
value = "[internal]"
} else if (value.constructor.name === "Socket") {
} else if (constructorName(value) === "Socket") {
value = "[internal]"
} else if (value.constructor.name === "Set") {
} else if (constructorName(value) === "Set") {
value = {
__enc__: true,
type: "set",
data: Array.from(value).slice(0,debuglength),
length: value.size
}
} else if (value.constructor.name === "Map") {
} else if (constructorName(value) === "Map") {
value = {
__enc__: true,
type: "map",
data: Object.fromEntries(Array.from(value.entries()).slice(0,debuglength)),
length: value.size
}
} else if (value.constructor.name === "RegExp") {
} else if (constructorName(value) === "RegExp") {
value = {
__enc__: true,
type: "regexp",
@@ -974,7 +986,7 @@ function encodeObject(msg,opts) {
if (e.name) {
errorMsg.name = e.name;
}
if (e.hasOwnProperty('message')) {
if (hasOwnProperty.call(e, 'message')) {
errorMsg.message = 'encodeObject Error: ['+e.message + '] Value: '+util.inspect(msg.msg);
} else {
errorMsg.message = 'encodeObject Error: ['+e.toString() + '] Value: '+util.inspect(msg.msg);