refactor: ensure IpcRenderer is not bridgable (#40330)

* refactor: ensure IpcRenderer is not bridgable

* chore: add notes to breaking-changes

* spec: fix test that bridged ipcrenderer
This commit is contained in:
Samuel Attard 2023-10-31 14:29:40 -07:00 committed by GitHub
parent 39d36e4462
commit 83892ab995
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 43 deletions

View file

@ -3,30 +3,30 @@ import { EventEmitter } from 'events';
const { ipc } = process._linkedBinding('electron_renderer_ipc');
const internal = false;
const ipcRenderer = new EventEmitter() as Electron.IpcRenderer;
ipcRenderer.send = function (channel, ...args) {
return ipc.send(internal, channel, args);
};
ipcRenderer.sendSync = function (channel, ...args) {
return ipc.sendSync(internal, channel, args);
};
ipcRenderer.sendToHost = function (channel, ...args) {
return ipc.sendToHost(channel, args);
};
ipcRenderer.invoke = async function (channel, ...args) {
const { error, result } = await ipc.invoke(internal, channel, args);
if (error) {
throw new Error(`Error invoking remote method '${channel}': ${error}`);
class IpcRenderer extends EventEmitter implements Electron.IpcRenderer {
send (channel: string, ...args: any[]) {
return ipc.send(internal, channel, args);
}
return result;
};
ipcRenderer.postMessage = function (channel: string, message: any, transferables: any) {
return ipc.postMessage(channel, message, transferables);
};
sendSync (channel: string, ...args: any[]) {
return ipc.sendSync(internal, channel, args);
}
export default ipcRenderer;
sendToHost (channel: string, ...args: any[]) {
return ipc.sendToHost(channel, args);
}
async invoke (channel: string, ...args: any[]) {
const { error, result } = await ipc.invoke(internal, channel, args);
if (error) {
throw new Error(`Error invoking remote method '${channel}': ${error}`);
}
return result;
}
postMessage (channel: string, message: any, transferables: any) {
return ipc.postMessage(channel, message, transferables);
}
}
export default new IpcRenderer();