@middy/http-multipart-body-parser 4.0.0-alpha.3 → 4.0.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/README.md +1 -0
- package/index.cjs +6 -4
- package/index.d.ts +1 -0
- package/index.js +6 -4
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -59,6 +59,7 @@ npm install --save @middy/http-multipart-body-parser
|
|
|
59
59
|
## Options
|
|
60
60
|
|
|
61
61
|
- `busboy` (object) (default `{}`): it can be used to pass extraparameters to the internal `busboy` instance at creation time. Checkout [the official documentation](https://www.npmjs.com/package/busboy#busboy-methods) for more information on the supported options.
|
|
62
|
+
- `charset` (string) (default `utf-8`): it can be used to change default charset.
|
|
62
63
|
|
|
63
64
|
**Note**: this middleware will buffer all the data as it is processed internally by `busboy`, so, if you are using this approach to parse significantly big volumes of data, keep in mind that all the data will be allocated in memory. This is somewhat inevitable with Lambdas (as the data is already encoded into the JSON in memory as Base64), but it's good to keep this in mind and evaluate the impact on you application.
|
|
64
65
|
If you really have to deal with big files, then you might also want to consider to allowing your users to [directly upload files to S3](https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-UsingHTTPPOST.html)
|
package/index.cjs
CHANGED
|
@@ -16,7 +16,8 @@ function _interopRequireDefault(obj) {
|
|
|
16
16
|
const mimePattern = /^multipart\/form-data(;.*)?$/;
|
|
17
17
|
const fieldnamePattern = /(.+)\[(.*)]$/;
|
|
18
18
|
const defaults = {
|
|
19
|
-
busboy: {}
|
|
19
|
+
busboy: {},
|
|
20
|
+
charset: 'utf8'
|
|
20
21
|
};
|
|
21
22
|
const httpMultipartBodyParserMiddleware = (opts = {})=>{
|
|
22
23
|
const options = {
|
|
@@ -27,7 +28,7 @@ const httpMultipartBodyParserMiddleware = (opts = {})=>{
|
|
|
27
28
|
const { headers } = request.event;
|
|
28
29
|
const contentType = headers['Content-Type'] ?? headers['content-type'];
|
|
29
30
|
if (!mimePattern.test(contentType)) return;
|
|
30
|
-
return parseMultipartData(request.event, options
|
|
31
|
+
return parseMultipartData(request.event, options).then((multipartData)=>{
|
|
31
32
|
request.event.body = multipartData;
|
|
32
33
|
}).catch((cause)=>{
|
|
33
34
|
throw (0, _util.createError)(422, 'Invalid or malformed multipart/form-data was provided', {
|
|
@@ -41,8 +42,9 @@ const httpMultipartBodyParserMiddleware = (opts = {})=>{
|
|
|
41
42
|
};
|
|
42
43
|
const parseMultipartData = (event, options)=>{
|
|
43
44
|
const multipartData = {};
|
|
45
|
+
const charset = event.isBase64Encoded ? 'base64' : options.charset;
|
|
44
46
|
const busboy = (0, _busboy.default)({
|
|
45
|
-
...options,
|
|
47
|
+
...options.busboy,
|
|
46
48
|
headers: {
|
|
47
49
|
'content-type': event.headers['Content-Type'] ?? event.headers['content-type']
|
|
48
50
|
}
|
|
@@ -82,7 +84,7 @@ const parseMultipartData = (event, options)=>{
|
|
|
82
84
|
multipartData[matches[1]].push(value);
|
|
83
85
|
}
|
|
84
86
|
}).on('close', ()=>resolve(multipartData)).on('error', (e)=>reject(e));
|
|
85
|
-
busboy.write(event.body,
|
|
87
|
+
busboy.write(event.body, charset);
|
|
86
88
|
busboy.end();
|
|
87
89
|
});
|
|
88
90
|
};
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -3,7 +3,8 @@ import { createError } from '@middy/util';
|
|
|
3
3
|
const mimePattern = /^multipart\/form-data(;.*)?$/;
|
|
4
4
|
const fieldnamePattern = /(.+)\[(.*)]$/;
|
|
5
5
|
const defaults = {
|
|
6
|
-
busboy: {}
|
|
6
|
+
busboy: {},
|
|
7
|
+
charset: 'utf8'
|
|
7
8
|
};
|
|
8
9
|
const httpMultipartBodyParserMiddleware = (opts = {})=>{
|
|
9
10
|
const options = {
|
|
@@ -14,7 +15,7 @@ const httpMultipartBodyParserMiddleware = (opts = {})=>{
|
|
|
14
15
|
const { headers } = request.event;
|
|
15
16
|
const contentType = headers['Content-Type'] ?? headers['content-type'];
|
|
16
17
|
if (!mimePattern.test(contentType)) return;
|
|
17
|
-
return parseMultipartData(request.event, options
|
|
18
|
+
return parseMultipartData(request.event, options).then((multipartData)=>{
|
|
18
19
|
request.event.body = multipartData;
|
|
19
20
|
}).catch((cause)=>{
|
|
20
21
|
throw createError(422, 'Invalid or malformed multipart/form-data was provided', {
|
|
@@ -28,8 +29,9 @@ const httpMultipartBodyParserMiddleware = (opts = {})=>{
|
|
|
28
29
|
};
|
|
29
30
|
const parseMultipartData = (event, options)=>{
|
|
30
31
|
const multipartData = {};
|
|
32
|
+
const charset = event.isBase64Encoded ? 'base64' : options.charset;
|
|
31
33
|
const busboy = BusBoy({
|
|
32
|
-
...options,
|
|
34
|
+
...options.busboy,
|
|
33
35
|
headers: {
|
|
34
36
|
'content-type': event.headers['Content-Type'] ?? event.headers['content-type']
|
|
35
37
|
}
|
|
@@ -69,7 +71,7 @@ const parseMultipartData = (event, options)=>{
|
|
|
69
71
|
multipartData[matches[1]].push(value);
|
|
70
72
|
}
|
|
71
73
|
}).on('close', ()=>resolve(multipartData)).on('error', (e)=>reject(e));
|
|
72
|
-
busboy.write(event.body,
|
|
74
|
+
busboy.write(event.body, charset);
|
|
73
75
|
busboy.end();
|
|
74
76
|
});
|
|
75
77
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-multipart-body-parser",
|
|
3
|
-
"version": "4.0.0
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Http event normalizer middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -62,13 +62,13 @@
|
|
|
62
62
|
},
|
|
63
63
|
"homepage": "https://middy.js.org",
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@middy/util": "4.0.0
|
|
65
|
+
"@middy/util": "4.0.0",
|
|
66
66
|
"busboy": "1.6.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
|
-
"@middy/core": "4.0.0
|
|
69
|
+
"@middy/core": "4.0.0",
|
|
70
70
|
"@types/aws-lambda": "^8.10.101",
|
|
71
71
|
"type-fest": "^3.0.0"
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "582286144bcd79968a8c7c2f8867a23c80079a47"
|
|
74
74
|
}
|