Skip to content

fix(Server): validate express.static.mime.types #1765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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)) {
Expand Down
49 changes: 49 additions & 0 deletions test/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down