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

Expose busboy options, update docs and tests. #34

Merged
merged 4 commits into from
Apr 5, 2018
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ jspm_packages

# Optional REPL history
.node_repl_history

# Ignore package-lock
package-lock.json
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,39 @@ fastify.listen(3000, err => {
})
```

You can also pass optional arguments to busboy when registering with fastify. This is useful for setting limits on the content that can be uploaded. A full list of available options can be found in the [busboy documentation](https://github.com/mscdex/busboy#busboy-methods).

```js
fastify.register(require('fastify-multipart'), {
limits: {
fieldNameSize: 100, // Max field name size in bytes
fieldSize: 1000000, // Max field value size in bytes
fields: 10, // Max number of non-file fields
fileSize: 100, // For multipart forms, the max file size
files: 1, // Max number of file fields
headerPairs: 2000 // Max number of header key=>value pairs
}
});
```

If you do set upload limits, be sure to listen for limit events in the handler method. An error or exception will not occur if a limit is reached, but rather the stream will be truncated. These events are documented in more detail [here](https://github.com/mscdex/busboy#busboy-special-events).

```js
function handler (field, file, filename, encoding, mimetype) {

file.on('limit', () => console.log('File size limit reached'));

file.on('partsLimit', () => console.log('Maximum number of form parts reached'));

file.on('filesLimit', () => console.log('Maximum number of files reached'));

file.on('fieldsLimit', () => console.log('Maximim number of fields reached'));

}
```



## Acknowledgements

This project is kindly sponsored by:
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ function fastifyMultipart (fastify, options, done) {

const req = this.req

const stream = new Busboy({ headers: req.headers })
const busboyOptions = { headers: req.headers }
const keys = Object.keys(options)
for (var i = 0; i < keys.length; i++) {
busboyOptions[keys[i]] = options[keys[i]]
}
const stream = new Busboy(busboyOptions)

req.on('error', function (err) {
stream.destroy()
Expand Down
8 changes: 6 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ const crypto = require('crypto')
const filePath = path.join(__dirname, 'README.md')

test('should parse forms', function (t) {
t.plan(12)
t.plan(14)

const fastify = Fastify()
t.tearDown(fastify.close.bind(fastify))

fastify.register(multipart)
fastify.register(multipart, {limits: {fields: 1}})

fastify.post('/', function (req, reply) {
t.ok(req.isMultipart())
Expand All @@ -32,6 +32,8 @@ test('should parse forms', function (t) {
})

mp.on('field', function (name, value) {
t.notEqual(name, 'willbe', 'Busboy fields limit ignored')
t.notEqual(value, 'dropped', 'Busboy fields limit ignored')
t.equal(name, 'hello')
t.equal(value, 'world')
})
Expand All @@ -41,6 +43,7 @@ test('should parse forms', function (t) {
t.equal(field, 'upload')
t.equal(encoding, '7bit')
t.equal(mimetype, 'text/markdown')
file.on('fieldsLimit', () => t.ok('field limit reached'))
var original = fs.readFileSync(filePath, 'utf8')
file.pipe(concat(function (buf) {
t.equal(buf.toString(), original)
Expand Down Expand Up @@ -70,6 +73,7 @@ test('should parse forms', function (t) {
var rs = fs.createReadStream(filePath)
form.append('upload', rs)
form.append('hello', 'world')
form.append('willbe', 'dropped')
pump(form, req, function (err) {
t.error(err, 'client pump: no err')
})
Expand Down