-
-
Notifications
You must be signed in to change notification settings - Fork 116
feat: add per route busboy configuration #580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
0e5c11d
9fa4a21
6b07a88
5f3c511
8198666
2c398c4
b8f7038
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -316,3 +316,127 @@ test('should NOT throw fileSize limitation error when throwFileSizeLimit is glob | |
| t.error(error, 'request') | ||
| } | ||
| }) | ||
|
|
||
| test('should throw fileSize limitation error when used alongside attachFieldsToBody and set request config', async function (t) { | ||
| t.plan(1) | ||
|
|
||
| const fastify = Fastify() | ||
| t.teardown(fastify.close.bind(fastify)) | ||
|
|
||
| fastify.register(multipart, { | ||
| attachFieldsToBody: true | ||
| }) | ||
|
|
||
| const randomFileBuffer = Buffer.alloc(2_000_000) | ||
| crypto.randomFillSync(randomFileBuffer) | ||
|
|
||
| fastify.post('/', { | ||
| config: { | ||
| multipartOptions: { | ||
| limits: { | ||
| fileSize: 1_000_000 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The file size limitation error isn’t caused by this configuration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @is2ei There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you share how you verified it locally? diff --git a/test/multipart-fileLimit.test.js b/test/multipart-fileLimit.test.js
index 6aed6a5..d6d4462 100644
--- a/test/multipart-fileLimit.test.js
+++ b/test/multipart-fileLimit.test.js
@@ -331,13 +331,13 @@ test('should throw fileSize limitation error when used alongside attachFieldsToB
crypto.randomFillSync(randomFileBuffer)
fastify.post('/', {
- config: {
- multipartOptions: {
- limits: {
- fileSize: 1_000_000
- }
- }
- }
+ // config: {
+ // multipartOptions: {
+ // limits: {
+ // fileSize: 1_000_000
+ // }
+ // }
+ // }
}, async function (req, reply) {
t.assert.fail('it should throw')There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @is2ei There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If necessary, I can change the values to make it clearer what is causing the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test should be updated so that the error is caused by the config, as the message states:
|
||
| } | ||
| } | ||
| } | ||
| }, async function (req, reply) { | ||
| t.fail('it should throw') | ||
is2ei marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| reply.status(200).send() | ||
| }) | ||
|
|
||
| await fastify.listen({ port: 0 }) | ||
|
|
||
| // request | ||
| const form = new FormData() | ||
| const opts = { | ||
| hostname: '127.0.0.1', | ||
| port: fastify.server.address().port, | ||
| path: '/', | ||
| headers: form.getHeaders(), | ||
| method: 'POST' | ||
| } | ||
|
|
||
| const tmpFile = 'test/random-file' | ||
| fs.writeFileSync(tmpFile, randomFileBuffer) | ||
|
|
||
| const req = http.request(opts) | ||
| form.append('upload', fs.createReadStream(tmpFile)) | ||
|
|
||
| form.pipe(req) | ||
|
|
||
| try { | ||
| const [res] = await once(req, 'response') | ||
| t.equal(res.statusCode, 413) | ||
| res.resume() | ||
| await once(res, 'end') | ||
|
|
||
| fs.unlinkSync(tmpFile) | ||
| } catch (error) { | ||
| t.error(error, 'request') | ||
| } | ||
| }) | ||
|
|
||
| test('should not throw fileSize limitation error when used alongside attachFieldsToBody and set request config', async function (t) { | ||
| t.plan(4) | ||
|
|
||
| const fastify = Fastify() | ||
| t.teardown(fastify.close.bind(fastify)) | ||
|
|
||
| fastify.register(multipart, { | ||
| attachFieldsToBody: true | ||
| }) | ||
|
|
||
| const randomFileBuffer = Buffer.alloc(900_000) | ||
| crypto.randomFillSync(randomFileBuffer) | ||
|
|
||
| fastify.post('/', { | ||
| config: { | ||
| multipartOptions: { | ||
| limits: { | ||
| fileSize: 1_000_000 | ||
| } | ||
| } | ||
| } | ||
| }, async function (req, reply) { | ||
| t.ok(req.isMultipart()) | ||
|
|
||
| t.same(Object.keys(req.body), ['upload']) | ||
|
|
||
| const content = await req.body.upload.toBuffer() | ||
|
|
||
| t.equal(content.toString(), randomFileBuffer.toString()) | ||
|
|
||
| reply.status(200).send() | ||
| }) | ||
|
|
||
| await fastify.listen({ port: 0 }) | ||
|
|
||
| // request | ||
| const form = new FormData() | ||
| const opts = { | ||
| hostname: '127.0.0.1', | ||
| port: fastify.server.address().port, | ||
| path: '/', | ||
| headers: form.getHeaders(), | ||
| method: 'POST' | ||
| } | ||
|
|
||
| const tmpFile = 'test/random-file' | ||
| fs.writeFileSync(tmpFile, randomFileBuffer) | ||
|
|
||
| const req = http.request(opts) | ||
| form.append('upload', fs.createReadStream(tmpFile)) | ||
|
|
||
| form.pipe(req) | ||
|
|
||
| try { | ||
| const [res] = await once(req, 'response') | ||
| t.equal(res.statusCode, 200) | ||
| res.resume() | ||
| await once(res, 'end') | ||
|
|
||
| fs.unlinkSync(tmpFile) | ||
| } catch (error) { | ||
| t.error(error, 'request') | ||
| } | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,10 @@ declare module 'fastify' { | |
| interface FastifyInstance { | ||
| multipartErrors: MultipartErrors; | ||
| } | ||
|
|
||
| interface FastifyContextConfig { | ||
| multipartOptions?: Omit<BusboyConfig, 'headers'> | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test for this type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| } | ||
|
|
||
| type FastifyMultipartPlugin = FastifyPluginCallback< | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.