Skip to content

Commit 96a7d9a

Browse files
committed
fmt
1 parent 488bd48 commit 96a7d9a

File tree

3 files changed

+68
-62
lines changed

3 files changed

+68
-62
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"test:coverage": "c8 --include=src pnpm test",
3838
"test:report": "c8 report --reporter=text-lcov > coverage.lcov",
3939
"build": "tsc -p tsconfig.build.json",
40-
"prepublishOnly": "pnpm build && pnpm test"
40+
"prepublishOnly": "pnpm build && pnpm test",
41+
"check": "biome check --write"
4142
},
4243
"packageManager": "pnpm@9.4.0",
4344
"publishConfig": {

src/index.ts

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,63 +37,65 @@ 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-
} next?.()
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)
8483
}
84+
next?.()
85+
}
8586

8687
/**
8788
* Parse raw payload
8889
* @param options
8990
*/
9091
const raw =
9192
({ 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-
} next?.()
93+
async (req: ReqWithBody, _res: Response, next?: NextFunction) => {
94+
if (hasBody(req.method!)) {
95+
req.body = await p((x) => x, payloadLimit, payloadLimitErrorFn)(req, _res, next)
9696
}
97+
next?.()
98+
}
9799

98100
const td = new TextDecoder()
99101
/**
@@ -103,27 +105,29 @@ const td = new TextDecoder()
103105
*/
104106
const text =
105107
({ 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-
} next?.()
108+
async (req: ReqWithBody, _res: Response, next?: NextFunction) => {
109+
if (hasBody(req.method!)) {
110+
req.body = await p((x) => td.decode(x), payloadLimit, payloadLimitErrorFn)(req, _res, next)
110111
}
112+
next?.()
113+
}
111114

112115
/**
113116
* Parse urlencoded payload
114117
* @param options
115118
*/
116119
const urlencoded =
117120
({ 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-
} next?.()
121+
async (req: ReqWithBody, _res: Response, next?: NextFunction) => {
122+
if (hasBody(req.method!)) {
123+
req.body = await p(
124+
(x) => Object.fromEntries(new URLSearchParams(x.toString()).entries()),
125+
payloadLimit,
126+
payloadLimitErrorFn
127+
)(req, _res, next)
126128
}
129+
next?.()
130+
}
127131

128132
const getBoundary = (contentType: string) => {
129133
const match = /boundary=(.+);?/.exec(contentType)
@@ -195,18 +199,19 @@ type MultipartOptions = Partial<{
195199
*/
196200
const multipart =
197201
({ 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-
} next?.()
202+
async (req: ReqWithBody, res: Response, next?: NextFunction) => {
203+
if (hasBody(req.method!)) {
204+
req.body = await p(
205+
(x) => {
206+
const boundary = getBoundary(req.headers['content-type']!)
207+
if (boundary) return parseMultipart(td.decode(x), boundary, opts)
208+
return {}
209+
},
210+
payloadLimit,
211+
payloadLimitErrorFn
212+
)(req, res, next)
210213
}
214+
next?.()
215+
}
211216

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

test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,14 +560,14 @@ describe('Framework integration', { timeout: 500 }, () => {
560560

561561
await fetch('/json', {
562562
body: JSON.stringify({ hello: 'world' }),
563-
method: 'POST',
564-
})
565-
.expect(200, { hello: 'world' })
563+
method: 'POST'
564+
}).expect(200, { hello: 'world' })
566565

567566
await fetch('/url', {
568567
body: 'hello=world',
569568
method: 'POST'
570569
})
571-
.expect(200, { hello: 'world' }).then(() => server.close())
570+
.expect(200, { hello: 'world' })
571+
.then(() => server.close())
572572
})
573-
})
573+
})

0 commit comments

Comments
 (0)