{"id":21848941,"url":"https://github.com/amit-a/formidable-serverless","last_synced_at":"2025-04-14T14:42:21.176Z","repository":{"id":49599221,"uuid":"173531299","full_name":"Amit-A/formidable-serverless","owner":"Amit-A","description":"Enables use of formidable (node.js module for parsing form data, especially file uploads) in serverless environments.","archived":false,"fork":false,"pushed_at":"2021-06-12T14:52:07.000Z","size":85,"stargazers_count":36,"open_issues_count":6,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T20:57:55.946Z","etag":null,"topics":["cloudfunctions","formidable","lambda","nodejs-modules","serverless"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/formidable-serverless","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/Amit-A.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-03T04:38:58.000Z","updated_at":"2024-11-19T15:05:22.000Z","dependencies_parsed_at":"2022-08-30T00:11:50.228Z","dependency_job_id":null,"html_url":"https://github.com/Amit-A/formidable-serverless","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Amit-A%2Fformidable-serverless","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Amit-A%2Fformidable-serverless/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Amit-A%2Fformidable-serverless/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Amit-A%2Fformidable-serverless/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Amit-A","download_url":"https://codeload.github.com/Amit-A/formidable-serverless/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248898585,"owners_count":21179798,"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":["cloudfunctions","formidable","lambda","nodejs-modules","serverless"],"created_at":"2024-11-28T00:09:31.619Z","updated_at":"2025-04-14T14:42:21.151Z","avatar_url":"https://github.com/Amit-A.png","language":"JavaScript","readme":"# formidable-serverless\n\n[![npm version](https://badge.fury.io/js/formidable-serverless.svg)](https://badge.fury.io/js/formidable-serverless)\n\n## Purpose\n\nThis module is a variant of [formidable](https://www.npmjs.com/package/formidable) with tweaks to enable use in serverless environments (AWS Lambda, Firebase/Google Cloud Functions, etc.) and environments where the request has already been processed (e.g. by bodyparser).\n\nThe functionality and usage/API are identical to [formidable](https://www.npmjs.com/package/formidable) (documentation cloned below).\n\n## Status\n\nSponsored and maintained by the folks at [testmail.app](https://testmail.app)\n\n## How it works\n\nThe preprocessing by bodyparsers built-in to serverless environments breaks formidable's parse handlers and causes \"Request Aborted\" errors. This module imports formidable as a dependency and modifies the handlers to support preprocessed request bodies.\n\nThis module can also be used in non-serverless environments (usage and API are identical), but it may be a version or two behind. This package is focused on serverless - if you have issues with formidable itself, please open them in the formidable repo.\n\n## Formidable\n\nA Node.js module for parsing form data, especially file uploads.\n\nFormidable was developed for [Transloadit](http://transloadit.com/), a service focused on uploading and encoding images and videos. It has been battle-tested against hundreds of GB of file uploads from a large variety of clients and is considered production-ready.\n\n## Features\n\n* Fast (~500mb/sec), non-buffering multipart parser\n* Automatically writing file uploads to disk\n* Low memory footprint\n* Graceful error handling\n* Very high test coverage\n\n## Installation\n\n```sh\nnpm install --save formidable-serverless\n```\n\nThis is a low-level package, and if you're using a high-level framework it may already be included. However, [Express v4](http://expressjs.com) does not include any multipart handling, nor does [body-parser](https://github.com/expressjs/body-parser).\n\n## Example\n\nParse an incoming file upload.\n\n```javascript\nconst formidable = require('formidable-serverless');\nconst http = require('http');\nconst util = require('util');\n\nhttp.createServer(function(req, res) {\n  if (req.url == '/upload' \u0026\u0026 req.method.toLowerCase() == 'post') {\n    // parse a file upload\n    const form = new formidable.IncomingForm();\n\n    form.parse(req, function(err, fields, files) {\n      res.writeHead(200, {'content-type': 'text/plain'});\n      res.write('received upload:\\n\\n');\n      res.end(util.inspect({fields: fields, files: files}));\n    });\n\n    return;\n  }\n\n  // show a file upload form\n  res.writeHead(200, {'content-type': 'text/html'});\n  res.end(\n    '\u003cform action=\"/upload\" enctype=\"multipart/form-data\" method=\"post\"\u003e'+\n    '\u003cinput type=\"text\" name=\"title\"\u003e\u003cbr\u003e'+\n    '\u003cinput type=\"file\" name=\"upload\" multiple=\"multiple\"\u003e\u003cbr\u003e'+\n    '\u003cinput type=\"submit\" value=\"Upload\"\u003e'+\n    '\u003c/form\u003e'\n  );\n}).listen(8080);\n```\n\n## API\n\n### Formidable.IncomingForm\n\n```javascript\nvar form = new formidable.IncomingForm()\n```\n\nCreates a new incoming form.\n\n```javascript\nform.encoding = 'utf-8';\n```\n\nSets encoding for incoming form fields.\n\n```javascript\nform.uploadDir = \"/my/dir\";\n```\n\nSets the directory for placing file uploads in. You can move them later on using `fs.rename()`. The default is `os.tmpdir()`.\n\n```javascript\nform.keepExtensions = false;\n```\n\nIf you want the files written to `form.uploadDir` to include the extensions of the original files, set this property to `true`.\n\n```javascript\nform.type\n```\n\nEither 'multipart' or 'urlencoded' depending on the incoming request.\n\n```javascript\nform.maxFieldsSize = 20 * 1024 * 1024;\n```\n\nLimits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an `'error'` event is emitted. The default size is 20MB.\n\n```javascript\nform.maxFileSize = 200 * 1024 * 1024;\n```\n\nLimits the size of uploaded file. If this value is exceeded, an `'error'` event is emitted. The default size is 200MB.\n\n```javascript\nform.maxFields = 1000;\n```\n\nLimits the number of fields that the querystring parser will decode. Defaults to 1000 (0 for unlimited).\n\n```javascript\nform.hash = false;\n```\n\nIf you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`.\n\n```javascript\nform.multiples = false;\n```\n\nIf this option is enabled, when you call `form.parse`, the `files` argument will contain arrays of files for inputs which submit multiple files using the HTML5 `multiple` attribute.\n\n```javascript\nform.bytesReceived\n```\n\nThe amount of bytes received for this form so far.\n\n```javascript\nform.bytesExpected\n```\n\nThe expected number of bytes in this form.\n\n```javascript\nform.parse(request, [cb]);\n```\n\nParses an incoming node.js `request` containing form data. If `cb` is provided, all fields and files are collected and passed to the callback:\n\n```javascript\nform.parse(req, function(err, fields, files) {\n  // ...\n});\n\nform.onPart(part);\n```\n\nYou may overwrite this method if you are interested in directly accessing the multipart stream. Doing so will disable any `'field'` / `'file'` events  processing which would occur otherwise, making you fully responsible for handling the processing.\n\n```javascript\nform.onPart = function(part) {\n  part.addListener('data', function() {\n    // ...\n  });\n}\n```\n\nIf you want to use formidable to only handle certain parts for you, you can do so:\n\n```javascript\nform.onPart = function(part) {\n  if (!part.filename) {\n    // let formidable handle all non-file parts\n    form.handlePart(part);\n  }\n}\n```\n\nCheck the code in this method for further inspiration.\n\n### Formidable.File\n\n```javascript\nfile.size = 0\n```\n\nThe size of the uploaded file in bytes. If the file is still being uploaded (see `'fileBegin'` event), this property says how many bytes of the file have been written to disk yet.\n\n```javascript\nfile.path = null\n```\n\nThe path this file is being written to. You can modify this in the `'fileBegin'` event in case you are unhappy with the way formidable generates a temporary path for your files.\n\n```javascript\nfile.name = null\n```\n\nThe name this file had according to the uploading client.\n\n```javascript\nfile.type = null\n```\n\nThe mime type of this file, according to the uploading client.\n\n```javascript\nfile.lastModifiedDate = null\n```\n\nA date object (or `null`) containing the time this file was last written to. Mostly here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/).\n\n```javascript\nfile.hash = null\n```\n\nIf hash calculation was set, you can read the hex digest out of this var.\n\n#### Formidable.File#toJSON()\n\nThis method returns a JSON-representation of the file, allowing you to `JSON.stringify()` the file which is useful for logging and responding to requests.\n\n### Events\n\n#### 'progress'\n\nEmitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.\n\n```javascript\nform.on('progress', function(bytesReceived, bytesExpected) {\n});\n```\n\n#### 'field'\n\nEmitted whenever a field / value pair has been received.\n\n```javascript\nform.on('field', function(name, value) {\n});\n```\n\n#### 'fileBegin'\n\nEmitted whenever a new file is detected in the upload stream. Use this event if you want to stream the file to somewhere else while buffering the upload on the file system.\n\n```javascript\nform.on('fileBegin', function(name, file) {\n});\n```\n\n#### 'file'\n\nEmitted whenever a field / file pair has been received. `file` is an instance of `File`.\n\n```javascript\nform.on('file', function(name, file) {\n});\n```\n\n#### 'error'\n\nEmitted when there is an error processing the incoming form. A request that experiences an error is automatically paused, you will have to manually call `request.resume()` if you want the request to continue firing `'data'` events.\n\n```javascript\nform.on('error', function(err) {\n});\n```\n\n#### 'aborted'\n\nEmitted when the request was aborted by the user. Right now this can be due to a 'timeout' or 'close' event on the socket. After this event is emitted, an `error` event will follow. In the future there will be a separate 'timeout' event (needs a change in the node core).\n\n```javascript\nform.on('aborted', function() {\n});\n```\n\n##### 'end'\n\n```javascript\nform.on('end', function() {\n});\n```\n\nEmitted when the entire request has been received, and all contained files have finished flushing to disk. This is a great place for you to send your response.\n\n## License\n\nFormidable and formidable-serverless are licensed under the MIT license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famit-a%2Fformidable-serverless","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famit-a%2Fformidable-serverless","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famit-a%2Fformidable-serverless/lists"}