krimlabs / eiphop

A fetch like wrapper for elecron's ipc.
https://eiphop.js.org
28 stars 8 forks source link

Export preload function #62

Open brumik opened 2 years ago

brumik commented 2 years ago

Since 2020 the security of the electron apps require recommend a preload function. To use this lib with preload it could export the preload function. I am able to make the pull request, it is just a questions if I should?

Export somehow this function

contextBridge.exposeInMainWorld('api', {
  send: (channel: string, requestId: string, action: string, payload: any): void => {
    const validChannel = ['asyncRequest'];
    if (validChannel.includes(channel)) {
      ipcRenderer.send(channel, requestId, action, payload);
    }
  },
  on: (channel: string, callback: Function): void => {
    const validChannel = [
      'asyncResponseNotify',
      'asyncResponse',
      'errorResponse',
    ]
    if (validChannel.includes(channel)) {
      // Strip event but pass an empty object to the callback
      ipcRenderer.on(channel, (_event, ...args) => callback({}, ...args));
    }
  }  
});

So the user can do:

// preload.ts
import electron from 'electron';
import { generatePreload } from 'eiphop';

generatePreload(electron);
shivekkhurana commented 2 years ago

Can you post a link to some docs? I don't know about this update.
In general, I'm always looking for enhancements to this project. But I feel that introducing this API will break existing projects, so we'll have to do a major release.

brumik commented 2 years ago

I don't think this is a breaking change, it's an extra function for the current release, I would call it a feature in schematic versioning.

Here is the post I found really useful, why is good to use the contextBridge: https://github.com/electron/electron/issues/9920#issuecomment-575839738