From 6a7f36bca44158159ddaa9db77a7ca4af7e0f948 Mon Sep 17 00:00:00 2001 From: Marcus K <36983163+MK-2001@users.noreply.github.com> Date: Tue, 27 Jan 2026 16:47:27 +0100 Subject: [PATCH 1/4] Implement safeStringify for circular reference handling Added a safeStringify method to handle circular references in JSON. --- telegrambot/nodes/out-node.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/telegrambot/nodes/out-node.js b/telegrambot/nodes/out-node.js index 438c5ee..7253176 100644 --- a/telegrambot/nodes/out-node.js +++ b/telegrambot/nodes/out-node.js @@ -1,3 +1,20 @@ +// safely handles circular references +JSON.safeStringify = (obj, indent = 2) => { + let cache = []; + const retVal = JSON.stringify( + obj, + (key, value) => + typeof value === "object" && value !== null + ? cache.includes(value) + ? undefined // Duplicate reference found, discard key + : cache.push(value) && value // Store value in our collection + : value, + indent + ); + cache = null; + return retVal; +}; + module.exports = function (RED) { const path = require('path'); const { pipeline } = require('stream'); @@ -83,7 +100,7 @@ module.exports = function (RED) { }; this.processError = function (exception, msg, nodeSend, nodeDone) { - let errorMessage = 'Caught exception in sender node:\r\n' + exception + '\r\nwhen processing message: \r\n' + JSON.stringify(msg); + let errorMessage = 'Caught exception in sender node:\r\n' + exception + '\r\nwhen processing message: \r\n' + JSON.safeStringify(msg); node.status({ fill: 'red', From e167b8ae20e6f781945f6d76d9736bad30ac42b3 Mon Sep 17 00:00:00 2001 From: Marcus K <36983163+MK-2001@users.noreply.github.com> Date: Tue, 27 Jan 2026 16:53:28 +0100 Subject: [PATCH 2/4] Refactor JSON.safeStringify for consistency --- telegrambot/nodes/out-node.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/telegrambot/nodes/out-node.js b/telegrambot/nodes/out-node.js index 7253176..06bed55 100644 --- a/telegrambot/nodes/out-node.js +++ b/telegrambot/nodes/out-node.js @@ -1,18 +1,18 @@ // safely handles circular references JSON.safeStringify = (obj, indent = 2) => { - let cache = []; - const retVal = JSON.stringify( - obj, - (key, value) => - typeof value === "object" && value !== null - ? cache.includes(value) - ? undefined // Duplicate reference found, discard key - : cache.push(value) && value // Store value in our collection - : value, - indent - ); - cache = null; - return retVal; + let cache = []; + const retVal = JSON.stringify( + obj, + (key, value) => + typeof value === 'object' && value !== null + ? cache.includes(value) + ? undefined // Duplicate reference found, discard key + : cache.push(value) && value // Store value in our collection + : value, + indent + ); + cache = null; + return retVal; }; module.exports = function (RED) { From 6d95479a6bdcefe3a0a073d7dcb3e76f15d15c58 Mon Sep 17 00:00:00 2001 From: Marcus K <36983163+MK-2001@users.noreply.github.com> Date: Tue, 27 Jan 2026 16:55:30 +0100 Subject: [PATCH 3/4] Update out-node.js --- telegrambot/nodes/out-node.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/telegrambot/nodes/out-node.js b/telegrambot/nodes/out-node.js index 06bed55..b028418 100644 --- a/telegrambot/nodes/out-node.js +++ b/telegrambot/nodes/out-node.js @@ -6,9 +6,9 @@ JSON.safeStringify = (obj, indent = 2) => { (key, value) => typeof value === 'object' && value !== null ? cache.includes(value) - ? undefined // Duplicate reference found, discard key - : cache.push(value) && value // Store value in our collection - : value, + ? undefined // Duplicate reference found, discard key + : cache.push(value) && value // Store value in our collection + : value, indent ); cache = null; From 39bc7db70fbb38084d5310ea805ac7ee2fb551c4 Mon Sep 17 00:00:00 2001 From: Marcus K <36983163+MK-2001@users.noreply.github.com> Date: Tue, 27 Jan 2026 16:56:54 +0100 Subject: [PATCH 4/4] Fix indentation and formatting in out-node.js --- telegrambot/nodes/out-node.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telegrambot/nodes/out-node.js b/telegrambot/nodes/out-node.js index b028418..e7211b8 100644 --- a/telegrambot/nodes/out-node.js +++ b/telegrambot/nodes/out-node.js @@ -7,8 +7,8 @@ JSON.safeStringify = (obj, indent = 2) => { typeof value === 'object' && value !== null ? cache.includes(value) ? undefined // Duplicate reference found, discard key - : cache.push(value) && value // Store value in our collection - : value, + : cache.push(value) && value // Store value in our collection + : value, indent ); cache = null;