added an option to prevent another process from being created

This commit is contained in:
antelle 2021-02-09 20:19:12 +01:00
parent 87d760708d
commit ee10894f5f
No known key found for this signature in database
GPG Key ID: 63C9777AAB7C563C
3 changed files with 68 additions and 46 deletions

View File

@ -1,7 +1,7 @@
let perfTimestamps = [{ name: 'pre-init', ts: process.hrtime() }];
if (process.send && process.argv.includes('--native-module-host')) {
require('./native-module-host');
require('./native-module-host').startInOwnProcess();
return;
}

View File

@ -5,8 +5,7 @@ const YubiKeyVendorIds = [0x1050];
const attachedYubiKeys = [];
let usbListenerRunning = false;
let autoType;
startListener();
let callback;
const messageHandlers = {
start() {},
@ -240,38 +239,49 @@ function kbdTextAsKeys(str, modifiers) {
}
}
function startListener() {
process.on('message', ({ callId, cmd, args }) => {
Promise.resolve()
.then(() => {
const handler = messageHandlers[cmd];
if (handler) {
return handler(...args);
} else {
throw new Error(`Handler not found: ${cmd}`);
}
})
.then((result) => {
callback('result', { callId, result });
})
.catch((error) => {
error = {
name: error.name,
message: error.message,
stack: error.stack,
code: error.code
};
callback('result', { callId, error });
});
});
function handleMessage({ callId, cmd, args }) {
Promise.resolve()
.then(() => {
const handler = messageHandlers[cmd];
if (handler) {
return handler(...args);
} else {
throw new Error(`Handler not found: ${cmd}`);
}
})
.then((result) => {
callback('result', { callId, result });
})
.catch((error) => {
error = {
name: error.name,
message: error.message,
stack: error.stack,
code: error.code
};
callback('result', { callId, error });
});
}
function startInOwnProcess() {
callback = (cmd, ...args) => {
try {
process.send({ cmd, args });
} catch {}
};
process.on('message', handleMessage);
process.on('disconnect', () => {
process.exit(0);
});
}
function callback(cmd, ...args) {
try {
process.send({ cmd, args });
} catch {}
function startInMain(channel) {
channel.on('send', handleMessage);
callback = (cmd, ...args) => {
channel.emit('message', { cmd, args });
};
}
module.exports = { startInOwnProcess, startInMain };

View File

@ -1,26 +1,38 @@
const path = require('path');
const { spawn } = require('child_process');
const { EventEmitter } = require('events');
let callbackWebContents;
let nativeModuleHost;
const spawnAnotherProcess = false;
function startHost() {
const exeName = path.basename(process.execPath, '.exe');
const args = ['--native-module-host', '--in-process-gpu', '--disable-gpu'];
if (exeName === 'Electron') {
args.unshift(path.join(process.mainModule.path, 'main.js'));
if (spawnAnotherProcess) {
const exeName = path.basename(process.execPath, '.exe');
const args = ['--native-module-host', '--in-process-gpu', '--disable-gpu'];
if (exeName === 'Electron') {
args.unshift(path.join(process.mainModule.path, 'main.js'));
}
nativeModuleHost = spawn(process.helperExecPath, args, {
env: process.env,
cwd: process.cwd(),
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
});
nativeModuleHost.on('message', onHostMessage);
nativeModuleHost.on('error', onHostError);
nativeModuleHost.on('exit', onHostExit);
nativeModuleHost.on('disconnect', onHostDisconnect);
} else {
nativeModuleHost = new EventEmitter();
nativeModuleHost.on('message', onHostMessage);
nativeModuleHost.send = (msg) => {
nativeModuleHost.emit('send', msg);
};
require('../../native-module-host').startInMain(nativeModuleHost);
}
nativeModuleHost = spawn(process.helperExecPath, args, {
env: process.env,
cwd: process.cwd(),
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
});
nativeModuleHost.on('message', onHostMessage);
nativeModuleHost.on('error', onHostError);
nativeModuleHost.on('exit', onHostExit);
nativeModuleHost.on('disconnect', onHostDisconnect);
}
function nativeModuleCall(event, msg) {