Installations
npm install async-busboy
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
14.16.1
NPM Version
6.14.12
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
m4nuC
Download Statistics
Total Downloads
2,009,766
Last Day
548
Last Week
2,537
Last Month
10,481
Last Year
158,470
GitHub Statistics
167 Stars
103 Commits
57 Forks
7 Watching
4 Branches
13 Contributors
Bundle Size
522.26 kB
Minified
191.35 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.1.0
Package Id
async-busboy@1.1.0
Size
6.40 kB
NPM Version
6.14.12
Node Version
14.16.1
Publised On
25 May 2021
Total Downloads
Cumulative downloads
Total Downloads
2,009,766
Last day
-3%
548
Compared to previous day
Last week
-9.1%
2,537
Compared to previous week
Last month
7.3%
10,481
Compared to previous month
Last year
-36.5%
158,470
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
4
Promise Based Multipart Form Parser
The typical use case for this library is when handling forms that contain file upload field(s) mixed with other inputs. Parsing logic relies on busboy. Designed for use with Koa2 and Async/Await.
Examples
Async/Await (using temp files)
1import asyncBusboy from 'async-busboy'; 2 3// Koa 2 middleware 4async function(ctx, next) { 5 const {files, fields} = await asyncBusboy(ctx.req); 6 7 // Make some validation on the fields before upload to S3 8 if ( checkFiles(fields) ) { 9 files.map(uploadFilesToS3) 10 } else { 11 return 'error'; 12 } 13}
Async/Await (using custom onFile handler, i.e. no temp files)
1import asyncBusboy from 'async-busboy';
2
3// Koa 2 middleware
4async function(ctx, next) {
5 Â const { fields } = await asyncBusboy(ctx.req, {
6 onFile: function(fieldname, file, filename, encoding, mimetype) {
7 uploadFilesToS3(file);
8 }
9 });
10
11 // Do validation, but files are already uploading...
12 if ( !checkFiles(fields) ) {
13 return 'error';
14 }
15}
ES5 with promise (using temp files)
1var asyncBusboy = require('async-busboy'); 2 3function(someHTTPRequest) { 4 asyncBusboy(someHTTPRequest).then(function(formData) { 5 // do something with formData.files 6 // do someting with formData.fields 7 }); 8}
Async API using temp files
The request streams are first written to temporary files using os.tmpdir()
. File read streams associated with the temporary files are returned from the call to async-busboy. When the consumer has drained the file read streams, the files will be automatically removed, otherwise the host OS should take care of the cleaning process.
Async API using custom onFile handler
If a custom onFile handler is specified in the options to async-busboy it
will only resolve an object containing fields, but instead no temporary files
needs to be created since the file stream is directly passed to the application.
Note that all file streams need to be consumed for async-busboy to resolve due
to the implementation of busboy. If you don't care about a received
file stream, simply call stream.resume()
to discard the content.
Working with nested inputs and objects
Make sure to serialize objects before sending them as formData. i.e:
1// Given an object that represent the form data: 2{ 3 'field1': 'value', 4 'objectField': { 5 'key': 'anotherValue' 6 }, 7 'arrayField': ['a', 'b'] 8 //... 9};
Should be sent as:
// -> field1[value]
// -> objectField[key][anotherKey]
// -> arrayField[0]['a']
// -> arrayField[1]['b']
// .....
Here is a function that can take care of this process
1const serializeFormData = (obj, formDataObj, namespace = null) => { 2 var formDataObj = formDataObj || {}; 3 var formKey; 4 for(var property in obj) { 5 if(obj.hasOwnProperty(property)) { 6 if(namespace) { 7 formKey = namespace + '[' + property + ']'; 8 } else { 9 formKey = property; 10 } 11 12 var value = obj[property]; 13 if(typeof value === 'object' && !(value instanceof File) && !(value instanceof Date)) { 14 serializeFormData(value, formDataObj, formKey); 15 } else if(value instanceof Date) { 16 formDataObj[formKey] = value.toISOString(); 17 } else { 18 formDataObj[formKey] = value; 19 } 20 } 21 } 22 return formDataObj; 23}; 24 25// -->
Try it on your local
If you want to run some test locally, clone this repo, then run: node examples/index.js
From there you can use something like Postman to send POST
request to localhost:8080
.
Note: When using Postman make sure to not send a Content-Type
header, if it's filed by default, just delete it. (This is to let the boudary
header be generated automaticaly)
Use cases:
-
Form sending only octet-stream (files)
-
Form sending file octet-stream (files) and input fields. a. File and fields are processed has they arrive. Their order do not matter. b. Fields must be processed (for example validated) before processing the files.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
Found 6/11 approved changesets -- score normalized to 5
Reason
6 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 25 are checked with a SAST tool
Score
2.9
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More