wao 0.27.1 → 0.27.3

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
@@ -114,13 +114,14 @@ function _sha() {
114
114
  return _sha.apply(this, arguments);
115
115
  }
116
116
  function formatFloat(num) {
117
- // Format float in scientific notation with proper padding
118
117
  var exp = num.toExponential(20);
119
- // Replace "1.23e+0" with "1.23e+00"
120
118
  exp = exp.replace(/e\+(\d)$/, "e+0$1");
121
119
  exp = exp.replace(/e-(\d)$/, "e-0$1");
122
120
  return exp;
123
121
  }
122
+ function hasNonAscii(str) {
123
+ return /[^\x00-\x7F]/.test(str);
124
+ }
124
125
  function encodeArrayItem(item) {
125
126
  if (typeof item === "number") {
126
127
  if (Number.isInteger(item)) {
@@ -140,7 +141,6 @@ function encodeArrayItem(item) {
140
141
  } else if (typeof item === "boolean") {
141
142
  return "\"(ao-type-atom) \\\"".concat(item, "\\\"\"");
142
143
  } else if (Array.isArray(item)) {
143
- // Nested array
144
144
  var nestedItems = item.map(function (nestedItem) {
145
145
  if (typeof nestedItem === "number") {
146
146
  if (Number.isInteger(nestedItem)) {
@@ -152,21 +152,64 @@ function encodeArrayItem(item) {
152
152
  return "\\\"".concat(nestedItem, "\\\"");
153
153
  } else if (nestedItem === null) {
154
154
  return "\\\"(ao-type-atom) \\\\\\\"null\\\\\\\"\\\"";
155
+ } else if (nestedItem === undefined) {
156
+ return "\\\"(ao-type-atom) \\\\\\\"undefined\\\\\\\"\\\"";
155
157
  } else if (_typeof(nestedItem) === "symbol") {
156
158
  var _desc = nestedItem.description || "Symbol.for()";
157
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, "\\\"");
158
203
  } else {
159
204
  return "\\\"".concat(String(nestedItem), "\\\"");
160
205
  }
161
206
  }).join(", ");
162
207
  return "\"(ao-type-list) ".concat(nestedItems, "\"");
163
208
  } else if (isBytes(item)) {
164
- // For empty binaries in arrays, return empty string
165
209
  var buffer = toBuffer(item);
166
210
  if (buffer.length === 0 || buffer.byteLength === 0) {
167
211
  return "\"\"";
168
212
  }
169
- // For non-empty binaries, we can't include them in headers
170
213
  return "\"(ao-type-binary)\"";
171
214
  } else if (isPojo(item)) {
172
215
  var json = JSON.stringify(item);
@@ -176,63 +219,144 @@ function encodeArrayItem(item) {
176
219
  return "\"".concat(String(item), "\"");
177
220
  }
178
221
  }
179
- function needsOwnBodyPart(value) {
180
- if (Array.isArray(value)) return true;
181
- if (isBytes(value)) return true;
182
- if (isPojo(value)) {
183
- // Check if object has complex fields
184
- return Object.values(value).some(function (v) {
185
- return Array.isArray(v) || isPojo(v) || isBytes(v) || v === null || v === undefined || _typeof(v) === "symbol";
186
- });
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);
187
231
  }
188
- return false;
189
232
  }
190
233
  function collectBodyKeys(obj) {
191
234
  var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
235
+ console.log("=== collectBodyKeys START ===");
236
+ console.log("Input object:", JSON.stringify(obj));
192
237
  var keys = [];
193
238
  function traverse(current, path) {
194
- // Track if current level has simple fields or empty objects
239
+ console.log("[traverse] Called with path: \"".concat(path, "\""));
195
240
  var hasSimpleFields = false;
196
- // Track nested paths that need body parts
197
241
  var nestedPaths = [];
242
+ var hasArraysWithObjects = false;
198
243
  var _loop = function _loop() {
199
244
  var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
200
245
  key = _Object$entries$_i[0],
201
246
  value = _Object$entries$_i[1];
202
247
  var fullPath = path ? "".concat(path, "/").concat(key) : key;
203
248
  if (Array.isArray(value)) {
249
+ console.log("[traverse] Found array at ".concat(fullPath, ", length: ").concat(value.length));
204
250
  var hasObjects = value.some(function (item) {
205
251
  return isPojo(item);
206
252
  });
207
253
  var hasNonObjects = value.some(function (item) {
208
254
  return !isPojo(item);
209
255
  });
210
- if (hasObjects) {
211
- // Each object in array gets its own key
212
- value.forEach(function (item, index) {
213
- if (isPojo(item)) {
214
- nestedPaths.push("".concat(fullPath, "/").concat(index + 1));
215
- }
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;
216
270
  });
217
271
 
218
- // If array ALSO has non-object items, it needs its own body part
219
- if (hasNonObjects) {
220
- hasSimpleFields = true;
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
+ });
279
+
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);
302
+ }
303
+ }
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
+ }
314
+ }
315
+ });
316
+ if (hasNonObjects) {
317
+ hasSimpleFields = true;
318
+ keys.push(fullPath);
319
+ }
221
320
  }
222
321
  } else {
223
- // Simple array - parent needs body part
322
+ console.log("[traverse] Non-empty array without objects at ".concat(fullPath, " - marking as simple field"));
224
323
  hasSimpleFields = true;
225
324
  }
226
325
  } else if (isPojo(value)) {
227
- // Check if this is an empty object
228
326
  if (Object.keys(value).length === 0) {
229
- // Empty objects need a body part
327
+ console.log("[traverse] Empty object at ".concat(fullPath, " - marking parent as having simple fields"));
230
328
  hasSimpleFields = true;
231
329
  } else {
232
- // Non-empty objects are processed recursively
233
- nestedPaths.push(fullPath);
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
+ }
234
358
  }
