wao 0.27.0 → 0.27.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/encode.js CHANGED
@@ -7,106 +7,28 @@ exports.enc = enc;
7
7
  var _base64url = _interopRequireDefault(require("base64url"));
8
8
  var _fastSha = require("fast-sha256");
9
9
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
10
- function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
11
10
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
12
- function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
13
- function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
11
+ function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
12
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
13
+ function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
14
+ function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
15
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
14
16
  function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
15
17
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
16
18
  function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
17
19
  function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
18
20
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
19
21
  function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
22
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
23
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
20
24
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
21
25
  function isBytes(value) {
22
- return value instanceof ArrayBuffer || ArrayBuffer.isView(value) || Buffer.isBuffer(value);
26
+ return value instanceof ArrayBuffer || ArrayBuffer.isView(value) || Buffer.isBuffer(value) || value && _typeof(value) === "object" && value.type === "Buffer" && Array.isArray(value.data);
23
27
  }
24
28
  function isPojo(value) {
25
29
  return !isBytes(value) && !Array.isArray(value) && !(value instanceof Blob) && _typeof(value) === "object" && value !== null;
26
30
  }
27
- function hbEncodeValue(value) {
28
- if (isBytes(value)) {
29
- var length = value.byteLength || value.length || 0;
30
- if (length === 0) return ["empty-binary", ""];
31
- return [undefined, value];
32
- }
33
- if (typeof value === "string") {
34
- if (value.length === 0) return ["empty-string", ""];
35
- return [undefined, value];
36
- }
37
- if (Array.isArray(value)) {
38
- if (value.length === 0) return ["empty-list", "[]"];
39
- var hasObjects = value.some(function (item) {
40
- return isPojo(item);
41
- });
42
- if (hasObjects) {
43
- return ["list_with_objects", value];
44
- }
45
- var items = value.map(function (item) {
46
- if (typeof item === "string") {
47
- return "\"".concat(item, "\"");
48
- } else if (typeof item === "number") {
49
- if (Number.isInteger(item)) {
50
- return "\"(ao-type-integer) ".concat(item, "\"");
51
- } else {
52
- return "\"(ao-type-float) ".concat(item.toExponential(20).replace("e+0", "e+00"), "\"");
53
- }
54
- } else if (typeof item === "boolean") {
55
- return "\"(ao-type-atom) \\\"".concat(item ? "true" : "false", "\\\"\"");
56
- } else if (_typeof(item) === "symbol") {
57
- var desc = item.description || "symbol";
58
- return "\"(ao-type-atom) \\\"".concat(desc, "\\\"\"");
59
- } else if (item === null) {
60
- return "\"(ao-type-atom) \\\"null\\\"\"";
61
- } else if (item === undefined) {
62
- return "\"(ao-type-atom) \\\"undefined\\\"\"";
63
- } else if (isBytes(item)) {
64
- var _length = item.byteLength || item.length || 0;
65
- if (_length === 0) {
66
- return "\"\"";
67
- } else {
68
- var base64 = _base64url["default"].encode(Buffer.from(item));
69
- return "\"(ao-type-binary) ".concat(base64, "\"");
70
- }
71
- } else if (Array.isArray(item)) {
72
- var _hbEncodeValue = hbEncodeValue(item),
73
- _hbEncodeValue2 = _slicedToArray(_hbEncodeValue, 2),
74
- encoded = _hbEncodeValue2[1];
75
- var escapedEncoded = encoded.replace(/"/g, '\\"');
76
- return "\"(ao-type-list) ".concat(escapedEncoded, "\"");
77
- } else {
78
- return "\"".concat(String(item), "\"");
79
- }
80
- });
81
- return ["list", items.join(", ")];
82
- }
83
- if (typeof value === "number") {
84
- if (!Number.isInteger(value)) return ["float", "".concat(value)];
85
- return ["integer", String(value)];
86
- }
87
- if (typeof value === "boolean") {
88
- return ["atom", value ? "true" : "false"];
89
- }
90
- if (_typeof(value) === "symbol") {
91
- var desc = value.description || "symbol";
92
- return ["atom", desc];
93
- }
94
- if (value === null) return ["atom", "null"];
95
- if (value === undefined) return ["atom", "undefined"];
96
- if (isPojo(value)) {
97
- throw new Error("Objects must be lifted");
98
- }
99
- throw new Error("Cannot encode value: ".concat(String(value)));
100
- }
101
31
  var MAX_HEADER_LENGTH = 4096;
102
- function encode_body_keys(bodyKeys) {
103
- if (!bodyKeys || bodyKeys.length === 0) return "";
104
- var items = bodyKeys.map(function (key) {
105
- var escaped = key.replace(/"/g, '\\"');
106
- return "\"".concat(escaped, "\"");
107
- });
108
- return items.join(", ");
109
- }
110
32
  function hasNewline(_x) {
111
33
  return _hasNewline.apply(this, arguments);
112
34
  }
@@ -191,139 +113,551 @@ function _sha() {
191
113
  }));
192
114
  return _sha.apply(this, arguments);
193
115
  }
