Skip to content
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
26 changes: 0 additions & 26 deletions .eslintrc.js

This file was deleted.

28 changes: 0 additions & 28 deletions CHANGELOG.md

This file was deleted.

76 changes: 31 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
# busboy

<div align="center">

[![Build Status](https://github.com/fastify/busboy/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/busboy/actions)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![Security Responsible Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/fastify/.github/blob/main/SECURITY.md)

</div>

<div align="center">

[![NPM version](https://img.shields.io/npm/v/@fastify/busboy.svg?style=flat)](https://www.npmjs.com/package/@fastify/busboy)
[![NPM downloads](https://img.shields.io/npm/dm/@fastify/busboy.svg?style=flat)](https://www.npmjs.com/package/@fastify/busboy)

</div>
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
[![Security Responsible Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/fastify/.github/blob/main/SECURITY.md)

Description
===========
Expand All @@ -37,15 +27,13 @@ Requirements

* [Node.js](http://nodejs.org/) 10+


Install
=======

```sh
npm i @fastify/busboy
```


Examples
========

Expand Down Expand Up @@ -189,7 +177,6 @@ http.createServer(function(req, res) {
// Done parsing form!
```


API
===

Expand All @@ -199,9 +186,9 @@ Busboy (special) events
-----------------------

* **file**(< _string_ >fieldname, < _ReadableStream_ >stream, < _string_ >filename, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new file form field found. `transferEncoding` contains the 'Content-Transfer-Encoding' value for the file stream. `mimeType` contains the 'Content-Type' value for the file stream.
* Note: if you listen for this event, you should always handle the `stream` no matter if you care about the file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any** incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards `files` and `parts` limits).
* If a configured file size limit was reached, `stream` will both have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
* The property `bytesRead` informs about the number of bytes that have been read so far.
* Note: if you listen for this event, you should always handle the `stream` no matter if you care about the file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any** incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards `files` and `parts` limits).
* If a configured file size limit was reached, `stream` will both have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
* The property `bytesRead` informs about the number of bytes that have been read so far.

* **limit**() - Emitted when a file exceeds the configured `fileSize` limit. You can listen on the file stream to handle it:

Expand All @@ -221,62 +208,61 @@ busboy.on('file', (fieldname, stream) => {

* **fieldsLimit**() - Emitted when specified `fields` limit has been reached. No more 'field' events will be emitted.


Busboy methods
--------------

* **(constructor)**(< _object_ >config) - Creates and returns a new Busboy instance.

* The constructor takes the following valid `config` settings:
* The constructor takes the following valid `config` settings:

* **headers** - _object_ - These are the HTTP headers of the incoming request, which are used by individual parsers.
* **headers** - _object_ - These are the HTTP headers of the incoming request, which are used by individual parsers.

* **autoDestroy** - _boolean_ - Whether this stream should automatically call .destroy() on itself after ending. (Default: false).
* **autoDestroy** - _boolean_ - Whether this stream should automatically call .destroy() on itself after ending. (Default: false).

* **highWaterMark** - _integer_ - highWaterMark to use for this Busboy instance (Default: WritableStream default).
* **highWaterMark** - _integer_ - highWaterMark to use for this Busboy instance (Default: WritableStream default).

* **fileHwm** - _integer_ - highWaterMark to use for file streams (Default: ReadableStream default).
* **fileHwm** - _integer_ - highWaterMark to use for file streams (Default: ReadableStream default).

* **defCharset** - _string_ - Default character set to use when one isn't defined (Default: 'utf8').
* **defCharset** - _string_ - Default character set to use when one isn't defined (Default: 'utf8').

* **preservePath** - _boolean_ - If paths in the multipart 'filename' field shall be preserved. (Default: false).
* **preservePath** - _boolean_ - If paths in the multipart 'filename' field shall be preserved. (Default: false).

* **isPartAFile** - __function__ - Use this function to override the default file detection functionality. It has following parameters:
* **isPartAFile** - **function** - Use this function to override the default file detection functionality. It has following parameters:

* fieldName - __string__ The name of the field.
* fieldName - **string** The name of the field.

* contentType - __string__ The content-type of the part, e.g. `text/plain`, `image/jpeg`, `application/octet-stream`
* contentType - **string** The content-type of the part, e.g. `text/plain`, `image/jpeg`, `application/octet-stream`

* fileName - __string__ The name of a file supplied by the part.
* fileName - **string** The name of a file supplied by the part.

(Default: `(fieldName, contentType, fileName) => (contentType === 'application/octet-stream' || fileName !== undefined)`)

* **limits** - _object_ - Various limits on incoming data. Valid properties are:
* **limits** - _object_ - Various limits on incoming data. Valid properties are:

* **fieldNameSize** - _integer_ - Max field name size (in bytes) (Default: 100 bytes).
* **fieldNameSize** - _integer_ - Max field name size (in bytes) (Default: 100 bytes).

* **fieldSize** - _integer_ - Max field value size (in bytes) (Default: 1 MiB, which is 1024 x 1024 bytes).
* **fieldSize** - _integer_ - Max field value size (in bytes) (Default: 1 MiB, which is 1024 x 1024 bytes).

* **fields** - _integer_ - Max number of non-file fields (Default: Infinity).
* **fields** - _integer_ - Max number of non-file fields (Default: Infinity).

* **fileSize** - _integer_ - For multipart forms, the max file size (in bytes) (Default: Infinity).
* **fileSize** - _integer_ - For multipart forms, the max file size (in bytes) (Default: Infinity).

* **files** - _integer_ - For multipart forms, the max number of file fields (Default: Infinity).
* **files** - _integer_ - For multipart forms, the max number of file fields (Default: Infinity).

* **parts** - _integer_ - For multipart forms, the max number of parts (fields + files) (Default: Infinity).
* **parts** - _integer_ - For multipart forms, the max number of parts (fields + files) (Default: Infinity).

* **headerPairs** - _integer_ - For multipart forms, the max number of header key=>value pairs to parse **Default:** 2000
* **headerPairs** - _integer_ - For multipart forms, the max number of header key=>value pairs to parse **Default:** 2000

* **headerSize** - _integer_ - For multipart forms, the max size of a multipart header **Default:** 81920.
* **headerSize** - _integer_ - For multipart forms, the max size of a multipart header **Default:** 81920.

* The constructor can throw errors:
* The constructor can throw errors:

* **Busboy expected an options-Object.** - Busboy expected an Object as first parameters.
* **Busboy expected an options-Object.** - Busboy expected an Object as first parameters.

* **Busboy expected an options-Object with headers-attribute.** - The first parameter is lacking of a headers-attribute.
* **Busboy expected an options-Object with headers-attribute.** - The first parameter is lacking of a headers-attribute.

* **Limit $limit is not a valid number** - Busboy expected the desired limit to be of type number. Busboy throws this Error to prevent a potential security issue by falling silently back to the Busboy-defaults. Potential source for this Error can be the direct use of environment variables without transforming them to the type number.
* **Limit $limit is not a valid number** - Busboy expected the desired limit to be of type number. Busboy throws this Error to prevent a potential security issue by falling silently back to the Busboy-defaults. Potential source for this Error can be the direct use of environment variables without transforming them to the type number.

* **Unsupported Content-Type.** - The `Content-Type` isn't one Busboy can parse.
* **Unsupported Content-Type.** - The `Content-Type` isn't one Busboy can parse.

* **Missing Content-Type-header.** - The provided headers don't include `Content-Type` at all.
* **Missing Content-Type-header.** - The provided headers don't include `Content-Type` at all.
50 changes: 24 additions & 26 deletions bench/busboy-form-bench-latin1.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
'use strict'

const Busboy = require('busboy');
const { createMultipartBufferForEncodingBench } = require("./createMultipartBufferForEncodingBench");
const Busboy = require('busboy')
const { createMultipartBufferForEncodingBench } = require('./createMultipartBufferForEncodingBench')

for (var i = 0, il = 10000; i < il; i++) { // eslint-disable-line no-var
const boundary = '-----------------------------168072824752491622650073',
busboy = new Busboy({
headers: {
'content-type': 'multipart/form-data; boundary=' + boundary
}
}),
buffer = createMultipartBufferForEncodingBench(boundary, 100, 'iso-8859-1'),
mb = buffer.length / 1048576;
for (var i = 0, il = 10000; i < il; i++) { // eslint-disable-line no-var
const boundary = '-----------------------------168072824752491622650073'
const busboy = new Busboy({
headers: {
'content-type': 'multipart/form-data; boundary=' + boundary
}
})
const buffer = createMultipartBufferForEncodingBench(boundary, 100, 'iso-8859-1')
const mb = buffer.length / 1048576

let processedData = 0;
busboy.on('file', (field, file, filename, encoding, mimetype) => {
file.resume()
})
busboy.on('file', (field, file, filename, encoding, mimetype) => {
file.resume()
})

busboy.on('error', function (err) {
})
busboy.on('finish', function () {
})
busboy.on('error', function () {
})
busboy.on('finish', function () {
})

const start = +new Date();
const result = busboy.write(buffer, () => { });
busboy.end();
const duration = +new Date - start;
const mbPerSec = (mb / (duration / 1000)).toFixed(2);
console.log(mbPerSec + ' mb/sec');
}
const start = +new Date()
busboy.end()
const duration = +new Date() - start
const mbPerSec = (mb / (duration / 1000)).toFixed(2)
console.log(mbPerSec + ' mb/sec')
}
50 changes: 24 additions & 26 deletions bench/busboy-form-bench-utf8.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
'use strict'

const Busboy = require('busboy');
const { createMultipartBufferForEncodingBench } = require("./createMultipartBufferForEncodingBench");
const Busboy = require('busboy')
const { createMultipartBufferForEncodingBench } = require('./createMultipartBufferForEncodingBench')

for (var i = 0, il = 10000; i < il; i++) { // eslint-disable-line no-var
const boundary = '-----------------------------168072824752491622650073',
busboy = new Busboy({
headers: {
'content-type': 'multipart/form-data; boundary=' + boundary
}
}),
buffer = createMultipartBufferForEncodingBench(boundary, 100, 'utf-8'),
mb = buffer.length / 1048576;
for (var i = 0, il = 10000; i < il; i++) { // eslint-disable-line no-var
const boundary = '-----------------------------168072824752491622650073'
const busboy = new Busboy({
headers: {
'content-type': 'multipart/form-data; boundary=' + boundary
}
})
const buffer = createMultipartBufferForEncodingBench(boundary, 100, 'utf-8')
const mb = buffer.length / 1048576

let processedData = 0;
busboy.on('file', (field, file, filename, encoding, mimetype) => {
file.resume()
})
busboy.on('file', (field, file, filename, encoding, mimetype) => {
file.resume()
})

busboy.on('error', function (err) {
})
busboy.on('finish', function () {
})
busboy.on('error', function () {
})
busboy.on('finish', function () {
})

const start = +new Date();
const result = busboy.write(buffer, () => { });
busboy.end();
const duration = +new Date - start;
const mbPerSec = (mb / (duration / 1000)).toFixed(2);
console.log(mbPerSec + ' mb/sec');
}
const start = +new Date()
busboy.end()
const duration = +new Date() - start
const mbPerSec = (mb / (duration / 1000)).toFixed(2)
console.log(mbPerSec + ' mb/sec')
}
23 changes: 12 additions & 11 deletions bench/createMultipartBufferForEncodingBench.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading