Gathering detailed insights and metrics for @caff/async-busboy
Gathering detailed insights and metrics for @caff/async-busboy
npm install @caff/async-busboy
Typescript
Module System
Node Version
NPM Version
70.7
Supply Chain
98.8
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (99.47%)
Shell (0.53%)
Total Downloads
1,258
Last Day
3
Last Week
6
Last Month
13
Last Year
170
524 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
3.0.2
Package Id
@caff/async-busboy@3.0.2
Unpacked Size
18.54 kB
Size
5.61 kB
File Count
6
NPM Version
8.13.2
Node Version
18.6.0
Publised On
26 Jan 2023
Cumulative downloads
Total Downloads
Last day
0%
3
Compared to previous day
Last week
500%
6
Compared to previous week
Last month
-35%
13
Compared to previous month
Last year
-71.6%
170
Compared to previous year
Forked from async-busboy.
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 ( 14 typeof value === 'object' && 15 !(value instanceof File) && 16 !(value instanceof Date) 17 ) { 18 serializeFormData(value, formDataObj, formKey); 19 } else if (value instanceof Date) { 20 formDataObj[formKey] = value.toISOString(); 21 } else { 22 formDataObj[formKey] = value; 23 } 24 } 25 } 26 return formDataObj; 27}; 28 29// -->
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.