# antelm-dev/electron-ipc-module architecture

> Architecture diagram of the GitHub repository antelm-dev/electron-ipc-module, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/antelm-dev/electron-ipc-module
- Repository: https://github.com/antelm-dev/electron-ipc-module

## Overview

#### Purpose and package surfaces
`electron-ipc-module` is an ESM TypeScript library for modular, type-safe Electron main/renderer IPC. It combines main-process channel registration and lifecycle management with static generation of a restricted, typed preload bridge. Public runtime exports originate at `src/index.ts`; command-line use enters through `src/cli.ts`; Rollup/Vite integration enters through `src/rollup-plugin.ts`.

#### Main-process IPC module runtime
`defineIpcModule` and channel helpers model prefixed handler/listener registrations over Electron `ipcMain`. They support request/response (`handle`, `handleOnce`), fire-and-forget (`listen`, `listenOnce`), typed emitted events, authorization, validation, cleanup, and registration rollback.
- `src/runtime/ipc-module.ts`
- `src/shared/types/runtime.ts`
- `src/shared/types/bridge.ts`

The key boundary is Electron structured-clone serialization: channel argument and handler-return types are constrained before code reaches the generated renderer API. Runtime authorization and validation remain necessary because renderer types are not trusted at runtime.

#### Module lifecycle and collision control
The IPC container owns multiple named module registrations and serializes all lifecycle mutations through a FIFO queue. It loads/replaces modules, offers transactional `loadAll`, unloads/disposes registrations, tracks committed channel names, and prevents physical-channel collisions across modules. Observer failures are isolated from lifecycle operations.
- `src/runtime/ipc-container.ts`
- `src/runtime/ipc-module.ts`

Control flow: application main code builds modules with injected services, then calls container `load`/`loadAll`; the container invokes module registration against Electron IPC and retains cleanup callbacks/channels only after successful commitment.

#### Static bridge analysis
The analyzer uses the TypeScript compiler at runtime to discover and inspect `*.ipc.ts` files. It extracts module names, channel definitions, argument/return types, and declared emitted events; it reports unsupported or ambiguous source shapes and scopes type diagnostics to sources reachable from IPC modules.
- `src/bridge/ipc-bridge-analyzer.ts`
- `src/shared/ts-utils.ts`
- `src/shared/types/bridge.ts`

This is a build-time boundary: exactly declared module APIs become renderer-accessible; arbitrary `ipcRenderer` access is not exposed.

#### Bridge generation and preload boundary
The generator converts analyzed IPC metadata into TypeScript defining a nested bridge: invoke wrappers for handlers, send wrappers for listeners, and subscription/unsubscription wrappers for events. Optional exposure emits `contextBridge.exposeInMainWorld` and a matching `Window` declaration.
- `src/bridge/ipc-bridge-generator.ts`
- `src/bridge/ipc-bridge.ts`
- `example/generated/ipc-bridge.ts`

Generated code is intended for Electron preload, then must be bundled as a self-contained CommonJS artifact when sandboxed. Electron is the material external runtime; expected security settings are context isolation enabled and Node integration disabled.

#### Build and CLI integration
The Rollup plugin invokes generation during builds and watches IPC sources; its compatible plugin shape also supports Vite. The CLI provides standalone generate, stale-output check, and watch workflows, reusing the same analysis/generation pipeline.
- `src/rollup-plugin.ts`
- `src/cli.ts`
- `src/bridge/ipc-bridge-generator.ts`

#### Runnable integration example
The example demonstrates the full application flow: main defines/loads IPC modules, generated bridge is imported by preload and exposed to the renderer, and renderer calls only that bridge.
- `example/main.ts`
- `example/ipc/greeting.ipc.ts`
- `example/preload.ts`
- `example/rollup.config.js`

