Skip to content

Commit

Permalink
Support loading an HTML file with a custom name (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
mhkeller and sindresorhus authored Jan 22, 2024
1 parent 2968431 commit c307a57
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ declare namespace electronServe {
*/
hostname?: string;

/**
Custom HTML filename. This gets appended with `'.html'`.
@default 'index'
*/
file?: string;

/**
Whether [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) should be enabled.
Useful for testing purposes.
Expand Down
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const stat = promisify(fs.stat);
// See https://cs.chromium.org/chromium/src/net/base/net_error_list.h
const FILE_NOT_FOUND = -6;

const getPath = async path_ => {
const getPath = async (path_, file) => {
try {
const result = await stat(path_);

Expand All @@ -18,7 +18,7 @@ const getPath = async path_ => {
}

if (result.isDirectory()) {
return getPath(path.join(path_, 'index.html'));
return getPath(path.join(path_, `${file}.html`));
}
} catch (_) {}
};
Expand All @@ -27,7 +27,8 @@ module.exports = options => {
options = Object.assign({
isCorsEnabled: true,
scheme: 'app',
hostname: '-'
hostname: '-',
file: 'index'
}, options);

if (!options.directory) {
Expand All @@ -37,9 +38,9 @@ module.exports = options => {
options.directory = path.resolve(electron.app.getAppPath(), options.directory);

const handler = async (request, callback) => {
const indexPath = path.join(options.directory, 'index.html');
const indexPath = path.join(options.directory, `${options.file}.html`);
const filePath = path.join(options.directory, decodeURIComponent(new URL(request.url).pathname));
const resolvedPath = await getPath(filePath);
const resolvedPath = await getPath(filePath, options.file);
const fileExtension = path.extname(filePath);

if (resolvedPath || !fileExtension || fileExtension === '.html' || fileExtension === '.asar') {
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ Default: `'-'`

Custom hostname.

##### file

Type: `string`\
Default: `'index'`

Custom HTML filename. This gets appended with `'.html'`.

##### isCorsEnabled

Type: `boolean`\
Expand Down
14 changes: 14 additions & 0 deletions test/fixture-custom-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const {app, BrowserWindow} = require('electron');
const serve = require('..');

const loadUrl = serve({directory: __dirname, file: 'owl'});

let mainWindow;

(async () => {
await app.whenReady();

mainWindow = new BrowserWindow();
loadUrl(mainWindow);
})();
9 changes: 9 additions & 0 deletions test/owl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>🦉</h1>
</body>
</html>
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ test('throws error if unresolved path has an extension other than .html', async
await t.throwsAsync(client.waitUntilTextExists('h1', '🦄', 5000));
t.pass();
});

test('serves directory custom file', async t => {
t.context.spectron = new Application({
path: electron,
args: ['fixture-custom-file.js']
});
await t.context.spectron.start();
const {client} = t.context.spectron;
await client.waitUntilWindowLoaded();
await client.waitUntilTextExists('h1', '🦉', 5000);
t.pass();
});

0 comments on commit c307a57

Please sign in to comment.