Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respond with error if file/directory is not found #13

Merged
merged 3 commits into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const electron = require('electron');

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_ => {
try {
const result = await stat(path_);
Expand Down Expand Up @@ -35,10 +38,15 @@ module.exports = options => {
const handler = async (request, callback) => {
const indexPath = path.join(options.directory, 'index.html');
const filePath = path.join(options.directory, decodeURIComponent(new URL(request.url).pathname));
const resolvedPath = await getPath(filePath);

callback({
path: (await getPath(filePath)) || indexPath
});
if (resolvedPath || !path.extname(filePath) || path.extname(filePath) === '.html') {
callback({
path: resolvedPath || indexPath
});
} else {
callback({error: FILE_NOT_FOUND});
}
};

if (electron.protocol.registerStandardSchemes) {
Expand Down
14 changes: 14 additions & 0 deletions test/fixture-404-error.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('..');

serve({directory: __dirname});

let mainWindow;

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

mainWindow = new BrowserWindow();
mainWindow.loadURL('app://-/subdir/page.pdf');
})();
14 changes: 14 additions & 0 deletions test/fixture-dir-fallback.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('..');

serve({directory: __dirname});

let mainWindow;

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

mainWindow = new BrowserWindow();
mainWindow.loadURL('app://-/subdir/page.html');
})();
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,26 @@ test('allows special characters in file paths', async t => {
await client.waitUntilTextExists('h1', '🚀', 5000);
t.pass();
});

test('fallbacks to root index if unresolved path has .html extension or no extension', async t => {
t.context.spectron = new Application({
path: electron,
args: ['fixture-dir-fallback.js']
});
await t.context.spectron.start();
const {client} = t.context.spectron;
await client.waitUntilWindowLoaded();
await client.waitUntilTextExists('h1', '🦄', 5000);
t.pass();
});

test('throws error if unresolved path has an extension other than .html', async t => {
t.context.spectron = new Application({
path: electron,
args: ['fixture-404-error.js']
});
await t.context.spectron.start();
const {client} = t.context.spectron;
await client.waitUntilWindowLoaded();
await t.throwsAsync(client.waitUntilTextExists('h1', '🦄', 5000));
});