@middy/http-multipart-body-parser 5.0.0-alpha.1 → 5.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +93 -103
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,107 +1,97 @@
|
|
|
1
|
-
import BusBoy from 'busboy'
|
|
2
|
-
import { createError } from '@middy/util'
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const fieldnamePattern = /(.+)\[(.*)]$/
|
|
6
|
-
|
|
1
|
+
import BusBoy from 'busboy';
|
|
2
|
+
import { createError } from '@middy/util';
|
|
3
|
+
const mimePattern = /^multipart\/form-data(;.*)?$/;
|
|
4
|
+
const fieldnamePattern = /(.+)\[(.*)]$/;
|
|
7
5
|
const defaults = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return parseMultipartData(request.event, options)
|
|
32
|
-
.then((multipartData) => {
|
|
33
|
-
// request.event.rawBody = body
|
|
34
|
-
request.event.body = multipartData
|
|
35
|
-
})
|
|
36
|
-
.catch((err) => {
|
|
37
|
-
// UnprocessableEntity
|
|
38
|
-
throw createError(
|
|
39
|
-
415,
|
|
40
|
-
'Invalid or malformed multipart/form-data was provided',
|
|
41
|
-
{ cause: { package: '@middy/multipart-body-parser', data: err } }
|
|
42
|
-
)
|
|
43
|
-
})
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
before: httpMultipartBodyParserMiddlewareBefore
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const parseMultipartData = (event, options) => {
|
|
52
|
-
const multipartData = {}
|
|
53
|
-
const charset = event.isBase64Encoded ? 'base64' : options.charset
|
|
54
|
-
// header must be lowercase (content-type)
|
|
55
|
-
const busboy = BusBoy({
|
|
56
|
-
...options.busboy,
|
|
57
|
-
headers: {
|
|
58
|
-
'content-type':
|
|
59
|
-
event.headers['Content-Type'] ?? event.headers['content-type']
|
|
60
|
-
}
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
return new Promise((resolve, reject) => {
|
|
64
|
-
busboy
|
|
65
|
-
.on('file', (fieldname, file, info) => {
|
|
66
|
-
const { filename, encoding, mimeType: mimetype } = info
|
|
67
|
-
const attachment = {
|
|
68
|
-
filename,
|
|
69
|
-
mimetype,
|
|
70
|
-
encoding
|
|
6
|
+
// busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
|
|
7
|
+
busboy: {},
|
|
8
|
+
charset: 'utf8',
|
|
9
|
+
disableContentTypeError: false
|
|
10
|
+
};
|
|
11
|
+
const httpMultipartBodyParserMiddleware = (opts = {})=>{
|
|
12
|
+
const options = {
|
|
13
|
+
...defaults,
|
|
14
|
+
...opts
|
|
15
|
+
};
|
|
16
|
+
const httpMultipartBodyParserMiddlewareBefore = async (request)=>{
|
|
17
|
+
const { headers } = request.event;
|
|
18
|
+
const contentType = headers?.['Content-Type'] ?? headers?.['content-type'];
|
|
19
|
+
if (!mimePattern.test(contentType)) {
|
|
20
|
+
if (options.disableContentTypeError) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
throw createError(415, 'Unsupported Media Type', {
|
|
24
|
+
cause: {
|
|
25
|
+
package: '@middy/multipart-body-parser',
|
|
26
|
+
data: contentType
|
|
27
|
+
}
|
|
28
|
+
});
|
|
71
29
|
}
|
|
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
|
-
multipartData[matches[1]].push(value)
|
|
30
|
+
return parseMultipartData(request.event, options).then((multipartData)=>{
|
|
31
|
+
// request.event.rawBody = body
|
|
32
|
+
request.event.body = multipartData;
|
|
33
|
+
}).catch((err)=>{
|
|
34
|
+
// UnprocessableEntity
|
|
35
|
+
throw createError(415, 'Invalid or malformed multipart/form-data was provided', {
|
|
36
|
+
cause: {
|
|
37
|
+
package: '@middy/multipart-body-parser',
|
|
38
|
+
data: err
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
before: httpMultipartBodyParserMiddlewareBefore
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const parseMultipartData = (event, options)=>{
|
|
48
|
+
const multipartData = {};
|
|
49
|
+
const charset = event.isBase64Encoded ? 'base64' : options.charset;
|
|
50
|
+
// header must be lowercase (content-type)
|
|
51
|
+
const busboy = BusBoy({
|
|
52
|
+
...options.busboy,
|
|
53
|
+
headers: {
|
|
54
|
+
'content-type': event.headers?.['Content-Type'] ?? event.headers?.['content-type']
|
|
98
55
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
56
|
+
});
|
|
57
|
+
return new Promise((resolve, reject)=>{
|
|
58
|
+
busboy.on('file', (fieldname, file, info)=>{
|
|
59
|
+
const { filename, encoding, mimeType: mimetype } = info;
|
|
60
|
+
const attachment = {
|
|
61
|
+
filename,
|
|
62
|
+
mimetype,
|
|
63
|
+
encoding
|
|
64
|
+
};
|
|
65
|
+
const chunks = [];
|
|
66
|
+
file.on('data', (data)=>{
|
|
67
|
+
chunks.push(data);
|
|
68
|
+
});
|
|
69
|
+
file.on('end', ()=>{
|
|
70
|
+
attachment.truncated = file.truncated;
|
|
71
|
+
attachment.content = Buffer.concat(chunks);
|
|
72
|
+
if (!multipartData[fieldname]) {
|
|
73
|
+
multipartData[fieldname] = attachment;
|
|
74
|
+
} else {
|
|
75
|
+
const current = multipartData[fieldname];
|
|
76
|
+
multipartData[fieldname] = [
|
|
77
|
+
attachment
|
|
78
|
+
].concat(current);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}).on('field', (fieldname, value)=>{
|
|
82
|
+
const matches = fieldname.match(fieldnamePattern);
|
|
83
|
+
if (!matches) {
|
|
84
|
+
multipartData[fieldname] = value;
|
|
85
|
+
} else {
|
|
86
|
+
if (!multipartData[matches[1]]) {
|
|
87
|
+
multipartData[matches[1]] = [];
|
|
88
|
+
}
|
|
89
|
+
multipartData[matches[1]].push(value);
|
|
90
|
+
}
|
|
91
|
+
}).on('close', ()=>resolve(multipartData)).on('error', (e)=>reject(e));
|
|
92
|
+
busboy.write(event.body, charset);
|
|
93
|
+
busboy.end();
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
export default httpMultipartBodyParserMiddleware;
|
|
102
97
|
|
|
103
|
-
busboy.write(event.body, charset)
|
|
104
|
-
busboy.end()
|
|
105
|
-
})
|
|
106
|
-
}
|
|
107
|
-
export default httpMultipartBodyParserMiddleware
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-multipart-body-parser",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-rc.0",
|
|
4
4
|
"description": "Http event normalizer middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -60,13 +60,13 @@
|
|
|
60
60
|
"url": "https://github.com/sponsors/willfarrell"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@middy/util": "5.0.0-
|
|
63
|
+
"@middy/util": "5.0.0-rc.0",
|
|
64
64
|
"busboy": "1.6.0"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@middy/core": "5.0.0-
|
|
67
|
+
"@middy/core": "5.0.0-rc.0",
|
|
68
68
|
"@types/aws-lambda": "^8.10.101",
|
|
69
69
|
"type-fest": "^4.0.0"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "403c54ba9f05e038d1ee7541f9e4a7a6d46e9916"
|
|
72
72
|
}
|