fastify-multipart: fileSize limit on fastify-multipart did not throw error
I don’t know if I use it correctly or not but it is just pass through if I passing file with size higher than limit.
/*
* other code
*/
fastify.post('/upload', async function (req, reply) {
const data = await req.file({
limits: {
fileSize: 1 * 1024 * 1024, // 1mb limit
}
})
await pump(data.file, fs.createWriteStream(data.filename))
reply.send()
})
It write file to my directory, when I expect it to throw error
- node version: 14.15.3
- fastify version: 3.9.2
- fastify multipart version: 3.3.1
- os: WSL
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 20 (14 by maintainers)
I guess we should error this.
I think the current behavior is correct because we can only guarantee error handling for a stream when we iterate over all
parts
untilonEnd
is called. In all other cases, the user can check forfile.truncated
after the stream was consumed. Thereq.file
API is error-prone because it can’t report the final error.I couldn’t reproduce it. It is only valid for
req.file
. Please provide a reproducible example.I think I found the issue.
await file()
returns the file immediately but the error is pushed asynchronously to the queue and it will never be consumed. We have to ensure internally that all events are consumed.It works for
req.saveRequestFiles
due to that exact reason because all files are consumed and it is guaranteed that the error is consumed as well.For now, throwing error is the simplest solution. We can introduce an option
throwFileLimit
to enable throwing behavior and which the default isfalse
. If we manage to bump an major, change it to defaulttrue
.