Skip to main content

Electron Layer

The main process and preload script are the core of OpenTree.

Main Process Responsibilities

// packages/main/src/main.ts

import { app, BrowserWindow, ipcMain } from 'electron'
import { registerIpcHandlers } from './ipc'
import { initDatabase } from './database'

app.whenReady().then(async () => {
// 1. Initialize database
await initDatabase()

// 2. Register IPC handlers
registerIpcHandlers()

// 3. Create windows
createWorldTreeWindow()
createMetaverseWindow()
})

IPC Channels

All IPC channels are exposed to the renderer process via contextBridge:

// packages/main/src/preload.ts

contextBridge.exposeInMainWorld('electronAPI', {
// Keychain
keychain: {
getAll: () => ipcRenderer.invoke('keychain-get-all'),
create: (data) => ipcRenderer.invoke('keychain-create', data),
update: (id, data) => ipcRenderer.invoke('keychain-update', id, data),
remove: (id) => ipcRenderer.invoke('keychain-delete', id),
},

// Favorites
getFavorites: () => ipcRenderer.invoke('get-favorites'),
addFavoriteManual: (data) => ipcRenderer.invoke('add-favorite-manual', data),
// ...
})

Security Policy

  • contextIsolation: true - Context isolation
  • nodeIntegration: false - Disable Node integration
  • preload script - Expose safe APIs via contextBridge