Repository: unjs/h3
Pull request #416 (2023-06-24 15:58:12)

fix(readMultipartFormData): handle utf8 encoding for `name` and `filename`
After I uploaded a file with a Chinese name:
```
20xx年xx月xx日一号楼电表数据模版.xlsx
```
the filename was parsed as ISO-8859-1 encoding:
```
20xxå¹´xxæ__xxæ_¥ä¸_å_·æ¥¼ç_µè¡¨æ_°æ_®æ¨¡ç__.xlsx
```

So I used Buffer to convert it to UTF-8.
``` js
let filename = "20xxå¹´xxæxxæ¥ä¸å·æ¥¼çµè¡¨æ°æ®æ¨¡ç.xlsx";
let buffer = Buffer.from(filename, 'latin1');
filename = buffer.toString('utf8');
console.log(filename);

```

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/unjs/h3/pull/416?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs) Report
> Merging [#416](https://app.codecov.io/gh/unjs/h3/pull/416?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs) (c2ce4cf) into [main](https://app.codecov.io/gh/unjs/h3/commit/ed3ae901b223f12e358b6ae1a392c043d24ba5b2?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs) (ed3ae90) will **decrease** coverage by `0.60%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head c2ce4cf differs from pull request most recent head 0540fdf. Consider uploading reports for the commit 0540fdf to get more accurate results

```diff
@@            Coverage Diff             @@
##             main     #416      +/-   ##
==========================================
- Coverage   78.36%   77.77%   -0.60%     
==========================================
  Files          26       26              
  Lines        2792     2695      -97     
  Branches      408      393      -15     
==========================================
- Hits         2188     2096      -92     
+ Misses        604      599       -5     
```


| [Impacted Files](https://app.codecov.io/gh/unjs/h3/pull/416?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs) | Coverage Δ | |
|---|---|---|
| [src/utils/internal/multipart.ts](https://app.codecov.io/gh/unjs/h3/pull/416?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs#diff-c3JjL3V0aWxzL2ludGVybmFsL211bHRpcGFydC50cw==) | `97.40% <100.00%> (+0.05%)` | :arrow_up: |

... and [7 files with indirect coverage changes](https://app.codecov.io/gh/unjs/h3/pull/416/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=unjs)



Patch:
diff --git a/src/utils/internal/multipart.ts b/src/utils/internal/multipart.ts
index cb48323..fc3d567 100644
--- a/src/utils/internal/multipart.ts
+++ b/src/utils/internal/multipart.ts
@@ -134,7 +134,8 @@ function process(data: number[], headers: [string, string][]): MultiPartData {
     }
     const key = (s[0] || "").trim();
     if (key === "name" || key === "filename") {
-      dataObj[key] = (s[1] || "").trim().replace(/"/g, "");
+      const _value = (s[1] || "").trim().replace(/"/g, "");
+      dataObj[key] = Buffer.from(_value, "latin1").toString("utf8");
     }
   }
 


Test patch:
diff --git a/test/body.test.ts b/test/body.test.ts
index 98696af..68fa02e 100644
--- a/test/body.test.ts
+++ b/test/body.test.ts
@@ -249,7 +249,7 @@ describe("", () => {
           "multipart/form-data; boundary=---------------------------12537827810750053901680552518"
         )
         .send(
-          '-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="baz"\r\n\r\nother\r\n-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="bar"\r\n\r\nsomething\r\n-----------------------------12537827810750053901680552518--\r\n'
+          '-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="baz"\r\n\r\nother\r\n-----------------------------12537827810750053901680552518\r\nContent-Disposition: form-data; name="号楼电表数据模版.xlsx"\r\n\r\nsomething\r\n-----------------------------12537827810750053901680552518--\r\n'
         );
 
       expect(result.body).toMatchInlineSnapshot(`
@@ -260,7 +260,7 @@ describe("", () => {
           },
           {
             "data": "something",
-            "name": "bar",
+            "name": "号楼电表数据模版.xlsx",
           },
         ]
       `);