Main entry points: [`src/runtime/ipc-module.ts`](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/runtime/ipc-module.ts) (IPC module), [`src/runtime/ipc-container.ts`](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/runtime/ipc-container.ts) (IPC container), [`src/bridge/ipc-bridge-analyzer.ts`](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/bridge/ipc-bridge-analyzer.ts) (IPC bridge analyzer), [`src/bridge/ipc-bridge.ts`](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/bridge/ipc-bridge.ts) (Bridge orchestration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Main-process runtime"]
  node_public_api["Public runtime API<br/>ESM exports<br/>[index.ts]"]
  node_ipc_module["IPC module<br/>registration runtime<br/>[ipc-module.ts]"]
  node_ipc_container["IPC container<br/>lifecycle manager<br/>[ipc-container.ts]"]
  node_runtime_types["Runtime IPC types<br/>type contracts<br/>[runtime.ts]"]
  node_electron_main{{"Electron ipcMain<br/>external runtime"}}
end

subgraph group_bridge["Bridge toolchain"]
  node_bridge_types["Bridge type contracts<br/>[bridge.ts]"]
  node_cli["CLI<br/>generation command<br/>[cli.ts]"]
  node_rollup_plugin["Rollup/Vite plugin<br/>build plugin<br/>[rollup-plugin.ts]"]
  node_analyzer["IPC bridge analyzer<br/>TypeScript analysis"]
  node_ts_utils["TypeScript utilities<br/>compiler support<br/>[ts-utils.ts]"]
  node_generator["Bridge generator<br/>code generator"]
  node_bridge_api["Bridge orchestration<br/>generation API<br/>[ipc-bridge.ts]"]
end

subgraph group_example["Electron example"]
  node_ipc_source["Greeting IPC declaration<br/>IPC source module<br/>[greeting.ipc.ts]"]
  node_main_app["Example main process<br/>Electron main<br/>[main.ts]"]
  node_generated_bridge["Generated preload bridge<br/>generated TypeScript<br/>[ipc-bridge.ts]"]
  node_preload["Example preload<br/>Electron preload<br/>[preload.ts]"]
  node_renderer["Renderer application<br/>Electron renderer"]
  node_example_build["Example Rollup build<br/>preload bundling<br/>[rollup.config.js]"]
end

node_public_api -->|"exports"| node_ipc_module
node_public_api -->|"exports"| node_ipc_container
node_ipc_module -->|"uses typed contracts"| node_runtime_types
node_ipc_module -->|"aligns declared API"| node_bridge_types
node_ipc_container -->|"loads and unloads"| node_ipc_module
node_ipc_container -->|"registers committed channels"| node_electron_main
node_main_app -->|"defines"| node_ipc_module
node_main_app -->|"load / loadAll"| node_ipc_container
node_cli -->|"runs"| node_bridge_api
node_rollup_plugin -->|"runs during build"| node_bridge_api
node_bridge_api -->|"analyzes IPC sources"| node_analyzer
node_bridge_api -->|"generates bridge"| node_generator
node_analyzer -->|"uses compiler helpers"| node_ts_utils
node_analyzer -->|"produces metadata"| node_bridge_types
node_ipc_source -->|"analyzed"| node_analyzer
node_generator -->|"emits"| node_generated_bridge
node_example_build -->|"configures"| node_rollup_plugin
node_example_build -->|"bundles"| node_preload
node_preload -->|"imports"| node_generated_bridge
node_generated_bridge -->|"exposes restricted API"| node_renderer
node_renderer -->|"invokes declared channels"| node_electron_main

click node_public_api "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/index.ts"
click node_ipc_module "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/runtime/ipc-module.ts"
click node_ipc_container "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/runtime/ipc-container.ts"
click node_runtime_types "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/shared/types/runtime.ts"
click node_bridge_types "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/shared/types/bridge.ts"
click node_cli "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/cli.ts"
click node_rollup_plugin "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/rollup-plugin.ts"
click node_analyzer "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/bridge/ipc-bridge-analyzer.ts"
click node_ts_utils "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/shared/ts-utils.ts"
click node_generator "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/bridge/ipc-bridge-generator.ts"
click node_bridge_api "https://github.com/antelm-dev/electron-ipc-module/blob/main/src/bridge/ipc-bridge.ts"
click node_ipc_source "https://github.com/antelm-dev/electron-ipc-module/blob/main/example/ipc/greeting.ipc.ts"
click node_main_app "https://github.com/antelm-dev/electron-ipc-module/blob/main/example/main.ts"
click node_generated_bridge "https://github.com/antelm-dev/electron-ipc-module/blob/main/example/generated/ipc-bridge.ts"
click node_preload "https://github.com/antelm-dev/electron-ipc-module/blob/main/example/preload.ts"
click node_example_build "https://github.com/antelm-dev/electron-ipc-module/blob/main/example/rollup.config.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_api,node_ipc_module,node_ipc_container,node_runtime_types,node_electron_main toneBlue
class node_bridge_types,node_cli,node_rollup_plugin,node_analyzer,node_ts_utils,node_generator,node_bridge_api toneAmber
class node_ipc_source,node_main_app,node_generated_bridge,node_preload,node_renderer,node_example_build toneMint
```

## Components

### Main-process runtime

- [Public runtime API](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/index.ts): ESM exports · Library runtime entry point. · `src/index.ts`
- [IPC module](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/runtime/ipc-module.ts): registration runtime · Defines prefixed handlers, listeners, events, authorization, and validation. · `src/runtime/ipc-module.ts`
- [IPC container](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/runtime/ipc-container.ts): lifecycle manager · Serializes module lifecycle changes and commits collision-free registrations. · `src/runtime/ipc-container.ts`
- [Runtime IPC types](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/shared/types/runtime.ts): type contracts · Constrains channel payloads and returns to structured-clone-safe values. · `src/shared/types/runtime.ts`
- **Electron ipcMain**: external runtime

### Bridge toolchain

- [Bridge type contracts](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/shared/types/bridge.ts): type contracts · `src/shared/types/bridge.ts`
- [CLI](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/cli.ts): generation command · Provides generate, stale-check, and watch workflows. · `src/cli.ts`
- [Rollup/Vite plugin](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/rollup-plugin.ts): build plugin · Generates and watches bridge output during builds. · `src/rollup-plugin.ts`
- [IPC bridge analyzer](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/bridge/ipc-bridge-analyzer.ts): TypeScript analysis · Discovers IPC modules and extracts their declared API metadata. · `src/bridge/ipc-bridge-analyzer.ts`
- [TypeScript utilities](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/shared/ts-utils.ts): compiler support · `src/shared/ts-utils.ts`
- [Bridge generator](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/bridge/ipc-bridge-generator.ts): code generator · Emits invoke, send, and event subscription bridge wrappers. · `src/bridge/ipc-bridge-generator.ts`
- [Bridge orchestration](https://github.com/antelm-dev/electron-ipc-module/blob/main/src/bridge/ipc-bridge.ts): generation API · `src/bridge/ipc-bridge.ts`

### Electron example

- [Greeting IPC declaration](https://github.com/antelm-dev/electron-ipc-module/blob/main/example/ipc/greeting.ipc.ts): IPC source module · Statically declared renderer-accessible API. · `example/ipc/greeting.ipc.ts`
- [Example main process](https://github.com/antelm-dev/electron-ipc-module/blob/main/example/main.ts): Electron main · Builds modules with services and loads them into the container. · `example/main.ts`
- [Generated preload bridge](https://github.com/antelm-dev/electron-ipc-module/blob/main/example/generated/ipc-bridge.ts): generated TypeScript · Restricted nested renderer API, optionally exposed through contextBridge. · `example/generated/ipc-bridge.ts`
- [Example preload](https://github.com/antelm-dev/electron-ipc-module/blob/main/example/preload.ts): Electron preload · Imports the generated bridge before renderer execution. · `example/preload.ts`
- **Renderer application**: Electron renderer · Uses only the exposed typed bridge.
- [Example Rollup build](https://github.com/antelm-dev/electron-ipc-module/blob/main/example/rollup.config.js): preload bundling · Bundles preload as a self-contained CommonJS artifact. · `example/rollup.config.js`

## Connections

- Public runtime API → IPC module: exports
- Public runtime API → IPC container: exports
- IPC module → Runtime IPC types: uses typed contracts
- IPC module → Bridge type contracts: aligns declared API
- IPC container → IPC module: loads and unloads
- IPC container → Electron ipcMain: registers committed channels
- Example main process → IPC module: defines
- Example main process → IPC container: load / loadAll
- CLI → Bridge orchestration: runs
- Rollup/Vite plugin → Bridge orchestration: runs during build
- Bridge orchestration → IPC bridge analyzer: analyzes IPC sources
- Bridge orchestration → Bridge generator: generates bridge
- IPC bridge analyzer → TypeScript utilities: uses compiler helpers
- IPC bridge analyzer → Bridge type contracts: produces metadata
- Greeting IPC declaration → IPC bridge analyzer: analyzed
- Bridge generator → Generated preload bridge: emits
- Example Rollup build → Rollup/Vite plugin: configures
- Example Rollup build → Example preload: bundles
- Example preload → Generated preload bridge: imports
- Generated preload bridge → Renderer application: exposes restricted API
- Renderer application → Electron ipcMain: invokes declared channels
