@middy/http-multipart-body-parser 4.6.5 → 5.0.0-alpha.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.
Files changed (3) hide show
  1. package/index.js +103 -87
  2. package/package.json +5 -11
  3. package/index.cjs +0 -106
package/index.js CHANGED
@@ -1,91 +1,107 @@
1
- import BusBoy from 'busboy';
2
- import { createError } from '@middy/util';
3
- const mimePattern = /^multipart\/form-data(;.*)?$/;
4
- const fieldnamePattern = /(.+)\[(.*)]$/;
1
+ import BusBoy from 'busboy'
2
+ import { createError } from '@middy/util'
3
+
4
+ const mimePattern = /^multipart\/form-data(;.*)?$/
5
+ const fieldnamePattern = /(.+)\[(.*)]$/
6
+
5
7
  const defaults = {
6
- // busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
7
- busboy: {},
8
- charset: 'utf8',
9
- disableContentTypeError: true
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: contentType
25
- });
8
+ // busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
9
+ busboy: {},
10
+ charset: 'utf8',
11
+ disableContentTypeError: false
12
+ }
13
+
14
+ const httpMultipartBodyParserMiddleware = (opts = {}) => {
15
+ const options = { ...defaults, ...opts }
16
+
17
+ const httpMultipartBodyParserMiddlewareBefore = async (request) => {
18
+ const { headers } = request.event
19
+
20
+ const contentType = headers?.['Content-Type'] ?? headers?.['content-type']
21
+
22
+ if (!mimePattern.test(contentType)) {
23
+ if (options.disableContentTypeError) {
24
+ return
25
+ }
26
+ throw createError(415, 'Unsupported Media Type', {
27
+ cause: { package: '@middy/multipart-body-parser', data: contentType }
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
26
71
  }
27
- return parseMultipartData(request.event, options).then((multipartData)=>{
28
- // request.event.rawBody = body
29
- request.event.body = multipartData;
30
- }).catch((cause)=>{
31
- // UnprocessableEntity
32
- throw createError(415, 'Invalid or malformed multipart/form-data was provided', {
33
- cause
34
- });
35
- });
36
- };
37
- return {
38
- before: httpMultipartBodyParserMiddlewareBefore
39
- };
40
- };
41
- const parseMultipartData = (event, options)=>{
42
- const multipartData = {};
43
- const charset = event.isBase64Encoded ? 'base64' : options.charset;
44
- // header must be lowercase (content-type)
45
- const busboy = BusBoy({
46
- ...options.busboy,
47
- headers: {
48
- 'content-type': event.headers['Content-Type'] ?? event.headers['content-type']
72
+
73
+ const chunks = []
74
+
75
+ file.on('data', (data) => {
76
+ chunks.push(data)
77
+ })
78
+ file.on('end', () => {
79
+ attachment.truncated = file.truncated
80
+ attachment.content = Buffer.concat(chunks)
81
+ if (!multipartData[fieldname]) {
82
+ multipartData[fieldname] = attachment
83
+ } else {
84
+ const current = multipartData[fieldname]
85
+ multipartData[fieldname] = [attachment].concat(current)
86
+ }
87
+ })
88
+ })
89
+ .on('field', (fieldname, value) => {
90
+ const matches = fieldname.match(fieldnamePattern)
91
+ if (!matches) {
92
+ multipartData[fieldname] = value
93
+ } else {
94
+ if (!multipartData[matches[1]]) {
95
+ multipartData[matches[1]] = []
96
+ }
97
+ multipartData[matches[1]].push(value)
49
98
  }
50
- });
51
- return new Promise((resolve, reject)=>{
52
- busboy.on('file', (fieldname, file, info)=>{
53
- const { filename, encoding, mimeType: mimetype } = info;
54
- const attachment = {
55
- filename,
56
- mimetype,
57
- encoding
58
- };
59
- const chunks = [];
60
- file.on('data', (data)=>{
61
- chunks.push(data);
62
- });
63
- file.on('end', ()=>{
64
- attachment.truncated = file.truncated;
65
- attachment.content = Buffer.concat(chunks);
66
- if (!multipartData[fieldname]) {
67
- multipartData[fieldname] = attachment;
68
- } else {
69
- const current = multipartData[fieldname];
70
- multipartData[fieldname] = [
71
- attachment
72
- ].concat(current);
73
- }
74
- });
75
- }).on('field', (fieldname, value)=>{
76
- const matches = fieldname.match(fieldnamePattern);
77
- if (!matches) {
78
- multipartData[fieldname] = value;
79
- } else {
80
- if (!multipartData[matches[1]]) {
81
- multipartData[matches[1]] = [];
82
- }
83
- multipartData[matches[1]].push(value);
84
- }
85
- }).on('close', ()=>resolve(multipartData)).on('error', (e)=>reject(e));
86
- busboy.write(event.body, charset);
87
- busboy.end();
88
- });
89
- };
90
- export default httpMultipartBodyParserMiddleware;
99
+ })
100
+ .on('close', () => resolve(multipartData))
101
+ .on('error', (e) => reject(e))
91
102
 
103
+ busboy.write(event.body, charset)
104
+ busboy.end()
105
+ })
106
+ }
107
+ export default httpMultipartBodyParserMiddleware
package/package.json CHANGED
@@ -1,33 +1,27 @@
1
1
  {
2
2
  "name": "@middy/http-multipart-body-parser",
3
- "version": "4.6.5",
3
+ "version": "5.0.0-alpha.1",
4
4
  "description": "Http event normalizer middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=18"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "./index.cjs",
14
13
  "module": "./index.js",
15
14
  "exports": {
16
15
  ".": {
17
16
  "import": {
18
17
  "types": "./index.d.ts",
19
18
  "default": "./index.js"
20
- },
21
- "require": {
22
- "types": "./index.d.ts",
23
- "default": "./index.cjs"
24
19
  }
25
20
  }
26
21
  },
27
22
  "types": "index.d.ts",
28
23
  "files": [
29
24
  "index.js",
30
- "index.cjs",
31
25
  "index.d.ts"
32
26
  ],
33
27
  "scripts": {
@@ -66,13 +60,13 @@
66
60
  "url": "https://github.com/sponsors/willfarrell"
67
61
  },
68
62
  "dependencies": {
69
- "@middy/util": "4.6.5",
63
+ "@middy/util": "5.0.0-alpha.1",
70
64
  "busboy": "1.6.0"
71
65
  },
72
66
  "devDependencies": {
73
- "@middy/core": "4.6.5",
67
+ "@middy/core": "5.0.0-alpha.1",
74
68
  "@types/aws-lambda": "^8.10.101",
75
69
  "type-fest": "^4.0.0"
76
70
  },
77
- "gitHead": "573d7b0bb243d8c5a9bcb00cf29d031aa7a0c606"
71
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
78
72
  }
