From c3fc4a7c2d0f72fdf89a64a6f4579257097ec09b Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Mon, 1 Apr 2019 18:33:36 +0200 Subject: [PATCH 1/9] add option serveIndex --- examples/api/simple/folder/simple.txt | 1 + examples/api/simple/webpack.config.js | 3 +++ lib/Server.js | 22 +++++++++++++--------- lib/options.json | 3 +++ 4 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 examples/api/simple/folder/simple.txt diff --git a/examples/api/simple/folder/simple.txt b/examples/api/simple/folder/simple.txt new file mode 100644 index 0000000000..f2403aead5 --- /dev/null +++ b/examples/api/simple/folder/simple.txt @@ -0,0 +1 @@ +simple text \ No newline at end of file diff --git a/examples/api/simple/webpack.config.js b/examples/api/simple/webpack.config.js index 5d09b56af5..47b35d81a7 100644 --- a/examples/api/simple/webpack.config.js +++ b/examples/api/simple/webpack.config.js @@ -10,4 +10,7 @@ module.exports = setup({ output: { filename: 'bundle.js', }, + devServer: { + serveIndex: false, + }, }); diff --git a/lib/Server.js b/lib/Server.js index b69663c4d1..e3d0f9e91f 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -103,6 +103,8 @@ class Server { this.clientOverlay = options.overlay; this.clientLogLevel = options.clientLogLevel; + this.serveIndex = options.serveIndex; + this.publicHost = options.public; this.allowedHosts = options.allowedHosts; this.disableHostCheck = !!options.disableHostCheck; @@ -470,15 +472,17 @@ class Server { } }, contentBaseIndex: () => { - if (Array.isArray(contentBase)) { - contentBase.forEach((item) => { - app.get('*', serveIndex(item)); - }); - } else if ( - !/^(https?:)?\/\//.test(contentBase) && - typeof contentBase !== 'number' - ) { - app.get('*', serveIndex(contentBase)); + if (this.serveIndex) { + if (Array.isArray(contentBase)) { + contentBase.forEach((item) => { + app.get('*', serveIndex(item)); + }); + } else if ( + !/^(https?:)?\/\//.test(contentBase) && + typeof contentBase !== 'number' + ) { + app.get('*', serveIndex(contentBase)); + } } }, watchContentBase: () => { diff --git a/lib/options.json b/lib/options.json index 7e4e2b3db9..30b45ed6a2 100644 --- a/lib/options.json +++ b/lib/options.json @@ -1,6 +1,9 @@ { "type": "object", "properties": { + "serveIndex": { + "type": "boolean" + }, "hot": { "type": "boolean" }, From 325c5cb5486a3edbb0ff02658187a919bd7fd3ae Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Tue, 2 Apr 2019 14:06:42 +0200 Subject: [PATCH 2/9] setting the default option serveIndex value to be true --- examples/api/simple/webpack.config.js | 3 --- lib/Server.js | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/api/simple/webpack.config.js b/examples/api/simple/webpack.config.js index 47b35d81a7..5d09b56af5 100644 --- a/examples/api/simple/webpack.config.js +++ b/examples/api/simple/webpack.config.js @@ -10,7 +10,4 @@ module.exports = setup({ output: { filename: 'bundle.js', }, - devServer: { - serveIndex: false, - }, }); diff --git a/lib/Server.js b/lib/Server.js index e3d0f9e91f..76ebb3ed4f 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -472,7 +472,7 @@ class Server { } }, contentBaseIndex: () => { - if (this.serveIndex) { + if (this.serveIndex || typeof this.serveIndex === 'undefined') { if (Array.isArray(contentBase)) { contentBase.forEach((item) => { app.get('*', serveIndex(item)); From c3e4aca83f1a5f0dcd69913ad2066cbf34eeabd0 Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Wed, 3 Apr 2019 13:38:21 +0200 Subject: [PATCH 3/9] add/remove contentBaseIndex feature to/from defaultFeatures using serveIndex Option --- examples/api/simple/folder/simple.txt | 1 - lib/Server.js | 25 +++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) delete mode 100644 examples/api/simple/folder/simple.txt diff --git a/examples/api/simple/folder/simple.txt b/examples/api/simple/folder/simple.txt deleted file mode 100644 index f2403aead5..0000000000 --- a/examples/api/simple/folder/simple.txt +++ /dev/null @@ -1 +0,0 @@ -simple text \ No newline at end of file diff --git a/lib/Server.js b/lib/Server.js index 76ebb3ed4f..82a82c99d2 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -472,17 +472,15 @@ class Server { } }, contentBaseIndex: () => { - if (this.serveIndex || typeof this.serveIndex === 'undefined') { - if (Array.isArray(contentBase)) { - contentBase.forEach((item) => { - app.get('*', serveIndex(item)); - }); - } else if ( - !/^(https?:)?\/\//.test(contentBase) && - typeof contentBase !== 'number' - ) { - app.get('*', serveIndex(contentBase)); - } + if (Array.isArray(contentBase)) { + contentBase.forEach((item) => { + app.get('*', serveIndex(item)); + }); + } else if ( + !/^(https?:)?\/\//.test(contentBase) && + typeof contentBase !== 'number' + ) { + app.get('*', serveIndex(contentBase)); } }, watchContentBase: () => { @@ -555,7 +553,10 @@ class Server { defaultFeatures.push('magicHtml'); - if (contentBase !== false) { + if ( + contentBase !== false && + (options.serveIndex || typeof options.serveIndex === 'undefined') + ) { defaultFeatures.push('contentBaseIndex'); } // compress is placed last and uses unshift so that it will be the first middleware used From e4261440bb2ba65cb5bfd60f8a3bc7ea7ae681dd Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Wed, 3 Apr 2019 14:57:17 +0200 Subject: [PATCH 4/9] remove typeof and compare the option value directly with undefined --- lib/Server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Server.js b/lib/Server.js index 82a82c99d2..480d0cb911 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -555,7 +555,7 @@ class Server { if ( contentBase !== false && - (options.serveIndex || typeof options.serveIndex === 'undefined') + (options.serveIndex || options.serveIndex === undefined) ) { defaultFeatures.push('contentBaseIndex'); } From bee7cbc4130860592f8c3b99d76460dccda081ff Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Wed, 3 Apr 2019 16:30:36 +0200 Subject: [PATCH 5/9] add the option with the default value to options.js & simplifying the condition --- bin/options.js | 5 +++++ lib/Server.js | 13 +++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/bin/options.js b/bin/options.js index 35db9b3158..eee3fcf430 100644 --- a/bin/options.js +++ b/bin/options.js @@ -21,6 +21,11 @@ const options = { type: 'boolean', describe: 'Lazy', }, + serveIndex: { + type: 'boolean', + describe: 'Enables/Disables serveIndex middleware', + default: true, + }, inline: { type: 'boolean', default: true, diff --git a/lib/Server.js b/lib/Server.js index 480d0cb911..115452abe9 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -103,8 +103,6 @@ class Server { this.clientOverlay = options.overlay; this.clientLogLevel = options.clientLogLevel; - this.serveIndex = options.serveIndex; - this.publicHost = options.public; this.allowedHosts = options.allowedHosts; this.disableHostCheck = !!options.disableHostCheck; @@ -553,12 +551,15 @@ class Server { defaultFeatures.push('magicHtml'); - if ( - contentBase !== false && - (options.serveIndex || options.serveIndex === undefined) - ) { + // checking if it's set to true or not set (Default : undefined => true) + options.serveIndex = options.serveIndex || options.serveIndex === undefined; + + const shouldHandleServeIndex = contentBase && options.serveIndex; + + if (shouldHandleServeIndex) { defaultFeatures.push('contentBaseIndex'); } + // compress is placed last and uses unshift so that it will be the first middleware used if (options.compress) { defaultFeatures.unshift('compress'); From 88a9252f81e10d33478e7f48ccfa0075c94f4281 Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Wed, 3 Apr 2019 16:42:08 +0200 Subject: [PATCH 6/9] setting options.serveIndex to a seperate variable --- lib/Server.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 115452abe9..95ca22987c 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -100,6 +100,8 @@ class Server { this.headers = options.headers; this.progress = options.progress; + this.serveIndex = options.serveIndex; + this.clientOverlay = options.overlay; this.clientLogLevel = options.clientLogLevel; @@ -552,9 +554,9 @@ class Server { defaultFeatures.push('magicHtml'); // checking if it's set to true or not set (Default : undefined => true) - options.serveIndex = options.serveIndex || options.serveIndex === undefined; + this.serveIndex = this.serveIndex || this.serveIndex === undefined; - const shouldHandleServeIndex = contentBase && options.serveIndex; + const shouldHandleServeIndex = contentBase && this.serveIndex; if (shouldHandleServeIndex) { defaultFeatures.push('contentBaseIndex'); From 43337af712cc46a59c2af8b44ba9e41ac836c343 Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Wed, 3 Apr 2019 18:29:22 +0200 Subject: [PATCH 7/9] add Tests --- test/ContentBase.test.js | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/ContentBase.test.js b/test/ContentBase.test.js index 06cf5b789b..fea9154e6e 100644 --- a/test/ContentBase.test.js +++ b/test/ContentBase.test.js @@ -64,7 +64,77 @@ describe('ContentBase', () => { }, 1000); }); }); + describe('test listing files in folders without index.html using the option serveIndex:false', () => { + beforeAll((done) => { + server = helper.start( + config, + { + contentBase: contentBasePublic, + watchContentBase: true, + serveIndex: false, + }, + done + ); + req = request(server.app); + }); + + afterAll((done) => { + helper.close(() => { + done(); + }); + }); + it("shouldn't list the files inside the assets folder (404)", (done) => { + req.get('/assets/').expect(404, done); + }); + }); + describe('test listing files in folders without index.html using the option serveIndex:true', () => { + beforeAll((done) => { + server = helper.start( + config, + { + contentBase: contentBasePublic, + watchContentBase: true, + serveIndex: true, + }, + done + ); + req = request(server.app); + }); + + afterAll((done) => { + helper.close(() => { + done(); + }); + }); + + it('should list the files inside the assets folder (200)', (done) => { + req.get('/assets/').expect(200, done); + }); + }); + describe('test listing files in folders without index.html using the option serveIndex default (true)', () => { + beforeAll((done) => { + server = helper.start( + config, + { + contentBase: contentBasePublic, + watchContentBase: true, + }, + done + ); + req = request(server.app); + }); + + afterAll((done) => { + helper.close(() => { + done(); + }); + }); + + it('should list the files inside the assets folder (200)', (done) => { + req.get('/assets/').expect(200, done); + }); + }); describe('to directories', () => { beforeAll((done) => { server = helper.start( From f367fda835da7b5197d057b249f86195ddb80fa6 Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Wed, 3 Apr 2019 18:41:56 +0200 Subject: [PATCH 8/9] add Test case where you have a folder with index.html inside of it --- test/ContentBase.test.js | 4 ++++ test/fixtures/contentbase-config/public/bar/index.html | 1 + 2 files changed, 5 insertions(+) create mode 100644 test/fixtures/contentbase-config/public/bar/index.html diff --git a/test/ContentBase.test.js b/test/ContentBase.test.js index fea9154e6e..bafb4fff88 100644 --- a/test/ContentBase.test.js +++ b/test/ContentBase.test.js @@ -87,6 +87,10 @@ describe('ContentBase', () => { it("shouldn't list the files inside the assets folder (404)", (done) => { req.get('/assets/').expect(404, done); }); + + it('should show Heyo. because bar has index.html inside it (200)', (done) => { + req.get('/bar/').expect(200, /Heyo/, done); + }); }); describe('test listing files in folders without index.html using the option serveIndex:true', () => { beforeAll((done) => { diff --git a/test/fixtures/contentbase-config/public/bar/index.html b/test/fixtures/contentbase-config/public/bar/index.html new file mode 100644 index 0000000000..17654db5a1 --- /dev/null +++ b/test/fixtures/contentbase-config/public/bar/index.html @@ -0,0 +1 @@ +Heyo From 4c33da3269caa0590306103a42a40f27b94c62a3 Mon Sep 17 00:00:00 2001 From: Eslam El-Hakmey Date: Fri, 5 Apr 2019 12:06:33 +0200 Subject: [PATCH 9/9] add (folder with index.html inside it) test case to serveIndex:true & default --- test/ContentBase.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/ContentBase.test.js b/test/ContentBase.test.js index bafb4fff88..0854fbd3d4 100644 --- a/test/ContentBase.test.js +++ b/test/ContentBase.test.js @@ -115,6 +115,10 @@ describe('ContentBase', () => { it('should list the files inside the assets folder (200)', (done) => { req.get('/assets/').expect(200, done); }); + + it('should show Heyo. because bar has index.html inside it (200)', (done) => { + req.get('/bar/').expect(200, /Heyo/, done); + }); }); describe('test listing files in folders without index.html using the option serveIndex default (true)', () => { beforeAll((done) => { @@ -138,6 +142,10 @@ describe('ContentBase', () => { it('should list the files inside the assets folder (200)', (done) => { req.get('/assets/').expect(200, done); }); + + it('should show Heyo. because bar has index.html inside it (200)', (done) => { + req.get('/bar/').expect(200, /Heyo/, done); + }); }); describe('to directories', () => { beforeAll((done) => {