@leofcoin/workers 1.5.40 → 1.5.41

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.
@@ -355,16 +355,16 @@ const ALPHABET$3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
355
355
  const ALPHABET_HEX$1 = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
356
356
  const base32 = base(ALPHABET$3);
357
357
  const base32Hex = base(ALPHABET_HEX$1);
358
- const decode$3 = base32.decode;
358
+ const decode$2 = base32.decode;
359
359
  const decodeHex$1 = base32Hex.decode;
360
- const encode$3 = base32.encode;
360
+ const encode$2 = base32.encode;
361
361
  const encodeHex$1 = base32Hex.encode;
362
362
  const isBase32 = (string, hex = false) => {
363
363
  try {
364
364
  if (hex)
365
365
  decodeHex$1(string);
366
366
  else
367
- decode$3(string);
367
+ decode$2(string);
368
368
  return true;
369
369
  }
370
370
  catch (e) {
@@ -374,9 +374,9 @@ const isBase32 = (string, hex = false) => {
374
374
  const isBase32Hex = (string) => {
375
375
  return isBase32(string, true);
376
376
  };
377
- var index$5 = {
378
- encode: encode$3,
379
- decode: decode$3,
377
+ var index$4 = {
378
+ encode: encode$2,
379
+ decode: decode$2,
380
380
  encodeHex: encodeHex$1,
381
381
  decodeHex: decodeHex$1,
382
382
  isBase32,
@@ -387,13 +387,13 @@ const ALPHABET$2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
387
387
  const ALPHABET_HEX = "0123456789ABCDEFGHJKLMNPQRSTUVabcdefghijklmnopqrstuv";
388
388
  const base58 = base(ALPHABET$2);
389
389
  const base58Hex = base(ALPHABET_HEX);
390
- const encode$2 = base58.encode;
391
- const decode$2 = base58.decode;
390
+ const encode$1 = base58.encode;
391
+ const decode$1 = base58.decode;
392
392
  const encodeHex = base58Hex.encode;
393
393
  const decodeHex = base58Hex.decode;
394
394
  const isBase58 = (string) => {
395
395
  try {
396
- decode$2(string);
396
+ decode$1(string);
397
397
  return true;
398
398
  }
399
399
  catch (e) {
@@ -411,7 +411,7 @@ const isBase58Hex = (string) => {
411
411
  };
412
412
  const whatType = (string) => {
413
413
  try {
414
- decode$2(string);
414
+ decode$1(string);
415
415
  return "base58";
416
416
  }
417
417
  catch (e) {
@@ -425,8 +425,8 @@ const whatType = (string) => {
425
425
  }
426
426
  };
427
427
  var base58$1 = {
428
- encode: encode$2,
429
- decode: decode$2,
428
+ encode: encode$1,
429
+ decode: decode$1,
430
430
  isBase58,
431
431
  isBase58Hex,
432
432
  encodeHex,
@@ -440,100 +440,228 @@ var isHex = string => /^[A-F0-9]+$/i.test(
440
440
  string
441
441
  );
442
442
 
443
- const MSB$1 = 0x80;
444
- const MSBALL = -128;
445
- const INT = Math.pow(2, 31);
446
- const encode$1 = (num, out, offset) => {
447
- if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
448
- encode$1.bytes = 0;
449
- throw new RangeError('Could not encode varint');
450
- }
451
- out = out || [];
452
- offset = offset || 0;
453
- const oldOffset = offset;
454
- while (num >= INT) {
455
- out[offset++] = (num & 0xFF) | MSB$1;
456
- num /= 128;
457
- }
458
- while (num & MSBALL) {
459
- out[offset++] = (num & 0xFF) | MSB$1;
460
- num >>>= 7;
461
- }
462
- out[offset] = num | 0;
463
- encode$1.bytes = offset - oldOffset + 1;
464
- return out;
465
- };
466
-
467
- const MSB = 0x80;
468
- const REST = 0x7F;
469
- const decode$1 = (buf, offset) => {
470
- offset = offset || 0;
471
- const l = buf.length;
472
- let counter = offset;
473
- let result = 0;
474
- let shift = 0;
475
- let b;
476
- do {
477
- if (counter >= l || shift > 49) {
478
- decode$1.bytes = 0;
479
- throw new RangeError('Could not decode varint');
480
- }
481
- b = buf[counter++];
482
- result += shift < 28
483
- ? (b & REST) << shift
484
- : (b & REST) * Math.pow(2, shift);
485
- shift += 7;
486
- } while (b >= MSB);
487
- decode$1.bytes = counter - offset;
488
- return result;
489
- };
490
-
491
- const N1 = Math.pow(2, 7);
492
- const N2 = Math.pow(2, 14);
493
- const N3 = Math.pow(2, 21);
494
- const N4 = Math.pow(2, 28);
495
- const N5 = Math.pow(2, 35);
496
- const N6 = Math.pow(2, 42);
497
- const N7 = Math.pow(2, 49);
498
- const N8 = Math.pow(2, 56);
499
- const N9 = Math.pow(2, 63);
500
- var encodingLength = (value) => (value < N1 ? 1
501
- : value < N2 ? 2
502
- : value < N3 ? 3
503
- : value < N4 ? 4
504
- : value < N5 ? 5
505
- : value < N6 ? 6
506
- : value < N7 ? 7
507
- : value < N8 ? 8
508
- : value < N9 ? 9
509
- : 10);
443
+ /** @leofcoin/binary: dependency-free unsigned framing, with no global byte counters. */
444
+ const MAX_BIGINT = (1n << 256n) - 1n;
445
+ function size(value) {
446
+ if (!Number.isSafeInteger(value) || value < 0)
447
+ throw new RangeError('Expected an unsigned safe integer');
448
+ }
449
+ function bytes(value) {
450
+ if (!(value instanceof Uint8Array))
451
+ throw new TypeError('Expected Uint8Array');
452
+ }
453
+ function varUintLength(value) {
454
+ size(value);
455
+ let length = 1;
456
+ while (value >= 128) {
457
+ value = Math.floor(value / 128);
458
+ length++;
459
+ }
460
+ return length;
461
+ }
462
+ function varBigIntLength(value) {
463
+ if (typeof value !== 'bigint')
464
+ throw new TypeError('Expected BigInt');
465
+ if (value < 0n || value > MAX_BIGINT)
466
+ throw new RangeError('Expected an unsigned 256-bit integer');
467
+ let length = 1;
468
+ while (value >= 128n) {
469
+ value >>= 7n;
470
+ length++;
471
+ }
472
+ return length;
473
+ }
474
+ /** Fixed-capacity writer. finish() is a view; copy it before reusing/resetting the writer. */
475
+ class BinaryWriter {
476
+ #buffer;
477
+ #position = 0;
478
+ constructor(capacity = 64) {
479
+ if (typeof capacity === 'number') {
480
+ size(capacity);
481
+ this.#buffer = new Uint8Array(capacity);
482
+ }
483
+ else {
484
+ bytes(capacity);
485
+ this.#buffer = capacity;
486
+ }
487
+ }
488
+ get position() {
489
+ return this.#position;
490
+ }
491
+ get remaining() {
492
+ return this.#buffer.length - this.#position;
493
+ }
494
+ #require(length) {
495
+ size(length);
496
+ if (length > this.remaining)
497
+ throw new RangeError('Binary output buffer is too small');
498
+ }
499
+ writeVarUint(value) {
500
+ this.#require(varUintLength(value));
501
+ this.#writeVarUint(value);
502
+ return this;
503
+ }
504
+ #writeVarUint(value) {
505
+ while (value >= 128) {
506
+ this.#buffer[this.#position++] = value % 128 | 128;
507
+ value = Math.floor(value / 128);
508
+ }
509
+ this.#buffer[this.#position++] = value;
510
+ }
511
+ writeVarBigInt(value) {
512
+ this.#require(varBigIntLength(value));
513
+ do {
514
+ const byte = Number(value & 127n);
515
+ value >>= 7n;
516
+ this.#buffer[this.#position++] = byte | (value ? 128 : 0);
517
+ } while (value);
518
+ return this;
519
+ }
520
+ writeBytes(value) {
521
+ bytes(value);
522
+ this.#require(value.length);
523
+ this.#buffer.set(value, this.#position);
524
+ this.#position += value.length;
525
+ return this;
526
+ }
527
+ writeFrame(value) {
528
+ bytes(value);
529
+ // Check the complete operation before writing its length prefix.
530
+ const prefixLength = varUintLength(value.length);
531
+ this.#require(prefixLength + value.length);
532
+ const prefixStart = this.#buffer.byteOffset + this.#position;
533
+ if (value.buffer === this.#buffer.buffer &&
534
+ value.byteOffset < prefixStart + prefixLength &&
535
+ value.byteOffset + value.length > prefixStart)
536
+ throw new RangeError('Frame source overlaps its length prefix');
537
+ this.#writeVarUint(value.length);
538
+ this.#buffer.set(value, this.#position);
539
+ this.#position += value.length;
540
+ return this;
541
+ }
542
+ reset() {
543
+ this.#position = 0;
544
+ return this;
545
+ }
546
+ finish(copy = false) {
547
+ const result = this.#buffer.subarray(0, this.#position);
548
+ // Buffer.slice() aliases memory; this constructor copies every Uint8Array subtype.
549
+ return copy ? new Uint8Array(result) : result;
550
+ }
551
+ }
552
+ /** Reads views of the original buffer. Failed operations leave the cursor unchanged. */
553
+ class BinaryReader {
554
+ #buffer;
555
+ #position = 0;
556
+ constructor(buffer) {
557
+ bytes(buffer);
558
+ this.#buffer = buffer;
559
+ }
560
+ get position() {
561
+ return this.#position;
562
+ }
563
+ get remaining() {
564
+ return this.#buffer.length - this.#position;
565
+ }
566
+ readVarUint(canonical = true) {
567
+ let result = 0;
568
+ let multiplier = 1;
569
+ for (let index = 0; index < 8; index++) {
570
+ const cursor = this.#position + index;
571
+ if (cursor >= this.#buffer.length)
572
+ throw new RangeError('Truncated varint');
573
+ const byte = this.#buffer[cursor];
574
+ const payload = byte & 127;
575
+ if (index === 7 && payload > Math.floor((Number.MAX_SAFE_INTEGER - result) / multiplier))
576
+ throw new RangeError('Varint exceeds safe integer range');
577
+ result += payload * multiplier;
578
+ if (byte < 128) {
579
+ if (canonical && index > 0 && payload === 0)
580
+ throw new RangeError('Non-canonical varint');
581
+ this.#position = cursor + 1;
582
+ return result;
583
+ }
584
+ multiplier *= 128;
585
+ }
586
+ throw new RangeError('Varint exceeds safe integer range');
587
+ }
588
+ readVarBigInt(canonical = true) {
589
+ let result = 0n;
590
+ for (let index = 0; index < 37; index++) {
591
+ const cursor = this.#position + index;
592
+ if (cursor >= this.#buffer.length)
593
+ throw new RangeError('Truncated varint');
594
+ const byte = this.#buffer[cursor];
595
+ result |= BigInt(byte & 127) << BigInt(index * 7);
596
+ if (result > MAX_BIGINT)
597
+ throw new RangeError('Varint exceeds 256 bits');
598
+ if (byte < 128) {
599
+ if (canonical && index > 0 && byte === 0)
600
+ throw new RangeError('Non-canonical varint');
601
+ this.#position = cursor + 1;
602
+ return result;
603
+ }
604
+ }
605
+ throw new RangeError('Varint exceeds 256 bits');
606
+ }
607
+ readBytes(length) {
608
+ size(length);
609
+ if (length > this.remaining)
610
+ throw new RangeError('Truncated binary field');
611
+ const start = this.#position;
612
+ this.#position += length;
613
+ return this.#buffer.subarray(start, this.#position);
614
+ }
615
+ readFrame(canonical = true) {
616
+ const start = this.#position;
617
+ try {
618
+ return this.readBytes(this.readVarUint(canonical));
619
+ }
620
+ catch (error) {
621
+ this.#position = start;
622
+ throw error;
623
+ }
624
+ }
625
+ }
626
+ /** Existing smart-concat bytes, with exactly one output allocation and no prefix arrays. */
627
+ function encodeFrames(frames, prefix) {
628
+ if (!Array.isArray(frames))
629
+ throw new TypeError('Expected an array of byte frames');
630
+ let length = 0;
631
+ for (const frame of frames) {
632
+ bytes(frame);
633
+ length += varUintLength(frame.length) + frame.length;
634
+ size(length);
635
+ }
636
+ const writer = new BinaryWriter(length);
637
+ for (const frame of frames)
638
+ writer.writeFrame(frame);
639
+ return writer.finish();
640
+ }
641
+ /** Iterative framing: bounded by the input length, not recursive stack depth. */
642
+ function decodeFrames(buffer, prefixLength = 0, options = {}) {
643
+ const reader = new BinaryReader(buffer);
644
+ reader.readBytes(prefixLength);
645
+ const maxFrames = options.maxFrames ?? buffer.length;
646
+ size(maxFrames);
647
+ const frames = [];
648
+ while (reader.remaining) {
649
+ if (frames.length >= maxFrames)
650
+ throw new RangeError('Too many binary frames');
651
+ frames.push(reader.readFrame(options.canonical ?? true));
652
+ }
653
+ return frames;
654
+ }
510
655
 
511
- var index$4 = {
512
- encode: encode$1,
513
- decode: decode$1,
514
- encodingLength
515
- };
516
-
517
- var index$3 = (input, prefix) => {
518
- const encodedArray = [];
519
- const length = input.reduce((total, current) => {
520
- const encoded = index$4.encode(current.length);
521
- encodedArray.push(encoded);
522
- total += current.length + encoded.length;
523
- return total;
524
- }, 0);
525
- const typedArray = new Uint8Array(length);
526
- let currentIndex = 0;
527
- let index = 0;
528
- for (const source of input) {
529
- typedArray.set(encodedArray[index], currentIndex);
530
- currentIndex += encodedArray[index].length;
531
- typedArray.set(source, currentIndex);
532
- currentIndex += source.length;
533
- index += 1;
534
- }
535
- return typedArray;
656
+ const asBytes = (source) => {
657
+ if (source instanceof Uint8Array)
658
+ return source;
659
+ // Keep legacy ArrayLike coercion. Typed-array callers take the zero-copy path.
660
+ const bytes = new Uint8Array(source.length);
661
+ bytes.set(source);
662
+ return bytes;
536
663
  };
664
+ var index$3 = (input, prefix) => encodeFrames(input.map(asBytes));
537
665
 
538
666
  /*! pako 2.2.0 https://github.com/nodeca/pako @license (MIT AND Zlib) */
539
667
  // (C) 1995-2013 Jean-loup Gailly and Mark Adler
@@ -7531,22 +7659,7 @@ const encode = (proto, input, compress) => {
7531
7659
  return compress ? pako.deflate(index$3(set)) : index$3(set);
7532
7660
  };
7533
7661
 
7534
- var index$2 = (typedArray, prefix) => {
7535
- const set = [];
7536
- const varintAndSub = (typedArray) => {
7537
- const length = index$4.decode(typedArray);
7538
- // remove length
7539
- typedArray = typedArray.subarray(index$4.decode.bytes);
7540
- // push value
7541
- set.push(typedArray.subarray(0, length));
7542
- // remove value
7543
- typedArray = typedArray.subarray(length);
7544
- if (typedArray.length !== 0)
7545
- return varintAndSub(typedArray);
7546
- return set;
7547
- };
7548
- return varintAndSub(typedArray);
7549
- };
7662
+ var index$2 = (typedArray, prefix) => decodeFrames(typedArray, 0, { canonical: false });
7550
7663
 
7551
7664
  const ALPHABET$1 = '0123456789ABCDEF';
7552
7665
  const base16 = base(ALPHABET$1);
@@ -7584,7 +7697,7 @@ const toHex = (bytes) => bytes.reduce((string, byte) => string + byte.toString(1
7584
7697
  const fromArrayLike = (array) => Uint8Array.from(array);
7585
7698
  const toBase58 = (uint8Array) => base58$1.encode(uint8Array);
7586
7699
  const fromBase58 = (string) => base58$1.decode(string);
7587
- const toBase32 = (uint8Array) => index$5.encode(uint8Array);
7700
+ const toBase32 = (uint8Array) => index$4.encode(uint8Array);
7588
7701
  var index$1 = {
7589
7702
  toString: toString$1};
7590
7703
 
@@ -7684,13 +7797,13 @@ class BasicInterface {
7684
7797
  this.fromBs58(value);
7685
7798
  }
7686
7799
  catch {
7687
- if (index$5.isBase32(value))
7800
+ if (index$4.isBase32(value))
7688
7801
  this.fromBs32(value);
7689
7802
  else
7690
7803
  throw new Error('Invalid base58/base32 input');
7691
7804
  }
7692
7805
  }
7693
- else if (index$5.isBase32(value))
7806
+ else if (index$4.isBase32(value))
7694
7807
  this.fromBs32(value);
7695
7808
  else
7696
7809
  this.fromString(value);
@@ -7758,13 +7871,13 @@ class BasicInterface {
7758
7871
  return isHex(string);
7759
7872
  }
7760
7873
  isBase32(string) {
7761
- return index$5.isBase32(string);
7874
+ return index$4.isBase32(string);
7762
7875
  }
7763
7876
  isBase58(string) {
7764
7877
  return base58$1.isBase58(string);
7765
7878
  }
7766
7879
  fromBs32(encoded) {
7767
- return this.decode(index$5.decode(encoded));
7880
+ return this.decode(index$4.decode(encoded));
7768
7881
  }
7769
7882
  fromBs58(encoded) {
7770
7883
  return this.decode(fromBase58(encoded));
@@ -8107,7 +8220,7 @@ class Codec extends BasicInterface {
8107
8220
  super();
8108
8221
  if (buffer) {
8109
8222
  if (buffer instanceof Uint8Array) {
8110
- const codec = index$4.decode(buffer);
8223
+ const codec = new BinaryReader(buffer).readVarUint(false);
8111
8224
  const name = this.getCodecName(codec);
8112
8225
  if (name) {
8113
8226
  this.name = name;
@@ -8119,11 +8232,12 @@ class Codec extends BasicInterface {
8119
8232
  }
8120
8233
  }
8121
8234
  else if (buffer instanceof ArrayBuffer) {
8122
- const codec = index$4.decode(buffer);
8235
+ const view = new Uint8Array(buffer);
8236
+ const codec = new BinaryReader(view).readVarUint(false);
8123
8237
  const name = this.getCodecName(codec);
8124
8238
  if (name) {
8125
8239
  this.name = name;
8126
- this.decode(buffer);
8240
+ this.decode(view);
8127
8241
  }
8128
8242
  else {
8129
8243
  this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
@@ -8148,7 +8262,7 @@ class Codec extends BasicInterface {
8148
8262
  }
8149
8263
  }
8150
8264
  fromEncoded(encoded) {
8151
- const codec = index$4.decode(encoded);
8265
+ const codec = new BinaryReader(encoded).readVarUint(false);
8152
8266
  const name = this.getCodecName(codec);
8153
8267
  this.name = name;
8154
8268
  this.encoded = encoded;
@@ -8167,7 +8281,7 @@ class Codec extends BasicInterface {
8167
8281
  this.name = this.getCodecName(codec);
8168
8282
  this.hashAlg = this.getHashAlg(this.name);
8169
8283
  this.codec = this.getCodec(this.name);
8170
- this.codecBuffer = index$4.encode(this.codec);
8284
+ this.codecBuffer = new BinaryWriter(varUintLength(this.codec)).writeVarUint(this.codec).finish();
8171
8285
  this.decoded = {
8172
8286
  name: this.name,
8173
8287
  hashAlg: this.hashAlg,
@@ -8180,7 +8294,7 @@ class Codec extends BasicInterface {
8180
8294
  this.name = name;
8181
8295
  this.codec = codec;
8182
8296
  this.hashAlg = this.getHashAlg(name);
8183
- this.codecBuffer = index$4.encode(this.codec);
8297
+ this.codecBuffer = new BinaryWriter(varUintLength(this.codec)).writeVarUint(this.codec).finish();
8184
8298
  this.decoded = {
8185
8299
  name: this.name,
8186
8300
  hashAlg: this.hashAlg,
@@ -8190,13 +8304,13 @@ class Codec extends BasicInterface {
8190
8304
  }
8191
8305
  decode(encoded) {
8192
8306
  encoded = encoded || this.encoded;
8193
- const codec = index$4.decode(encoded);
8307
+ const codec = new BinaryReader(encoded).readVarUint(false);
8194
8308
  this.fromCodec(codec);
8195
8309
  return this.decoded;
8196
8310
  }
8197
8311
  encode(codec) {
8198
8312
  codec = codec || this.codec;
8199
- this.encoded = index$4.encode(codec);
8313
+ this.encoded = new BinaryWriter(varUintLength(codec)).writeVarUint(codec).finish();
8200
8314
  return this.encoded;
8201
8315
  }
8202
8316
  }
@@ -8792,10 +8906,37 @@ const digestBuffer = async (hashVariant, buffer) => {
8792
8906
  const digest = hasher.digest('binary');
8793
8907
  return digest instanceof Uint8Array ? digest : new Uint8Array(digest);
8794
8908
  };
8909
+ const hashHeader = (buffer) => {
8910
+ if (!(buffer instanceof Uint8Array))
8911
+ throw new TypeError('CodecHash only supports Uint8Array input');
8912
+ const reader = new BinaryReader(buffer);
8913
+ const codec = reader.readVarUint();
8914
+ const name = utils.getCodecName(codec);
8915
+ if (!name)
8916
+ throw new Error('unknown hash codec');
8917
+ const size = reader.readVarUint();
8918
+ if (size !== reader.remaining)
8919
+ throw new Error('hash length inconsistent');
8920
+ return { name, size, offset: reader.position };
8921
+ };
8922
+ const hasHashHeader = (buffer) => {
8923
+ const reader = new BinaryReader(buffer);
8924
+ try {
8925
+ // Recognition is deliberately separate from strict parsing: a padded known
8926
+ // codec must reach decode() and fail, not silently become raw content.
8927
+ return Boolean(utils.getCodecName(reader.readVarUint(false)));
8928
+ }
8929
+ catch (error) {
8930
+ if (!(error instanceof RangeError))
8931
+ throw error;
8932
+ return false;
8933
+ }
8934
+ };
8795
8935
  class CodecHash extends BasicInterface {
8796
8936
  codec;
8797
8937
  discoCodec;
8798
8938
  size;
8939
+ digest;
8799
8940
  codecs;
8800
8941
  constructor(options = { name: 'disco-hash', codecs: {} }) {
8801
8942
  super();
@@ -8803,21 +8944,14 @@ class CodecHash extends BasicInterface {
8803
8944
  this.name = options.name;
8804
8945
  else
8805
8946
  this.name = 'disco-hash';
8806
- if (options?.codecs)
8807
- this.codecs = options.codecs;
8947
+ this.codecs = options?.codecs ?? {};
8808
8948
  }
8809
8949
  async init(uint8Array) {
8810
8950
  if (uint8Array) {
8811
8951
  if (!(uint8Array instanceof Uint8Array)) {
8812
8952
  throw new Error('CodecHash only supports Uint8Array input');
8813
8953
  }
8814
- // For large buffers, only check first bytes for codec prefix
8815
- // Codec prefixes are small (varint encoded), so 100 bytes is more than enough
8816
- const checkBuffer = uint8Array.length > 100 ? uint8Array.subarray(0, 100) : uint8Array;
8817
- this.discoCodec = new Codec(checkBuffer);
8818
- const name = this.discoCodec.name;
8819
- if (name) {
8820
- this.name = name;
8954
+ if (hasHashHeader(uint8Array)) {
8821
8955
  this.decode(uint8Array);
8822
8956
  }
8823
8957
  else {
@@ -8826,15 +8960,19 @@ class CodecHash extends BasicInterface {
8826
8960
  }
8827
8961
  return this;
8828
8962
  }
8963
+ #clearEncodingCache() {
8964
+ for (const key of ['_bs32', '_hex', '_string'])
8965
+ delete this[key];
8966
+ }
8829
8967
  get prefix() {
8830
8968
  const length = this.length;
8831
8969
  const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
8832
- uint8Array.set(length);
8833
- uint8Array.set(this.discoCodec.codecBuffer, length.length);
8970
+ uint8Array.set(this.discoCodec.codecBuffer);
8971
+ uint8Array.set(length, this.discoCodec.codecBuffer.length);
8834
8972
  return uint8Array;
8835
8973
  }
8836
8974
  get length() {
8837
- return index$4.encode(this.size);
8975
+ return new BinaryWriter(varUintLength(this.size)).writeVarUint(this.size).finish();
8838
8976
  }
8839
8977
  get buffer() {
8840
8978
  return this.encoded;
@@ -8861,6 +8999,8 @@ class CodecHash extends BasicInterface {
8861
8999
  const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
8862
9000
  uint8Array.set(this.prefix);
8863
9001
  uint8Array.set(this.digest, this.prefix.length);
9002
+ this.#clearEncodingCache();
9003
+ this.decoded = undefined;
8864
9004
  this.encoded = uint8Array;
8865
9005
  return this.encoded;
8866
9006
  }
@@ -8868,35 +9008,30 @@ class CodecHash extends BasicInterface {
8868
9008
  if (!(buffer instanceof Uint8Array)) {
8869
9009
  throw new Error('CodecHash only supports Uint8Array input');
8870
9010
  }
8871
- const codec = index$4.decode(buffer);
8872
- if (this.codecs[codec]) {
8873
- this.decode(buffer);
8874
- }
8875
- else {
8876
- await this.encode(buffer);
8877
- }
9011
+ await this.init(buffer);
8878
9012
  }
8879
9013
  decode(buffer) {
9014
+ buffer = buffer ?? this.encoded;
9015
+ // Validate completely before changing this instance, including cached encodings.
9016
+ const header = hashHeader(buffer);
9017
+ const codec = new Codec(header.name);
9018
+ const digest = new Uint8Array(buffer.subarray(header.offset));
9019
+ this.#clearEncodingCache();
8880
9020
  this.encoded = buffer;
8881
- const codec = index$4.decode(buffer);
8882
- this.discoCodec = new Codec(codec, this.codecs);
8883
- // TODO: validate codec
8884
- buffer = buffer.slice(index$4.decode.bytes);
8885
- this.size = index$4.decode(buffer);
8886
- this.digest = buffer.slice(index$4.decode.bytes);
8887
- if (this.digest.length !== this.size) {
8888
- throw new Error(`hash length inconsistent: 0x${this.encoded.toString()}`);
8889
- }
8890
- // const discoCodec = new Codec(codec, this.codecs)
8891
- this.name = this.discoCodec.name;
8892
- this.size = this.digest.length;
8893
- return {
9021
+ this.discoCodec = codec;
9022
+ this.codec = codec.codecBuffer;
9023
+ this.name = header.name;
9024
+ this.size = header.size;
9025
+ this.digest = digest;
9026
+ const decoded = {
8894
9027
  codec: this.codec,
8895
9028
  name: this.name,
8896
9029
  size: this.size,
8897
9030
  length: this.length,
8898
9031
  digest: this.digest
8899
9032
  };
9033
+ this.decoded = decoded;
9034
+ return decoded;
8900
9035
  }
8901
9036
  }
8902
9037
 
@@ -9009,13 +9144,14 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
9009
9144
  !Object.prototype.hasOwnProperty.call(this.decoded, 'hash')) {
9010
9145
  // @ts-ignore
9011
9146
  const hash = new CodecHash({ name: this.name });
9012
- return hash.init(this.contentBytes);
9147
+ // These are explicitly raw message bytes, even if they resemble a hash header.
9148
+ return hash.encode(this.contentBytes).then(() => hash);
9013
9149
  }
9014
9150
  // Fallback: must re-encode without hash property
9015
9151
  const decoded = this.beforeHashing({ ...this.decoded });
9016
9152
  // @ts-ignore
9017
9153
  const hash = new CodecHash({ name: this.name });
9018
- return hash.init(decoded);
9154
+ return hash.encode(this.protoEncode(decoded)).then(() => hash);
9019
9155
  }
9020
9156
  /**
9021
9157
  * @return {peernetHash}
@@ -9236,4 +9372,4 @@ class EasyWorker {
9236
9372
  }
9237
9373
  }
9238
9374
 
9239
- export { EasyWorker as E, FormatInterface as F, proto$3 as a, index$2 as b, proto$1 as c, proto as d, jsonParseBigInt$1 as e, index$3 as i, jsonStringifyBigInt$1 as j, proto$2 as p };
9375
+ export { EasyWorker as E, FormatInterface as F, proto$3 as a, proto$1 as b, proto as c, decodeFrames as d, encodeFrames as e, jsonParseBigInt$1 as f, jsonStringifyBigInt$1 as j, proto$2 as p };