Skip to content

Commit 7ae17b6

Browse files
authored
🔨 chore: add electron server ipc (lobehub#7246)
1 parent 4309730 commit 7ae17b6

File tree

9 files changed

+604
-0
lines changed

9 files changed

+604
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# @lobechat/electron-server-ipc
2+
3+
LobeHub 的 Electron 应用与服务端之间的 IPC(进程间通信)模块,提供可靠的跨进程通信能力。
4+
5+
## 📝 简介
6+
7+
`@lobechat/electron-server-ipc` 是 LobeHub 桌面应用的核心组件,负责处理 Electron 进程与 nextjs 服务端之间的通信。它提供了一套简单而健壮的 API,用于在不同进程间传递数据和执行远程方法调用。
8+
9+
## 🛠️ 核心功能
10+
11+
- **可靠的 IPC 通信**: 基于 Socket 的通信机制,确保跨进程通信的稳定性和可靠性
12+
- **自动重连机制**: 客户端具备断线重连功能,提高应用稳定性
13+
- **类型安全**: 使用 TypeScript 提供完整的类型定义,确保 API 调用的类型安全
14+
- **跨平台支持**: 同时支持 Windows、macOS 和 Linux 平台
15+
16+
## 🧩 核心组件
17+
18+
### IPC 服务端 (ElectronIPCServer)
19+
20+
负责监听客户端请求并响应,通常运行在 Electron 的主进程中:
21+
22+
```typescript
23+
import { ElectronIPCEventHandler, ElectronIPCServer } from '@lobechat/electron-server-ipc';
24+
25+
// 定义处理函数
26+
const eventHandler: ElectronIPCEventHandler = {
27+
getDatabasePath: async () => {
28+
return '/path/to/database';
29+
},
30+
// 其他处理函数...
31+
};
32+
33+
// 创建并启动服务器
34+
const server = new ElectronIPCServer(eventHandler);
35+
server.start();
36+
```
37+
38+
### IPC 客户端 (ElectronIpcClient)
39+
40+
负责连接到服务端并发送请求,通常在服务端(如 Next.js 服务)中使用:
41+
42+
```typescript
43+
import { ElectronIPCMethods, ElectronIpcClient } from '@lobechat/electron-server-ipc';
44+
45+
// 创建客户端
46+
const client = new ElectronIpcClient();
47+
48+
// 发送请求
49+
const dbPath = await client.sendRequest(ElectronIPCMethods.getDatabasePath);
50+
```
51+
52+
## 📌 说明
53+
54+
这是 LobeHub 的内部模块 (`"private": true`),专为 LobeHub 桌面应用设计,不作为独立包发布。
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@lobechat/electron-server-ipc",
3+
"version": "1.0.0",
4+
"private": true,
5+
"main": "src/index.ts",
6+
"types": "src/index.ts"
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const SOCK_FILE = 'lobehub-electron-ipc.sock';
2+
3+
export const SOCK_INFO_FILE = 'lobehub-electron-ipc-info.json';
4+
5+
export const WINDOW_PIPE_FILE = '\\\\.\\pipe\\lobehub-electron-ipc';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './ipcClient';
2+
export * from './ipcServer';
3+
export * from './types';
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import fs from 'node:fs';
2+
import net from 'node:net';
3+
import os from 'node:os';
4+
import path from 'node:path';
5+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6+
7+
import { ElectronIpcClient } from './ipcClient';
8+
import { ElectronIPCMethods } from './types';
9+
10+
// Mock node modules
11+
vi.mock('node:fs');
12+
vi.mock('node:net');
13+
vi.mock('node:os');
14+
vi.mock('node:path');
15+
16+
describe('ElectronIpcClient', () => {
17+
// Mock data
18+
const mockTempDir = '/mock/temp/dir';
19+
const mockSocketInfoPath = '/mock/temp/dir/lobehub-electron-ipc-info.json';
20+
const mockSocketInfo = { socketPath: '/mock/socket/path' };
21+
22+
// Mock socket
23+
const mockSocket = {
24+
on: vi.fn(),
25+
write: vi.fn(),
26+
end: vi.fn(),
27+
};
28+
29+
beforeEach(() => {
30+
// Use fake timers
31+
vi.useFakeTimers();
32+
33+
// Reset all mocks
34+
vi.resetAllMocks();
35+
36+
// Setup common mocks
37+
vi.mocked(os.tmpdir).mockReturnValue(mockTempDir);
38+
vi.mocked(path.join).mockImplementation((...args) => args.join('/'));
39+
vi.mocked(net.createConnection).mockReturnValue(mockSocket as unknown as net.Socket);
40+
41+
// Mock console methods
42+
vi.spyOn(console, 'error').mockImplementation(() => {});
43+
vi.spyOn(console, 'log').mockImplementation(() => {});
44+
});
45+
46+
afterEach(() => {
47+
vi.restoreAllMocks();
48+
vi.useRealTimers();
49+
});
50+
51+
describe('initialization', () => {
52+
it('should initialize with socket path from info file if it exists', () => {
53+
// Setup
54+
vi.mocked(fs.existsSync).mockReturnValue(true);
55+
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(mockSocketInfo));
56+
57+
// Execute
58+
new ElectronIpcClient();
59+
60+
// Verify
61+
expect(fs.existsSync).toHaveBeenCalledWith(mockSocketInfoPath);
62+
expect(fs.readFileSync).toHaveBeenCalledWith(mockSocketInfoPath, 'utf8');
63+
});
64+
65+
it('should initialize with default socket path if info file does not exist', () => {
66+
// Setup
67+
vi.mocked(fs.existsSync).mockReturnValue(false);
68+
69+
// Execute
70+
new ElectronIpcClient();
71+
72+
// Verify
73+
expect(fs.existsSync).toHaveBeenCalledWith(mockSocketInfoPath);
74+
expect(fs.readFileSync).not.toHaveBeenCalled();
75+
76+
// Test platform-specific behavior
77+
const originalPlatform = process.platform;
78+
Object.defineProperty(process, 'platform', { value: 'win32' });
79+
new ElectronIpcClient();
80+
Object.defineProperty(process, 'platform', { value: originalPlatform });
81+
});
82+
83+
it('should handle initialization errors gracefully', () => {
84+
// Setup - Mock the error
85+
vi.mocked(fs.existsSync).mockImplementation(() => {
86+
throw new Error('Mock file system error');
87+
});
88+
89+
// Execute
90+
new ElectronIpcClient();
91+
92+
// Verify
93+
expect(console.error).toHaveBeenCalledWith(
94+
'Failed to initialize IPC client:',
95+
expect.objectContaining({ message: 'Mock file system error' }),
96+
);
97+
});
98+
});
99+
100+
describe('connection and request handling', () => {
101+
let client: ElectronIpcClient;
102+
103+
beforeEach(() => {
104+
// Setup a client with a known socket path
105+
vi.mocked(fs.existsSync).mockReturnValue(true);
106+
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(mockSocketInfo));
107+
client = new ElectronIpcClient();
108+
109+
// Reset socket mocks for each test
110+
mockSocket.on.mockReset();
111+
mockSocket.write.mockReset();
112+
113+
// Default implementation for socket.on
114+
mockSocket.on.mockImplementation((event, callback) => {
115+
return mockSocket;
116+
});
117+
118+
// Default implementation for socket.write
119+
mockSocket.write.mockImplementation((data, callback) => {
120+
if (callback) callback();
121+
return true;
122+
});
123+
});
124+
125+
it('should handle connection errors', async () => {
126+
// Start request - but don't await it yet
127+
const requestPromise = client.sendRequest(ElectronIPCMethods.getDatabasePath);
128+
129+
// Find the error event handler
130+
const errorCallArgs = mockSocket.on.mock.calls.find((call) => call[0] === 'error');
131+
if (errorCallArgs && typeof errorCallArgs[1] === 'function') {
132+
const errorHandler = errorCallArgs[1];
133+
134+
// Trigger the error handler
135+
errorHandler(new Error('Connection error'));
136+
}
137+
138+
// Now await the promise
139+
await expect(requestPromise).rejects.toThrow('Connection error');
140+
});
141+
142+
it('should handle write errors', async () => {
143+
// Setup connection callback
144+
let connectionCallback: Function | undefined;
145+
vi.mocked(net.createConnection).mockImplementation((path, callback) => {
146+
connectionCallback = callback as Function;
147+
return mockSocket as unknown as net.Socket;
148+
});
149+
150+
// Setup write to fail
151+
mockSocket.write.mockImplementation((data, callback) => {
152+
if (callback) callback(new Error('Write error'));
153+
return true;
154+
});
155+
156+
// Start request
157+
const requestPromise = client.sendRequest(ElectronIPCMethods.getDatabasePath);
158+
159+
// Simulate connection established
160+
if (connectionCallback) connectionCallback();
161+
162+
// Now await the promise
163+
await expect(requestPromise).rejects.toThrow('Write error');
164+
});
165+
});
166+
167+
describe('close method', () => {
168+
let client: ElectronIpcClient;
169+
170+
beforeEach(() => {
171+
// Setup a client with a known socket path
172+
vi.mocked(fs.existsSync).mockReturnValue(true);
173+
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(mockSocketInfo));
174+
client = new ElectronIpcClient();
175+
176+
// Setup socket.on
177+
mockSocket.on.mockImplementation((event, callback) => {
178+
return mockSocket;
179+
});
180+
});
181+
182+
it('should close the socket connection', async () => {
183+
// Setup connection callback
184+
let connectionCallback: Function | undefined;
185+
vi.mocked(net.createConnection).mockImplementation((path, callback) => {
186+
connectionCallback = callback as Function;
187+
return mockSocket as unknown as net.Socket;
188+
});
189+
190+
// Start a request to establish connection (but don't wait for it)
191+
const requestPromise = client.sendRequest(ElectronIPCMethods.getDatabasePath).catch(() => {}); // Ignore any errors
192+
193+
// Simulate connection
194+
if (connectionCallback) connectionCallback();
195+
196+
// Close the connection
197+
client.close();
198+
199+
// Verify
200+
expect(mockSocket.end).toHaveBeenCalled();
201+
});
202+
203+
it('should handle close when not connected', () => {
204+
// Close without connecting
205+
client.close();
206+
207+
// Verify no errors
208+
expect(mockSocket.end).not.toHaveBeenCalled();
209+
});
210+
});
211+
});

0 commit comments

Comments
 (0)