Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function fastifyMultipart (fastify, options, done) {
return
}

for await (const part of req.parts()) {
for await (const part of req.parts(req.routeOptions.config.multipartOptions)) {
req.body = part.fields

if (part.file) {
Expand Down
124 changes: 124 additions & 0 deletions 124 test/multipart-fileLimit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}, async function (req, reply) {
t.fail('it should throw')

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')
}
})
4 changes: 4 additions & 0 deletions 4 types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ declare module 'fastify' {
interface FastifyInstance {
multipartErrors: MultipartErrors;
}

interface FastifyContextConfig {
multipartOptions?: Omit<BusboyConfig, 'headers'>
}
}

type FastifyMultipartPlugin = FastifyPluginCallback<
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.