/
getFormDataMethod.js
65 lines (63 loc) · 2.07 KB
/
getFormDataMethod.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
const busboy = require('busboy');
const { resolve } = require('path');
const streams = require("stream");
module.exports = function getFormDataMethod(File, limits) {
return function formData() {
if (this.body == null) {
return null;
}
const contentType = this.headers.get('Content-Type');
const nodeReadable = this.body.on ? this.body : streams.Readable.from(this.body);
const bb = busboy({
headers: {
'content-type': contentType
},
limits,
defParamCharset: 'utf-8'
});
return new Promise((resolve, reject) => {
const formData = new Map();
bb.on('field', (name, value, { nameTruncated, valueTruncated }) => {
if (nameTruncated) {
reject(new Error(`Field name size exceeded: ${limits.fieldNameSize} bytes`));
}
if (valueTruncated) {
reject(new Error(`Field value size exceeded: ${limits.fieldSize} bytes`));
}
formData.set(name, value)
})
bb.on('fieldsLimit', () => {
reject(new Error(`Fields limit exceeded: ${limits.fields}`));
})
bb.on('file', (name, fileStream, { filename, mimeType }) => {
const chunks = [];
fileStream.on('limit', () => {
reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`));
})
fileStream.on('data', (chunk) => {
chunks.push(Buffer.from(chunk));
})
fileStream.on('close', () => {
if (fileStream.truncated) {
reject(new Error(`File size limit exceeded: ${limits.fileSize} bytes`));
}
const file = new File(chunks, filename, { type: mimeType });
formData.set(name, file);
});
})
bb.on('filesLimit', () => {
reject(new Error(`Files limit exceeded: ${limits.files}`));
})
bb.on('partsLimit', () => {
reject(new Error(`Parts limit exceeded: ${limits.parts}`));
})
bb.on('close', () => {
resolve(formData);
});
bb.on('error', err => {
reject(err);
})
nodeReadable.pipe(bb);
})
}
}