index.js
4.0 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
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* koa-body - index.js
* Copyright(c) 2014
* MIT Licensed
*
* @author Daryl Lau (@dlau)
* @author Charlike Mike Reagent (@tunnckoCore)
* @api private
*/
'use strict';
/**
* Module dependencies.
*/
var buddy = require('co-body');
var forms = require('formidable');
/**
* Expose `requestbody()`.
*/
module.exports = requestbody;
/**
*
* @param {Object} options
* @see https://github.com/dlau/koa-body
* @api public
*/
function requestbody(opts) {
opts = opts || {};
opts.onError = 'onError' in opts ? opts.onError : false;
opts.patchNode = 'patchNode' in opts ? opts.patchNode : false;
opts.patchKoa = 'patchKoa' in opts ? opts.patchKoa : true;
opts.multipart = 'multipart' in opts ? opts.multipart : false;
opts.urlencoded = 'urlencoded' in opts ? opts.urlencoded : true;
opts.json = 'json' in opts ? opts.json : true;
opts.text = 'text' in opts ? opts.text : true;
opts.encoding = 'encoding' in opts ? opts.encoding : 'utf-8';
opts.jsonLimit = 'jsonLimit' in opts ? opts.jsonLimit : '1mb';
opts.formLimit = 'formLimit' in opts ? opts.formLimit : '56kb';
opts.queryString = 'queryString' in opts ? opts.queryString : null;
opts.formidable = 'formidable' in opts ? opts.formidable : {};
opts.textLimit = 'textLimit' in opts ? opts.textLimit : '56kb';
opts.strict = 'strict' in opts ? opts.strict : true;
return function (ctx, next) {
var bodyPromise;
// so don't parse the body in strict mode
if (!opts.strict || ["GET", "HEAD", "DELETE"].indexOf(ctx.method.toUpperCase()) === -1) {
try {
if (opts.json && ctx.is('json')) {
bodyPromise = buddy.json(ctx, {
encoding: opts.encoding,
limit: opts.jsonLimit
});
} else if (opts.urlencoded && ctx.is('urlencoded')) {
bodyPromise = buddy.form(ctx, {
encoding: opts.encoding,
limit: opts.formLimit,
queryString: opts.queryString
});
} else if (opts.text && ctx.is('text')) {
bodyPromise = buddy.text(ctx, {
encoding: opts.encoding,
limit: opts.textLimit
});
} else if (opts.multipart && ctx.is('multipart')) {
bodyPromise = formy(ctx, opts.formidable);
}
} catch (parsingError) {
if (typeof opts.onError === 'function') {
opts.onError(parsingError, ctx);
} else {
throw parsingError;
}
}
}
bodyPromise = bodyPromise || Promise.resolve({});
return bodyPromise.catch(function(parsingError) {
if (typeof opts.onError === 'function') {
opts.onError(parsingError, ctx);
} else {
throw parsingError;
}
return next();
})
.then(function(body) {
if (opts.patchNode) {
ctx.req.body = body;
}
if (opts.patchKoa) {
ctx.request.body = body;
}
return next();
})
};
}
/**
* Donable formidable
*
* @param {Stream} ctx
* @param {Object} opts
* @return {Object}
* @api private
*/
function formy(ctx, opts) {
return new Promise(function (resolve, reject) {
var fields = {};
var files = {};
var form = new forms.IncomingForm(opts);
form.on('end', function () {
return resolve({
fields: fields,
files: files
});
}).on('error', function (err) {
return reject(err);
}).on('field', function (field, value) {
if (fields[field]) {
if (Array.isArray(fields[field])) {
fields[field].push(value);
} else {
fields[field] = [fields[field], value];
}
} else {
fields[field] = value;
}
}).on('file', function (field, file) {
if (files[field]) {
if (Array.isArray(files[field])) {
files[field].push(file);
} else {
files[field] = [files[field], file];
}
} else {
files[field] = file;
}
});
if (opts.onFileBegin) {
form.on('fileBegin', opts.onFileBegin);
}
form.parse(ctx.req);
});
}