235
- } else if (isBytes(value)) {
359
+ } else if (isBytes(value) && value.length > 0) {
236
360
  hasSimpleFields = true;
237
361
  } else if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null || value === undefined || _typeof(value) === "symbol") {
238
362
  hasSimpleFields = true;
@@ -241,15 +365,33 @@ function collectBodyKeys(obj) {
241
365
  for (var _i = 0, _Object$entries = Object.entries(current); _i < _Object$entries.length; _i++) {
242
366
  _loop();
243
367
  }
244
-
245
- // Add current path if it has simple fields or empty objects
246
368
  if (hasSimpleFields) {
369
+ console.log("[traverse] Adding \"".concat(path, "\" to keys (has simple fields)"));
370
+ keys.push(path);
371
+ } else if (hasArraysWithObjects && path) {
372
+ // If the object only contains arrays with objects, we still need to add it as a body key
373
+ console.log("[traverse] Adding \"".concat(path, "\" to keys (contains arrays with objects)"));
247
374
  keys.push(path);
248
375
  }
249
376
 
250
- // Process nested paths
251
- for (var _i2 = 0, _nestedPaths = nestedPaths; _i2 < _nestedPaths.length; _i2++) {
252
- var nestedPath = _nestedPaths[_i2];
377
+ // Check for arrays with only empty elements that need their own body parts
378
+ for (var _i2 = 0, _Object$entries2 = Object.entries(current); _i2 < _Object$entries2.length; _i2++) {
379
+ var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
380
+ key = _Object$entries2$_i[0],
381
+ value = _Object$entries2$_i[1];
382
+ var fullPath = path ? "".concat(path, "/").concat(key) : key;
383
+ if (Array.isArray(value) && value.length > 0) {
384
+ var hasOnlyEmptyElements = value.every(function (item) {
385
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
386
+ });
387
+ if (hasOnlyEmptyElements) {
388
+ console.log("[traverse] Array at ".concat(fullPath, " has only empty elements - adding as body key"));
389
+ keys.push(fullPath);
390
+ }
391
+ }
392
+ }
393
+ for (var _i3 = 0, _nestedPaths = nestedPaths; _i3 < _nestedPaths.length; _i3++) {
394
+ var nestedPath = _nestedPaths[_i3];
253
395
  var parts = nestedPath.split("/");
254
396
  var nestedObj = obj;
255
397
  var _iterator = _createForOfIteratorHelper(parts),
@@ -273,13 +415,29 @@ function collectBodyKeys(obj) {
273
415
  }
274
416
  }
275
417
  }
276
-
277
- // Handle top-level fields
418
+ var objKeys = Object.keys(obj);
278
419
  var _loop2 = function _loop2() {
279
- var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i3], 2),
280
- key = _Object$entries2$_i[0],
281
- value = _Object$entries2$_i[1];
282
- if (Array.isArray(value)) {
420
+ var _Object$entries3$_i = _slicedToArray(_Object$entries3[_i4], 2),
421
+ key = _Object$entries3$_i[0],
422
+ value = _Object$entries3$_i[1];
423
+ console.log("\n[main loop] Processing key: \"".concat(key, "\""));
424
+ console.log("[main loop] Value type: ".concat(Array.isArray(value) ? "array" : _typeof(value)));
425
+ console.log("[main loop] Array length: ".concat(Array.isArray(value) ? value.length : "N/A"));
426
+ if ((key === "data" || key === "body") && (typeof value === "string" || typeof value === "boolean" || typeof value === "number" || value === null || value === undefined || _typeof(value) === "symbol") && objKeys.length > 1) {
427
+ // Special handling: only add to body keys if there's no other data/body field with an object
428
+ if (key === "data" && obj.body && isPojo(obj.body) && Object.keys(obj.body).length > 0) {
429
+ console.log("[main loop] Skipping special data field");
430
+ } else if (key === "body" && obj.data && isPojo(obj.data) && Object.keys(obj.data).length > 0) {
431
+ console.log("[main loop] Skipping special body field");
432
+ } else {
433
+ console.log("[main loop] Adding special data/body key: \"".concat(key, "\""));
434
+ keys.push(key);
435
+ }
436
+ } else if (Array.isArray(value)) {
437
+ if (value.length === 0) {
438
+ console.log("[main loop] SKIPPING empty array for key: \"".concat(key, "\""));
439
+ return 1; // continue
440
+ }
283
441
  var hasObjects = value.some(function (item) {
284
442
  return isPojo(item);
285
443
  });
@@ -289,61 +447,223 @@ function collectBodyKeys(obj) {
289
447
  var hasNonObjects = value.some(function (item) {
290
448
  return !isPojo(item);
291
449
  });
292
- if (hasObjects) {
293
- value.forEach(function (item, index) {
294
- if (isPojo(item)) {
295
- keys.push("".concat(key, "/").concat(index + 1));
296
- // Also need to traverse into nested objects within array items
297
- for (var _i4 = 0, _Object$entries3 = Object.entries(item); _i4 < _Object$entries3.length; _i4++) {
298
- var _Object$entries3$_i = _slicedToArray(_Object$entries3[_i4], 2),
299
- nestedKey = _Object$entries3$_i[0],
300
- nestedValue = _Object$entries3$_i[1];
301
- if (isPojo(nestedValue)) {
302
- keys.push("".concat(key, "/").concat(index + 1, "/").concat(nestedKey));
303
- }
304
- }
305
- }
450
+
451
+ // Check if this is an array of arrays containing objects
452
+ var hasArraysOfObjects = value.some(function (item) {
453
+ return Array.isArray(item) && item.some(function (subItem) {
454
+ return isPojo(subItem);
455
+ });
456
+ });
457
+ console.log("[main loop] Array analysis: hasObjects=".concat(hasObjects, ", hasArrays=").concat(hasArrays, ", hasNonObjects=").concat(hasNonObjects, ", hasArraysOfObjects=").concat(hasArraysOfObjects));
458
+ if (value.length > 0) {
459
+ var bodyPartCounter = 1; // Start counting from 1
460
+
461
+ // Check for special mixed array case
462
+ var hasEmptyStrings = value.some(function (item) {
463
+ return typeof item === "string" && item === "";
464
+ });
465
+ var hasEmptyObjects = value.some(function (item) {
466
+ return isPojo(item) && Object.keys(item).length === 0;
306
467
  });
307
468
 
308
- // Mixed arrays also need their own body part
309
- if (hasNonObjects) {
469
+ // Check for objects that contain only empty values
470
+ var hasObjectsWithOnlyEmptyValues = value.some(function (item) {
471
+ if (!isPojo(item) || Object.keys(item).length === 0) return false;
472
+ return Object.values(item).every(function (v) {
473
+ return typeof v === "string" && v === "" || Array.isArray(v) && v.length === 0 || isPojo(v) && Object.keys(v).length === 0;
474
+ });
475
+ });
476
+ if (hasArraysOfObjects) {
477
+ // Handle arrays of arrays containing objects
478
+ value.forEach(function (item, index) {
479
+ if (Array.isArray(item)) {
480
+ item.forEach(function (subItem, subIndex) {
481
+ if (isPojo(subItem)) {
482
+ var path = "".concat(key, "/").concat(index + 1, "/").concat(subIndex + 1);
483
+ console.log("[main loop] Adding nested object path: \"".concat(path, "\""));
484
+ keys.push(path);
485
+ }
486
+ });
487
+ }
488
+ bodyPartCounter++;
489
+ });
490
+ // Always add the main array key
491
+ console.log("[main loop] ADDING main array key: \"".concat(key, "\""));
492
+ keys.push(key);
493
+ } else if (hasObjects && (hasEmptyStrings || hasEmptyObjects) && !hasObjectsWithOnlyEmptyValues) {
494
+ // Special handling: only non-empty objects get parts
495
+ value.forEach(function (item, index) {
496
+ if (isPojo(item) && Object.keys(item).length > 0) {
497
+ var path = "".concat(key, "/").concat(bodyPartCounter);
498
+ console.log("[main loop] Adding non-empty object path: \"".concat(path, "\" (array index ").concat(index, ")"));
499
+ keys.push(path);
500
+ // Add paths for nested objects
501
+ for (var _i5 = 0, _Object$entries4 = Object.entries(item); _i5 < _Object$entries4.length; _i5++) {
502
+ var _Object$entries4$_i = _slicedToArray(_Object$entries4[_i5], 2),
503
+ nestedKey = _Object$entries4$_i[0],
504
+ nestedValue = _Object$entries4$_i[1];
505
+ if (isPojo(nestedValue)) {
506
+ var nestedPath = "".concat(key, "/").concat(bodyPartCounter, "/").concat(nestedKey);
507
+ console.log("[main loop] Adding nested object path: \"".concat(nestedPath, "\""));
508
+ keys.push(nestedPath);
509
+ }
510
+ }
511
+ }
512
+ bodyPartCounter++;
513
+ });
514
+ // Always add the main array key
515
+ console.log("[main loop] ADDING main array key: \"".concat(key, "\""));
310
516
  keys.push(key);
517
+ } else if (hasObjects) {
518
+ // Normal handling: all objects get parts (except if parent array has only empty elements)
519
+ var skipEmptyObjects = false;
520
+
521
+ // Check if this array contains only empty elements
522
+ var arrayHasOnlyEmptyElements = value.every(function (item) {
523
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
524
+ });
525
+ if (arrayHasOnlyEmptyElements) {
526
+ skipEmptyObjects = true;
527
+ }
528
+ value.forEach(function (item, index) {
529
+ if (isPojo(item)) {
530
+ // Skip empty objects if array has only empty elements
531
+ if (skipEmptyObjects && Object.keys(item).length === 0) {
532
+ bodyPartCounter++;
533
+ return;
534
+ }
535
+ var path = "".concat(key, "/").concat(bodyPartCounter);
536
+ console.log("[main loop] Adding object path: \"".concat(path, "\" (array index ").concat(index, ", empty=").concat(Object.keys(item).length === 0, ")"));
537
+ keys.push(path);
538
+ // Add paths for nested objects (but not empty ones)
539
+ if (Object.keys(item).length > 0) {
540
+ for (var _i6 = 0, _Object$entries5 = Object.entries(item); _i6 < _Object$entries5.length; _i6++) {
541
+ var _Object$entries5$_i = _slicedToArray(_Object$entries5[_i6], 2),
542
+ nestedKey = _Object$entries5$_i[0],
543
+ nestedValue = _Object$entries5$_i[1];
544
+ if (isPojo(nestedValue) && Object.keys(nestedValue).length > 0) {
545
+ var nestedPath = "".concat(key, "/").concat(bodyPartCounter, "/").concat(nestedKey);
546
+ console.log("[main loop] Adding nested object path: \"".concat(nestedPath, "\""));
547
+ keys.push(nestedPath);
548
+ }
549
+ }
550
+ }
551
+ } else if (typeof item === "string" && item === "") {
552
+ // Empty strings may get parts in some formats
553
+ var _path = "".concat(key, "/").concat(bodyPartCounter);
554
+ console.log("[main loop] Adding empty string path: \"".concat(_path, "\" (array index ").concat(index, ")"));
555
+ keys.push(_path);
556
+ }
557
+ bodyPartCounter++;
558
+ });
559
+ // Don't add main array key for arrays with only objects containing empty values
560
+ if (!hasObjectsWithOnlyEmptyValues || value.some(function (item) {
561
+ return !isPojo(item);
562
+ })) {
563
+ // Check if array has only empty elements
564
+ var hasOnlyEmptyElements = value.every(function (item) {
565
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
566
+ });
567
+ if (!hasOnlyEmptyElements) {
568
+ // Always add the main array key
569
+ console.log("[main loop] ADDING main array key: \"".concat(key, "\""));
570
+ keys.push(key);
571
+ }
572
+ }
573
+ } else {
574
+ // Check if array has only empty elements
575
+ var hasOnlyEmptyArraysOrObjects = value.every(function (item) {
576
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
577
+ });
578
+ if (hasOnlyEmptyArraysOrObjects && value.length > 0) {
579
+ // Always add the main array key for arrays with only empty elements
580
+ console.log("[main loop] ADDING main array key for empty elements: \"".concat(key, "\""));
581
+ keys.push(key);
582
+ } else if (!hasOnlyEmptyArraysOrObjects) {
583
+ // Always add the main array key
584
+ console.log("[main loop] ADDING main array key: \"".concat(key, "\""));
585
+ keys.push(key);
586
+ }
311
587
  }
312
- } else if (hasArrays) {
313
- // Array containing arrays needs body part
314
- keys.push(key);
315
- } else {
316
- // Simple array at top level - DO NOT add to body keys
317
- // It will go in headers instead
318
588
  }
319
589
  } else if (isPojo(value)) {
320
- // Top-level object that may have nested structures
590
+ console.log("[main loop] Traversing object at key: \"".concat(key, "\""));
591
+ // Check if this object contains arrays with only empty elements
592
+ var hasArraysWithOnlyEmptyElements = false;
593
+ for (var _i7 = 0, _Object$entries6 = Object.entries(value); _i7 < _Object$entries6.length; _i7++) {
594
+ var _Object$entries6$_i = _slicedToArray(_Object$entries6[_i7], 2),
595
+ k = _Object$entries6$_i[0],
596
+ v = _Object$entries6$_i[1];
597
+ if (Array.isArray(v) && v.length > 0 && v.every(function (item) {
598
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
599
+ })) {
600
+ hasArraysWithOnlyEmptyElements = true;
601
+ keys.push("".concat(key, "/").concat(k));
602
+ }
603
+ }
321
604
  traverse(value, key);
322
- } else if (isBytes(value)) {
323
- // All binary data needs body parts, even empty ones
605
+ } else if (isBytes(value) && value.length > 0) {
606
+ console.log("[main loop] Adding key for non-empty bytes: \"".concat(key, "\""));
324
607
  keys.push(key);
325
608
  } else if (typeof value === "string" && value.includes("\n")) {
326
- // Multiline string
609
+ console.log("[main loop] Adding key for string with newline: \"".concat(key, "\""));
327
610
  keys.push(key);
611
+ } else if (typeof value === "string" && hasNonAscii(value)) {
612
+ console.log("[main loop] Adding key for non-ASCII string: \"".concat(key, "\""));
613
+ keys.push(key);
614
+ } else {
615
+ console.log("[main loop] Skipping key: \"".concat(key, "\" (no match)"));
328
616
  }
329
617
  };
330
- for (var _i3 = 0, _Object$entries2 = Object.entries(obj); _i3 < _Object$entries2.length; _i3++) {
331
- _loop2();
618
+ for (var _i4 = 0, _Object$entries3 = Object.entries(obj); _i4 < _Object$entries3.length; _i4++) {
619
+ if (_loop2()) continue;
332
620
  }
333
- return _toConsumableArray(new Set(keys)).filter(function (k) {
334
- return k !== "";
621
+ var result = _toConsumableArray(new Set(keys)).filter(function (k) {
622
+ if (k === "") return false;
623
+
624
+ // Check if this is a path to an empty object inside an array with only empty elements
625
+ var parts = k.split("/");
626
+ if (parts.length >= 2 && /^\d+$/.test(parts[parts.length - 1])) {
627
+ // This is an array element path like "maps/1"
628
+ var arrayPath = parts.slice(0, -1).join("/");
629
+ var arrayValue = obj;
630
+
631
+ // Navigate to the array
632
+ var _iterator2 = _createForOfIteratorHelper(parts.slice(0, -1)),
633
+ _step2;
634
+ try {
635
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
636
+ var part = _step2.value;
637
+ if (/^\d+$/.test(part)) {
638
+ arrayValue = arrayValue[parseInt(part) - 1];
639
+ } else {
640
+ arrayValue = arrayValue[part];
641
+ }
642
+ }
643
+
644
+ // Check if this array contains only empty elements
645
+ } catch (err) {
646
+ _iterator2.e(err);
647
+ } finally {
648
+ _iterator2.f();
649
+ }
650
+ if (Array.isArray(arrayValue)) {
651
+ var hasOnlyEmptyElements = arrayValue.every(function (item) {
652
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
653
+ });
654
+ if (hasOnlyEmptyElements) {
655
+ // Filter out paths to individual empty elements
656
+ console.log("[filter] Removing path to empty element: \"".concat(k, "\""));
657
+ return false;
658
+ }
659
+ }
660
+ }
661
+ return true;
335
662
  });
336
- }
337
- function toBuffer(value) {
338
- if (Buffer.isBuffer(value)) {
339
- return value;
340
- } else if (value && _typeof(value) === "object" && value.type === "Buffer" && Array.isArray(value.data)) {
341
- return Buffer.from(value.data);
342
- } else if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
343
- return Buffer.from(value);
344
- } else {
345
- return Buffer.from(value);
346
- }
663
+ console.log("\n=== collectBodyKeys RESULT ===");
664
+ console.log("Final bodyKeys:", JSON.stringify(result));
665
+ console.log("=== collectBodyKeys END ===\n");
666
+ return result;
347
667
  }
348
668
  function encode() {
349
669
  return _encode.apply(this, arguments);
@@ -353,50 +673,66 @@ function _encode() {
353
673
  var obj,
354
674
  _processValue,
355
675
  processedObj,
356
- _i6,
357
- _Object$entries5,
358
- _Object$entries5$_i,
676
+ _i9,
677
+ _Object$entries8,
678
+ _Object$entries8$_i,
359
679
  k,
360
680
  v,
681
+ objKeys,
682
+ fieldName,
683
+ fieldValue,
684
+ _headers,
361
685
  hasBodyBinary,
362
686
  otherFields,
363
- allOthersSimpleOrEmptyBinary,
364
- _headers,
365
- _headerTypes,
366
- _i7,
367
- _Object$entries6,
368
- _Object$entries6$_i,
369
- key,
370
- value,
371
- encodedItems,
687
+ _headers2,
372
688
  bodyBuffer,
373
689
  bodyArrayBuffer,
374
- _contentDigest,
375
- _base,
376
- objKeys,
377
- fieldName,
378
- binaryData,
379
- _headers2,
690
+ contentDigest,
691
+ base64,
692
+ _fieldName,
693
+ _fieldValue,
694
+ _headers3,
380
695
  _bodyBuffer,
381
696
  _bodyArrayBuffer,
697
+ _contentDigest,
698
+ _base,
699
+ _headers4,
700
+ bodyContent,
701
+ encoder,
702
+ encoded,
382
703
  _contentDigest2,
383
704
  _base2,
705
+ _headers5,
706
+ _encoder,
707
+ _encoded,
708
+ _contentDigest3,
709
+ _base3,
384
710
  headers,
385
711
  headerTypes,
386
712
  bodyKeys,
387
713
  _loop3,
388
- _i8,
389
- _Object$entries7,
714
+ _i10,
715
+ _Object$entries9,
390
716
  _loop4,
391
- _i9,
392
- _Object$entries8,
717
+ _i11,
718
+ _Object$entries10,
393
719
  allBodyKeysAreEmptyBinaries,
720
+ singleKey,
721
+ pathParts,
722
+ value,
723
+ _iterator4,
724
+ _step4,
725
+ part,
726
+ otherFieldsAreEmpty,
727
+ _bodyBuffer2,
728
+ _bodyArrayBuffer2,
729
+ _contentDigest4,
730
+ _base4,
394
731
  sortedBodyKeys,
395
732
  hasSpecialDataBody,
396
- inlineKey,
397
733
  bodyParts,
398
- _iterator3,
399
- _step3,
734
+ _iterator5,
735
+ _step5,
400
736
  _loop5,
401
737
  _ret,
402
738
  buffer,
@@ -409,19 +745,15 @@ function _encode() {
409
745
  i,
410
746
  body,
411
747
  finalContent,
412
- contentDigest,
413
- base64,
414
- bodyText,
415
- boundaryMatch,
416
- debugBoundary,
417
- parts,
418
- lines,
748
+ _contentDigest5,
749
+ _base5,
419
750
  _args6 = arguments;
420
751
  return _regeneratorRuntime().wrap(function _callee3$(_context6) {
421
752
  while (1) switch (_context6.prev = _context6.next) {
422
753
  case 0:
423
754
  obj = _args6.length > 0 && _args6[0] !== undefined ? _args6[0] : {};
424
- // Convert symbols to strings for logging
755
+ console.log("\n=== ENCODE START ===");
756
+ console.log("Encoding object:", JSON.stringify(obj));
425
757
  _processValue = function processValue(value) {
426
758
  if (_typeof(value) === "symbol") {
427
759
  return value.description || "Symbol.for()";
@@ -429,10 +761,10 @@ function _encode() {
429
761
  return value.map(_processValue);
430
762
  } else if (isPojo(value)) {
431
763
  var result = {};
432
- for (var _i5 = 0, _Object$entries4 = Object.entries(value); _i5 < _Object$entries4.length; _i5++) {
433
- var _Object$entries4$_i = _slicedToArray(_Object$entries4[_i5], 2),
434
- k = _Object$entries4$_i[0],
435
- v = _Object$entries4$_i[1];
764
+ for (var _i8 = 0, _Object$entries7 = Object.entries(value); _i8 < _Object$entries7.length; _i8++) {
765
+ var _Object$entries7$_i = _slicedToArray(_Object$entries7[_i8], 2),
766
+ k = _Object$entries7$_i[0],
767
+ v = _Object$entries7$_i[1];
436
768
  result[k] = _processValue(v);
437
769
  }
438
770
  return result;
@@ -440,243 +772,306 @@ function _encode() {
440
772
  return value;
441
773
  };
442
774
  processedObj = {};
443
- for (_i6 = 0, _Object$entries5 = Object.entries(obj); _i6 < _Object$entries5.length; _i6++) {
444
- _Object$entries5$_i = _slicedToArray(_Object$entries5[_i6], 2), k = _Object$entries5$_i[0], v = _Object$entries5$_i[1];
775
+ for (_i9 = 0, _Object$entries8 = Object.entries(obj); _i9 < _Object$entries8.length; _i9++) {
776
+ _Object$entries8$_i = _slicedToArray(_Object$entries8[_i9], 2), k = _Object$entries8$_i[0], v = _Object$entries8$_i[1];
445
777
  processedObj[k] = _processValue(v);
446
778
  }
447
-
448
- // Remove debug logging for cleaner output
449
- console.log("[encode] START with obj:", JSON.stringify(processedObj));
450
779
  if (!(Object.keys(obj).length === 0)) {
451
- _context6.next = 7;
780
+ _context6.next = 8;
452
781
  break;
453
782
  }
454
783
  return _context6.abrupt("return", {
455
784
  headers: {},
456
785
  body: undefined
457
786
  });
458
- case 7:
459
- // Check for special case: body field with binary + other simple fields or empty binaries
460
- hasBodyBinary = obj.body && isBytes(obj.body);
461
- otherFields = Object.keys(obj).filter(function (k) {
462
- return k !== "body";
463
- });
464
- allOthersSimpleOrEmptyBinary = otherFields.every(function (k) {
465
- var v = obj[k];
466
- // Allow empty binaries as "simple"
467
- if (isBytes(v) && (v.length === 0 || v.byteLength === 0)) return true;
468
- return !isBytes(v) && !isPojo(v) && !(Array.isArray(v) && v.some(function (item) {
469
- return isPojo(item) || isBytes(item);
470
- }));
471
- });
472
- if (!(hasBodyBinary && allOthersSimpleOrEmptyBinary)) {
473
- _context6.next = 34;
787
+ case 8:
788
+ objKeys = Object.keys(obj);
789
+ if (!(objKeys.length === 1)) {
790
+ _context6.next = 16;
474
791
  break;
475
792
  }
476
- console.log("[encode] Special case: body with binary + simple fields");
477
- // Special case: body with binary + other simple fields
478
- _headers = {};
479
- _headerTypes = []; // Process other fields into headers
480
- _i7 = 0, _Object$entries6 = Object.entries(obj);
481
- case 15:
482
- if (!(_i7 < _Object$entries6.length)) {
483
- _context6.next = 24;
793
+ fieldName = objKeys[0];
794
+ fieldValue = obj[fieldName];
795
+ if (!(isBytes(fieldValue) && (fieldValue.length === 0 || fieldValue.byteLength === 0))) {
796
+ _context6.next = 16;
484
797
  break;
485
798
  }
486
- _Object$entries6$_i = _slicedToArray(_Object$entries6[_i7], 2), key = _Object$entries6$_i[0], value = _Object$entries6$_i[1];
487
- if (!(key === "body")) {
488
- _context6.next = 19;
799
+ _headers = {};
800
+ _headers["ao-types"] = "".concat(fieldName.toLowerCase(), "=\"empty-binary\"");
801
+ return _context6.abrupt("return", {
802
+ headers: _headers,
803
+ body: undefined
804
+ });
805
+ case 16:
806
+ if (obj.body && isBytes(obj.body) && (obj.body.length === 0 || obj.body.byteLength === 0) && objKeys.length > 1) {}
807
+ hasBodyBinary = obj.body && isBytes(obj.body);
808
+ otherFields = Object.keys(obj).filter(function (k) {
809
+ return k !== "body";
810
+ });
811
+ if (!(hasBodyBinary && otherFields.length === 0)) {
812
+ _context6.next = 29;
489
813
  break;
490
814
  }
491
- return _context6.abrupt("continue", 21);
492
- case 19:
493
- console.log("[encode] Processing special case field: ".concat(key, " = ").concat(JSON.stringify(value)));
494
- if (value === null) {
495
- _headers[key] = '"null"';
496
- _headerTypes.push("".concat(key, "=\"atom\""));
497
- } else if (value === undefined) {
498
- _headers[key] = '"undefined"';
499
- _headerTypes.push("".concat(key, "=\"atom\""));
500
- } else if (typeof value === "boolean") {
501
- _headers[key] = "\"".concat(value, "\"");
502
- _headerTypes.push("".concat(key, "=\"atom\""));
503
- } else if (_typeof(value) === "symbol") {
504
- _headers[key] = "\"".concat(value.description || "Symbol.for()", "\"");
505
- _headerTypes.push("".concat(key, "=\"atom\""));
506
- } else if (typeof value === "number") {
507
- _headers[key] = String(value);
508
- _headerTypes.push("".concat(key, "=\"").concat(Number.isInteger(value) ? "integer" : "float", "\""));
509
- } else if (typeof value === "string") {
510
- if (value.length === 0) {
511
- // Empty strings only go in ao-types, not as headers
512
- console.log("[encode] Adding empty string type for key: ".concat(key));
513
- _headerTypes.push("".concat(key, "=\"empty-binary\""));
514
- } else {
515
- _headers[key] = value;
516
- }
517
- } else if (Array.isArray(value) && value.length === 0) {
518
- // Empty array only goes in ao-types, not as a header
519
- _headerTypes.push("".concat(key, "=\"empty-list\""));
520
- } else if (Array.isArray(value) && !value.some(function (item) {
521
- return isPojo(item);
522
- })) {
523
- encodedItems = value.map(function (item) {
524
- return encodeArrayItem(item);
525
- }).join(", ");
526
- _headers[key] = encodedItems;
527
- _headerTypes.push("".concat(key, "=\"list\""));
528
- } else if (isBytes(value) && (value.length === 0 || value.byteLength === 0)) {
529
- // Empty binary goes in ao-types only
530
- _headerTypes.push("".concat(key, "=\"empty-binary\""));
531
- }
532
- case 21:
533
- _i7++;
534
- _context6.next = 15;
535
- break;
536
- case 24:
537
- // Add ao-types if needed
538
- if (_headerTypes.length > 0) {
539
- _headers["ao-types"] = _headerTypes.sort().join(", ");
540
- }
541
-
542
- // Set body to binary
815
+ _headers2 = {};
543
816
  bodyBuffer = toBuffer(obj.body);
544
817
  bodyArrayBuffer = bodyBuffer.buffer.slice(bodyBuffer.byteOffset, bodyBuffer.byteOffset + bodyBuffer.byteLength);
545
- _context6.next = 29;
818
+ _context6.next = 25;
546
819
  return sha256(bodyArrayBuffer);
547
- case 29:
548
- _contentDigest = _context6.sent;
549
- _base = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest));
550
- _headers["content-digest"] = "sha-256=:".concat(_base, ":");
551
- console.log("[encode] FINAL (body with binary) - headers:", _headers, "body:", obj.body);
820
+ case 25:
821
+ contentDigest = _context6.sent;
822
+ base64 = _base64url["default"].toBase64(_base64url["default"].encode(contentDigest));
823
+ _headers2["content-digest"] = "sha-256=:".concat(base64, ":");
552
824
  return _context6.abrupt("return", {
553
- headers: _headers,
825
+ headers: _headers2,
554
826
  body: obj.body
555
827
  });
556
- case 34:
557
- // Check if single binary field
558
- objKeys = Object.keys(obj);
559
- if (!(objKeys.length === 1 && isBytes(obj[objKeys[0]]))) {
560
- _context6.next = 49;
828
+ case 29:
829
+ if (!(objKeys.length === 1)) {
830
+ _context6.next = 71;
561
831
  break;
562
832
  }
563
- fieldName = objKeys[0];
564
- binaryData = obj[fieldName];
565
- _headers2 = {};
566
- _bodyBuffer = toBuffer(binaryData);
833
+ _fieldName = objKeys[0];
834
+ _fieldValue = obj[_fieldName];
835
+ if (!(isBytes(_fieldValue) && _fieldValue.length > 0)) {
836
+ _context6.next = 45;
837
+ break;
838
+ }
839
+ _headers3 = {};
840
+ _bodyBuffer = toBuffer(_fieldValue);
567
841
  _bodyArrayBuffer = _bodyBuffer.buffer.slice(_bodyBuffer.byteOffset, _bodyBuffer.byteOffset + _bodyBuffer.byteLength);
568
- _context6.next = 43;
842
+ _context6.next = 38;
569
843
  return sha256(_bodyArrayBuffer);
570
- case 43:
844
+ case 38:
845
+ _contentDigest = _context6.sent;
846
+ _base = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest));
847
+ _headers3["content-digest"] = "sha-256=:".concat(_base, ":");
848
+ if (_fieldName !== "body") {
849
+ _headers3["inline-body-key"] = _fieldName;
850
+ }
851
+ return _context6.abrupt("return", {
852
+ headers: _headers3,
853
+ body: _fieldValue
854
+ });
855
+ case 45:
856
+ if (!((_fieldName === "data" || _fieldName === "body") && (typeof _fieldValue === "string" || typeof _fieldValue === "boolean" || typeof _fieldValue === "number" || _fieldValue === null || _fieldValue === undefined || _typeof(_fieldValue) === "symbol"))) {
857
+ _context6.next = 60;
858
+ break;
859
+ }
860
+ _headers4 = {};
861
+ if (typeof _fieldValue === "string") {
862
+ bodyContent = _fieldValue;
863
+ } else if (typeof _fieldValue === "boolean") {
864
+ bodyContent = "\"".concat(_fieldValue, "\"");
865
+ } else if (typeof _fieldValue === "number") {
866
+ bodyContent = String(_fieldValue);
867
+ } else if (_fieldValue === null) {
868
+ bodyContent = '"null"';
869
+ } else if (_fieldValue === undefined) {
870
+ bodyContent = '"undefined"';
871
+ } else if (_typeof(_fieldValue) === "symbol") {
872
+ bodyContent = "\"".concat(_fieldValue.description || "Symbol.for()", "\"");
873
+ }
874
+ encoder = new TextEncoder();
875
+ encoded = encoder.encode(bodyContent);
876
+ _context6.next = 52;
877
+ return sha256(encoded.buffer);
878
+ case 52:
571
879
  _contentDigest2 = _context6.sent;
572
880
  _base2 = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest2));
573
- _headers2["content-digest"] = "sha-256=:".concat(_base2, ":");
574
-
575
- // Add inline-body-key header to preserve the field name
576
- if (fieldName !== "body") {
577
- _headers2["inline-body-key"] = fieldName;
881
+ _headers4["content-digest"] = "sha-256=:".concat(_base2, ":");
882
+ if (typeof _fieldValue === "boolean" || _fieldValue === null || _fieldValue === undefined || _typeof(_fieldValue) === "symbol") {
883
+ _headers4["ao-types"] = "".concat(_fieldName.toLowerCase(), "=\"atom\"");
884
+ } else if (typeof _fieldValue === "number") {
885
+ _headers4["ao-types"] = "".concat(_fieldName.toLowerCase(), "=\"").concat(Number.isInteger(_fieldValue) ? "integer" : "float", "\"");
886
+ }
887
+ if (_fieldName !== "body") {
888
+ _headers4["inline-body-key"] = _fieldName;
578
889
  }
579
- console.log("[encode] FINAL (simple binary field) - headers:", _headers2, "body:", binaryData);
580
890
  return _context6.abrupt("return", {
581
- headers: _headers2,
582
- body: binaryData
891
+ headers: _headers4,
892
+ body: bodyContent
893
+ });
894
+ case 60:
895
+ if (!(typeof _fieldValue === "string" && hasNonAscii(_fieldValue))) {
896
+ _context6.next = 71;
897
+ break;
898
+ }
899
+ _headers5 = {};
900
+ _encoder = new TextEncoder();
901
+ _encoded = _encoder.encode(_fieldValue);
902
+ _context6.next = 66;
903
+ return sha256(_encoded.buffer);
904
+ case 66:
905
+ _contentDigest3 = _context6.sent;
906
+ _base3 = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest3));
907
+ _headers5["content-digest"] = "sha-256=:".concat(_base3, ":");
908
+ if (_fieldName !== "body") {
909
+ _headers5["inline-body-key"] = _fieldName;
910
+ }
911
+ return _context6.abrupt("return", {
912
+ headers: _headers5,
913
+ body: _fieldValue
583
914
  });
584
- case 49:
585
- // Continue with normal multipart processing
915
+ case 71:
586
916
  headers = {};
587
- headerTypes = []; // Collect all body keys
588
- bodyKeys = collectBodyKeys(obj); // Process simple header fields AND collect types for body fields
917
+ headerTypes = [];
918
+ bodyKeys = collectBodyKeys(obj);
589
919
  _loop3 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop3() {
590
- var _Object$entries7$_i, key, value, needsBody, _encodedItems;
920
+ var _Object$entries9$_i, key, value, needsBody, hasNonAsciiItems, encodedItems;
591
921
  return _regeneratorRuntime().wrap(function _loop3$(_context3) {
592
922
  while (1) switch (_context3.prev = _context3.next) {
593
923
  case 0:
594
- _Object$entries7$_i = _slicedToArray(_Object$entries7[_i8], 2), key = _Object$entries7$_i[0], value = _Object$entries7$_i[1];
595
- console.log("[encode] Processing field: ".concat(key, " = ").concat(JSON.stringify(value), ", type: ").concat(_typeof(value)));
924
+ _Object$entries9$_i = _slicedToArray(_Object$entries9[_i10], 2), key = _Object$entries9$_i[0], value = _Object$entries9$_i[1];
596
925
  needsBody = bodyKeys.includes(key) || bodyKeys.some(function (k) {
597
926
  return k.startsWith("".concat(key, "/"));
598
927
  });
599
- if (!needsBody) {
600
- console.log("[encode] Field ".concat(key, " doesn't need body, adding to headers"));
601
- // Simple value goes in header
602
- if (value === null) {
603
- headers[key] = '"null"';
604
- headerTypes.push("".concat(key, "=\"atom\""));
605
- } else if (value === undefined) {
606
- headers[key] = '"undefined"';
607
- headerTypes.push("".concat(key, "=\"atom\""));
608
- } else if (typeof value === "boolean") {
609
- headers[key] = "\"".concat(value, "\"");
610
- headerTypes.push("".concat(key, "=\"atom\""));
611
- } else if (_typeof(value) === "symbol") {
612
- headers[key] = "\"".concat(value.description || "Symbol.for()", "\"");
613
- headerTypes.push("".concat(key, "=\"atom\""));
614
- } else if (typeof value === "number") {
615
- headers[key] = String(value);
616
- headerTypes.push("".concat(key, "=\"").concat(Number.isInteger(value) ? "integer" : "float", "\""));
617
- } else if (typeof value === "string") {
618
- if (value.length === 0) {
619
- headerTypes.push("".concat(key, "=\"empty-binary\""));
620
- // Don't add empty strings as headers
621
- } else {
622
- headers[key] = value;
623
- }
624
- } else if (Array.isArray(value) && !value.some(function (item) {
625
- return isPojo(item);
626
- })) {
627
- // Simple array (no objects) goes in header
628
- _encodedItems = value.map(function (item) {
928
+ if (needsBody) {
929
+ _context3.next = 43;
930
+ break;
931
+ }
932
+ if (!(value === null)) {
933
+ _context3.next = 8;
934
+ break;
935
+ }
936
+ headers[key] = '"null"';
937
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
938
+ _context3.next = 41;
939
+ break;
940
+ case 8:
941
+ if (!(value === undefined)) {
942
+ _context3.next = 13;
943
+ break;
944
+ }
945
+ headers[key] = '"undefined"';
946
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
947
+ _context3.next = 41;
948
+ break;
949
+ case 13:
950
+ if (!(typeof value === "boolean")) {
951
+ _context3.next = 18;
952
+ break;
953
+ }
954
+ headers[key] = "\"".concat(value, "\"");
955
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
956
+ _context3.next = 41;
957
+ break;
958
+ case 18:
959
+ if (!(_typeof(value) === "symbol")) {
960
+ _context3.next = 23;
961
+ break;
962
+ }
963
+ headers[key] = "\"".concat(value.description || "Symbol.for()", "\"");
964
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
965
+ _context3.next = 41;
966
+ break;
967
+ case 23:
968
+ if (!(typeof value === "number")) {
969
+ _context3.next = 28;
970
+ break;
971
+ }
972
+ headers[key] = String(value);
973
+ headerTypes.push("".concat(key.toLowerCase(), "=\"").concat(Number.isInteger(value) ? "integer" : "float", "\""));
974
+ _context3.next = 41;
975
+ break;
976
+ case 28:
977
+ if (!(typeof value === "string")) {
978
+ _context3.next = 40;
979
+ break;
980
+ }
981
+ if (!(value.length === 0)) {
982
+ _context3.next = 33;
983
+ break;
984
+ }
985
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-binary\""));
986
+ _context3.next = 38;
987
+ break;
988
+ case 33:
989
+ if (!hasNonAscii(value)) {
990
+ _context3.next = 37;
991
+ break;
992
+ }
993
+ return _context3.abrupt("return", 1);
994
+ case 37:
995
+ headers[key] = value;
996
+ case 38:
997
+ _context3.next = 41;
998
+ break;
999
+ case 40:
1000
+ if (Array.isArray(value) && value.length === 0) {
1001
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-list\""));
1002
+ } else if (Array.isArray(value) && !value.some(function (item) {
1003
+ return isPojo(item);
1004
+ })) {
1005
+ hasNonAsciiItems = value.some(function (item) {
1006
+ return typeof item === "string" && hasNonAscii(item);
1007
+ });
1008
+ if (!hasNonAsciiItems) {
1009
+ encodedItems = value.map(function (item) {
629
1010
  return encodeArrayItem(item);
630
1011
  }).join(", ");
631
- headers[key] = _encodedItems;
632
- headerTypes.push("".concat(key, "=\"list\""));
633
- }
634
- } else {
635
- // Field needs body - still need to add type info to ao-types
636
- if (isBytes(value) && (value.length === 0 || value.byteLength === 0)) {
637
- headerTypes.push("".concat(key, "=\"empty-binary\""));
638
- } else if (typeof value === "string" && value.length === 0) {
639
- headerTypes.push("".concat(key, "=\"empty-binary\""));
640
- } else if (Array.isArray(value) && value.length === 0) {
641
- headerTypes.push("".concat(key, "=\"empty-list\""));
642
- } else if (isPojo(value) && Object.keys(value).length === 0) {
643
- headerTypes.push("".concat(key, "=\"empty-message\""));
1012
+ headers[key] = encodedItems;
1013
+ headerTypes.push("".concat(key.toLowerCase(), "=\"list\""));
644
1014
  }
1015
+ } else if (isBytes(value) && (value.length === 0 || value.byteLength === 0)) {
1016
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-binary\""));
1017
+ } else if (isPojo(value) && Object.keys(value).length === 0) {
1018
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-message\""));
1019
+ }
1020
+ case 41:
1021
+ _context3.next = 44;
1022
+ break;
1023
+ case 43:
1024
+ if (isBytes(value) && (value.length === 0 || value.byteLength === 0)) {
1025
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-binary\""));
1026
+ } else if (typeof value === "string" && value.length === 0) {
1027
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-binary\""));
1028
+ } else if (Array.isArray(value) && value.length === 0) {
1029
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-list\""));
1030
+ } else if (isPojo(value) && Object.keys(value).length === 0) {
1031
+ headerTypes.push("".concat(key.toLowerCase(), "=\"empty-message\""));
1032
+ } else if (typeof value === "boolean" || value === null || value === undefined || _typeof(value) === "symbol") {
1033
+ headerTypes.push("".concat(key.toLowerCase(), "=\"atom\""));
1034
+ } else if (typeof value === "number") {
1035
+ headerTypes.push("".concat(key.toLowerCase(), "=\"").concat(Number.isInteger(value) ? "integer" : "float", "\""));
645
1036
  }
646
- case 4:
1037
+ case 44:
647
1038
  case "end":
648
1039
  return _context3.stop();
649
1040
  }
650
1041
  }, _loop3);
651
1042
  });
652
- _i8 = 0, _Object$entries7 = Object.entries(obj);
653
- case 54:
654
- if (!(_i8 < _Object$entries7.length)) {
655
- _context6.next = 59;
1043
+ _i10 = 0, _Object$entries9 = Object.entries(obj);
1044
+ case 76:
1045
+ if (!(_i10 < _Object$entries9.length)) {
1046
+ _context6.next = 83;
656
1047
  break;
657
1048
  }
658
- return _context6.delegateYield(_loop3(), "t0", 56);
659
- case 56:
660
- _i8++;
661
- _context6.next = 54;
1049
+ return _context6.delegateYield(_loop3(), "t0", 78);
1050
+ case 78:
1051
+ if (!_context6.t0) {
1052
+ _context6.next = 80;
1053
+ break;
1054
+ }
1055
+ return _context6.abrupt("continue", 80);
1056
+ case 80:
1057
+ _i10++;
1058
+ _context6.next = 76;
662
1059
  break;
663
- case 59:
1060
+ case 83:
664
1061
  _loop4 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop4() {
665
- var _Object$entries8$_i, key, value;
1062
+ var _Object$entries10$_i, key, value;
666
1063
  return _regeneratorRuntime().wrap(function _loop4$(_context4) {
667
1064
  while (1) switch (_context4.prev = _context4.next) {
668
1065
  case 0:
669
- _Object$entries8$_i = _slicedToArray(_Object$entries8[_i9], 2), key = _Object$entries8$_i[0], value = _Object$entries8$_i[1];
1066
+ _Object$entries10$_i = _slicedToArray(_Object$entries10[_i11], 2), key = _Object$entries10$_i[0], value = _Object$entries10$_i[1];
670
1067
  if (Array.isArray(value)) {
671
- // Check if this array goes in the body
672
1068
  if (bodyKeys.includes(key) || bodyKeys.some(function (k) {
673
1069
  return k.startsWith("".concat(key, "/"));
674
1070
  })) {
675
- // Don't add list type if it's already been added
676
1071
  if (!headerTypes.some(function (t) {
677
- return t.startsWith("".concat(key, "="));
1072
+ return t.startsWith("".concat(key.toLowerCase(), "="));
678
1073
  })) {
679
- headerTypes.push("".concat(key, "=\"list\""));
1074
+ headerTypes.push("".concat(key.toLowerCase(), "=\"list\""));
680
1075
  }
681
1076
  }
682
1077
  }
@@ -686,752 +1081,950 @@ function _encode() {
686
1081
  }
687
1082
  }, _loop4);
688
1083
  });
689
- _i9 = 0, _Object$entries8 = Object.entries(obj);
690
- case 61:
691
- if (!(_i9 < _Object$entries8.length)) {
692
- _context6.next = 66;
1084
+ _i11 = 0, _Object$entries10 = Object.entries(obj);
1085
+ case 85:
1086
+ if (!(_i11 < _Object$entries10.length)) {
1087
+ _context6.next = 90;
693
1088
  break;
694
1089
  }
695
- return _context6.delegateYield(_loop4(), "t1", 63);
696
- case 63:
697
- _i9++;
698
- _context6.next = 61;
1090
+ return _context6.delegateYield(_loop4(), "t1", 87);
1091
+ case 87:
1092
+ _i11++;
1093
+ _context6.next = 85;
699
1094
  break;
700
- case 66:
1095
+ case 90:
701
1096
  if (!(bodyKeys.length === 0)) {
702
- _context6.next = 70;
1097
+ _context6.next = 94;
703
1098
  break;
704
1099
  }
1100
+ console.log("No bodyKeys, returning headers only");
705
1101
  if (headerTypes.length > 0) {
706
1102
  headers["ao-types"] = headerTypes.sort().join(", ");
707
1103
  }
708
- console.log("[encode] FINAL - headers:", headers, "body:", undefined);
709
1104
  return _context6.abrupt("return", {
710
1105
  headers: headers,
711
1106
  body: undefined
712
1107
  });
713
- case 70:
714
- // Check if all body keys are for empty binaries - if so, treat as no body needed
1108
+ case 94:
715
1109
  allBodyKeysAreEmptyBinaries = bodyKeys.every(function (key) {
716
1110
  var pathParts = key.split("/");
717
1111
  var value = obj;
718
- var _iterator2 = _createForOfIteratorHelper(pathParts),
719
- _step2;
1112
+ var _iterator3 = _createForOfIteratorHelper(pathParts),
1113
+ _step3;
720
1114
  try {
721
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
722
- var part = _step2.value;
1115
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
1116
+ var part = _step3.value;
723
1117
  if (/^\d+$/.test(part)) {
724
- value = value[parseInt(part) - 1];
1118
+ var index = parseInt(part) - 1;
1119
+ console.log("[Body part] Getting array element at index ".concat(index, " from part ").concat(part));
1120
+ value = value[index];
725
1121
  } else {
726
1122
  value = value[part];
727
1123
  }
728
1124
  }
729
1125
  } catch (err) {
730
- _iterator2.e(err);
1126
+ _iterator3.e(err);
731
1127
  } finally {
732
- _iterator2.f();
1128
+ _iterator3.f();
733
1129
  }
734
1130
  return isBytes(value) && (value.length === 0 || value.byteLength === 0);
735
1131
  });
736
1132
  if (!allBodyKeysAreEmptyBinaries) {
737
- _context6.next = 75;
1133
+ _context6.next = 98;
738
1134
  break;
739
1135
  }
740
- // Treat as header-only encoding
741
1136
  if (headerTypes.length > 0) {
742
1137
  headers["ao-types"] = headerTypes.sort().join(", ");
743
1138
  }
744
- console.log("[encode] FINAL (all empty binaries) - headers:", headers, "body:", undefined);
745
1139
  return _context6.abrupt("return", {
746
1140
  headers: headers,
747
1141
  body: undefined
748
1142
  });
749
- case 75:
750
- // Sort body keys and add to headers
1143
+ case 98:
1144
+ if (!(bodyKeys.length === 1)) {
1145
+ _context6.next = 116;
1146
+ break;
1147
+ }
1148
+ singleKey = bodyKeys[0];
1149
+ pathParts = singleKey.split("/");
1150
+ value = obj;
1151
+ _iterator4 = _createForOfIteratorHelper(pathParts);
1152
+ try {
1153
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
1154
+ part = _step4.value;
1155
+ if (/^\d+$/.test(part)) {
1156
+ value = value[parseInt(part) - 1];
1157
+ } else {
1158
+ value = value[part];
1159
+ }
1160
+ }
1161
+ } catch (err) {
1162
+ _iterator4.e(err);
1163
+ } finally {
1164
+ _iterator4.f();
1165
+ }
1166
+ otherFieldsAreEmpty = Object.entries(obj).every(function (_ref5) {
1167
+ var _ref6 = _slicedToArray(_ref5, 2),
1168
+ key = _ref6[0],
1169
+ val = _ref6[1];
1170
+ if (key === singleKey) return true;
1171
+ 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;
1172
+ });
1173
+ if (!(otherFieldsAreEmpty && isBytes(value) && value.length > 0)) {
1174
+ _context6.next = 116;
1175
+ break;
1176
+ }
1177
+ _bodyBuffer2 = toBuffer(value);
1178
+ _bodyArrayBuffer2 = _bodyBuffer2.buffer.slice(_bodyBuffer2.byteOffset, _bodyBuffer2.byteOffset + _bodyBuffer2.byteLength);
1179
+ _context6.next = 110;
1180
+ return sha256(_bodyArrayBuffer2);
1181
+ case 110:
1182
+ _contentDigest4 = _context6.sent;
1183
+ _base4 = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest4));
1184
+ headers["content-digest"] = "sha-256=:".concat(_base4, ":");
1185
+ if (singleKey !== "body") {
1186
+ headers["inline-body-key"] = singleKey;
1187
+ }
1188
+ if (headerTypes.length > 0) {
1189
+ headers["ao-types"] = headerTypes.sort().join(", ");
1190
+ }
1191
+ return _context6.abrupt("return", {
1192
+ headers: headers,
1193
+ body: value
1194
+ });
1195
+ case 116:
1196
+ // Sort body keys: main array comes first, then element parts by index
751
1197
  sortedBodyKeys = bodyKeys.sort(function (a, b) {
752
- // Special sorting to ensure parent paths come before child paths
753
- if (a.startsWith(b + "/")) return 1;
754
- if (b.startsWith(a + "/")) return -1;
1198
+ var aIsArrayElement = /\/\d+$/.test(a);
1199
+ var bIsArrayElement = /\/\d+$/.test(b);
1200
+ var aBase = a.split("/")[0];
1201
+ var bBase = b.split("/")[0];
1202
+
1203
+ // If both are for the same array
1204
+ if (aBase === bBase) {
1205
+ // Main array comes before element parts
1206
+ if (!aIsArrayElement && bIsArrayElement) {
1207
+ return -1; // main array comes first
1208
+ }
1209
+ if (aIsArrayElement && !bIsArrayElement) {
1210
+ return 1; // element parts come after
1211
+ }
1212
+ // Both are elements - sort by index
1213
+ if (aIsArrayElement && bIsArrayElement) {
1214
+ var aIndex = parseInt(a.split("/")[1]);
1215
+ var bIndex = parseInt(b.split("/")[1]);
1216
+ return aIndex - bIndex;
1217
+ }
1218
+ return a.localeCompare(b);
1219
+ }
1220
+
1221
+ // Different arrays, sort by base name
755
1222
  return a.localeCompare(b);
756
- }); // Special case: if we have both 'data' and 'body' keys where data.body is binary
757
- // then we need special handling per Erlang behavior
1223
+ }); // Check if we have the special case where data contains body with bytes and body contains data with bytes
758
1224
  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);