package/index.cjs DELETED
@@ -1,106 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: function() {
8
- return _default;
9
- }
10
- });
11
- const _busboy = /*#__PURE__*/ _interop_require_default(require("busboy"));
12
- const _util = require("@middy/util");
13
- function _interop_require_default(obj) {
14
- return obj && obj.__esModule ? obj : {
15
- default: obj
16
- };
17
- }
18
- const mimePattern = /^multipart\/form-data(;.*)?$/;
19
- const fieldnamePattern = /(.+)\[(.*)]$/;
20
- const defaults = {
21
- // busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
22
- busboy: {},
23
- charset: 'utf8',
24
- disableContentTypeError: true
25
- };
26
- const httpMultipartBodyParserMiddleware = (opts = {})=>{
27
- const options = {
28
- ...defaults,
29
- ...opts
30
- };
31
- const httpMultipartBodyParserMiddlewareBefore = async (request)=>{
32
- const { headers } = request.event;
33
- const contentType = headers?.['Content-Type'] ?? headers?.['content-type'];
34
- if (!mimePattern.test(contentType)) {
35
- if (options.disableContentTypeError) {
36
- return;
37
- }
38
- throw (0, _util.createError)(415, 'Unsupported Media Type', {
39
- cause: contentType
40
- });
41
- }
42
- return parseMultipartData(request.event, options).then((multipartData)=>{
43
- // request.event.rawBody = body
44
- request.event.body = multipartData;
45
- }).catch((cause)=>{
46
- // UnprocessableEntity
47
- throw (0, _util.createError)(415, 'Invalid or malformed multipart/form-data was provided', {
48
- cause
49
- });
50
- });
51
- };
52
- return {
53
- before: httpMultipartBodyParserMiddlewareBefore
54
- };
55
- };
56
- const parseMultipartData = (event, options)=>{
57
- const multipartData = {};
58
- const charset = event.isBase64Encoded ? 'base64' : options.charset;
59
- // header must be lowercase (content-type)
60
- const busboy = (0, _busboy.default)({
61
- ...options.busboy,
62
- headers: {
63
- 'content-type': event.headers['Content-Type'] ?? event.headers['content-type']
64
- }
65
- });
66
- return new Promise((resolve, reject)=>{
67
- busboy.on('file', (fieldname, file, info)=>{
68
- const { filename, encoding, mimeType: mimetype } = info;
69
- const attachment = {
70
- filename,
71
- mimetype,
72
- encoding
73
- };
74
- const chunks = [];
75
- file.on('data', (data)=>{
76
- chunks.push(data);
77
- });
78
- file.on('end', ()=>{
79
- attachment.truncated = file.truncated;
80
- attachment.content = Buffer.concat(chunks);
81
- if (!multipartData[fieldname]) {
82
- multipartData[fieldname] = attachment;
83
- } else {
84
- const current = multipartData[fieldname];
85
- multipartData[fieldname] = [
86
- attachment
87
- ].concat(current);
88
- }
89
- });
90
- }).on('field', (fieldname, value)=>{
91
- const matches = fieldname.match(fieldnamePattern);
92
- if (!matches) {
93
- multipartData[fieldname] = value;
94
- } else {
95
- if (!multipartData[matches[1]]) {
96
- multipartData[matches[1]] = [];
97
- }
98
- multipartData[matches[1]].push(value);
99
- }
100
- }).on('close', ()=>resolve(multipartData)).on('error', (e)=>reject(e));
101
- busboy.write(event.body, charset);
102
- busboy.end();
103
- });
104
- };
105
- const _default = httpMultipartBodyParserMiddleware;
106
-