FormDataEncoder.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  2. if (kind === "m") throw new TypeError("Private method is not writable");
  3. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  4. 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");
  5. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  6. };
  7. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  8. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  9. 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");
  10. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  11. };
  12. var _FormDataEncoder_instances, _FormDataEncoder_CRLF, _FormDataEncoder_CRLF_BYTES, _FormDataEncoder_CRLF_BYTES_LENGTH, _FormDataEncoder_DASHES, _FormDataEncoder_encoder, _FormDataEncoder_footer, _FormDataEncoder_form, _FormDataEncoder_options, _FormDataEncoder_getFieldHeader;
  13. import createBoundary from "./util/createBoundary.js";
  14. import isPlainObject from "./util/isPlainObject.js";
  15. import normalize from "./util/normalizeValue.js";
  16. import escape from "./util/escapeName.js";
  17. import { isFileLike } from "./util/isFileLike.js";
  18. import { isFormData } from "./util/isFormData.js";
  19. const defaultOptions = {
  20. enableAdditionalHeaders: false
  21. };
  22. export class FormDataEncoder {
  23. constructor(form, boundaryOrOptions, options) {
  24. _FormDataEncoder_instances.add(this);
  25. _FormDataEncoder_CRLF.set(this, "\r\n");
  26. _FormDataEncoder_CRLF_BYTES.set(this, void 0);
  27. _FormDataEncoder_CRLF_BYTES_LENGTH.set(this, void 0);
  28. _FormDataEncoder_DASHES.set(this, "-".repeat(2));
  29. _FormDataEncoder_encoder.set(this, new TextEncoder());
  30. _FormDataEncoder_footer.set(this, void 0);
  31. _FormDataEncoder_form.set(this, void 0);
  32. _FormDataEncoder_options.set(this, void 0);
  33. if (!isFormData(form)) {
  34. throw new TypeError("Expected first argument to be a FormData instance.");
  35. }
  36. let boundary;
  37. if (isPlainObject(boundaryOrOptions)) {
  38. options = boundaryOrOptions;
  39. }
  40. else {
  41. boundary = boundaryOrOptions;
  42. }
  43. if (!boundary) {
  44. boundary = createBoundary();
  45. }
  46. if (typeof boundary !== "string") {
  47. throw new TypeError("Expected boundary argument to be a string.");
  48. }
  49. if (options && !isPlainObject(options)) {
  50. throw new TypeError("Expected options argument to be an object.");
  51. }
  52. __classPrivateFieldSet(this, _FormDataEncoder_form, form, "f");
  53. __classPrivateFieldSet(this, _FormDataEncoder_options, { ...defaultOptions, ...options }, "f");
  54. __classPrivateFieldSet(this, _FormDataEncoder_CRLF_BYTES, __classPrivateFieldGet(this, _FormDataEncoder_encoder, "f").encode(__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f")), "f");
  55. __classPrivateFieldSet(this, _FormDataEncoder_CRLF_BYTES_LENGTH, __classPrivateFieldGet(this, _FormDataEncoder_CRLF_BYTES, "f").byteLength, "f");
  56. this.boundary = `form-data-boundary-${boundary}`;
  57. this.contentType = `multipart/form-data; boundary=${this.boundary}`;
  58. __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");
  59. this.contentLength = String(this.getContentLength());
  60. this.headers = Object.freeze({
  61. "Content-Type": this.contentType,
  62. "Content-Length": this.contentLength
  63. });
  64. Object.defineProperties(this, {
  65. boundary: { writable: false, configurable: false },
  66. contentType: { writable: false, configurable: false },
  67. contentLength: { writable: false, configurable: false },
  68. headers: { writable: false, configurable: false }
  69. });
  70. }
  71. getContentLength() {
  72. let length = 0;
  73. for (const [name, raw] of __classPrivateFieldGet(this, _FormDataEncoder_form, "f")) {
  74. const value = isFileLike(raw) ? raw : __classPrivateFieldGet(this, _FormDataEncoder_encoder, "f").encode(normalize(raw));
  75. length += __classPrivateFieldGet(this, _FormDataEncoder_instances, "m", _FormDataEncoder_getFieldHeader).call(this, name, value).byteLength;
  76. length += isFileLike(value) ? value.size : value.byteLength;
  77. length += __classPrivateFieldGet(this, _FormDataEncoder_CRLF_BYTES_LENGTH, "f");
  78. }
  79. return length + __classPrivateFieldGet(this, _FormDataEncoder_footer, "f").byteLength;
  80. }
  81. *values() {
  82. for (const [name, raw] of __classPrivateFieldGet(this, _FormDataEncoder_form, "f").entries()) {
  83. const value = isFileLike(raw) ? raw : __classPrivateFieldGet(this, _FormDataEncoder_encoder, "f").encode(normalize(raw));
  84. yield __classPrivateFieldGet(this, _FormDataEncoder_instances, "m", _FormDataEncoder_getFieldHeader).call(this, name, value);
  85. yield value;
  86. yield __classPrivateFieldGet(this, _FormDataEncoder_CRLF_BYTES, "f");
  87. }
  88. yield __classPrivateFieldGet(this, _FormDataEncoder_footer, "f");
  89. }
  90. async *encode() {
  91. for (const part of this.values()) {
  92. if (isFileLike(part)) {
  93. yield* part.stream();
  94. }
  95. else {
  96. yield part;
  97. }
  98. }
  99. }
  100. [(_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) {
  101. let header = "";
  102. header += `${__classPrivateFieldGet(this, _FormDataEncoder_DASHES, "f")}${this.boundary}${__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f")}`;
  103. header += `Content-Disposition: form-data; name="${escape(name)}"`;
  104. if (isFileLike(value)) {
  105. header += `; filename="${escape(value.name)}"${__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f")}`;
  106. header += `Content-Type: ${value.type || "application/octet-stream"}`;
  107. }
  108. if (__classPrivateFieldGet(this, _FormDataEncoder_options, "f").enableAdditionalHeaders === true) {
  109. header += `${__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f")}Content-Length: ${isFileLike(value) ? value.size : value.byteLength}`;
  110. }
  111. return __classPrivateFieldGet(this, _FormDataEncoder_encoder, "f").encode(`${header}${__classPrivateFieldGet(this, _FormDataEncoder_CRLF, "f").repeat(2)}`);
  112. }, Symbol.iterator)]() {
  113. return this.values();
  114. }
  115. [Symbol.asyncIterator]() {
  116. return this.encode();
  117. }
  118. }
  119. export const Encoder = FormDataEncoder;