Skip to content

Commit

Permalink
change payloadLimitErrorFn signature
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Oct 2, 2024
1 parent 6f0077f commit eafa122
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 15 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ export const hasBody = (method: string) => ['POST', 'PUT', 'PATCH', 'DELETE'].in

const defaultPayloadLimit = 104857600 // 100KB

export type LimitErrorFn = (payloadLimit: number) => string
export type LimitErrorFn = (payloadLimit: number) => Error

export type ParserOptions = Partial<{
payloadLimit: number
errorFn: LimitErrorFn
payloadLimitErrorFn: LimitErrorFn
}>

const defaultErrorFn: LimitErrorFn = (payloadLimit) => `Payload too large. Limit: ${payloadLimit} bytes`
const defaultErrorFn: LimitErrorFn = (payloadLimit) => new Error(`Payload too large. Limit: ${payloadLimit} bytes`)

// Main function
export const p =
<T = any>(fn: (body: any) => any, payloadLimit = defaultPayloadLimit, errorFn: LimitErrorFn = defaultErrorFn) =>
<T = any>(fn: (body: any) => any, payloadLimit = defaultPayloadLimit, payloadLimitErrorFn: LimitErrorFn = defaultErrorFn) =>
async (req: ReqWithBody<T>, _res: Response, next: (err?: any) => void) => {
try {
let body = ''

for await (const chunk of req) {
if (body.length > payloadLimit) throw new Error(errorFn(payloadLimit))
if (body.length > payloadLimit) throw payloadLimitErrorFn(payloadLimit)
body += chunk
}

Expand All @@ -47,31 +47,31 @@ const custom =
}

const json =
({ payloadLimit, errorFn }: ParserOptions = {}) =>
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
async (req: ReqWithBody, res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p((x) => (x ? JSON.parse(x.toString()) : {}), payloadLimit, errorFn)(req, res, next)
req.body = await p((x) => (x ? JSON.parse(x.toString()) : {}), payloadLimit, payloadLimitErrorFn)(req, res, next)
} else next()
}

const raw =
({ payloadLimit, errorFn }: ParserOptions = {}) =>
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p((x) => x, payloadLimit, errorFn)(req, _res, next)
req.body = await p((x) => x, payloadLimit, payloadLimitErrorFn)(req, _res, next)
} else next()
}

const text =
({ payloadLimit, errorFn }: ParserOptions = {}) =>
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p((x) => x.toString(), payloadLimit, errorFn)(req, _res, next)
req.body = await p((x) => x.toString(), payloadLimit, payloadLimitErrorFn)(req, _res, next)
} else next()
}

const urlencoded =
({ payloadLimit, errorFn }: ParserOptions = {}) =>
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p(
Expand All @@ -80,7 +80,7 @@ const urlencoded =
return Object.fromEntries(urlSearchParam.entries())
},
payloadLimit,
errorFn
payloadLimitErrorFn
)(req, _res, next)
} else next()
}
Expand Down Expand Up @@ -129,13 +129,13 @@ type MultipartOptions = Partial<{
}>

const multipart =
({ payloadLimit, errorFn, ...opts }: MultipartOptions & ParserOptions = {}) =>
({ payloadLimit, payloadLimitErrorFn, ...opts }: MultipartOptions & ParserOptions = {}) =>
async (req: ReqWithBody, res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p((x) => {
const boundary = getBoundary(req.headers['content-type']!)
if (boundary) return parseMultipart(x, boundary, opts)
}, payloadLimit, errorFn)(req, res, next)
}, payloadLimit, payloadLimitErrorFn)(req, res, next)
next()
} else next()
}
Expand Down
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ test('should throw on custom payloadLimit', async () => {

test('should throw on payloadLimit with custom error message', async () => {
const server = createServer(async (req: ReqWithBody, res) => {
await text({ payloadLimit: 1024, errorFn: (payloadLimit) => `Payload too large. Limit: ${payloadLimit / 1024}KB` })(req, res, (err) => {
await text({ payloadLimit: 1024, payloadLimitErrorFn: (payloadLimit) => new Error(`Payload too large. Limit: ${payloadLimit / 1024}KB`) })(req, res, (err) => {
if (err) res.writeHead(413).end(err.message)
else res.end(req.body)
})
Expand Down

0 comments on commit eafa122

Please sign in to comment.