import type { IpcRendererEvent } from 'electron'; import { contextBridge, ipcRenderer } from 'electron'; import log from 'electron-log/renderer'; // Initialize renderer logger log.transports.console.level = 'silly'; log.transports.console.format = '{h}:{i}:{s}.{ms} {text}'; // Whitelist of valid channels used for IPC communication (Send message from Renderer to Main) const mainAvailChannels: string[] = [ 'msgRequestGetVersion', 'msgOpenExternalLink', 'msgOpenFile', 'msgSelectDirectory', 'msgSelectFileOrFolder', 'msgLoadData', 'msgSaveData', 'msgExecuteTemplate', 'msgFindFilesInFolder', 'msgCheckForUpdates', 'msgDownloadUpdate', 'msgInstallUpdate', ]; const rendererAvailChannels: string[] = [ 'update-checking', 'update-available', 'update-not-available', 'update-download-progress', 'update-downloaded', 'update-error', ]; contextBridge.exposeInMainWorld('mainApi', { send: (channel: string, ...data: any[]): void => { if (mainAvailChannels.includes(channel)) { ipcRenderer.send.apply(null, [channel, ...data]); if (process.env.NODE_ENV === 'development') { log.silly(`[IPC_SEND::${channel}]`, { request: data }); } } else { throw new Error(`Unknown ipc channel name: ${channel}`); } }, on: ( channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void, ): void => { if (rendererAvailChannels.includes(channel)) { ipcRenderer.on(channel, listener); } else { throw new Error(`Unknown ipc channel name: ${channel}`); } }, once: ( channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void, ): void => { if (rendererAvailChannels.includes(channel)) { ipcRenderer.once(channel, listener); } else { throw new Error(`Unknown ipc channel name: ${channel}`); } }, off: ( channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void, ): void => { if (rendererAvailChannels.includes(channel)) { ipcRenderer.off(channel, listener); } else { throw new Error(`Unknown ipc channel name: ${channel}`); } }, invoke: async (channel: string, ...data: any[]): Promise => { if (mainAvailChannels.includes(channel)) { const result = await ipcRenderer.invoke.apply(null, [channel, ...data]); if (process.env.NODE_ENV === 'development') { log.silly(`[IPC_INVOKE::${channel}]`, { request: data, result, }); } return result; } throw new Error(`Unknown ipc channel name: ${channel}`); }, });