Repository: koajs/koa-body

Issue:
Upgrade formidable
## Problem/Feature Request Summary

Hello,

Do you plan to update the formidable dependency to 3.x ? Formidable 1.x is currently deprecated.
I can help if you want.

Thank you

Hints from issue comments:
@RayJason: when I add dependencies:
```bash
WARN  deprecated formidable@1.2.6: Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau
```@djulen: Hello, last month in `formidable` was found vulnerability issue https://www.mend.io/vulnerability-database/CVE-2022-29622. This issue was fixed in the last version of the library. Do we have a plan to update  `formidable`  to the latest version soon?@guillenotfound: Are there any plans to update the dependencies?@wichopy: about to drop this library due to lack of updates for the last year and this security issue@dantonini: I made a PR about it: https://github.com/koajs/koa-body/pull/242

There are also many other opened issues (and PRs) on the same topic, don't know if or when it will be merged@TheDadi: done in https://github.com/koajs/koa-body/pull/239

Solution patch:
diff --git a/README.md b/README.md
index f23b8f5..4d5ad01 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@ npm install koa-body
 ## Features
 - can handle requests such as:
   * **multipart/form-data**
-  * **application/x-www-urlencoded**
+  * **application/x-www-form-urlencoded**
   * **application/json**
   * **application/json-patch+json**
   * **application/vnd.api+json**
@@ -158,7 +158,7 @@ Some applications require crytopgraphic verification of request bodies, for exam
 - `maxFieldsSize` **{Integer}** Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default `2mb (2 * 1024 * 1024)`
 - `uploadDir` **{String}** Sets the directory for placing file uploads in, default `os.tmpDir()`
 - `keepExtensions` **{Boolean}** Files written to `uploadDir` will include the extensions of the original files, default `false`
