From 2129d9046c3344222f90899e78316083db03bb04 Mon Sep 17 00:00:00 2001 From: Lam Tang Date: Sun, 25 Sep 2022 12:38:38 +0800 Subject: [PATCH] refactor: do not capture ipcMain messages But capture the return values of ipcRenderer.sendSync and ipcRenderer.invoke --- .../browser-preload/index.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/core-electron-main/browser-preload/index.js b/packages/core-electron-main/browser-preload/index.js index f4267c400c0..eb30e29d600 100644 --- a/packages/core-electron-main/browser-preload/index.js +++ b/packages/core-electron-main/browser-preload/index.js @@ -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) { @@ -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