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