@basmilius/apple-encoding 0.6.5 → 0.6.11

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.
Files changed (2) hide show
  1. package/dist/index.js +261 -146
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -391,6 +391,8 @@ var TAG = {
391
391
  DICT_VARIABLE: 239,
392
392
  DICT_TERMINATOR: 129
393
393
  };
394
+ var textDecoder = new TextDecoder;
395
+ var textEncoder = new TextEncoder;
394
396
 
395
397
  class Float {
396
398
  get value() {
@@ -439,22 +441,12 @@ function sizedInteger(value, size) {
439
441
  return new SizedInteger(value, size);
440
442
  }
441
443
  function decode3(data) {
442
- const [value] = _unpack(data, []);
443
- return value;
444
+ const result = _unpackAt(data, 0, []);
445
+ return result.value;
444
446
  }
445
447
  function encode2(data) {
446
448
  return _pack(data, []);
447
449
  }
448
- function concat(arrays) {
449
- const total = arrays.reduce((sum, a) => sum + a.length, 0);
450
- const out = new Uint8Array(total);
451
- let offset = 0;
452
- for (const a of arrays) {
453
- out.set(a, offset);
454
- offset += a.length;
455
- }
456
- return out;
457
- }
458
450
  function u8(b) {
459
451
  return Uint8Array.of(b);
460
452
  }
@@ -467,18 +459,20 @@ function uintToLEBytes(value, byteLen) {
467
459
  }
468
460
  return out;
469
461
  }
470
- function ensureAvailable(buf, need) {
471
- if (buf.length < need) {
472
- throw new TypeError(`Not enough data: need ${need} bytes, have ${buf.length}`);
473
- }
474
- }
475
462
  function readLittleEndian(buf, offset, len) {
476
- ensureAvailable(buf.subarray(offset), len);
477
- let v = 0n;
478
- for (let i = len - 1;i >= 0; i--) {
479
- v = v << 8n | BigInt(buf[offset + i]);
463
+ if (len === 1) {
464
+ return buf[offset];
465
+ } else if (len === 2) {
466
+ return buf[offset] | buf[offset + 1] << 8;
467
+ } else if (len === 4) {
468
+ return buf[offset] | buf[offset + 1] << 8 | buf[offset + 2] << 16 | buf[offset + 3] << 24 >>> 0;
469
+ } else {
470
+ let v = 0n;
471
+ for (let i = len - 1;i >= 0; i--) {
472
+ v = v << 8n | BigInt(buf[offset + i]);
473
+ }
474
+ return Number(v);
480
475
  }
481
- return Number(v);
482
476
  }
483
477
  function _pack(data, objectList) {
484
478
  let packed = null;
@@ -487,96 +481,187 @@ function _pack(data, objectList) {
487
481
  } else if (typeof data === "boolean") {
488
482
  packed = u8(data ? TAG.TRUE : TAG.FALSE);
489
483
  } else if (data instanceof Float) {
490
- const buf = new ArrayBuffer(8);
491
- new DataView(buf).setFloat64(0, data.value, true);
492
- packed = concat([u8(TAG.FLOAT64), new Uint8Array(buf)]);
484
+ packed = new Uint8Array(9);
485
+ packed[0] = TAG.FLOAT64;
486
+ new DataView(packed.buffer, packed.byteOffset + 1, 8).setFloat64(0, data.value, true);
493
487
  } else if (data instanceof Integer) {
494
488
  const val = data.value;
495
489
  if (val <= TAG.INT_INLINE_MAX_VALUE) {
496
490
  packed = u8(TAG.INT_BASE + val);
497
491
  } else if (val <= 255) {
498
- packed = concat([u8(TAG.INT_1BYTE), uintToLEBytes(val, 1)]);
492
+ packed = new Uint8Array(2);
493
+ packed[0] = TAG.INT_1BYTE;
494
+ packed[1] = val;
499
495
  } else if (val <= 65535) {
500
- packed = concat([u8(TAG.INT_2BYTE), uintToLEBytes(val, 2)]);
496
+ packed = new Uint8Array(3);
497
+ packed[0] = TAG.INT_2BYTE;
498
+ packed[1] = val & 255;
499
+ packed[2] = val >> 8 & 255;
501
500
  } else if (val <= 4294967295) {
502
- packed = concat([u8(TAG.INT_4BYTE), uintToLEBytes(val, 4)]);
501
+ packed = new Uint8Array(5);
502
+ packed[0] = TAG.INT_4BYTE;
503
+ packed[1] = val & 255;
504
+ packed[2] = val >> 8 & 255;
505
+ packed[3] = val >> 16 & 255;
506
+ packed[4] = val >> 24 & 255;
503
507
  } else {
504
- packed = concat([u8(TAG.INT_8BYTE), uintToLEBytes(val, 8)]);
508
+ packed = new Uint8Array(9);
509
+ packed[0] = TAG.INT_8BYTE;
510
+ packed.set(uintToLEBytes(val, 8), 1);
505
511
  }
506
512
  } else if (typeof data === "number") {
507
513
  if (!Number.isInteger(data)) {
508
- const buf = new ArrayBuffer(8);
509
- new DataView(buf).setFloat64(0, data, true);
510
- packed = concat([u8(TAG.FLOAT64), new Uint8Array(buf)]);
514
+ packed = new Uint8Array(9);
515
+ packed[0] = TAG.FLOAT64;
516
+ new DataView(packed.buffer, packed.byteOffset + 1, 8).setFloat64(0, data, true);
511
517
  } else {
512
518
  if (data <= TAG.INT_INLINE_MAX_VALUE) {
513
519
  packed = u8(TAG.INT_BASE + data);
514
520
  } else if (data <= 255) {
515
- packed = concat([u8(TAG.INT_1BYTE), uintToLEBytes(data, 1)]);
521
+ packed = new Uint8Array(2);
522
+ packed[0] = TAG.INT_1BYTE;
523
+ packed[1] = data;
516
524
  } else if (data <= 65535) {
517
- packed = concat([u8(TAG.INT_2BYTE), uintToLEBytes(data, 2)]);
525
+ packed = new Uint8Array(3);
526
+ packed[0] = TAG.INT_2BYTE;
527
+ packed[1] = data & 255;
528
+ packed[2] = data >> 8 & 255;
518
529
  } else if (data <= 4294967295) {
519
- packed = concat([u8(TAG.INT_4BYTE), uintToLEBytes(data, 4)]);
530
+ packed = new Uint8Array(5);
531
+ packed[0] = TAG.INT_4BYTE;
532
+ packed[1] = data & 255;
533
+ packed[2] = data >> 8 & 255;
534
+ packed[3] = data >> 16 & 255;
535
+ packed[4] = data >> 24 & 255;
520
536
  } else {
521
- packed = concat([u8(TAG.INT_8BYTE), uintToLEBytes(data, 8)]);
537
+ packed = new Uint8Array(9);
538
+ packed[0] = TAG.INT_8BYTE;
539
+ packed.set(uintToLEBytes(data, 8), 1);
522
540
  }
523
541
  }
524
542
  } else if (data instanceof SizedInteger) {
525
- packed = concat([u8(TAG.INT_1BYTE + Math.log2(data.size)), uintToLEBytes(data.valueOf(), data.size)]);
543
+ const byteSize = data.size;
544
+ packed = new Uint8Array(byteSize + 1);
545
+ packed[0] = TAG.INT_1BYTE + (31 - Math.clz32(byteSize));
546
+ if (byteSize === 1) {
547
+ packed[1] = data.valueOf();
548
+ } else if (byteSize === 2) {
549
+ const val = data.valueOf();
550
+ packed[1] = val & 255;
551
+ packed[2] = val >> 8 & 255;
552
+ } else if (byteSize === 4) {
553
+ const val = data.valueOf();
554
+ packed[1] = val & 255;
555
+ packed[2] = val >> 8 & 255;
556
+ packed[3] = val >> 16 & 255;
557
+ packed[4] = val >> 24 & 255;
558
+ } else {
559
+ packed.set(uintToLEBytes(data.valueOf(), 8), 1);
560
+ }
526
561
  } else if (typeof data === "string") {
527
- const b = new TextEncoder().encode(data);
562
+ const b = textEncoder.encode(data);
528
563
  const len = b.length;
529
564
  if (len <= 32) {
530
- packed = concat([u8(TAG.STR_BASE + len), b]);
565
+ packed = new Uint8Array(1 + len);
566
+ packed[0] = TAG.STR_BASE + len;
567
+ packed.set(b, 1);
531
568
  } else if (len <= 255) {
532
- packed = concat([u8(TAG.STR_1BYTE_LEN), uintToLEBytes(len, 1), b]);
569
+ packed = new Uint8Array(2 + len);
570
+ packed[0] = TAG.STR_1BYTE_LEN;
571
+ packed[1] = len;
572
+ packed.set(b, 2);
533
573
  } else if (len <= 65535) {
534
- packed = concat([u8(TAG.STR_2BYTE_LEN), uintToLEBytes(len, 2), b]);
574
+ packed = new Uint8Array(3 + len);
575
+ packed[0] = TAG.STR_2BYTE_LEN;
576
+ packed[1] = len & 255;
577
+ packed[2] = len >> 8 & 255;
578
+ packed.set(b, 3);
535
579
  } else if (len <= 16777215) {
536
- packed = concat([u8(TAG.STR_3BYTE_LEN), uintToLEBytes(len, 3), b]);
580
+ packed = new Uint8Array(4 + len);
581
+ packed[0] = TAG.STR_3BYTE_LEN;
582
+ packed[1] = len & 255;
583
+ packed[2] = len >> 8 & 255;
584
+ packed[3] = len >> 16 & 255;
585
+ packed.set(b, 4);
537
586
  } else {
538
- packed = concat([u8(TAG.STR_4BYTE_LEN), uintToLEBytes(len, 4), b]);
587
+ packed = new Uint8Array(5 + len);
588
+ packed[0] = TAG.STR_4BYTE_LEN;
589
+ packed[1] = len & 255;
590
+ packed[2] = len >> 8 & 255;
591
+ packed[3] = len >> 16 & 255;
592
+ packed[4] = len >> 24 & 255;
593
+ packed.set(b, 5);
539
594
  }
540
595
  } else if (data instanceof Uint8Array || Buffer.isBuffer(data)) {
541
596
  const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
542
597
  const len = bytes.length;
543
598
  if (len <= 32) {
544
- packed = concat([u8(TAG.BYTES_BASE + len), bytes]);
599
+ packed = new Uint8Array(1 + len);
600
+ packed[0] = TAG.BYTES_BASE + len;
601
+ packed.set(bytes, 1);
545
602
  } else if (len <= 255) {
546
- packed = concat([u8(TAG.BYTES_1BYTE_LEN), uintToLEBytes(len, 1), bytes]);
603
+ packed = new Uint8Array(2 + len);
604
+ packed[0] = TAG.BYTES_1BYTE_LEN;
605
+ packed[1] = len;
606
+ packed.set(bytes, 2);
547
607
  } else if (len <= 65535) {
548
- packed = concat([u8(TAG.BYTES_2BYTE_LEN), uintToLEBytes(len, 2), bytes]);
608
+ packed = new Uint8Array(3 + len);
609
+ packed[0] = TAG.BYTES_2BYTE_LEN;
610
+ packed[1] = len & 255;
611
+ packed[2] = len >> 8 & 255;
612
+ packed.set(bytes, 3);
549
613
  } else {
550
- packed = concat([u8(TAG.BYTES_4BYTE_LEN), uintToLEBytes(len, 4), bytes]);
614
+ packed = new Uint8Array(5 + len);
615
+ packed[0] = TAG.BYTES_4BYTE_LEN;
616
+ packed[1] = len & 255;
617
+ packed[2] = len >> 8 & 255;
618
+ packed[3] = len >> 16 & 255;
619
+ packed[4] = len >> 24 & 255;
620
+ packed.set(bytes, 5);
551
621
  }
552
622
  } else if (Array.isArray(data)) {
553
- const body = concat(data.map((d) => _pack(d, objectList)));
623
+ const parts2 = data.map((d) => _pack(d, objectList));
624
+ const bodyLen = parts2.reduce((sum, p) => sum + p.length, 0);
554
625
  const len = data.length;
555
- if (len <= 15) {
556
- packed = concat([u8(TAG.ARRAY_BASE + len), body]);
557
- if (len >= 15) {
558
- packed = concat([packed, u8(TAG.TERMINATOR)]);
626
+ if (len < 15) {
627
+ packed = new Uint8Array(1 + bodyLen);
628
+ packed[0] = TAG.ARRAY_BASE + len;
629
+ let pos = 1;
630
+ for (const part of parts2) {
631
+ packed.set(part, pos);
632
+ pos += part.length;
559
633
  }
560
634
  } else {
561
- packed = concat([u8(TAG.ARRAY_VARIABLE), body, u8(TAG.TERMINATOR)]);
635
+ packed = new Uint8Array(2 + bodyLen);
636
+ packed[0] = TAG.ARRAY_VARIABLE;
637
+ let pos = 1;
638
+ for (const part of parts2) {
639
+ packed.set(part, pos);
640
+ pos += part.length;
641
+ }
642
+ packed[pos] = TAG.TERMINATOR;
562
643
  }
563
644
  } else if (typeof data === "object") {
564
645
  const keys = Object.keys(data);
565
646
  const len = keys.length;
566
- const pairs = [];
647
+ const parts2 = [];
648
+ let bodyLen = 0;
567
649
  for (const k of keys) {
568
- pairs.push(_pack(k, objectList));
569
- pairs.push(_pack(data[k], objectList));
650
+ const keyPacked = _pack(k, objectList);
651
+ const valPacked = _pack(data[k], objectList);
652
+ parts2.push(keyPacked, valPacked);
653
+ bodyLen += keyPacked.length + valPacked.length;
570
654
  }
571
- let header;
572
- if (len <= 15) {
573
- header = u8(TAG.DICT_BASE + len);
574
- } else {
575
- header = u8(TAG.DICT_VARIABLE);
655
+ const needsTerminator = len >= 15;
656
+ packed = new Uint8Array(1 + bodyLen + (needsTerminator ? 1 : 0));
657
+ packed[0] = len <= 15 ? TAG.DICT_BASE + len : TAG.DICT_VARIABLE;
658
+ let pos = 1;
659
+ for (const part of parts2) {
660
+ packed.set(part, pos);
661
+ pos += part.length;
576
662
  }
577
- packed = concat([header, concat(pairs)]);
578
- if (len >= 15 || objectList.some((v) => v === packed)) {
579
- packed = concat([packed, u8(TAG.DICT_TERMINATOR)]);
663
+ if (needsTerminator) {
664
+ packed[pos] = TAG.DICT_TERMINATOR;
580
665
  }
581
666
  } else {
582
667
  throw new TypeError(typeof data + "");
@@ -586,119 +671,134 @@ function _pack(data, objectList) {
586
671
  if (idx < 33) {
587
672
  packed = u8(TAG.REF_BASE + idx);
588
673
  } else if (idx <= 255) {
589
- packed = concat([u8(TAG.REF_1BYTE), uintToLEBytes(idx, 1)]);
674
+ packed = new Uint8Array(2);
675
+ packed[0] = TAG.REF_1BYTE;
676
+ packed[1] = idx;
590
677
  } else if (idx <= 65535) {
591
- packed = concat([u8(TAG.REF_2BYTE), uintToLEBytes(idx, 2)]);
678
+ packed = new Uint8Array(3);
679
+ packed[0] = TAG.REF_2BYTE;
680
+ packed[1] = idx & 255;
681
+ packed[2] = idx >> 8 & 255;
592
682
  } else if (idx <= 4294967295) {
593
- packed = concat([u8(TAG.REF_4BYTE), uintToLEBytes(idx, 4)]);
683
+ packed = new Uint8Array(5);
684
+ packed[0] = TAG.REF_4BYTE;
685
+ packed[1] = idx & 255;
686
+ packed[2] = idx >> 8 & 255;
687
+ packed[3] = idx >> 16 & 255;
688
+ packed[4] = idx >> 24 & 255;
594
689
  } else {
595
- packed = concat([u8(TAG.REF_8BYTE), uintToLEBytes(idx, 8)]);
690
+ packed = new Uint8Array(9);
691
+ packed[0] = TAG.REF_8BYTE;
692
+ packed.set(uintToLEBytes(idx, 8), 1);
596
693
  }
597
694
  } else if (packed.length > 1) {
598
695
  objectList.push(packed);
599
696
  }
600
697
  return packed;
601
698
  }
602
- function _unpack(data, objectList) {
603
- if (data.length === 0)
699
+ function _unpackAt(data, offset, objectList) {
700
+ if (offset >= data.length)
604
701
  throw new TypeError("No data to unpack");
605
- const tag = data[0];
702
+ const tag = data[offset];
606
703
  let addToObjectList = true;
607
704
  let value;
608
- let rest;
705
+ let newOffset;
609
706
  if (tag === TAG.TRUE) {
610
707
  value = true;
611
- rest = data.subarray(1);
708
+ newOffset = offset + 1;
612
709
  } else if (tag === TAG.FALSE) {
613
710
  value = false;
614
- rest = data.subarray(1);
711
+ newOffset = offset + 1;
615
712
  } else if (tag === TAG.NULL) {
616
713
  value = null;
617
- rest = data.subarray(1);
714
+ newOffset = offset + 1;
618
715
  } else if (tag === TAG.UUID) {
619
- value = data.subarray(1, 17);
620
- rest = data.subarray(17);
716
+ value = data.subarray(offset + 1, offset + 17);
717
+ newOffset = offset + 17;
621
718
  } else if (tag === TAG.TIMESTAMP) {
622
- value = readLittleEndian(data, 1, 8);
623
- rest = data.subarray(9);
719
+ value = readLittleEndian(data, offset + 1, 8);
720
+ newOffset = offset + 9;
624
721
  } else if (tag >= TAG.INT_BASE && tag <= TAG.INT_MAX_INLINE) {
625
722
  value = tag - TAG.INT_BASE;
626
- rest = data.subarray(1);
723
+ newOffset = offset + 1;
627
724
  } else if (tag === TAG.FLOAT32) {
628
- const view = new DataView(data.buffer, data.byteOffset + 1, 4);
725
+ const view = new DataView(data.buffer, data.byteOffset + offset + 1, 4);
629
726
  value = view.getFloat32(0, true);
630
- rest = data.subarray(5);
727
+ newOffset = offset + 5;
631
728
  } else if (tag === TAG.FLOAT64) {
632
- const view = new DataView(data.buffer, data.byteOffset + 1, 8);
729
+ const view = new DataView(data.buffer, data.byteOffset + offset + 1, 8);
633
730
  value = view.getFloat64(0, true);
634
- rest = data.subarray(9);
731
+ newOffset = offset + 9;
635
732
  } else if ((tag & 240) === TAG.INT_1BYTE) {
636
- const noOfBytes = 2 ** (tag & 15);
637
- const val = readLittleEndian(data, 1, noOfBytes);
733
+ const noOfBytes = 1 << (tag & 15);
734
+ const val = readLittleEndian(data, offset + 1, noOfBytes);
638
735
  value = sizedInteger(val, noOfBytes);
639
- rest = data.subarray(1 + noOfBytes);
736
+ newOffset = offset + 1 + noOfBytes;
640
737
  } else if (tag >= TAG.STR_BASE && tag <= TAG.STR_MAX_INLINE) {
641
738
  const length = tag - TAG.STR_BASE;
642
- value = new TextDecoder().decode(data.subarray(1, 1 + length));
643
- rest = data.subarray(1 + length);
739
+ value = textDecoder.decode(data.subarray(offset + 1, offset + 1 + length));
740
+ newOffset = offset + 1 + length;
644
741
  } else if (tag >= TAG.STR_1BYTE_LEN && tag <= TAG.STR_4BYTE_LEN) {
645
742
  const lenBytes = tag & 15;
646
- const length = readLittleEndian(data, 1, lenBytes);
647
- value = new TextDecoder().decode(data.subarray(1 + lenBytes, 1 + lenBytes + length));
648
- rest = data.subarray(1 + lenBytes + length);
743
+ const length = readLittleEndian(data, offset + 1, lenBytes);
744
+ const start = offset + 1 + lenBytes;
745
+ value = textDecoder.decode(data.subarray(start, start + length));
746
+ newOffset = start + length;
649
747
  } else if (tag >= TAG.BYTES_BASE && tag <= TAG.BYTES_MAX_INLINE) {
650
748
  const length = tag - TAG.BYTES_BASE;
651
- value = data.subarray(1, 1 + length);
652
- rest = data.subarray(1 + length);
749
+ value = data.subarray(offset + 1, offset + 1 + length);
750
+ newOffset = offset + 1 + length;
653
751
  } else if (tag >= TAG.BYTES_1BYTE_LEN && tag <= TAG.BYTES_4BYTE_LEN) {
654
752
  const noOfBytes = 1 << (tag & 15) - 1;
655
- const length = readLittleEndian(data, 1, noOfBytes);
656
- const start = 1 + noOfBytes;
753
+ const length = readLittleEndian(data, offset + 1, noOfBytes);
754
+ const start = offset + 1 + noOfBytes;
657
755
  value = data.subarray(start, start + length);
658
- rest = data.subarray(start + length);
756
+ newOffset = start + length;
659
757
  } else if ((tag & 240) === TAG.ARRAY_BASE) {
660
758
  const count = tag & 15;
661
- let ptr = data.subarray(1);
662
- const arr = [];
759
+ let pos = offset + 1;
663
760
  if (count === 15) {
664
- while (ptr[0] !== TAG.TERMINATOR) {
665
- const [v, r] = _unpack(ptr, objectList);
666
- arr.push(v);
667
- ptr = r;
761
+ const arr = [];
762
+ while (data[pos] !== TAG.TERMINATOR) {
763
+ const result = _unpackAt(data, pos, objectList);
764
+ arr.push(result.value);
765
+ pos = result.offset;
668
766
  }
669
- ptr = ptr.subarray(1);
767
+ pos++;
768
+ value = arr;
670
769
  } else {
770
+ const arr = new Array(count);
671
771
  for (let i = 0;i < count; i++) {
672
- const [v, r] = _unpack(ptr, objectList);
673
- arr.push(v);
674
- ptr = r;
772
+ const result = _unpackAt(data, pos, objectList);
773
+ arr[i] = result.value;
774
+ pos = result.offset;
675
775
  }
776
+ value = arr;
676
777
  }
677
- value = arr;
678
- rest = ptr;
778
+ newOffset = pos;
679
779
  addToObjectList = false;
680
780
  } else if ((tag & 240) === TAG.DICT_BASE) {
681
781
  const count = tag & 15;
682
782
  const obj = {};
683
- let ptr = data.subarray(1);
783
+ let pos = offset + 1;
684
784
  if (count === 15) {
685
- while (ptr[0] !== TAG.TERMINATOR) {
686
- const [k, r1] = _unpack(ptr, objectList);
687
- const [v, r2] = _unpack(r1, objectList);
688
- obj[k] = v;
689
- ptr = r2;
785
+ while (data[pos] !== TAG.TERMINATOR) {
786
+ const keyResult = _unpackAt(data, pos, objectList);
787
+ const valResult = _unpackAt(data, keyResult.offset, objectList);
788
+ obj[keyResult.value] = valResult.value;
789
+ pos = valResult.offset;
690
790
  }
691
- ptr = ptr.subarray(1);
791
+ pos++;
692
792
  } else {
693
793
  for (let i = 0;i < count; i++) {
694
- const [k, r1] = _unpack(ptr, objectList);
695
- const [v, r2] = _unpack(r1, objectList);
696
- obj[k] = v;
697
- ptr = r2;
794
+ const keyResult = _unpackAt(data, pos, objectList);
795
+ const valResult = _unpackAt(data, keyResult.offset, objectList);
796
+ obj[keyResult.value] = valResult.value;
797
+ pos = valResult.offset;
698
798
  }
699
799
  }
700
800
  value = obj;
701
- rest = ptr;
801
+ newOffset = pos;
702
802
  addToObjectList = false;
703
803
  } else if (tag >= TAG.REF_BASE && tag <= TAG.REF_MAX_INLINE) {
704
804
  const idx = tag - TAG.REF_BASE;
@@ -706,16 +806,16 @@ function _unpack(data, objectList) {
706
806
  throw new TypeError(`Reference index ${idx} out of range`);
707
807
  }
708
808
  value = objectList[idx];
709
- rest = data.subarray(1);
809
+ newOffset = offset + 1;
710
810
  addToObjectList = false;
711
811
  } else if (tag >= TAG.REF_1BYTE && tag <= TAG.REF_8BYTE) {
712
812
  const len = tag - TAG.REF_MAX_INLINE;
713
- const uid = readLittleEndian(data, 1, len);
813
+ const uid = readLittleEndian(data, offset + 1, len);
714
814
  if (uid >= objectList.length) {
715
815
  throw new TypeError(`UID ${uid} out of range`);
716
816
  }
717
817
  value = objectList[uid];
718
- rest = data.subarray(1 + len);
818
+ newOffset = offset + 1 + len;
719
819
  addToObjectList = false;
720
820
  } else {
721
821
  throw new TypeError(`Unknown tag 0x${tag.toString(16)}`);
@@ -723,7 +823,7 @@ function _unpack(data, objectList) {
723
823
  if (addToObjectList) {
724
824
  objectList.push(value);
725
825
  }
726
- return [value, rest];
826
+ return { value, offset: newOffset };
727
827
  }
728
828
  // src/plist.ts
729
829
  var exports_plist = {};
@@ -1032,7 +1132,7 @@ var toUTF16 = (string) => {
1032
1132
  }
1033
1133
  return new Uint8Array(buf);
1034
1134
  };
1035
- var concat2 = (first, second) => {
1135
+ var concat = (first, second) => {
1036
1136
  const third = new Uint8Array(first.length + second.length);
1037
1137
  third.set(first);
1038
1138
  third.set(second, first.length);
@@ -1134,8 +1234,8 @@ var serialize = (value) => {
1134
1234
  });
1135
1235
  }
1136
1236
  function writeTrailer() {
1137
- buffer = concat2(buffer, nullBytes);
1138
- buffer = concat2(buffer, new Uint8Array([offsetSizeInBytes, idSizeInBytes]));
1237
+ buffer = concat(buffer, nullBytes);
1238
+ buffer = concat(buffer, new Uint8Array([offsetSizeInBytes, idSizeInBytes]));
1139
1239
  buffer = writeUInt(buffer, BigInt(entries.length), 8);
1140
1240
  buffer = writeUInt(buffer, BigInt("0"), 8);
1141
1241
  buffer = writeUInt(buffer, BigInt(offsetTableOffset), 8);
@@ -1202,7 +1302,7 @@ var serialize = (value) => {
1202
1302
  const hex = value2.toString(16);
1203
1303
  const buf = fromHexString(hex.padStart(width * 2, "0").slice(0, width * 2));
1204
1304
  buffer = writeUInt(buffer, 20, 1);
1205
- buffer = concat2(buffer, buf);
1305
+ buffer = concat(buffer, buf);
1206
1306
  } else {
1207
1307
  if (Number.isInteger(value2)) {
1208
1308
  if (value2 < 0) {
@@ -1249,16 +1349,16 @@ var serialize = (value) => {
1249
1349
  utf16[i + 0] = utf16[i + 1];
1250
1350
  utf16[i + 1] = t;
1251
1351
  }
1252
- buffer = concat2(buffer, utf16);
1352
+ buffer = concat(buffer, utf16);
1253
1353
  } else {
1254
1354
  const utf8 = encoder.encode(value2);
1255
1355
  writeIntHeader(5, utf8.length);
1256
- buffer = concat2(buffer, utf8);
1356
+ buffer = concat(buffer, utf8);
1257
1357
  }
1258
1358
  }
1259
1359
  function writeData(data) {
1260
1360
  writeIntHeader(4, data.byteLength);
1261
- buffer = concat2(buffer, new Uint8Array(data));
1361
+ buffer = concat(buffer, new Uint8Array(data));
1262
1362
  }
1263
1363
  function writeIntHeader(kind, value2) {
1264
1364
  if (value2 < 15) {
@@ -1521,29 +1621,42 @@ function bail(data) {
1521
1621
  throw new Error("Invalid response");
1522
1622
  }
1523
1623
  function encode3(entries) {
1524
- const chunks = [];
1624
+ let totalSize = 0;
1625
+ for (const [, valueRaw] of entries) {
1626
+ const len = typeof valueRaw === "number" ? 1 : valueRaw.length;
1627
+ const chunks = Math.max(1, Math.ceil(len / 255));
1628
+ totalSize += chunks * 2 + len;
1629
+ }
1630
+ const result = Buffer.allocUnsafe(totalSize);
1631
+ let pos = 0;
1525
1632
  for (const [type, valueRaw] of entries) {
1526
1633
  let value;
1634
+ let valueLen;
1527
1635
  if (typeof valueRaw === "number") {
1528
- value = Buffer.from([valueRaw]);
1529
- } else if (valueRaw instanceof Uint8Array) {
1530
- value = Buffer.from(valueRaw);
1636
+ value = Buffer.allocUnsafe(1);
1637
+ value[0] = valueRaw;
1638
+ valueLen = 1;
1531
1639
  } else {
1532
1640
  value = valueRaw;
1641
+ valueLen = value.length;
1533
1642
  }
1534
1643
  let offset = 0;
1535
1644
  do {
1536
- const len = Math.min(value.length - offset, 255);
1537
- chunks.push(type, len);
1645
+ const len = Math.min(valueLen - offset, 255);
1646
+ result[pos++] = type;
1647
+ result[pos++] = len;
1538
1648
  if (len > 0) {
1539
- for (let i = 0;i < len; i++) {
1540
- chunks.push(value[offset + i]);
1649
+ if (value instanceof Buffer) {
1650
+ value.copy(result, pos, offset, offset + len);
1651
+ } else {
1652
+ result.set(value.subarray(offset, offset + len), pos);
1541
1653
  }
1654
+ pos += len;
1542
1655
  }
1543
1656
  offset += len;
1544
- } while (offset < value.length);
1657
+ } while (offset < valueLen);
1545
1658
  }
1546
- return Buffer.from(chunks);
1659
+ return result;
1547
1660
  }
1548
1661
  function decode4(buf) {
1549
1662
  const map = new Map;
@@ -1551,14 +1664,16 @@ function decode4(buf) {
1551
1664
  while (i < buf.length) {
1552
1665
  const type = buf[i++];
1553
1666
  const len = buf[i++];
1554
- const value = new Uint8Array(buf).slice(i, i + len);
1555
- i += len;
1556
1667
  const existing = map.get(type);
1557
1668
  if (existing) {
1558
- map.set(type, Buffer.concat([existing, value]));
1669
+ const newBuf = Buffer.allocUnsafe(existing.length + len);
1670
+ existing.copy(newBuf, 0);
1671
+ buf.copy(newBuf, existing.length, i, i + len);
1672
+ map.set(type, newBuf);
1559
1673
  } else {
1560
- map.set(type, Buffer.from(value));
1674
+ map.set(type, buf.subarray(i, i + len));
1561
1675
  }
1676
+ i += len;
1562
1677
  }
1563
1678
  return map;
1564
1679
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@basmilius/apple-encoding",
3
3
  "description": "Common encoding utilities for Apple Protocols.",
4
- "version": "0.6.5",
4
+ "version": "0.6.11",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {
@@ -23,7 +23,7 @@
23
23
  ],
24
24
  "publishConfig": {
25
25
  "access": "public",
26
- "provenance": true
26
+ "provenance": false
27
27
  },
28
28
  "scripts": {
29
29
  "build": "tsgo && bun -b build.ts"