@middy/http-multipart-body-parser 2.5.2 → 2.5.6

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 (2) hide show
  1. package/index.js +56 -61
  2. package/package.json +4 -5
package/index.js CHANGED
@@ -1,87 +1,82 @@
1
- "use strict";
2
-
3
- const BusBoy = require('busboy');
4
-
5
- const mimePattern = /^multipart\/form-data(;.*)?$/;
6
- const fieldnamePattern = /(.+)\[(.*)]$/;
1
+ const BusBoy = require('busboy')
2
+ const mimePattern = /^multipart\/form-data(;.*)?$/
3
+ const fieldnamePattern = /(.+)\[(.*)]$/
7
4
 
8
5
  const httpMultipartBodyParserMiddleware = (opts = {}) => {
9
6
  const defaults = {
10
7
  // busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
11
8
  busboy: {}
12
- };
13
- const options = { ...defaults,
14
- ...opts
15
- };
9
+ }
10
+
11
+ const options = { ...defaults, ...opts }
16
12
 
17
- const httpMultipartBodyParserMiddlewareBefore = async request => {
18
- var _headers$ContentType;
13
+ const httpMultipartBodyParserMiddlewareBefore = async (request) => {
14
+ const { headers } = request.event
19
15
 
20
- const {
21
- headers
22
- } = request.event;
23
- const contentType = (_headers$ContentType = headers === null || headers === void 0 ? void 0 : headers['Content-Type']) !== null && _headers$ContentType !== void 0 ? _headers$ContentType : headers === null || headers === void 0 ? void 0 : headers['content-type'];
24
- if (!mimePattern.test(contentType)) return;
25
- return parseMultipartData(request.event, options.busboy).then(multipartData => {
26
- request.event.body = multipartData;
27
- }).catch(() => {
28
- const {
29
- createError
30
- } = require('@middy/util'); // UnprocessableEntity
16
+ const contentType = headers?.['Content-Type'] ?? headers?.['content-type']
31
17
 
18
+ if (!mimePattern.test(contentType)) return
32
19
 
33
- throw createError(422, 'Invalid or malformed multipart/form-data was provided');
34
- });
35
- };
20
+ return parseMultipartData(request.event, options.busboy)
21
+ .then((multipartData) => {
22
+ request.event.body = multipartData
23
+ })
24
+ .catch(() => {
25
+ const { createError } = require('@middy/util')
26
+ // UnprocessableEntity
27
+ throw createError(422, 'Invalid or malformed multipart/form-data was provided')
28
+ })
29
+ }
36
30
 
37
31
  return {
38
32
  before: httpMultipartBodyParserMiddlewareBefore
39
- };
40
- };
33
+ }
34
+ }
41
35
 
42
36
  const parseMultipartData = (event, options) => {
43
- const multipartData = {};
44
- const bb = BusBoy({ ...options,
45
- headers: event.headers
46
- });
37
+ const multipartData = {}
38
+ const bb = BusBoy({ ...options, headers: event.headers })
39
+
47
40
  return new Promise((resolve, reject) => {
48
41
  bb.on('file', (fieldname, file, filename, encoding, mimetype) => {
49
42
  const attachment = {
50
43
  filename,
51
44
  mimetype,
52
45
  encoding
53
- };
54
- const chunks = [];
55
- file.on('data', data => {
56
- chunks.push(data);
57
- });
58
- file.on('end', () => {
59
- attachment.truncated = file.truncated;
60
- attachment.content = Buffer.concat(chunks);
46
+ }
61
47
 
48
+ const chunks = []
49
+
50
+ file.on('data', (data) => {
51
+ chunks.push(data)
52
+ })
53
+ file.on('end', () => {
54
+ attachment.truncated = file.truncated
55
+ attachment.content = Buffer.concat(chunks)
62
56
  if (!multipartData[fieldname]) {
63
- multipartData[fieldname] = attachment;
57
+ multipartData[fieldname] = attachment
64
58
  } else {
65
- const current = multipartData[fieldname];
66
- multipartData[fieldname] = [attachment].concat(current);
59
+ const current = multipartData[fieldname]
60
+ multipartData[fieldname] = [attachment].concat(current)
67
61
  }
68
- });
69
- }).on('field', (fieldname, value) => {
70
- const matches = fieldname.match(fieldnamePattern);
71
-
72
- if (!matches) {
73
- multipartData[fieldname] = value;
74
- } else {
75
- if (!multipartData[matches[1]]) {
76
- multipartData[matches[1]] = [];
62
+ })
63
+ })
64
+ .on('field', (fieldname, value) => {
65
+ const matches = fieldname.match(fieldnamePattern)
66
+ if (!matches) {
67
+ multipartData[fieldname] = value
68
+ } else {
69
+ if (!multipartData[matches[1]]) {
70
+ multipartData[matches[1]] = []
71
+ }
72
+ multipartData[matches[1]].push(value)
77
73
  }
74
+ })
75
+ .on('finish', () => resolve(multipartData))
76
+ .on('error', (err) => reject(err))
78
77
 
79
- multipartData[matches[1]].push(value);
80
- }
81
- }).on('finish', () => resolve(multipartData)).on('error', err => reject(err));
82
- bb.write(event.body, event.isBase64Encoded ? 'base64' : 'binary');
83
- bb.end();
84
- });
85
- };
86
-
87
- module.exports = httpMultipartBodyParserMiddleware;
78
+ bb.write(event.body, event.isBase64Encoded ? 'base64' : 'binary')
79
+ bb.end()
80
+ })
81
+ }
82
+ module.exports = httpMultipartBodyParserMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-multipart-body-parser",
3
- "version": "2.5.2",
3
+ "version": "2.5.6",
4
4
  "description": "Http event normalizer middleware for the middy framework",
5
5
  "type": "commonjs",
6
6
  "engines": {
@@ -46,12 +46,11 @@
46
46
  },
47
47
  "homepage": "https://github.com/middyjs/middy#readme",
48
48
  "dependencies": {
49
- "@middy/util": "^2.5.2",
49
+ "@middy/util": "^2.5.6",
50
50
  "busboy": "0.3.1"
51
51
  },
52
52
  "devDependencies": {
53
- "@middy/core": "^2.5.2",
54
- "http-errors": "^1.8.0"
53
+ "@middy/core": "^2.5.6"
55
54
  },
56
- "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
55
+ "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
57
56
  }