mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-29 05:16:00 +00:00
electron initialization without remote
This commit is contained in:
@@ -159,8 +159,8 @@ function createWindow() {
|
|||||||
...bounds,
|
...bounds,
|
||||||
icon: os.platform() == 'win32' ? 'icon.ico' : path.resolve(__dirname, '../icon.png'),
|
icon: os.platform() == 'win32' ? 'icon.ico' : path.resolve(__dirname, '../icon.png'),
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
// nodeIntegration: true,
|
nodeIntegration: true,
|
||||||
// contextIsolation: false,
|
contextIsolation: false,
|
||||||
// enableRemoteModule: true,
|
// enableRemoteModule: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -172,7 +172,7 @@ function createWindow() {
|
|||||||
mainMenu = buildMenu();
|
mainMenu = buildMenu();
|
||||||
mainWindow.setMenu(mainMenu);
|
mainWindow.setMenu(mainMenu);
|
||||||
|
|
||||||
function loadMainWindow() {
|
function loadMainWindow(initArgs) {
|
||||||
const startUrl =
|
const startUrl =
|
||||||
process.env.ELECTRON_START_URL ||
|
process.env.ELECTRON_START_URL ||
|
||||||
url.format({
|
url.format({
|
||||||
@@ -181,7 +181,16 @@ function createWindow() {
|
|||||||
slashes: true,
|
slashes: true,
|
||||||
});
|
});
|
||||||
mainWindow.webContents.on('did-finish-load', function () {
|
mainWindow.webContents.on('did-finish-load', function () {
|
||||||
// hideSplash();
|
mainWindow.webContents.executeJavaScript(
|
||||||
|
`runInit=()=>{
|
||||||
|
try{
|
||||||
|
dbgate_initializeElectron(${JSON.stringify(initArgs)});
|
||||||
|
}catch(e){
|
||||||
|
setTimeout(runInit,100)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
runInit()`
|
||||||
|
);
|
||||||
});
|
});
|
||||||
mainWindow.on('close', () => {
|
mainWindow.on('close', () => {
|
||||||
store.set('winBounds', mainWindow.getBounds());
|
store.set('winBounds', mainWindow.getBounds());
|
||||||
@@ -194,7 +203,7 @@ function createWindow() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (process.env.ELECTRON_START_URL) {
|
if (process.env.ELECTRON_START_URL) {
|
||||||
loadMainWindow();
|
loadMainWindow({});
|
||||||
} else {
|
} else {
|
||||||
const apiProcess = fork(path.join(__dirname, '../packages/api/dist/bundle.js'), [
|
const apiProcess = fork(path.join(__dirname, '../packages/api/dist/bundle.js'), [
|
||||||
'--dynport',
|
'--dynport',
|
||||||
@@ -206,9 +215,11 @@ function createWindow() {
|
|||||||
apiProcess.on('message', msg => {
|
apiProcess.on('message', msg => {
|
||||||
if (msg.msgtype == 'listening') {
|
if (msg.msgtype == 'listening') {
|
||||||
const { port, authorization } = msg;
|
const { port, authorization } = msg;
|
||||||
global['port'] = port;
|
|
||||||
global['authorization'] = authorization;
|
loadMainWindow({
|
||||||
loadMainWindow();
|
port,
|
||||||
|
authorization,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,10 +12,16 @@
|
|||||||
import axiosInstance from './utility/axiosInstance';
|
import axiosInstance from './utility/axiosInstance';
|
||||||
import ErrorHandler from './utility/ErrorHandler.svelte';
|
import ErrorHandler from './utility/ErrorHandler.svelte';
|
||||||
import OpenTabsOnStartup from './utility/OpenTabsOnStartup.svelte';
|
import OpenTabsOnStartup from './utility/OpenTabsOnStartup.svelte';
|
||||||
|
import { shouldWaitForElectronInitialize } from './utility/getElectron';
|
||||||
|
|
||||||
let loadedApi = false;
|
let loadedApi = false;
|
||||||
|
|
||||||
async function loadApi() {
|
async function loadApi() {
|
||||||
|
if (shouldWaitForElectronInitialize()) {
|
||||||
|
setTimeout(loadApi, 100);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const settings = await axiosInstance.get('config/get-settings');
|
const settings = await axiosInstance.get('config/get-settings');
|
||||||
const connections = await axiosInstance.get('connections/list');
|
const connections = await axiosInstance.get('connections/list');
|
||||||
@@ -43,7 +49,6 @@
|
|||||||
setAppLoaded();
|
setAppLoaded();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<DataGridRowHeightMeter />
|
<DataGridRowHeightMeter />
|
||||||
|
|||||||
@@ -74,8 +74,6 @@ export const currentThemeDefinition = derived([currentTheme, extensions], ([$cur
|
|||||||
$extensions.themes.find(x => x.className == $currentTheme)
|
$extensions.themes.find(x => x.className == $currentTheme)
|
||||||
);
|
);
|
||||||
|
|
||||||
const electron = getElectron();
|
|
||||||
|
|
||||||
subscribeCssVariable(selectedWidget, x => (x ? 1 : 0), '--dim-visible-left-panel');
|
subscribeCssVariable(selectedWidget, x => (x ? 1 : 0), '--dim-visible-left-panel');
|
||||||
subscribeCssVariable(visibleToolbar, x => (x ? 1 : 0), '--dim-visible-toolbar');
|
subscribeCssVariable(visibleToolbar, x => (x ? 1 : 0), '--dim-visible-toolbar');
|
||||||
subscribeCssVariable(leftPanelWidth, x => `${x}px`, '--dim-left-panel-width');
|
subscribeCssVariable(leftPanelWidth, x => `${x}px`, '--dim-left-panel-width');
|
||||||
@@ -119,9 +117,9 @@ let commandsValue = null;
|
|||||||
commands.subscribe(value => {
|
commands.subscribe(value => {
|
||||||
commandsValue = value;
|
commandsValue = value;
|
||||||
|
|
||||||
|
const electron = getElectron();
|
||||||
if (electron) {
|
if (electron) {
|
||||||
const { ipcRenderer } = electron;
|
electron.send('update-commands', JSON.stringify(value));
|
||||||
ipcRenderer.send('update-commands', JSON.stringify(value));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
export const getCommands = () => commandsValue;
|
export const getCommands = () => commandsValue;
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import resolveApi, { resolveApiHeaders } from './resolveApi';
|
import resolveApi, { resolveApiHeaders } from './resolveApi';
|
||||||
|
|
||||||
const axiosInstance = axios.create({
|
let axiosInstance;
|
||||||
baseURL: resolveApi(),
|
|
||||||
});
|
|
||||||
|
|
||||||
axiosInstance.defaults.headers = {
|
function recreateAxiosInstance() {
|
||||||
'Cache-Control': 'no-cache',
|
axiosInstance = axios.create({
|
||||||
Pragma: 'no-cache',
|
baseURL: resolveApi(),
|
||||||
Expires: '0',
|
});
|
||||||
...resolveApiHeaders(),
|
|
||||||
};
|
axiosInstance.defaults.headers = {
|
||||||
|
'Cache-Control': 'no-cache',
|
||||||
|
Pragma: 'no-cache',
|
||||||
|
Expires: '0',
|
||||||
|
...resolveApiHeaders(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
window['dbgate_recreateAxiosInstance'] = recreateAxiosInstance;
|
||||||
|
|
||||||
|
recreateAxiosInstance();
|
||||||
|
|
||||||
export default axiosInstance;
|
export default axiosInstance;
|
||||||
|
|||||||
@@ -1,7 +1,57 @@
|
|||||||
export default function getElectron() {
|
class ElectronApi {
|
||||||
|
public port?: number;
|
||||||
|
public authorization?: string;
|
||||||
|
private ipcRenderer = getIpcRenderer();
|
||||||
|
|
||||||
|
constructor(args) {
|
||||||
|
this.port = args.port;
|
||||||
|
this.authorization = args.authorization;
|
||||||
|
}
|
||||||
|
|
||||||
|
send(msg, args) {
|
||||||
|
this.ipcRenderer.send(msg, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let apiInstance = null;
|
||||||
|
|
||||||
|
function initializeElectron(args) {
|
||||||
|
// console.log('Initialize electron with args:', args);
|
||||||
|
|
||||||
|
apiInstance = new ElectronApi(args);
|
||||||
|
if (window['dbgate_recreateAxiosInstance']) {
|
||||||
|
// console.log('Recreating axios instance');
|
||||||
|
|
||||||
|
window['dbgate_recreateAxiosInstance']();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window['dbgate_initializeElectron'] = initializeElectron;
|
||||||
|
|
||||||
|
function getIpcRenderer() {
|
||||||
if (window['require']) {
|
if (window['require']) {
|
||||||
const electron = window['require']('electron');
|
const electron = window['require']('electron');
|
||||||
return electron;
|
return electron?.ipcRenderer;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function shouldWaitForElectronInitialize() {
|
||||||
|
return !!getIpcRenderer() && !apiInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function getElectron(): ElectronApi {
|
||||||
|
return apiInstance;
|
||||||
|
// try {
|
||||||
|
// // @ts-ignore
|
||||||
|
// return ipcRenderer;
|
||||||
|
// } catch (e) {
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
// if (window['require']) {
|
||||||
|
// const electron = window['require']('electron');
|
||||||
|
// console.log('electron?.ipcRenderer', electron?.ipcRenderer);
|
||||||
|
// return electron?.ipcRenderer;
|
||||||
|
// }
|
||||||
|
// return null;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,18 +1,14 @@
|
|||||||
|
import getElectron from './getElectron';
|
||||||
|
|
||||||
let apiUrl = null;
|
let apiUrl = null;
|
||||||
try {
|
try {
|
||||||
apiUrl = process.env.API_URL;
|
apiUrl = process.env.API_URL;
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
export default function resolveApi() {
|
export default function resolveApi() {
|
||||||
if (window['require']) {
|
const electron = getElectron();
|
||||||
const electron = window['require']('electron');
|
if (electron?.port) {
|
||||||
|
return `http://localhost:${electron.port}`;
|
||||||
if (electron) {
|
|
||||||
const port = electron.remote.getGlobal('port');
|
|
||||||
if (port) {
|
|
||||||
return `http://localhost:${port}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (apiUrl) {
|
if (apiUrl) {
|
||||||
@@ -22,14 +18,13 @@ export default function resolveApi() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function resolveApiHeaders() {
|
export function resolveApiHeaders() {
|
||||||
if (window['require']) {
|
const electron = getElectron();
|
||||||
const electron = window['require']('electron');
|
|
||||||
|
|
||||||
if (electron) {
|
if (electron?.authorization) {
|
||||||
return {
|
return {
|
||||||
Authorization: electron.remote.getGlobal('authorization'),
|
Authorization: electron.authorization,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user