-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
116 lines (101 loc) · 4.3 KB
/
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
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
var mergeParams = require('merge-params');
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json({
limit: '10000kb'
});
module['exports'] = function parseRequestBody (req, res, next) {
var fields = {};
var acceptTypes = [];
// auto-assigns req.jsonResponse boolean for use later ( it's useful to know )
if (req.headers && req.headers.accept) {
acceptTypes = req.headers.accept.split(',');
}
if (acceptTypes.indexOf('text/html') === -1) {
req.jsonResponse = true;
}
if (req.method === "POST" || req.method === "PUT") {
// only attempt to parse body if its multipart form or urlencoded form, if not
// do not parse as we don't want to interrupt req
var contentTypes = [];
if (req.headers && req.headers['content-type']) {
contentTypes = req.headers['content-type'].split(';');
}
//
// If a content-type of multipart/form-data or application/x-www-form-urlencoded is detected,
// use busboy to parse the incoming form.
//
// For multipart file uploads:
// Each file is added to the request.resource.params as a pipe,
// this file upload pipe can later be processed by the hook
//
if (contentTypes.indexOf("multipart/form-data") !== -1 ||
contentTypes.indexOf("application/x-www-form-urlencoded") !== -1) {
var Busboy = require('busboy');
var inspect = require('util').inspect;
// create two busboy instances
// one for parsing multipart form files, another for parsing urlencoded form fields
var busboyFiles = new Busboy({ headers: req.headers });
var busboyFields = new Busboy({
headers: req.headers
});
// console.log('parsing')
// a multipart file upload was detected, add this upload pipe to the resource params
busboyFiles.on('file', function(fieldname, file, filename, encoding, mimetype) {
// console.log('found file', fieldname);
fields[fieldname] = file;
});
// a urlencoded form field was detected, add it's value to resource params
busboyFields.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
// console.log('found field', fieldname, val);
fields[fieldname] = val;
});
// when all fields have been parsed, merge the resource params and continue
busboyFields.on('finish', function() {
mergeParams(req, res, function(){
for (var p in fields) {
req.resource.params[p] = fields[p];
}
next(req, res, req.resource.params);
});
});
// when all multipart files have been uploaded, do nothing
// these upload file pipes should be handled by the user-defined Hook
busboyFiles.on('finish', function() {
// do nothing
});
// pipe the incoming request to busboy for processing
req.pipe(busboyFiles);
req.pipe(busboyFields);
// removed? piping should end the response?
// do we have any uncaught stream errors here?
//return next(req, res);
} else if (req.jsonResponse) {
jsonParser(req, res, function jsonParserCallback (err, fields) {
// could have error here due to invalid json
// current behavior is to ignore bad json and continue request
if (err) {
// console.log('Error in parsing JSON:', err);
// for most json validation errors, we want to pass the error back to the user
// for `request.size.invalid` errors, we should ignore the error and continue
// this is due to a strange bug with the request body not being preserved and body-parser module failing
// root cause is unknown, but it should be able to be fixed by somehow preserving the body
// see: https://github.com/stream-utils/raw-body/issues/55
if (err.type !== 'request.size.invalid') {
return res.json(err)
}
}
mergeParams(req, res, function mergeParamsCallback () {
next(req, res);
});
});
} else {
// Incoming request was a POST, but did not contain content types of multipart or urlencoded form
// This means we should treat the incoming request is a streaming request
next(req, res, req.body);
}
} else {
mergeParams(req, res, function mergeParamsCallback () {
next(req, res, req.body);
});
}
}