From fd64d244708669d5e06f3ad5031343d5a0458bf5 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Fri, 2 Feb 2024 23:38:12 +0100 Subject: [PATCH] improve serialize safe --- src/main/utils/sendToRender.js | 48 ++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/main/utils/sendToRender.js b/src/main/utils/sendToRender.js index 73bf3fe..6fc3784 100644 --- a/src/main/utils/sendToRender.js +++ b/src/main/utils/sendToRender.js @@ -5,27 +5,35 @@ const forbidden = [ ] export default (event, data) => { - function serializeIpc(data) { - if (!data) { - return undefined + try { + function serializeIpc(data) { + if (!data) { + return undefined + } + + data = JSON.stringify(data) + + data = JSON.parse(data) + + const copy = lodash.cloneDeep(data) + + if (!Array.isArray(copy)) { + Object.keys(copy).forEach((key) => { + if (forbidden.includes(key)) { + delete copy[key] + } + + if (typeof copy[key] === "function") { + delete copy[key] + } + }) + } + + return copy } - const copy = lodash.cloneDeep(data) - - if (!Array.isArray(copy)) { - Object.keys(copy).forEach((key) => { - if (forbidden.includes(key)) { - delete copy[key] - } - - if (typeof copy[key] === "function") { - delete copy[key] - } - }) - } - - return copy + global.win.webContents.send(event, serializeIpc(data)) + } catch (error) { + console.error(error) } - - global.win.webContents.send(event, serializeIpc(data)) } \ No newline at end of file