Skip to content

Commit a7d59c3

Browse files
committed
Small tweaks
1 parent 50c83e8 commit a7d59c3

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

packages/form-data-parser/src/lib/form-data.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,19 @@ export interface FileUploadHandler {
7070
}
7171

7272
async function defaultFileUploadHandler(file: FileUpload): Promise<File> {
73-
// Do the slow thing and buffer the entire file in memory.
73+
// Buffer the file in memory
7474
let buffer = await file.arrayBuffer();
7575
return new File([buffer], file.name, { type: file.type, lastModified: file.lastModified });
7676
}
7777

7878
/**
79-
* Parses a `Request` body into a `FormData` object. This is useful for accessing the data contained
80-
* in a HTTP `POST` request generated by a HTML `<form>` element.
79+
* Parses a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) body into a [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)
80+
* object. This is useful when accessing the data contained in a HTTP `multipart/form-data` request
81+
* generated by a HTML `<form>` element.
8182
*
82-
* The major difference between this function and using the built-in `request.formData()` API is the
83-
* ability to customize the handling of file uploads. Instead of buffering the entire file in memory,
84-
* the `uploadHandler` allows you to store the file on disk or in a cloud storage service.
83+
* The main difference between this function and using [the built-in `request.formData()` API](https://developer.mozilla.org/en-US/docs/Web/API/Request/formData)
84+
* is the ability to customize the handling of file uploads. Instead of buffering the entire file in
85+
* memory, the `uploadHandler` allows you to stream the file to disk or a cloud storage service.
8586
*/
8687
export async function parseFormData(
8788
request: Request,
@@ -96,14 +97,15 @@ export async function parseFormData(
9697
let promises: Promise<void | null | string | File>[] = [];
9798

9899
for await (let part of parseMultipartRequest(request, parserOptions)) {
99-
if (!part.name) continue;
100+
let fieldName = part.name;
101+
102+
if (!fieldName) continue;
100103

101104
if (part.isFile) {
102105
let value = uploadHandler(new FileUpload(part));
103106

104107
if (value != null) {
105108
if (isPromise(value)) {
106-
let fieldName = part.name;
107109
promises.push(
108110
value.then((file) => {
109111
if (file != null) {
@@ -112,11 +114,11 @@ export async function parseFormData(
112114
}),
113115
);
114116
} else {
115-
formData.append(part.name, value);
117+
formData.append(fieldName, value);
116118
}
117119
}
118120
} else {
119-
formData.append(part.name, await part.text());
121+
formData.append(fieldName, await part.text());
120122
}
121123
}
122124

0 commit comments

Comments
 (0)