194
- function collectParts(obj) {
195
- var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
196
- var parts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
197
- var types = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
198
- var _loop = function _loop() {
199
- var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
200
- key = _Object$entries$_i[0],
201
- value = _Object$entries$_i[1];
202
- var currentPath = path ? "".concat(path, "/").concat(key) : key;
203
- if (value === null || value === undefined) {
204
- if (!parts[path]) parts[path] = {};
205
- parts[path][key] = value === null ? '"null"' : '"undefined"';
206
- types[currentPath] = "atom";
207
- } else if (isBytes(value)) {
208
- if (!parts[path]) parts[path] = {};
209
- parts[path][key] = value;
210
- var length = value.byteLength || value.length || 0;
211
- if (length === 0) {
212
- types[currentPath] = "empty-binary";
213
- }
214
- } else if (typeof value === "string") {
215
- if (value.length === 0) {
216
- types[currentPath] = "empty-binary";
217
- if (!parts[path]) parts[path] = {};
218
- parts[path][key] = "";
116
+ function formatFloat(num) {
117
+ var exp = num.toExponential(20);
118
+ exp = exp.replace(/e\+(\d)$/, "e+0$1");
119
+ exp = exp.replace(/e-(\d)$/, "e-0$1");
120
+ return exp;
121
+ }
122
+ function hasNonAscii(str) {
123
+ return /[^\x00-\x7F]/.test(str);
124
+ }
125
+ function encodeArrayItem(item) {
126
+ if (typeof item === "number") {
127
+ if (Number.isInteger(item)) {
128
+ return "\"(ao-type-integer) ".concat(item, "\"");
129
+ } else {
130
+ return "\"(ao-type-float) ".concat(formatFloat(item), "\"");
131
+ }
132
+ } else if (typeof item === "string") {
133
+ return "\"".concat(item, "\"");
134
+ } else if (item === null) {
135
+ return "\"(ao-type-atom) \\\"null\\\"\"";
136
+ } else if (item === undefined) {
137
+ return "\"(ao-type-atom) \\\"undefined\\\"\"";
138
+ } else if (_typeof(item) === "symbol") {
139
+ var desc = item.description || "Symbol.for()";
140
+ return "\"(ao-type-atom) \\\"".concat(desc, "\\\"\"");
141
+ } else if (typeof item === "boolean") {
142
+ return "\"(ao-type-atom) \\\"".concat(item, "\\\"\"");
143
+ } else if (Array.isArray(item)) {
144
+ var nestedItems = item.map(function (nestedItem) {
145
+ if (typeof nestedItem === "number") {
146
+ if (Number.isInteger(nestedItem)) {
147
+ return "\\\"(ao-type-integer) ".concat(nestedItem, "\\\"");
148
+ } else {
149
+ return "\\\"(ao-type-float) ".concat(formatFloat(nestedItem), "\\\"");
150
+ }
151
+ } else if (typeof nestedItem === "string") {
152
+ return "\\\"".concat(nestedItem, "\\\"");
153
+ } else if (nestedItem === null) {
154
+ return "\\\"(ao-type-atom) \\\\\\\"null\\\\\\\"\\\"";
155
+ } else if (nestedItem === undefined) {
156
+ return "\\\"(ao-type-atom) \\\\\\\"undefined\\\\\\\"\\\"";
157
+ } else if (_typeof(nestedItem) === "symbol") {
158
+ var _desc = nestedItem.description || "Symbol.for()";
159
+ return "\\\"(ao-type-atom) \\\\\\\"".concat(_desc, "\\\\\\\"\\\"");
160
+ } else if (typeof nestedItem === "boolean") {
161
+ return "\\\"(ao-type-atom) \\\\\\\"".concat(nestedItem, "\\\\\\\"\\\"");
162
+ } else if (Array.isArray(nestedItem)) {
163
+ // Handle nested arrays recursively
164
+ var deeperItems = nestedItem.map(function (deepItem) {
165
+ if (typeof deepItem === "number") {
166
+ if (Number.isInteger(deepItem)) {
167
+ return "\\\\\\\"(ao-type-integer) ".concat(deepItem, "\\\\\\\"");
168
+ } else {
169
+ return "\\\\\\\"(ao-type-float) ".concat(formatFloat(deepItem), "\\\\\\\"");
170
+ }
171
+ } else if (typeof deepItem === "string") {
172
+ return "\\\\\\\"".concat(deepItem, "\\\\\\\"");
173
+ } else if (Array.isArray(deepItem)) {
174
+ // Even deeper nesting - need to escape more
175
+ var deepestItems = deepItem.map(function (deepestItem) {
176
+ if (typeof deepestItem === "number") {
177
+ if (Number.isInteger(deepestItem)) {
178
+ return "\\\\\\\\\\\\\\\"(ao-type-integer) ".concat(deepestItem, "\\\\\\\\\\\\\\\"");
179
+ } else {
180
+ return "\\\\\\\\\\\\\\\"(ao-type-float) ".concat(formatFloat(deepestItem), "\\\\\\\\\\\\\\\"");
181
+ }
182
+ } else if (typeof deepestItem === "string") {
183
+ return "\\\\\\\\\\\\\\\"".concat(deepestItem, "\\\\\\\\\\\\\\\"");
184
+ } else {
185
+ return "\\\\\\\\\\\\\\\"".concat(String(deepestItem), "\\\\\\\\\\\\\\\"");
186
+ }
187
+ }).join(", ");
188
+ return "\\\\\\\"(ao-type-list) ".concat(deepestItems, "\\\\\\\"");
189
+ } else if (deepItem === null) {
190
+ return "\\\\\\\"(ao-type-atom) \\\\\\\\\\\\\\\"null\\\\\\\\\\\\\\\"\\\\\\\"";
191
+ } else if (deepItem === undefined) {
192
+ return "\\\\\\\"(ao-type-atom) \\\\\\\\\\\\\\\"undefined\\\\\\\\\\\\\\\"\\\\\\\"";
193
+ } else if (_typeof(deepItem) === "symbol") {
194
+ var _desc2 = deepItem.description || "Symbol.for()";
195
+ return "\\\\\\\"(ao-type-atom) \\\\\\\\\\\\\\\"".concat(_desc2, "\\\\\\\\\\\\\\\"\\\\\\\"");
196
+ } else if (typeof deepItem === "boolean") {
197
+ return "\\\\\\\"(ao-type-atom) \\\\\\\\\\\\\\\"".concat(deepItem, "\\\\\\\\\\\\\\\"\\\\\\\"");
198
+ } else {
199
+ return "\\\\\\\"".concat(String(deepItem), "\\\\\\\"");
200
+ }
201
+ }).join(", ");
202
+ return "\\\"(ao-type-list) ".concat(deeperItems, "\\\"");
219
203
  } else {
220
- if (!parts[path]) parts[path] = {};
221
- parts[path][key] = value;
204
+ return "\\\"".concat(String(nestedItem), "\\\"");
222
205
  }
223
- } else if (Array.isArray(value)) {
224
- if (value.length === 0) {
225
- types[currentPath] = "empty-list";
226
- if (!parts[path]) parts[path] = {};
227
- parts[path][key] = "[]";
228
- } else {
206
+ }).join(", ");
207
+ return "\"(ao-type-list) ".concat(nestedItems, "\"");
208
+ } else if (isBytes(item)) {
209
+ var buffer = toBuffer(item);
210
+ if (buffer.length === 0 || buffer.byteLength === 0) {
211
+ return "\"\"";
212
+ }
213
+ return "\"(ao-type-binary)\"";
214
+ } else if (isPojo(item)) {
215
+ var json = JSON.stringify(item);
216
+ var escaped = json.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
217
+ return "\"(ao-type-map) ".concat(escaped, "\"");
218
+ } else {
219
+ return "\"".concat(String(item), "\"");
220
+ }
221
+ }
222
+ function toBuffer(value) {
223
+ if (Buffer.isBuffer(value)) {
224
+ return value;
225
+ } else if (value && _typeof(value) === "object" && value.type === "Buffer" && Array.isArray(value.data)) {
226
+ return Buffer.from(value.data);
227
+ } else if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
228
+ return Buffer.from(value);
229
+ } else {
230
+ return Buffer.from(value);
231
+ }
232
+ }
233
+ function collectBodyKeys(obj) {
234
+ var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
235
+ console.log("=== collectBodyKeys START ===");
236
+ console.log("Input object:", JSON.stringify(obj));
237
+ var keys = [];
238
+ function traverse(current, path) {
239
+ console.log("[traverse] Called with path: \"".concat(path, "\""));
240
+ var hasSimpleFields = false;
241
+ var nestedPaths = [];
242
+ var hasArraysWithObjects = false;
243
+ var _loop = function _loop() {
244
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
245
+ key = _Object$entries$_i[0],
246
+ value = _Object$entries$_i[1];
247
+ var fullPath = path ? "".concat(path, "/").concat(key) : key;
248
+ if (Array.isArray(value)) {
249
+ console.log("[traverse] Found array at ".concat(fullPath, ", length: ").concat(value.length));
229
250
  var hasObjects = value.some(function (item) {
230
251
  return isPojo(item);
231
252
  });
232
- if (hasObjects) {
233
- // Arrays with objects: create separate parts for each indexed item
234
- types[currentPath] = "list";
235
- value.forEach(function (item, index) {
236
- var indexKey = String(index + 1);
237
- var indexPath = "".concat(currentPath, "/").concat(indexKey);
238
- if (isPojo(item)) {
239
- // Each object in the array becomes a separate part
240
- if (!parts[indexPath]) parts[indexPath] = {};
241
- for (var _i2 = 0, _Object$entries2 = Object.entries(item); _i2 < _Object$entries2.length; _i2++) {
242
- var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
243
- objKey = _Object$entries2$_i[0],
244
- objValue = _Object$entries2$_i[1];
245
- parts[indexPath][objKey] = objValue;
253
+ var hasNonObjects = value.some(function (item) {
254
+ return !isPojo(item);
255
+ });
256
+ if (value.length === 0) {
257
+ console.log("[traverse] Empty array at ".concat(fullPath, " - marking parent as having simple fields"));
258
+ hasSimpleFields = true;
259
+ } else if (hasObjects) {
260
+ hasArraysWithObjects = true;
261
+ // Check if we need special handling for mixed arrays
262
+ var hasEmptyStrings = value.some(function (item) {
263
+ return typeof item === "string" && item === "";
264
+ });
265
+ var hasEmptyObjects = value.some(function (item) {
266
+ return isPojo(item) && Object.keys(item).length === 0;
267
+ });
268
+ var hasNonEmptyObjects = value.some(function (item) {
269
+ return isPojo(item) && Object.keys(item).length > 0;
270
+ });
271
+
272
+ // Check if objects contain only empty values (not empty objects)
273
+ var hasObjectsWithOnlyEmptyValues = value.some(function (item) {
274
+ if (!isPojo(item) || Object.keys(item).length === 0) return false;
275
+ return Object.values(item).every(function (v) {
276
+ return typeof v === "string" && v === "" || Array.isArray(v) && v.length === 0 || isPojo(v) && Object.keys(v).length === 0;
277
+ });
278
+ });
246
279
 
247
- // Set types for object fields
248
- var fieldPath = "".concat(indexPath, "/").concat(objKey);
249
- if (typeof objValue === "number") {
250
- types[fieldPath] = Number.isInteger(objValue) ? "integer" : "float";
251
- } else if (typeof objValue === "boolean") {
252
- types[fieldPath] = "atom";
253
- } else if (_typeof(objValue) === "symbol") {
254
- types[fieldPath] = "atom";
255
- // Store the symbol's description for later use
256
- parts[indexPath][objKey] = objValue;
257
- } else if (typeof objValue === "string" && objValue.length === 0) {
258
- types[fieldPath] = "empty-binary";
259
- } else if (Array.isArray(objValue) && objValue.length === 0) {
260
- types[fieldPath] = "empty-list";
261
- } else if (isPojo(objValue) && Object.keys(objValue).length === 0) {
262
- types[fieldPath] = "empty-message";
280
+ // Only use special handling if we have BOTH empty elements AND non-empty objects
281
+ if ((hasEmptyStrings || hasEmptyObjects) && hasNonEmptyObjects) {
282
+ // Special case: mixed array with empty strings/objects - only non-empty objects get parts
283
+ value.forEach(function (item, index) {
284
+ if (isPojo(item) && Object.keys(item).length > 0) {
285
+ var itemPath = "".concat(fullPath, "/").concat(index + 1);
286
+ keys.push(itemPath);
287
+ nestedPaths.push(itemPath);
288
+ }
289
+ });
290
+ if (hasNonObjects) {
291
+ hasSimpleFields = true;
292
+ keys.push(fullPath);
293
+ }
294
+ } else if (hasObjectsWithOnlyEmptyValues && !hasNonObjects) {
295
+ // Special case: objects that contain only empty values should get parts
296
+ value.forEach(function (item, index) {
297
+ if (isPojo(item)) {
298
+ var itemPath = "".concat(fullPath, "/").concat(index + 1);
299
+ keys.push(itemPath);
300
+ if (Object.keys(item).length > 0) {
301
+ nestedPaths.push(itemPath);
263
302
  }
264
303
  }
265
- } else {
266
- // Non-object items in the array
267
- if (!parts[currentPath]) parts[currentPath] = {};
268
- parts[currentPath][indexKey] = item;
269
- if (typeof item === "number") {
270
- types["".concat(currentPath, "/").concat(indexKey)] = Number.isInteger(item) ? "integer" : "float";
271
- } else if (typeof item === "boolean") {
272
- types["".concat(currentPath, "/").concat(indexKey)] = "atom";
304
+ });
305
+ } else {
306
+ // Normal case: all objects get parts
307
+ value.forEach(function (item, index) {
308
+ if (isPojo(item)) {
309
+ var itemPath = "".concat(fullPath, "/").concat(index + 1);
310
+ keys.push(itemPath);
311
+ if (Object.keys(item).length > 0) {
312
+ nestedPaths.push(itemPath);
313
+ }
273
314
  }
315
+ });
316
+ if (hasNonObjects) {
317
+ hasSimpleFields = true;
318
+ keys.push(fullPath);
274
319
  }
275
- });
320
+ }
276
321
  } else {
277
- // Simple arrays without objects
278
- var _hbEncodeValue3 = hbEncodeValue(value),
279
- _hbEncodeValue4 = _slicedToArray(_hbEncodeValue3, 2),
280
- type = _hbEncodeValue4[0],
281
- encoded = _hbEncodeValue4[1];
282
- if (!parts[path]) parts[path] = {};
283
- parts[path][key] = encoded;
284
- types[currentPath] = type || "list";
322
+ console.log("[traverse] Non-empty array without objects at ".concat(fullPath, " - marking as simple field"));
323
+ hasSimpleFields = true;
324
+ }
325
+ } else if (isPojo(value)) {
326
+ if (Object.keys(value).length === 0) {
327
+ console.log("[traverse] Empty object at ".concat(fullPath, " - marking parent as having simple fields"));
328
+ hasSimpleFields = true;
329
+ } else {
330
+ // Don't traverse into the object if it only contains empty values
331
+ var containsOnlyEmptyCollections = Object.entries(value).every(function (_ref) {
332
+ var _ref2 = _slicedToArray(_ref, 2),
333
+ k = _ref2[0],
334
+ v = _ref2[1];
335
+ return Array.isArray(v) && v.length === 0 || isPojo(v) && Object.keys(v).length === 0 || isBytes(v) && (v.length === 0 || v.byteLength === 0) || typeof v === "string" && v.length === 0;
336
+ });
337
+ if (containsOnlyEmptyCollections && Object.keys(value).length > 0) {
338
+ console.log("[traverse] Object at ".concat(fullPath, " contains only empty collections - adding as body key"));
339
+ keys.push(fullPath);
340
+ } else {
341
+ // Check if this object contains arrays with only empty elements
342
+ var hasArraysWithOnlyEmptyElements = Object.entries(value).some(function (_ref3) {
343
+ var _ref4 = _slicedToArray(_ref3, 2),
344
+ k = _ref4[0],
345
+ v = _ref4[1];
346
+ return Array.isArray(v) && v.length > 0 && v.every(function (item) {
347
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
348
+ });
349
+ });
350
+ if (hasArraysWithOnlyEmptyElements) {
351
+ // This object needs a body part to show its array types
352
+ console.log("[traverse] Object at ".concat(fullPath, " has arrays with empty elements - adding as body key"));
353
+ keys.push(fullPath);
354
+ }
355
+ console.log("[traverse] Non-empty object at ".concat(fullPath, " - will traverse into it"));
356
+ nestedPaths.push(fullPath);
357
+ }
358
+ }
359
+ } else if (isBytes(value)) {
360
+ var buffer = toBuffer(value);
361
+ if (buffer.length > 0) {
362
+ hasSimpleFields = true;
285
363
  }
364
+ } else if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null || value === undefined || _typeof(value) === "symbol") {
365
+ hasSimpleFields = true;
286
366
  }
287
- } else if (isPojo(value)) {
288
- if (Object.keys(value).length === 0) {
289
- types[currentPath] = "empty-message";
290
- if (!parts[path]) parts[path] = {};
291
- parts[path][key] = "{}";
367
+ };
368
+ for (var _i = 0, _Object$entries = Object.entries(current); _i < _Object$entries.length; _i++) {
369
+ _loop();
370
+ }
371
+ if (hasSimpleFields) {
372
+ console.log("[traverse] Adding \"".concat(path, "\" to keys (has simple fields)"));
373
+ keys.push(path);
374
+ } else if (hasArraysWithObjects && path) {
375
+ // If the object only contains arrays with objects, we still need to add it as a body key
376
+ console.log("[traverse] Adding \"".concat(path, "\" to keys (contains arrays with objects)"));
377
+ keys.push(path);
378
+ }
379
+
380
+ // Check for arrays with only empty elements that need their own body parts
381
+ for (var _i2 = 0, _Object$entries2 = Object.entries(current); _i2 < _Object$entries2.length; _i2++) {
382
+ var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
383
+ key = _Object$entries2$_i[0],
384
+ value = _Object$entries2$_i[1];
385
+ var fullPath = path ? "".concat(path, "/").concat(key) : key;
386
+ if (Array.isArray(value) && value.length > 0) {
387
+ var hasOnlyEmptyElements = value.every(function (item) {
388
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
389
+ });
390
+ if (hasOnlyEmptyElements) {
391
+ console.log("[traverse] Array at ".concat(fullPath, " has only empty elements - adding as body key"));
392
+ keys.push(fullPath);
393
+ }
394
+ }
395
+ }
396
+ for (var _i3 = 0, _nestedPaths = nestedPaths; _i3 < _nestedPaths.length; _i3++) {
397
+ var nestedPath = _nestedPaths[_i3];
398
+ var parts = nestedPath.split("/");
399
+ var nestedObj = obj;
400
+ var _iterator = _createForOfIteratorHelper(parts),
401
+ _step;
402
+ try {
403
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
404
+ var part = _step.value;
405
+ if (/^\d+$/.test(part)) {
406
+ nestedObj = nestedObj[parseInt(part) - 1];
407
+ } else {
408
+ nestedObj = nestedObj[part];
409
+ }
410
+ }
411
+ } catch (err) {
412
+ _iterator.e(err);
413
+ } finally {
414
+ _iterator.f();
415
+ }
416
+ if (isPojo(nestedObj)) {
417
+ traverse(nestedObj, nestedPath);
418
+ }
419
+ }
420
+ }
421
+ var objKeys = Object.keys(obj);
422
+ var _loop2 = function _loop2() {
423
+ var _Object$entries3$_i = _slicedToArray(_Object$entries3[_i4], 2),
424
+ key = _Object$entries3$_i[0],
425
+ value = _Object$entries3$_i[1];
426
+ console.log("\n[main loop] Processing key: \"".concat(key, "\""));
427
+ console.log("[main loop] Value type: ".concat(Array.isArray(value) ? "array" : _typeof(value)));
428
+ console.log("[main loop] Array length: ".concat(Array.isArray(value) ? value.length : "N/A"));
429
+ if ((key === "data" || key === "body") && (typeof value === "string" || typeof value === "boolean" || typeof value === "number" || value === null || value === undefined || _typeof(value) === "symbol") && objKeys.length > 1) {
430
+ // Special handling: only add to body keys if there's no other data/body field with an object
431
+ if (key === "data" && obj.body && isPojo(obj.body) && Object.keys(obj.body).length > 0) {
432
+ console.log("[main loop] Skipping special data field");
433
+ } else if (key === "body" && obj.data && isPojo(obj.data) && Object.keys(obj.data).length > 0) {
434
+ console.log("[main loop] Skipping special body field");
292
435
  } else {
293
- var hasOnlyEmptyChildren = Object.entries(value).every(function (_ref) {
294
- var _ref2 = _slicedToArray(_ref, 2),
295
- k = _ref2[0],
296
- v = _ref2[1];
297
- return Array.isArray(v) && v.length === 0 || isPojo(v) && Object.keys(v).length === 0;
436
+ console.log("[main loop] Adding special data/body key: \"".concat(key, "\""));
437
+ keys.push(key);
438
+ }
439
+ } else if (Array.isArray(value)) {
440
+ if (value.length === 0) {
441
+ console.log("[main loop] SKIPPING empty array for key: \"".concat(key, "\""));
442
+ return 1; // continue
443
+ }
444
+ var hasObjects = value.some(function (item) {
445
+ return isPojo(item);
446
+ });
447
+ var hasArrays = value.some(function (item) {
448
+ return Array.isArray(item);
449
+ });
450
+ var hasNonObjects = value.some(function (item) {
451
+ return !isPojo(item);
452
+ });
453
+
454
+ // Check if this is an array of arrays containing objects
455
+ var hasArraysOfObjects = value.some(function (item) {
456
+ return Array.isArray(item) && item.some(function (subItem) {
457
+ return isPojo(subItem);
298
458
  });
299
- if (hasOnlyEmptyChildren) {
300
- if (!parts[currentPath]) parts[currentPath] = {};
459
+ });
460
+ console.log("[main loop] Array analysis: hasObjects=".concat(hasObjects, ", hasArrays=").concat(hasArrays, ", hasNonObjects=").concat(hasNonObjects, ", hasArraysOfObjects=").concat(hasArraysOfObjects));
461
+ if (value.length > 0) {
462
+ var bodyPartCounter = 1; // Start counting from 1
463
+
464
+ // Check for special mixed array case
465
+ var hasEmptyStrings = value.some(function (item) {
466
+ return typeof item === "string" && item === "";
467
+ });
468
+ var hasEmptyObjects = value.some(function (item) {
469
+ return isPojo(item) && Object.keys(item).length === 0;
470
+ });
471
+
472
+ // Check for objects that contain only empty values
473
+ var hasObjectsWithOnlyEmptyValues = value.some(function (item) {
474
+ if (!isPojo(item) || Object.keys(item).length === 0) return false;
475
+ return Object.values(item).every(function (v) {
476
+ return typeof v === "string" && v === "" || Array.isArray(v) && v.length === 0 || isPojo(v) && Object.keys(v).length === 0;
477
+ });
478
+ });
479
+ if (hasArraysOfObjects) {
480
+ // Handle arrays of arrays containing objects
481
+ value.forEach(function (item, index) {
482
+ if (Array.isArray(item)) {
483
+ item.forEach(function (subItem, subIndex) {
484
+ if (isPojo(subItem)) {
485
+ var path = "".concat(key, "/").concat(index + 1, "/").concat(subIndex + 1);
486
+ console.log("[main loop] Adding nested object path: \"".concat(path, "\""));
487
+ keys.push(path);
488
+ }
489
+ });
490
+ }
491
+ bodyPartCounter++;
492
+ });
493
+ // Always add the main array key
494
+ console.log("[main loop] ADDING main array key: \"".concat(key, "\""));
495
+ keys.push(key);
496
+ } else if (hasObjects && (hasEmptyStrings || hasEmptyObjects) && !hasObjectsWithOnlyEmptyValues) {
497
+ // Special handling: only non-empty objects get parts
498
+ value.forEach(function (item, index) {
499
+ if (isPojo(item) && Object.keys(item).length > 0) {
500
+ var path = "".concat(key, "/").concat(bodyPartCounter);
501
+ console.log("[main loop] Adding non-empty object path: \"".concat(path, "\" (array index ").concat(index, ")"));
502
+ keys.push(path);
503
+ // Add paths for nested objects
504
+ for (var _i5 = 0, _Object$entries4 = Object.entries(item); _i5 < _Object$entries4.length; _i5++) {
505
+ var _Object$entries4$_i = _slicedToArray(_Object$entries4[_i5], 2),
506
+ nestedKey = _Object$entries4$_i[0],
507
+ nestedValue = _Object$entries4$_i[1];
508
+ if (isPojo(nestedValue)) {
509
+ var nestedPath = "".concat(key, "/").concat(bodyPartCounter, "/").concat(nestedKey);
510
+ console.log("[main loop] Adding nested object path: \"".concat(nestedPath, "\""));
511
+ keys.push(nestedPath);
512
+ }
513
+ }
514
+ }
515
+ bodyPartCounter++;
516
+ });
517
+ // Always add the main array key
518
+ console.log("[main loop] ADDING main array key: \"".concat(key, "\""));
519
+ keys.push(key);
520
+ } else if (hasObjects) {
521
+ // Normal handling: all objects get parts (except if parent array has only empty elements)
522
+ var skipEmptyObjects = false;
523
+
524
+ // Check if this array contains only empty elements
525
+ var arrayHasOnlyEmptyElements = value.every(function (item) {
526
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
527
+ });
528
+ if (arrayHasOnlyEmptyElements) {
529
+ skipEmptyObjects = true;
530
+ }
531
+ value.forEach(function (item, index) {
532
+ if (isPojo(item)) {
533
+ // Skip empty objects if array has only empty elements
534
+ if (skipEmptyObjects && Object.keys(item).length === 0) {
535
+ bodyPartCounter++;
536
+ return;
537
+ }
538
+ var path = "".concat(key, "/").concat(bodyPartCounter);
539
+ console.log("[main loop] Adding object path: \"".concat(path, "\" (array index ").concat(index, ", empty=").concat(Object.keys(item).length === 0, ")"));
540
+ keys.push(path);
541
+ // Add paths for nested objects (but not empty ones)
542
+ if (Object.keys(item).length > 0) {
543
+ for (var _i6 = 0, _Object$entries5 = Object.entries(item); _i6 < _Object$entries5.length; _i6++) {
544
+ var _Object$entries5$_i = _slicedToArray(_Object$entries5[_i6], 2),
545
+ nestedKey = _Object$entries5$_i[0],
546
+ nestedValue = _Object$entries5$_i[1];
547
+ if (isPojo(nestedValue) && Object.keys(nestedValue).length > 0) {
548
+ var nestedPath = "".concat(key, "/").concat(bodyPartCounter, "/").concat(nestedKey);
549
+ console.log("[main loop] Adding nested object path: \"".concat(nestedPath, "\""));
550
+ keys.push(nestedPath);
551
+ }
552
+ }
553
+ }
554
+ } else if (typeof item === "string" && item === "") {
555
+ // Empty strings may get parts in some formats
556
+ var _path = "".concat(key, "/").concat(bodyPartCounter);
557
+ console.log("[main loop] Adding empty string path: \"".concat(_path, "\" (array index ").concat(index, ")"));
558
+ keys.push(_path);
559
+ }
560
+ bodyPartCounter++;
561
+ });
562
+ // Don't add main array key for arrays with only objects containing empty values
563
+ if (!hasObjectsWithOnlyEmptyValues || value.some(function (item) {
564
+ return !isPojo(item);
565
+ })) {
566
+ // Check if array has only empty elements
567
+ var hasOnlyEmptyElements = value.every(function (item) {
568
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
569
+ });
570
+ if (!hasOnlyEmptyElements) {
571
+ // Always add the main array key
572
+ console.log("[main loop] ADDING main array key: \"".concat(key, "\""));
573
+ keys.push(key);
574
+ }
575
+ }
576
+ } else {
577
+ // Check if array has only empty elements
578
+ var hasOnlyEmptyArraysOrObjects = value.every(function (item) {
579
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
580
+ });
581
+ if (hasOnlyEmptyArraysOrObjects && value.length > 0) {
582
+ // Always add the main array key for arrays with only empty elements
583
+ console.log("[main loop] ADDING main array key for empty elements: \"".concat(key, "\""));
584
+ keys.push(key);
585
+ } else if (!hasOnlyEmptyArraysOrObjects) {
586
+ // Always add the main array key
587
+ console.log("[main loop] ADDING main array key: \"".concat(key, "\""));
588
+ keys.push(key);
589
+ }
301
590
  }
302
- collectParts(value, currentPath, parts, types);
303
591
  }
304
- } else if (_typeof(value) === "symbol") {
305
- if (!parts[path]) parts[path] = {};
306
- parts[path][key] = value.description || "symbol";
307
- types[currentPath] = "atom";
308
- } else if (typeof value === "boolean") {
309
- if (!parts[path]) parts[path] = {};
310
- parts[path][key] = value;
311
- types[currentPath] = "atom";
312
- } else {
313
- if (!parts[path]) parts[path] = {};
314
- parts[path][key] = value;
315
- if (typeof value === "number") {
316
- types[currentPath] = Number.isInteger(value) ? "integer" : "float";
592
+ } else if (isPojo(value)) {
593
+ console.log("[main loop] Processing object at key: \"".concat(key, "\""));
594
+ // Objects should be traversed, not have their fields individually added
595
+ traverse(value, key);
596
+ } else if (isBytes(value)) {
597
+ var buffer = toBuffer(value);
598
+ if (buffer.length > 0) {
599
+ console.log("[main loop] Adding key for non-empty bytes: \"".concat(key, "\""));
600
+ keys.push(key);
317
601
  }
602
+ } else if (typeof value === "string" && value.includes("\n")) {
603
+ console.log("[main loop] Adding key for string with newline: \"".concat(key, "\""));
604
+ keys.push(key);
605
+ } else if (typeof value === "string" && hasNonAscii(value)) {
606
+ console.log("[main loop] Adding key for non-ASCII string: \"".concat(key, "\""));
607
+ keys.push(key);
608
+ } else {
609
+ console.log("[main loop] Skipping key: \"".concat(key, "\" (no match)"));
318
610
  }
319
611
  };
320
- for (var _i = 0, _Object$entries = Object.entries(obj); _i < _Object$entries.length; _i++) {
321
- _loop();
612
+ for (var _i4 = 0, _Object$entries3 = Object.entries(obj); _i4 < _Object$entries3.length; _i4++) {
613
+ if (_loop2()) continue;
322
614
  }
323
- return {
324
- parts: parts,
325
- types: types
326
- };
615
+ var result = _toConsumableArray(new Set(keys)).filter(function (k) {
616
+ if (k === "") return false;
617
+
618
+ // Check if this is a path to an empty object inside an array with only empty elements
619
+ var parts = k.split("/");
620
+ if (parts.length >= 2 && /^\d+$/.test(parts[parts.length - 1])) {
621
+ // This is an array element path like "maps/1"
622
+ var arrayPath = parts.slice(0, -1).join("/");
623
+ var arrayValue = obj;
624
+
625
+ // Navigate to the array
626
+ var _iterator2 = _createForOfIteratorHelper(parts.slice(0, -1)),
627
+ _step2;
628
+ try {
629
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
630
+ var part = _step2.value;
631
+ if (/^\d+$/.test(part)) {
632
+ arrayValue = arrayValue[parseInt(part) - 1];
633
+ } else {
634
+ arrayValue = arrayValue[part];
635
+ }
636
+ }
637
+
638
+ // Check if this array contains only empty elements
639
+ } catch (err) {
640
+ _iterator2.e(err);
641
+ } finally {
642
+ _iterator2.f();
643
+ }
644
+ if (Array.isArray(arrayValue)) {
645
+ var hasOnlyEmptyElements = arrayValue.every(function (item) {
646
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
647
+ });
648
+ if (hasOnlyEmptyElements) {
649
+ // Filter out paths to individual empty elements
650
+ console.log("[filter] Removing path to empty element: \"".concat(k, "\""));
651
+ return false;
652
+ }
653
+ }
654
+ }
655
+ return true;
656
+ });
657
+ console.log("\n=== collectBodyKeys RESULT ===");
658
+ console.log("Final bodyKeys:", JSON.stringify(result));
659
+ console.log("=== collectBodyKeys END ===\n");
660
+ return result;
327
661
  }
328
662
  function encode() {
329
663
  return _encode.apply(this, arguments);
@@ -331,717 +665,1043 @@ function encode() {
331
665
  function _encode() {
332
666
  _encode = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
333
667
  var obj,
668
+ _processValue,
669
+ processedObj,
670
+ _i8,
671
+ _Object$entries7,
672
+ _Object$entries7$_i,
673
+ k,
674
+ v,
334
675
  objKeys,
335
676
  fieldName,
336
- binaryData,
337
- headers,
677
+ fieldValue,
678
+ _headers,
679
+ hasBodyBinary,
680
+ otherFields,
681
+ _headers2,
338
682
  bodyBuffer,
339
683
  bodyArrayBuffer,
340
684
  contentDigest,
341
685
  base64,
342
- _headers,
343
- types,
344
- needsMultipart,
345
- _i3,
346
- _Object$entries3,
347
- _Object$entries3$_i,
348
- key,
349
- value,
350
- _hbEncodeValue5,
351
- _hbEncodeValue6,
352
- type,
353
- encoded,
354
- body,
686
+ _fieldName,
687
+ _fieldValue,
688
+ _headers3,
355
689
  _bodyBuffer,
356
690
  _bodyArrayBuffer,
357
691
  _contentDigest,
358
692
  _base,
359
- _args3 = arguments;
360
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
361
- while (1) switch (_context3.prev = _context3.next) {
693
+ _headers4,
694
+ bodyContent,
695
+ encoder,
696
+ encoded,
697
+ _contentDigest2,
698
+ _base2,
699
+ _headers5,
700
+ _encoder,
701
+ _encoded,
702
+ _contentDigest3,
703
+ _base3,
704
+ bodyKeys,
705
+ headers,
706
+ headerTypes,
707
+ _loop3,
708
+ _i9,
709
+ _Object$entries8,
710
+ _loop4,
711
+ _i10,
712
+ _Object$entries9,
713
+ allBodyKeysAreEmptyBinaries,
714
+ singleKey,
715
+ pathParts,
716
+ value,
717
+ _iterator4,
718
+ _step4,
719
+ part,
720
+ otherFieldsAreEmpty,
721
+ _bodyBuffer2,
722
+ _bodyArrayBuffer2,
723
+ _contentDigest4,
724
+ _base4,
725
+ sortedBodyKeys,
726
+ hasSpecialDataBody,
727
+ bodyParts,
728
+ _iterator5,
729
+ _step5,
730
+ _loop5,
731
+ _ret,
732
+ buffer,
733
+ specialPart,
734
+ partsContent,
735
+ allContent,
736
+ boundaryHash,
737
+ boundary,
738
+ finalParts,
739
+ i,
740
+ body,
741
+ finalContent,
742
+ _contentDigest5,
743
+ _base5,
744
+ _args6 = arguments;
745
+ return _regeneratorRuntime().wrap(function _callee3$(_context6) {
746
+ while (1) switch (_context6.prev = _context6.next) {
362
747
  case 0:
363
- obj = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {};
364
- console.log("[encode] START with obj:", JSON.stringify(obj));
748
+ obj = _args6.length > 0 && _args6[0] !== undefined ? _args6[0] : {};
749
+ console.log("\n=== ENCODE START ===");
750
+ console.log("Encoding object:", JSON.stringify(obj));
751
+ _processValue = function processValue(value) {
752
+ if (_typeof(value) === "symbol") {
753
+ return value.description || "Symbol.for()";
754
+ } else if (Array.isArray(value)) {
755
+ return value.map(_processValue);
756
+ } else if (isPojo(value)) {
757
+ var result = {};
758
+ for (var _i7 = 0, _Object$entries6 = Object.entries(value); _i7 < _Object$entries6.length; _i7++) {
759
+ var _Object$entries6$_i = _slicedToArray(_Object$entries6[_i7], 2),
760
+ k = _Object$entries6$_i[0],
761
+ v = _Object$entries6$_i[1];
762
+ result[k] = _processValue(v);
763
+ }
764
+ return result;
765
+ }
766
+ return value;
767
+ };
768
+ processedObj = {};
769
+ for (_i8 = 0, _Object$entries7 = Object.entries(obj); _i8 < _Object$entries7.length; _i8++) {
770
+ _Object$entries7$_i = _slicedToArray(_Object$entries7[_i8], 2), k = _Object$entries7$_i[0], v = _Object$entries7$_i[1];
771
+ processedObj[k] = _processValue(v);
772
+ }
365
773
  if (!(Object.keys(obj).length === 0)) {
366
- _context3.next = 4;
774
+ _context6.next = 8;
367
775
  break;
368
776
  }
369
- return _context3.abrupt("return", {
777
+ return _context6.abrupt("return", {
370
778
  headers: {},
371
779
  body: undefined
372
780
  });
373
- case 4:
374
- // Check if we have a simple binary field
781
+ case 8:
375
782
  objKeys = Object.keys(obj);
376
- if (!(objKeys.length === 1 && isBytes(obj[objKeys[0]]))) {
377
- _context3.next = 18;
783
+ if (!(objKeys.length === 1)) {
784
+ _context6.next = 16;
378
785
  break;
379
786
  }
380
- // Single binary field - return it directly
381
787
  fieldName = objKeys[0];
382
- binaryData = obj[fieldName];
383
- headers = {};
384
- bodyBuffer = Buffer.isBuffer(binaryData) ? binaryData : Buffer.from(binaryData);
385
- bodyArrayBuffer = bodyBuffer.buffer.slice(bodyBuffer.byteOffset, bodyBuffer.byteOffset + bodyBuffer.byteLength);
386
- _context3.next = 13;
387
- return sha256(bodyArrayBuffer);
388
- case 13:
389
- contentDigest = _context3.sent;
390
- base64 = _base64url["default"].toBase64(_base64url["default"].encode(contentDigest));
391
- headers["content-digest"] = "sha-256=:".concat(base64, ":");
392
- console.log("[encode] FINAL (simple binary field) - headers:", headers, "body:", binaryData);
393
- return _context3.abrupt("return", {
394
- headers: headers,
395
- body: binaryData
396
- });
397
- case 18:
398
- if (!("body" in obj && isBytes(obj.body))) {
399
- _context3.next = 108;
788
+ fieldValue = obj[fieldName];
789
+ if (!(isBytes(fieldValue) && (fieldValue.length === 0 || fieldValue.byteLength === 0))) {
790
+ _context6.next = 16;
400
791
  break;
401
792
  }
402
793
  _headers = {};
403
- types = [];
404
- needsMultipart = false;
405
- _i3 = 0, _Object$entries3 = Object.entries(obj);
406
- case 23:
407
- if (!(_i3 < _Object$entries3.length)) {
408
- _context3.next = 95;
409
- break;
410
- }
411
- _Object$entries3$_i = _slicedToArray(_Object$entries3[_i3], 2), key = _Object$entries3$_i[0], value = _Object$entries3$_i[1];
412
- if (!(key === "body")) {
413
- _context3.next = 27;
414
- break;
415
- }
416
- return _context3.abrupt("continue", 92);
417
- case 27:
418
- if (!(value === null)) {
419
- _context3.next = 32;
794
+ _headers["ao-types"] = "".concat(fieldName.toLowerCase(), "=\"empty-binary\"");
795
+ return _context6.abrupt("return", {
796
+ headers: _headers,
797
+ body: undefined
798
+ });
799
+ case 16:
800
+ if (obj.body && isBytes(obj.body) && (obj.body.length === 0 || obj.body.byteLength === 0) && objKeys.length > 1) {}
801
+ hasBodyBinary = obj.body && isBytes(obj.body);
802
+ otherFields = Object.keys(obj).filter(function (k) {
803
+ return k !== "body";
804
+ });
805
+ if (!(hasBodyBinary && otherFields.length === 0)) {
806
+ _context6.next = 29;
420
807
  break;
421
808
  }
422
- _headers[key] = "null";
423
- types.push("".concat(key, "=\"atom\""));
424
- _context3.next = 92;
425
- break;
426
- case 32:
427
- if (!(value === undefined)) {
428
- _context3.next = 37;
809
+ _headers2 = {};
810
+ bodyBuffer = toBuffer(obj.body);
811
+ bodyArrayBuffer = bodyBuffer.buffer.slice(bodyBuffer.byteOffset, bodyBuffer.byteOffset + bodyBuffer.byteLength);
812
+ _context6.next = 25;
813
+ return sha256(bodyArrayBuffer);
814
+ case 25:
815
+ contentDigest = _context6.sent;
816
+ base64 = _base64url["default"].toBase64(_base64url["default"].encode(contentDigest));
817
+ _headers2["content-digest"] = "sha-256=:".concat(base64, ":");
818
+ return _context6.abrupt("return", {
819
+ headers: _headers2,
820
+ body: obj.body
821
+ });
822
+ case 29:
823
+ if (!(objKeys.length === 1)) {
824
+ _context6.next = 71;
429
825
  break;
430
826
  }
431
- _headers[key] = "undefined";
432
- types.push("".concat(key, "=\"atom\""));
433
- _context3.next = 92;
434
- break;
435
- case 37:
436
- if (!(typeof value === "string" && value.length === 0)) {
437
- _context3.next = 41;
827
+ _fieldName = objKeys[0];
828
+ _fieldValue = obj[_fieldName];
829
+ if (!(isBytes(_fieldValue) && _fieldValue.length > 0)) {
830
+ _context6.next = 45;
438
831
  break;
439
832
  }
440
- types.push("".concat(key, "=\"empty-binary\""));
441
- _context3.next = 92;
442
- break;
443
- case 41:
444
- if (!Array.isArray(value)) {
445
- _context3.next = 57;
446
- break;
833
+ _headers3 = {};
834
+ _bodyBuffer = toBuffer(_fieldValue);
835
+ _bodyArrayBuffer = _bodyBuffer.buffer.slice(_bodyBuffer.byteOffset, _bodyBuffer.byteOffset + _bodyBuffer.byteLength);
836
+ _context6.next = 38;
837
+ return sha256(_bodyArrayBuffer);
838
+ case 38:
839
+ _contentDigest = _context6.sent;
840
+ _base = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest));
841
+ _headers3["content-digest"] = "sha-256=:".concat(_base, ":");
842
+ if (_fieldName !== "body") {
843
+ _headers3["inline-body-key"] = _fieldName;
447
844
  }
448
- if (!(value.length === 0)) {
449
- _context3.next = 46;
845
+ return _context6.abrupt("return", {
846
+ headers: _headers3,
847
+ body: _fieldValue
848
+ });
849
+ case 45:
850
+ if (!((_fieldName === "data" || _fieldName === "body") && (typeof _fieldValue === "string" || typeof _fieldValue === "boolean" || typeof _fieldValue === "number" || _fieldValue === null || _fieldValue === undefined || _typeof(_fieldValue) === "symbol"))) {
851
+ _context6.next = 60;
450
852
  break;
451
853
  }
452
- types.push("".concat(key, "=\"empty-list\""));
453
- _context3.next = 55;
454
- break;
455
- case 46:
456
- if (!value.some(function (item) {
457
- return isPojo(item);
458
- })) {
459
- _context3.next = 52;
460
- break;
854
+ _headers4 = {};
855
+ if (typeof _fieldValue === "string") {
856
+ bodyContent = _fieldValue;
857
+ } else if (typeof _fieldValue === "boolean") {
858
+ bodyContent = "\"".concat(_fieldValue, "\"");
859
+ } else if (typeof _fieldValue === "number") {
860
+ bodyContent = String(_fieldValue);
861
+ } else if (_fieldValue === null) {
862
+ bodyContent = '"null"';
863
+ } else if (_fieldValue === undefined) {
864
+ bodyContent = '"undefined"';
865
+ } else if (_typeof(_fieldValue) === "symbol") {
866
+ bodyContent = "\"".concat(_fieldValue.description || "Symbol.for()", "\"");
461
867
  }
462
- // Arrays with objects need multipart
463
- types.push("".concat(key, "=\"list\""));
464
- needsMultipart = true;
465
- return _context3.abrupt("break", 95);
868
+ encoder = new TextEncoder();
869
+ encoded = encoder.encode(bodyContent);
870
+ _context6.next = 52;
871
+ return sha256(encoded.buffer);
466
872
  case 52:
467
- _hbEncodeValue5 = hbEncodeValue(value), _hbEncodeValue6 = _slicedToArray(_hbEncodeValue5, 2), type = _hbEncodeValue6[0], encoded = _hbEncodeValue6[1];
468
- _headers[key] = encoded;
469
- types.push("".concat(key, "=\"").concat(type, "\""));
470
- case 55:
471
- _context3.next = 92;
472
- break;
473
- case 57:
474
- if (!isBytes(value)) {
475
- _context3.next = 66;
476
- break;
873
+ _contentDigest2 = _context6.sent;
874
+ _base2 = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest2));
875
+ _headers4["content-digest"] = "sha-256=:".concat(_base2, ":");
876
+ if (typeof _fieldValue === "boolean" || _fieldValue === null || _fieldValue === undefined || _typeof(_fieldValue) === "symbol") {
877
+ _headers4["ao-types"] = "".concat(_fieldName.toLowerCase(), "=\"atom\"");
878
+ } else if (typeof _fieldValue === "number") {
879
+ _headers4["ao-types"] = "".concat(_fieldName.toLowerCase(), "=\"").concat(Number.isInteger(_fieldValue) ? "integer" : "float", "\"");
880
+ }
881
+ if (_fieldName !== "body") {
882
+ _headers4["inline-body-key"] = _fieldName;
477
883
  }
478
- if (!(value.length === 0 || value.byteLength === 0)) {
479
- _context3.next = 62;
884
+ return _context6.abrupt("return", {
885
+ headers: _headers4,
886
+ body: bodyContent
887
+ });
888
+ case 60:
889
+ if (!(typeof _fieldValue === "string" && hasNonAscii(_fieldValue))) {
890
+ _context6.next = 71;
480
891
  break;
481
892
  }
482
- types.push("".concat(key, "=\"empty-binary\""));
483
- _context3.next = 64;
484
- break;
485
- case 62:
486
- needsMultipart = true;
487
- return _context3.abrupt("break", 95);
488
- case 64:
489
- _context3.next = 92;
490
- break;
893
+ _headers5 = {};
894
+ _encoder = new TextEncoder();
895
+ _encoded = _encoder.encode(_fieldValue);
896
+ _context6.next = 66;
897
+ return sha256(_encoded.buffer);
491
898
  case 66:
492
- if (!(typeof value === "boolean")) {
493
- _context3.next = 71;
494
- break;
899
+ _contentDigest3 = _context6.sent;
900
+ _base3 = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest3));
901
+ _headers5["content-digest"] = "sha-256=:".concat(_base3, ":");
902
+ if (_fieldName !== "body") {
903
+ _headers5["inline-body-key"] = _fieldName;
495
904
  }
496
- _headers[key] = "\"".concat(value, "\"");
497
- types.push("".concat(key, "=\"atom\""));
498
- _context3.next = 92;
499
- break;
905
+ return _context6.abrupt("return", {
906
+ headers: _headers5,
907
+ body: _fieldValue
908
+ });
500
909
  case 71:
501
- if (!(_typeof(value) === "symbol")) {
502
- _context3.next = 76;
503
- break;
504
- }
505
- _headers[key] = value.description || "symbol";
506
- types.push("".concat(key, "=\"atom\""));
507
- _context3.next = 92;
508
- break;
910
+ bodyKeys = collectBodyKeys(obj);
911
+ headers = {};
912
+ headerTypes = [];
913
+ _loop3 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop3() {
914
+ var _Object$entries8$_i, key, value, needsBody, hasNonAsciiItems, encodedItems;
915
+ return _regeneratorRuntime().wrap(function _loop3$(_context3) {
916
+ while (1) switch (_context3.prev = _context3.next) {
917
+ case 0:
918
+ _Object$entries8$_i = _slicedToArray(_Object$entries8[_i9], 2), key = _Object$entries8$_i[0], value = _Object$entries8$_i[1];
919
+ needsBody = bodyKeys.includes(key) || bodyKeys.some(function (k) {
920
+ return k.startsWith("".concat(key, "/"));
921
+ });
922
+ if (needsBody) {
923
+ _context3.next = 43;
924
+ break;
925
+ }
926
+ if (!(value === null)) {
927
+ _context3.next = 8;
928
+ break;
929
+ }
930
+ headers[key] = '"null"';
931
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
932
+ _context3.next = 41;
933
+ break;
934
+ case 8:
935
+ if (!(value === undefined)) {
936
+ _context3.next = 13;
937
+ break;
938
+ }
939
+ headers[key] = '"undefined"';
940
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
941
+ _context3.next = 41;
942
+ break;
943
+ case 13:
944
+ if (!(typeof value === "boolean")) {
945
+ _context3.next = 18;
946
+ break;
947
+ }
948
+ headers[key] = "\"".concat(value, "\"");
949
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
950
+ _context3.next = 41;
951
+ break;
952
+ case 18:
953
+ if (!(_typeof(value) === "symbol")) {
954
+ _context3.next = 23;
955
+ break;
956
+ }
957
+ headers[key] = "\"".concat(value.description || "Symbol.for()", "\"");
958
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
959
+ _context3.next = 41;
960
+ break;
961
+ case 23:
962
+ if (!(typeof value === "number")) {
963
+ _context3.next = 28;
964
+ break;
965
+ }
966
+ headers[key] = String(value);
967
+ headerTypes.push("".concat(key.toLowerCase(), "=\"").concat(Number.isInteger(value) ? "integer" : "float", "\""));
968
+ _context3.next = 41;
969
+ break;
970
+ case 28:
971
+ if (!(typeof value === "string")) {
972
+ _context3.next = 40;
973
+ break;
974
+ }
975
+ if (!(value.length === 0)) {
976
+ _context3.next = 33;
977
+ break;
978
+ }
979
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-binary\""));
980
+ _context3.next = 38;
981
+ break;
982
+ case 33:
983
+ if (!hasNonAscii(value)) {
984
+ _context3.next = 37;
985
+ break;
986
+ }
987
+ return _context3.abrupt("return", 1);
988
+ case 37:
989
+ headers[key] = value;
990
+ case 38:
991
+ _context3.next = 41;
992
+ break;
993
+ case 40:
994
+ if (Array.isArray(value) && value.length === 0) {
995
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-list\""));
996
+ } else if (Array.isArray(value) && !value.some(function (item) {
997
+ return isPojo(item);
998
+ })) {
999
+ hasNonAsciiItems = value.some(function (item) {
1000
+ return typeof item === "string" && hasNonAscii(item);
1001
+ });
1002
+ if (!hasNonAsciiItems) {
1003
+ encodedItems = value.map(function (item) {
1004
+ return encodeArrayItem(item);
1005
+ }).join(", ");
1006
+ headers[key] = encodedItems;
1007
+ headerTypes.push("".concat(key.toLowerCase(), "=\"list\""));
1008
+ }
1009
+ } else if (isBytes(value) && (value.length === 0 || value.byteLength === 0)) {
1010
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-binary\""));
1011
+ } else if (isPojo(value) && Object.keys(value).length === 0) {
1012
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-message\""));
1013
+ }
1014
+ case 41:
1015
+ _context3.next = 44;
1016
+ break;
1017
+ case 43:
1018
+ if (isBytes(value) && (value.length === 0 || value.byteLength === 0)) {
1019
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-binary\""));
1020
+ } else if (typeof value === "string" && value.length === 0) {
1021
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-binary\""));
1022
+ } else if (Array.isArray(value) && value.length === 0) {
1023
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-list\""));
1024
+ } else if (isPojo(value) && Object.keys(value).length === 0) {
1025
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-message\""));
1026
+ } else if (typeof value === "boolean" || value === null || value === undefined || _typeof(value) === "symbol") {
1027
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
1028
+ } else if (typeof value === "number") {
1029
+ headerTypes.push("".concat(key.toLowerCase(), "=\"").concat(Number.isInteger(value) ? "integer" : "float", "\""));
1030
+ }
1031
+ case 44:
1032
+ case "end":
1033
+ return _context3.stop();
1034
+ }
1035
+ }, _loop3);
1036
+ });
1037
+ _i9 = 0, _Object$entries8 = Object.entries(obj);
509
1038
  case 76:
510
- if (!(typeof value === "number")) {
511
- _context3.next = 81;
1039
+ if (!(_i9 < _Object$entries8.length)) {
1040
+ _context6.next = 83;
512
1041
  break;
513
1042
  }
514
- _headers[key] = String(value);
515
- types.push("".concat(key, "=\"").concat(Number.isInteger(value) ? "integer" : "float", "\""));
516
- _context3.next = 92;
517
- break;
518
- case 81:
519
- if (!(typeof value === "string")) {
520
- _context3.next = 85;
1043
+ return _context6.delegateYield(_loop3(), "t0", 78);
1044
+ case 78:
1045
+ if (!_context6.t0) {
1046
+ _context6.next = 80;
521
1047
  break;
522
1048
  }
523
- _headers[key] = value;
524
- _context3.next = 92;
1049
+ return _context6.abrupt("continue", 80);
1050
+ case 80:
1051
+ _i9++;
1052
+ _context6.next = 76;
525
1053
  break;
1054
+ case 83:
1055
+ _loop4 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop4() {
1056
+ var _Object$entries9$_i, key, value;
1057
+ return _regeneratorRuntime().wrap(function _loop4$(_context4) {
1058
+ while (1) switch (_context4.prev = _context4.next) {
1059
+ case 0:
1060
+ _Object$entries9$_i = _slicedToArray(_Object$entries9[_i10], 2), key = _Object$entries9$_i[0], value = _Object$entries9$_i[1];
1061
+ if (Array.isArray(value)) {
1062
+ if (bodyKeys.includes(key) || bodyKeys.some(function (k) {
1063
+ return k.startsWith("".concat(key, "/"));
1064
+ })) {
1065
+ if (!headerTypes.some(function (t) {
1066
+ return t.startsWith("".concat(key.toLowerCase(), "="));
1067
+ })) {
1068
+ headerTypes.push("".concat(key.toLowerCase(), "=\"list\""));
1069
+ }
1070
+ }
1071
+ }
1072
+ case 2:
1073
+ case "end":
1074
+ return _context4.stop();
1075
+ }
1076
+ }, _loop4);
1077
+ });
1078
+ _i10 = 0, _Object$entries9 = Object.entries(obj);
526
1079
  case 85:
527
- if (!(isPojo(value) && Object.keys(value).length === 0)) {
528
- _context3.next = 89;
529
- break;
530
- }
531
- types.push("".concat(key, "=\"empty-message\""));
532
- _context3.next = 92;
533
- break;
534
- case 89:
535
- if (!isPojo(value)) {
536
- _context3.next = 92;
1080
+ if (!(_i10 < _Object$entries9.length)) {
1081
+ _context6.next = 90;
537
1082
  break;
538
1083
  }
539
- needsMultipart = true;
540
- return _context3.abrupt("break", 95);
541
- case 92:
542
- _i3++;
543
- _context3.next = 23;
1084
+ return _context6.delegateYield(_loop4(), "t1", 87);
1085
+ case 87:
1086
+ _i10++;
1087
+ _context6.next = 85;
544
1088
  break;
545
- case 95:
546
- if (!needsMultipart) {
547
- _context3.next = 97;
1089
+ case 90:
1090
+ if (!(bodyKeys.length === 0)) {
1091
+ _context6.next = 94;
548
1092
  break;
549
1093
  }
550
- return _context3.abrupt("return", encodeMultipart(obj));
551
- case 97:
552
- if (types.length > 0) {
553
- _headers["ao-types"] = types.sort().join(", ");
1094
+ console.log("No bodyKeys, returning headers only");
1095
+ if (headerTypes.length > 0) {
1096
+ headers["ao-types"] = headerTypes.sort().join(", ");
554
1097
  }
555
- body = obj.body;
556
- _bodyBuffer = Buffer.isBuffer(body) ? body : Buffer.from(body);
557
- _bodyArrayBuffer = _bodyBuffer.buffer.slice(_bodyBuffer.byteOffset, _bodyBuffer.byteOffset + _bodyBuffer.byteLength);
558
- _context3.next = 103;
559
- return sha256(_bodyArrayBuffer);
560
- case 103:
561
- _contentDigest = _context3.sent;
562
- _base = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest));
563
- _headers["content-digest"] = "sha-256=:".concat(_base, ":");
564
- console.log("[encode] FINAL (simple body encoding) - headers:", _headers, "body:", body);
565
- return _context3.abrupt("return", {
566
- headers: _headers,
567
- body: body
1098
+ return _context6.abrupt("return", {
1099
+ headers: headers,
1100
+ body: undefined
568
1101
  });
569
- case 108:
570
- return _context3.abrupt("return", encodeMultipart(obj));
571
- case 109:
572
- case "end":
573
- return _context3.stop();
574
- }
575
- }, _callee3);
576
- }));
577
- return _encode.apply(this, arguments);
578
- }
579
- function encodeMultipart(_x3) {
580
- return _encodeMultipart.apply(this, arguments);
581
- }
582
- function _encodeMultipart() {
583
- _encodeMultipart = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(obj) {
584
- var _collectParts, parts, types, headers, bodyParts, bodyKeys, headerTypes, _i4, _Object$entries4, _Object$entries4$_i, key, value, _i5, _Object$entries5, _Object$entries5$_i, path, content, _i6, _Object$entries6, _Object$entries6$_i, _key, _value, length, valueStr, type, _i7, _Object$entries7, _Object$entries7$_i, _key2, _value2, _type, _i8, _Object$entries8, _Object$entries8$_i, _key3, _type2, inlineKey, _iterator, _step, _path, fieldValue, headerStr, headerBlob, dataBlob, _lines, _isInlineKey, _type3, _content, lines, binaryParts, isInlineKey, sortedKeys, partTypes, _iterator3, _step3, _key4, fullPath, _type4, _iterator4, _step4, _key5, _value3, _fullPath, _type5, symbolValue, allParts, _iterator5, _step5, binaryPart, partsForBoundary, _iterator2, _step2, part, partContent, allPartsContent, allPartsBuffer, hashResult, boundary, finalParts, i, body, finalContent, contentDigest, base64;
585
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
586
- while (1) switch (_context4.prev = _context4.next) {
587
- case 0:
588
- _collectParts = collectParts(obj), parts = _collectParts.parts, types = _collectParts.types;
589
- console.log("[encode] Parts:", parts, "Types:", types);
590
- headers = {};
591
- bodyParts = [];
592
- bodyKeys = []; // Collect header types for arrays with objects
593
- headerTypes = [];
594
- for (_i4 = 0, _Object$entries4 = Object.entries(obj); _i4 < _Object$entries4.length; _i4++) {
595
- _Object$entries4$_i = _slicedToArray(_Object$entries4[_i4], 2), key = _Object$entries4$_i[0], value = _Object$entries4$_i[1];
596
- if (Array.isArray(value) && value.some(function (item) {
597
- return isPojo(item);
598
- })) {
599
- headerTypes.push("".concat(key, "=\"list\""));
1102
+ case 94:
1103
+ allBodyKeysAreEmptyBinaries = bodyKeys.every(function (key) {
1104
+ var pathParts = key.split("/");
1105
+ var value = obj;
1106
+ var _iterator3 = _createForOfIteratorHelper(pathParts),
1107
+ _step3;
1108
+ try {
1109
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
1110
+ var part = _step3.value;
1111
+ if (/^\d+$/.test(part)) {
1112
+ var index = parseInt(part) - 1;
1113
+ console.log("[Body part] Getting array element at index ".concat(index, " from part ").concat(part));
1114
+ value = value[index];
1115
+ } else {
1116
+ value = value[part];
1117
+ }
1118
+ }
1119
+ } catch (err) {
1120
+ _iterator3.e(err);
1121
+ } finally {
1122
+ _iterator3.f();
600
1123
  }
601
- }
602
- _i5 = 0, _Object$entries5 = Object.entries(parts);
603
- case 8:
604
- if (!(_i5 < _Object$entries5.length)) {
605
- _context4.next = 57;
606
- break;
607
- }
608
- _Object$entries5$_i = _slicedToArray(_Object$entries5[_i5], 2), path = _Object$entries5$_i[0], content = _Object$entries5$_i[1];
609
- if (!(path === "")) {
610
- _context4.next = 53;
611
- break;
612
- }
613
- _i6 = 0, _Object$entries6 = Object.entries(content);
614
- case 12:
615
- if (!(_i6 < _Object$entries6.length)) {
616
- _context4.next = 51;
617
- break;
618
- }
619
- _Object$entries6$_i = _slicedToArray(_Object$entries6[_i6], 2), _key = _Object$entries6$_i[0], _value = _Object$entries6$_i[1];
620
- if (!isBytes(_value)) {
621
- _context4.next = 25;
622
- break;
623
- }
624
- length = _value.byteLength || _value.length || 0;
625
- if (!(length === 0)) {
626
- _context4.next = 21;
627
- break;
628
- }
629
- // Empty binaries stay in headers
630
- console.log("[encode] Empty binary field ".concat(_key, " staying in headers"));
631
- return _context4.abrupt("continue", 48);
632
- case 21:
633
- console.log("[encode] Binary field ".concat(_key, " going to body"));
634
- bodyKeys.push(_key);
635
- case 23:
636
- _context4.next = 48;
637
- break;
638
- case 25:
639
- valueStr = String(_value);
640
- type = types[_key];
641
- if (!(type === "empty-binary" && valueStr === "")) {
642
- _context4.next = 29;
1124
+ return isBytes(value) && (value.length === 0 || value.byteLength === 0);
1125
+ });
1126
+ if (!allBodyKeysAreEmptyBinaries) {
1127
+ _context6.next = 98;
643
1128
  break;
644
1129
  }
645
- return _context4.abrupt("continue", 48);
646
- case 29:
647
- _context4.next = 31;
648
- return hasNewline(valueStr);
649
- case 31:
650
- _context4.t3 = !_context4.sent;
651
- if (!_context4.t3) {
652
- _context4.next = 34;
653
- break;
1130
+ if (headerTypes.length > 0) {
1131
+ headers["ao-types"] = headerTypes.sort().join(", ");
654
1132
  }
655
- _context4.t3 = !_key.includes("/");
656
- case 34:
657
- _context4.t2 = _context4.t3;
658
- if (!_context4.t2) {
659
- _context4.next = 37;
1133
+ return _context6.abrupt("return", {
1134
+ headers: headers,
1135
+ body: undefined
1136
+ });
1137
+ case 98:
1138
+ if (!(bodyKeys.length === 1)) {
1139
+ _context6.next = 116;
660
1140
  break;
661
1141
  }
662
- _context4.t2 = Buffer.from(valueStr).byteLength <= MAX_HEADER_LENGTH;
663
- case 37:
664
- _context4.t1 = _context4.t2;
665
- if (!_context4.t1) {
666
- _context4.next = 40;
667
- break;
1142
+ singleKey = bodyKeys[0];
1143
+ pathParts = singleKey.split("/");
1144
+ value = obj;
1145
+ _iterator4 = _createForOfIteratorHelper(pathParts);
1146
+ try {
1147
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
1148
+ part = _step4.value;
1149
+ if (/^\d+$/.test(part)) {
1150
+ value = value[parseInt(part) - 1];
1151
+ } else {
1152
+ value = value[part];
1153
+ }
1154
+ }
1155
+ } catch (err) {
1156
+ _iterator4.e(err);
1157
+ } finally {
1158
+ _iterator4.f();
668
1159
  }
669
- _context4.t1 = !isPojo(_value);
670
- case 40:
671
- _context4.t0 = _context4.t1;
672
- if (!_context4.t0) {
673
- _context4.next = 43;
1160
+ otherFieldsAreEmpty = Object.entries(obj).every(function (_ref5) {
1161
+ var _ref6 = _slicedToArray(_ref5, 2),
1162
+ key = _ref6[0],
1163
+ val = _ref6[1];
1164
+ if (key === singleKey) return true;
1165
+ return Array.isArray(val) && val.length === 0 || isPojo(val) && Object.keys(val).length === 0 || isBytes(val) && (val.length === 0 || val.byteLength === 0) || typeof val === "string" && val.length === 0;
1166
+ });
1167
+ if (!(otherFieldsAreEmpty && isBytes(value) && value.length > 0)) {
1168
+ _context6.next = 116;
674
1169
  break;
675
1170
  }
676
- _context4.t0 = !(_key === "data" || _key === "body");
677
- case 43:
678
- if (!_context4.t0) {
679
- _context4.next = 47;
680
- break;
1171
+ _bodyBuffer2 = toBuffer(value);
1172
+ _bodyArrayBuffer2 = _bodyBuffer2.buffer.slice(_bodyBuffer2.byteOffset, _bodyBuffer2.byteOffset + _bodyBuffer2.byteLength);
1173
+ _context6.next = 110;
1174
+ return sha256(_bodyArrayBuffer2);
1175
+ case 110:
1176
+ _contentDigest4 = _context6.sent;
1177
+ _base4 = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest4));
1178
+ headers["content-digest"] = "sha-256=:".concat(_base4, ":");
1179
+ if (singleKey !== "body") {
1180
+ headers["inline-body-key"] = singleKey;
681
1181
  }
682
- if (typeof _value === "number") {
683
- headers[_key] = String(_value);
684
- } else if (typeof _value === "boolean") {
685
- headers[_key] = "\"".concat(_value, "\"");
686
- } else if (_value === "[]" && type === "empty_list") {
687
- headers[_key] = "[]";
688
- } else if (_value === '"null"' || _value === '"undefined"') {
689
- headers[_key] = _value;
690
- } else {
691
- headers[_key] = _value;
1182
+ if (headerTypes.length > 0) {
1183
+ headers["ao-types"] = headerTypes.sort().join(", ");
692
1184
  }
693
- _context4.next = 48;
694
- break;
695
- case 47:
696
- bodyKeys.push(_key);
697
- case 48:
698
- _i6++;
699
- _context4.next = 12;
700
- break;
701
- case 51:
702
- _context4.next = 54;
703
- break;
704
- case 53:
705
- bodyKeys.push(path);
706
- case 54:
707
- _i5++;
708
- _context4.next = 8;
709
- break;
710
- case 57:
711
- console.log("[encode] Headers before filtering:", headers, "BodyKeys:", bodyKeys);
712
- bodyKeys.sort();
1185
+ return _context6.abrupt("return", {
1186
+ headers: headers,
1187
+ body: value
1188
+ });
1189
+ case 116:
1190
+ // Sort body keys: main array comes first, then element parts by index
1191
+ sortedBodyKeys = bodyKeys.sort(function (a, b) {
1192
+ var aIsArrayElement = /\/\d+$/.test(a);
1193
+ var bIsArrayElement = /\/\d+$/.test(b);
1194
+ var aBase = a.split("/")[0];
1195
+ var bBase = b.split("/")[0];
713
1196
 
714
- // Add types for values in headers
715
- for (_i7 = 0, _Object$entries7 = Object.entries(headers); _i7 < _Object$entries7.length; _i7++) {
716
- _Object$entries7$_i = _slicedToArray(_Object$entries7[_i7], 2), _key2 = _Object$entries7$_i[0], _value2 = _Object$entries7$_i[1];
717
- _type = types[_key2];
718
- if (_type) {
719
- headerTypes.push("".concat(_key2, "=\"").concat(_type, "\""));
1197
+ // If both are for the same array
1198
+ if (aBase === bBase) {
1199
+ // Main array comes before element parts
1200
+ if (!aIsArrayElement && bIsArrayElement) {
1201
+ return -1; // main array comes first
1202
+ }
1203
+ if (aIsArrayElement && !bIsArrayElement) {
1204
+ return 1; // element parts come after
1205
+ }
1206
+ // Both are elements - sort by index
1207
+ if (aIsArrayElement && bIsArrayElement) {
1208
+ var aIndex = parseInt(a.split("/")[1]);
1209
+ var bIndex = parseInt(b.split("/")[1]);
1210
+ return aIndex - bIndex;
1211
+ }
1212
+ return a.localeCompare(b);
720
1213
  }
721
- }
722
1214
 
723
- // Add types for empty values not in headers
724
- for (_i8 = 0, _Object$entries8 = Object.entries(types); _i8 < _Object$entries8.length; _i8++) {
725
- _Object$entries8$_i = _slicedToArray(_Object$entries8[_i8], 2), _key3 = _Object$entries8$_i[0], _type2 = _Object$entries8$_i[1];
726
- if (!_key3.includes("/") && _type2.startsWith("empty") && !headers[_key3]) {
727
- headerTypes.push("".concat(_key3, "=\"").concat(_type2.replace("empty_", "empty-"), "\""));
1215
+ // Different arrays, sort by base name
1216
+ return a.localeCompare(b);
1217
+ }); // Check if we have the special case where data contains body with bytes and body contains data with bytes
1218
+ hasSpecialDataBody = sortedBodyKeys.includes("data") && sortedBodyKeys.includes("body") && obj.data && obj.data.body && isBytes(obj.data.body) && obj.body && obj.body.data && isBytes(obj.body.data);
1219
+ headers["body-keys"] = sortedBodyKeys.map(function (k) {
1220
+ return "\"".concat(k, "\"");
1221
+ }).join(", ");
1222
+ if (!hasSpecialDataBody) {
1223
+ if (sortedBodyKeys.includes("body") && sortedBodyKeys.length === 1) {
1224
+ headers["inline-body-key"] = "body";
728
1225
  }
729
1226
  }
730
1227
  if (headerTypes.length > 0) {
731
1228
  headers["ao-types"] = headerTypes.sort().join(", ");
732
1229
  }
733
- if (!(bodyKeys.length > 0)) {
734
- _context4.next = 185;
735
- break;
736
- }
737
- headers["body-keys"] = encode_body_keys(bodyKeys);
738
- if (bodyKeys.includes("data") || bodyKeys.includes("body")) {
739
- inlineKey = bodyKeys.find(function (k) {
740
- return k === "data" || k === "body";
741
- });
742
- headers["inline-body-key"] = inlineKey;
743
- }
1230
+ bodyParts = [];
1231
+ _iterator5 = _createForOfIteratorHelper(sortedBodyKeys);
1232
+ _context6.prev = 123;
1233
+ _loop5 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop5() {
1234
+ var bodyKey, lines, pathParts, value, parent, _i11, _part, _buffer, headerText, hasOnlyEmptyElements, hasOnlyNonEmptyObjects, hasOnlyEmptyObjects, hasObjects, hasArrays, nonObjectItems, _fieldLines, _partTypes, _isInline, orderedLines, _orderedLines, isLastBodyPart, hasOnlyTypes, indicesWithOwnParts, hasNestedObjectParts, fieldLines, partTypes, hasEmptyStrings, hasEmptyObjects, hasObjectsWithOnlyEmptyValues, isMixedArray, _isInline2, _orderedLines2, _iterator6, _step6, line, _orderedLines3, _iterator7, _step7, _line, isInline, content;
1235
+ return _regeneratorRuntime().wrap(function _loop5$(_context5) {
1236
+ while (1) switch (_context5.prev = _context5.next) {
1237
+ case 0:
1238
+ bodyKey = _step5.value;
1239
+ console.log("\n[Body part] Processing bodyKey: ".concat(bodyKey));
1240
+ lines = [];
1241
+ pathParts = bodyKey.split("/");
1242
+ value = obj;
1243
+ parent = null;
1244
+ for (_i11 = 0; _i11 < pathParts.length; _i11++) {
1245
+ parent = value;
1246
+ _part = pathParts[_i11];
1247
+ if (/^\d+$/.test(_part)) {
1248
+ value = value[parseInt(_part) - 1];
1249
+ } else {
1250
+ value = value[_part];
1251
+ }
1252
+ }
1253
+ console.log("[Body part] Value at ".concat(bodyKey, ":"), JSON.stringify(value));
744
1254
 
745
- // Create multipart body parts
746
- _iterator = _createForOfIteratorHelper(bodyKeys);
747
- _context4.prev = 66;
748
- _iterator.s();
749
- case 68:
750
- if ((_step = _iterator.n()).done) {
751
- _context4.next = 133;
752
- break;
753
- }
754
- _path = _step.value;
755
- console.log("[encode] Processing bodyKey: ".concat(_path));
1255
+ // Special handling for empty strings in arrays
1256
+ if (!(typeof value === "string" && value === "" && pathParts.length > 1)) {
1257
+ _context5.next = 15;
1258
+ break;
1259
+ }
1260
+ console.log("[Body part] Creating part for empty string at ".concat(bodyKey));
1261
+ lines.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1262
+ lines.push("");
1263
+ lines.push("");
1264
+ bodyParts.push(new Blob([lines.join("\r\n")]));
1265
+ return _context5.abrupt("return", 0);
1266
+ case 15:
1267
+ if (!isBytes(value)) {
1268
+ _context5.next = 22;
1269
+ break;
1270
+ }
1271
+ console.log("[Body part] Creating part for binary at ".concat(bodyKey));
1272
+ lines.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1273
+ _buffer = toBuffer(value);
1274
+ headerText = lines.join("\r\n") + "\r\n\r\n";
1275
+ bodyParts.push(new Blob([headerText, _buffer]));
1276
+ return _context5.abrupt("return", 0);
1277
+ case 22:
1278
+ if (!Array.isArray(value)) {
1279
+ _context5.next = 52;
1280
+ break;
1281
+ }
1282
+ hasOnlyEmptyElements = value.length > 0 && value.every(function (item) {
1283
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
1284
+ });
1285
+ hasOnlyNonEmptyObjects = value.length > 0 && value.every(function (item) {
1286
+ return isPojo(item) && Object.keys(item).length > 0;
1287
+ });
1288
+ hasOnlyEmptyObjects = value.length > 0 && value.every(function (item) {
1289
+ return isPojo(item) && Object.keys(item).length === 0;
1290
+ });
1291
+ hasObjects = value.some(function (item) {
1292
+ return isPojo(item);
1293
+ });
1294
+ hasArrays = value.some(function (item) {
1295
+ return Array.isArray(item);
1296
+ });
1297
+ nonObjectItems = value.map(function (item, index) {
1298
+ return {
1299
+ item: item,
1300
+ index: index + 1
1301
+ };
1302
+ }).filter(function (_ref7) {
1303
+ var item = _ref7.item;
1304
+ return !isPojo(item);
1305
+ });
1306
+ if (!hasOnlyNonEmptyObjects) {
1307
+ _context5.next = 31;
1308
+ break;
1309
+ }
1310
+ return _context5.abrupt("return", 0);
1311
+ case 31:
1312
+ if (!hasOnlyEmptyElements) {
1313
+ _context5.next = 38;
1314
+ break;
1315
+ }
1316
+ _fieldLines = [];
1317
+ _partTypes = []; // Process items for type information
1318
+ value.forEach(function (item, idx) {
1319
+ var index = idx + 1;
1320
+ if (Array.isArray(item) && item.length === 0) {
1321
+ _partTypes.push("".concat(index, "=\"empty-list\""));
1322
+ } else if (isPojo(item) && Object.keys(item).length === 0) {
1323
+ _partTypes.push("".concat(index, "=\"empty-message\""));
1324
+ } else if (typeof item === "string" && item === "") {
1325
+ _partTypes.push("".concat(index, "=\"empty-binary\""));
1326
+ }
1327
+ });
1328
+ _isInline = bodyKey === "body" && headers["inline-body-key"] === "body";
1329
+ if (_isInline) {
1330
+ orderedLines = [];
1331
+ if (_partTypes.length > 0) {
1332
+ orderedLines.push("ao-types: ".concat(_partTypes.sort(function (a, b) {
1333
+ var aNum = parseInt(a.split("=")[0]);
1334
+ var bNum = parseInt(b.split("=")[0]);
1335
+ return aNum - bNum;
1336
+ }).join(", ")));
1337
+ }
1338
+ orderedLines.push("content-disposition: inline");
1339
+ orderedLines.push("");
1340
+ bodyParts.push(new Blob([orderedLines.join("\r\n")]));
1341
+ } else {
1342
+ _orderedLines = [];
1343
+ if (_partTypes.length > 0) {
1344
+ _orderedLines.push("ao-types: ".concat(_partTypes.sort(function (a, b) {
1345
+ var aNum = parseInt(a.split("=")[0]);
1346
+ var bNum = parseInt(b.split("=")[0]);
1347
+ return aNum - bNum;
1348
+ }).join(", ")));
1349
+ }
1350
+ _orderedLines.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
756
1351
 
757
- // Handle root-level fields
758
- if (_path.includes("/")) {
759
- _context4.next = 94;
760
- break;
761
- }
762
- // This is a root-level field
763
- fieldValue = parts[""] && parts[""][_path];
764
- console.log("[encode] Root field ".concat(_path, " value:"), fieldValue);
765
- if (!(fieldValue !== undefined)) {
766
- _context4.next = 94;
767
- break;
768
- }
769
- if (!isBytes(fieldValue)) {
770
- _context4.next = 84;
771
- break;
772
- }
773
- // Binary field - create a simple multipart section
774
- // The format should be:
775
- // content-disposition: form-data;name="fieldname"
776
- // [empty line]
777
- // [binary data]
778
- headerStr = "content-disposition: form-data;name=\"".concat(_path, "\"\r\n\r\n");
779
- headerBlob = new Blob([headerStr]);
780
- dataBlob = new Blob([fieldValue]);
781
- bodyParts.push(new Blob([headerBlob, dataBlob]));
782
- console.log("[encode] Added binary field ".concat(_path, " to bodyParts"));
783
- return _context4.abrupt("continue", 131);
784
- case 84:
785
- // Non-binary root field that needs to go in body
786
- _lines = []; // Check if this is an inline key
787
- _isInlineKey = _path === "data" || _path === "body";
788
- if (_isInlineKey) {
789
- _lines.push("content-disposition: inline");
790
- } else {
791
- _lines.push("content-disposition: form-data;name=\"".concat(_path, "\""));
792
- }
1352
+ // Check if this is the last body part
1353
+ isLastBodyPart = sortedBodyKeys.indexOf(bodyKey) === sortedBodyKeys.length - 1;
1354
+ hasOnlyTypes = _partTypes.length > 0 && _fieldLines.length === 0;
1355
+ if (isLastBodyPart && hasOnlyTypes) {
1356
+ // Don't add empty line for last part with only types
1357
+ bodyParts.push(new Blob([_orderedLines.join("\r\n")]));
1358
+ } else {
1359
+ _orderedLines.push("");
1360
+ bodyParts.push(new Blob([_orderedLines.join("\r\n")]));
1361
+ }
1362
+ }
1363
+ return _context5.abrupt("return", 0);
1364
+ case 38:
1365
+ // Build list of which indices have their own parts
1366
+ indicesWithOwnParts = new Set();
1367
+ sortedBodyKeys.forEach(function (key) {
1368
+ if (key.startsWith(bodyKey + "/")) {
1369
+ var subPath = key.substring(bodyKey.length + 1);
1370
+ var match = subPath.match(/^(\d+)/);
1371
+ if (match) {
1372
+ indicesWithOwnParts.add(parseInt(match[1]));
1373
+ }
1374
+ }
1375
+ });
793
1376
 
794
- // Add the field type if available
795
- _type3 = types[_path];
796
- if (_type3) {
797
- _lines.push("ao-types: ".concat(_path, "=\"").concat(_type3, "\""));
798
- }
1377
+ // Check if this array contains sub-arrays with objects that have their own parts
1378
+ hasNestedObjectParts = sortedBodyKeys.some(function (key) {
1379
+ return key.startsWith(bodyKey + "/") && key.split("/").length > pathParts.length + 1;
1380
+ });
1381
+ fieldLines = [];
1382
+ partTypes = []; // For arrays that contain sub-arrays with objects, we need to add type info for the sub-arrays
1383
+ if (hasNestedObjectParts) {
1384
+ value.forEach(function (item, idx) {
1385
+ var index = idx + 1;
1386
+ if (Array.isArray(item)) {
1387
+ partTypes.push("".concat(index, "=\"list\""));
1388
+ }
1389
+ });
1390
+ }
799
1391
 
800
- // Add empty line before content
801
- _lines.push("");
1392
+ // Check if this array has mixed content with empty strings/objects
1393
+ hasEmptyStrings = value.some(function (item) {
1394
+ return typeof item === "string" && item === "";
1395
+ });
1396
+ hasEmptyObjects = value.some(function (item) {
1397
+ return isPojo(item) && Object.keys(item).length === 0;
1398
+ }); // Check if we have objects with only empty values (like {empty: ""})
1399
+ hasObjectsWithOnlyEmptyValues = value.some(function (item) {
1400
+ if (!isPojo(item) || Object.keys(item).length === 0) return false;
1401
+ return Object.values(item).every(function (v) {
1402
+ return typeof v === "string" && v === "" || Array.isArray(v) && v.length === 0 || isPojo(v) && Object.keys(v).length === 0;
1403
+ });
1404
+ });
1405
+ isMixedArray = hasObjects && (hasEmptyStrings || hasEmptyObjects) && !hasObjectsWithOnlyEmptyValues; // Process ALL items for type information
1406
+ value.forEach(function (item, idx) {
1407
+ var index = idx + 1;
802
1408
 
803
- // Add the actual value
804
- _lines.push(String(fieldValue));
805
- bodyParts.push(new Blob([_lines.join("\r\n")]));
806
- console.log("[encode] Added non-binary field ".concat(_path, " to bodyParts"));
807
- return _context4.abrupt("continue", 131);
808
- case 94:
809
- // Handle nested paths
810
- _content = parts[_path];
811
- if (_content) {
812
- _context4.next = 98;
813
- break;
814
- }
815
- console.log("[encode] No content found for path: ".concat(_path));
816
- return _context4.abrupt("continue", 131);
817
- case 98:
818
- lines = [];
819
- binaryParts = [];
820
- isInlineKey = (_path === "data" || _path === "body") && !_path.includes("/");
821
- sortedKeys = Object.keys(_content).sort(); // Collect ao-types for this part
822
- partTypes = [];
823
- _iterator3 = _createForOfIteratorHelper(sortedKeys);
824
- try {
825
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
826
- _key4 = _step3.value;
827
- fullPath = _path ? "".concat(_path, "/").concat(_key4) : _key4;
828
- _type4 = types[fullPath];
829
- if (_type4) {
830
- partTypes.push("".concat(_key4, "=\"").concat(_type4, "\""));
831
- }
832
- }
833
- } catch (err) {
834
- _iterator3.e(err);
835
- } finally {
836
- _iterator3.f();
837
- }
838
- if (partTypes.length > 0) {
839
- lines.push("ao-types: ".concat(partTypes.sort().join(", ")));
840
- }
841
- if (isInlineKey) {
842
- lines.push("content-disposition: inline");
843
- } else {
844
- lines.push("content-disposition: form-data;name=\"".concat(_path, "\""));
845
- }
1409
+ // Skip type info for elements that have their own parts
1410
+ if (indicesWithOwnParts.has(index)) {
1411
+ return;
1412
+ }
846
1413
 
847
- // Add content fields
848
- _iterator4 = _createForOfIteratorHelper(sortedKeys);
849
- _context4.prev = 108;
850
- _iterator4.s();
851
- case 110:
852
- if ((_step4 = _iterator4.n()).done) {
853
- _context4.next = 122;
854
- break;
855
- }
856
- _key5 = _step4.value;
857
- _value3 = _content[_key5];
858
- _fullPath = _path ? "".concat(_path, "/").concat(_key5) : _key5;
859
- _type5 = types[_fullPath];
860
- if (!(_type5 === "empty-message" || _type5 === "empty-list" || _type5 === "empty-binary")) {
861
- _context4.next = 117;
862
- break;
863
- }
864
- return _context4.abrupt("continue", 120);
865
- case 117:
866
- if (!((_value3 === "[]" || _value3 === "{}" || _value3 === "") && _type5 && _type5.startsWith("empty"))) {
867
- _context4.next = 119;
868
- break;
869
- }
870
- return _context4.abrupt("continue", 120);
871
- case 119:
872
- if (isBytes(_value3)) {
873
- binaryParts.push({
874
- key: _key5,
875
- value: _value3
876
- });
877
- } else if (_type5 === "atom" && typeof _value3 === "boolean") {
878
- lines.push("".concat(_key5, ": \"").concat(_value3, "\""));
879
- } else if (_typeof(_value3) === "symbol") {
880
- // Handle Symbol values
881
- symbolValue = _value3.description || "symbol";
882
- lines.push("".concat(_key5, ": ").concat(symbolValue));
883
- } else {
884
- lines.push("".concat(_key5, ": ").concat(_value3));
885
- }
886
- case 120:
887
- _context4.next = 110;
888
- break;
889
- case 122:
890
- _context4.next = 127;
891
- break;
892
- case 124:
893
- _context4.prev = 124;
894
- _context4.t4 = _context4["catch"](108);
895
- _iterator4.e(_context4.t4);
896
- case 127:
897
- _context4.prev = 127;
898
- _iterator4.f();
899
- return _context4.finish(127);
900
- case 130:
901
- if (lines.length >= 1 || binaryParts.length > 0) {
902
- if (binaryParts.length > 0) {
903
- allParts = []; // Add text headers first
904
- if (lines.length > 0) {
905
- allParts.push(lines.join("\r\n"));
906
- allParts.push("\r\n");
907
- }
1414
+ // If we have nested object parts and this is an array with objects, skip processing it inline
1415
+ if (hasNestedObjectParts && Array.isArray(item) && item.some(function (subItem) {
1416
+ return isPojo(subItem);
1417
+ })) {
1418
+ // Type info already added above
1419
+ return;
1420
+ }
1421
+
1422
+ // For all arrays (not just mixed ones), we need to process all items
1423
+ if (typeof item === "string" && item === "") {
1424
+ // Empty strings get type annotation but no field line
1425
+ partTypes.push("".concat(index, "=\"empty-binary\""));
1426
+ } else if (isPojo(item) && Object.keys(item).length === 0) {
1427
+ // Empty objects don't get field lines but do get type annotations
1428
+ partTypes.push("".concat(index, "=\"empty-message\""));
1429
+ } else if (isPojo(item)) {
1430
+ // Non-empty objects might have parts
1431
+ } else if (Array.isArray(item)) {
1432
+ if (item.length === 0) {
1433
+ // Empty arrays don't get field lines but do get type annotations
1434
+ partTypes.push("".concat(index, "=\"empty-list\""));
1435
+ } else {
1436
+ partTypes.push("".concat(index, "=\"list\""));
1437
+ // Encode array
1438
+ var encodedItems = item.map(function (subItem) {
1439
+ if (typeof subItem === "number") {
1440
+ if (Number.isInteger(subItem)) {
1441
+ return "\"(ao-type-integer) ".concat(subItem, "\"");
1442
+ } else {
1443
+ return "\"(ao-type-float) ".concat(formatFloat(subItem), "\"");
1444
+ }
1445
+ } else if (typeof subItem === "string") {
1446
+ return "\"".concat(subItem, "\"");
1447
+ } else if (subItem === null) {
1448
+ return "\"(ao-type-atom) \\\"null\\\"\"";
1449
+ } else if (subItem === undefined) {
1450
+ return "\"(ao-type-atom) \\\"undefined\\\"\"";
1451
+ } else if (_typeof(subItem) === "symbol") {
1452
+ var desc = subItem.description || "Symbol.for()";
1453
+ return "\"(ao-type-atom) \\\"".concat(desc, "\\\"\"");
1454
+ } else if (typeof subItem === "boolean") {
1455
+ return "\"(ao-type-atom) \\\"".concat(subItem, "\\\"\"");
1456
+ } else if (Array.isArray(subItem)) {
1457
+ // Use the full encodeArrayItem for nested arrays
1458
+ return encodeArrayItem(subItem);
1459
+ } else if (isBytes(subItem)) {
1460
+ var _buffer2 = toBuffer(subItem);
1461
+ if (_buffer2.length === 0 || _buffer2.byteLength === 0) {
1462
+ return "\"\"";
1463
+ }
1464
+ return "\"(ao-type-binary)\"";
1465
+ } else if (isPojo(subItem)) {
1466
+ var json = JSON.stringify(subItem);
1467
+ var escaped = json.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1468
+ return "\"(ao-type-map) ".concat(escaped, "\"");
1469
+ } else {
1470
+ return "\"".concat(String(subItem), "\"");
1471
+ }
1472
+ }).join(", ");
1473
+ fieldLines.push("".concat(index, ": ").concat(encodedItems));
1474
+ }
1475
+ } else if (typeof item === "number") {
1476
+ if (Number.isInteger(item)) {
1477
+ partTypes.push("".concat(index, "=\"integer\""));
1478
+ fieldLines.push("".concat(index, ": ").concat(item));
1479
+ } else {
1480
+ partTypes.push("".concat(index, "=\"float\""));
1481
+ fieldLines.push("".concat(index, ": ").concat(formatFloat(item)));
1482
+ }
1483
+ } else if (typeof item === "string") {
1484
+ // Non-empty strings just get field lines, no type annotation
1485
+ fieldLines.push("".concat(index, ": ").concat(item));
1486
+ } else if (item === null || item === undefined || _typeof(item) === "symbol" || typeof item === "boolean") {
1487
+ partTypes.push("".concat(index, "=\"atom\""));
1488
+ if (item === null) {
1489
+ fieldLines.push("".concat(index, ": null"));
1490
+ } else if (item === undefined) {
1491
+ fieldLines.push("".concat(index, ": undefined"));
1492
+ } else if (_typeof(item) === "symbol") {
1493
+ var desc = item.description || "Symbol.for()";
1494
+ fieldLines.push("".concat(index, ": ").concat(desc));
1495
+ } else {
1496
+ fieldLines.push("".concat(index, ": ").concat(item));
1497
+ }
1498
+ } else if (isBytes(item)) {
1499
+ var _buffer3 = toBuffer(item);
1500
+ if (_buffer3.length === 0) {
1501
+ partTypes.push("".concat(index, "=\"empty-binary\""));
1502
+ } else {
1503
+ partTypes.push("".concat(index, "=\"binary\""));
1504
+ }
1505
+ }
1506
+ });
1507
+ _isInline2 = bodyKey === "body" && headers["inline-body-key"] === "body";
1508
+ if (_isInline2) {
1509
+ _orderedLines2 = [];
1510
+ if (partTypes.length > 0) {
1511
+ _orderedLines2.push("ao-types: ".concat(partTypes.sort(function (a, b) {
1512
+ var aNum = parseInt(a.split("=")[0]);
1513
+ var bNum = parseInt(b.split("=")[0]);
1514
+ return aNum - bNum;
1515
+ }).join(", ")));
1516
+ }
1517
+ _orderedLines2.push("content-disposition: inline");
1518
+ if (fieldLines.length > 0) {
1519
+ _orderedLines2.push("");
1520
+ _iterator6 = _createForOfIteratorHelper(fieldLines);
1521
+ try {
1522
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
1523
+ line = _step6.value;
1524
+ _orderedLines2.push(line);
1525
+ }
1526
+ } catch (err) {
1527
+ _iterator6.e(err);
1528
+ } finally {
1529
+ _iterator6.f();
1530
+ }
1531
+ }
1532
+ bodyParts.push(new Blob([_orderedLines2.join("\r\n") + "\r\n"]));
1533
+ } else {
1534
+ // Put ao-types first, then content-disposition, then field lines
1535
+ _orderedLines3 = [];
1536
+ if (partTypes.length > 0) {
1537
+ _orderedLines3.push("ao-types: ".concat(partTypes.sort(function (a, b) {
1538
+ var aNum = parseInt(a.split("=")[0]);
1539
+ var bNum = parseInt(b.split("=")[0]);
1540
+ return aNum - bNum;
1541
+ }).join(", ")));
1542
+ }
1543
+ _orderedLines3.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1544
+
1545
+ // Add field lines directly without blank line
1546
+ _iterator7 = _createForOfIteratorHelper(fieldLines);
1547
+ try {
1548
+ for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
1549
+ _line = _step7.value;
1550
+ _orderedLines3.push(_line);
1551
+ }
908
1552
 
909
- // Add binary data
910
- _iterator5 = _createForOfIteratorHelper(binaryParts);
911
- try {
912
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
913
- binaryPart = _step5.value;
914
- allParts.push("".concat(binaryPart.key, ": "));
915
- allParts.push(binaryPart.value);
916
- if (binaryParts.indexOf(binaryPart) < binaryParts.length - 1) {
917
- allParts.push("\r\n");
1553
+ // Add trailing blank line if we have field lines
1554
+ } catch (err) {
1555
+ _iterator7.e(err);
1556
+ } finally {
1557
+ _iterator7.f();
1558
+ }
1559
+ if (fieldLines.length > 0) {
1560
+ bodyParts.push(new Blob([_orderedLines3.join("\r\n") + "\r\n"]));
1561
+ } else {
1562
+ bodyParts.push(new Blob([_orderedLines3.join("\r\n")]));
1563
+ }
918
1564
  }
919
- }
920
- } catch (err) {
921
- _iterator5.e(err);
922
- } finally {
923
- _iterator5.f();
1565
+ return _context5.abrupt("return", 0);
1566
+ case 52:
1567
+ // This should not be reached for binary values as they're handled above
1568
+ isInline = bodyKey === "body" && headers["inline-body-key"] === "body";
1569
+ if (isInline) {
1570
+ lines.push("content-disposition: inline");
1571
+ } else {
1572
+ lines.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1573
+ }
1574
+ if (typeof value === "string") {
1575
+ lines.push("");
1576
+ lines.push(value);
1577
+ bodyParts.push(new Blob([lines.join("\r\n")]));
1578
+ } else if (typeof value === "boolean" || typeof value === "number" || value === null || value === undefined || _typeof(value) === "symbol") {
1579
+ if (typeof value === "boolean") {
1580
+ content = "\"".concat(value, "\"");
1581
+ } else if (typeof value === "number") {
1582
+ content = String(value);
1583
+ } else if (value === null) {
1584
+ content = '"null"';
1585
+ } else if (value === undefined) {
1586
+ content = '"undefined"';
1587
+ } else if (_typeof(value) === "symbol") {
1588
+ content = "\"".concat(value.description || "Symbol.for()", "\"");
1589
+ }
1590
+ lines.push("");
1591
+ lines.push(content);
1592
+ bodyParts.push(new Blob([lines.join("\r\n")]));
1593
+ }
1594
+ case 55:
1595
+ case "end":
1596
+ return _context5.stop();
924
1597
  }
925
- bodyParts.push(new Blob(allParts));
926
- } else {
927
- bodyParts.push(new Blob([lines.join("\r\n")]));
928
- }
1598
+ }, _loop5);
1599
+ });
1600
+ _iterator5.s();
1601
+ case 126:
1602
+ if ((_step5 = _iterator5.n()).done) {
1603
+ _context6.next = 133;
1604
+ break;
1605
+ }
1606
+ return _context6.delegateYield(_loop5(), "t2", 128);
1607
+ case 128:
1608
+ _ret = _context6.t2;
1609
+ if (!(_ret === 0)) {
1610
+ _context6.next = 131;
1611
+ break;
929
1612
  }
1613
+ return _context6.abrupt("continue", 131);
930
1614
  case 131:
931
- _context4.next = 68;
1615
+ _context6.next = 126;
932
1616
  break;
933
1617
  case 133:
934
- _context4.next = 138;
1618
+ _context6.next = 138;
935
1619
  break;
936
1620
  case 135:
937
- _context4.prev = 135;
938
- _context4.t5 = _context4["catch"](66);
939
- _iterator.e(_context4.t5);
1621
+ _context6.prev = 135;
1622
+ _context6.t3 = _context6["catch"](123);
1623
+ _iterator5.e(_context6.t3);
940
1624
  case 138:
941
- _context4.prev = 138;
942
- _iterator.f();
943
- return _context4.finish(138);
1625
+ _context6.prev = 138;
1626
+ _iterator5.f();
1627
+ return _context6.finish(138);
944
1628
  case 141:
945
- console.log("[encode] Total bodyParts: ".concat(bodyParts.length));
946
-
947
- // Create boundary based on parts content
948
- partsForBoundary = [];
949
- _iterator2 = _createForOfIteratorHelper(bodyParts);
950
- _context4.prev = 144;
951
- _iterator2.s();
952
- case 146:
953
- if ((_step2 = _iterator2.n()).done) {
954
- _context4.next = 154;
955
- break;
1629
+ // Add the special data/body part if needed
1630
+ if (hasSpecialDataBody && obj.data && obj.data.body && isBytes(obj.data.body)) {
1631
+ buffer = toBuffer(obj.data.body);
1632
+ specialPart = ["content-disposition: form-data;name=\"data/body\"", "", ""].join("\r\n");
1633
+ bodyParts.push(new Blob([specialPart, buffer]));
956
1634
  }
957
- part = _step2.value;
958
- _context4.next = 150;
959
- return part.text();
960
- case 150:
961
- partContent = _context4.sent;
962
- partsForBoundary.push(partContent);
963
- case 152:
964
- _context4.next = 146;
965
- break;
966
- case 154:
967
- _context4.next = 159;
968
- break;
969
- case 156:
970
- _context4.prev = 156;
971
- _context4.t6 = _context4["catch"](144);
972
- _iterator2.e(_context4.t6);
973
- case 159:
974
- _context4.prev = 159;
975
- _iterator2.f();
976
- return _context4.finish(159);
977
- case 162:
978
- allPartsContent = partsForBoundary.join("");
979
- console.log("[encode] All parts content length: ".concat(allPartsContent.length));
980
- allPartsBuffer = Buffer.from(allPartsContent);
981
- _context4.next = 167;
982
- return sha256(allPartsBuffer.buffer.slice(allPartsBuffer.byteOffset, allPartsBuffer.byteOffset + allPartsBuffer.byteLength));
983
- case 167:
984
- hashResult = _context4.sent;
985
- boundary = _base64url["default"].encode(Buffer.from(hashResult)); // Create final multipart body
1635
+ _context6.next = 144;
1636
+ return Promise.all(bodyParts.map(function (part) {
1637
+ return part.text();
1638
+ }));
1639
+ case 144:
1640
+ partsContent = _context6.sent;
1641
+ allContent = partsContent.join("");
1642
+ _context6.next = 148;
1643
+ return sha256(new TextEncoder().encode(allContent));
1644
+ case 148:
1645
+ boundaryHash = _context6.sent;
1646
+ boundary = _base64url["default"].encode(Buffer.from(boundaryHash));
986
1647
  finalParts = [];
987
1648
  for (i = 0; i < bodyParts.length; i++) {
988
- finalParts.push("--".concat(boundary));
989
- finalParts.push("\r\n");
990
- finalParts.push(bodyParts[i]);
991
- if (i < bodyParts.length - 1) {
992
- finalParts.push("\r\n");
1649
+ if (i === 0) {
1650
+ finalParts.push(new Blob(["--".concat(boundary, "\r\n")]));
1651
+ } else {
1652
+ finalParts.push(new Blob(["\r\n--".concat(boundary, "\r\n")]));
993
1653
  }
1654
+ finalParts.push(bodyParts[i]);
994
1655
  }
995
- finalParts.push("\r\n--".concat(boundary, "--"));
1656
+ finalParts.push(new Blob(["\r\n--".concat(boundary, "--")]));
996
1657
  headers["content-type"] = "multipart/form-data; boundary=\"".concat(boundary, "\"");
997
1658
  body = new Blob(finalParts);
998
- _context4.next = 176;
1659
+ _context6.next = 157;
999
1660
  return body.arrayBuffer();
1000
- case 176:
1001
- finalContent = _context4.sent;
1002
- _context4.next = 179;
1661
+ case 157:
1662
+ finalContent = _context6.sent;
1663
+ if (!(finalContent.byteLength > 0)) {
1664
+ _context6.next = 164;
1665
+ break;
1666
+ }
1667
+ _context6.next = 161;
1003
1668
  return sha256(finalContent);
1004
- case 179:
1005
- contentDigest = _context4.sent;
1006
- base64 = _base64url["default"].toBase64(_base64url["default"].encode(contentDigest));
1007
- headers["content-digest"] = "sha-256=:".concat(base64, ":");
1669
+ case 161:
1670
+ _contentDigest5 = _context6.sent;
1671
+ _base5 = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest5));
1672
+ headers["content-digest"] = "sha-256=:".concat(_base5, ":");
1673
+ case 164:
1008
1674
  headers["content-length"] = String(finalContent.byteLength);
1009
- console.log("[encode] FINAL - headers:", headers, "body:", body);
1010
- return _context4.abrupt("return", {
1675
+ console.log("=== ENCODE END ===\n");
1676
+ return _context6.abrupt("return", {
1011
1677
  headers: headers,
1012
1678
  body: body
1013
1679
  });
1014
- case 185:
1015
- console.log("[encode] FINAL - headers:", headers, "body:", undefined);
1016
- return _context4.abrupt("return", {
1017
- headers: headers,
1018
- body: undefined
1019
- });
1020
- case 187:
1680
+ case 167:
1021
1681
  case "end":
1022
- return _context4.stop();
1682
+ return _context6.stop();
1023
1683
  }
1024
- }, _callee4, null, [[66, 135, 138, 141], [108, 124, 127, 130], [144, 156, 159, 162]]);
1684
+ }, _callee3, null, [[123, 135, 138, 141]]);
1025
1685
  }));
1026
- return _encodeMultipart.apply(this, arguments);
1686
+ return _encode.apply(this, arguments);
1027
1687
  }
1028
- function enc(_x4) {
1688
+ function enc(_x3) {
1029
1689
  return _enc.apply(this, arguments);
1030
1690
  }
1031
1691
  function _enc() {
1032
- _enc = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(fields) {
1033
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1034
- while (1) switch (_context5.prev = _context5.next) {
1692
+ _enc = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(fields) {
1693
+ return _regeneratorRuntime().wrap(function _callee4$(_context7) {
1694
+ while (1) switch (_context7.prev = _context7.next) {
1035
1695
  case 0:
1036
- _context5.next = 2;
1696
+ _context7.next = 2;
1037
1697
  return encode(fields);
1038
1698
  case 2:
1039
- return _context5.abrupt("return", _context5.sent);
1699
+ return _context7.abrupt("return", _context7.sent);
1040
1700
  case 3:
1041
1701
  case "end":
1042
- return _context5.stop();
1702
+ return _context7.stop();
1043
1703
  }
1044
- }, _callee5);
1704
+ }, _callee4);
1045
1705
  }));
1046
1706
  return _enc.apply(this, arguments);
1047
1707
  }