Skip to content

Commit

Permalink
rename to prevent confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Oct 2, 2024
1 parent be0fb67 commit 6f0077f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 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 = (limit: number) => string
export type LimitErrorFn = (payloadLimit: number) => string

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

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

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

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

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

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

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

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

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

const multipart =
({ limit, errorFn, ...opts }: MultipartOptions & ParserOptions = {}) =>
({ payloadLimit, errorFn, ...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)
}, limit, errorFn)(req, res, next)
}, payloadLimit, errorFn)(req, res, next)
next()
} else next()
}
Expand Down
10 changes: 5 additions & 5 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ test('should support multiple files', async () => {
}).expect(200)
})

test('should throw on default limit', async () => {
test('should throw on default payloadLimit', async () => {
const server = createServer(async (req: ReqWithBody, res) => {
await text()(req, res, (err) => {
if (err) res.writeHead(413).end(err.message)
Expand All @@ -405,9 +405,9 @@ test('should throw on default limit', async () => {
}).expect(413, 'Payload too large. Limit: 104857600 bytes')
})

test('should throw on custom limit', async () => {
test('should throw on custom payloadLimit', async () => {
const server = createServer(async (req: ReqWithBody, res) => {
await text({ limit: 1024 })(req, res, (err) => {
await text({ payloadLimit: 1024 })(req, res, (err) => {
if (err) res.writeHead(413).end(err.message)
else res.end(req.body)
})
Expand All @@ -423,9 +423,9 @@ test('should throw on custom limit', async () => {
}).expect(413, 'Payload too large. Limit: 1024 bytes')
})

test('should throw on limit with custom error message', async () => {
test('should throw on payloadLimit with custom error message', async () => {
const server = createServer(async (req: ReqWithBody, res) => {
await text({ limit: 1024, errorFn: (limit) => `Payload too large. Limit: ${limit / 1024}KB` })(req, res, (err) => {
await text({ payloadLimit: 1024, errorFn: (payloadLimit) => `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 6f0077f

Please sign in to comment.