improve serialize safe

This commit is contained in:
SrGooglo 2024-02-02 23:38:12 +01:00
parent e7cd36a5b3
commit fd64d24470

View File

@ -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))
}