-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
46 lines (35 loc) · 971 Bytes
/
index.js
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
'use strict';
const busboy = require('busboy');
const RE_MIME = /^(?:multipart\/.+)|(?:application\/x-www-form-urlencoded)$/i;
module.exports = (options) => {
options = options || {};
return (req, res, next) => {
if (req.busboy
|| req.method === 'GET'
|| req.method === 'HEAD'
|| !hasBody(req)
|| !RE_MIME.test(mime(req))) {
return next();
}
const cfg = { ...options, headers: req.headers };
req.busboy = busboy(cfg);
if (options.immediate) {
process.nextTick(() => {
req.pipe(req.busboy);
});
}
next();
};
};
// Utility functions copied from Connect
function hasBody(req) {
const encoding = ('transfer-encoding' in req.headers);
const length = (
'content-length' in req.headers && req.headers['content-length'] !== '0'
);
return (encoding || length);
}
function mime(req) {
const str = (req.headers['content-type'] || '');
return str.split(';')[0];
}