Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
259 lines (223 loc) · 5.96 KB
/
Copy pathmain.js
File metadata and controls
259 lines (223 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { app, BrowserWindow, Menu, shell, ipcMain } from 'electron';
import { fork } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
import getPort from 'get-port';
import updater from './updater.js';
import Store from 'electron-store';
const store = new Store({
encryptionKey: `elastron-${process.platform}-${process.arch}`,
clearInvalidConfig: true
});
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Lifecycle management for the server process
let serverProcess = null;
let serverPort = null;
const startServer = async () => {
if (process.env.npm_lifecycle_event === 'dev') {
serverPort = 5173;
return 5173;
}
serverPort = await getPort();
const serverPath = path.join(__dirname, 'build', 'index.js');
console.log(`Starting SvelteKit server at port ${serverPort}...`);
serverProcess = fork(serverPath, [], {
env: {
...process.env,
PORT: serverPort,
HOST: 'localhost',
ORIGIN: `http://localhost:${serverPort}`,
ADDRESS_HEADER: 'x-forwarded-for',
XFF_DEPTH: '1',
// adapter-node rejects request bodies over 512K by default, a guard
// meant for public servers. This one serves only the app's own
// windows on localhost, and assistant turns and bulk Playground
// requests can legitimately be larger.
BODY_SIZE_LIMIT: '50M'
}
});
let retries = 0;
while (retries < 20) {
try {
await fetch(`http://localhost:${serverPort}`);
console.log('Server is ready!');
return serverPort;
} catch (e) {
await new Promise(r => setTimeout(r, 500));
retries++;
}
}
throw new Error('Server failed to start');
};
// Open windows, in the order they were created (drives the Window menu list)
const openWindows = [];
const createWindow = (port, routeSuffix = '') => {
const mainWindow = new BrowserWindow({
width: 1440,
height: 960,
minWidth: 1280,
minHeight: 768,
titleBarStyle: 'hiddenInset',
show: true,
backgroundColor: '#000',
webPreferences: {
nodeIntegration: false, // Security: SvelteKit handles backend
contextIsolation: true,
devTools: true,
preload: path.join(__dirname, 'preload.js')
},
});
const url = `http://localhost:${port}${routeSuffix}`;
mainWindow.loadURL(url);
mainWindow.webContents.setWindowOpenHandler(({ url: targetUrl }) => {
if (/^https?:$/.test(new URL(targetUrl).protocol)) {
shell.openExternal(targetUrl);
}
return { action: 'deny' };
});
mainWindow.webContents.on('will-navigate', (e, targetUrl) => {
if (new URL(targetUrl).origin !== `http://localhost:${port}`) {
e.preventDefault();
shell.openExternal(targetUrl);
}
});
openWindows.push(mainWindow);
// Keep the Window menu in sync with what is actually open, focused and titled
mainWindow.on('closed', () => {
const index = openWindows.indexOf(mainWindow);
if (index >= 0) openWindows.splice(index, 1);
buildApplicationMenu();
});
mainWindow.on('focus', buildApplicationMenu);
// The title is applied right after this event, so rebuild on the next tick
mainWindow.webContents.on('page-title-updated', () => {
setImmediate(buildApplicationMenu);
});
buildApplicationMenu();
return mainWindow;
};
const focusWindow = (win) => {
if (win.isMinimized()) win.restore();
win.show();
win.focus();
};
const buildWindowMenuItems = () => {
const items = [
{ role: 'minimize' },
{ role: 'zoom' },
{ type: 'separator' },
{ role: 'close' }
];
if (process.platform === 'darwin') {
items.push({ type: 'separator' }, { role: 'front' });
}
if (openWindows.length) {
items.push({ type: 'separator' });
// Windows on the same connection share a title — number the duplicates
const seen = new Map();
openWindows.forEach((win, index) => {
const title = win.getTitle() || `Window ${index + 1}`;
const count = (seen.get(title) ?? 0) + 1;
seen.set(title, count);
items.push({
label: count > 1 ? `${title} (${count})` : title,
type: 'checkbox',
checked: win.isFocused(),
accelerator: index < 9 ? `CmdOrCtrl+${index + 1}` : undefined,
click: () => focusWindow(win)
});
});
}
return items;
};
function buildApplicationMenu() {
Menu.setApplicationMenu(
Menu.buildFromTemplate([
{
label: app.name,
submenu: [{ role: 'about' }, { role: 'quit' }],
},
{
role: 'editMenu'
},
{
label: 'Window',
submenu: buildWindowMenuItems()
},
{
role: 'help',
submenu: [
{
label: 'Learn More',
click: async () => {
await shell.openExternal('https://elastron.eney.solutions')
},
},
{
label: 'Check for Updates',
click: () => {
updater.checkForUpdates(true)
}
},
{
label: 'Debug',
click: () => {
const win = BrowserWindow.getFocusedWindow();
if (win) {
win.webContents.openDevTools();
}
}
}
]
}
])
);
}
let globalHandlersSetup = false;
function setupGlobalHandlers() {
if (globalHandlersSetup) return;
globalHandlersSetup = true;
ipcMain.on('header-doubleclick', (event) => {
const win = BrowserWindow.fromWebContents(event.sender);
if (!win) return;
if (win.isMaximized()) {
win.unmaximize();
} else {
win.maximize();
}
});
ipcMain.on('check-for-updates', () => {
updater.checkForUpdates();
});
ipcMain.on('restart-and-install', () => {
updater.restartAndInstall();
});
ipcMain.handle('store:get', (event, key, defaultValue) => {
return store.get(key, defaultValue);
});
ipcMain.on('store:set', (event, key, value) => {
store.set(key, value);
});
ipcMain.on('window:new', (event, routeSuffix) => {
if (serverPort) {
createWindow(serverPort, routeSuffix);
}
});
buildApplicationMenu();
}
app.whenReady().then(async () => {
try {
const port = await startServer();
setupGlobalHandlers();
const mainWindow = createWindow(port);
updater.init(mainWindow);
updater.checkForUpdates();
} catch (e) {
console.error('Failed to start app:', e);
app.quit();
}
});
app.on('will-quit', () => {
if (serverProcess)
serverProcess.kill();
});