Skip to content

Commit 8a4c0e8

Browse files
committed
fix: calculating path to file url; rename modules
1 parent dd180c1 commit 8a4c0e8

File tree

5 files changed

+23
-30
lines changed

5 files changed

+23
-30
lines changed

electron/main/app.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ logger.debug('app paths', {
6464
// This is necessary for both security and for single-page apps.
6565
// https://bishopfox.com/blog/reasonably-secure-electron
6666
if (appEnvIsProd) {
67-
const { prodServe } = await import('./electron-next/prod-server.js');
68-
prodServe({
67+
const { serve } = await import('./electron-next/serve.prod.js');
68+
serve({
6969
scheme: prodAppScheme,
7070
dirPath: prodRendererPath,
7171
});
@@ -80,8 +80,8 @@ const createMainWindow = async (): Promise<void> => {
8080
// If running in development, serve the renderer from localhost.
8181
// This must be done once the app is ready.
8282
// This enables hot reloading of the renderer.
83-
const { devServe } = await import('./electron-next/dev-server.js');
84-
await devServe({
83+
const { serve } = await import('./electron-next/serve.dev.js');
84+
await serve({
8585
port: devPort,
8686
dirPath: devRendererPath,
8787
});

electron/main/electron-next/__tests__/path-to-file-url.test.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ describe('path-to-file-url', () => {
1313
});
1414

1515
describe('#pathToFileURL', () => {
16-
it('returns absolute file url to directory path joined with file path', async () => {
17-
expect(
18-
pathToFileURL({
19-
dirPath: '/a/b/',
20-
filePath: 'c.html',
21-
})
22-
).toEqual('file:///a/b/c.html');
16+
it('prepends file protocol when given absolute path', async () => {
17+
const filePath = '/a/b/c.html';
18+
expect(pathToFileURL(filePath)).toEqual(`file://${filePath}`);
19+
});
20+
21+
it('prepends file protocol and working directory when given relative path', async () => {
22+
const cwd = process.cwd();
23+
const filePath = 'a/b/c.html';
24+
expect(pathToFileURL(filePath)).toEqual(`file://${cwd}/${filePath}`);
2325
});
2426
});
2527
});
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
import path from 'node:path';
21
import url from 'node:url';
32

43
/**
5-
* Converts a relative file path to an absolute file URL.
6-
* Example: 'file.txt' -> 'file:///path/to/file.txt'
4+
* Converts a file path to an absolute file URL.
5+
* For most reliable results, provide an absolute file path.
6+
* Example: '/path/to/file.txt' -> 'file:///path/to/file.txt'
77
*/
8-
export const pathToFileURL = (options: {
9-
/**
10-
* The directory path to resolve the file path against.
11-
*/
12-
dirPath: string;
13-
/**
14-
* The relative file path to convert to a file URL.
15-
*/
16-
filePath: string;
17-
}): string => {
18-
const { dirPath, filePath } = options;
19-
return url.pathToFileURL(path.join(dirPath, filePath)).toString();
8+
export const pathToFileURL = (filePath: string): string => {
9+
return url.pathToFileURL(filePath).toString();
2010
};

electron/main/electron-next/dev-server.ts electron/main/electron-next/serve.dev.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { NextServer, NextServerOptions } from 'next/dist/server/next.js';
1313
import { runInBackground } from '../async/run-in-background.js';
1414
import { logger } from './logger.js';
1515

16-
export const devServe = async (options: {
16+
export const serve = async (options: {
1717
/**
1818
* The directory to serve, relative to the app root directory.
1919
*/

electron/main/electron-next/prod-server.ts electron/main/electron-next/serve.prod.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { isSafePath } from './is-safe-path.js';
1919
import { logger } from './logger.js';
2020
import { pathToFileURL } from './path-to-file-url.js';
2121

22-
export const prodServe = (options: {
22+
export const serve = (options: {
2323
/**
2424
* The protocol to serve the directory on.
2525
* All URL requests that use this protocol will be served from the directory.
@@ -37,7 +37,8 @@ export const prodServe = (options: {
3737
dirPath,
3838
});
3939

40-
const error404Page = pathToFileURL({ dirPath, filePath: '404.html' });
40+
const error404Path = path.join(dirPath, '404.html');
41+
const error404Page = pathToFileURL(error404Path);
4142

4243
const requestHandler = async (httpReq: Request): Promise<Response> => {
4344
const requestURL = new URL(httpReq.url);
@@ -54,7 +55,7 @@ export const prodServe = (options: {
5455
const isSafe = isSafePath({ dirPath, filePath: pathToServe });
5556

5657
if (isSafe) {
57-
pageToServe = pathToFileURL({ dirPath, filePath: pathToServe });
58+
pageToServe = pathToFileURL(pathToServe);
5859
} else {
5960
pageToServe = error404Page;
6061
}

0 commit comments

Comments
 (0)