diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-01-27 16:49:11 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-02-08 21:43:17 +0100 |
commit | 852c3b0eaed48265354046d068f0cfa565827e7c (patch) | |
tree | 858a60359dda635da2647900564e4963f68d57df /packages/main/src/index.ts | |
parent | chore: Annotate shared packages for purity (diff) | |
download | sophie-852c3b0eaed48265354046d068f0cfa565827e7c.tar.gz sophie-852c3b0eaed48265354046d068f0cfa565827e7c.tar.zst sophie-852c3b0eaed48265354046d068f0cfa565827e7c.zip |
refactor: Extract resource path management
Lets us access absolute paths and URLs without directly calling node
APIs.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/main/src/index.ts')
-rw-r--r-- | packages/main/src/index.ts | 70 |
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 @@ | |||
22 | import { readFileSync } from 'node:fs'; | 22 | import { readFileSync } from 'node:fs'; |
23 | import { readFile } from 'node:fs/promises'; | 23 | import { readFile } from 'node:fs/promises'; |
24 | import { arch } from 'node:os'; | 24 | import { arch } from 'node:os'; |
25 | import path from 'node:path'; | ||
26 | import { URL } from 'node:url'; | 25 | import { URL } from 'node:url'; |
27 | 26 | ||
28 | import { | 27 | import { |
@@ -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'; |
49 | import getDistResources from './infrastructure/resources/impl/getDistResources'; | ||
50 | import initReactions from './initReactions'; | 50 | import initReactions from './initReactions'; |
51 | import { createMainStore } from './stores/MainStore'; | 51 | import { createMainStore } from './stores/MainStore'; |
52 | import { getLogger } from './utils/log'; | 52 | import { 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. | 110 | const resources = getDistResources(isDevelopment); |
111 | const thisDir = __dirname; | ||
112 | 111 | ||
113 | function getResourcePath(relativePath: string): string { | 112 | const serviceInjectPath = resources.getPath('service-inject', 'index.js'); |
114 | return path.join(thisDir, relativePath); | ||
115 | } | ||
116 | |||
117 | const baseUrl = `file://${thisDir}`; | ||
118 | function getResourceUrl(relativePath: string): string { | ||
119 | return new URL(relativePath, baseUrl).toString(); | ||
120 | } | ||
121 | |||
122 | const serviceInjectRelativePath = '../../service-inject/dist/index.js'; | ||
123 | const serviceInjectPath = getResourcePath(serviceInjectRelativePath); | ||
124 | const serviceInject: WebSource = { | 113 | const 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 | ||
129 | let mainWindow: BrowserWindow | undefined; | 118 | let 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 | ||
142 | const rendererBaseUrl = getResourceUrl('../renderer/'); | 131 | const rendererBaseURL = resources.getRendererURL('/'); |
143 | function shouldCancelMainWindowRequest(url: string, method: string): boolean { | 132 | function 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 | ||
174 | async function createWindow(): Promise<unknown> { | 157 | async 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 | ||
376 | app.on('second-instance', () => { | 356 | app.on('second-instance', () => { |