@middy/http-multipart-body-parser 6.1.6 → 6.2.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/index.d.ts +28 -28
- package/index.js +106 -106
- package/package.json +69 -72
package/index.d.ts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import middy from
|
|
2
|
-
import { APIGatewayEvent } from
|
|
3
|
-
import { JsonValue } from
|
|
1
|
+
import type middy from "@middy/core";
|
|
2
|
+
import type { APIGatewayEvent } from "aws-lambda";
|
|
3
|
+
import type { JsonValue } from "type-fest";
|
|
4
4
|
|
|
5
5
|
interface Options {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
6
|
+
busboy?: {
|
|
7
|
+
headers?: any;
|
|
8
|
+
highWaterMark?: number;
|
|
9
|
+
fileHwm?: number;
|
|
10
|
+
defCharset?: string;
|
|
11
|
+
preservePath?: boolean;
|
|
12
|
+
limits?: {
|
|
13
|
+
fieldNameSize?: number;
|
|
14
|
+
fieldSize?: number;
|
|
15
|
+
fields?: number;
|
|
16
|
+
fileSize?: number;
|
|
17
|
+
files?: number;
|
|
18
|
+
parts?: number;
|
|
19
|
+
headerPairs?: number;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
charset?: string;
|
|
23
|
+
disableContentTypeError?: boolean;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
export type Event = Omit<APIGatewayEvent,
|
|
27
|
-
|
|
28
|
-
}
|
|
26
|
+
export type Event = Omit<APIGatewayEvent, "body"> & {
|
|
27
|
+
body: JsonValue;
|
|
28
|
+
};
|
|
29
29
|
|
|
30
|
-
declare function multipartBodyParser
|
|
31
|
-
|
|
32
|
-
): middy.MiddlewareObj<Event
|
|
30
|
+
declare function multipartBodyParser(
|
|
31
|
+
options?: Options,
|
|
32
|
+
): middy.MiddlewareObj<Event>;
|
|
33
33
|
|
|
34
|
-
export default multipartBodyParser
|
|
34
|
+
export default multipartBodyParser;
|
package/index.js
CHANGED
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
import BusBoy from
|
|
2
|
-
import { createError } from
|
|
1
|
+
import BusBoy from "@fastify/busboy";
|
|
2
|
+
import { createError } from "@middy/util";
|
|
3
3
|
|
|
4
4
|
const mimePattern =
|
|
5
|
-
|
|
6
|
-
const fieldnamePattern = /(.+)\[(.*)]
|
|
5
|
+
/^multipart\/form-data; boundary=[-]*[a-zA-Z0-9]*(; ?[cC]harset=[\w-]+)?$/;
|
|
6
|
+
const fieldnamePattern = /(.+)\[(.*)]$/;
|
|
7
7
|
|
|
8
8
|
const defaults = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
9
|
+
// busboy options as per documentation: https://www.npmjs.com/package/busboy#busboy-methods
|
|
10
|
+
busboy: {},
|
|
11
|
+
charset: "utf8",
|
|
12
|
+
disableContentTypeError: false,
|
|
13
|
+
};
|
|
14
14
|
|
|
15
15
|
const httpMultipartBodyParserMiddleware = (opts = {}) => {
|
|
16
|
-
|
|
16
|
+
const options = { ...defaults, ...opts };
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
const httpMultipartBodyParserMiddlewareBefore = async (request) => {
|
|
19
|
+
const { headers, body } = request.event;
|
|
20
|
+
if (typeof body === "undefined") {
|
|
21
|
+
throw createError(
|
|
22
|
+
415,
|
|
23
|
+
"Invalid or malformed multipart/form-data was provided",
|
|
24
|
+
{ cause: { package: "@middy/http-multipart-body-parser", data: body } },
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
const contentType = headers?.["content-type"] ?? headers?.["Content-Type"];
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
30
|
+
if (!mimePattern.test(contentType)) {
|
|
31
|
+
if (options.disableContentTypeError) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
throw createError(415, "Unsupported Media Type", {
|
|
35
|
+
cause: {
|
|
36
|
+
package: "@middy/http-multipart-body-parser",
|
|
37
|
+
data: contentType,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
42
|
+
return parseMultipartData(request.event, options)
|
|
43
|
+
.then((multipartData) => {
|
|
44
|
+
// request.event.rawBody = body
|
|
45
|
+
request.event.body = multipartData;
|
|
46
|
+
})
|
|
47
|
+
.catch((err) => {
|
|
48
|
+
// UnprocessableEntity
|
|
49
|
+
throw createError(
|
|
50
|
+
415,
|
|
51
|
+
"Invalid or malformed multipart/form-data was provided",
|
|
52
|
+
{
|
|
53
|
+
cause: {
|
|
54
|
+
package: "@middy/http-multipart-body-parser",
|
|
55
|
+
data: body,
|
|
56
|
+
message: err.message,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
63
|
+
return {
|
|
64
|
+
before: httpMultipartBodyParserMiddlewareBefore,
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
67
|
|
|
68
68
|
const parseMultipartData = (event, options) => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
const multipartData = {};
|
|
70
|
+
const charset = event.isBase64Encoded ? "base64" : options.charset;
|
|
71
|
+
// header must be lowercase (content-type)
|
|
72
|
+
const busboy = BusBoy({
|
|
73
|
+
...options.busboy,
|
|
74
|
+
headers: {
|
|
75
|
+
"content-type":
|
|
76
|
+
event.headers?.["content-type"] ?? event.headers?.["Content-Type"],
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
busboy
|
|
82
|
+
.on("file", (fieldname, file, filename, encoding, mimetype) => {
|
|
83
|
+
const attachment = {
|
|
84
|
+
filename,
|
|
85
|
+
mimetype,
|
|
86
|
+
encoding,
|
|
87
|
+
};
|
|
88
88
|
|
|
89
|
-
|
|
89
|
+
const chunks = [];
|
|
90
90
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
91
|
+
file.on("data", (data) => {
|
|
92
|
+
chunks.push(data);
|
|
93
|
+
});
|
|
94
|
+
file.on("end", () => {
|
|
95
|
+
attachment.truncated = file.truncated;
|
|
96
|
+
attachment.content = Buffer.concat(chunks);
|
|
97
|
+
if (!multipartData[fieldname]) {
|
|
98
|
+
multipartData[fieldname] = attachment;
|
|
99
|
+
} else {
|
|
100
|
+
const current = multipartData[fieldname];
|
|
101
|
+
multipartData[fieldname] = [attachment].concat(current);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
})
|
|
105
|
+
.on("field", (fieldname, value) => {
|
|
106
|
+
const matches = fieldname.match(fieldnamePattern);
|
|
107
|
+
if (!matches) {
|
|
108
|
+
multipartData[fieldname] = value;
|
|
109
|
+
} else {
|
|
110
|
+
if (!multipartData[matches[1]]) {
|
|
111
|
+
multipartData[matches[1]] = [];
|
|
112
|
+
}
|
|
113
|
+
multipartData[matches[1]].push(value);
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
.on("finish", () => resolve(multipartData))
|
|
117
|
+
.on("error", (e) => reject(e));
|
|
118
118
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
export default httpMultipartBodyParserMiddleware
|
|
119
|
+
busboy.write(event.body, charset);
|
|
120
|
+
busboy.end();
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
export default httpMultipartBodyParserMiddleware;
|
package/package.json
CHANGED
|
@@ -1,74 +1,71 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"type-fest": "^4.0.0"
|
|
72
|
-
},
|
|
73
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/http-multipart-body-parser",
|
|
3
|
+
"version": "6.2.1",
|
|
4
|
+
"description": "Http event normalizer middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"HTTP",
|
|
43
|
+
"API",
|
|
44
|
+
"Multipart Body"
|
|
45
|
+
],
|
|
46
|
+
"author": {
|
|
47
|
+
"name": "Middy contributors",
|
|
48
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
53
|
+
"directory": "packages/http-multipart-body-parser"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://middy.js.org",
|
|
59
|
+
"funding": {
|
|
60
|
+
"type": "github",
|
|
61
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@fastify/busboy": "3.1.1"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@types/aws-lambda": "^8.10.101",
|
|
68
|
+
"type-fest": "^4.0.0"
|
|
69
|
+
},
|
|
70
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
74
71
|
}
|