Skip to content

Commit 423a84b

Browse files
author
Ben Demboski
committed
Simplify index file logic
The directoryIndexFile/indexPath logic was unnecessarily complicated, and would do the wrong thing if only indexPath was specified. This simlifies and fixes that issue.
1 parent 2e247a4 commit 423a84b

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { join } = require('path');
12
const createHandler = require('./lib/handler');
23

34
function requiredParam(param, errorMessage) {
@@ -16,8 +17,7 @@ function requiredParam(param, errorMessage) {
1617
* @param {Object} options.protocol electron.protocol
1718
* @param {String} options.name name of your protocol, defaults to `serve`
1819
* @param {String} options.endpoint endpoint of your protocol, defaults to `dist`
19-
* @param {String} options.directoryIndexFile directory index. usally the default, `index.html`
20-
* @param {String} options.indexPath defaults to cwd + directoryIndexFile
20+
* @param {String} options.indexPath defaults to 'index.html' in cwd
2121
*
2222
* @return {String} name of your protocol
2323
*/
@@ -27,15 +27,16 @@ module.exports = function protocolServe({
2727
protocol,
2828
name = 'serve',
2929
endpoint = 'dist',
30-
directoryIndexFile = 'index.html',
3130
indexPath = undefined,
3231
}) {
3332
requiredParam(cwd, 'cwd must be specified, should be a valid path');
3433
requiredParam(protocol, 'protocol must be specified, should be electron.protocol');
3534
requiredParam(app, 'app must be specified, should be electron.app');
3635

36+
indexPath = indexPath || join(cwd, 'index.html');
37+
3738
app.on('ready', () => {
38-
const options = { cwd, name, endpoint, directoryIndexFile, indexPath };
39+
const options = { cwd, name, endpoint, indexPath };
3940
protocol.registerFileProtocol(name, createHandler(options), error => {
4041
if (error) {
4142
console.error('Failed to register protocol');

lib/handler.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ module.exports = function createHandler({
66
cwd,
77
name,
88
endpoint,
9-
directoryIndexFile,
109
indexPath,
1110
}) {
12-
indexPath = indexPath || join(cwd, directoryIndexFile);
1311
const cache = {};
1412

1513
return ({ url }, callback) => {
@@ -27,25 +25,22 @@ module.exports = function createHandler({
2725
}
2826

2927
const pathSegments = pathname.split('/').filter(segment => segment !== '');
30-
31-
if (pathSegments.length === 0) {
32-
pathSegments.push(directoryIndexFile);
33-
}
34-
3528
const filepath = join(cwd, ...pathSegments);
3629

3730
// Basic request caching
3831
if (!cache[url]) {
3932
try {
4033
fs.accessSync(filepath);
4134

42-
cache[url] = filepath;
35+
if (fs.statSync(filepath).isFile()) {
36+
cache[url] = filepath;
37+
}
4338
} catch (err) {
4439
//
4540
}
4641
}
4742

48-
// redirect unmet requests to directoryIndexFile
43+
// redirect unmet requests to indexPath
4944
callback({ path: cache[url] || indexPath });
5045
};
5146
};

tests/unit/handler-test.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ const subject = require('../../lib/handler');
88
describe('handler', () => {
99
let sandbox;
1010
let mockFiles;
11+
let mockDirs;
1112

1213
before(() => {
1314
sandbox = sinon.sandbox.create();
1415
// Stub accessSync to act like only files in mockFiles exist
1516
sandbox.stub(fs, 'accessSync', path => {
16-
if (mockFiles.indexOf(path) === -1) {
17+
if (mockFiles.indexOf(path) === -1 && mockDirs.indexOf(path) === -1) {
1718
throw new Error('Doesn\'t exist');
1819
}
1920
});
21+
sandbox.stub(fs, 'statSync', path => ({
22+
isFile() {
23+
return mockFiles.indexOf(path) !== -1;
24+
},
25+
}));
2026
});
2127

2228
after(() => {
@@ -25,6 +31,7 @@ describe('handler', () => {
2531

2632
beforeEach(() => {
2733
mockFiles = [];
34+
mockDirs = [];
2835
});
2936

3037
// Create a handler with the specified options
@@ -33,8 +40,7 @@ describe('handler', () => {
3340
cwd: '.',
3441
name: 'serve',
3542
endpoint: 'dist',
36-
directoryIndexFile: 'index.html',
37-
indexPath: undefined,
43+
indexPath: join('.', 'index.html'),
3844
}, options));
3945
}
4046

@@ -57,13 +63,15 @@ describe('handler', () => {
5763
}
5864

5965
it('works', () => {
66+
mockDirs.push('.');
6067
mockFiles.push('script.js');
6168
return handlerExec('serve://dist/script.js').then(path => {
6269
assert.equal(path, 'script.js');
6370
});
6471
});
6572

6673
it('works with multiple requests', () => {
74+
mockDirs.push('.');
6775
mockFiles.push('script1.js');
6876
mockFiles.push('script2.js');
6977

@@ -77,25 +85,19 @@ describe('handler', () => {
7785
});
7886
});
7987

80-
it('serves directoryIndexFile from the root', () => {
88+
it('serves indexPath from the root', () => {
89+
mockDirs.push('.');
8190
mockFiles.push('foo.html');
8291
return handlerExec('serve://dist', {
83-
directoryIndexFile: 'foo.html',
84-
}).then(path => {
85-
assert.equal(path, 'foo.html');
86-
});
87-
});
88-
89-
it('serves directoryIndexFile for missing files', () => {
90-
mockFiles.push('foo.html');
91-
return handlerExec('serve://dist/missing.js', {
92-
directoryIndexFile: 'foo.html',
92+
cwd: '.',
93+
indexPath: join('.', 'foo.html'),
9394
}).then(path => {
94-
assert.equal(path, 'foo.html');
95+
assert.equal(path, join('.', 'foo.html'));
9596
});
9697
});
9798

9899
it('respects relative cwd', () => {
100+
mockDirs.push(join('foo', 'bar'));
99101
mockFiles.push(join('foo', 'bar', 'script.js'));
100102
return handlerExec('serve://dist/script.js', {
101103
cwd: join('foo', 'bar'),
@@ -105,6 +107,7 @@ describe('handler', () => {
105107
});
106108

107109
it('respects absolute cwd', () => {
110+
mockDirs.push(resolve('foo', 'bar'));
108111
mockFiles.push(resolve('foo', 'bar', 'script.js'));
109112
return handlerExec('serve://dist/script.js', {
110113
cwd: resolve('foo', 'bar'),
@@ -114,6 +117,7 @@ describe('handler', () => {
114117
});
115118

116119
it('respects endpoint', () => {
120+
mockDirs.push('.');
117121
mockFiles.push('script.js');
118122
return handlerExec('serve://custom/script.js', {
119123
endpoint: 'custom',
@@ -123,22 +127,26 @@ describe('handler', () => {
123127
});
124128

125129
it('respects indexPath for missing files', () => {
130+
mockDirs.push('bar');
126131
mockFiles.push(join('foo', 'bar.html'));
127132
return handlerExec('serve://dist/missing.js', {
133+
cwd: 'bar',
128134
indexPath: join('foo', 'bar.html'),
129135
}).then(path => {
130136
assert.equal(path, join('foo', 'bar.html'));
131137
});
132138
});
133139

134140
it('ignores hashes', () => {
141+
mockDirs.push('.');
135142
mockFiles.push('script.js');
136143
return handlerExec('serve://dist/script.js#hash').then(path => {
137144
assert.equal(path, 'script.js');
138145
});
139146
});
140147

141148
it('ignores query params', () => {
149+
mockDirs.push('.');
142150
mockFiles.push('script.js');
143151
return handlerExec('serve://dist/script.js?query=param').then(path => {
144152
assert.equal(path, 'script.js');

tests/unit/index-test.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { assert } = require('chai');
22
const mock = require('mock-require');
33
const { join } = require('path');
44

5-
describe('handler', () => {
5+
describe('index', () => {
66
let app;
77
let protocol;
88
let onAppReady;
@@ -51,8 +51,23 @@ describe('handler', () => {
5151
cwd: '.',
5252
name: 'serve',
5353
endpoint: 'dist',
54-
directoryIndexFile: 'index.html',
55-
indexPath: undefined,
54+
indexPath: join('.', 'index.html'),
55+
});
56+
});
57+
58+
it('works with a custom cwd', () => {
59+
register({
60+
cwd: join('foo', 'bar'),
61+
});
62+
assert.ok(onAppReady);
63+
onAppReady();
64+
65+
assert.equal(protocolName, 'serve');
66+
assert.deepEqual(handlerOptions, {
67+
cwd: join('foo', 'bar'),
68+
name: 'serve',
69+
endpoint: 'dist',
70+
indexPath: join('foo', 'bar', 'index.html'),
5671
});
5772
});
5873

@@ -61,8 +76,7 @@ describe('handler', () => {
6176
cwd: join('my', 'old'),
6277
name: 'friend',
6378
endpoint: 'so',
64-
directoryIndexFile: 'we.html',
65-
indexPath: join('meet', 'again'),
79+
indexPath: join('we', 'meet', 'again.html'),
6680
});
6781
assert.ok(onAppReady);
6882
onAppReady();
@@ -72,8 +86,7 @@ describe('handler', () => {
7286
cwd: join('my', 'old'),
7387
name: 'friend',
7488
endpoint: 'so',
75-
directoryIndexFile: 'we.html',
76-
indexPath: join('meet', 'again'),
89+
indexPath: join('we', 'meet', 'again.html'),
7790
});
7891
});
7992

0 commit comments

Comments
 (0)