Skip to content

Commit e37a490

Browse files
authored
Merge pull request #934 from salesh/feature/multipart-body-parser-optional-charset
feat: add optional charset option to multipart body parser
2 parents 87cd713 + 8807221 commit e37a490

File tree

5 files changed

+11
-5
lines changed

5 files changed

+11
-5
lines changed

packages/http-multipart-body-parser/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ npm install --save @middy/http-multipart-body-parser
5959
## Options
6060

6161
- `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.
6263

6364
**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.
6465
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)

packages/http-multipart-body-parser/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface Options {
1919
headerPairs?: number
2020
}
2121
}
22+
charset?: string
2223
}
2324

2425
export type Event = Omit<APIGatewayEvent, 'body'> & {

packages/http-multipart-body-parser/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const fieldnamePattern = /(.+)\[(.*)]$/
66

77
const defaults = {
88
// busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
9-
busboy: {}
9+
busboy: {},
10+
charset: 'utf8'
1011
}
1112

1213
const httpMultipartBodyParserMiddleware = (opts = {}) => {
@@ -19,7 +20,7 @@ const httpMultipartBodyParserMiddleware = (opts = {}) => {
1920

2021
if (!mimePattern.test(contentType)) return
2122

22-
return parseMultipartData(request.event, options.busboy)
23+
return parseMultipartData(request.event, options)
2324
.then((multipartData) => {
2425
// request.event.rawBody = body
2526
request.event.body = multipartData
@@ -41,9 +42,10 @@ const httpMultipartBodyParserMiddleware = (opts = {}) => {
4142

4243
const parseMultipartData = (event, options) => {
4344
const multipartData = {}
45+
const charset = event.isBase64Encoded ? 'base64' : options.charset
4446
// header must be lowercase (content-type)
4547
const busboy = BusBoy({
46-
...options,
48+
...options.busboy,
4749
headers: {
4850
'content-type':
4951
event.headers['Content-Type'] ?? event.headers['content-type']
@@ -90,7 +92,7 @@ const parseMultipartData = (event, options) => {
9092
.on('close', () => resolve(multipartData))
9193
.on('error', (e) => reject(e))
9294

93-
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'utf8')
95+
busboy.write(event.body, charset)
9496
busboy.end()
9597
})
9698
}

packages/http-multipart-body-parser/index.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ middleware = multipartBodyParser({
2323
parts: 100,
2424
headerPairs: 100
2525
}
26-
}
26+
},
27+
charset: 'utf8'
2728
})
2829
expectType<middy.MiddlewareObj<Event>>(middleware)

website/docs/middlewares/http-multipart-body-parser.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ npm install --save @middy/http-multipart-body-parser
2424
## Options
2525

2626
- `busboy` (`object`) (optional): defaults to `{}` and 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.
27+
- `charset` (string) (default `utf8`): it can be used to change default charset.
2728

2829
**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.
2930
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)

0 commit comments

Comments
 (0)