UNPKG

1.12 kBJavaScriptView Raw
1const ACCEPTABLE_CONTENT_TYPE = /^(multipart\/.+);(.*)$/i;
2const UNACCEPTABLE_METHODS = ['GET', 'HEAD'];
3
4/**
5 * Ensures the request contains a content body
6 * @param {Object} req Express req object
7 * @returns {Boolean}
8 */
9const hasBody = (req) => {
10 return ('transfer-encoding' in req.headers) ||
11 ('content-length' in req.headers && req.headers['content-length'] !== '0');
12};
13
14/**
15 * Ensures the request is not using a non-compliant multipart method
16 * such as GET or HEAD
17 * @param {Object} req Express req object
18 * @returns {Boolean}
19 */
20const hasAcceptableMethod = req => !UNACCEPTABLE_METHODS.includes(req.method);
21
22/**
23 * Ensures that only multipart requests are processed by express-fileupload
24 * @param {Object} req Express req object
25 * @returns {Boolean}
26 */
27const hasAcceptableContentType = req => ACCEPTABLE_CONTENT_TYPE.test(req.headers['content-type']);
28
29/**
30 * Ensures that the request in question is eligible for file uploads
31 * @param {Object} req Express req object
32 * @returns {Boolean}
33 */
34module.exports = req => hasBody(req) && hasAcceptableMethod(req) && hasAcceptableContentType(req);