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