Skip to content

Commit b362783

Browse files
committed
fix fmt
1 parent c5a5099 commit b362783

File tree

2 files changed

+61
-62
lines changed

2 files changed

+61
-62
lines changed

src/index.ts

+61-61
Original file line numberDiff line numberDiff line change
@@ -37,63 +37,63 @@ export const p =
3737
payloadLimit = defaultPayloadLimit,
3838
payloadLimitErrorFn: LimitErrorFn = defaultErrorFn
3939
) =>
40-
async (req: ReqWithBody<T>, _res: Response, next: (err?: any) => void) => {
41-
try {
42-
const body: Buffer[] = []
43-
44-
for await (const chunk of req) {
45-
const totalSize = body.reduce((total, buffer) => total + buffer.byteLength, 0)
46-
if (totalSize > payloadLimit) throw payloadLimitErrorFn(payloadLimit)
47-
body.push(chunk as Buffer)
48-
}
49-
50-
return fn(Buffer.concat(body))
51-
} catch (e) {
52-
next(e)
40+
async (req: ReqWithBody<T>, _res: Response, next: (err?: any) => void) => {
41+
try {
42+
const body: Buffer[] = []
43+
44+
for await (const chunk of req) {
45+
const totalSize = body.reduce((total, buffer) => total + buffer.byteLength, 0)
46+
if (totalSize > payloadLimit) throw payloadLimitErrorFn(payloadLimit)
47+
body.push(chunk as Buffer)
5348
}
49+
50+
return fn(Buffer.concat(body))
51+
} catch (e) {
52+
next(e)
5453
}
54+
}
5555

5656
/**
5757
* Parse payload with a custom function
5858
* @param fn
5959
*/
6060
const custom =
6161
<T = any>(fn: (body: Buffer) => any) =>
62-
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
63-
if (hasBody(req.method!)) req.body = await p<T>(fn)(req, _res, next)
64-
next()
65-
}
62+
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
63+
if (hasBody(req.method!)) req.body = await p<T>(fn)(req, _res, next)
64+
next()
65+
}
6666

6767
/**
6868
* Parse JSON payload
6969
* @param options
7070
*/
7171
const json =
7272
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
73-
async (req: ReqWithBody, res: Response, next: NextFunction) => {
74-
if (hasBody(req.method!)) {
75-
req.body = await p(
76-
(x) => {
77-
const str = td.decode(x)
78-
return str ? JSON.parse(str) : {}
79-
},
80-
payloadLimit,
81-
payloadLimitErrorFn
82-
)(req, res, next)
83-
} else next()
84-
}
73+
async (req: ReqWithBody, res: Response, next: NextFunction) => {
74+
if (hasBody(req.method!)) {
75+
req.body = await p(
76+
(x) => {
77+
const str = td.decode(x)
78+
return str ? JSON.parse(str) : {}
79+
},
80+
payloadLimit,
81+
payloadLimitErrorFn
82+
)(req, res, next)
83+
} else next()
84+
}
8585

8686
/**
8787
* Parse raw payload
8888
* @param options
8989
*/
9090
const raw =
9191
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
92-
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
93-
if (hasBody(req.method!)) {
94-
req.body = await p((x) => x, payloadLimit, payloadLimitErrorFn)(req, _res, next)
95-
} else next()
96-
}
92+
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
93+
if (hasBody(req.method!)) {
94+
req.body = await p((x) => x, payloadLimit, payloadLimitErrorFn)(req, _res, next)
95+
} else next()
96+
}
9797

9898
const td = new TextDecoder()
9999
/**
@@ -103,27 +103,27 @@ const td = new TextDecoder()
103103
*/
104104
const text =
105105
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
106-
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
107-
if (hasBody(req.method!)) {
108-
req.body = await p((x) => td.decode(x), payloadLimit, payloadLimitErrorFn)(req, _res, next)
109-
} else next()
110-
}
106+
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
107+
if (hasBody(req.method!)) {
108+
req.body = await p((x) => td.decode(x), payloadLimit, payloadLimitErrorFn)(req, _res, next)
109+
} else next()
110+
}
111111

112112
/**
113113
* Parse urlencoded payload
114114
* @param options
115115
*/
116116
const urlencoded =
117117
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
118-
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
119-
if (hasBody(req.method!)) {
120-
req.body = await p(
121-
(x) => Object.fromEntries(new URLSearchParams(x.toString()).entries()),
122-
payloadLimit,
123-
payloadLimitErrorFn
124-
)(req, _res, next)
125-
} else next()
126-
}
118+
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
119+
if (hasBody(req.method!)) {
120+
req.body = await p(
121+
(x) => Object.fromEntries(new URLSearchParams(x.toString()).entries()),
122+
payloadLimit,
123+
payloadLimitErrorFn
124+
)(req, _res, next)
125+
} else next()
126+
}
127127

128128
const getBoundary = (contentType: string) => {
129129
const match = /boundary=(.+);?/.exec(contentType)
@@ -195,18 +195,18 @@ type MultipartOptions = Partial<{
195195
*/
196196
const multipart =
197197
({ payloadLimit = Number.POSITIVE_INFINITY, payloadLimitErrorFn, ...opts }: MultipartOptions & ParserOptions = {}) =>
198-
async (req: ReqWithBody, res: Response, next: NextFunction) => {
199-
if (hasBody(req.method!)) {
200-
req.body = await p(
201-
(x) => {
202-
const boundary = getBoundary(req.headers['content-type']!)
203-
if (boundary) return parseMultipart(td.decode(x), boundary, opts)
204-
return {}
205-
},
206-
payloadLimit,
207-
payloadLimitErrorFn
208-
)(req, res, next)
209-
} else next()
210-
}
198+
async (req: ReqWithBody, res: Response, next: NextFunction) => {
199+
if (hasBody(req.method!)) {
200+
req.body = await p(
201+
(x) => {
202+
const boundary = getBoundary(req.headers['content-type']!)
203+
if (boundary) return parseMultipart(td.decode(x), boundary, opts)
204+
return {}
205+
},
206+
payloadLimit,
207+
payloadLimitErrorFn
208+
)(req, res, next)
209+
} else next()
210+
}
211211

212212
export { custom, json, raw, text, urlencoded, multipart }

test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,6 @@ describe('Multipart', () => {
409409
const server = createServer(async (req: ReqWithBody<{ file: [File] }>, res) => {
410410
await multipart()(req, res, (err) => err && console.log(err))
411411

412-
413412
const formBuf = new Uint8Array(await file.arrayBuffer())
414413
const buf = new Uint8Array(await req.body!.file[0].arrayBuffer())
415414

0 commit comments

Comments
 (0)