Skip to content
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

add parts type and enable coverage checking #491

Merged
merged 6 commits into from
Oct 23, 2023
Merged
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
1 change: 0 additions & 1 deletion .taprc
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
check-coverage: false
files:
- test/**/*.test.js
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ function fastifyMultipart (fastify, options, done) {
const key = reqBodyKeys[i]
const field = req.body[key]

/* Don't modify the body if a field doesn't have a value or an attached buffer */
/* istanbul ignore else */
Fdawgs marked this conversation as resolved.
Show resolved Hide resolved
if (field.value !== undefined) {
body[key] = field.value
} else if (field._buf) {
Expand Down Expand Up @@ -453,7 +455,8 @@ function fastifyMultipart (fastify, options, done) {
try {
await unlink(filepath)
} catch (error) {
this.log.error(error, 'could not delete file')
/* istanbul ignore next */
this.log.error(error, 'Could not delete file')
}
}
}
Expand All @@ -462,6 +465,8 @@ function fastifyMultipart (fastify, options, done) {
const parts = this[kMultipartHandler](options)
let part
while ((part = await parts()) != null) {
/* Only return a part if the file property exists */
/* istanbul ignore else */
if (part.file) {
return part
}
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ declare namespace fastifyMultipart {
* Max number of header key=>value pairs
*/
headerPairs?: number;

/**
* For multipart forms, the max number of parts (fields + files)
* @default 1000
*/
parts?: number;
};
}

Expand Down
5 changes: 4 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const runServer = async () => {

app.register(fastifyMultipart, {
attachFieldsToBody: true,
limits: {
parts: 500
},
onFile: (part: MultipartFile) => {
console.log(part)
}
Expand Down Expand Up @@ -73,7 +76,7 @@ const runServer = async () => {

// busboy
app.post('/', async function (req, reply) {
const options: Partial<BusboyConfig> = { limits: { fileSize: 1000 } }
const options: Partial<BusboyConfig> = { limits: { fileSize: 1000, parts: 500 } }
const data = await req.file(options)
if (!data) throw new Error('missing file')
await pump(data.file, fs.createWriteStream(data.filename))
Expand Down