@middy/http-multipart-body-parser 2.5.2 → 3.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.
- package/LICENSE +1 -1
- package/README.md +7 -3
- package/index.js +11 -19
- package/package.json +9 -9
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-
|
|
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
|
|
64
|
+
handler
|
|
65
|
+
.use(httpHeaderNormalizer())
|
|
66
|
+
.use(httpMultipartBodyParser())
|
|
67
|
+
|
|
64
68
|
// invokes the handler
|
|
65
69
|
const event = {
|
|
66
70
|
headers: {
|
|
67
|
-
'
|
|
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-
|
|
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,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const BusBoy = require('busboy');
|
|
4
|
-
|
|
1
|
+
import BusBoy from 'busboy';
|
|
2
|
+
import { createError } from '@middy/util';
|
|
5
3
|
const mimePattern = /^multipart\/form-data(;.*)?$/;
|
|
6
4
|
const fieldnamePattern = /(.+)\[(.*)]$/;
|
|
7
5
|
|
|
8
6
|
const httpMultipartBodyParserMiddleware = (opts = {}) => {
|
|
9
7
|
const defaults = {
|
|
10
|
-
// busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
|
|
11
8
|
busboy: {}
|
|
12
9
|
};
|
|
13
10
|
const options = { ...defaults,
|
|
@@ -15,22 +12,15 @@ const httpMultipartBodyParserMiddleware = (opts = {}) => {
|
|
|
15
12
|
};
|
|
16
13
|
|
|
17
14
|
const httpMultipartBodyParserMiddlewareBefore = async request => {
|
|
18
|
-
var _headers$ContentType;
|
|
19
|
-
|
|
20
15
|
const {
|
|
21
16
|
headers
|
|
22
17
|
} = request.event;
|
|
23
|
-
const contentType =
|
|
18
|
+
const contentType = headers['Content-Type'] ?? headers['content-type'];
|
|
24
19
|
if (!mimePattern.test(contentType)) return;
|
|
25
20
|
return parseMultipartData(request.event, options.busboy).then(multipartData => {
|
|
26
21
|
request.event.body = multipartData;
|
|
27
|
-
}).catch(
|
|
28
|
-
|
|
29
|
-
createError
|
|
30
|
-
} = require('@middy/util'); // UnprocessableEntity
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
throw createError(422, 'Invalid or malformed multipart/form-data was provided');
|
|
22
|
+
}).catch(e => {
|
|
23
|
+
throw createError(422, 'Invalid or malformed multipart/form-data was provided - ' + e.message);
|
|
34
24
|
});
|
|
35
25
|
};
|
|
36
26
|
|
|
@@ -42,7 +32,9 @@ const httpMultipartBodyParserMiddleware = (opts = {}) => {
|
|
|
42
32
|
const parseMultipartData = (event, options) => {
|
|
43
33
|
const multipartData = {};
|
|
44
34
|
const bb = BusBoy({ ...options,
|
|
45
|
-
headers:
|
|
35
|
+
headers: {
|
|
36
|
+
'content-type': event.headers['Content-Type'] ?? event.headers['content-type']
|
|
37
|
+
}
|
|
46
38
|
});
|
|
47
39
|
return new Promise((resolve, reject) => {
|
|
48
40
|
bb.on('file', (fieldname, file, filename, encoding, mimetype) => {
|
|
@@ -78,10 +70,10 @@ const parseMultipartData = (event, options) => {
|
|
|
78
70
|
|
|
79
71
|
multipartData[matches[1]].push(value);
|
|
80
72
|
}
|
|
81
|
-
}).on('
|
|
82
|
-
bb.write(event.body, event.isBase64Encoded ? 'base64' : '
|
|
73
|
+
}).on('close', () => resolve(multipartData)).on('error', e => reject(e));
|
|
74
|
+
bb.write(event.body, event.isBase64Encoded ? 'base64' : 'utf8');
|
|
83
75
|
bb.end();
|
|
84
76
|
});
|
|
85
77
|
};
|
|
86
78
|
|
|
87
|
-
|
|
79
|
+
export default httpMultipartBodyParserMiddleware;
|
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-multipart-body-parser",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
4
|
"description": "Http event normalizer middleware for the middy framework",
|
|
5
|
-
"type": "
|
|
5
|
+
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=14"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"
|
|
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": {
|
|
@@ -46,12 +47,11 @@
|
|
|
46
47
|
},
|
|
47
48
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
48
49
|
"dependencies": {
|
|
49
|
-
"@middy/util": "^
|
|
50
|
-
"busboy": "
|
|
50
|
+
"@middy/util": "^3.0.0-alpha.1",
|
|
51
|
+
"busboy": "1.3.0"
|
|
51
52
|
},
|
|
52
53
|
"devDependencies": {
|
|
53
|
-
"@middy/core": "^
|
|
54
|
-
"http-errors": "^1.8.0"
|
|
54
|
+
"@middy/core": "^3.0.0-alpha.1"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "a14125c6b2e21b181824f9985a919a47f1e4711f"
|
|
57
57
|
}
|