@middy/http-multipart-body-parser 3.0.0-alpha.6 → 3.0.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/README.md +25 -15
- package/index.cjs +3 -0
- package/index.js +2 -80
- package/package.json +13 -6
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
# Middy http-multipart-body-parser middleware
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy http-multipart-body-parser middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>HTTP json body parser middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
14
|
-
<img src="https://badge.fury.io/js/%40middy%2Fhttp-
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/http-multipart-body-parser?activeTab=versions">
|
|
7
|
+
<img src="https://badge.fury.io/js/%40middy%2Fhttp-multipart-body-parser.svg" alt="npm version" style="max-width:100%;">
|
|
8
|
+
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/http-multipart-body-parser">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/http-multipart-body-parser" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
15
18
|
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
21
27
|
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
|
@@ -47,7 +57,7 @@ npm install --save @middy/http-multipart-body-parser
|
|
|
47
57
|
|
|
48
58
|
## Options
|
|
49
59
|
|
|
50
|
-
- `busboy` (object) (
|
|
60
|
+
- `busboy` (object) (default `{}`): it can be used to pass extraparameters to the internal `busboy` instance at creation time. Checkout [the official documentation](https://www.npmjs.com/package/busboy#busboy-methods) for more information on the supported options.
|
|
51
61
|
|
|
52
62
|
**Note**: this middleware will buffer all the data as it is processed internally by `busboy`, so, if you are using this approach to parse significantly big volumes of data, keep in mind that all the data will be allocated in memory. This is somewhat inevitable with Lambdas (as the data is already encoded into the JSON in memory as Base64), but it's good to keep this in mind and evaluate the impact on you application.
|
|
53
63
|
If you really have to deal with big files, then you might also want to consider to allowing your users to [directly upload files to S3](https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-UsingHTTPPOST.html)
|
package/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _busboy=_interopRequireDefault(require("busboy"));var _util=require("@middy/util");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}const mimePattern=/^multipart\/form-data(;.*)?$/;const fieldnamePattern=/(.+)\[(.*)]$/;const defaults={busboy:{}};const httpMultipartBodyParserMiddleware=(opts={})=>{const options={...defaults,...opts};const httpMultipartBodyParserMiddlewareBefore=async request=>{const{headers}=request.event;const contentType=headers["Content-Type"]??headers["content-type"];if(!mimePattern.test(contentType))return;return parseMultipartData(request.event,options.busboy).then(multipartData=>{request.event.body=multipartData}).catch(cause=>{const error=(0,_util).createError(422,"Invalid or malformed multipart/form-data was provided");error.cause=cause;throw error})};return{before:httpMultipartBodyParserMiddlewareBefore}};const parseMultipartData=(event,options)=>{const multipartData={};const busboy=(0,_busboy).default({...options,headers:{"content-type":event.headers["Content-Type"]??event.headers["content-type"]}});return new Promise((resolve,reject)=>{busboy.on("file",(fieldname,file,filename,encoding,mimetype)=>{const attachment={filename,mimetype,encoding};const chunks=[];file.on("data",data=>{chunks.push(data)});file.on("end",()=>{attachment.truncated=file.truncated;attachment.content=Buffer.concat(chunks);if(!multipartData[fieldname]){multipartData[fieldname]=attachment}else{const current=multipartData[fieldname];multipartData[fieldname]=[attachment].concat(current)}})}).on("field",(fieldname,value)=>{const matches=fieldname.match(fieldnamePattern);if(!matches){multipartData[fieldname]=value}else{if(!multipartData[matches[1]]){multipartData[matches[1]]=[]}multipartData[matches[1]].push(value)}}).on("close",()=>resolve(multipartData)).on("error",e=>reject(e));busboy.write(event.body,event.isBase64Encoded?"base64":"utf8");busboy.end()})};var _default=httpMultipartBodyParserMiddleware;exports.default=_default
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,81 +1,3 @@
|
|
|
1
|
-
import BusBoy from
|
|
2
|
-
import { createError } from '@middy/util';
|
|
3
|
-
const mimePattern = /^multipart\/form-data(;.*)?$/;
|
|
4
|
-
const fieldnamePattern = /(.+)\[(.*)]$/;
|
|
5
|
-
const defaults = {
|
|
6
|
-
busboy: {}
|
|
7
|
-
};
|
|
1
|
+
import BusBoy from"busboy";import{createError}from"@middy/util";const mimePattern=/^multipart\/form-data(;.*)?$/;const fieldnamePattern=/(.+)\[(.*)]$/;const defaults={busboy:{}};const httpMultipartBodyParserMiddleware=(opts={})=>{const options={...defaults,...opts};const httpMultipartBodyParserMiddlewareBefore=async request=>{const{headers}=request.event;const contentType=headers["Content-Type"]??headers["content-type"];if(!mimePattern.test(contentType))return;return parseMultipartData(request.event,options.busboy).then(multipartData=>{request.event.body=multipartData}).catch(cause=>{const error=createError(422,"Invalid or malformed multipart/form-data was provided");error.cause=cause;throw error})};return{before:httpMultipartBodyParserMiddlewareBefore}};const parseMultipartData=(event,options)=>{const multipartData={};const busboy=BusBoy({...options,headers:{"content-type":event.headers["Content-Type"]??event.headers["content-type"]}});return new Promise((resolve,reject)=>{busboy.on("file",(fieldname,file,filename,encoding,mimetype)=>{const attachment={filename,mimetype,encoding};const chunks=[];file.on("data",data=>{chunks.push(data)});file.on("end",()=>{attachment.truncated=file.truncated;attachment.content=Buffer.concat(chunks);if(!multipartData[fieldname]){multipartData[fieldname]=attachment}else{const current=multipartData[fieldname];multipartData[fieldname]=[attachment].concat(current)}})}).on("field",(fieldname,value)=>{const matches=fieldname.match(fieldnamePattern);if(!matches){multipartData[fieldname]=value}else{if(!multipartData[matches[1]]){multipartData[matches[1]]=[]}multipartData[matches[1]].push(value)}}).on("close",()=>resolve(multipartData)).on("error",e=>reject(e));busboy.write(event.body,event.isBase64Encoded?"base64":"utf8");busboy.end()})};export default httpMultipartBodyParserMiddleware
|
|
8
2
|
|
|
9
|
-
|
|
10
|
-
const options = { ...defaults,
|
|
11
|
-
...opts
|
|
12
|
-
};
|
|
13
|
-
|
|
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(cause => {
|
|
23
|
-
const error = createError(422, 'Invalid or malformed multipart/form-data was provided');
|
|
24
|
-
error.cause = cause;
|
|
25
|
-
throw error;
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
before: httpMultipartBodyParserMiddlewareBefore
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const parseMultipartData = (event, options) => {
|
|
35
|
-
const multipartData = {};
|
|
36
|
-
const busboy = BusBoy({ ...options,
|
|
37
|
-
headers: {
|
|
38
|
-
'content-type': event.headers['Content-Type'] ?? event.headers['content-type']
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
return new Promise((resolve, reject) => {
|
|
42
|
-
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
|
|
43
|
-
const attachment = {
|
|
44
|
-
filename,
|
|
45
|
-
mimetype,
|
|
46
|
-
encoding
|
|
47
|
-
};
|
|
48
|
-
const chunks = [];
|
|
49
|
-
file.on('data', data => {
|
|
50
|
-
chunks.push(data);
|
|
51
|
-
});
|
|
52
|
-
file.on('end', () => {
|
|
53
|
-
attachment.truncated = file.truncated;
|
|
54
|
-
attachment.content = Buffer.concat(chunks);
|
|
55
|
-
|
|
56
|
-
if (!multipartData[fieldname]) {
|
|
57
|
-
multipartData[fieldname] = attachment;
|
|
58
|
-
} else {
|
|
59
|
-
const current = multipartData[fieldname];
|
|
60
|
-
multipartData[fieldname] = [attachment].concat(current);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}).on('field', (fieldname, value) => {
|
|
64
|
-
const matches = fieldname.match(fieldnamePattern);
|
|
65
|
-
|
|
66
|
-
if (!matches) {
|
|
67
|
-
multipartData[fieldname] = value;
|
|
68
|
-
} else {
|
|
69
|
-
if (!multipartData[matches[1]]) {
|
|
70
|
-
multipartData[matches[1]] = [];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
multipartData[matches[1]].push(value);
|
|
74
|
-
}
|
|
75
|
-
}).on('close', () => resolve(multipartData)).on('error', e => reject(e));
|
|
76
|
-
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'utf8');
|
|
77
|
-
busboy.end();
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
export default httpMultipartBodyParserMiddleware;
|
|
3
|
+
//# 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.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Http event normalizer middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"exports":
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./index.js",
|
|
16
|
+
"require": "./index.cjs",
|
|
17
|
+
"types": "./index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
14
20
|
"types": "index.d.ts",
|
|
15
21
|
"files": [
|
|
16
22
|
"index.js",
|
|
23
|
+
"index.cjs",
|
|
17
24
|
"index.d.ts"
|
|
18
25
|
],
|
|
19
26
|
"scripts": {
|
|
@@ -46,13 +53,13 @@
|
|
|
46
53
|
"bugs": {
|
|
47
54
|
"url": "https://github.com/middyjs/middy/issues"
|
|
48
55
|
},
|
|
49
|
-
"homepage": "https://
|
|
56
|
+
"homepage": "https://middy.js.org",
|
|
50
57
|
"dependencies": {
|
|
51
|
-
"@middy/util": "^3.0.0
|
|
58
|
+
"@middy/util": "^3.0.0",
|
|
52
59
|
"busboy": "1.4.0"
|
|
53
60
|
},
|
|
54
61
|
"devDependencies": {
|
|
55
|
-
"@middy/core": "^3.0.0
|
|
62
|
+
"@middy/core": "^3.0.0"
|
|
56
63
|
},
|
|
57
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6"
|
|
58
65
|
}
|