FormDataEncoder.js 8.1 KB

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