Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit b1a0e60

Browse files
committed
feat: cleaned the code
1 parent 03405dd commit b1a0e60

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

multiparser.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ function createFormData(): FormDataBody {
4949

5050
function getForm(pieces: Uint8Array[]) {
5151
let form: FormDataBody = createFormData();
52-
// let form: Form = { fields: {}, files: {} };
5352

5453
for (let piece of pieces) {
5554
const { headerByte, contentByte } = splitPiece(piece);
@@ -60,11 +59,15 @@ function getForm(pieces: Uint8Array[]) {
6059
// empty content, discard it
6160
if (contentByte.byteLength === 1 && contentByte[0] === 13) {
6261
continue;
63-
} else {
64-
// headers = "field1"
62+
}
63+
64+
// headers = "field1"
65+
else {
6566
form.fields[headers] = decoder.decode(contentByte);
6667
}
67-
} // it's a file field
68+
}
69+
70+
// it's a file field
6871
else {
6972
let file: FormFile = {
7073
name: headers.name,
@@ -86,7 +89,9 @@ function getHeaders(headerByte: Uint8Array) {
8689
// no contentType, it may be a string field, return name only
8790
if (contentTypeIndex < 0) {
8891
return getNameOnly(headerByte);
89-
} // file field, return with name, filename and contentType
92+
}
93+
94+
// file field, return with name, filename and contentType
9095
else {
9196
return getHeaderNContentType(headerByte, contentTypeIndex);
9297
}
@@ -137,11 +142,13 @@ function getNameNFilename(headerLineByte: Uint8Array, filenameIndex: number) {
137142

138143
function getNameOnly(headerLineByte: Uint8Array) {
139144
let nameIndex = bytes.findIndex(headerLineByte, encode.name);
145+
140146
// jump <name="> and get string inside double quote => "string"
141147
let nameByte = headerLineByte.slice(
142148
nameIndex + encode.name.byteLength + 2,
143149
headerLineByte.byteLength - 1,
144150
);
151+
145152
return decoder.decode(nameByte);
146153
}
147154

@@ -166,6 +173,7 @@ function getFieldPieces(
166173
// jump over boundary + '\r\n'
167174
buf = buf.slice(startBoundaryByte.byteLength + 2);
168175
let boundaryIndex = bytes.findIndex(buf, startBoundaryByte);
176+
169177
// get field content piece
170178
pieces.push(buf.slice(0, boundaryIndex - 1));
171179
buf = buf.slice(boundaryIndex);
@@ -177,6 +185,7 @@ function getFieldPieces(
177185
function getBoundary(contentType: string): Uint8Array | undefined {
178186
let contentTypeByte = encoder.encode(contentType);
179187
let boundaryIndex = bytes.findIndex(contentTypeByte, encode.boundaryEqual);
188+
180189
if (boundaryIndex >= 0) {
181190
// jump over 'boundary=' to get the real boundary
182191
let boundary = contentTypeByte.slice(

0 commit comments

Comments
 (0)