Gathering detailed insights and metrics for @amagitechnologies/async-busboy
Gathering detailed insights and metrics for @amagitechnologies/async-busboy
Promise based multipart form parser for KoaJS
npm install @amagitechnologies/async-busboy
Typescript
Module System
Node Version
NPM Version
70.5
Supply Chain
97.4
Quality
75
Maintenance
50
Vulnerability
99.6
License
JavaScript (100%)
Total Downloads
2,253
Last Day
1
Last Week
7
Last Month
53
Last Year
1,245
109 Commits
2 Forks
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.0
Package Id
@amagitechnologies/async-busboy@1.1.0
Unpacked Size
23.72 kB
Size
6.61 kB
File Count
7
NPM Version
8.4.1
Node Version
16.13.2
Cumulative downloads
Total Downloads
Last day
-75%
1
Compared to previous day
Last week
-68.2%
7
Compared to previous week
Last month
-62.4%
53
Compared to previous month
Last year
37%
1,245
Compared to previous year
1
4
Originally by @m4nuC, and forked/updated by AMAGI
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.
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}
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}
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}
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.
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.
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// -->
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)
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.
No security vulnerabilities found.