Skip to content

Commit 9e1ea37

Browse files
committed
fix(Server): validate express.static.mime.types
fixes: #1724
1 parent d5d60cb commit 9e1ea37

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

lib/Server.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ class Server {
8989
if (options.lazy && !options.filename) {
9090
throw new Error("'filename' option must be set in lazy mode.");
9191
}
92-
92+
9393
// if the user enables http2, we can safely enable https
9494
if (options.http2 && !options.https) {
9595
options.https = true;
9696
}
97-
97+
9898
updateCompiler(compiler, options);
9999

100100
this.stats =
@@ -174,7 +174,14 @@ class Server {
174174

175175
// ref: https://github.com/webpack/webpack-dev-server/issues/1575
176176
// remove this when send@^0.16.3
177-
express.static.mime.types.wasm = 'application/wasm';
177+
if (
178+
express.static &&
179+
express.static.mime &&
180+
express.static.mime.types &&
181+
!express.static.mime.types.wasm
182+
) {
183+
express.static.mime.types.wasm = 'application/wasm';
184+
}
178185

179186
app.all('*', (req, res, next) => {
180187
if (this.checkHost(req.headers)) {

test/Server.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,54 @@ const allStats = [
1616
];
1717

1818
describe('Server', () => {
19+
// issue: https://github.com/webpack/webpack-dev-server/issues/1724
20+
describe('express.static.mine.types', () => {
21+
beforeEach(() => {
22+
jest.resetModules();
23+
});
24+
25+
afterEach(() => {
26+
jest.unmock('express');
27+
});
28+
29+
it("should succeed even if mine.types doesn't exist", () => {
30+
const Server = require('../lib/Server');
31+
32+
jest.mock('express', () => {
33+
const data = jest.requireActual('express');
34+
const { static: st } = data;
35+
const { mime } = st;
36+
37+
delete mime.types;
38+
39+
expect(typeof mime.types).toEqual('undefined');
40+
41+
return Object.assign(data, {
42+
static: Object.assign(st, {
43+
mime,
44+
}),
45+
});
46+
});
47+
48+
return new Promise((res) => {
49+
const compiler = webpack(config);
50+
const server = new Server(compiler);
51+
52+
compiler.hooks.done.tap('webpack-dev-server', (s) => {
53+
const output = server.getStats(s);
54+
expect(output.errors.length).toEqual(0);
55+
56+
server.close(() => {
57+
res();
58+
});
59+
});
60+
61+
compiler.run(() => {});
62+
server.listen(8080, 'localhost');
63+
});
64+
});
65+
});
66+
1967
it('should cascade warningsFilter', () => {
2068
const stats = { warningsFilter: 'test' };
2169
return new Promise((res) => {

0 commit comments

Comments
 (0)