From 2d0a57289bca563fff018823c7f19969884c6dae Mon Sep 17 00:00:00 2001 From: Yuta Hiroto Date: Fri, 5 Apr 2019 15:14:59 +0100 Subject: [PATCH] fix(Server): validate express.static.mime.types fixes: #1724 --- lib/Server.js | 9 ++++++--- test/Server.test.js | 49 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 170b1fb7e6..bac0e72d0a 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -89,12 +89,12 @@ class Server { if (options.lazy && !options.filename) { throw new Error("'filename' option must be set in lazy mode."); } - + // if the user enables http2, we can safely enable https if (options.http2 && !options.https) { options.https = true; } - + updateCompiler(compiler, options); this.stats = @@ -173,8 +173,11 @@ class Server { const app = (this.app = new express()); // ref: https://github.com/webpack/webpack-dev-server/issues/1575 + // ref: https://github.com/webpack/webpack-dev-server/issues/1724 // remove this when send@^0.16.3 - express.static.mime.types.wasm = 'application/wasm'; + if (express.static && express.static.mime && express.static.mime.types) { + express.static.mime.types.wasm = 'application/wasm'; + } app.all('*', (req, res, next) => { if (this.checkHost(req.headers)) { diff --git a/test/Server.test.js b/test/Server.test.js index 0e15b487e7..b538ce9eac 100644 --- a/test/Server.test.js +++ b/test/Server.test.js @@ -16,6 +16,55 @@ const allStats = [ ]; describe('Server', () => { + // issue: https://github.com/webpack/webpack-dev-server/issues/1724 + describe('express.static.mine.types', () => { + beforeEach(() => { + jest.resetModules(); + }); + + afterEach(() => { + jest.unmock('express'); + }); + + it("should success even if mine.types doesn't exist", () => { + // eslint-disable-next-line + const Server = require('../lib/Server'); + + jest.mock('express', () => { + const data = jest.requireActual('express'); + const { static: st } = data; + const { mime } = st; + + delete mime.types; + + expect(typeof mime.types).toEqual('undefined'); + + return Object.assign(data, { + static: Object.assign(st, { + mime, + }), + }); + }); + + return new Promise((res) => { + const compiler = webpack(config); + const server = new Server(compiler); + + compiler.hooks.done.tap('webpack-dev-server', (s) => { + const output = server.getStats(s); + expect(output.errors.length).toEqual(0); + + server.close(() => { + res(); + }); + }); + + compiler.run(() => {}); + server.listen(8080, 'localhost'); + }); + }); + }); + it('should cascade warningsFilter', () => { const stats = { warningsFilter: 'test' }; return new Promise((res) => {