Skip to content

Commit 7fa2c15

Browse files
authored
fix(middleware): prefer mime type option over built-in (#670)
1 parent b1bf4b9 commit 7fa2c15

File tree

3 files changed

+109
-14
lines changed

3 files changed

+109
-14
lines changed

src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export default function wdm(compiler, options = {}) {
2121
if (mimeTypes) {
2222
const { types } = mime;
2323

24-
mime.types = { ...mimeTypes, ...types };
24+
// mimeTypes from user provided options should take priority
25+
// over existing, known types
26+
mime.types = { ...types, ...mimeTypes };
2527
}
2628

2729
const context = {

src/middleware.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,9 @@ export default function wrapper(context) {
4949
return;
5050
}
5151

52-
try {
53-
content = context.outputFileSystem.readFileSync(filename);
54-
} catch (_ignoreError) {
55-
await goNext();
56-
return;
57-
}
58-
59-
// Buffer
60-
content = handleRangeHeaders(content, req, res);
52+
// Content-Type and headers need to be set before checking if
53+
// the file is in the outputFileSystem, as these things should be
54+
// applied to all files that are being served
6155

6256
if (!res.get('Content-Type')) {
6357
// content-type name(like application/javascript; charset=utf-8) or false
@@ -74,6 +68,16 @@ export default function wrapper(context) {
7468
}
7569
}
7670

71+
try {
72+
content = context.outputFileSystem.readFileSync(filename);
73+
} catch (_ignoreError) {
74+
await goNext();
75+
return;
76+
}
77+
78+
// Buffer
79+
content = handleRangeHeaders(content, req, res);
80+
7781
// send Buffer
7882
res.send(content);
7983
}

test/middleware.test.js

+93-4
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,7 @@ describe('middleware', () => {
19901990

19911991
afterAll(close);
19921992

1993-
it('should return the "200" code for the "GET" request to "file.html"', (done) => {
1993+
it('should return the "200" code for the "GET" request to "file.phtml"', (done) => {
19941994
request(app)
19951995
.get('/file.phtml')
19961996
.expect('Content-Type', 'application/octet-stream')
@@ -2031,13 +2031,91 @@ describe('middleware', () => {
20312031

20322032
afterAll(close);
20332033

2034-
it('should return the "200" code for the "GET" request "file.html"', (done) => {
2034+
it('should return the "200" code for the "GET" request "file.phtml"', (done) => {
20352035
request(app)
20362036
.get('/file.phtml')
20372037
.expect('Content-Type', 'text/html; charset=utf-8')
20382038
.expect(200, 'welcome', done);
20392039
});
20402040
});
2041+
2042+
describe('should override value for "Content-Type" header for known MIME type', () => {
2043+
beforeAll((done) => {
2044+
const outputPath = path.resolve(__dirname, './outputs/basic');
2045+
const compiler = getCompiler({
2046+
...webpackConfig,
2047+
output: {
2048+
filename: 'bundle.js',
2049+
path: outputPath,
2050+
},
2051+
});
2052+
2053+
instance = middleware(compiler, {
2054+
mimeTypes: {
2055+
jpg: 'application/octet-stream',
2056+
},
2057+
});
2058+
2059+
app = express();
2060+
app.use(instance);
2061+
2062+
listen = listenShorthand(done);
2063+
2064+
instance.context.outputFileSystem.mkdirSync(outputPath, {
2065+
recursive: true,
2066+
});
2067+
instance.context.outputFileSystem.writeFileSync(
2068+
path.resolve(outputPath, 'file.jpg'),
2069+
'welcome'
2070+
);
2071+
});
2072+
2073+
afterAll(close);
2074+
2075+
it('should return the "200" code for the "GET" request "file.jpg"', (done) => {
2076+
request(app)
2077+
.get('/file.jpg')
2078+
.expect('Content-Type', /application\/octet-stream/)
2079+
.expect(200, done);
2080+
});
2081+
});
2082+
2083+
describe('should set "Content-Type" header for route not from outputFileSystem', () => {
2084+
beforeAll((done) => {
2085+
const outputPath = path.resolve(__dirname, './outputs/basic');
2086+
const compiler = getCompiler({
2087+
...webpackConfig,
2088+
output: {
2089+
filename: 'bundle.js',
2090+
path: outputPath,
2091+
},
2092+
});
2093+
2094+
instance = middleware(compiler, {
2095+
mimeTypes: {
2096+
jpg: 'application/octet-stream',
2097+
},
2098+
});
2099+
2100+
app = express();
2101+
app.use(instance);
2102+
2103+
app.get('/file.jpg', (req, res) => {
2104+
res.send('welcome');
2105+
});
2106+
2107+
listen = listenShorthand(done);
2108+
});
2109+
2110+
afterAll(close);
2111+
2112+
it('should return the "200" code for the "GET" request "file.jpg"', (done) => {
2113+
request(app)
2114+
.get('/file.jpg')
2115+
.expect('Content-Type', /application\/octet-stream/)
2116+
.expect(200, done);
2117+
});
2118+
});
20412119
});
20422120

20432121
describe('watchOptions option', () => {
@@ -2640,7 +2718,7 @@ describe('middleware', () => {
26402718
});
26412719

26422720
describe('headers option', () => {
2643-
beforeAll((done) => {
2721+
beforeEach((done) => {
26442722
const compiler = getCompiler(webpackConfig);
26452723

26462724
instance = middleware(compiler, {
@@ -2653,7 +2731,7 @@ describe('middleware', () => {
26532731
listen = listenShorthand(done);
26542732
});
26552733

2656-
afterAll(close);
2734+
afterEach(close);
26572735

26582736
it('should return the "200" code for the "GET" request to the bundle file and return headers', (done) => {
26592737
request(app)
@@ -2662,6 +2740,17 @@ describe('middleware', () => {
26622740
.expect('X-nonsense-2', 'no')
26632741
.expect(200, done);
26642742
});
2743+
2744+
it('should return the "200" code for the "GET" request to path not in outputFileSystem and return headers', (done) => {
2745+
app.get('/file.jpg', (req, res) => {
2746+
res.send('welcome');
2747+
});
2748+
request(app)
2749+
.get('/file.jpg')
2750+
.expect('X-nonsense-1', 'yes')
2751+
.expect('X-nonsense-2', 'no')
2752+
.expect(200, done);
2753+
});
26652754
});
26662755

26672756
describe('publicPath option', () => {

0 commit comments

Comments
 (0)