@middy/http-multipart-body-parser 3.0.3 → 3.0.4
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/index.cjs +87 -1
- package/index.js +76 -1
- package/package.json +4 -4
package/index.cjs
CHANGED
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
module.exports = void 0;
|
|
6
|
+
var _busboy = _interopRequireDefault(require("busboy"));
|
|
7
|
+
var _util = require("@middy/util");
|
|
8
|
+
function _interopRequireDefault(obj) {
|
|
9
|
+
return obj && obj.__esModule ? obj : {
|
|
10
|
+
default: obj
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
const mimePattern = /^multipart\/form-data(;.*)?$/;
|
|
14
|
+
const fieldnamePattern = /(.+)\[(.*)]$/;
|
|
15
|
+
const defaults = {
|
|
16
|
+
busboy: {}
|
|
17
|
+
};
|
|
18
|
+
const httpMultipartBodyParserMiddleware = (opts = {})=>{
|
|
19
|
+
const options = {
|
|
20
|
+
...defaults,
|
|
21
|
+
...opts
|
|
22
|
+
};
|
|
23
|
+
const httpMultipartBodyParserMiddlewareBefore = async (request)=>{
|
|
24
|
+
const { headers } = request.event;
|
|
25
|
+
const contentType = headers['Content-Type'] ?? headers['content-type'];
|
|
26
|
+
if (!mimePattern.test(contentType)) return;
|
|
27
|
+
return parseMultipartData(request.event, options.busboy).then((multipartData)=>{
|
|
28
|
+
request.event.body = multipartData;
|
|
29
|
+
}).catch((cause)=>{
|
|
30
|
+
const error = (0, _util).createError(422, 'Invalid or malformed multipart/form-data was provided');
|
|
31
|
+
error.cause = cause;
|
|
32
|
+
throw error;
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
before: httpMultipartBodyParserMiddlewareBefore
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
const parseMultipartData = (event, options)=>{
|
|
40
|
+
const multipartData = {};
|
|
41
|
+
const busboy = (0, _busboy).default({
|
|
42
|
+
...options,
|
|
43
|
+
headers: {
|
|
44
|
+
'content-type': event.headers['Content-Type'] ?? event.headers['content-type']
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return new Promise((resolve, reject)=>{
|
|
48
|
+
busboy.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
|
+
if (!multipartData[fieldname]) {
|
|
62
|
+
multipartData[fieldname] = attachment;
|
|
63
|
+
} else {
|
|
64
|
+
const current = multipartData[fieldname];
|
|
65
|
+
multipartData[fieldname] = [
|
|
66
|
+
attachment
|
|
67
|
+
].concat(current);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}).on('field', (fieldname, value)=>{
|
|
71
|
+
const matches = fieldname.match(fieldnamePattern);
|
|
72
|
+
if (!matches) {
|
|
73
|
+
multipartData[fieldname] = value;
|
|
74
|
+
} else {
|
|
75
|
+
if (!multipartData[matches[1]]) {
|
|
76
|
+
multipartData[matches[1]] = [];
|
|
77
|
+
}
|
|
78
|
+
multipartData[matches[1]].push(value);
|
|
79
|
+
}
|
|
80
|
+
}).on('close', ()=>resolve(multipartData)).on('error', (e)=>reject(e));
|
|
81
|
+
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'utf8');
|
|
82
|
+
busboy.end();
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
var _default = httpMultipartBodyParserMiddleware;
|
|
86
|
+
module.exports = _default;
|
|
87
|
+
|
|
2
88
|
|
|
3
89
|
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,3 +1,78 @@
|
|
|
1
|
-
import BusBoy from
|
|
1
|
+
import BusBoy from 'busboy';
|
|
2
|
+
import { createError } from '@middy/util';
|
|
3
|
+
const mimePattern = /^multipart\/form-data(;.*)?$/;
|
|
4
|
+
const fieldnamePattern = /(.+)\[(.*)]$/;
|
|
5
|
+
const defaults = {
|
|
6
|
+
busboy: {}
|
|
7
|
+
};
|
|
8
|
+
const httpMultipartBodyParserMiddleware = (opts = {})=>{
|
|
9
|
+
const options = {
|
|
10
|
+
...defaults,
|
|
11
|
+
...opts
|
|
12
|
+
};
|
|
13
|
+
const httpMultipartBodyParserMiddlewareBefore = async (request)=>{
|
|
14
|
+
const { headers } = request.event;
|
|
15
|
+
const contentType = headers['Content-Type'] ?? headers['content-type'];
|
|
16
|
+
if (!mimePattern.test(contentType)) return;
|
|
17
|
+
return parseMultipartData(request.event, options.busboy).then((multipartData)=>{
|
|
18
|
+
request.event.body = multipartData;
|
|
19
|
+
}).catch((cause)=>{
|
|
20
|
+
const error = createError(422, 'Invalid or malformed multipart/form-data was provided');
|
|
21
|
+
error.cause = cause;
|
|
22
|
+
throw error;
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
before: httpMultipartBodyParserMiddlewareBefore
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
const parseMultipartData = (event, options)=>{
|
|
30
|
+
const multipartData = {};
|
|
31
|
+
const busboy = BusBoy({
|
|
32
|
+
...options,
|
|
33
|
+
headers: {
|
|
34
|
+
'content-type': event.headers['Content-Type'] ?? event.headers['content-type']
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return new Promise((resolve, reject)=>{
|
|
38
|
+
busboy.on('file', (fieldname, file, filename, encoding, mimetype)=>{
|
|
39
|
+
const attachment = {
|
|
40
|
+
filename,
|
|
41
|
+
mimetype,
|
|
42
|
+
encoding
|
|
43
|
+
};
|
|
44
|
+
const chunks = [];
|
|
45
|
+
file.on('data', (data)=>{
|
|
46
|
+
chunks.push(data);
|
|
47
|
+
});
|
|
48
|
+
file.on('end', ()=>{
|
|
49
|
+
attachment.truncated = file.truncated;
|
|
50
|
+
attachment.content = Buffer.concat(chunks);
|
|
51
|
+
if (!multipartData[fieldname]) {
|
|
52
|
+
multipartData[fieldname] = attachment;
|
|
53
|
+
} else {
|
|
54
|
+
const current = multipartData[fieldname];
|
|
55
|
+
multipartData[fieldname] = [
|
|
56
|
+
attachment
|
|
57
|
+
].concat(current);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}).on('field', (fieldname, value)=>{
|
|
61
|
+
const matches = fieldname.match(fieldnamePattern);
|
|
62
|
+
if (!matches) {
|
|
63
|
+
multipartData[fieldname] = value;
|
|
64
|
+
} else {
|
|
65
|
+
if (!multipartData[matches[1]]) {
|
|
66
|
+
multipartData[matches[1]] = [];
|
|
67
|
+
}
|
|
68
|
+
multipartData[matches[1]].push(value);
|
|
69
|
+
}
|
|
70
|
+
}).on('close', ()=>resolve(multipartData)).on('error', (e)=>reject(e));
|
|
71
|
+
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'utf8');
|
|
72
|
+
busboy.end();
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
export default httpMultipartBodyParserMiddleware;
|
|
76
|
+
|
|
2
77
|
|
|
3
78
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-multipart-body-parser",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "Http event normalizer middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -55,11 +55,11 @@
|
|
|
55
55
|
},
|
|
56
56
|
"homepage": "https://middy.js.org",
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@middy/util": "3.0.
|
|
58
|
+
"@middy/util": "3.0.4",
|
|
59
59
|
"busboy": "1.6.0"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@middy/core": "3.0.
|
|
62
|
+
"@middy/core": "3.0.4"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "3e9bc83e791f943c71cd7003fc27f0a3692d83a1"
|
|
65
65
|
}
|