@middy/http-multipart-body-parser 3.0.0-alpha.2 → 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 (2) hide show
  1. package/index.js +65 -60
  2. package/package.json +6 -5
package/index.js CHANGED
@@ -1,79 +1,84 @@
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
+
7
+ const defaults = {
8
+ // busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
9
+ busboy: {}
10
+ }
5
11
 
6
12
  const httpMultipartBodyParserMiddleware = (opts = {}) => {
7
- const defaults = {
8
- busboy: {}
9
- };
10
- const options = { ...defaults,
11
- ...opts
12
- };
13
+ const options = { ...defaults, ...opts }
14
+
15
+ const httpMultipartBodyParserMiddlewareBefore = async (request) => {
16
+ const { headers } = request.event
17
+
18
+ const contentType = headers['Content-Type'] ?? headers['content-type']
13
19
 
14
- const httpMultipartBodyParserMiddlewareBefore = async request => {
15
- const {
16
- headers
17
- } = request.event;
18
- const contentType = headers['Content-Type'] ?? headers['content-type'];
19
- if (!mimePattern.test(contentType)) return;
20
- return parseMultipartData(request.event, options.busboy).then(multipartData => {
21
- request.event.body = multipartData;
22
- }).catch(e => {
23
- throw createError(422, 'Invalid or malformed multipart/form-data was provided - ' + e.message);
24
- });
25
- };
20
+ if (!mimePattern.test(contentType)) return
21
+
22
+ return parseMultipartData(request.event, options.busboy)
23
+ .then((multipartData) => {
24
+ request.event.body = multipartData
25
+ })
26
+ .catch((e) => {
27
+ // UnprocessableEntity
28
+ throw createError(422, 'Invalid or malformed multipart/form-data was provided - ' + e.message)
29
+ })
30
+ }
26
31
 
27
32
  return {
28
33
  before: httpMultipartBodyParserMiddlewareBefore
29
- };
30
- };
34
+ }
35
+ }
31
36
 
32
37
  const parseMultipartData = (event, options) => {
33
- const multipartData = {};
34
- const bb = BusBoy({ ...options,
35
- headers: {
36
- 'content-type': event.headers['Content-Type'] ?? event.headers['content-type']
37
- }
38
- });
38
+ const multipartData = {}
39
+ // header must be lowercase (content-type)
40
+ const busboy = BusBoy({ ...options, headers: { 'content-type': event.headers['Content-Type'] ?? event.headers['content-type'] } })
41
+
39
42
  return new Promise((resolve, reject) => {
40
- bb.on('file', (fieldname, file, filename, encoding, mimetype) => {
43
+ busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
41
44
  const attachment = {
42
45
  filename,
43
46
  mimetype,
44
47
  encoding
45
- };
46
- const chunks = [];
47
- file.on('data', data => {
48
- chunks.push(data);
49
- });
50
- file.on('end', () => {
51
- attachment.truncated = file.truncated;
52
- attachment.content = Buffer.concat(chunks);
48
+ }
49
+
50
+ const chunks = []
53
51
 
52
+ file.on('data', (data) => {
53
+ chunks.push(data)
54
+ })
55
+ file.on('end', () => {
56
+ attachment.truncated = file.truncated
57
+ attachment.content = Buffer.concat(chunks)
54
58
  if (!multipartData[fieldname]) {
55
- multipartData[fieldname] = attachment;
59
+ multipartData[fieldname] = attachment
56
60
  } else {
57
- const current = multipartData[fieldname];
58
- multipartData[fieldname] = [attachment].concat(current);
61
+ const current = multipartData[fieldname]
62
+ multipartData[fieldname] = [attachment].concat(current)
59
63
  }
60
- });
61
- }).on('field', (fieldname, value) => {
62
- const matches = fieldname.match(fieldnamePattern);
63
-
64
- if (!matches) {
65
- multipartData[fieldname] = value;
66
- } else {
67
- if (!multipartData[matches[1]]) {
68
- multipartData[matches[1]] = [];
64
+ })
65
+ })
66
+ .on('field', (fieldname, value) => {
67
+ const matches = fieldname.match(fieldnamePattern)
68
+ if (!matches) {
69
+ multipartData[fieldname] = value
70
+ } else {
71
+ if (!multipartData[matches[1]]) {
72
+ multipartData[matches[1]] = []
73
+ }
74
+ multipartData[matches[1]].push(value)
69
75
  }
76
+ })
77
+ .on('close', () => resolve(multipartData))
78
+ .on('error', (e) => reject(e))
70
79
 
71
- multipartData[matches[1]].push(value);
72
- }
73
- }).on('close', () => resolve(multipartData)).on('error', e => reject(e));
74
- bb.write(event.body, event.isBase64Encoded ? 'base64' : 'utf8');
75
- bb.end();
76
- });
77
- };
78
-
79
- export default httpMultipartBodyParserMiddleware;
80
+ busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'utf8')
81
+ busboy.end()
82
+ })
83
+ }
84
+ export default httpMultipartBodyParserMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-multipart-body-parser",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "Http event normalizer middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -18,7 +18,8 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "test": "npm run test:unit",
21
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
22
23
  },
23
24
  "license": "MIT",
24
25
  "keywords": [
@@ -47,11 +48,11 @@
47
48
  },
48
49
  "homepage": "https://github.com/middyjs/middy#readme",
49
50
  "dependencies": {
50
- "@middy/util": "^3.0.0-alpha.2",
51
+ "@middy/util": "^3.0.0-alpha.3",
51
52
  "busboy": "1.3.0"
52
53
  },
53
54
  "devDependencies": {
54
- "@middy/core": "^3.0.0-alpha.2"
55
+ "@middy/core": "^3.0.0-alpha.3"
55
56
  },
56
- "gitHead": "de30419273ecbff08f367f47c7e320ec981cf145"
57
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156"
57
58
  }