759
1225
  headers["body-keys"] = sortedBodyKeys.map(function (k) {
760
1226
  return "\"".concat(k, "\"");
761
1227
  }).join(", ");
762
-
763
- // Check for inline keys - but not in the special data/body case
764
1228
  if (!hasSpecialDataBody) {
765
- inlineKey = headers["inline-body-key"];
766
- if (!inlineKey) {
767
- // Only set inline-body-key if we have ONLY body (not data)
768
- if (sortedBodyKeys.includes("body") && !sortedBodyKeys.includes("data")) {
769
- headers["inline-body-key"] = "body";
770
- }
1229
+ if (sortedBodyKeys.includes("body") && sortedBodyKeys.length === 1) {
1230
+ headers["inline-body-key"] = "body";
771
1231
  }
772
1232
  }
773
-
774
- // Add ao-types header if needed
775
1233
  if (headerTypes.length > 0) {
776
1234
  headers["ao-types"] = headerTypes.sort().join(", ");
777
1235
  }
778
-
779
- // Create multipart body parts
780
1236
  bodyParts = [];
781
- _iterator3 = _createForOfIteratorHelper(sortedBodyKeys);
782
- _context6.prev = 82;
1237
+ _iterator5 = _createForOfIteratorHelper(sortedBodyKeys);
1238
+ _context6.prev = 123;
783
1239
  _loop5 = /*#__PURE__*/_regeneratorRuntime().mark(function _loop5() {
784
- var bodyKey, lines, pathParts, value, parent, _i10, part, isInline, _buffer, textPart, _textPart, objectTypes, fieldLines, binaryFields, _i11, _Object$entries9, _Object$entries9$_i, _k, _v, childPath, _buffer2, _encodedItems2, onlyEmptyCollections, orderedLines, _iterator4, _step4, line, _binaryFields, _parts, _iterator5, _step5, _step5$value, _key, _buffer3, fullBody, _iterator6, _step6, _line, _parts2, headerText, _iterator7, _step7, _step7$value, _key2, _buffer4, _fullBody, hasObjects, hasArrays, nonObjectItems, _fieldLines, partTypes, _iterator8, _step8, _step8$value, item, index, _buffer5, _encodedItems3, _orderedLines, _iterator9, _step9, _line2, _iterator10, _step10, _line3, _fieldLines2, _partTypes, _orderedLines2, _iterator11, _step11, _line4, _iterator12, _step12, _line5, _fieldName, _partTypes2;
1240
+ var bodyKey, lines, pathParts, value, parent, _i12, _part, 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, parentPath, parentValue, _iterator8, _step8, _part2, _hasEmptyStrings, _hasEmptyObjects, _hasObjects, _isInline3, objectTypes, _fieldLines2, binaryFields, hasOnlyEmptyCollections, hasArraysWithOnlyEmptyElements, arrayTypes, _i13, _Object$entries11, _Object$entries11$_i, _k, _v, _i14, _Object$entries12, _Object$entries12$_i, _k2, _v2, childPath, hasOnlyEmpty, desc, _buffer3, _childPath, _hasObjects2, encodedItems, allTypes, shouldSkipObject, onlyEmptyCollections, _orderedLines4, _iterator9, _step9, _line2, binaryFieldsForInline, parts, _iterator10, _step10, _step10$value, key, _buffer4, fullBody, _isLastBodyPart, _hasOnlyTypes, _orderedLines5, hasBinaryFields, _iterator11, _step11, _line3, _parts, headerText, _i15, _binaryFields$_i, _key, _buffer5, _fullBody, isInline, _buffer6, _headerText, content;
785
1241
  return _regeneratorRuntime().wrap(function _loop5$(_context5) {
786
1242
  while (1) switch (_context5.prev = _context5.next) {
787
1243
  case 0:
788
- bodyKey = _step3.value;
789
- lines = []; // Parse the path to get to the value
1244
+ bodyKey = _step5.value;
1245
+ console.log("\n[Body part] Processing bodyKey: ".concat(bodyKey));
1246
+ lines = [];
790
1247
  pathParts = bodyKey.split("/");
791
1248
  value = obj;
792
- parent = null; // Get the actual value at this path
793
- for (_i10 = 0; _i10 < pathParts.length; _i10++) {
1249
+ parent = null;
1250
+ for (_i12 = 0; _i12 < pathParts.length; _i12++) {
794
1251
  parent = value;
795
- part = pathParts[_i10];
796
- if (/^\d+$/.test(part)) {
797
- value = value[parseInt(part) - 1];
1252
+ _part = pathParts[_i12];
1253
+ if (/^\d+$/.test(_part)) {
1254
+ value = value[parseInt(_part) - 1];
798
1255
  } else {
799
- value = value[part];
1256
+ value = value[_part];
800
1257
  }
801
1258
  }
802
- console.log("[encode] Processing body key:", bodyKey, "value type:", Array.isArray(value) ? "array" : _typeof(value));
1259
+ console.log("[Body part] Value at ".concat(bodyKey, ":"), JSON.stringify(value));
803
1260
 
804
- // Skip if value is an array with only objects (no content for this body part)
805
- if (!(Array.isArray(value) && value.every(function (item) {
806
- return isPojo(item);
807
- }))) {
808
- _context5.next = 9;
1261
+ // Special handling for empty strings in arrays
1262
+ if (!(typeof value === "string" && value === "" && pathParts.length > 1)) {
1263
+ _context5.next = 15;
809
1264
  break;
810
1265
  }
1266
+ console.log("[Body part] Creating part for empty string at ".concat(bodyKey));
1267
+ lines.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1268
+ lines.push("");
1269
+ lines.push("");
1270
+ bodyParts.push(new Blob([lines.join("\r\n")]));
811
1271
  return _context5.abrupt("return", 0);
812
- case 9:
813
- if (!(isPojo(value) && Object.keys(value).length === 0)) {
814
- _context5.next = 11;
1272
+ case 15:
1273
+ if (!Array.isArray(value)) {
1274
+ _context5.next = 45;
815
1275
  break;
816
1276
  }
817
- return _context5.abrupt("return", 0);
818
- case 11:
819
- if (!(hasSpecialDataBody && bodyKey === "data" && isPojo(value) && Object.keys(value).length === 1 && value.body && isBytes(value.body))) {
820
- _context5.next = 13;
1277
+ hasOnlyEmptyElements = value.length > 0 && value.every(function (item) {
1278
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
1279
+ });
1280
+ hasOnlyNonEmptyObjects = value.length > 0 && value.every(function (item) {
1281
+ return isPojo(item) && Object.keys(item).length > 0;
1282
+ });
1283
+ hasOnlyEmptyObjects = value.length > 0 && value.every(function (item) {
1284
+ return isPojo(item) && Object.keys(item).length === 0;
1285
+ });
1286
+ hasObjects = value.some(function (item) {
1287
+ return isPojo(item);
1288
+ });
1289
+ hasArrays = value.some(function (item) {
1290
+ return Array.isArray(item);
1291
+ });
1292
+ nonObjectItems = value.map(function (item, index) {
1293
+ return {
1294
+ item: item,
1295
+ index: index + 1
1296
+ };
1297
+ }).filter(function (_ref7) {
1298
+ var item = _ref7.item;
1299
+ return !isPojo(item);
1300
+ });
1301
+ if (!hasOnlyNonEmptyObjects) {
1302
+ _context5.next = 24;
821
1303
  break;
822
1304
  }
823
1305
  return _context5.abrupt("return", 0);
824
- case 13:
825
- console.log("[encode] Creating body part for key:", bodyKey, "value type:", _typeof(value), "isBytes:", isBytes(value));
826
-
827
- // Determine content-disposition
828
- isInline = bodyKey === "body" && headers["inline-body-key"] === "body";
829
- if (isInline) {
830
- lines.push("content-disposition: inline");
1306
+ case 24:
1307
+ if (!hasOnlyEmptyElements) {
1308
+ _context5.next = 31;
1309
+ break;
1310
+ }
1311
+ _fieldLines = [];
1312
+ _partTypes = []; // Process items for type information
1313
+ value.forEach(function (item, idx) {
1314
+ var index = idx + 1;
1315
+ if (Array.isArray(item) && item.length === 0) {
1316
+ _partTypes.push("".concat(index, "=\"empty-list\""));
1317
+ } else if (isPojo(item) && Object.keys(item).length === 0) {
1318
+ _partTypes.push("".concat(index, "=\"empty-message\""));
1319
+ } else if (typeof item === "string" && item === "") {
1320
+ _partTypes.push("".concat(index, "=\"empty-binary\""));
1321
+ }
1322
+ });
1323
+ _isInline = bodyKey === "body" && headers["inline-body-key"] === "body";
1324
+ if (_isInline) {
1325
+ orderedLines = [];
1326
+ if (_partTypes.length > 0) {
1327
+ orderedLines.push("ao-types: ".concat(_partTypes.sort(function (a, b) {
1328
+ var aNum = parseInt(a.split("=")[0]);
1329
+ var bNum = parseInt(b.split("=")[0]);
1330
+ return aNum - bNum;
1331
+ }).join(", ")));
1332
+ }
1333
+ orderedLines.push("content-disposition: inline");
1334
+ orderedLines.push("");
1335
+ bodyParts.push(new Blob([orderedLines.join("\r\n")]));
831
1336
  } else {
832
- lines.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1337
+ _orderedLines = [];
1338
+ if (_partTypes.length > 0) {
1339
+ _orderedLines.push("ao-types: ".concat(_partTypes.sort(function (a, b) {
1340
+ var aNum = parseInt(a.split("=")[0]);
1341
+ var bNum = parseInt(b.split("=")[0]);
1342
+ return aNum - bNum;
1343
+ }).join(", ")));
1344
+ }
1345
+ _orderedLines.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1346
+
1347
+ // Check if this is the last body part
1348
+ isLastBodyPart = sortedBodyKeys.indexOf(bodyKey) === sortedBodyKeys.length - 1;
1349
+ hasOnlyTypes = _partTypes.length > 0 && _fieldLines.length === 0;
1350
+ if (isLastBodyPart && hasOnlyTypes) {
1351
+ // Don't add empty line for last part with only types
1352
+ bodyParts.push(new Blob([_orderedLines.join("\r\n")]));
1353
+ } else {
1354
+ _orderedLines.push("");
1355
+ bodyParts.push(new Blob([_orderedLines.join("\r\n")]));
1356
+ }
833
1357
  }
834
- console.log("[encode] Value type checks:", {
835
- isBytes: isBytes(value),
836
- isPojo: isPojo(value),
837
- isArray: Array.isArray(value),
838
- valueType: _typeof(value)
1358
+ return _context5.abrupt("return", 0);
1359
+ case 31:
1360
+ // Build list of which indices have their own parts
1361
+ indicesWithOwnParts = new Set();
1362
+ sortedBodyKeys.forEach(function (key) {
1363
+ if (key.startsWith(bodyKey + "/")) {
1364
+ var subPath = key.substring(bodyKey.length + 1);
1365
+ var match = subPath.match(/^(\d+)/);
1366
+ if (match) {
1367
+ indicesWithOwnParts.add(parseInt(match[1]));
1368
+ }
1369
+ }
839
1370
  });
840
- if (!isBytes(value)) {
841
- _context5.next = 23;
842
- break;
1371
+
1372
+ // Check if this array contains sub-arrays with objects that have their own parts
1373
+ hasNestedObjectParts = sortedBodyKeys.some(function (key) {
1374
+ return key.startsWith(bodyKey + "/") && key.split("/").length > pathParts.length + 1;
1375
+ });
1376
+ fieldLines = [];
1377
+ partTypes = []; // For arrays that contain sub-arrays with objects, we need to add type info for the sub-arrays
1378
+ if (hasNestedObjectParts) {
1379
+ value.forEach(function (item, idx) {
1380
+ var index = idx + 1;
1381
+ if (Array.isArray(item)) {
1382
+ partTypes.push("".concat(index, "=\"list\""));
1383
+ }
1384
+ });
843
1385
  }
844
- console.log("[encode] Processing binary value for key:", bodyKey);
845
- // Binary data
846
- _buffer = toBuffer(value); // Check if this is a nested path like "data/body"
847
- if (bodyKey.includes("/")) {
848
- // For nested binary, we need to replace the disposition
849
- lines[lines.length - 1] = "content-disposition: form-data;name=\"".concat(bodyKey, "\"");
850
- lines.push(""); // Empty line
851
- lines.push(""); // Another empty line before binary
852
- textPart = lines.join("\r\n");
853
- bodyParts.push(new Blob([textPart, _buffer]));
1386
+
1387
+ // Check if this array has mixed content with empty strings/objects
1388
+ hasEmptyStrings = value.some(function (item) {
1389
+ return typeof item === "string" && item === "";
1390
+ });
1391
+ hasEmptyObjects = value.some(function (item) {
1392
+ return isPojo(item) && Object.keys(item).length === 0;
1393
+ }); // Check if we have objects with only empty values (like {empty: ""})
1394
+ hasObjectsWithOnlyEmptyValues = value.some(function (item) {
1395
+ if (!isPojo(item) || Object.keys(item).length === 0) return false;
1396
+ return Object.values(item).every(function (v) {
1397
+ return typeof v === "string" && v === "" || Array.isArray(v) && v.length === 0 || isPojo(v) && Object.keys(v).length === 0;
1398
+ });
1399
+ });
1400
+ isMixedArray = hasObjects && (hasEmptyStrings || hasEmptyObjects) && !hasObjectsWithOnlyEmptyValues; // Process ALL items for type information
1401
+ value.forEach(function (item, idx) {
1402
+ var index = idx + 1;
1403
+
1404
+ // Skip type info for elements that have their own parts
1405
+ if (indicesWithOwnParts.has(index)) {
1406
+ return;
1407
+ }
1408
+
1409
+ // If we have nested object parts and this is an array with objects, skip processing it inline
1410
+ if (hasNestedObjectParts && Array.isArray(item) && item.some(function (subItem) {
1411
+ return isPojo(subItem);
1412
+ })) {
1413
+ // Type info already added above
1414
+ return;
1415
+ }
1416
+
1417
+ // For all arrays (not just mixed ones), we need to process all items
1418
+ if (typeof item === "string" && item === "") {
1419
+ // Empty strings get type annotation but no field line
1420
+ partTypes.push("".concat(index, "=\"empty-binary\""));
1421
+ } else if (isPojo(item) && Object.keys(item).length === 0) {
1422
+ // Empty objects don't get field lines but do get type annotations
1423
+ partTypes.push("".concat(index, "=\"empty-message\""));
1424
+ } else if (isPojo(item)) {
1425
+ // Non-empty objects might have parts
1426
+ } else if (Array.isArray(item)) {
1427
+ if (item.length === 0) {
1428
+ // Empty arrays don't get field lines but do get type annotations
1429
+ partTypes.push("".concat(index, "=\"empty-list\""));
1430
+ } else {
1431
+ partTypes.push("".concat(index, "=\"list\""));
1432
+ // Encode array
1433
+ var encodedItems = item.map(function (subItem) {
1434
+ if (typeof subItem === "number") {
1435
+ if (Number.isInteger(subItem)) {
1436
+ return "\"(ao-type-integer) ".concat(subItem, "\"");
1437
+ } else {
1438
+ return "\"(ao-type-float) ".concat(formatFloat(subItem), "\"");
1439
+ }
1440
+ } else if (typeof subItem === "string") {
1441
+ return "\"".concat(subItem, "\"");
1442
+ } else if (subItem === null) {
1443
+ return "\"(ao-type-atom) \\\"null\\\"\"";
1444
+ } else if (subItem === undefined) {
1445
+ return "\"(ao-type-atom) \\\"undefined\\\"\"";
1446
+ } else if (_typeof(subItem) === "symbol") {
1447
+ var desc = subItem.description || "Symbol.for()";
1448
+ return "\"(ao-type-atom) \\\"".concat(desc, "\\\"\"");
1449
+ } else if (typeof subItem === "boolean") {
1450
+ return "\"(ao-type-atom) \\\"".concat(subItem, "\\\"\"");
1451
+ } else if (Array.isArray(subItem)) {
1452
+ // Use the full encodeArrayItem for nested arrays
1453
+ return encodeArrayItem(subItem);
1454
+ } else if (isBytes(subItem)) {
1455
+ var _buffer = toBuffer(subItem);
1456
+ if (_buffer.length === 0 || _buffer.byteLength === 0) {
1457
+ return "\"\"";
1458
+ }
1459
+ return "\"(ao-type-binary)\"";
1460
+ } else if (isPojo(subItem)) {
1461
+ var json = JSON.stringify(subItem);
1462
+ var escaped = json.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1463
+ return "\"(ao-type-map) ".concat(escaped, "\"");
1464
+ } else {
1465
+ return "\"".concat(String(subItem), "\"");
1466
+ }
1467
+ }).join(", ");
1468
+ fieldLines.push("".concat(index, ": ").concat(encodedItems));
1469
+ }
1470
+ } else if (typeof item === "number") {
1471
+ if (Number.isInteger(item)) {
1472
+ partTypes.push("".concat(index, "=\"integer\""));
1473
+ fieldLines.push("".concat(index, ": ").concat(item));
1474
+ } else {
1475
+ partTypes.push("".concat(index, "=\"float\""));
1476
+ fieldLines.push("".concat(index, ": ").concat(formatFloat(item)));
1477
+ }
1478
+ } else if (typeof item === "string") {
1479
+ // Non-empty strings just get field lines, no type annotation
1480
+ fieldLines.push("".concat(index, ": ").concat(item));
1481
+ } else if (item === null || item === undefined || _typeof(item) === "symbol" || typeof item === "boolean") {
1482
+ partTypes.push("".concat(index, "=\"atom\""));
1483
+ if (item === null) {
1484
+ fieldLines.push("".concat(index, ": null"));
1485
+ } else if (item === undefined) {
1486
+ fieldLines.push("".concat(index, ": undefined"));
1487
+ } else if (_typeof(item) === "symbol") {
1488
+ var desc = item.description || "Symbol.for()";
1489
+ fieldLines.push("".concat(index, ": ").concat(desc));
1490
+ } else {
1491
+ fieldLines.push("".concat(index, ": ").concat(item));
1492
+ }
1493
+ } else if (isBytes(item)) {
1494
+ var _buffer2 = toBuffer(item);
1495
+ if (_buffer2.length === 0) {
1496
+ partTypes.push("".concat(index, "=\"empty-binary\""));
1497
+ } else {
1498
+ partTypes.push("".concat(index, "=\"binary\""));
1499
+ }
1500
+ }
1501
+ });
1502
+ _isInline2 = bodyKey === "body" && headers["inline-body-key"] === "body";
1503
+ if (_isInline2) {
1504
+ _orderedLines2 = [];
1505
+ if (partTypes.length > 0) {
1506
+ _orderedLines2.push("ao-types: ".concat(partTypes.sort(function (a, b) {
1507
+ var aNum = parseInt(a.split("=")[0]);
1508
+ var bNum = parseInt(b.split("=")[0]);
1509
+ return aNum - bNum;
1510
+ }).join(", ")));
1511
+ }
1512
+ _orderedLines2.push("content-disposition: inline");
1513
+ if (fieldLines.length > 0) {
1514
+ _orderedLines2.push("");
1515
+ _iterator6 = _createForOfIteratorHelper(fieldLines);
1516
+ try {
1517
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
1518
+ line = _step6.value;
1519
+ _orderedLines2.push(line);
1520
+ }
1521
+ } catch (err) {
1522
+ _iterator6.e(err);
1523
+ } finally {
1524
+ _iterator6.f();
1525
+ }
1526
+ }
1527
+ bodyParts.push(new Blob([_orderedLines2.join("\r\n") + "\r\n"]));
854
1528
  } else {
855
- lines.push(""); // Empty line after headers
856
- lines.push(""); // Another empty line before binary data
857
- _textPart = lines.join("\r\n");
858
- bodyParts.push(new Blob([_textPart, _buffer]));
1529
+ // Put ao-types first, then content-disposition, then field lines
1530
+ _orderedLines3 = [];
1531
+ if (partTypes.length > 0) {
1532
+ _orderedLines3.push("ao-types: ".concat(partTypes.sort(function (a, b) {
1533
+ var aNum = parseInt(a.split("=")[0]);
1534
+ var bNum = parseInt(b.split("=")[0]);
1535
+ return aNum - bNum;
1536
+ }).join(", ")));
1537
+ }
1538
+ _orderedLines3.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1539
+
1540
+ // Add field lines directly without blank line
1541
+ _iterator7 = _createForOfIteratorHelper(fieldLines);
1542
+ try {
1543
+ for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
1544
+ _line = _step7.value;
1545
+ _orderedLines3.push(_line);
1546
+ }
1547
+
1548
+ // Add trailing blank line if we have field lines
1549
+ } catch (err) {
1550
+ _iterator7.e(err);
1551
+ } finally {
1552
+ _iterator7.f();
1553
+ }
1554
+ if (fieldLines.length > 0) {
1555
+ bodyParts.push(new Blob([_orderedLines3.join("\r\n") + "\r\n"]));
1556
+ } else {
1557
+ bodyParts.push(new Blob([_orderedLines3.join("\r\n")]));
1558
+ }
859
1559
  }
860
- _context5.next = 80;
861
- break;
862
- case 23:
1560
+ return _context5.abrupt("return", 0);
1561
+ case 45:
863
1562
  if (!isPojo(value)) {
864
- _context5.next = 79;
1563
+ _context5.next = 126;
1564
+ break;
1565
+ }
1566
+ if (!(Object.keys(value).length === 0)) {
1567
+ _context5.next = 60;
1568
+ break;
1569
+ }
1570
+ // Check if this is a parent array context where empty objects should get parts
1571
+ parentPath = pathParts.slice(0, -1).join("/");
1572
+ parentValue = obj;
1573
+ _iterator8 = _createForOfIteratorHelper(pathParts.slice(0, -1));
1574
+ try {
1575
+ for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
1576
+ _part2 = _step8.value;
1577
+ if (/^\d+$/.test(_part2)) {
1578
+ parentValue = parentValue[parseInt(_part2) - 1];
1579
+ } else {
1580
+ parentValue = parentValue[_part2];
1581
+ }
1582
+ }
1583
+ } catch (err) {
1584
+ _iterator8.e(err);
1585
+ } finally {
1586
+ _iterator8.f();
1587
+ }
1588
+ if (!Array.isArray(parentValue)) {
1589
+ _context5.next = 58;
865
1590
  break;
866
1591
  }
867
- console.log("[encode] Processing object value");
868
- // Object - only include fields that aren't handled by nested body parts
1592
+ // Check if parent array has mixed content
1593
+ _hasEmptyStrings = parentValue.some(function (item) {
1594
+ return typeof item === "string" && item === "";
1595
+ });
1596
+ _hasEmptyObjects = parentValue.some(function (item) {
1597
+ return isPojo(item) && Object.keys(item).length === 0;
1598
+ });
1599
+ _hasObjects = parentValue.some(function (item) {
1600
+ return isPojo(item);
1601
+ });
1602
+ if (!(_hasObjects && (_hasEmptyStrings || _hasEmptyObjects))) {
1603
+ _context5.next = 58;
1604
+ break;
1605
+ }
1606
+ // Special mixed array case - empty objects don't get parts
1607
+ console.log("[Body part] Skipping empty object in mixed array at ".concat(bodyKey));
1608
+ return _context5.abrupt("return", 0);
1609
+ case 58:
1610
+ // Normal case - empty objects might get parts
1611
+ console.log("[Body part] Empty object at ".concat(bodyKey));
1612
+ // For now, skip empty objects
1613
+ return _context5.abrupt("return", 0);
1614
+ case 60:
1615
+ if (!(hasSpecialDataBody && bodyKey === "data" && Object.keys(value).length === 1 && value.body && isBytes(value.body))) {
1616
+ _context5.next = 62;
1617
+ break;
1618
+ }
1619
+ return _context5.abrupt("return", 0);
1620
+ case 62:
1621
+ // Handle non-empty objects
1622
+ _isInline3 = bodyKey === "body" && headers["inline-body-key"] === "body";
1623
+ if (_isInline3) {
1624
+ lines.push("content-disposition: inline");
1625
+ } else {
1626
+ lines.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1627
+ }
1628
+
1629
+ // Continue with object processing...
869
1630
  objectTypes = [];
870
- fieldLines = [];
871
- binaryFields = [];
872
- _i11 = 0, _Object$entries9 = Object.entries(value);
873
- case 29:
874
- if (!(_i11 < _Object$entries9.length)) {
875
- _context5.next = 75;
1631
+ _fieldLines2 = [];
1632
+ binaryFields = []; // First check if this object only contains empty collections
1633
+ hasOnlyEmptyCollections = Object.entries(value).every(function (_ref8) {
1634
+ var _ref9 = _slicedToArray(_ref8, 2),
1635
+ k = _ref9[0],
1636
+ v = _ref9[1];
1637
+ 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;
1638
+ }); // Also check if it contains arrays with only empty elements
1639
+ hasArraysWithOnlyEmptyElements = Object.entries(value).some(function (_ref10) {
1640
+ var _ref11 = _slicedToArray(_ref10, 2),
1641
+ k = _ref11[0],
1642
+ v = _ref11[1];
1643
+ return Array.isArray(v) && v.length > 0 && v.every(function (item) {
1644
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
1645
+ });
1646
+ }); // Collect type information for arrays in the object BEFORE processing fields
1647
+ arrayTypes = [];
1648
+ for (_i13 = 0, _Object$entries11 = Object.entries(value); _i13 < _Object$entries11.length; _i13++) {
1649
+ _Object$entries11$_i = _slicedToArray(_Object$entries11[_i13], 2), _k = _Object$entries11$_i[0], _v = _Object$entries11$_i[1];
1650
+ if (Array.isArray(_v)) {
1651
+ arrayTypes.push("".concat(_k.toLowerCase(), "=\"").concat(_v.length === 0 ? "empty-list" : "list", "\""));
1652
+ }
1653
+ }
1654
+ _i14 = 0, _Object$entries12 = Object.entries(value);
1655
+ case 72:
1656
+ if (!(_i14 < _Object$entries12.length)) {
1657
+ _context5.next = 119;
876
1658
  break;
877
1659
  }
878
- _Object$entries9$_i = _slicedToArray(_Object$entries9[_i11], 2), _k = _Object$entries9$_i[0], _v = _Object$entries9$_i[1];
879
- childPath = "".concat(bodyKey, "/").concat(_k); // Skip if this field has its own body part
1660
+ _Object$entries12$_i = _slicedToArray(_Object$entries12[_i14], 2), _k2 = _Object$entries12$_i[0], _v2 = _Object$entries12$_i[1];
1661
+ childPath = "".concat(bodyKey, "/").concat(_k2);
880
1662
  if (!sortedBodyKeys.includes(childPath)) {
881
- _context5.next = 34;
1663
+ _context5.next = 77;
882
1664
  break;
883
1665
  }
884
- return _context5.abrupt("continue", 72);
885
- case 34:
886
- if (!(Array.isArray(_v) && _v.some(function (item) {
1666
+ return _context5.abrupt("continue", 116);
1667
+ case 77:
1668
+ if (!(Array.isArray(_v2) && _v2.some(function (item) {
887
1669
  return isPojo(item);
888
1670
  }))) {
889
- _context5.next = 36;
1671
+ _context5.next = 83;
890
1672
  break;
891
1673
  }
892
- return _context5.abrupt("continue", 72);
893
- case 36:
894
- // Add type info
895
- if (Array.isArray(_v)) {
896
- objectTypes.push("".concat(_k, "=\"").concat(_v.length === 0 ? "empty-list" : "list", "\""));
897
- } else if (_v === null || _v === undefined || _typeof(_v) === "symbol" || typeof _v === "boolean") {
898
- objectTypes.push("".concat(_k, "=\"atom\""));
899
- } else if (typeof _v === "number") {
900
- objectTypes.push("".concat(_k, "=\"").concat(Number.isInteger(_v) ? "integer" : "float", "\""));
901
- } else if (typeof _v === "string" && _v.length === 0) {
902
- objectTypes.push("".concat(_k, "=\"empty-binary\""));
903
- } else if (isBytes(_v) && (_v.length === 0 || _v.byteLength === 0)) {
904
- objectTypes.push("".concat(_k, "=\"empty-binary\""));
905
- } else if (isPojo(_v) && Object.keys(_v).length === 0) {
906
- objectTypes.push("".concat(_k, "=\"empty-message\""));
1674
+ if (!sortedBodyKeys.includes(childPath)) {
1675
+ _context5.next = 80;
1676
+ break;
907
1677
  }
908
-
909
- // Add field value
910
- if (!(typeof _v === "string")) {
911
- _context5.next = 41;
1678
+ return _context5.abrupt("continue", 116);
1679
+ case 80:
1680
+ // Check if array has only empty elements
1681
+ hasOnlyEmpty = _v2.every(function (item) {
1682
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
1683
+ });
1684
+ if (!hasOnlyEmpty) {
1685
+ _context5.next = 83;
912
1686
  break;
913
1687
  }
914
- fieldLines.push("".concat(_k, ": ").concat(_v));
915
- _context5.next = 72;
916
- break;
917
- case 41:
918
- if (!(typeof _v === "number")) {
919
- _context5.next = 45;
1688
+ return _context5.abrupt("continue", 116);
1689
+ case 83:
1690
+ if (Array.isArray(_v2)) {
1691
+ // Type info already added above in arrayTypes
1692
+ } else if (_v2 === null || _v2 === undefined || _typeof(_v2) === "symbol" || typeof _v2 === "boolean") {
1693
+ objectTypes.push("".concat(_k2.toLowerCase(), "=\"atom\""));
1694
+ } else if (typeof _v2 === "number") {
1695
+ objectTypes.push("".concat(_k2.toLowerCase(), "=\"").concat(Number.isInteger(_v2) ? "integer" : "float", "\""));
1696
+ } else if (typeof _v2 === "string" && _v2.length === 0) {
1697
+ objectTypes.push("".concat(_k2.toLowerCase(), "=\"empty-binary\""));
1698
+ } else if (isBytes(_v2) && (_v2.length === 0 || _v2.byteLength === 0)) {
1699
+ objectTypes.push("".concat(_k2.toLowerCase(), "=\"empty-binary\""));
1700
+ } else if (isPojo(_v2) && Object.keys(_v2).length === 0) {
1701
+ objectTypes.push("".concat(_k2.toLowerCase(), "=\"empty-message\""));
1702
+ }
1703
+ if (!(typeof _v2 === "string")) {
1704
+ _context5.next = 88;
920
1705
  break;
921
1706
  }
922
- fieldLines.push("".concat(_k, ": ").concat(_v));
923
- _context5.next = 72;
1707
+ if (_v2.length === 0) {
1708
+ // Empty strings are shown as "empty: " (with trailing space)
1709
+ _fieldLines2.push("".concat(_k2, ": "));
1710
+ } else {
1711
+ _fieldLines2.push("".concat(_k2, ": ").concat(_v2));
1712
+ }
1713
+ _context5.next = 116;
924
1714
  break;
925
- case 45:
926
- if (!(typeof _v === "boolean")) {
927
- _context5.next = 49;
1715
+ case 88:
1716
+ if (!(typeof _v2 === "number")) {
1717
+ _context5.next = 92;
928
1718
  break;
929
1719
  }
930
- fieldLines.push("".concat(_k, ": \"").concat(_v, "\""));
931
- _context5.next = 72;
1720
+ _fieldLines2.push("".concat(_k2, ": ").concat(_v2));
1721
+ _context5.next = 116;
932
1722
  break;
933
- case 49:
934
- if (!(_v === null)) {
935
- _context5.next = 53;
1723
+ case 92:
1724
+ if (!(typeof _v2 === "boolean")) {
1725
+ _context5.next = 96;
936
1726
  break;
937
1727
  }
938
- fieldLines.push("".concat(_k, ": \"null\""));
939
- _context5.next = 72;
1728
+ _fieldLines2.push("".concat(_k2, ": \"").concat(_v2, "\""));
1729
+ _context5.next = 116;
940
1730
  break;
941
- case 53:
942
- if (!(_v === undefined)) {
943
- _context5.next = 57;
1731
+ case 96:
1732
+ if (!(_v2 === null)) {
1733
+ _context5.next = 100;
944
1734
  break;
945
1735
  }
946
- fieldLines.push("".concat(_k, ": \"undefined\""));
947
- _context5.next = 72;
1736
+ _fieldLines2.push("".concat(_k2, ": \"null\""));
1737
+ _context5.next = 116;
948
1738
  break;
949
- case 57:
950
- if (!(_typeof(_v) === "symbol")) {
951
- _context5.next = 61;
1739
+ case 100:
1740
+ if (!(_v2 === undefined)) {
1741
+ _context5.next = 104;
952
1742
  break;
953
1743
  }
954
- fieldLines.push("".concat(_k, ": \"").concat(_v.description || "Symbol.for()", "\""));
955
- _context5.next = 72;
1744
+ _fieldLines2.push("".concat(_k2, ": \"undefined\""));
1745
+ _context5.next = 116;
956
1746
  break;
957
- case 61:
958
- if (!isBytes(_v)) {
959
- _context5.next = 71;
1747
+ case 104:
1748
+ if (!(_typeof(_v2) === "symbol")) {
1749
+ _context5.next = 109;
960
1750
  break;
961
1751
  }
962
- _buffer2 = toBuffer(_v); // For inline data/body parts, binary fields get raw bytes
963
- if (!isInline) {
964
- _context5.next = 67;
1752
+ desc = _v2.description || "Symbol.for()";
1753
+ _fieldLines2.push("".concat(_k2, ": \"").concat(desc, "\""));
1754
+ _context5.next = 116;
1755
+ break;
1756
+ case 109:
1757
+ if (!isBytes(_v2)) {
1758
+ _context5.next = 115;
965
1759
  break;
966
1760
  }
967
- return _context5.abrupt("continue", 72);
968
- case 67:
969
- // For non-inline parts, we need to add raw bytes, not base64
970
- // Store the binary field for later processing
1761
+ _buffer3 = toBuffer(_v2);
971
1762
  binaryFields.push({
972
- key: _k,
973
- buffer: _buffer2
1763
+ key: _k2,
1764
+ buffer: _buffer3
974
1765
  });
975
- return _context5.abrupt("continue", 72);
976
- case 69:
977
- _context5.next = 72;
978
- break;
979
- case 71:
980
- if (Array.isArray(_v)) {
981
- if (_v.length === 0) {
982
- fieldLines.push("".concat(_k, ": "));
1766
+ return _context5.abrupt("continue", 116);
1767
+ case 115:
1768
+ if (Array.isArray(_v2)) {
1769
+ if (_v2.length === 0) {
1770
+ // Don't add field line for empty array
983
1771
  } else {
984
- _encodedItems2 = _v.map(function (item) {
985
- return encodeArrayItem(item);
986
- }).join(", ");
987
- fieldLines.push("".concat(_k, ": ").concat(_encodedItems2));
1772
+ // Check if this array will have its own body part
1773
+ _childPath = "".concat(bodyKey, "/").concat(_k2);
1774
+ if (!sortedBodyKeys.includes(_childPath)) {
1775
+ // Check if this array contains objects - if so, don't add field line
1776
+ _hasObjects2 = _v2.some(function (item) {
1777
+ return isPojo(item);
1778
+ });
1779
+ if (!_hasObjects2) {
1780
+ encodedItems = _v2.map(function (item) {
1781
+ return encodeArrayItem(item);
1782
+ }).join(", ");
1783
+ _fieldLines2.push("".concat(_k2, ": ").concat(encodedItems));
1784
+ }
1785
+ }
988
1786
  }
989
- } else if (isPojo(_v) && Object.keys(_v).length === 0) {
990
- // Empty object - no content line needed, just ao-type
1787
+ } else if (isPojo(_v2) && Object.keys(_v2).length === 0) {
1788
+ // Empty object - don't add field line
991
1789
  }
992
- case 72:
993
- _i11++;
994
- _context5.next = 29;
1790
+ case 116:
1791
+ _i14++;
1792
+ _context5.next = 72;
995
1793
  break;
996
- case 75:
997
- // Check if this object only has empty collections
998
- onlyEmptyCollections = Object.entries(value).every(function (_ref) {
999
- var _ref2 = _slicedToArray(_ref, 2),
1000
- k = _ref2[0],
1001
- v = _ref2[1];
1794
+ case 119:
1795
+ // Combine arrayTypes with objectTypes
1796
+ allTypes = [].concat(arrayTypes, objectTypes); // Check if this object should be skipped entirely
1797
+ shouldSkipObject = Object.entries(value).every(function (_ref12) {
1798
+ var _ref13 = _slicedToArray(_ref12, 2),
1799
+ k = _ref13[0],
1800
+ v = _ref13[1];
1801
+ var childPath = "".concat(bodyKey, "/").concat(k);
1802
+ if (sortedBodyKeys.includes(childPath)) return true;
1803
+ if (Array.isArray(v) && v.some(function (item) {
1804
+ return isPojo(item);
1805
+ })) {
1806
+ // Check if array has only empty elements
1807
+ var _hasOnlyEmpty = v.every(function (item) {
1808
+ return Array.isArray(item) && item.length === 0 || isPojo(item) && Object.keys(item).length === 0 || typeof item === "string" && item === "";
1809
+ });
1810
+ return _hasOnlyEmpty || sortedBodyKeys.includes(childPath);
1811
+ }
1812
+ return false;
1813
+ });
1814
+ if (!(shouldSkipObject && !hasOnlyEmptyCollections && !hasArraysWithOnlyEmptyElements)) {
1815
+ _context5.next = 123;
1816
+ break;
1817
+ }
1818
+ return _context5.abrupt("return", 0);
1819
+ case 123:
1820
+ onlyEmptyCollections = Object.entries(value).every(function (_ref14) {
1821
+ var _ref15 = _slicedToArray(_ref14, 2),
1822
+ k = _ref15[0],
1823
+ v = _ref15[1];
1002
1824
  var childPath = "".concat(bodyKey, "/").concat(k);
1003
1825
  if (sortedBodyKeys.includes(childPath)) return true;
1004
1826
  if (Array.isArray(v) && v.some(function (item) {
1005
1827
  return isPojo(item);
1006
1828
  })) return true;
1007
1829
  return Array.isArray(v) && v.length === 0 || isPojo(v) && Object.keys(v).length === 0 || isBytes(v) && (v.length === 0 || v.byteLength === 0);
1008
- }); // Special handling for inline body
1009
- if (isInline) {
1010
- // For inline: fields first, then ao-types, then content-disposition
1011
- orderedLines = []; // First: field lines
1012
- if (!onlyEmptyCollections) {
1013
- _iterator4 = _createForOfIteratorHelper(fieldLines);
1014
- try {
1015
- for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
1016
- line = _step4.value;
1017
- orderedLines.push(line);
1018
- }
1019
- } catch (err) {
1020
- _iterator4.e(err);
1021
- } finally {
1022
- _iterator4.f();
1830
+ });
1831
+ if (_isInline3) {
1832
+ _orderedLines4 = []; // FIXED: For inline body, put field lines FIRST
1833
+ _iterator9 = _createForOfIteratorHelper(_fieldLines2);
1834
+ try {
1835
+ for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
1836
+ _line2 = _step9.value;
1837
+ _orderedLines4.push(_line2);
1023
1838
  }
1839
+ } catch (err) {
1840
+ _iterator9.e(err);
1841
+ } finally {
1842
+ _iterator9.f();
1024
1843
  }
1025
-
1026
- // Then: ao-types
1027
- if (objectTypes.length > 0) {
1028
- orderedLines.push("ao-types: ".concat(objectTypes.sort().join(", ")));
1844
+ if (allTypes.length > 0) {
1845
+ _orderedLines4.push("ao-types: ".concat(allTypes.sort().join(", ")));
1029
1846
  }
1030
-
1031
- // Finally: content-disposition
1032
- orderedLines.push("content-disposition: inline");
1033
-
1034
- // Check if this has binary fields
1035
- _binaryFields = Object.entries(value).filter(function (_ref3) {
1036
- var _ref4 = _slicedToArray(_ref3, 2),
1037
- k = _ref4[0],
1038
- v = _ref4[1];
1847
+ _orderedLines4.push("content-disposition: inline");
1848
+ binaryFieldsForInline = Object.entries(value).filter(function (_ref16) {
1849
+ var _ref17 = _slicedToArray(_ref16, 2),
1850
+ k = _ref17[0],
1851
+ v = _ref17[1];
1039
1852
  return isBytes(v) && !sortedBodyKeys.includes("".concat(bodyKey, "/").concat(k));
1040
- }).map(function (_ref5) {
1041
- var _ref6 = _slicedToArray(_ref5, 2),
1042
- k = _ref6[0],
1043
- v = _ref6[1];
1853
+ }).map(function (_ref18) {
1854
+ var _ref19 = _slicedToArray(_ref18, 2),
1855
+ k = _ref19[0],
1856
+ v = _ref19[1];
1044
1857
  return {
1045
1858
  key: k,
1046
1859
  buffer: toBuffer(v)
1047
1860
  };
1048
1861
  });
1049
- if (_binaryFields.length > 0) {
1050
- // Build the parts
1051
- _parts = []; // Add the text part
1052
- _parts.push(Buffer.from(orderedLines.join("\r\n")));
1053
-
1054
- // Add binary fields with raw bytes
1055
- _iterator5 = _createForOfIteratorHelper(_binaryFields);
1862
+ if (binaryFieldsForInline.length > 0) {
1863
+ parts = [];
1864
+ parts.push(Buffer.from(_orderedLines4.join("\r\n")));
1865
+ _iterator10 = _createForOfIteratorHelper(binaryFieldsForInline);
1056
1866
  try {
1057
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
1058
- _step5$value = _step5.value, _key = _step5$value.key, _buffer3 = _step5$value.buffer;
1059
- _parts.push(Buffer.from("\r\n".concat(_key, ": ")));
1060
- _parts.push(_buffer3);
1867
+ for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
1868
+ _step10$value = _step10.value, key = _step10$value.key, _buffer4 = _step10$value.buffer;
1869
+ parts.push(Buffer.from("\r\n".concat(key, ": ")));
1870
+ parts.push(_buffer4);
1061
1871
  }
1062
-
1063
- // Add trailing \r\n for inline parts with binary
1064
1872
  } catch (err) {
1065
- _iterator5.e(err);
1873
+ _iterator10.e(err);
1066
1874
  } finally {
1067
- _iterator5.f();
1875
+ _iterator10.f();
1068
1876
  }
1069
- _parts.push(Buffer.from("\r\n"));
1070
- fullBody = Buffer.concat(_parts);
1877
+ parts.push(Buffer.from("\r\n"));
1878
+ fullBody = Buffer.concat(parts);
1071
1879
  bodyParts.push(new Blob([fullBody]));
1072
1880
  } else {
1073
- orderedLines.push(""); // Add empty line for trailing \r\n
1074
- bodyParts.push(new Blob([orderedLines.join("\r\n")]));
1881
+ // Check if this is the last body part for special handling
1882
+ _isLastBodyPart = sortedBodyKeys.indexOf(bodyKey) === sortedBodyKeys.length - 1;
1883
+ _hasOnlyTypes = allTypes.length > 0 && _fieldLines2.length === 0;
1884
+ if (_isLastBodyPart && _hasOnlyTypes) {
1885
+ // Don't add empty line for last part with only types
1886
+ bodyParts.push(new Blob([_orderedLines4.join("\r\n")]));
1887
+ } else if (_fieldLines2.length === 0) {
1888
+ bodyParts.push(new Blob([_orderedLines4.join("\r\n")]));
1889
+ } else {
1890
+ bodyParts.push(new Blob([_orderedLines4.join("\r\n") + "\r\n"]));
1891
+ }
1075
1892
  }
1076
1893
  } else {
1077
- // Normal handling (non-inline)
1078
- // ao-types first if needed
1079
- if (objectTypes.length > 0) {
1080
- lines.unshift("ao-types: ".concat(objectTypes.sort().join(", ")));
1894
+ // Put ao-types first, then content-disposition, then field lines
1895
+ _orderedLines5 = [];
1896
+ if (allTypes.length > 0) {
1897
+ _orderedLines5.push("ao-types: ".concat(allTypes.sort().join(", ")));
1081
1898
  }
1899
+ _orderedLines5.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1082
1900
 
1083
- // Only add field lines if not all collections are empty
1084
- if (!onlyEmptyCollections) {
1085
- _iterator6 = _createForOfIteratorHelper(fieldLines);
1086
- try {
1087
- for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
1088
- _line = _step6.value;
1089
- lines.push(_line);
1090
- }
1091
- } catch (err) {
1092
- _iterator6.e(err);
1093
- } finally {
1094
- _iterator6.f();
1095
- }
1901
+ // Check if we have any binary fields
1902
+ hasBinaryFields = binaryFields && binaryFields.length > 0; // Add blank line before field lines if we have binary fields or no field lines
1903
+ if (hasBinaryFields || _fieldLines2.length === 0) {
1904
+ _orderedLines5.push("");
1096
1905
  }
1097
1906
 
1098
- // Then handle binary fields with raw bytes if any
1907
+ // Add field lines
1908
+ _iterator11 = _createForOfIteratorHelper(_fieldLines2);
1909
+ try {
1910
+ for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
1911
+ _line3 = _step11.value;
1912
+ _orderedLines5.push(_line3);
1913
+ }
1914
+ } catch (err) {
1915
+ _iterator11.e(err);
1916
+ } finally {
1917
+ _iterator11.f();
1918
+ }
1099
1919
  if (binaryFields && binaryFields.length > 0) {
1100
- // Create parts array for proper ordering
1101
- _parts2 = []; // Add headers and text fields
1102
- headerText = lines.join("\r\n") + "\r\n";
1103
- _parts2.push(Buffer.from(headerText));
1104
-
1105
- // Add binary fields with raw bytes
1106
- _iterator7 = _createForOfIteratorHelper(binaryFields);
1107
- try {
1108
- for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
1109
- _step7$value = _step7.value, _key2 = _step7$value.key, _buffer4 = _step7$value.buffer;
1110
- _parts2.push(Buffer.from("".concat(_key2, ": ")));
1111
- _parts2.push(_buffer4);
1112
- _parts2.push(Buffer.from("\r\n"));
1920
+ _parts = [];
1921
+ headerText = _orderedLines5.join("\r\n");
1922
+ _parts.push(Buffer.from(headerText));
1923
+ for (_i15 = 0; _i15 < binaryFields.length; _i15++) {
1924
+ _binaryFields$_i = binaryFields[_i15], _key = _binaryFields$_i.key, _buffer5 = _binaryFields$_i.buffer;
1925
+ if (_i15 > 0) {
1926
+ _parts.push(Buffer.from("\r\n"));
1113
1927
  }
1114
- } catch (err) {
1115
- _iterator7.e(err);
1116
- } finally {
1117
- _iterator7.f();
1928
+ _parts.push(Buffer.from("".concat(_key, ": ")));
1929
+ _parts.push(_buffer5);
1118
1930
  }
1119
- _fullBody = Buffer.concat(_parts2);
1931
+ _parts.push(Buffer.from("\r\n"));
1932
+ _fullBody = Buffer.concat(_parts);
1120
1933
  bodyParts.push(new Blob([_fullBody]));
1121
1934
  } else {
1122
- lines.push("");
1123
- bodyParts.push(new Blob([lines.join("\r\n")]));
1124
- }
1125
- }
1126
- _context5.next = 80;
1127
- break;
1128
- case 79:
1129
- if (Array.isArray(value)) {
1130
- // Array field - check if it's a mixed array or array of arrays
1131
- hasObjects = value.some(function (item) {
1132
- return isPojo(item);
1133
- });
1134
- hasArrays = value.some(function (item) {
1135
- return Array.isArray(item);
1136
- });
1137
- nonObjectItems = value.map(function (item, index) {
1138
- return {
1139
- item: item,
1140
- index: index + 1
1141
- };
1142
- }).filter(function (_ref7) {
1143
- var item = _ref7.item;
1144
- return !isPojo(item);
1145
- });
1146
- if (hasObjects && nonObjectItems.length > 0) {
1147
- // Mixed array - only include non-object items
1148
- _fieldLines = [];
1149
- partTypes = [];
1150
- _iterator8 = _createForOfIteratorHelper(nonObjectItems);
1151
- try {
1152
- for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
1153
- _step8$value = _step8.value, item = _step8$value.item, index = _step8$value.index;
1154
- if (typeof item === "number") {
1155
- if (Number.isInteger(item)) {
1156
- partTypes.push("".concat(index, "=\"integer\""));
1157
- _fieldLines.push("".concat(index, ": ").concat(item));
1158
- } else {
1159
- partTypes.push("".concat(index, "=\"float\""));
1160
- _fieldLines.push("".concat(index, ": ").concat(formatFloat(item)));
1161
- }
1162
- } else if (typeof item === "string") {
1163
- _fieldLines.push("".concat(index, ": ").concat(item));
1164
- } else if (item === null || item === undefined || _typeof(item) === "symbol" || typeof item === "boolean") {
1165
- partTypes.push("".concat(index, "=\"atom\""));
1166
- if (item === null) {
1167
- _fieldLines.push("".concat(index, ": \"null\""));
1168
- } else if (item === undefined) {
1169
- _fieldLines.push("".concat(index, ": \"undefined\""));
1170
- } else if (_typeof(item) === "symbol") {
1171
- _fieldLines.push("".concat(index, ": \"").concat(item.description || "Symbol.for()", "\""));
1172
- } else {
1173
- _fieldLines.push("".concat(index, ": \"").concat(item, "\""));
1174
- }
1175
- } else if (isBytes(item)) {
1176
- // Binary items in arrays need special handling
1177
- _buffer5 = toBuffer(item);
1178
- if (_buffer5.length === 0) {
1179
- partTypes.push("".concat(index, "=\"empty-binary\""));
1180
- }
1181
- // For now, skip binary items in mixed arrays
1182
- // They should be handled differently
1183
- partTypes.push("".concat(index, "=\"binary\""));
1184
- } else if (Array.isArray(item)) {
1185
- partTypes.push("".concat(index, "=\"list\""));
1186
- _encodedItems3 = item.map(function (subItem) {
1187
- return encodeArrayItem(subItem);
1188
- }).join(", ");
1189
- _fieldLines.push("".concat(index, ": ").concat(_encodedItems3));
1190
- }
1191
- }
1192
-
1193
- // For inline arrays, use different order
1194
- } catch (err) {
1195
- _iterator8.e(err);
1196
- } finally {
1197
- _iterator8.f();
1198
- }
1199
- if (isInline) {
1200
- console.log("[encode] Reordering for inline array:", {
1201
- bodyKey: bodyKey,
1202
- fieldLines: _fieldLines,
1203
- partTypes: partTypes
1204
- });
1205
-
1206
- // Rebuild in correct order: field lines, ao-types, content-disposition
1207
- _orderedLines = []; // First: field lines
1208
- _iterator9 = _createForOfIteratorHelper(_fieldLines);
1209
- try {
1210
- for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
1211
- _line2 = _step9.value;
1212
- _orderedLines.push(_line2);
1213
- }
1214
-
1215
- // Then: ao-types
1216
- } catch (err) {
1217
- _iterator9.e(err);
1218
- } finally {
1219
- _iterator9.f();
1220
- }
1221
- if (partTypes.length > 0) {
1222
- _orderedLines.push("ao-types: ".concat(partTypes.sort(function (a, b) {
1223
- var aNum = parseInt(a.split("=")[0]);
1224
- var bNum = parseInt(b.split("=")[0]);
1225
- return aNum - bNum;
1226
- }).join(", ")));
1227
- }
1228
-
1229
- // Finally: content-disposition (from lines[0])
1230
- _orderedLines.push(lines[0]);
1231
- _orderedLines.push("");
1232
- console.log("[encode] Ordered lines:", _orderedLines);
1233
- bodyParts.push(new Blob([_orderedLines.join("\r\n")]));
1234
- } else {
1235
- // Normal order for non-inline parts
1236
- if (partTypes.length > 0) {
1237
- lines.unshift("ao-types: ".concat(partTypes.sort(function (a, b) {
1238
- var aNum = parseInt(a.split("=")[0]);
1239
- var bNum = parseInt(b.split("=")[0]);
1240
- return aNum - bNum;
1241
- }).join(", ")));
1242
- }
1243
- _iterator10 = _createForOfIteratorHelper(_fieldLines);
1244
- try {
1245
- for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
1246
- _line3 = _step10.value;
1247
- lines.push(_line3);
1248
- }
1249
- } catch (err) {
1250
- _iterator10.e(err);
1251
- } finally {
1252
- _iterator10.f();
1253
- }
1254
- lines.push("");
1255
- bodyParts.push(new Blob([lines.join("\r\n")]));
1256
- }
1257
- } else if (hasArrays || !hasObjects && value.length > 0) {
1258
- // Array of arrays or simple array - use indexed format
1259
- _fieldLines2 = [];
1260
- _partTypes = [];
1261
- value.forEach(function (item, idx) {
1262
- var index = idx + 1;
1263
- if (Array.isArray(item)) {
1264
- if (item.length === 0) {
1265
- _partTypes.push("".concat(index, "=\"empty-list\""));
1266
- } else {
1267
- _partTypes.push("".concat(index, "=\"list\""));
1268
- var _encodedItems4 = item.map(function (subItem) {
1269
- return encodeArrayItem(subItem);
1270
- }).join(", ");
1271
- _fieldLines2.push("".concat(index, ": ").concat(_encodedItems4));
1272
- }
1273
- } else if (typeof item === "number") {
1274
- if (Number.isInteger(item)) {
1275
- _partTypes.push("".concat(index, "=\"integer\""));
1276
- _fieldLines2.push("".concat(index, ": ").concat(item));
1277
- } else {
1278
- _partTypes.push("".concat(index, "=\"float\""));
1279
- _fieldLines2.push("".concat(index, ": ").concat(formatFloat(item)));
1280
- }
1281
- } else if (typeof item === "string") {
1282
- if (item.length === 0) {
1283
- _partTypes.push("".concat(index, "=\"empty-binary\""));
1284
- }
1285
- _fieldLines2.push("".concat(index, ": ").concat(item));
1286
- } else if (item === null || item === undefined || _typeof(item) === "symbol" || typeof item === "boolean") {
1287
- _partTypes.push("".concat(index, "=\"atom\""));
1288
- if (item === null) {
1289
- _fieldLines2.push("".concat(index, ": \"null\""));
1290
- } else if (item === undefined) {
1291
- _fieldLines2.push("".concat(index, ": \"undefined\""));
1292
- } else if (_typeof(item) === "symbol") {
1293
- _fieldLines2.push("".concat(index, ": \"").concat(item.description || "Symbol.for()", "\""));
1294
- } else {
1295
- _fieldLines2.push("".concat(index, ": \"").concat(item, "\""));
1296
- }
1297
- } else if (isBytes(item)) {
1298
- var _buffer6 = toBuffer(item);
1299
- if (_buffer6.length === 0) {
1300
- _partTypes.push("".concat(index, "=\"empty-binary\""));
1301
- } else {
1302
- _partTypes.push("".concat(index, "=\"binary\""));
1303
- }
1304
- // For indexed format, we also can't include raw bytes inline
1305
- // This is a limitation of the format
1306
- }
1307
- });
1308
-
1309
- // For inline arrays, use different order
1310
- if (isInline) {
1311
- console.log("[encode] Reordering for inline array - indexed format");
1312
- _orderedLines2 = []; // First: field lines
1313
- _iterator11 = _createForOfIteratorHelper(_fieldLines2);
1314
- try {
1315
- for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
1316
- _line4 = _step11.value;
1317
- _orderedLines2.push(_line4);
1318
- }
1319
-
1320
- // Then: ao-types
1321
- } catch (err) {
1322
- _iterator11.e(err);
1323
- } finally {
1324
- _iterator11.f();
1325
- }
1326
- if (_partTypes.length > 0) {
1327
- _orderedLines2.push("ao-types: ".concat(_partTypes.sort(function (a, b) {
1328
- var aNum = parseInt(a.split("=")[0]);
1329
- var bNum = parseInt(b.split("=")[0]);
1330
- return aNum - bNum;
1331
- }).join(", ")));
1332
- }
1333
-
1334
- // Finally: content-disposition
1335
- _orderedLines2.push(lines[0]);
1336
- _orderedLines2.push("");
1337
- console.log("[encode] Final ordered lines:", _orderedLines2);
1338
- bodyParts.push(new Blob([_orderedLines2.join("\r\n")]));
1935
+ // Add trailing blank line if we have field lines
1936
+ if (_fieldLines2.length > 0) {
1937
+ bodyParts.push(new Blob([_orderedLines5.join("\r\n") + "\r\n"]));
1339
1938
  } else {
1340
- // Normal order for non-inline parts
1341
- if (_partTypes.length > 0) {
1342
- console.log("[encode] Adding ao-types to beginning of lines array");
1343
- lines.unshift("ao-types: ".concat(_partTypes.sort(function (a, b) {
1344
- var aNum = parseInt(a.split("=")[0]);
1345
- var bNum = parseInt(b.split("=")[0]);
1346
- return aNum - bNum;
1347
- }).join(", ")));
1348
- }
1349
- console.log("[encode] Adding field lines:", _fieldLines2);
1350
- _iterator12 = _createForOfIteratorHelper(_fieldLines2);
1351
- try {
1352
- for (_iterator12.s(); !(_step12 = _iterator12.n()).done;) {
1353
- _line5 = _step12.value;
1354
- lines.push(_line5);
1355
- }
1356
- } catch (err) {
1357
- _iterator12.e(err);
1358
- } finally {
1359
- _iterator12.f();
1360
- }
1361
- console.log("[encode] Final lines before blob:", lines);
1362
- lines.push("");
1363
- bodyParts.push(new Blob([lines.join("\r\n")]));
1939
+ bodyParts.push(new Blob([_orderedLines5.join("\r\n")]));
1364
1940
  }
1365
- } else if (!hasObjects && value.length === 0) {
1366
- // Empty array
1367
- _fieldName = pathParts[pathParts.length - 1];
1368
- _partTypes2 = ["".concat(_fieldName, "=\"empty-list\"")];
1369
- lines.unshift("ao-types: ".concat(_partTypes2.join(", ")));
1370
- lines.push("");
1371
- bodyParts.push(new Blob([lines.join("\r\n")]));
1372
1941
  }
1942
+ }
1943
+ return _context5.abrupt("return", 0);
1944
+ case 126:
1945
+ isInline = bodyKey === "body" && headers["inline-body-key"] === "body";
1946
+ if (isInline) {
1947
+ lines.push("content-disposition: inline");
1948
+ } else {
1949
+ lines.push("content-disposition: form-data;name=\"".concat(bodyKey, "\""));
1950
+ }
1951
+ if (isBytes(value)) {
1952
+ _buffer6 = toBuffer(value);
1953
+ _headerText = lines.join("\r\n") + "\r\n\r\n";
1954
+ bodyParts.push(new Blob([_headerText, _buffer6]));
1373
1955
  } else if (typeof value === "string") {
1374
- // String with newlines or too long
1375
1956
  lines.push("");
1376
1957
  lines.push(value);
1958
+ bodyParts.push(new Blob([lines.join("\r\n")]));
1959
+ } else if (typeof value === "boolean" || typeof value === "number" || value === null || value === undefined || _typeof(value) === "symbol") {
1960
+ if (typeof value === "boolean") {
1961
+ content = "\"".concat(value, "\"");
1962
+ } else if (typeof value === "number") {
1963
+ content = String(value);
1964
+ } else if (value === null) {
1965
+ content = '"null"';
1966
+ } else if (value === undefined) {
1967
+ content = '"undefined"';
1968
+ } else if (_typeof(value) === "symbol") {
1969
+ content = "\"".concat(value.description || "Symbol.for()", "\"");
1970
+ }
1377
1971
  lines.push("");
1972
+ lines.push(content);
1378
1973
  bodyParts.push(new Blob([lines.join("\r\n")]));
1379
1974
  }
1380
- case 80:
1975
+ case 129:
1381
1976
  case "end":
1382
1977
  return _context5.stop();
1383
1978
  }
1384
1979
  }, _loop5);
1385
1980
  });
1386
- _iterator3.s();
1387
- case 85:
1388
- if ((_step3 = _iterator3.n()).done) {
1389
- _context6.next = 92;
1981
+ _iterator5.s();
1982
+ case 126:
1983
+ if ((_step5 = _iterator5.n()).done) {
1984
+ _context6.next = 133;
1390
1985
  break;
1391
1986
  }
1392
- return _context6.delegateYield(_loop5(), "t2", 87);
1393
- case 87:
1987
+ return _context6.delegateYield(_loop5(), "t2", 128);
1988
+ case 128:
1394
1989
  _ret = _context6.t2;
1395
1990
  if (!(_ret === 0)) {
1396
- _context6.next = 90;
1991
+ _context6.next = 131;
1397
1992
  break;
1398
1993
  }
1399
- return _context6.abrupt("continue", 90);
1400
- case 90:
1401
- _context6.next = 85;
1994
+ return _context6.abrupt("continue", 131);
1995
+ case 131:
1996
+ _context6.next = 126;
1402
1997
  break;
1403
- case 92:
1404
- _context6.next = 97;
1998
+ case 133:
1999
+ _context6.next = 138;
1405
2000
  break;
1406
- case 94:
1407
- _context6.prev = 94;
1408
- _context6.t3 = _context6["catch"](82);
1409
- _iterator3.e(_context6.t3);
1410
- case 97:
1411
- _context6.prev = 97;
1412
- _iterator3.f();
1413
- return _context6.finish(97);
1414
- case 100:
1415
- // Special case: add data/body as a separate form-data part if needed
2001
+ case 135:
2002
+ _context6.prev = 135;
2003
+ _context6.t3 = _context6["catch"](123);
2004
+ _iterator5.e(_context6.t3);
2005
+ case 138:
2006
+ _context6.prev = 138;
2007
+ _iterator5.f();
2008
+ return _context6.finish(138);
2009
+ case 141:
2010
+ // Add the special data/body part if needed
1416
2011
  if (hasSpecialDataBody && obj.data && obj.data.body && isBytes(obj.data.body)) {
1417
2012
  buffer = toBuffer(obj.data.body);
1418
2013
  specialPart = ["content-disposition: form-data;name=\"data/body\"", "", ""].join("\r\n");
1419
2014
  bodyParts.push(new Blob([specialPart, buffer]));
1420
2015
  }
1421
-
1422
- // Calculate boundary from content
1423
- _context6.next = 103;
2016
+ _context6.next = 144;
1424
2017
  return Promise.all(bodyParts.map(function (part) {
1425
2018
  return part.text();
1426
2019
  }));
1427
- case 103:
2020
+ case 144:
1428
2021
  partsContent = _context6.sent;
1429
2022
  allContent = partsContent.join("");
1430
- _context6.next = 107;
2023
+ _context6.next = 148;
1431
2024
  return sha256(new TextEncoder().encode(allContent));
1432
- case 107:
2025
+ case 148:
1433
2026
  boundaryHash = _context6.sent;
1434
- boundary = _base64url["default"].encode(Buffer.from(boundaryHash)); // Assemble final multipart body - NO newlines after each part except the last
2027
+ boundary = _base64url["default"].encode(Buffer.from(boundaryHash));
1435
2028
  finalParts = [];
1436
2029
  for (i = 0; i < bodyParts.length; i++) {
1437
2030
  if (i === 0) {
@@ -1443,60 +2036,33 @@ function _encode() {
1443
2036
  }
1444
2037
  finalParts.push(new Blob(["\r\n--".concat(boundary, "--")]));
1445
2038
  headers["content-type"] = "multipart/form-data; boundary=\"".concat(boundary, "\"");
1446
- body = new Blob(finalParts); // Calculate content digest
1447
- _context6.next = 116;
2039
+ body = new Blob(finalParts);
2040
+ _context6.next = 157;
1448
2041
  return body.arrayBuffer();
1449
- case 116:
2042
+ case 157:
1450
2043
  finalContent = _context6.sent;
1451
- _context6.next = 119;
2044
+ if (!(finalContent.byteLength > 0)) {
2045
+ _context6.next = 164;
2046
+ break;
2047
+ }
2048
+ _context6.next = 161;
1452
2049
  return sha256(finalContent);
1453
- case 119:
1454
- contentDigest = _context6.sent;
1455
- base64 = _base64url["default"].toBase64(_base64url["default"].encode(contentDigest));
1456
- headers["content-digest"] = "sha-256=:".concat(base64, ":");
2050
+ case 161:
2051
+ _contentDigest5 = _context6.sent;
2052
+ _base5 = _base64url["default"].toBase64(_base64url["default"].encode(_contentDigest5));
2053
+ headers["content-digest"] = "sha-256=:".concat(_base5, ":");
2054
+ case 164:
1457
2055
  headers["content-length"] = String(finalContent.byteLength);
1458
- console.log("[encode] FINAL - headers:", headers, "body:", body);
1459
-
1460
- // Debug: decode the multipart body to verify structure
1461
- _context6.next = 126;
1462
- return body.text();
1463
- case 126:
1464
- bodyText = _context6.sent;
1465
- console.log("\n[encode] DEBUG - Full body text:");
1466
- console.log(bodyText);
1467
-
1468
- // Parse multipart body
1469
- boundaryMatch = headers["content-type"].match(/boundary="([^"]+)"/);
1470
- if (boundaryMatch) {
1471
- debugBoundary = boundaryMatch[1];
1472
- parts = bodyText.split("--".concat(debugBoundary));
1473
- console.log("\n[encode] DEBUG - Multipart parts:");
1474
- parts.forEach(function (part, idx) {
1475
- console.log("Part ".concat(idx, ":"), JSON.stringify(part));
1476
- });
1477
-
1478
- // Show the actual content part
1479
- if (parts[1]) {
1480
- console.log("\n[encode] DEBUG - Content part structure:");
1481
- lines = parts[1].trim().split("\r\n");
1482
- lines.forEach(function (line, idx) {
1483
- if (line.includes("\0")) {
1484
- console.log("Line ".concat(idx, ": \"").concat(line.substring(0, line.indexOf("\0")), "\" + [").concat(line.length - line.indexOf("\0"), " bytes]"));
1485
- } else {
1486
- console.log("Line ".concat(idx, ": \"").concat(line, "\""));
1487
- }
1488
- });
1489
- }
1490
- }
2056
+ console.log("=== ENCODE END ===\n");
1491
2057
  return _context6.abrupt("return", {
1492
2058
  headers: headers,
1493
2059
  body: body
1494
2060
  });
1495
- case 132:
2061
+ case 167:
1496
2062
  case "end":
1497
2063
  return _context6.stop();
1498
2064
  }
1499
- }, _callee3, null, [[82, 94, 97, 100]]);
2065
+ }, _callee3, null, [[123, 135, 138, 141]]);
1500
2066
  }));
1501
2067
  return _encode.apply(this, arguments);
1502
2068
  }