eiphop icon indicating copy to clipboard operation
eiphop copied to clipboard

Export preload function

Open brumik opened this issue 3 years ago • 2 comments

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);

brumik avatar Dec 19 '21 15:12 brumik

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.

shivekkhurana avatar Dec 19 '21 18:12 shivekkhurana

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

brumik avatar Dec 19 '21 19:12 brumik