@middy/http-multipart-body-parser 2.5.1 → 3.0.0-alpha.0
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 +8 -4
- package/package.json +8 -9
- package/index.js +0 -87
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
|
|
59
|
+
import httpHeaderNormalizer from '@middy/http-header-normalizer'
|
|
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/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-multipart-body-parser",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.0",
|
|
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
16
|
"index.d.ts"
|
|
@@ -46,12 +46,11 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@middy/util": "^
|
|
50
|
-
"busboy": "
|
|
49
|
+
"@middy/util": "^3.0.0-alpha.0",
|
|
50
|
+
"busboy": "1.3.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@middy/core": "^
|
|
54
|
-
"http-errors": "^1.8.0"
|
|
53
|
+
"@middy/core": "^3.0.0-alpha.0"
|
|
55
54
|
},
|
|
56
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db"
|
|
57
56
|
}
|
package/index.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const BusBoy = require('busboy');
|
|
4
|
-
|
|
5
|
-
const mimePattern = /^multipart\/form-data(;.*)?$/;
|
|
6
|
-
const fieldnamePattern = /(.+)\[(.*)]$/;
|
|
7
|
-
|
|
8
|
-
const httpMultipartBodyParserMiddleware = (opts = {}) => {
|
|
9
|
-
const defaults = {
|
|
10
|
-
// busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
|
|
11
|
-
busboy: {}
|
|
12
|
-
};
|
|
13
|
-
const options = { ...defaults,
|
|
14
|
-
...opts
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const httpMultipartBodyParserMiddlewareBefore = async request => {
|
|
18
|
-
var _headers$ContentType;
|
|
19
|
-
|
|
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
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
throw createError(422, 'Invalid or malformed multipart/form-data was provided');
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
before: httpMultipartBodyParserMiddlewareBefore
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const parseMultipartData = (event, options) => {
|
|
43
|
-
const multipartData = {};
|
|
44
|
-
const bb = BusBoy({ ...options,
|
|
45
|
-
headers: event.headers
|
|
46
|
-
});
|
|
47
|
-
return new Promise((resolve, reject) => {
|
|
48
|
-
bb.on('file', (fieldname, file, filename, encoding, mimetype) => {
|
|
49
|
-
const attachment = {
|
|
50
|
-
filename,
|
|
51
|
-
mimetype,
|
|
52
|
-
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);
|
|
61
|
-
|
|
62
|
-
if (!multipartData[fieldname]) {
|
|
63
|
-
multipartData[fieldname] = attachment;
|
|
64
|
-
} else {
|
|
65
|
-
const current = multipartData[fieldname];
|
|
66
|
-
multipartData[fieldname] = [attachment].concat(current);
|
|
67
|
-
}
|
|
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]] = [];
|
|
77
|
-
}
|
|
78
|
-
|
|
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;
|