Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
electron-re
Advanced tools
Using electron-re
to generate some service processs and communicate between main process
,render process
and service
. In some Best Practices
of electron tutorials, it suggest to put your code that occupy the CPU into rendering process instead of in main process, exactly electron-re
means to do.
The service
process is a customized render process that works in the background, receiving path
, options
as arguments:
const { BrowserService } = require('electron');
const myServcie = new BrowserService('app', path.join(__dirname, 'path/to/app.service.js'));
new BrowserWindow()
options
/* --- package.json --- */
{
...
scripts: {
// method1: declare dev env
start: 'cross-env NODE_ENV=dev electron index.js',
// method2: declare development env
start: 'cross-env NODE_ENV=development electron index.js',
}
...
}
/* --- main.js --- */
// can also set nodeEnv directly instead of declaring it in package.json
global.nodeEnv = 'dev';
const myService = new BrowserService('app', 'path/to/app.service.js', {
...
// when webSecurity closed and in dev mode
// the service will reload after code changed
webPreferences: { webSecurity: false }
});
In order to send data from main or other process to a service you need use MesssageChannel
, such as: MessageChannel.send('service-name', 'channel', 'params')
The service is a customized BrowserWindow
instance, it has only method connected()
which return a resolved Promise
when service is ready, suggest to put some business-related code into a service.
/* --- main.js --- */
const { BrowserService } = require('electron-re');
...
app.whenReady().then(() => {
// after app is ready in main process
const myService = new BrowserService('app', 'path/to/app.service.js');
myService.connected()
.then(() => {
// use the electron build-in method to send data
mhyService.webContents.send('channel1', { value: 'test1' });
...
})
.catch((err) => console.log(`Error in service-app : ${err}`));
});
/* --- app.service.js --- */
const { ipcRenderer } = require('electron');
ipcRenderer.on('channel1', (event, result) => {
// works
...
});
This is a messaging tool expanding some method from electron build-in ipc:
/* --- main --- */
const { BrowserService, MessageChannel } = require('electron-re');
...
// after app is ready in main process
app.whenReady().then(() => {
const myService = new BrowserService('app', 'path/to/app.service.js');
myService.connected().then(() => {
// open devtools in dev mode for debugging
if (isInDev) myService.openDevTools();
// send data to a service - like the build-in ipcMain.send
MessageChannel.send('app', 'channel1', { value: 'test1' });
// send data to a service and return a Promise - extension method
MessageChannel.invoke('app', 'channel2', { value: 'test1' }).then((response) => {
console.log(response);
});
// listen a channel, same as ipcMain.on
MessageChannel.on('channel3', (event, response) => {
console.log(response);
});
// handle a channel signal, same as ipcMain.handle
// you can return data directly or return a Promise instance
MessageChannel.handle('channel4', (event, response) => {
console.log(response);
return { res: 'channel4-res' };
});
})
});
/* --- service-app --- */
const { ipcRenderer } = require('electron');
const { MessageChannel } = require('electron-re');
// listen a channel, same as ipcRenderer.on
MessageChannel.on('channel1', (event, result) => {
console.log(result);
});
// handle a channel signal, just like ipcMain.handle
MessageChannel.handle('channel2', (event, result) => {
console.log(result);
return { response: 'channel2-response' }
});
// send data to another service and return a promise , just like ipcRenderer.invoke
MessageChannel.invoke('app2', 'channel3' (event, result) => {
console.log(result);
});
// send data to a service - like the build-in ipcRenderer.send
MessageChannel.send('app', 'channel4', { value: 'channel4' });
/* --- service-app2 --- */
// handle a channel signal, just like ipcMain.handle
MessageChannel.handle('channel3', (event, result) => {
console.log(result);
return { response: 'channel3-response' }
});
// listen a channel, same as ipcRenderer.once
MessageChannel.once('channel4', (event, result) => {
console.log(result);
});
// send data to main process, just like ipcRenderer.send
MessageChannel.send('main', 'channel3', { value: 'channel3' });
// send data to main process and return a Promise, just like ipcRenderer.invoke
MessageChannel.invoke('main', 'channel4', { value: 'channel4' });
/* --- render process --- */
const { ipcRenderer } = require('electron');
const { MessageChannel } = rrequire('electron-re');
// send data to a service
MessageChannel.send('app', ....);
MessageChannel.invoke('app', ....);
// send data to main process
MessageChannel.send('main', ....);
MessageChannel.invoke('main', ....);
electronux - this is a project of mine that uses electron-re
, also you can check the index.test.js
and test
dir in root, there are some cases, then run npm run test
to see test result of the library.
FAQs
Electron Process Manager
The npm package electron-re receives a total of 39 weekly downloads. As such, electron-re popularity was classified as not popular.
We found that electron-re demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.