Skip to content

Commit

Permalink
fix fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Oct 3, 2024
1 parent c5a5099 commit b362783
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 62 deletions.
122 changes: 61 additions & 61 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,63 +37,63 @@ export const p =
payloadLimit = defaultPayloadLimit,
payloadLimitErrorFn: LimitErrorFn = defaultErrorFn
) =>
async (req: ReqWithBody<T>, _res: Response, next: (err?: any) => void) => {
try {
const body: Buffer[] = []

for await (const chunk of req) {
const totalSize = body.reduce((total, buffer) => total + buffer.byteLength, 0)
if (totalSize > payloadLimit) throw payloadLimitErrorFn(payloadLimit)
body.push(chunk as Buffer)
}

return fn(Buffer.concat(body))
} catch (e) {
next(e)
async (req: ReqWithBody<T>, _res: Response, next: (err?: any) => void) => {
try {
const body: Buffer[] = []

for await (const chunk of req) {
const totalSize = body.reduce((total, buffer) => total + buffer.byteLength, 0)
if (totalSize > payloadLimit) throw payloadLimitErrorFn(payloadLimit)
body.push(chunk as Buffer)
}

return fn(Buffer.concat(body))
} catch (e) {
next(e)
}
}

/**
* Parse payload with a custom function
* @param fn
*/
const custom =
<T = any>(fn: (body: Buffer) => any) =>
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) req.body = await p<T>(fn)(req, _res, next)
next()
}
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) req.body = await p<T>(fn)(req, _res, next)
next()
}

/**
* Parse JSON payload
* @param options
*/
const json =
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
async (req: ReqWithBody, res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p(
(x) => {
const str = td.decode(x)
return str ? JSON.parse(str) : {}
},
payloadLimit,
payloadLimitErrorFn
)(req, res, next)
} else next()
}
async (req: ReqWithBody, res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p(
(x) => {
const str = td.decode(x)
return str ? JSON.parse(str) : {}
},
payloadLimit,
payloadLimitErrorFn
)(req, res, next)
} else next()
}

/**
* Parse raw payload
* @param options
*/
const raw =
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p((x) => x, payloadLimit, payloadLimitErrorFn)(req, _res, next)
} else next()
}
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p((x) => x, payloadLimit, payloadLimitErrorFn)(req, _res, next)
} else next()
}

const td = new TextDecoder()
/**
Expand All @@ -103,27 +103,27 @@ const td = new TextDecoder()
*/
const text =
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p((x) => td.decode(x), payloadLimit, payloadLimitErrorFn)(req, _res, next)
} else next()
}
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p((x) => td.decode(x), payloadLimit, payloadLimitErrorFn)(req, _res, next)
} else next()
}

/**
* Parse urlencoded payload
* @param options
*/
const urlencoded =
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p(
(x) => Object.fromEntries(new URLSearchParams(x.toString()).entries()),
payloadLimit,
payloadLimitErrorFn
)(req, _res, next)
} else next()
}
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
if (hasBody(req.method!)) {
req.body = await p(
(x) => Object.fromEntries(new URLSearchParams(x.toString()).entries()),
payloadLimit,
payloadLimitErrorFn
)(req, _res, next)
} else next()
}

const getBoundary = (contentType: string) => {
const match = /boundary=(.+);?/.exec(contentType)
Expand Down Expand Up @@ -195,18 +195,18 @@ type MultipartOptions = Partial<{
*/
const multipart =
({ payloadLimit = Number.POSITIVE_INFINITY, 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(td.decode(x), boundary, opts)
return {}
},
payloadLimit,
payloadLimitErrorFn
)(req, res, next)
} else next()
}
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(td.decode(x), boundary, opts)
return {}
},
payloadLimit,
payloadLimitErrorFn
)(req, res, next)
} else next()
}

export { custom, json, raw, text, urlencoded, multipart }
1 change: 0 additions & 1 deletion test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ describe('Multipart', () => {
const server = createServer(async (req: ReqWithBody<{ file: [File] }>, res) => {
await multipart()(req, res, (err) => err && console.log(err))


const formBuf = new Uint8Array(await file.arrayBuffer())
const buf = new Uint8Array(await req.body!.file[0].arrayBuffer())

Expand Down

0 comments on commit b362783

Please sign in to comment.