-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (70 loc) · 2.85 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
'use strict';
var util = require('util');
var bytes = require('bytes');
var EventEmitter = require('events').EventEmitter;
var multipart = require('./lib/multipart');
var textplain = require('./lib/textplain');
var json = require('./lib/json');
var urlencoded = require('./lib/urlencoded');
// @TODO Consider the destruction of persistent documents
var defaults = {
accept: null, // Regexp or Function, Upload file only, limit accept file type
write: false, // Boolean, Upload file only, whether write file to disk
maxBodySize: '1mb', // String, allow maximum body size
maxFileSize: '3mb', // String, Upload file only, allow maximum file size to be uploaded
minFileSize: null, // String, Upload file only, allow minimum file size to be uploaded
keepFilename: false, // Boolean, Upload file only, whether keep origin file name
generateFilename: null, // Function, Upload file only, generate filename handle function
dest: process.cwd() + '/upload' // String, Upload file only, default upload folder
};
var BodyReceiver = function (options) {
EventEmitter.call(this);
this.options = Object.assign({}, defaults, options || {});
this.options.maxBodySize = bytes(this.options.maxBodySize);
this.options.maxFileSize = bytes(this.options.maxFileSize);
if (this.options.minFileSize) {
this.options.minFileSize = bytes(this.options.minFileSize);
}
};
util.inherits(BodyReceiver, EventEmitter);
BodyReceiver.prototype.startup = function () {
var self = this;
// handle error event
if (!self.listenerCount('error')) {
self.on('error', function (error, ctx) {
console.error('Body Receiver Error:', error);
});
}
return async function bodyReceiver (ctx, next) {
var req = ctx.req;
var method = req.method.toLowerCase();
var headers = req.headers;
var contentType;
var result;
if ((method === 'post' || method === 'put') && 'content-type' in headers && ('transfer-encoding' in headers || 'content-length' in headers)) {
contentType = headers['content-type'];
// multipart
if (~contentType.indexOf('multipart/form-data')) {
result = multipart(self, ctx);
}
// text/plain
else if (~contentType.indexOf('text/plain')) {
result = textplain(self, ctx);
}
// application/json
else if (~contentType.indexOf('application/json')) {
result = json(self, ctx);
}
// application/x-www-form-urlencoded
else {
result = urlencoded(self, ctx);
}
ctx.request.body = await result;
if (!ctx.request.body) {
ctx.request.body = {};
}
}
await next();
};
};
module.exports = BodyReceiver;