@middy/http-multipart-body-parser 2.5.6 → 3.0.0-alpha.3

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.
Files changed (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +7 -3
  3. package/index.js +19 -17
  4. package/package.json +11 -9
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
3
+ Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -56,15 +56,19 @@ If you really have to deal with big files, then you might also want to consider
56
56
 
57
57
  ```javascript
58
58
  import middy from '@middy/core'
59
+ import httpHeaderNormalizer from '@middy/http-header-normalizer'
59
60
  import httpMultipartBodyParser from '@middy/http-multipart-body-parser'
60
61
  const handler = middy((event, context) => {
61
62
  return {}
62
63
  })
63
- handler.use(httpMultipartBodyParser())
64
+ handler
65
+ .use(httpHeaderNormalizer())
66
+ .use(httpMultipartBodyParser())
67
+
64
68
  // invokes the handler
65
69
  const event = {
66
70
  headers: {
67
- 'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryppsQEwf2BVJeCe0M'
71
+ 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryppsQEwf2BVJeCe0M'
68
72
  },
69
73
  body: 'LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5cHBzUUV3ZjJCVkplQ2UwTQ0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmb28iDQoNCmJhcg0KLS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5cHBzUUV3ZjJCVkplQ2UwTS0t',
70
74
  isBase64Encoded: true
@@ -86,7 +90,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
86
90
 
87
91
  ## License
88
92
 
89
- Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
93
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
90
94
 
91
95
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
92
96
  <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
package/index.js CHANGED
@@ -1,19 +1,21 @@
1
- const BusBoy = require('busboy')
1
+ import BusBoy from 'busboy'
2
+ import { createError } from '@middy/util'
3
+
2
4
  const mimePattern = /^multipart\/form-data(;.*)?$/
3
5
  const fieldnamePattern = /(.+)\[(.*)]$/
4
6
 
5
- const httpMultipartBodyParserMiddleware = (opts = {}) => {
6
- const defaults = {
7
- // busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
8
- busboy: {}
9
- }
7
+ const defaults = {
8
+ // busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
9
+ busboy: {}
10
+ }
10
11
 
12
+ const httpMultipartBodyParserMiddleware = (opts = {}) => {
11
13
  const options = { ...defaults, ...opts }
12
14
 
13
15
  const httpMultipartBodyParserMiddlewareBefore = async (request) => {
14
16
  const { headers } = request.event
15
17
 
16
- const contentType = headers?.['Content-Type'] ?? headers?.['content-type']
18
+ const contentType = headers['Content-Type'] ?? headers['content-type']
17
19
 
18
20
  if (!mimePattern.test(contentType)) return
19
21
 
@@ -21,10 +23,9 @@ const httpMultipartBodyParserMiddleware = (opts = {}) => {
21
23
  .then((multipartData) => {
22
24
  request.event.body = multipartData
23
25
  })
24
- .catch(() => {
25
- const { createError } = require('@middy/util')
26
+ .catch((e) => {
26
27
  // UnprocessableEntity
27
- throw createError(422, 'Invalid or malformed multipart/form-data was provided')
28
+ throw createError(422, 'Invalid or malformed multipart/form-data was provided - ' + e.message)
28
29
  })
29
30
  }
30
31
 
@@ -35,10 +36,11 @@ const httpMultipartBodyParserMiddleware = (opts = {}) => {
35
36
 
36
37
  const parseMultipartData = (event, options) => {
37
38
  const multipartData = {}
38
- const bb = BusBoy({ ...options, headers: event.headers })
39
+ // header must be lowercase (content-type)
40
+ const busboy = BusBoy({ ...options, headers: { 'content-type': event.headers['Content-Type'] ?? event.headers['content-type'] } })
39
41
 
40
42
  return new Promise((resolve, reject) => {
41
- bb.on('file', (fieldname, file, filename, encoding, mimetype) => {
43
+ busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
42
44
  const attachment = {
43
45
  filename,
44
46
  mimetype,
@@ -72,11 +74,11 @@ const parseMultipartData = (event, options) => {
72
74
  multipartData[matches[1]].push(value)
73
75
  }
74
76
  })
75
- .on('finish', () => resolve(multipartData))
76
- .on('error', (err) => reject(err))
77
+ .on('close', () => resolve(multipartData))
78
+ .on('error', (e) => reject(e))
77
79
 
78
- bb.write(event.body, event.isBase64Encoded ? 'base64' : 'binary')
79
- bb.end()
80
+ busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'utf8')
81
+ busboy.end()
80
82
  })
81
83
  }
82
- module.exports = httpMultipartBodyParserMiddleware
84
+ export default httpMultipartBodyParserMiddleware
package/package.json CHANGED
@@ -1,23 +1,25 @@
1
1
  {
2
2
  "name": "@middy/http-multipart-body-parser",
3
- "version": "2.5.6",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "Http event normalizer middleware for the middy framework",
5
- "type": "commonjs",
5
+ "type": "module",
6
6
  "engines": {
7
- "node": ">=12"
7
+ "node": ">=14"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "index.js",
13
+ "exports": "./index.js",
14
14
  "types": "index.d.ts",
15
15
  "files": [
16
+ "index.js",
16
17
  "index.d.ts"
17
18
  ],
18
19
  "scripts": {
19
20
  "test": "npm run test:unit",
20
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
21
23
  },
22
24
  "license": "MIT",
23
25
  "keywords": [
@@ -46,11 +48,11 @@
46
48
  },
47
49
  "homepage": "https://github.com/middyjs/middy#readme",
48
50
  "dependencies": {
49
- "@middy/util": "^2.5.6",
50
- "busboy": "0.3.1"
51
+ "@middy/util": "^3.0.0-alpha.3",
52
+ "busboy": "1.3.0"
51
53
  },
52
54
  "devDependencies": {
53
- "@middy/core": "^2.5.6"
55
+ "@middy/core": "^3.0.0-alpha.3"
54
56
  },
55
- "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
57
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156"
56
58
  }