Skip to content

Commit 8eed33e

Browse files
hadeebsindresorhus
authored andcommittedJul 6, 2019
Respond with error if file/directory is not found (#13)
1 parent afbddf2 commit 8eed33e

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed
 

‎index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const electron = require('electron');
66

77
const stat = promisify(fs.stat);
88

9+
// See https://cs.chromium.org/chromium/src/net/base/net_error_list.h
10+
const FILE_NOT_FOUND = -6;
11+
912
const getPath = async path_ => {
1013
try {
1114
const result = await stat(path_);
@@ -35,10 +38,15 @@ module.exports = options => {
3538
const handler = async (request, callback) => {
3639
const indexPath = path.join(options.directory, 'index.html');
3740
const filePath = path.join(options.directory, decodeURIComponent(new URL(request.url).pathname));
41+
const resolvedPath = await getPath(filePath);
3842

39-
callback({
40-
path: (await getPath(filePath)) || indexPath
41-
});
43+
if (resolvedPath || !path.extname(filePath) || path.extname(filePath) === '.html') {
44+
callback({
45+
path: resolvedPath || indexPath
46+
});
47+
} else {
48+
callback({error: FILE_NOT_FOUND});
49+
}
4250
};
4351

4452
if (electron.protocol.registerStandardSchemes) {

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"url"
3737
],
3838
"devDependencies": {
39-
"ava": "^1.4.1",
39+
"ava": "^2.1.0",
4040
"electron": "^3.0.0",
4141
"spectron": "^5.0.0",
4242
"tsd": "^0.7.3",

‎test/fixture-404-error.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
const {app, BrowserWindow} = require('electron');
3+
const serve = require('..');
4+
5+
serve({directory: __dirname});
6+
7+
let mainWindow;
8+
9+
(async () => {
10+
await app.whenReady();
11+
12+
mainWindow = new BrowserWindow();
13+
mainWindow.loadURL('app://-/subdir/page.pdf');
14+
})();

‎test/fixture-dir-fallback.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
const {app, BrowserWindow} = require('electron');
3+
const serve = require('..');
4+
5+
serve({directory: __dirname});
6+
7+
let mainWindow;
8+
9+
(async () => {
10+
await app.whenReady();
11+
12+
mainWindow = new BrowserWindow();
13+
mainWindow.loadURL('app://-/subdir/page.html');
14+
})();

‎test/test.js

+24
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,27 @@ test('allows special characters in file paths', async t => {
3030
await client.waitUntilTextExists('h1', '🚀', 5000);
3131
t.pass();
3232
});
33+
34+
test('fallbacks to root index if unresolved path has .html extension or no extension', async t => {
35+
t.context.spectron = new Application({
36+
path: electron,
37+
args: ['fixture-dir-fallback.js']
38+
});
39+
await t.context.spectron.start();
40+
const {client} = t.context.spectron;
41+
await client.waitUntilWindowLoaded();
42+
await client.waitUntilTextExists('h1', '🦄', 5000);
43+
t.pass();
44+
});
45+
46+
test('throws error if unresolved path has an extension other than .html', async t => {
47+
t.context.spectron = new Application({
48+
path: electron,
49+
args: ['fixture-404-error.js']
50+
});
51+
await t.context.spectron.start();
52+
const {client} = t.context.spectron;
53+
await client.waitUntilWindowLoaded();
54+
await t.throwsAsync(client.waitUntilTextExists('h1', '🦄', 5000));
55+
t.pass();
56+
});

0 commit comments

Comments
 (0)
Please sign in to comment.