@middy/http-multipart-body-parser 4.0.0-alpha.4 → 4.0.1

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 CHANGED
@@ -36,69 +36,6 @@
36
36
  <p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/http-multipart-body-parser">https://middy.js.org/docs/middlewares/http-multipart-body-parser</a></p>
37
37
  </div>
38
38
 
39
-
40
- Automatically parses HTTP requests with content type `multipart/form-data` and converts the body into an
41
- object. Also handles gracefully broken JSON as UnprocessableEntity (422 errors)
42
- if used in combination with `httpErrorHandler`.
43
-
44
- It can also be used in combination with validator so that the content can be validated.
45
-
46
- **Note**: by default this is going to parse only events that contain the header `Content-Type` (or `content-type`) set to `multipart/form-data`. If you want to support different casing for the header name (e.g. `Content-type`) then you should use the [`httpHeaderNormalizer`](#httpheadernormalizer) middleware before this middleware.
47
-
48
-
49
-
50
- ## Install
51
-
52
- To install this middleware you can use NPM:
53
-
54
- ```bash
55
- npm install --save @middy/http-multipart-body-parser
56
- ```
57
-
58
-
59
- ## Options
60
-
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
-
63
- **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
- 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)
65
-
66
- ## Sample usage
67
-
68
- ```javascript
69
- import middy from '@middy/core'
70
- import httpHeaderNormalizer from '@middy/http-header-normalizer'
71
- import httpMultipartBodyParser from '@middy/http-multipart-body-parser'
72
- const handler = middy((event, context) => {
73
- return {}
74
- })
75
- handler
76
- .use(httpHeaderNormalizer())
77
- .use(httpMultipartBodyParser())
78
-
79
- // invokes the handler
80
- const event = {
81
- headers: {
82
- 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryppsQEwf2BVJeCe0M'
83
- },
84
- body: 'LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5cHBzUUV3ZjJCVkplQ2UwTQ0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmb28iDQoNCmJhcg0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5cHBzUUV3ZjJCVkplQ2UwTS0t',
85
- isBase64Encoded: true
86
- }
87
- handler(event, {}, (_, body) => {
88
- t.is(body,{ foo: 'bar' })
89
- })
90
- ```
91
-
92
- ## Middy documentation and examples
93
-
94
- For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).
95
-
96
-
97
- ## Contributing
98
-
99
- Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
100
-
101
-
102
39
  ## License
103
40
 
104
41
  Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
@@ -106,4 +43,3 @@ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino]
106
43
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
107
44
  <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
108
45
  </a>
109
-
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.busboy).then((multipartData)=>{
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, event.isBase64Encoded ? 'base64' : 'utf8');
87
+ busboy.write(event.body, charset);
86
88
  busboy.end();
87
89
  });
88
90
  };
package/index.d.ts CHANGED
@@ -19,6 +19,7 @@ interface Options {
19
19
  headerPairs?: number
20
20
  }
21
21
  }
22
+ charset?: string
22
23
  }
23
24
 
24
25
  export type Event = Omit<APIGatewayEvent, 'body'> & {
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.busboy).then((multipartData)=>{
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, event.isBase64Encoded ? 'base64' : 'utf8');
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-alpha.4",
3
+ "version": "4.0.1",
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-alpha.4",
65
+ "@middy/util": "4.0.1",
66
66
  "busboy": "1.6.0"
67
67
  },
68
68
  "devDependencies": {
69
- "@middy/core": "4.0.0-alpha.4",
69
+ "@middy/core": "4.0.1",
70
70
  "@types/aws-lambda": "^8.10.101",
71
71
  "type-fest": "^3.0.0"
72
72
  },
73
- "gitHead": "cbf2af8e31dab58ed28d26143cd77cca544657ae"
73
+ "gitHead": "c5ece2bfbb0d607dcdea5685bf194a6cc19acc8d"
74
74
  }