@lynker-desktop/electron-ipc 0.0.9-alpha.7 → 0.0.9-alpha.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/main/index.js CHANGED
@@ -1,8 +1,18 @@
1
1
  const events = require('events');
2
2
  const electron = require('electron');
3
+ const index = require('../common/index.js');
3
4
 
4
- // import { getRandomUUID } from '../common'
5
5
  exports.isInitialized = false;
6
+ const TIMEOUT = 1000 * 60 * 30; // 30分钟超时
7
+ /**
8
+ * 主进程 IPC 通信类
9
+ * 负责处理主进程与渲染进程之间的消息通信
10
+ * 使用单例模式确保全局唯一实例
11
+ *
12
+ * 修复说明:
13
+ * - 为每个请求生成唯一的 requestId,解决并发请求数据错乱问题
14
+ * - 确保请求和响应能够正确匹配,避免多个并发请求互相干扰
15
+ */
6
16
  class MainIPC {
7
17
  constructor() {
8
18
  this.eventEmitter = new events.EventEmitter();
@@ -14,54 +24,109 @@ class MainIPC {
14
24
  }
15
25
  /**
16
26
  * 发送给主进程消息
17
- * @param channel
18
- * @param args
19
- * @returns
27
+ * 使用唯一的 requestId 确保并发请求不会互相干扰
28
+ *
29
+ * @param channel 消息通道名称
30
+ * @param args 传递给处理器的参数
31
+ * @returns Promise<any> 返回处理结果
32
+ *
33
+ * 修复说明:
34
+ * - 为每个请求生成唯一的 requestId
35
+ * - 监听 `${channel}-reply-${requestId}` 事件,确保只接收对应请求的回复
36
+ * - 发送请求时包含 requestId,让处理器知道如何回复
20
37
  */
21
38
  async invokeMain(channel, ...args) {
22
- return new Promise(resolve => {
23
- this.eventEmitter.once(`${channel}-reply`, result => {
24
- resolve(result);
25
- });
26
- this.eventEmitter.emit(channel, ...args);
27
- });
28
- }
29
- /**
30
- * 处理一次性主进程发送过来的消息
31
- * @param channel
32
- * @param handler
33
- */
34
- handleMainOnce(channel, handler) {
35
- this.eventEmitter.once(channel, async (...args) => {
36
- const result = await handler(...args);
37
- this.eventEmitter.emit(`${channel}-reply`, result);
39
+ return new Promise((resolve, reject) => {
40
+ const requestId = index.getRandomUUID();
41
+ const replyChannel = `${channel}-reply-${requestId}`;
42
+ let isResolved = false;
43
+ const replyHandler = (result) => {
44
+ if (!isResolved) {
45
+ isResolved = true;
46
+ this.eventEmitter.removeListener(replyChannel, replyHandler);
47
+ clearTimeout(timeoutId);
48
+ resolve(result);
49
+ }
50
+ };
51
+ this.eventEmitter.once(replyChannel, replyHandler);
52
+ // 添加超时机制,确保监听器最终会被清理
53
+ const timeoutId = setTimeout(() => {
54
+ if (!isResolved) {
55
+ isResolved = true;
56
+ this.eventEmitter.removeListener(replyChannel, replyHandler);
57
+ reject(new Error(`IPC request timeout: ${channel} (30s)`));
58
+ }
59
+ }, TIMEOUT); // 30秒超时
60
+ this.eventEmitter.emit(channel, requestId, ...args);
38
61
  });
39
62
  }
40
63
  /**
41
64
  * 处理主进程发送过来的消息
42
- * @param channel
43
- * @param handler
65
+ * 持续监听指定通道的消息
66
+ *
67
+ * @param channel 消息通道名称
68
+ * @param handler 处理函数,接收除 requestId 外的所有参数
69
+ *
70
+ * 修复说明:
71
+ * - 接收 requestId 作为第一个参数
72
+ * - 使用 `${channel}-reply-${requestId}` 发送回复,确保回复给正确的请求
73
+ * - 支持多个并发请求,每个请求都有独立的回复通道
44
74
  */
45
75
  handleMain(channel, handler) {
46
- this.eventEmitter.on(channel, async (...args) => {
76
+ this.eventEmitter.on(channel, async (requestId, ...args) => {
47
77
  const result = await handler(...args);
48
- this.eventEmitter.emit(`${channel}-reply`, result);
78
+ this.eventEmitter.emit(`${channel}-reply-${requestId}`, result);
49
79
  });
80
+ return {
81
+ cancel: () => {
82
+ this.eventEmitter.removeAllListeners(channel);
83
+ }
84
+ };
50
85
  }
51
86
  /**
52
87
  * 发送给渲染进程消息
53
- * @param webContents
54
- * @param channel
55
- * @param args
56
- * @returns
88
+ * 使用唯一的 requestId 确保并发请求不会互相干扰
89
+ *
90
+ * @param webContents 目标渲染进程的 WebContents 对象
91
+ * @param channel 消息通道名称
92
+ * @param args 传递给渲染进程的参数
93
+ * @returns Promise<any> 返回渲染进程的处理结果
94
+ *
95
+ * 修复说明:
96
+ * - 为每个请求生成唯一的 requestId
97
+ * - 监听 `${channel}-reply-${requestId}` 事件,确保只接收对应请求的回复
98
+ * - 发送请求时包含 requestId,让渲染进程知道如何回复
99
+ * - 等待渲染进程加载完成后再发送消息,确保消息能够被正确接收
57
100
  */
58
101
  invokeRenderer(webContents, channel, ...args) {
59
- return new Promise(resolve => {
102
+ return new Promise((resolve, reject) => {
103
+ const requestId = index.getRandomUUID();
104
+ const replyChannel = `${channel}-reply-${requestId}`;
105
+ let isResolved = false;
60
106
  const sendMessage = () => {
61
- electron.ipcMain.once(`${channel}-reply`, (_event, result) => {
62
- resolve(result);
63
- });
64
- webContents.send(channel, ...args);
107
+ // 检查 webContents 是否已销毁
108
+ if (webContents.isDestroyed()) {
109
+ reject(new Error('WebContents has been destroyed'));
110
+ return;
111
+ }
112
+ const replyHandler = (_event, result) => {
113
+ if (!isResolved) {
114
+ isResolved = true;
115
+ electron.ipcMain.removeListener(replyChannel, replyHandler);
116
+ clearTimeout(timeoutId);
117
+ resolve(result);
118
+ }
119
+ };
120
+ electron.ipcMain.once(replyChannel, replyHandler);
121
+ // 添加超时机制,确保监听器最终会被清理
122
+ const timeoutId = setTimeout(() => {
123
+ if (!isResolved) {
124
+ isResolved = true;
125
+ electron.ipcMain.removeListener(replyChannel, replyHandler);
126
+ reject(new Error(`IPC request timeout: ${channel} (30s)`));
127
+ }
128
+ }, TIMEOUT); // 30秒超时
129
+ webContents.send(channel, requestId, ...args);
65
130
  };
66
131
  // 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息
67
132
  if (webContents.isLoading()) {
@@ -75,19 +140,55 @@ class MainIPC {
75
140
  });
76
141
  }
77
142
  /**
78
- * 发送给所有渲染进程消息
79
- * @param channel
80
- * @param args
81
- * @returns
82
- */
143
+ * 发送给所有渲染进程消息
144
+ * 向所有渲染进程发送消息并收集所有响应
145
+ *
146
+ * @param channel 消息通道名称
147
+ * @param args 传递给所有渲染进程的参数
148
+ * @returns Promise<any[]> 返回所有渲染进程的处理结果数组
149
+ *
150
+ * 修复说明:
151
+ * - 为每个请求生成唯一的 requestId
152
+ * - 收集所有渲染进程的响应,当所有响应都收到时才 resolve
153
+ * - 使用 `${channel}-reply-${requestId}` 确保只接收对应请求的回复
154
+ * - 如果没有渲染进程,直接返回空数组
155
+ * - 等待每个渲染进程加载完成后再发送消息
156
+ */
83
157
  invokeAllRenderer(channel, ...args) {
84
158
  return new Promise(resolve => {
159
+ const requestId = index.getRandomUUID();
160
+ let responseCount = 0;
161
+ const totalWebContents = electron.webContents.getAllWebContents().length;
162
+ if (totalWebContents === 0) {
163
+ resolve([]);
164
+ return;
165
+ }
166
+ const responses = [];
167
+ const replyChannel = `${channel}-reply-${requestId}`;
168
+ // 只注册一个监听器,避免内存泄漏
169
+ // 使用 on 而不是 once,因为需要接收多个响应
170
+ const replyHandler = (_event, result) => {
171
+ responses.push(result);
172
+ responseCount++;
173
+ if (responseCount === totalWebContents) {
174
+ // 清理监听器
175
+ electron.ipcMain.removeListener(replyChannel, replyHandler);
176
+ clearTimeout(timeoutId);
177
+ resolve(responses);
178
+ }
179
+ };
180
+ electron.ipcMain.on(replyChannel, replyHandler);
181
+ // 添加超时机制,确保监听器最终会被清理
182
+ const timeoutId = setTimeout(() => {
183
+ electron.ipcMain.removeListener(replyChannel, replyHandler);
184
+ if (responseCount < totalWebContents) {
185
+ // 超时后返回已收到的响应
186
+ resolve(responses);
187
+ }
188
+ }, TIMEOUT); // 30秒超时
85
189
  electron.webContents.getAllWebContents().forEach(webContent => {
86
190
  const sendMessage = () => {
87
- electron.ipcMain.once(`${channel}-reply`, (_event, result) => {
88
- resolve(result);
89
- });
90
- webContent.send(channel, ...args);
191
+ webContent.send(channel, requestId, ...args);
91
192
  };
92
193
  // 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息
93
194
  if (webContent.isLoading()) {
@@ -101,39 +202,36 @@ class MainIPC {
101
202
  });
102
203
  });
103
204
  }
104
- /**
105
- * 处理一次性渲染进程发送过来的消息
106
- * @param channel
107
- * @param handler
108
- */
109
- handleRendererOnce(channel, handler) {
110
- electron.ipcMain.once(channel, async (_event, ...args) => {
111
- const result = await handler(...args);
112
- _event.sender.send(`${channel}-reply`, result);
113
- });
114
- }
115
205
  /**
116
206
  * 处理渲染进程发送过来的消息
117
- * @param channel
118
- * @param handler
207
+ * 持续监听指定通道的消息,支持超时处理
208
+ *
209
+ * @param channel 消息通道名称
210
+ * @param handler 处理函数,接收除 requestId 外的所有参数
211
+ *
212
+ * 修复说明:
213
+ * - 接收 requestId 作为第一个参数
214
+ * - 使用 ipcMain.handle 替代 ipcMain.on,提供更好的错误处理
215
+ * - 支持 8 秒超时机制,避免长时间等待
216
+ * - 支持并发请求,每个请求都有独立的处理流程
217
+ * - 提供详细的错误日志记录
119
218
  */
120
219
  handleRenderer(channel, handler) {
121
220
  try {
122
221
  electron.ipcMain.handle(channel, async (event, ...args) => {
123
222
  try {
124
223
  const timeout = () => {
125
- return new Promise((_, reject) => {
224
+ return new Promise((resolve, reject) => {
126
225
  setTimeout(() => {
127
226
  reject(new Error('请求超时'));
128
- }, 1000 * 8); // 设定超时时间为8秒
227
+ }, TIMEOUT);
129
228
  });
130
229
  };
131
230
  const processing = () => {
132
- return new Promise(async (_, reject) => {
231
+ return new Promise(async (resolve, reject) => {
133
232
  try {
134
233
  const data = await handler(...args);
135
- // console.error(`[${channel}] processing data: `, data, args);
136
- _(data);
234
+ resolve(data);
137
235
  }
138
236
  catch (error) {
139
237
  reject(error);
@@ -146,53 +244,80 @@ class MainIPC {
146
244
  return result; // 返回正常结果
147
245
  }
148
246
  catch (error) {
149
- console.error('SDK handleRenderer error: ', `channel: ${channel}`, error);
247
+ log('error', 'SDK handleRenderer error: ', `channel: ${channel}`, error);
150
248
  // @ts-ignore
151
249
  return error?.message; // 返回超时错误信息
152
250
  }
153
251
  });
154
252
  }
155
253
  catch (error) {
156
- console.error('SDK handleRenderer error: ', error);
254
+ log('error', 'SDK handleRenderer error: ', error);
157
255
  }
256
+ return {
257
+ cancel: () => {
258
+ electron.ipcMain.removeHandler(channel);
259
+ }
260
+ };
158
261
  }
159
262
  /**
160
- * 初始化
263
+ * 初始化消息中继功能
264
+ * 设置消息转发和回复机制,支持跨渲染进程通信
265
+ *
266
+ * 功能说明:
267
+ * - relay-message: 转发消息到指定的渲染进程或所有渲染进程
268
+ * - relay-reply: 处理回复消息,广播给所有渲染进程
269
+ * - __GetCurrentWebContentId__: 获取当前 WebContent ID
270
+ * - __OpenCurrentWebContentDevTools__: 打开当前 WebContent 的开发者工具
161
271
  */
162
272
  relayMessage() {
163
- electron.ipcMain.on('relay-message', (_event, { targetWebContentId, channel, args }) => {
273
+ // 防止重复注册监听器:先移除再添加
274
+ const relayMessageHandler = (_event, { targetWebContentId, channel, requestId, args }) => {
275
+ log('log', 'relay-message', { targetWebContentId, channel, requestId, args });
164
276
  if (targetWebContentId) {
277
+ // 转发到指定的渲染进程
165
278
  const targetWebContent = electron.webContents.fromId(targetWebContentId);
166
279
  if (targetWebContent) {
167
- targetWebContent.send(channel, ...args);
280
+ targetWebContent.send(channel, requestId, ...args);
168
281
  }
169
282
  }
170
283
  else {
284
+ // 广播到所有渲染进程
171
285
  electron.webContents.getAllWebContents().forEach(webContent => {
172
- webContent.send(channel, ...args);
286
+ webContent.send(channel, requestId, ...args);
173
287
  });
174
288
  }
175
- });
176
- electron.ipcMain.on('relay-reply', (_event, { originalChannel, result }) => {
177
- // 所有渲染进程都会接收 ${originalChannel}-reply 事件,要求仅需要的渲染进程处理该事件的回复。
178
- // 这样可以避免需要指定窗口 ID 回复消息,并确保消息能够正确传递和接收
289
+ };
290
+ electron.ipcMain.removeListener('relay-message', relayMessageHandler);
291
+ electron.ipcMain.on('relay-message', relayMessageHandler);
292
+ // 处理回复消息广播
293
+ const relayReplyHandler = (_event, { originalChannel, requestId, result }) => {
294
+ log('log', 'relay-reply', { originalChannel, requestId, result });
295
+ // 使用 requestId 确保回复发送给正确的请求
296
+ // 所有渲染进程都会接收 ${originalChannel}-reply-${requestId} 事件
297
+ // 只有对应的请求才会处理该回复,避免数据错乱
179
298
  electron.webContents.getAllWebContents().forEach(webContent => {
180
- webContent.send(`${originalChannel}-reply`, result);
299
+ webContent.send(`${originalChannel}-reply-${requestId}`, result);
181
300
  });
182
- });
301
+ };
302
+ electron.ipcMain.removeListener('relay-reply', relayReplyHandler);
303
+ electron.ipcMain.on('relay-reply', relayReplyHandler);
304
+ // 获取当前 WebContent ID 的通道
183
305
  const getCurrentWebContentIdChannel = '__GetCurrentWebContentId__';
184
- electron.ipcMain.on(getCurrentWebContentIdChannel, (_event) => {
306
+ const getCurrentWebContentIdHandler = (_event) => {
185
307
  try {
186
308
  _event.frameId;
187
309
  const webContentId = _event?.sender?.id;
188
310
  _event.sender.send(`${getCurrentWebContentIdChannel}-reply`, webContentId);
189
311
  }
190
312
  catch (error) {
191
- return undefined;
313
+ log('error', 'getCurrentWebContentId error:', error);
192
314
  }
193
- });
315
+ };
316
+ electron.ipcMain.removeListener(getCurrentWebContentIdChannel, getCurrentWebContentIdHandler);
317
+ electron.ipcMain.on(getCurrentWebContentIdChannel, getCurrentWebContentIdHandler);
318
+ // 打开当前 WebContent 开发者工具的通道
194
319
  const openCurrentWebContentDevToolsChannel = '__OpenCurrentWebContentDevTools__';
195
- electron.ipcMain.on(openCurrentWebContentDevToolsChannel, (_event) => {
320
+ const openCurrentWebContentDevToolsHandler = (_event) => {
196
321
  try {
197
322
  _event.frameId;
198
323
  const webContentId = _event?.sender?.id;
@@ -200,13 +325,30 @@ class MainIPC {
200
325
  _event.sender.send(`${openCurrentWebContentDevToolsChannel}-reply`, webContentId);
201
326
  }
202
327
  catch (error) {
203
- return undefined;
328
+ log('error', 'openCurrentWebContentDevTools error:', error);
204
329
  }
205
- });
330
+ };
331
+ electron.ipcMain.removeListener(openCurrentWebContentDevToolsChannel, openCurrentWebContentDevToolsHandler);
332
+ electron.ipcMain.on(openCurrentWebContentDevToolsChannel, openCurrentWebContentDevToolsHandler);
206
333
  }
207
334
  }
335
+ /**
336
+ * 全局 MainIPC 实例
337
+ * 使用全局变量确保单例模式,避免重复创建实例
338
+ */
208
339
  // @ts-ignore
209
340
  const mainIPC = global['__ELECTRON_IPC__'] ? global['__ELECTRON_IPC__'] : (global['__ELECTRON_IPC__'] = new MainIPC());
341
+ /**
342
+ * 初始化 IPC 通信系统
343
+ * 设置消息中继功能,确保跨进程通信正常工作
344
+ *
345
+ * @returns MainIPC 实例
346
+ *
347
+ * 功能说明:
348
+ * - 检查是否已经初始化,避免重复初始化
349
+ * - 设置消息中继功能,支持跨渲染进程通信
350
+ * - 返回全局 MainIPC 实例
351
+ */
210
352
  const initialize = () => {
211
353
  // @ts-ignore
212
354
  if (exports.isInitialized && global['__ELECTRON_IPC__']) {
@@ -219,6 +361,22 @@ const initialize = () => {
219
361
  // @ts-ignore
220
362
  return global['__ELECTRON_IPC__'];
221
363
  };
364
+ /**
365
+ * 日志记录工具函数
366
+ * 提供统一的日志格式和错误处理
367
+ *
368
+ * @param type 日志类型:'log' 或 'error'
369
+ * @param data 要记录的数据
370
+ */
371
+ const log = (type, ...data) => {
372
+ const key = `[electron-ipc]: `;
373
+ try {
374
+ console[type](key, ...data);
375
+ }
376
+ catch (error) {
377
+ console.error(key, error);
378
+ }
379
+ };
222
380
 
223
381
  exports.initialize = initialize;
224
382
  exports.mainIPC = mainIPC;
package/main/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/src/main/index.ts"],"sourcesContent":["import { EventEmitter } from 'events'\nimport { ipcMain, webContents } from 'electron'\n// import { getRandomUUID } from '../common'\n\nexport let isInitialized = false;\nclass MainIPC {\n\tstatic instance: MainIPC\n\tprivate eventEmitter: EventEmitter = new EventEmitter()\n\n\tconstructor() {\n\t\tif (!MainIPC.instance) {\n\t\t\tthis.eventEmitter = new EventEmitter()\n\t\t\tMainIPC.instance = this\n\t\t}\n\t\treturn MainIPC.instance\n\t}\n\n /**\n * 发送给主进程消息\n * @param channel\n * @param args\n * @returns\n */\n\tasync invokeMain(channel: string, ...args: any[]) {\n\t\treturn new Promise(resolve => {\n\t\t\tthis.eventEmitter.once(`${channel}-reply`, result => {\n\t\t\t\tresolve(result)\n\t\t\t})\n\t\t\tthis.eventEmitter.emit(channel, ...args)\n\t\t})\n\t}\n\n /**\n * 处理一次性主进程发送过来的消息\n * @param channel\n * @param handler\n */\n\thandleMainOnce(channel: string, handler: (...args: any[]) => Promise<any>) {\n\t\tthis.eventEmitter.once(channel, async (...args) => {\n\t\t\tconst result = await handler(...args)\n\t\t\tthis.eventEmitter.emit(`${channel}-reply`, result)\n\t\t})\n\t}\n\n /**\n * 处理主进程发送过来的消息\n * @param channel\n * @param handler\n */\n\thandleMain(channel: string, handler: (...args: any[]) => Promise<any>) {\n\t\tthis.eventEmitter.on(channel, async (...args) => {\n\t\t\tconst result = await handler(...args)\n\t\t\tthis.eventEmitter.emit(`${channel}-reply`, result)\n\t\t})\n\t}\n\n /**\n * 发送给渲染进程消息\n * @param webContents\n * @param channel\n * @param args\n * @returns\n */\n\tinvokeRenderer(webContents: Electron.WebContents, channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise(resolve => {\n\t\t\tconst sendMessage = () => {\n\t\t\t\tipcMain.once(`${channel}-reply`, (_event, result) => {\n\t\t\t\t\tresolve(result)\n\t\t\t\t})\n\t\t\t\twebContents.send(channel, ...args)\n\t\t\t}\n\n\t\t\t// 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息\n\t\t\tif (webContents.isLoading()) {\n\t\t\t\twebContents.once('did-finish-load', () => {\n\t\t\t\t\tsetTimeout(() => sendMessage())\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tsendMessage()\n\t\t\t}\n\t\t})\n\t}\n\n /**\n * 发送给所有渲染进程消息\n * @param channel\n * @param args\n * @returns\n */\n\tinvokeAllRenderer(channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise(resolve => {\n\t\t\twebContents.getAllWebContents().forEach(webContent => {\n\t\t\t\tconst sendMessage = () => {\n\t\t\t\t\tipcMain.once(`${channel}-reply`, (_event, result) => {\n\t\t\t\t\t\tresolve(result)\n\t\t\t\t\t})\n\t\t\t\t\twebContent.send(channel, ...args)\n\t\t\t\t}\n\n\t\t\t\t// 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息\n\t\t\t\tif (webContent.isLoading()) {\n\t\t\t\t\twebContent.once('did-finish-load', () => {\n\t\t\t\t\t\tsetTimeout(() => sendMessage())\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tsendMessage()\n\t\t\t\t}\n\t\t\t})\n\t\t})\n\t}\n /**\n * 处理一次性渲染进程发送过来的消息\n * @param channel\n * @param handler\n */\n\thandleRendererOnce(channel: string, handler: (...args: any[]) => Promise<any>) {\n\t\tipcMain.once(channel, async (_event, ...args: any[]) => {\n\t\t\tconst result = await handler(...args)\n\t\t\t_event.sender.send(`${channel}-reply`, result)\n\t\t})\n\t}\n /**\n * 处理渲染进程发送过来的消息\n * @param channel\n * @param handler\n */\n\thandleRenderer(channel: string, handler: (...args: any[]) => Promise<any>) {\n try {\n ipcMain.handle(channel, async (event, ...args) => {\n try {\n const timeout = () => {\n return new Promise((_, reject) => {\n setTimeout(() => {\n reject(new Error('请求超时'));\n }, 1000 * 8); // 设定超时时间为8秒\n });\n }\n\n const processing = () => {\n return new Promise(async (_, reject) => {\n try {\n const data = await handler(...args);\n // console.error(`[${channel}] processing data: `, data, args);\n _(data);\n } catch (error) {\n reject(error);\n }\n });\n }\n // 这里是实际的异步操作\n const result = await Promise.race([processing(), timeout()]);\n // console.error(`[${channel}] handleRenderer result: `, result)\n return result; // 返回正常结果\n } catch (error) {\n console.error('SDK handleRenderer error: ', `channel: ${channel}`, error);\n // @ts-ignore\n return error?.message; // 返回超时错误信息\n }\n });\n } catch (error) {\n console.error('SDK handleRenderer error: ', error);\n }\n\t}\n /**\n * 初始化\n */\n\trelayMessage() {\n\t\tipcMain.on('relay-message', (_event, { targetWebContentId, channel, args }) => {\n\t\t\tif (targetWebContentId) {\n\t\t\t\tconst targetWebContent = webContents.fromId(targetWebContentId)\n\t\t\t\tif (targetWebContent) {\n\t\t\t\t\ttargetWebContent.send(channel, ...args)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\twebContents.getAllWebContents().forEach(webContent => {\n\t\t\t\t\twebContent.send(channel, ...args)\n\t\t\t\t})\n\t\t\t}\n\t\t})\n\n\t\tipcMain.on('relay-reply', (_event, { originalChannel, result }) => {\n\t\t\t// 所有渲染进程都会接收 ${originalChannel}-reply 事件,要求仅需要的渲染进程处理该事件的回复。\n\t\t\t// 这样可以避免需要指定窗口 ID 回复消息,并确保消息能够正确传递和接收\n webContents.getAllWebContents().forEach(webContent => {\n webContent.send(`${originalChannel}-reply`, result)\n })\n\t\t})\n\n const getCurrentWebContentIdChannel = '__GetCurrentWebContentId__'\n ipcMain.on(getCurrentWebContentIdChannel, (_event) => {\n try {\n _event.frameId\n const webContentId = _event?.sender?.id;\n _event.sender.send(`${getCurrentWebContentIdChannel}-reply`, webContentId)\n } catch (error) {\n return undefined\n }\n\t\t})\n const openCurrentWebContentDevToolsChannel = '__OpenCurrentWebContentDevTools__'\n ipcMain.on(openCurrentWebContentDevToolsChannel, (_event) => {\n try {\n _event.frameId\n const webContentId = _event?.sender?.id;\n webContents.fromId(webContentId)?.openDevTools();\n _event.sender.send(`${openCurrentWebContentDevToolsChannel}-reply`, webContentId)\n } catch (error) {\n return undefined\n }\n\t\t})\n\n\t}\n\n}\n// @ts-ignore\nexport const mainIPC: MainIPC = global['__ELECTRON_IPC__'] ? global['__ELECTRON_IPC__'] : (global['__ELECTRON_IPC__'] = new MainIPC());\n\nexport const initialize = () => {\n // @ts-ignore\n if (isInitialized && global['__ELECTRON_IPC__']){\n // @ts-ignore\n return global['__ELECTRON_IPC__']\n }\n isInitialized = true;\n // @ts-ignore\n global['__ELECTRON_IPC__'].relayMessage()\n // @ts-ignore\n return global['__ELECTRON_IPC__']\n}\n\n\nconst log = (type: 'log' | 'error', ...data: any[]) => {\n const key = `[electron-ipc]: `\n try {\n console[type](key, ...data);\n } catch (error) {\n console.error(key, error)\n }\n}\n\n"],"names":["isInitialized","EventEmitter","ipcMain","webContents"],"mappings":";;;AAEA;AAEWA,qBAAa,GAAG,MAAM;AACjC,MAAM,OAAO,CAAA;AAIZ,IAAA,WAAA,GAAA;AAFQ,QAAA,IAAA,CAAA,YAAY,GAAiB,IAAIC,mBAAY,EAAE,CAAA;AAGtD,QAAA,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;AACtB,YAAA,IAAI,CAAC,YAAY,GAAG,IAAIA,mBAAY,EAAE,CAAA;AACtC,YAAA,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAA;SACvB;QACD,OAAO,OAAO,CAAC,QAAQ,CAAA;KACvB;AAEA;;;;;AAKG;AACJ,IAAA,MAAM,UAAU,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;AAC/C,QAAA,OAAO,IAAI,OAAO,CAAC,OAAO,IAAG;YAC5B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAG,EAAA,OAAO,CAAQ,MAAA,CAAA,EAAE,MAAM,IAAG;gBACnD,OAAO,CAAC,MAAM,CAAC,CAAA;AAChB,aAAC,CAAC,CAAA;YACF,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAA;AACzC,SAAC,CAAC,CAAA;KACF;AAEA;;;;AAIG;IACJ,cAAc,CAAC,OAAe,EAAE,OAAyC,EAAA;AACxE,QAAA,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,KAAI;YACjD,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,IAAI,CAAC,CAAA;YACrC,IAAI,CAAC,YAAY,C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CAAC,OAAO,CAAA;AACd,gBAAA,MAAM,YAAY,GAAG,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC;gBACxCC,oBAAW,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,YAAY,EAAE,CAAC;gBACjD,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAG,EAAA,oCAAoC,CAAQ,MAAA,CAAA,EAAE,YAAY,CAAC,CAAA;aAClF;YAAC,OAAO,KAAK,EAAE;AACd,gBAAA,OAAO,SAAS,CAAA;aACjB;AACL,SAAC,CAAC,CAAA;KAEF;AAED,CAAA;AACD;AACO,MAAM,OAAO,GAAY,MAAM,CAAC,kBAAkB,CAAC,GAAG,MAAM,CAAC,kBAAkB,CAAC,IAAI,MAAM,CAAC,kBAAkB,CAAC,GAAG,IAAI,OAAO,EAAE,EAAE;AAEhI,MAAM,UAAU,GAAG,MAAK;;AAE7B,IAAA,IAAIH,qBAAa,IAAI,MAAM,CAAC,kBAAkB,CAAC,EAAC;;AAE9C,QAAA,OAAO,MAAM,CAAC,kBAAkB,CAAC,CAAA;KAClC;IACDA,qBAAa,GAAG,IAAI,CAAC;;AAErB,IAAA,MAAM,CAAC,kBAAkB,CAAC,CAAC,YAAY,EAAE,CAAA;;AAEzC,IAAA,OAAO,MAAM,CAAC,kBAAkB,CAAC,CAAA;AACnC;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/src/main/index.ts"],"sourcesContent":["import { EventEmitter } from 'events'\nimport { ipcMain, webContents } from 'electron'\nimport { getRandomUUID } from '../common'\n\nexport let isInitialized = false;\nconst TIMEOUT = 1000 * 60 * 30; // 30分钟超时\n/**\n * 主进程 IPC 通信类\n * 负责处理主进程与渲染进程之间的消息通信\n * 使用单例模式确保全局唯一实例\n *\n * 修复说明:\n * - 为每个请求生成唯一的 requestId,解决并发请求数据错乱问题\n * - 确保请求和响应能够正确匹配,避免多个并发请求互相干扰\n */\nclass MainIPC {\n\tstatic instance: MainIPC\n\tprivate eventEmitter: EventEmitter = new EventEmitter()\n\n\tconstructor() {\n\t\tif (!MainIPC.instance) {\n\t\t\tthis.eventEmitter = new EventEmitter()\n\t\t\tMainIPC.instance = this\n\t\t}\n\t\treturn MainIPC.instance\n\t}\n\n /**\n * 发送给主进程消息\n * 使用唯一的 requestId 确保并发请求不会互相干扰\n *\n * @param channel 消息通道名称\n * @param args 传递给处理器的参数\n * @returns Promise<any> 返回处理结果\n *\n * 修复说明:\n * - 为每个请求生成唯一的 requestId\n * - 监听 `${channel}-reply-${requestId}` 事件,确保只接收对应请求的回复\n * - 发送请求时包含 requestId,让处理器知道如何回复\n */\n\tasync invokeMain(channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst requestId = getRandomUUID()\n\t\t\tconst replyChannel = `${channel}-reply-${requestId}`\n\t\t\tlet isResolved = false\n\n\t\t\tconst replyHandler = (result: any) => {\n\t\t\t\tif (!isResolved) {\n\t\t\t\t\tisResolved = true\n\t\t\t\t\tthis.eventEmitter.removeListener(replyChannel, replyHandler)\n\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t\tresolve(result)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis.eventEmitter.once(replyChannel, replyHandler)\n\n\t\t\t// 添加超时机制,确保监听器最终会被清理\n\t\t\tconst timeoutId = setTimeout(() => {\n\t\t\t\tif (!isResolved) {\n\t\t\t\t\tisResolved = true\n\t\t\t\t\tthis.eventEmitter.removeListener(replyChannel, replyHandler)\n\t\t\t\t\treject(new Error(`IPC request timeout: ${channel} (30s)`))\n\t\t\t\t}\n\t\t\t}, TIMEOUT) // 30秒超时\n\n\t\t\tthis.eventEmitter.emit(channel, requestId, ...args)\n\t\t})\n\t}\n\n /**\n * 处理主进程发送过来的消息\n * 持续监听指定通道的消息\n *\n * @param channel 消息通道名称\n * @param handler 处理函数,接收除 requestId 外的所有参数\n *\n * 修复说明:\n * - 接收 requestId 作为第一个参数\n * - 使用 `${channel}-reply-${requestId}` 发送回复,确保回复给正确的请求\n * - 支持多个并发请求,每个请求都有独立的回复通道\n */\n\thandleMain(channel: string, handler: (...args: any[]) => Promise<any>) {\n\t\tthis.eventEmitter.on(channel, async (requestId: string, ...args) => {\n\t\t\tconst result = await handler(...args)\n\t\t\tthis.eventEmitter.emit(`${channel}-reply-${requestId}`, result)\n\t\t})\n return {\n cancel: () => {\n this.eventEmitter.removeAllListeners(channel)\n }\n };\n\t}\n\n /**\n * 发送给渲染进程消息\n * 使用唯一的 requestId 确保并发请求不会互相干扰\n *\n * @param webContents 目标渲染进程的 WebContents 对象\n * @param channel 消息通道名称\n * @param args 传递给渲染进程的参数\n * @returns Promise<any> 返回渲染进程的处理结果\n *\n * 修复说明:\n * - 为每个请求生成唯一的 requestId\n * - 监听 `${channel}-reply-${requestId}` 事件,确保只接收对应请求的回复\n * - 发送请求时包含 requestId,让渲染进程知道如何回复\n * - 等待渲染进程加载完成后再发送消息,确保消息能够被正确接收\n */\n\tinvokeRenderer(webContents: Electron.WebContents, channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst requestId = getRandomUUID()\n\t\t\tconst replyChannel = `${channel}-reply-${requestId}`\n\t\t\tlet isResolved = false\n\n\t\t\tconst sendMessage = () => {\n\t\t\t\t// 检查 webContents 是否已销毁\n\t\t\t\tif (webContents.isDestroyed()) {\n\t\t\t\t\treject(new Error('WebContents has been destroyed'))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst replyHandler = (_event: Electron.IpcMainEvent, result: any) => {\n\t\t\t\t\tif (!isResolved) {\n\t\t\t\t\t\tisResolved = true\n\t\t\t\t\t\tipcMain.removeListener(replyChannel, replyHandler)\n\t\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t\t\tresolve(result)\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tipcMain.once(replyChannel, replyHandler)\n\n\t\t\t\t// 添加超时机制,确保监听器最终会被清理\n\t\t\t\tconst timeoutId = setTimeout(() => {\n\t\t\t\t\tif (!isResolved) {\n\t\t\t\t\t\tisResolved = true\n\t\t\t\t\t\tipcMain.removeListener(replyChannel, replyHandler)\n\t\t\t\t\t\treject(new Error(`IPC request timeout: ${channel} (30s)`))\n\t\t\t\t\t}\n\t\t\t\t}, TIMEOUT) // 30秒超时\n\n\t\t\t\twebContents.send(channel, requestId, ...args)\n\t\t\t}\n\n\t\t\t// 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息\n\t\t\tif (webContents.isLoading()) {\n\t\t\t\twebContents.once('did-finish-load', () => {\n\t\t\t\t\tsetTimeout(() => sendMessage())\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tsendMessage()\n\t\t\t}\n\t\t})\n\t}\n\n /**\n * 发送给所有渲染进程消息\n * 向所有渲染进程发送消息并收集所有响应\n *\n * @param channel 消息通道名称\n * @param args 传递给所有渲染进程的参数\n * @returns Promise<any[]> 返回所有渲染进程的处理结果数组\n *\n * 修复说明:\n * - 为每个请求生成唯一的 requestId\n * - 收集所有渲染进程的响应,当所有响应都收到时才 resolve\n * - 使用 `${channel}-reply-${requestId}` 确保只接收对应请求的回复\n * - 如果没有渲染进程,直接返回空数组\n * - 等待每个渲染进程加载完成后再发送消息\n */\n\tinvokeAllRenderer(channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise(resolve => {\n\t\t\tconst requestId = getRandomUUID()\n\t\t\tlet responseCount = 0\n\t\t\tconst totalWebContents = webContents.getAllWebContents().length\n\n\t\t\tif (totalWebContents === 0) {\n\t\t\t\tresolve([])\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tconst responses: any[] = []\n\t\t\tconst replyChannel = `${channel}-reply-${requestId}`\n\n\t\t\t// 只注册一个监听器,避免内存泄漏\n\t\t\t// 使用 on 而不是 once,因为需要接收多个响应\n\t\t\tconst replyHandler = (_event: Electron.IpcMainEvent, result: any) => {\n\t\t\t\tresponses.push(result)\n\t\t\t\tresponseCount++\n\t\t\t\tif (responseCount === totalWebContents) {\n\t\t\t\t\t// 清理监听器\n\t\t\t\t\tipcMain.removeListener(replyChannel, replyHandler)\n\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t\tresolve(responses)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tipcMain.on(replyChannel, replyHandler)\n\n\t\t\t// 添加超时机制,确保监听器最终会被清理\n\t\t\tconst timeoutId = setTimeout(() => {\n\t\t\t\tipcMain.removeListener(replyChannel, replyHandler)\n\t\t\t\tif (responseCount < totalWebContents) {\n\t\t\t\t\t// 超时后返回已收到的响应\n\t\t\t\t\tresolve(responses)\n\t\t\t\t}\n\t\t\t}, TIMEOUT) // 30秒超时\n\n\t\t\twebContents.getAllWebContents().forEach(webContent => {\n\t\t\t\tconst sendMessage = () => {\n\t\t\t\t\twebContent.send(channel, requestId, ...args)\n\t\t\t\t}\n\n\t\t\t\t// 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息\n\t\t\t\tif (webContent.isLoading()) {\n\t\t\t\t\twebContent.once('did-finish-load', () => {\n\t\t\t\t\t\tsetTimeout(() => sendMessage())\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tsendMessage()\n\t\t\t\t}\n\t\t\t})\n\t\t})\n\t}\n\n /**\n * 处理渲染进程发送过来的消息\n * 持续监听指定通道的消息,支持超时处理\n *\n * @param channel 消息通道名称\n * @param handler 处理函数,接收除 requestId 外的所有参数\n *\n * 修复说明:\n * - 接收 requestId 作为第一个参数\n * - 使用 ipcMain.handle 替代 ipcMain.on,提供更好的错误处理\n * - 支持 8 秒超时机制,避免长时间等待\n * - 支持并发请求,每个请求都有独立的处理流程\n * - 提供详细的错误日志记录\n */\n\thandleRenderer(channel: string, handler: (...args: any[]) => Promise<any>) {\n try {\n ipcMain.handle(channel, async (event, ...args) => {\n try {\n const timeout = () => {\n return new Promise((resolve, reject) => {\n setTimeout(() => {\n reject(new Error('请求超时'));\n }, TIMEOUT);\n });\n }\n\n const processing = () => {\n return new Promise(async (resolve, reject) => {\n try {\n const data = await handler(...args);\n resolve(data);\n } catch (error) {\n reject(error);\n }\n });\n }\n // 这里是实际的异步操作\n const result = await Promise.race([processing(), timeout()]);\n // console.error(`[${channel}] handleRenderer result: `, result)\n return result; // 返回正常结果\n } catch (error) {\n log('error', 'SDK handleRenderer error: ', `channel: ${channel}`, error);\n // @ts-ignore\n return error?.message; // 返回超时错误信息\n }\n });\n } catch (error) {\n log('error', 'SDK handleRenderer error: ', error);\n }\n return {\n cancel: () => {\n ipcMain.removeHandler(channel)\n }\n };\n\t}\n /**\n * 初始化消息中继功能\n * 设置消息转发和回复机制,支持跨渲染进程通信\n *\n * 功能说明:\n * - relay-message: 转发消息到指定的渲染进程或所有渲染进程\n * - relay-reply: 处理回复消息,广播给所有渲染进程\n * - __GetCurrentWebContentId__: 获取当前 WebContent ID\n * - __OpenCurrentWebContentDevTools__: 打开当前 WebContent 的开发者工具\n */\n\trelayMessage() {\n\t\t// 防止重复注册监听器:先移除再添加\n\t\tconst relayMessageHandler = (_event: Electron.IpcMainEvent, { targetWebContentId, channel, requestId, args }: any) => {\n log('log', 'relay-message', { targetWebContentId, channel, requestId, args })\n\t\t\tif (targetWebContentId) {\n\t\t\t\t// 转发到指定的渲染进程\n\t\t\t\tconst targetWebContent = webContents.fromId(targetWebContentId)\n\t\t\t\tif (targetWebContent) {\n\t\t\t\t\ttargetWebContent.send(channel, requestId, ...args)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// 广播到所有渲染进程\n\t\t\t\twebContents.getAllWebContents().forEach(webContent => {\n\t\t\t\t\twebContent.send(channel, requestId, ...args)\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\n\t\tipcMain.removeListener('relay-message', relayMessageHandler)\n\t\tipcMain.on('relay-message', relayMessageHandler)\n\n\t\t// 处理回复消息广播\n\t\tconst relayReplyHandler = (_event: Electron.IpcMainEvent, { originalChannel, requestId, result }: any) => {\n log('log', 'relay-reply', { originalChannel, requestId, result })\n\t\t\t// 使用 requestId 确保回复发送给正确的请求\n\t\t\t// 所有渲染进程都会接收 ${originalChannel}-reply-${requestId} 事件\n\t\t\t// 只有对应的请求才会处理该回复,避免数据错乱\n webContents.getAllWebContents().forEach(webContent => {\n webContent.send(`${originalChannel}-reply-${requestId}`, result)\n })\n\t\t}\n\n\t\tipcMain.removeListener('relay-reply', relayReplyHandler)\n\t\tipcMain.on('relay-reply', relayReplyHandler)\n\n // 获取当前 WebContent ID 的通道\n const getCurrentWebContentIdChannel = '__GetCurrentWebContentId__'\n const getCurrentWebContentIdHandler = (_event: Electron.IpcMainEvent) => {\n try {\n _event.frameId\n const webContentId = _event?.sender?.id;\n _event.sender.send(`${getCurrentWebContentIdChannel}-reply`, webContentId)\n } catch (error) {\n log('error', 'getCurrentWebContentId error:', error)\n }\n\t\t}\n\t\tipcMain.removeListener(getCurrentWebContentIdChannel, getCurrentWebContentIdHandler)\n ipcMain.on(getCurrentWebContentIdChannel, getCurrentWebContentIdHandler)\n\n // 打开当前 WebContent 开发者工具的通道\n const openCurrentWebContentDevToolsChannel = '__OpenCurrentWebContentDevTools__'\n const openCurrentWebContentDevToolsHandler = (_event: Electron.IpcMainEvent) => {\n try {\n _event.frameId\n const webContentId = _event?.sender?.id;\n webContents.fromId(webContentId)?.openDevTools();\n _event.sender.send(`${openCurrentWebContentDevToolsChannel}-reply`, webContentId)\n } catch (error) {\n log('error', 'openCurrentWebContentDevTools error:', error)\n }\n\t\t}\n\t\tipcMain.removeListener(openCurrentWebContentDevToolsChannel, openCurrentWebContentDevToolsHandler)\n ipcMain.on(openCurrentWebContentDevToolsChannel, openCurrentWebContentDevToolsHandler)\n\n\t}\n\n}\n\n/**\n * 全局 MainIPC 实例\n * 使用全局变量确保单例模式,避免重复创建实例\n */\n// @ts-ignore\nexport const mainIPC: MainIPC = global['__ELECTRON_IPC__'] ? global['__ELECTRON_IPC__'] : (global['__ELECTRON_IPC__'] = new MainIPC());\n\n/**\n * 初始化 IPC 通信系统\n * 设置消息中继功能,确保跨进程通信正常工作\n *\n * @returns MainIPC 实例\n *\n * 功能说明:\n * - 检查是否已经初始化,避免重复初始化\n * - 设置消息中继功能,支持跨渲染进程通信\n * - 返回全局 MainIPC 实例\n */\nexport const initialize = () => {\n // @ts-ignore\n if (isInitialized && global['__ELECTRON_IPC__']){\n // @ts-ignore\n return global['__ELECTRON_IPC__']\n }\n isInitialized = true;\n // @ts-ignore\n global['__ELECTRON_IPC__'].relayMessage()\n // @ts-ignore\n return global['__ELECTRON_IPC__']\n}\n\n\n/**\n * 日志记录工具函数\n * 提供统一的日志格式和错误处理\n *\n * @param type 日志类型:'log' 或 'error'\n * @param data 要记录的数据\n */\nconst log = (type: 'log' | 'error', ...data: any[]) => {\n const key = `[electron-ipc]: `\n try {\n console[type](key, ...data);\n } catch (error) {\n console.error(key, error)\n }\n}\n\n"],"names":["isInitialized","EventEmitter","getRandomUUID","ipcMain","webContents"],"mappings":";;;;AAIWA,qBAAa,GAAG,MAAM;AACjC,MAAM,OAAO,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,CAAC;AAC/B;;;;;;;;AAQG;AACH,MAAM,OAAO,CAAA;AAIZ,IAAA,WAAA,GAAA;AAFQ,QAAA,IAAA,CAAA,YAAY,GAAiB,IAAIC,mBAAY,EAAE,CAAA;AAGtD,QAAA,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;AACtB,YAAA,IAAI,CAAC,YAAY,GAAG,IAAIA,mBAAY,EAAE,CAAA;AACtC,YAAA,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAA;SACvB;QACD,OAAO,OAAO,CAAC,QAAQ,CAAA;KACvB;AAEA;;;;;;;;;;;;AAYG;AACJ,IAAA,MAAM,UAAU,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;QAC/C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAI;AACtC,YAAA,MAAM,SAAS,GAAGC,mBAAa,EAAE,CAAA;AACjC,YAAA,MAAM,YAA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A;KAExF;AAED,CAAA;AAED;;;AAGG;AACH;AACO,MAAM,OAAO,GAAY,MAAM,CAAC,kBAAkB,CAAC,GAAG,MAAM,CAAC,kBAAkB,CAAC,IAAI,MAAM,CAAC,kBAAkB,CAAC,GAAG,IAAI,OAAO,EAAE,EAAE;AAEvI;;;;;;;;;;AAUG;AACI,MAAM,UAAU,GAAG,MAAK;;AAE7B,IAAA,IAAIH,qBAAa,IAAI,MAAM,CAAC,kBAAkB,CAAC,EAAC;;AAE9C,QAAA,OAAO,MAAM,CAAC,kBAAkB,CAAC,CAAA;KAClC;IACDA,qBAAa,GAAG,IAAI,CAAC;;AAErB,IAAA,MAAM,CAAC,kBAAkB,CAAC,CAAC,YAAY,EAAE,CAAA;;AAEzC,IAAA,OAAO,MAAM,CAAC,kBAAkB,CAAC,CAAA;AACnC,EAAC;AAGD;;;;;;AAMG;AACH,MAAM,GAAG,GAAG,CAAC,IAAqB,EAAE,GAAG,IAAW,KAAI;IACpD,MAAM,GAAG,GAAG,CAAA,gBAAA,CAAkB,CAAA;AAC9B,IAAA,IAAI;QACF,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;KAC7B;IAAC,OAAO,KAAK,EAAE;AACd,QAAA,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;KAC1B;AACH,CAAC;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynker-desktop/electron-ipc",
3
- "version": "0.0.9-alpha.7",
3
+ "version": "0.0.9-alpha.72",
4
4
  "description": "electron-ipc",
5
5
  "keywords": [
6
6
  "electron",
@@ -73,5 +73,5 @@
73
73
  "tslib": "^2.6.3",
74
74
  "typescript": "^5.5.3"
75
75
  },
76
- "gitHead": "57e45315411374b8928f5b13abdfd7636716f727"
76
+ "gitHead": "46514369e8cccca12e1b5464698bca1a0d404e60"
77
77
  }
package/preload/index.js CHANGED
@@ -16,7 +16,22 @@ else {
16
16
  // This will fail if contextIsolation is not enabled
17
17
  try {
18
18
  // @ts-ignore
19
- electron.contextBridge.exposeInMainWorld(WINDOWL_GLOBAL_KEY, electron.ipcRenderer);
19
+ electron.contextBridge.exposeInMainWorld(WINDOWL_GLOBAL_KEY, {
20
+ ...electron.ipcRenderer,
21
+ emit: electron.ipcRenderer.emit.bind(electron.ipcRenderer),
22
+ prependListener: electron.ipcRenderer.prependListener.bind(electron.ipcRenderer),
23
+ prependOnceListener: electron.ipcRenderer.prependOnceListener.bind(electron.ipcRenderer),
24
+ once: electron.ipcRenderer.once.bind(electron.ipcRenderer),
25
+ on: electron.ipcRenderer.on.bind(electron.ipcRenderer),
26
+ off: electron.ipcRenderer.off.bind(electron.ipcRenderer),
27
+ send: electron.ipcRenderer.send.bind(electron.ipcRenderer),
28
+ sendSync: electron.ipcRenderer.sendSync.bind(electron.ipcRenderer),
29
+ sendToHost: electron.ipcRenderer.sendToHost.bind(electron.ipcRenderer),
30
+ invoke: electron.ipcRenderer.invoke.bind(electron.ipcRenderer),
31
+ addListener: electron.ipcRenderer.addListener.bind(electron.ipcRenderer),
32
+ removeAllListeners: electron.ipcRenderer.removeAllListeners.bind(electron.ipcRenderer),
33
+ removeListener: electron.ipcRenderer.removeListener.bind(electron.ipcRenderer),
34
+ });
20
35
  }
21
36
  catch (e) {
22
37
  // console.error(e)
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/src/common/index.ts","../src/src/preload/index.ts"],"sourcesContent":["import type { BrowserWindow } from 'electron';\nimport md5 from 'md5';\nimport { v5 as uuidv5 } from 'uuid';\n\nexport const WINDOWL_GLOBAL_KEY = `__ELECTRON_IPC__`;\n\n// @ts-ignore\ndeclare global {\n interface Window {\n __ELECTRON_WINDOW_MANAGER__?: any;\n }\n}\n\n/** Tab ID */\nexport type TabID = number;\n\nexport interface Tab {\n /** 标签URL */\n url: string;\n /** 标签href */\n href: string;\n /** 标签标题 */\n title: string;\n /** 标签favicon */\n favicon: string;\n /** loading */\n isLoading: boolean;\n /** 是否可返回上一页 */\n canGoBack: boolean;\n canGoForward: boolean;\n}\n\nexport interface Tabs {\n [key: number]: Tab;\n}\n\nexport interface ElectronWindowsManagerOptions {\n name: string;\n loadingView?: {\n url: string;\n };\n browserWindow?: any;\n openDevTools?: boolean;\n preventOriginClose?: boolean;\n preventOriginNavigate?: boolean;\n}\n\nexport interface WindowItem extends BrowserWindow {\n /** BW别名 */\n _name: string;\n}\n\nexport function getRandomUUID(key: string = `${Date.now()}`): string {\n let webcrypto!: Crypto;\n let randomValue = Math?.random()?.toString()?.replace('0.', '');\n try {\n if (!webcrypto && !!window && window?.crypto) {\n webcrypto = window.crypto;\n }\n } catch (error) {console.log}\n try {\n const ar = webcrypto.getRandomValues(new Uint8Array(12));\n randomValue = `${md5(ar)}` || randomValue;\n } catch (error) {}\n const uuid = uuidv5(\n `${JSON.stringify(key)}_${Date.now()}_${randomValue}`,\n uuidv5.URL,\n );\n return uuid;\n}\n","import { ipcRenderer, contextBridge } from 'electron';\nimport { WINDOWL_GLOBAL_KEY } from '../common';\n\n// @ts-ignore\nif (window[WINDOWL_GLOBAL_KEY]) {\n // eslint-disable-next-line no-console\n console.log('electron-ipc Electron preload has already been run');\n} else {\n // @ts-ignore\n window[WINDOWL_GLOBAL_KEY] = ipcRenderer;\n if (contextBridge) {\n // This will fail if contextIsolation is not enabled\n try {\n // @ts-ignore\n contextBridge.exposeInMainWorld(WINDOWL_GLOBAL_KEY, ipcRenderer);\n } catch (e) {\n // console.error(e)\n }\n }\n}\n"],"names":["ipcRenderer","contextBridge"],"mappings":";;;;AAIO,MAAM,kBAAkB,GAAG,kBAAkB;;ACDpD;AACA,IAAI,MAAM,CAAC,kBAAkB,CAAC,EAAE;;AAE9B,IAAA,OAAO,CAAC,GAAG,CAAC,oDAAoD,CAAC,CAAC;AACpE,CAAC;KAAM;;AAEL,IAAA,MAAM,CAAC,kBAAkB,CAAC,GAAGA,oBAAW,CAAC;IACzC,IAAIC,sBAAa,EAAE;;AAEjB,QAAA,IAAI;;AAEF,YAAAA,sBAAa,CAAC,iBAAiB,CAAC,kBAAkB,EAAED,oBAAW,CAAC,CAAC;SAClE;QAAC,OAAO,CAAC,EAAE;;SAEX;KACF;AACH;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/src/common/index.ts","../src/src/preload/index.ts"],"sourcesContent":["import type { BrowserWindow } from 'electron';\nimport md5 from 'md5';\nimport { v5 as uuidv5 } from 'uuid';\n\nexport const WINDOWL_GLOBAL_KEY = `__ELECTRON_IPC__`;\n\n// @ts-ignore\ndeclare global {\n interface Window {\n __ELECTRON_WINDOW_MANAGER__?: any;\n }\n}\n\n/** Tab ID */\nexport type TabID = number;\n\nexport interface Tab {\n /** 标签URL */\n url: string;\n /** 标签href */\n href: string;\n /** 标签标题 */\n title: string;\n /** 标签favicon */\n favicon: string;\n /** loading */\n isLoading: boolean;\n /** 是否可返回上一页 */\n canGoBack: boolean;\n canGoForward: boolean;\n}\n\nexport interface Tabs {\n [key: number]: Tab;\n}\n\nexport interface ElectronWindowsManagerOptions {\n name: string;\n loadingView?: {\n url: string;\n };\n browserWindow?: any;\n openDevTools?: boolean;\n preventOriginClose?: boolean;\n preventOriginNavigate?: boolean;\n}\n\nexport interface WindowItem extends BrowserWindow {\n /** BW别名 */\n _name: string;\n}\n\nexport const getRandomUUID = (key: string = `${Date.now()}`): string => {\n let webcrypto!: Crypto;\n let randomValue = Math?.random()?.toString()?.replace('0.', '');\n try {\n if (!webcrypto && !!window && window?.crypto) {\n webcrypto = window.crypto;\n }\n } catch (error) {console.log}\n try {\n const ar = webcrypto.getRandomValues(new Uint8Array(12));\n randomValue = `${md5(ar)}` || randomValue;\n } catch (error) {}\n const uuid = uuidv5(\n `${JSON.stringify(key)}_${Date.now()}_${randomValue}`,\n uuidv5.URL,\n );\n return uuid;\n}\n","import { ipcRenderer, contextBridge } from 'electron';\nimport { WINDOWL_GLOBAL_KEY } from '../common';\n\n// @ts-ignore\nif (window[WINDOWL_GLOBAL_KEY]) {\n // eslint-disable-next-line no-console\n console.log('electron-ipc Electron preload has already been run');\n} else {\n // @ts-ignore\n window[WINDOWL_GLOBAL_KEY] = ipcRenderer;\n if (contextBridge) {\n // This will fail if contextIsolation is not enabled\n try {\n // @ts-ignore\n contextBridge.exposeInMainWorld(WINDOWL_GLOBAL_KEY, {\n ...ipcRenderer,\n emit: ipcRenderer.emit.bind(ipcRenderer),\n prependListener: ipcRenderer.prependListener.bind(ipcRenderer),\n prependOnceListener: ipcRenderer.prependOnceListener.bind(ipcRenderer),\n once: ipcRenderer.once.bind(ipcRenderer),\n on: ipcRenderer.on.bind(ipcRenderer),\n off: ipcRenderer.off.bind(ipcRenderer),\n send: ipcRenderer.send.bind(ipcRenderer),\n sendSync: ipcRenderer.sendSync.bind(ipcRenderer),\n sendToHost: ipcRenderer.sendToHost.bind(ipcRenderer),\n invoke: ipcRenderer.invoke.bind(ipcRenderer),\n addListener: ipcRenderer.addListener.bind(ipcRenderer),\n removeAllListeners: ipcRenderer.removeAllListeners.bind(ipcRenderer),\n removeListener: ipcRenderer.removeListener.bind(ipcRenderer),\n });\n } catch (e) {\n // console.error(e)\n }\n }\n}\n"],"names":["ipcRenderer","contextBridge"],"mappings":";;;;AAIO,MAAM,kBAAkB,GAAG,kBAAkB;;ACDpD;AACA,IAAI,MAAM,CAAC,kBAAkB,CAAC,EAAE;;AAE9B,IAAA,OAAO,CAAC,GAAG,CAAC,oDAAoD,CAAC,CAAC;AACpE,CAAC;KAAM;;AAEL,IAAA,MAAM,CAAC,kBAAkB,CAAC,GAAGA,oBAAW,CAAC;IACzC,IAAIC,sBAAa,EAAE;;AAEjB,QAAA,IAAI;;AAEF,YAAAA,sBAAa,CAAC,iBAAiB,CAAC,kBAAkB,EAAE;AAClD,gBAAA,GAAGD,oBAAW;gBACd,IAAI,EAAEA,oBAAW,CAAC,IAAI,CAAC,IAAI,CAACA,oBAAW,CAAC;gBACxC,eAAe,EAAEA,oBAAW,CAAC,e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