{"id":14989544,"url":"https://github.com/m4nuc/async-busboy","last_synced_at":"2025-04-04T11:11:38.672Z","repository":{"id":3416440,"uuid":"49399491","full_name":"m4nuC/async-busboy","owner":"m4nuC","description":"Promise based multipart form parser for KoaJS","archived":false,"fork":false,"pushed_at":"2024-05-22T09:59:27.000Z","size":101,"stargazers_count":167,"open_issues_count":10,"forks_count":58,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-04T10:58:23.300Z","etag":null,"topics":["async","async-busboy","busboy","formdata","koa2","multipart","promise"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/m4nuC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-01-11T03:23:07.000Z","updated_at":"2024-04-23T11:28:03.000Z","dependencies_parsed_at":"2024-12-11T12:07:06.758Z","dependency_job_id":"1c12eca2-ddf6-4b71-ab12-1b2df4994c83","html_url":"https://github.com/m4nuC/async-busboy","commit_stats":{"total_commits":82,"total_committers":17,"mean_commits":4.823529411764706,"dds":"0.36585365853658536","last_synced_commit":"c68d7b8138e0cabfd425b6a79b0a2072148a045d"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m4nuC%2Fasync-busboy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m4nuC%2Fasync-busboy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m4nuC%2Fasync-busboy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/m4nuC%2Fasync-busboy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/m4nuC","download_url":"https://codeload.github.com/m4nuC/async-busboy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247166168,"owners_count":20894654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","async-busboy","busboy","formdata","koa2","multipart","promise"],"created_at":"2024-09-24T14:18:32.502Z","updated_at":"2025-04-04T11:11:38.656Z","avatar_url":"https://github.com/m4nuC.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Promise Based Multipart Form Parser\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/async-busboy.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/async-busboy\n[travis-image]: https://img.shields.io/travis/m4nuC/async-busboy.svg?style=flat-square\n[travis-url]: https://travis-ci.org/m4nuC/async-busboy\n[codecov-image]: https://codecov.io/github/m4nuC/async-busboy/coverage.svg?branch=master\n[codecov-url]: https://codecov.io/github/m4nuC/async-busboy?branch=master\n[download-image]: https://img.shields.io/npm/dm/async-busboy.svg?style=flat-square\n[download-url]: https://npmjs.org/package/async-busboy\n\nThe typical use case for this library is when handling forms that contain file upload field(s) mixed with other inputs.\nParsing logic relies on [busboy](http://github.com/mscdex/busboy).\nDesigned for use with [Koa2](https://github.com/koajs/koa/tree/v2.x) and [Async/Await](https://github.com/tc39/ecmascript-asyncawait).\n\n## Examples\n\n### Async/Await (using temp files)\n\n```js\nimport asyncBusboy from 'async-busboy';\n\n// Koa 2 middleware\nasync function someFunction(ctx, next) {\n  const { files, fields } = await asyncBusboy(ctx.req);\n\n  // Make some validation on the fields before upload to S3\n  if (checkFiles(fields)) {\n    files.map(uploadFilesToS3);\n  } else {\n    return 'error';\n  }\n}\n```\n\n### Async/Await (using custom onFile handler, i.e. no temp files)\n\n```js\nimport asyncBusboy from 'async-busboy';\n\n// Koa 2 middleware\nasync function someFunction(ctx, next) {\n  const { fields } = await asyncBusboy(ctx.req, {\n    onFile: function (fieldname, file, filename, encoding, mimetype) {\n      uploadFilesToS3(file);\n    },\n  });\n\n  // Do validation, but files are already uploading...\n  if (!checkFiles(fields)) {\n    return 'error';\n  }\n}\n```\n\n### ES5 with promise (using temp files)\n\n```js\nvar asyncBusboy = require('async-busboy');\n\nfunction someFunction(someHTTPRequest) {\n  asyncBusboy(someHTTPRequest).then(function (formData) {\n    // do something with formData.files\n    // do someting with formData.fields\n  });\n}\n```\n\n## Async API using temp files\n\nThe 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.\n\n## Async API using custom onFile handler\n\nIf a custom onFile handler is specified in the options to async-busboy it\nwill only resolve an object containing fields, but instead no temporary files\nneeds to be created since the file stream is directly passed to the application.\nNote that all file streams need to be consumed for async-busboy to resolve due\nto the implementation of busboy. If you don't care about a received\nfile stream, simply call `stream.resume()` to discard the content.\n\n## Working with nested inputs and objects\n\nMake sure to serialize objects before sending them as formData.\ni.e:\n\n```json5\n// Given an object that represent the form data:\n{\n  field1: 'value',\n  objectField: {\n    key: 'anotherValue',\n  },\n  arrayField: ['a', 'b'],\n  //...\n}\n```\n\nShould be sent as:\n\n```js\n// -\u003e field1[value]\n// -\u003e objectField[key][anotherKey]\n// -\u003e arrayField[0]['a']\n// -\u003e arrayField[1]['b']\n// .....\n```\n\nHere is a function that can take care of this process\n\n```js\nconst serializeFormData = (obj, formDataObj, namespace = null) =\u003e {\n  var formDataObj = formDataObj || {};\n  var formKey;\n  for (var property in obj) {\n    if (obj.hasOwnProperty(property)) {\n      if (namespace) {\n        formKey = namespace + '[' + property + ']';\n      } else {\n        formKey = property;\n      }\n\n      var value = obj[property];\n      if (\n        typeof value === 'object' \u0026\u0026\n        !(value instanceof File) \u0026\u0026\n        !(value instanceof Date)\n      ) {\n        serializeFormData(value, formDataObj, formKey);\n      } else if (value instanceof Date) {\n        formDataObj[formKey] = value.toISOString();\n      } else {\n        formDataObj[formKey] = value;\n      }\n    }\n  }\n  return formDataObj;\n};\n\n// --\u003e\n```\n\n### Try it on your local\n\nIf you want to run some test locally, clone this repo, then run: `node examples/index.js`\nFrom there you can use something like [Postman](https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en) to send `POST` request to `localhost:8080`.\nNote: 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 automatically)\n\n### Use cases:\n\n- Form sending only octet-stream (files)\n\n- Form sending file octet-stream (files) and input fields.\n  a. File and fields are processed has they arrive. Their order do not matter.\n  b. Fields must be processed (for example validated) before processing the files.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm4nuc%2Fasync-busboy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fm4nuc%2Fasync-busboy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fm4nuc%2Fasync-busboy/lists"}