2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-04-14 16:49:16 -04:00
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../webidl/internal.d.ts" />
|
|
|
|
/// <reference path="../web/internal.d.ts" />
|
2021-06-10 09:26:10 -04:00
|
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
2021-04-14 16:49:16 -04:00
|
|
|
/// <reference path="./internal.d.ts" />
|
2021-06-14 07:51:02 -04:00
|
|
|
/// <reference path="../web/06_streams_types.d.ts" />
|
2021-04-14 16:49:16 -04:00
|
|
|
/// <reference path="./lib.deno_fetch.d.ts" />
|
|
|
|
/// <reference lib="esnext" />
|
|
|
|
"use strict";
|
|
|
|
|
2021-06-05 17:10:07 -04:00
|
|
|
((window) => {
|
|
|
|
const core = window.Deno.core;
|
2021-04-14 16:49:16 -04:00
|
|
|
const webidl = globalThis.__bootstrap.webidl;
|
2022-02-01 12:06:11 -05:00
|
|
|
const { Blob, BlobPrototype, File, FilePrototype } =
|
|
|
|
globalThis.__bootstrap.file;
|
2021-07-06 05:32:59 -04:00
|
|
|
const {
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
ArrayPrototypeSplice,
|
|
|
|
Map,
|
|
|
|
MapPrototypeGet,
|
|
|
|
MapPrototypeSet,
|
|
|
|
MathRandom,
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2021-07-06 05:32:59 -04:00
|
|
|
Symbol,
|
|
|
|
StringFromCharCode,
|
|
|
|
StringPrototypeTrim,
|
|
|
|
StringPrototypeSlice,
|
|
|
|
StringPrototypeSplit,
|
|
|
|
StringPrototypeReplace,
|
|
|
|
StringPrototypeIndexOf,
|
|
|
|
StringPrototypePadStart,
|
|
|
|
StringPrototypeCodePointAt,
|
|
|
|
StringPrototypeReplaceAll,
|
|
|
|
TypeError,
|
|
|
|
TypedArrayPrototypeSubarray,
|
|
|
|
} = window.__bootstrap.primordials;
|
2021-04-14 16:49:16 -04:00
|
|
|
|
|
|
|
const entryList = Symbol("entry list");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string | Blob} value
|
|
|
|
* @param {string | undefined} filename
|
|
|
|
* @returns {FormDataEntry}
|
|
|
|
*/
|
|
|
|
function createEntry(name, value, filename) {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BlobPrototype, value) &&
|
|
|
|
!ObjectPrototypeIsPrototypeOf(FilePrototype, value)
|
|
|
|
) {
|
2021-06-22 08:06:37 -04:00
|
|
|
value = new File([value], "blob", { type: value.type });
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(FilePrototype, value) &&
|
|
|
|
filename !== undefined
|
|
|
|
) {
|
2021-06-22 08:06:37 -04:00
|
|
|
value = new File([value], filename, {
|
2021-04-14 16:49:16 -04:00
|
|
|
type: value.type,
|
|
|
|
lastModified: value.lastModified,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
// @ts-expect-error because TS is not smart enough
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef FormDataEntry
|
|
|
|
* @property {string} name
|
|
|
|
* @property {FormDataEntryValue} value
|
|
|
|
*/
|
|
|
|
|
|
|
|
class FormData {
|
|
|
|
/** @type {FormDataEntry[]} */
|
|
|
|
[entryList] = [];
|
|
|
|
|
|
|
|
/** @param {void} form */
|
|
|
|
constructor(form) {
|
|
|
|
if (form !== undefined) {
|
|
|
|
webidl.illegalConstructor();
|
|
|
|
}
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-28 10:08:51 -04:00
|
|
|
* @param {string} name
|
|
|
|
* @param {string | Blob} valueOrBlobValue
|
2021-04-14 16:49:16 -04:00
|
|
|
* @param {string} [filename]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
append(name, valueOrBlobValue, filename) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, FormDataPrototype);
|
2021-04-14 16:49:16 -04:00
|
|
|
const prefix = "Failed to execute 'append' on 'FormData'";
|
|
|
|
webidl.requiredArguments(arguments.length, 2, { prefix });
|
|
|
|
|
|
|
|
name = webidl.converters["USVString"](name, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
2022-02-01 12:06:11 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(BlobPrototype, valueOrBlobValue)) {
|
2021-04-14 16:49:16 -04:00
|
|
|
valueOrBlobValue = webidl.converters["Blob"](valueOrBlobValue, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
|
|
|
if (filename !== undefined) {
|
|
|
|
filename = webidl.converters["USVString"](filename, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 3",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valueOrBlobValue = webidl.converters["USVString"](valueOrBlobValue, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const entry = createEntry(name, valueOrBlobValue, filename);
|
|
|
|
|
2021-07-06 05:32:59 -04:00
|
|
|
ArrayPrototypePush(this[entryList], entry);
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-28 10:08:51 -04:00
|
|
|
* @param {string} name
|
2021-04-14 16:49:16 -04:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
delete(name) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, FormDataPrototype);
|
2021-04-14 16:49:16 -04:00
|
|
|
const prefix = "Failed to execute 'name' on 'FormData'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
|
|
|
|
name = webidl.converters["USVString"](name, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
|
|
|
const list = this[entryList];
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
if (list[i].name === name) {
|
2021-07-06 05:32:59 -04:00
|
|
|
ArrayPrototypeSplice(list, i, 1);
|
2021-04-14 16:49:16 -04:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-28 10:08:51 -04:00
|
|
|
* @param {string} name
|
2021-04-14 16:49:16 -04:00
|
|
|
* @returns {FormDataEntryValue | null}
|
|
|
|
*/
|
|
|
|
get(name) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, FormDataPrototype);
|
2021-04-14 16:49:16 -04:00
|
|
|
const prefix = "Failed to execute 'get' on 'FormData'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
|
|
|
|
name = webidl.converters["USVString"](name, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const entry of this[entryList]) {
|
|
|
|
if (entry.name === name) return entry.value;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-28 10:08:51 -04:00
|
|
|
* @param {string} name
|
2021-04-14 16:49:16 -04:00
|
|
|
* @returns {FormDataEntryValue[]}
|
|
|
|
*/
|
|
|
|
getAll(name) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, FormDataPrototype);
|
2021-04-14 16:49:16 -04:00
|
|
|
const prefix = "Failed to execute 'getAll' on 'FormData'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
|
|
|
|
name = webidl.converters["USVString"](name, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
|
|
|
const returnList = [];
|
|
|
|
for (const entry of this[entryList]) {
|
2021-07-06 05:32:59 -04:00
|
|
|
if (entry.name === name) ArrayPrototypePush(returnList, entry.value);
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
|
|
|
return returnList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-28 10:08:51 -04:00
|
|
|
* @param {string} name
|
2021-04-14 16:49:16 -04:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
has(name) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, FormDataPrototype);
|
2021-04-14 16:49:16 -04:00
|
|
|
const prefix = "Failed to execute 'has' on 'FormData'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
|
|
|
|
name = webidl.converters["USVString"](name, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const entry of this[entryList]) {
|
|
|
|
if (entry.name === name) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-28 10:08:51 -04:00
|
|
|
* @param {string} name
|
|
|
|
* @param {string | Blob} valueOrBlobValue
|
2021-04-14 16:49:16 -04:00
|
|
|
* @param {string} [filename]
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
set(name, valueOrBlobValue, filename) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, FormDataPrototype);
|
2021-04-14 16:49:16 -04:00
|
|
|
const prefix = "Failed to execute 'set' on 'FormData'";
|
|
|
|
webidl.requiredArguments(arguments.length, 2, { prefix });
|
|
|
|
|
|
|
|
name = webidl.converters["USVString"](name, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
2022-02-01 12:06:11 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(BlobPrototype, valueOrBlobValue)) {
|
2021-04-14 16:49:16 -04:00
|
|
|
valueOrBlobValue = webidl.converters["Blob"](valueOrBlobValue, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
|
|
|
if (filename !== undefined) {
|
|
|
|
filename = webidl.converters["USVString"](filename, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 3",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valueOrBlobValue = webidl.converters["USVString"](valueOrBlobValue, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const entry = createEntry(name, valueOrBlobValue, filename);
|
|
|
|
|
|
|
|
const list = this[entryList];
|
|
|
|
let added = false;
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
if (list[i].name === name) {
|
|
|
|
if (!added) {
|
|
|
|
list[i] = entry;
|
|
|
|
added = true;
|
|
|
|
} else {
|
2021-07-06 05:32:59 -04:00
|
|
|
ArrayPrototypeSplice(list, i, 1);
|
2021-04-14 16:49:16 -04:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!added) {
|
2021-07-06 05:32:59 -04:00
|
|
|
ArrayPrototypePush(list, entry);
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
webidl.mixinPairIterable("FormData", FormData, entryList, "name", "value");
|
|
|
|
|
2021-06-07 04:04:10 -04:00
|
|
|
webidl.configurePrototype(FormData);
|
2022-02-01 12:06:11 -05:00
|
|
|
const FormDataPrototype = FormData.prototype;
|
2021-06-07 04:04:10 -04:00
|
|
|
|
2022-07-09 14:28:02 -04:00
|
|
|
const escape = (str, isFilename) => {
|
|
|
|
const escapeMap = {
|
|
|
|
"\n": "%0A",
|
|
|
|
"\r": "%0D",
|
|
|
|
'"': "%22",
|
|
|
|
};
|
|
|
|
|
|
|
|
return StringPrototypeReplace(
|
|
|
|
isFilename ? str : StringPrototypeReplace(str, /\r?\n|\r/g, "\r\n"),
|
|
|
|
/([\n\r"])/g,
|
|
|
|
(c) => escapeMap[c],
|
2021-07-06 05:32:59 -04:00
|
|
|
);
|
2022-07-09 14:28:02 -04:00
|
|
|
};
|
2021-04-14 16:49:16 -04:00
|
|
|
|
2021-06-22 08:06:37 -04:00
|
|
|
/**
|
|
|
|
* convert FormData to a Blob synchronous without reading all of the files
|
|
|
|
* @param {globalThis.FormData} formData
|
|
|
|
*/
|
|
|
|
function formDataToBlob(formData) {
|
2021-07-06 05:32:59 -04:00
|
|
|
const boundary = StringPrototypePadStart(
|
|
|
|
StringPrototypeSlice(
|
|
|
|
StringPrototypeReplaceAll(`${MathRandom()}${MathRandom()}`, ".", ""),
|
|
|
|
-28,
|
|
|
|
),
|
|
|
|
32,
|
|
|
|
"-",
|
|
|
|
);
|
2021-06-22 08:06:37 -04:00
|
|
|
const chunks = [];
|
|
|
|
const prefix = `--${boundary}\r\nContent-Disposition: form-data; name="`;
|
|
|
|
|
|
|
|
for (const [name, value] of formData) {
|
|
|
|
if (typeof value === "string") {
|
2021-07-06 05:32:59 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
chunks,
|
2021-06-22 08:06:37 -04:00
|
|
|
prefix + escape(name) + '"' + CRLF + CRLF +
|
2021-07-06 05:32:59 -04:00
|
|
|
StringPrototypeReplace(value, /\r(?!\n)|(?<!\r)\n/g, CRLF) + CRLF,
|
2021-06-22 08:06:37 -04:00
|
|
|
);
|
|
|
|
} else {
|
2021-07-06 05:32:59 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
chunks,
|
2021-06-22 08:06:37 -04:00
|
|
|
prefix + escape(name) + `"; filename="${escape(value.name, true)}"` +
|
|
|
|
CRLF +
|
|
|
|
`Content-Type: ${value.type || "application/octet-stream"}\r\n\r\n`,
|
|
|
|
value,
|
|
|
|
CRLF,
|
|
|
|
);
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
2021-05-27 19:33:11 -04:00
|
|
|
}
|
2021-04-14 16:49:16 -04:00
|
|
|
|
2021-07-06 05:32:59 -04:00
|
|
|
ArrayPrototypePush(chunks, `--${boundary}--`);
|
2021-04-14 16:49:16 -04:00
|
|
|
|
2021-06-22 08:06:37 -04:00
|
|
|
return new Blob(chunks, {
|
|
|
|
type: "multipart/form-data; boundary=" + boundary,
|
|
|
|
});
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} value
|
|
|
|
* @returns {Map<string, string>}
|
|
|
|
*/
|
|
|
|
function parseContentDisposition(value) {
|
|
|
|
/** @type {Map<string, string>} */
|
|
|
|
const params = new Map();
|
|
|
|
// Forced to do so for some Map constructor param mismatch
|
2022-10-05 12:31:36 -04:00
|
|
|
const values = ArrayPrototypeSlice(StringPrototypeSplit(value, ";"), 1);
|
|
|
|
for (let i = 0; i < values.length; i++) {
|
|
|
|
const entries = StringPrototypeSplit(StringPrototypeTrim(values[i]), "=");
|
|
|
|
if (entries.length > 1) {
|
|
|
|
MapPrototypeSet(
|
|
|
|
params,
|
|
|
|
entries[0],
|
|
|
|
StringPrototypeReplace(entries[1], /^"([^"]*)"$/, "$1"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 16:49:16 -04:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2021-06-22 08:06:37 -04:00
|
|
|
const CRLF = "\r\n";
|
2021-07-06 05:32:59 -04:00
|
|
|
const LF = StringPrototypeCodePointAt(CRLF, 1);
|
|
|
|
const CR = StringPrototypeCodePointAt(CRLF, 0);
|
2021-04-14 16:49:16 -04:00
|
|
|
|
|
|
|
class MultipartParser {
|
|
|
|
/**
|
2021-04-28 10:08:51 -04:00
|
|
|
* @param {Uint8Array} body
|
|
|
|
* @param {string | undefined} boundary
|
2021-04-14 16:49:16 -04:00
|
|
|
*/
|
|
|
|
constructor(body, boundary) {
|
|
|
|
if (!boundary) {
|
|
|
|
throw new TypeError("multipart/form-data must provide a boundary");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.boundary = `--${boundary}`;
|
|
|
|
this.body = body;
|
2021-06-05 17:10:07 -04:00
|
|
|
this.boundaryChars = core.encode(this.boundary);
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} headersText
|
|
|
|
* @returns {{ headers: Headers, disposition: Map<string, string> }}
|
|
|
|
*/
|
2021-05-27 19:33:11 -04:00
|
|
|
#parseHeaders(headersText) {
|
2021-04-14 16:49:16 -04:00
|
|
|
const headers = new Headers();
|
2021-07-06 05:32:59 -04:00
|
|
|
const rawHeaders = StringPrototypeSplit(headersText, "\r\n");
|
2021-04-14 16:49:16 -04:00
|
|
|
for (const rawHeader of rawHeaders) {
|
2021-07-06 05:32:59 -04:00
|
|
|
const sepIndex = StringPrototypeIndexOf(rawHeader, ":");
|
2021-04-14 16:49:16 -04:00
|
|
|
if (sepIndex < 0) {
|
|
|
|
continue; // Skip this header
|
|
|
|
}
|
2021-07-06 05:32:59 -04:00
|
|
|
const key = StringPrototypeSlice(rawHeader, 0, sepIndex);
|
|
|
|
const value = StringPrototypeSlice(rawHeader, sepIndex + 1);
|
2021-04-14 16:49:16 -04:00
|
|
|
headers.set(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const disposition = parseContentDisposition(
|
|
|
|
headers.get("Content-Disposition") ?? "",
|
|
|
|
);
|
|
|
|
|
|
|
|
return { headers, disposition };
|
2021-05-27 19:33:11 -04:00
|
|
|
}
|
2021-04-14 16:49:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {FormData}
|
|
|
|
*/
|
|
|
|
parse() {
|
2022-10-07 07:59:30 -04:00
|
|
|
// To have fields body must be at least 2 boundaries + \r\n + --
|
|
|
|
// on the last boundary.
|
2021-04-20 08:47:22 -04:00
|
|
|
if (this.body.length < (this.boundary.length * 2) + 4) {
|
2022-10-07 07:59:30 -04:00
|
|
|
const decodedBody = core.decode(this.body);
|
|
|
|
const lastBoundary = this.boundary + "--";
|
|
|
|
// check if it's an empty valid form data
|
|
|
|
if (
|
|
|
|
decodedBody === lastBoundary ||
|
|
|
|
decodedBody === lastBoundary + "\r\n"
|
|
|
|
) {
|
|
|
|
return new FormData();
|
|
|
|
}
|
|
|
|
throw new TypeError("Unable to parse body as form data.");
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
|
|
|
|
2021-04-14 16:49:16 -04:00
|
|
|
const formData = new FormData();
|
|
|
|
let headerText = "";
|
|
|
|
let boundaryIndex = 0;
|
|
|
|
let state = 0;
|
|
|
|
let fileStart = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < this.body.length; i++) {
|
|
|
|
const byte = this.body[i];
|
|
|
|
const prevByte = this.body[i - 1];
|
|
|
|
const isNewLine = byte === LF && prevByte === CR;
|
|
|
|
|
|
|
|
if (state === 1 || state === 2 || state == 3) {
|
2021-07-06 05:32:59 -04:00
|
|
|
headerText += StringFromCharCode(byte);
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
|
|
|
if (state === 0 && isNewLine) {
|
|
|
|
state = 1;
|
|
|
|
} else if (state === 1 && isNewLine) {
|
|
|
|
state = 2;
|
|
|
|
const headersDone = this.body[i + 1] === CR &&
|
|
|
|
this.body[i + 2] === LF;
|
|
|
|
|
|
|
|
if (headersDone) {
|
|
|
|
state = 3;
|
|
|
|
}
|
|
|
|
} else if (state === 2 && isNewLine) {
|
|
|
|
state = 3;
|
|
|
|
} else if (state === 3 && isNewLine) {
|
|
|
|
state = 4;
|
|
|
|
fileStart = i + 1;
|
|
|
|
} else if (state === 4) {
|
|
|
|
if (this.boundaryChars[boundaryIndex] !== byte) {
|
|
|
|
boundaryIndex = 0;
|
|
|
|
} else {
|
|
|
|
boundaryIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boundaryIndex >= this.boundary.length) {
|
|
|
|
const { headers, disposition } = this.#parseHeaders(headerText);
|
2021-07-06 05:32:59 -04:00
|
|
|
const content = TypedArrayPrototypeSubarray(
|
|
|
|
this.body,
|
2021-04-14 16:49:16 -04:00
|
|
|
fileStart,
|
|
|
|
i - boundaryIndex - 1,
|
|
|
|
);
|
|
|
|
// https://fetch.spec.whatwg.org/#ref-for-dom-body-formdata
|
2021-07-06 05:32:59 -04:00
|
|
|
const filename = MapPrototypeGet(disposition, "filename");
|
|
|
|
const name = MapPrototypeGet(disposition, "name");
|
2021-04-14 16:49:16 -04:00
|
|
|
|
|
|
|
state = 5;
|
|
|
|
// Reset
|
|
|
|
boundaryIndex = 0;
|
|
|
|
headerText = "";
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
continue; // Skip, unknown name
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filename) {
|
|
|
|
const blob = new Blob([content], {
|
|
|
|
type: headers.get("Content-Type") || "application/octet-stream",
|
|
|
|
});
|
|
|
|
formData.append(name, blob, filename);
|
|
|
|
} else {
|
2021-06-05 17:10:07 -04:00
|
|
|
formData.append(name, core.decode(content));
|
2021-04-14 16:49:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (state === 5 && isNewLine) {
|
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-28 10:08:51 -04:00
|
|
|
* @param {Uint8Array} body
|
2021-04-14 16:49:16 -04:00
|
|
|
* @param {string | undefined} boundary
|
|
|
|
* @returns {FormData}
|
|
|
|
*/
|
|
|
|
function parseFormData(body, boundary) {
|
|
|
|
const parser = new MultipartParser(body, boundary);
|
|
|
|
return parser.parse();
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
/**
|
|
|
|
* @param {FormDataEntry[]} entries
|
2021-04-28 10:08:51 -04:00
|
|
|
* @returns {FormData}
|
2021-04-20 08:47:22 -04:00
|
|
|
*/
|
|
|
|
function formDataFromEntries(entries) {
|
|
|
|
const fd = new FormData();
|
|
|
|
fd[entryList] = entries;
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
webidl.converters["FormData"] = webidl
|
2022-02-01 12:06:11 -05:00
|
|
|
.createInterfaceConverter("FormData", FormDataPrototype);
|
2021-04-20 08:47:22 -04:00
|
|
|
|
|
|
|
globalThis.__bootstrap.formData = {
|
|
|
|
FormData,
|
2022-02-01 12:06:11 -05:00
|
|
|
FormDataPrototype,
|
2021-06-22 08:06:37 -04:00
|
|
|
formDataToBlob,
|
2021-04-20 08:47:22 -04:00
|
|
|
parseFormData,
|
|
|
|
formDataFromEntries,
|
|
|
|
};
|
2021-04-14 16:49:16 -04:00
|
|
|
})(globalThis);
|