-- `hash` **{String}** If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`, default `false`
+- `hashAlgorithm` **{String}** If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`, default `false`
 - `multiples` **{Boolean}** Multiple file uploads or no, default `true`
 - `onFileBegin` **{Function}** Special callback on file begin. The function is executed directly by formidable. It can be used to rename files before saving them to disk. [See the docs](https://github.com/felixge/node-formidable#filebegin)
 
diff --git a/index.d.ts b/index.d.ts
index 02afce3..ef121cb 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -1,5 +1,5 @@
 import * as Koa from "koa";
-import { Files } from 'formidable';
+import { File, Files } from 'formidable';
 
 declare module "koa" {
     interface Request extends Koa.BaseRequest {
@@ -40,7 +40,7 @@ declare namespace koaBody {
         /**
          * {String} If you want checksums calculated for incoming files, set this to either 'sha1' or 'md5', default false
          */
-        hash?: string;
+        hashAlgorithm?: string;
 
         /**
          * {Boolean} Multiple file uploads or no, default true
@@ -51,7 +51,7 @@ declare namespace koaBody {
          * {Function} Special callback on file begin. The function is executed directly by formidable.
          * It can be used to rename files before saving them to disk. See https://github.com/felixge/node-formidable#filebegin
          */
-        onFileBegin?: (name: string, file: any) => void;
+        onFileBegin?: (name: string, file: File) => void;
     }
     interface IKoaBodyOptions {
         /**
diff --git a/package.json b/package.json
index 1997ae8..90ecc31 100644
--- a/package.json
+++ b/package.json
@@ -36,9 +36,9 @@
     "unparsed.js"
   ],
   "dependencies": {
-    "@types/formidable": "^1.0.31",
+    "@types/formidable": "^2.0.4",
     "co-body": "^5.1.1",
-    "formidable": "^1.1.1"
+    "formidable": "^2.0.1"
   },
   "devDependencies": {
     "@types/koa": "^2.0.39",



Test patch:
diff --git a/test/unit/index.js b/test/unit/index.js
index 1fbdbe6..bebe46f 100644
--- a/test/unit/index.js
+++ b/test/unit/index.js
@@ -168,39 +168,39 @@ describe('koa-body', () => {
         res.body.user.names[0].should.equal('John');
         res.body.user.names[1].should.equal('Paul');
         res.body._files.firstField.should.be.an.Object();
-        res.body._files.firstField.name.should.equal('package.json');
-        should(fs.statSync(res.body._files.firstField.path)).be.ok();
-        fs.unlinkSync(res.body._files.firstField.path);
+        res.body._files.firstField.originalFilename.should.equal('package.json');
+        should(fs.statSync(res.body._files.firstField.filepath)).be.ok();
+        fs.unlinkSync(res.body._files.firstField.filepath);
 
         res.body._files.secondField.should.be.an.Array().and.have.lengthOf(2);
         res.body._files.secondField.should.containDeep([{
-          name: 'index.js'
+          originalFilename: 'index.js'
         }]);
         res.body._files.secondField.should.containDeep([{
-          name: 'package.json'
+          originalFilename: 'package.json'
         }]);
-        should(fs.statSync(res.body._files.secondField[0].path)).be.ok();
-        should(fs.statSync(res.body._files.secondField[1].path)).be.ok();
-        fs.unlinkSync(res.body._files.secondField[0].path);
-        fs.unlinkSync(res.body._files.secondField[1].path);
+        should(fs.statSync(res.body._files.secondField[0].filepath)).be.ok();
+        should(fs.statSync(res.body._files.secondField[1].filepath)).be.ok();
+        fs.unlinkSync(res.body._files.secondField[0].filepath);
+        fs.unlinkSync(res.body._files.secondField[1].filepath);
 
         res.body._files.thirdField.should.be.an.Array().and.have.lengthOf(3);
 
         res.body._files.thirdField.should.containDeep([{
-          name: 'LICENSE'
+          originalFilename: 'LICENSE'
         }]);
         res.body._files.thirdField.should.containDeep([{
-          name: 'README.md'
+          originalFilename: 'README.md'
         }]);
         res.body._files.thirdField.should.containDeep([{
-          name: 'package.json'
+          originalFilename: 'package.json'
         }]);
-        should(fs.statSync(res.body._files.thirdField[0].path)).be.ok();
-        fs.unlinkSync(res.body._files.thirdField[0].path);
-        should(fs.statSync(res.body._files.thirdField[1].path)).be.ok();
-        fs.unlinkSync(res.body._files.thirdField[1].path);
-        should(fs.statSync(res.body._files.thirdField[2].path)).be.ok();
-        fs.unlinkSync(res.body._files.thirdField[2].path);
+        should(fs.statSync(res.body._files.thirdField[0].filepath)).be.ok();
+        fs.unlinkSync(res.body._files.thirdField[0].filepath);
+        should(fs.statSync(res.body._files.thirdField[1].filepath)).be.ok();
+        fs.unlinkSync(res.body._files.thirdField[1].filepath);
+        should(fs.statSync(res.body._files.thirdField[2].filepath)).be.ok();
+        fs.unlinkSync(res.body._files.thirdField[2].filepath);
 
         done();
       });
@@ -212,9 +212,9 @@ describe('koa-body', () => {
       formidable: {
         uploadDir: '/tmp',
         onFileBegin:  (name, file) => {
-          file.name = 'backage.json'
-          const folder = path.dirname(file.path);
-          file.path = path.join(folder, file.name);
+          file.newFilename = 'backage.json'
+          const folder = path.dirname(file.filepath);
+          file.filepath = path.join(folder, file.newFilename);
         }
       }
     }));
@@ -229,9 +229,9 @@ describe('koa-body', () => {
         if (err) return done(err);
 
         res.body._files.firstField.should.be.an.Object();
-        res.body._files.firstField.name.should.equal('backage.json');
-        should(fs.statSync(res.body._files.firstField.path)).be.ok();
-        fs.unlinkSync(res.body._files.firstField.path);
+        res.body._files.firstField.newFilename.should.equal('backage.json');
+        should(fs.statSync(res.body._files.firstField.filepath)).be.ok();
+        fs.unlinkSync(res.body._files.firstField.filepath);
 
         done();
       });