MCPcopy Create free account
hub / github.com/nodejs/node / multipartFormDataParser

Function multipartFormDataParser

deps/undici/undici.js:6369–6430  ·  view source on GitHub ↗
(input, mimeType)

Source from the content-addressed store, hash-verified

6367 }
6368 __name(validateBoundary, "validateBoundary");
6369 function multipartFormDataParser(input, mimeType) {
6370 assert(mimeType !== "failure" && mimeType.essence === "multipart/form-data");
6371 const boundaryString = mimeType.parameters.get("boundary");
6372 if (boundaryString === void 0) {
6373 throw parsingError("missing boundary in content-type header");
6374 }
6375 const boundary = Buffer.from(`--${boundaryString}`, "utf8");
6376 const entryList = [];
6377 const position = { position: 0 };
6378 const firstBoundaryIndex = input.indexOf(boundary);
6379 if (firstBoundaryIndex === -1) {
6380 throw parsingError("no boundary found in multipart body");
6381 }
6382 position.position = firstBoundaryIndex;
6383 while (true) {
6384 if (input.subarray(position.position, position.position + boundary.length).equals(boundary)) {
6385 position.position += boundary.length;
6386 } else {
6387 throw parsingError("expected a value starting with -- and the boundary");
6388 }
6389 if (bufferStartsWith(input, dd, position)) {
6390 return entryList;
6391 }
6392 if (input[position.position] !== 13 || input[position.position + 1] !== 10) {
6393 throw parsingError("expected CRLF");
6394 }
6395 position.position += 2;
6396 const result = parseMultipartFormDataHeaders(input, position);
6397 let { name, filename, contentType, encoding } = result;
6398 position.position += 2;
6399 let body;
6400 {
6401 const boundaryIndex = input.indexOf(boundary.subarray(2), position.position);
6402 if (boundaryIndex === -1) {
6403 throw parsingError("expected boundary after body");
6404 }
6405 body = input.subarray(position.position, boundaryIndex - 4);
6406 position.position += body.length;
6407 if (encoding === "base64") {
6408 body = Buffer.from(body.toString(), "base64");
6409 }
6410 }
6411 if (input[position.position] !== 13 || input[position.position + 1] !== 10) {
6412 throw parsingError("expected CRLF");
6413 } else {
6414 position.position += 2;
6415 }
6416 let value;
6417 if (filename !== null) {
6418 contentType ??= "text/plain";
6419 if (!isAsciiString(contentType)) {
6420 contentType = "";
6421 }
6422 value = new File([body], filename, { type: contentType });
6423 } else {
6424 value = decoderIgnoreBOM.decode(Buffer.from(body));
6425 }
6426 assert(webidl.is.USVString(name));

Callers 1

formDataFunction · 0.70

Calls 14

USVStringMethod · 0.80
parsingErrorFunction · 0.70
bufferStartsWithFunction · 0.70
isAsciiStringFunction · 0.70
makeEntryFunction · 0.70
getMethod · 0.65
decodeMethod · 0.65
assertFunction · 0.50
fromMethod · 0.45
indexOfMethod · 0.45
equalsMethod · 0.45

Tested by

no test coverage detected