middleware.js
3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
const path = require('path');
const mime = require('mime');
const DevMiddlewareError = require('./DevMiddlewareError');
const {
getFilenameFromUrl,
handleRangeHeaders,
handleRequest,
ready,
} = require('./util');
// Do not add a charset to the Content-Type header of these file types
// otherwise the client will fail to render them correctly.
const NonCharsetFileTypes = /\.(wasm|usdz)$/;
module.exports = function wrapper(context) {
return function middleware(req, res, next) {
// fixes #282. credit @cexoso. in certain edge situations res.locals is
// undefined.
// eslint-disable-next-line no-param-reassign
res.locals = res.locals || {};
function goNext() {
if (!context.options.serverSideRender) {
return next();
}
return new Promise((resolve) => {
ready(
context,
() => {
// eslint-disable-next-line no-param-reassign
res.locals.webpackStats = context.webpackStats;
// eslint-disable-next-line no-param-reassign
res.locals.fs = context.fs;
resolve(next());
},
req
);
});
}
const acceptedMethods = context.options.methods || ['GET', 'HEAD'];
if (acceptedMethods.indexOf(req.method) === -1) {
return goNext();
}
let filename = getFilenameFromUrl(
context.options.publicPath,
context.compiler,
req.url
);
if (filename === false) {
return goNext();
}
return new Promise((resolve) => {
handleRequest(context, filename, processRequest, req);
// eslint-disable-next-line consistent-return
function processRequest() {
try {
let stat = context.fs.statSync(filename);
if (!stat.isFile()) {
if (stat.isDirectory()) {
let { index } = context.options;
// eslint-disable-next-line no-undefined
if (index === undefined || index === true) {
index = 'index.html';
} else if (!index) {
throw new DevMiddlewareError('next');
}
filename = path.posix.join(filename, index);
stat = context.fs.statSync(filename);
if (!stat.isFile()) {
throw new DevMiddlewareError('next');
}
} else {
throw new DevMiddlewareError('next');
}
}
} catch (e) {
return resolve(goNext());
}
// server content
let content = context.fs.readFileSync(filename);
content = handleRangeHeaders(content, req, res);
let contentType = mime.getType(filename) || '';
if (!NonCharsetFileTypes.test(filename)) {
contentType += '; charset=UTF-8';
}
if (!res.getHeader || !res.getHeader('Content-Type')) {
res.setHeader('Content-Type', contentType);
}
res.setHeader('Content-Length', content.length);
const { headers } = context.options;
if (headers) {
for (const name in headers) {
if ({}.hasOwnProperty.call(headers, name)) {
res.setHeader(name, context.options.headers[name]);
}
}
}
// Express automatically sets the statusCode to 200, but not all servers do (Koa).
// eslint-disable-next-line no-param-reassign
res.statusCode = res.statusCode || 200;
if (res.send) {
res.send(content);
} else {
res.end(content);
}
resolve();
}
});
};
};