Skip to content

Commit 919ff77

Browse files
hiroppyevilebottnawi
authored andcommitted
fix(Server): validate express.static.mime.types (#1765)
1 parent 1b3cd4f commit 919ff77

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

lib/Server.js

+6-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 =
@@ -173,8 +173,11 @@ class Server {
173173
const app = (this.app = new express());
174174

175175
// ref: https://github.com/webpack/webpack-dev-server/issues/1575
176+
// ref: https://github.com/webpack/webpack-dev-server/issues/1724
176177
// remove this when send@^0.16.3
177-
express.static.mime.types.wasm = 'application/wasm';
178+
if (express.static && express.static.mime && express.static.mime.types) {
179+
express.static.mime.types.wasm = 'application/wasm';
180+
}
178181

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

test/Server.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,55 @@ 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 success even if mine.types doesn't exist", () => {
30+
// eslint-disable-next-line
31+
const Server = require('../lib/Server');
32+
33+
jest.mock('express', () => {
34+
const data = jest.requireActual('express');
35+
const { static: st } = data;
36+
const { mime } = st;
37+
38+
delete mime.types;
39+
40+
expect(typeof mime.types).toEqual('undefined');
41+
42+
return Object.assign(data, {
43+
static: Object.assign(st, {
44+
mime,
45+
}),
46+
});
47+
});
48+
49+
return new Promise((res) => {
50+
const compiler = webpack(config);
51+
const server = new Server(compiler);
52+
53+
compiler.hooks.done.tap('webpack-dev-server', (s) => {
54+
const output = server.getStats(s);
55+
expect(output.errors.length).toEqual(0);
56+
57+
server.close(() => {
58+
res();
59+
});
60+
});
61+
62+
compiler.run(() => {});
63+
server.listen(8080, 'localhost');
64+
});
65+
});
66+
});
67+
1968
it('should cascade warningsFilter', () => {
2069
const stats = { warningsFilter: 'test' };
2170
return new Promise((res) => {

0 commit comments

Comments
 (0)