Skip to content

Commit

Permalink
refactor: do not capture ipcMain messages
Browse files Browse the repository at this point in the history
But capture the return values of ipcRenderer.sendSync
and ipcRenderer.invoke
  • Loading branch information
tyn1998 committed Sep 25, 2022
1 parent 00eee42 commit 2129d90
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/core-electron-main/browser-preload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const os = require('os');

const { ipcRenderer } = require('electron');

// for generating requestId to pair request and response
const uuid = () => Date.now().toString(36) + Math.random().toString(36).substr(2);

const initForDevtools = () => {
const getCapturer = () => {
if (window.__OPENSUMI_DEVTOOLS_GLOBAL_HOOK__?.captureIPC) {
Expand Down Expand Up @@ -40,15 +43,23 @@ const initForDevtools = () => {
// ipcRenderer.sendSync
const originalIpcRendererSendSync = ipcRenderer.sendSync;
ipcRenderer.sendSync = (channel, ...args) => {
capture({ ipcMethod: 'ipcRenderer.sendSync', channel, args });
return originalIpcRendererSendSync.call(ipcRenderer, channel, ...args);
const requestId = uuid();
capture({ ipcMethod: 'ipcRenderer.sendSync', channel, requestId, args });
const result = originalIpcRendererSendSync.call(ipcRenderer, channel, ...args);
capture({ ipcMethod: 'ipcRenderer.sendSync', channel, requestId, result });
return result;
};

// ipcRenderer.invoke
const originalIpcRendererInvoke = ipcRenderer.invoke;
ipcRenderer.invoke = (channel, ...args) => {
capture({ ipcMethod: 'ipcRenderer.invoke', channel, args });
return originalIpcRendererInvoke.call(ipcRenderer, channel, ...args);
const requestId = uuid();
capture({ ipcMethod: 'ipcRenderer.invoke', channel, requestId, args });
const resultPromise = originalIpcRendererInvoke.call(ipcRenderer, channel, ...args);
resultPromise.then((result) => {
capture({ ipcMethod: 'ipcRenderer.invoke', channel, requestId, result });
});
return resultPromise;
};

// receive messages that transfered from main process and capture them
Expand Down

0 comments on commit 2129d90

Please sign in to comment.