Skip to content

Commit aba7e16

Browse files
committed
fix tests
1 parent 7b187d9 commit aba7e16

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type ReqWithBody<T = any> = IncomingMessage & {
1212

1313
export const hasBody = (method: string) => ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method)
1414

15-
const defaultPayloadLimit = 104857600 // 100KB
15+
const defaultPayloadLimit = 102400 // 100KB
1616

1717
export type LimitErrorFn = (limit: number) => Error
1818

@@ -132,10 +132,16 @@ const getBoundary = (contentType: string) => {
132132

133133
const defaultFileSizeLimitErrorFn: LimitErrorFn = (limit) => new Error(`File too large. Limit: ${limit} bytes`)
134134

135+
const defaultFileSizeLimit = 200 * 1024 * 1024
136+
135137
const parseMultipart = (
136138
body: string,
137139
boundary: string,
138-
{ fileCountLimit, fileSizeLimit, fileSizeLimitErrorFn = defaultFileSizeLimitErrorFn }: MultipartOptions
140+
{
141+
fileCountLimit,
142+
fileSizeLimit = defaultFileSizeLimit,
143+
fileSizeLimitErrorFn = defaultFileSizeLimitErrorFn
144+
}: MultipartOptions
139145
) => {
140146
const parts = body.split(new RegExp(`${boundary}(--)?`)).filter((part) => !!part && /content-disposition/i.test(part))
141147
const parsedBody: Record<string, (File | string)[]> = {}
@@ -147,7 +153,7 @@ const parseMultipart = (
147153
const [headers, ...lines] = part.split('\r\n').filter((part) => !!part)
148154
const data = lines.join('\r\n').trim()
149155

150-
if (fileSizeLimit && data.length > fileSizeLimit) throw fileSizeLimitErrorFn(fileSizeLimit)
156+
if (data.length > fileSizeLimit) throw fileSizeLimitErrorFn(fileSizeLimit)
151157

152158
// Extract the name and filename from the headers
153159
const name = /name="(.+?)"/.exec(headers)![1]
@@ -184,7 +190,7 @@ type MultipartOptions = Partial<{
184190
/**
185191
* Parse multipart form data (supports files as well)
186192
*
187-
* Does not restrict total payload size by default
193+
* Does not restrict total payload size by default.
188194
* @param options
189195
*/
190196
const multipart =
@@ -200,7 +206,6 @@ const multipart =
200206
payloadLimit,
201207
payloadLimitErrorFn
202208
)(req, res, next)
203-
next()
204209
} else next()
205210
}
206211

test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ describe('Limits', () => {
431431
Accept: 'text/plain',
432432
'Content-Type': 'text/plain'
433433
}
434-
}).expect(413, 'Payload too large. Limit: 104857600 bytes')
434+
}).expect(413, 'Payload too large. Limit: 102400 bytes')
435435
})
436436

437437
it('should throw on custom payloadLimit', async () => {
@@ -513,21 +513,21 @@ describe('Limits', () => {
513513
it('should throw multipart if exceeds allowed file size with a custom error', async () => {
514514
const server = createServer(async (req: ReqWithBody, res) => {
515515
await multipart({
516-
fileSizeLimit: 10,
516+
fileSizeLimit: 20,
517517
fileSizeLimitErrorFn: (limit) => new Error(`File too large. Limit: ${limit / 1024}KB`)
518518
})(req, res, (err) => {
519519
if (err) res.writeHead(413).end(err.message)
520-
else res.end(req.body)
520+
else res.end('ok')
521521
})
522522
})
523523

524524
const fd = new FormData()
525525

526-
fd.set('file', new File(['hello world'], 'hello.txt', { type: 'text/plain' }))
526+
fd.set('file', new File(['hello world to everyone'], 'hello.txt', { type: 'text/plain' }))
527527

528528
await makeFetch(server)('/', {
529529
body: fd,
530530
method: 'POST'
531-
}).expect(413, 'File too large. Limit: 0.009765625KB')
531+
}).expect(413, 'File too large. Limit: 0.01953125KB')
532532
})
533533
})

0 commit comments

Comments
 (0)