aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/index.ts')
-rw-r--r--packages/main/src/index.ts70
1 files changed, 25 insertions, 45 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts
index a886a16..128ae35 100644
--- a/packages/main/src/index.ts
+++ b/packages/main/src/index.ts
@@ -22,7 +22,6 @@
22import { readFileSync } from 'node:fs'; 22import { readFileSync } from 'node:fs';
23import { readFile } from 'node:fs/promises'; 23import { readFile } from 'node:fs/promises';
24import { arch } from 'node:os'; 24import { arch } from 'node:os';
25import path from 'node:path';
26import { URL } from 'node:url'; 25import { URL } from 'node:url';
27 26
28import { 27import {
@@ -46,7 +45,8 @@ import {
46 enableStacktraceSourceMaps, 45 enableStacktraceSourceMaps,
47 installDevToolsExtensions, 46 installDevToolsExtensions,
48 openDevToolsWhenReady, 47 openDevToolsWhenReady,
49} from './devTools'; 48} from './infrastructure/electron/impl/devTools';
49import getDistResources from './infrastructure/resources/impl/getDistResources';
50import initReactions from './initReactions'; 50import initReactions from './initReactions';
51import { createMainStore } from './stores/MainStore'; 51import { createMainStore } from './stores/MainStore';
52import { getLogger } from './utils/log'; 52import { getLogger } from './utils/log';
@@ -107,23 +107,12 @@ app.setAboutPanelOptions({
107 version: '', 107 version: '',
108}); 108});
109 109
110// eslint-disable-next-line unicorn/prefer-module -- Electron apps run in a commonjs environment. 110const resources = getDistResources(isDevelopment);
111const thisDir = __dirname;
112 111
113function getResourcePath(relativePath: string): string { 112const serviceInjectPath = resources.getPath('service-inject', 'index.js');
114 return path.join(thisDir, relativePath);
115}
116
117const baseUrl = `file://${thisDir}`;
118function getResourceUrl(relativePath: string): string {
119 return new URL(relativePath, baseUrl).toString();
120}
121
122const serviceInjectRelativePath = '../../service-inject/dist/index.js';
123const serviceInjectPath = getResourcePath(serviceInjectRelativePath);
124const serviceInject: WebSource = { 113const serviceInject: WebSource = {
125 code: readFileSync(serviceInjectPath, 'utf8'), 114 code: readFileSync(serviceInjectPath, 'utf8'),
126 url: getResourceUrl(serviceInjectRelativePath), 115 url: resources.getFileURL('service-inject', 'index.js'),
127}; 116};
128 117
129let mainWindow: BrowserWindow | undefined; 118let mainWindow: BrowserWindow | undefined;
@@ -139,36 +128,30 @@ initReactions(store)
139 log.log('Failed to initialize application', error); 128 log.log('Failed to initialize application', error);
140 }); 129 });
141 130
142const rendererBaseUrl = getResourceUrl('../renderer/'); 131const rendererBaseURL = resources.getRendererURL('/');
143function shouldCancelMainWindowRequest(url: string, method: string): boolean { 132function shouldCancelMainWindowRequest(url: string, method: string): boolean {
144 if (method !== 'GET') { 133 if (method !== 'GET') {
145 return true; 134 return true;
146 } 135 }
147 let normalizedUrl: string; 136 let normalizedURL: string;
148 try { 137 try {
149 normalizedUrl = new URL(url).toString(); 138 normalizedURL = new URL(url).toString();
150 } catch { 139 } catch {
151 return true; 140 return true;
152 } 141 }
153 if (isDevelopment) { 142 if (
154 if ( 143 isDevelopment &&
155 DEVMODE_ALLOWED_URL_PREFIXES.some((prefix) => 144 DEVMODE_ALLOWED_URL_PREFIXES.some((prefix) =>
156 normalizedUrl.startsWith(prefix), 145 normalizedURL.startsWith(prefix),
157 ) 146 )
158 ) { 147 ) {
159 return false; 148 return false;
160 }
161 if (import.meta.env.VITE_DEV_SERVER_URL !== undefined) {
162 const isHttp = normalizedUrl.startsWith(
163 import.meta.env.VITE_DEV_SERVER_URL,
164 );
165 const isWs = normalizedUrl.startsWith(
166 import.meta.env.VITE_DEV_SERVER_URL.replace(/^http:/, 'ws:'),
167 );
168 return !isHttp && !isWs;
169 }
170 } 149 }
171 return !normalizedUrl.startsWith(getResourceUrl(rendererBaseUrl)); 150 const isHttp = normalizedURL.startsWith(rendererBaseURL);
151 const isWs = normalizedURL.startsWith(
152 rendererBaseURL.replace(/^http:/, 'ws:'),
153 );
154 return !isHttp && !isWs;
172} 155}
173 156
174async function createWindow(): Promise<unknown> { 157async function createWindow(): Promise<unknown> {
@@ -178,7 +161,7 @@ async function createWindow(): Promise<unknown> {
178 webPreferences: { 161 webPreferences: {
179 sandbox: true, 162 sandbox: true,
180 devTools: isDevelopment, 163 devTools: isDevelopment,
181 preload: getResourcePath('../../preload/dist/index.cjs'), 164 preload: resources.getPath('preload', 'index.cjs'),
182 }, 165 },
183 }); 166 });
184 167
@@ -200,13 +183,10 @@ async function createWindow(): Promise<unknown> {
200 }, 183 },
201 ); 184 );
202 185
203 const pageUrl = 186 const pageURL = resources.getRendererURL('index.html');
204 isDevelopment && import.meta.env.VITE_DEV_SERVER_URL !== undefined
205 ? import.meta.env.VITE_DEV_SERVER_URL
206 : getResourceUrl('../renderer/dist/index.html');
207 187
208 webContents.on('will-navigate', (event, url) => { 188 webContents.on('will-navigate', (event, url) => {
209 if (url !== pageUrl) { 189 if (url !== pageURL) {
210 event.preventDefault(); 190 event.preventDefault();
211 } 191 }
212 }); 192 });
@@ -225,7 +205,7 @@ async function createWindow(): Promise<unknown> {
225 webPreferences: { 205 webPreferences: {
226 sandbox: true, 206 sandbox: true,
227 nodeIntegrationInSubFrames: true, 207 nodeIntegrationInSubFrames: true,
228 preload: getResourcePath('../../service-preload/dist/index.cjs'), 208 preload: resources.getPath('service-preload', 'index.cjs'),
229 partition: 'persist:service', 209 partition: 'persist:service',
230 }, 210 },
231 }); 211 });
@@ -370,7 +350,7 @@ async function createWindow(): Promise<unknown> {
370 log.error('Failed to load browser', error); 350 log.error('Failed to load browser', error);
371 }); 351 });
372 352
373 return mainWindow.loadURL(pageUrl); 353 return mainWindow.loadURL(pageURL);
374} 354}
375 355
376app.on('second-instance', () => { 356app.on('second-instance', () => {