hexo/node_modules/form-data-encoder/lib/esm/FormDataEncoder.js

124 lines
7.6 KiB
JavaScript
Raw Normal View History

2023-09-25 15:58:56 +08:00
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _FormDataEncoder_instances, _FormDataEncoder_CRLF, _FormDataEncoder_CRLF_BYTES, _FormDataEncoder_CRLF_BYTES_LENGTH, _FormDataEncoder_DASHES, _FormDataEncoder_encoder, _FormDataEncoder_footer, _FormDataEncoder_form, _FormDataEncoder_options, _FormDataEncoder_getFieldHeader;
import getStreamIterator from "./util/getStreamIterator.js";
import createBoundary from "./util/createBoundary.js";
import isPlainObject from "./util/isPlainObject.js";
import normalize from "./util/normalizeValue.js";
import escape from "./util/escapeName.js";
import { isFileLike } from "./util/isFileLike.js";
import { isFormData } from "./util/isFormData.js";
const defaultOptions = {
enableAdditionalHeaders: false
};
export class FormDataEncoder {
constructor(form, boundaryOrOptions, options) {
_FormDataEncoder_instances.add(this);
_FormDataEncoder_CRLF.set(this, "\r\n");
_FormDataEncoder_CRLF_BYTES.set(this, void 0);
_FormDataEncoder_CRLF_BYTES_LENGTH.set(this, void 0);
_FormDataEncoder_DASHES.set(this, "-".repeat(2));
_FormDataEncoder_encoder.set(this, new TextEncoder());
_FormDataEncoder_footer.set(this, void 0);
_FormDataEncoder_form.set(this, void 0);
_FormDataEncoder_options.set(this, void 0);
if (!isFormData(form)) {
throw new TypeError("Expected first argument to be a FormData instance.");
}
let boundary;
if (isPlainObject(boundaryOrOptions)) {
options = boundaryOrOptions;
}
else {
boundary = boundaryOrOptions;
}
if (!boundary) {
boundary = createBoundary();
}
if (typeof boundary !== "string") {
throw new TypeError("Expected boundary argument to be a string.");
}
if (options && !isPlainObject(options)) {
throw new TypeError("Expected options argument to be an object.");
}
__classPrivateFieldSet(this, _FormDataEncoder_form, form, "f");
__classPrivateFieldSet(this, _FormDataEncoder_options, { ...defaultOptions, ...options }, "f");
__classPrivateFieldSet(this, _FormDataEncoder_CRLF_BYTES, __classPrivateFieldGet(this, _FormDataEncoder_encoder, "f").encode(__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f")), "f");
__classPrivateFieldSet(this, _FormDataEncoder_CRLF_BYTES_LENGTH, __classPrivateFieldGet(this, _FormDataEncoder_CRLF_BYTES, "f").byteLength, "f");
this.boundary = `form-data-boundary-${boundary}`;
this.contentType = `multipart/form-data; boundary=${this.boundary}`;
__classPrivateFieldSet(this, _FormDataEncoder_footer, __classPrivateFieldGet(this, _FormDataEncoder_encoder, "f").encode(`${__classPrivateFieldGet(this, _FormDataEncoder_DASHES, "f")}${this.boundary}${__classPrivateFieldGet(this, _FormDataEncoder_DASHES, "f")}${__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f").repeat(2)}`), "f");
this.contentLength = String(this.getContentLength());
this.headers = Object.freeze({
"Content-Length": this.contentLength,
"Content-Type": this.contentType
});
Object.defineProperties(this, {
boundary: { writable: false, configurable: false },
contentType: { writable: false, configurable: false },
contentLength: { writable: false, configurable: false },
headers: { writable: false, configurable: false }
});
}
getContentLength() {
let length = 0;
for (const [name, raw] of __classPrivateFieldGet(this, _FormDataEncoder_form, "f")) {
const value = isFileLike(raw) ? raw : __classPrivateFieldGet(this, _FormDataEncoder_encoder, "f").encode(normalize(raw));
length += __classPrivateFieldGet(this, _FormDataEncoder_instances, "m", _FormDataEncoder_getFieldHeader).call(this, name, value).byteLength;
length += isFileLike(value) ? value.size : value.byteLength;
length += __classPrivateFieldGet(this, _FormDataEncoder_CRLF_BYTES_LENGTH, "f");
}
return length + __classPrivateFieldGet(this, _FormDataEncoder_footer, "f").byteLength;
}
*values() {
for (const [name, raw] of __classPrivateFieldGet(this, _FormDataEncoder_form, "f").entries()) {
const value = isFileLike(raw) ? raw : __classPrivateFieldGet(this, _FormDataEncoder_encoder, "f").encode(normalize(raw));
yield __classPrivateFieldGet(this, _FormDataEncoder_instances, "m", _FormDataEncoder_getFieldHeader).call(this, name, value);
yield value;
yield __classPrivateFieldGet(this, _FormDataEncoder_CRLF_BYTES, "f");
}
yield __classPrivateFieldGet(this, _FormDataEncoder_footer, "f");
}
async *encode() {
for (const part of this.values()) {
if (isFileLike(part)) {
yield* getStreamIterator(part.stream());
}
else {
yield part;
}
}
}
[(_FormDataEncoder_CRLF = new WeakMap(), _FormDataEncoder_CRLF_BYTES = new WeakMap(), _FormDataEncoder_CRLF_BYTES_LENGTH = new WeakMap(), _FormDataEncoder_DASHES = new WeakMap(), _FormDataEncoder_encoder = new WeakMap(), _FormDataEncoder_footer = new WeakMap(), _FormDataEncoder_form = new WeakMap(), _FormDataEncoder_options = new WeakMap(), _FormDataEncoder_instances = new WeakSet(), _FormDataEncoder_getFieldHeader = function _FormDataEncoder_getFieldHeader(name, value) {
let header = "";
header += `${__classPrivateFieldGet(this, _FormDataEncoder_DASHES, "f")}${this.boundary}${__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f")}`;
header += `Content-Disposition: form-data; name="${escape(name)}"`;
if (isFileLike(value)) {
header += `; filename="${escape(value.name)}"${__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f")}`;
header += `Content-Type: ${value.type || "application/octet-stream"}`;
}
if (__classPrivateFieldGet(this, _FormDataEncoder_options, "f").enableAdditionalHeaders === true) {
const size = isFileLike(value) ? value.size : value.byteLength;
if (size != null && !isNaN(size)) {
header += `${__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f")}Content-Length: ${size}`;
}
}
return __classPrivateFieldGet(this, _FormDataEncoder_encoder, "f").encode(`${header}${__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f").repeat(2)}`);
}, Symbol.iterator)]() {
return this.values();
}
[Symbol.asyncIterator]() {
return this.encode();
}
}
export const Encoder = FormDataEncoder;