@waku/core 0.0.30-e49e728.0 → 0.0.31-ce62600.0

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/CHANGELOG.md CHANGED
@@ -5,6 +5,33 @@ All notable changes to this project will be documented in this file.
5
5
  The file is maintained by [Release Please](https://github.com/googleapis/release-please) based on [Conventional Commits](https://www.conventionalcommits.org) specification,
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.0.30](https://github.com/waku-org/js-waku/compare/core-v0.0.29...core-v0.0.30) (2024-07-10)
9
+
10
+
11
+ ### ⚠ BREAKING CHANGES
12
+
13
+ * **filter:** return error codes instead of throwing errors ([#1971](https://github.com/waku-org/js-waku/issues/1971))
14
+
15
+ ### Features
16
+
17
+ * **filter:** Return error codes instead of throwing errors ([#1971](https://github.com/waku-org/js-waku/issues/1971)) ([4eb06c6](https://github.com/waku-org/js-waku/commit/4eb06c64eb05c015e2f51e3f45a9d7143a934385))
18
+ * **lightpush:** Peer management for protocols ([#2003](https://github.com/waku-org/js-waku/issues/2003)) ([93e78c3](https://github.com/waku-org/js-waku/commit/93e78c3b876e084ab70e07c64c9b721693b659f8))
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * Failing `node_optional` check ([#2025](https://github.com/waku-org/js-waku/issues/2025)) ([984fb94](https://github.com/waku-org/js-waku/commit/984fb94b5b23a0d2f7edebad36170911ce7a2e84))
24
+ * Increasing maxInboundStreams for lightpush from 32 to 100 ([#2021](https://github.com/waku-org/js-waku/issues/2021)) ([2311a59](https://github.com/waku-org/js-waku/commit/2311a595b28b9e7c99fcd96044cf098ad975c70c))
25
+
26
+
27
+ ### Dependencies
28
+
29
+ * The following workspace dependencies were updated
30
+ * dependencies
31
+ * @waku/enr bumped from ^0.0.23 to ^0.0.24
32
+ * @waku/interfaces bumped from 0.0.24 to 0.0.25
33
+ * @waku/utils bumped from 0.0.17 to 0.0.18
34
+
8
35
  ## [0.0.29](https://github.com/waku-org/js-waku/compare/core-v0.0.28...core-v0.0.29) (2024-04-30)
9
36
 
10
37
 
@@ -1,5 +1,4 @@
1
- import { c as bytesToUtf8, L as Logger, e as ensureShardingConfigured } from './index-BcSodzY4.js';
2
- import { T as Tags } from './browser-zSUobdfj.js';
1
+ import { h as bytesToUtf8, T as Tags, L as Logger, i as ensureShardingConfigured } from './index-DnW8ifxc.js';
3
2
 
4
3
  const decodeRelayShard = (bytes) => {
5
4
  // explicitly converting to Uint8Array to avoid Buffer
@@ -1,3 +1,19 @@
1
+ /**
2
+ * Returns a `Uint8Array` of the requested size. Referenced memory will
3
+ * be initialized to 0.
4
+ */
5
+ function alloc(size = 0) {
6
+ return new Uint8Array(size);
7
+ }
8
+ /**
9
+ * Where possible returns a Uint8Array of the requested size that references
10
+ * uninitialized memory. Only use if you are certain you will immediately
11
+ * overwrite every value in the returned `Uint8Array`.
12
+ */
13
+ function allocUnsafe(size = 0) {
14
+ return new Uint8Array(size);
15
+ }
16
+
1
17
  function coerce(o) {
2
18
  if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array')
3
19
  return o;
@@ -8,10 +24,10 @@ function coerce(o) {
8
24
  }
9
25
  throw new Error('Unknown type, must be binary type');
10
26
  }
11
- function fromString(str) {
27
+ function fromString$1(str) {
12
28
  return new TextEncoder().encode(str);
13
29
  }
14
- function toString(b) {
30
+ function toString$1(b) {
15
31
  return new TextDecoder().decode(b);
16
32
  }
17
33
 
@@ -608,8 +624,8 @@ var base8$1 = /*#__PURE__*/Object.freeze({
608
624
  const identity = from({
609
625
  prefix: '\x00',
610
626
  name: 'identity',
611
- encode: (buf) => toString(buf),
612
- decode: (str) => fromString(str)
627
+ encode: (buf) => toString$1(buf),
628
+ decode: (str) => fromString$1(str)
613
629
  });
614
630
 
615
631
  var identityBase = /*#__PURE__*/Object.freeze({
@@ -620,6 +636,366 @@ var identityBase = /*#__PURE__*/Object.freeze({
620
636
  new TextEncoder();
621
637
  new TextDecoder();
622
638
 
639
+ const bases = { ...identityBase, ...base2$1, ...base8$1, ...base10$1, ...base16$1, ...base32$1, ...base36$1, ...base58, ...base64$1, ...base256emoji$1 };
640
+
641
+ function createCodec(name, prefix, encode, decode) {
642
+ return {
643
+ name,
644
+ prefix,
645
+ encoder: {
646
+ name,
647
+ prefix,
648
+ encode
649
+ },
650
+ decoder: {
651
+ decode
652
+ }
653
+ };
654
+ }
655
+ const string = createCodec('utf8', 'u', (buf) => {
656
+ const decoder = new TextDecoder('utf8');
657
+ return 'u' + decoder.decode(buf);
658
+ }, (str) => {
659
+ const encoder = new TextEncoder();
660
+ return encoder.encode(str.substring(1));
661
+ });
662
+ const ascii = createCodec('ascii', 'a', (buf) => {
663
+ let string = 'a';
664
+ for (let i = 0; i < buf.length; i++) {
665
+ string += String.fromCharCode(buf[i]);
666
+ }
667
+ return string;
668
+ }, (str) => {
669
+ str = str.substring(1);
670
+ const buf = allocUnsafe(str.length);
671
+ for (let i = 0; i < str.length; i++) {
672
+ buf[i] = str.charCodeAt(i);
673
+ }
674
+ return buf;
675
+ });
676
+ const BASES = {
677
+ utf8: string,
678
+ 'utf-8': string,
679
+ hex: bases.base16,
680
+ latin1: ascii,
681
+ ascii,
682
+ binary: ascii,
683
+ ...bases
684
+ };
685
+
686
+ /**
687
+ * Create a `Uint8Array` from the passed string
688
+ *
689
+ * Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module.
690
+ *
691
+ * Also `ascii` which is similar to node's 'binary' encoding.
692
+ */
693
+ function fromString(string, encoding = 'utf8') {
694
+ const base = BASES[encoding];
695
+ if (base == null) {
696
+ throw new Error(`Unsupported encoding "${encoding}"`);
697
+ }
698
+ // add multibase prefix
699
+ return base.decoder.decode(`${base.prefix}${string}`); // eslint-disable-line @typescript-eslint/restrict-template-expressions
700
+ }
701
+
702
+ // copied from utils
703
+ function isBytes(a) {
704
+ return (a instanceof Uint8Array ||
705
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
706
+ }
707
+ function bytes(b, ...lengths) {
708
+ if (!isBytes(b))
709
+ throw new Error('Uint8Array expected');
710
+ if (lengths.length > 0 && !lengths.includes(b.length))
711
+ throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
712
+ }
713
+ function exists(instance, checkFinished = true) {
714
+ if (instance.destroyed)
715
+ throw new Error('Hash instance has been destroyed');
716
+ if (checkFinished && instance.finished)
717
+ throw new Error('Hash#digest() has already been called');
718
+ }
719
+ function output(out, instance) {
720
+ bytes(out);
721
+ const min = instance.outputLen;
722
+ if (out.length < min) {
723
+ throw new Error(`digestInto() expects output buffer of length at least ${min}`);
724
+ }
725
+ }
726
+
727
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
728
+ // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
729
+ // node.js versions earlier than v19 don't declare it in global scope.
730
+ // For node.js, package.json#exports field mapping rewrites import
731
+ // from `crypto` to `cryptoNode`, which imports native module.
732
+ // Makes the utils un-importable in browsers without a bundler.
733
+ // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
734
+ // Cast array to view
735
+ const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
736
+ // The rotate right (circular right shift) operation for uint32
737
+ const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
738
+ new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
739
+ /**
740
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
741
+ */
742
+ function utf8ToBytes$1(str) {
743
+ if (typeof str !== 'string')
744
+ throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
745
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
746
+ }
747
+ /**
748
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
749
+ * Warning: when Uint8Array is passed, it would NOT get copied.
750
+ * Keep in mind for future mutable operations.
751
+ */
752
+ function toBytes(data) {
753
+ if (typeof data === 'string')
754
+ data = utf8ToBytes$1(data);
755
+ bytes(data);
756
+ return data;
757
+ }
758
+ // For runtime check if class implements interface
759
+ class Hash {
760
+ // Safe version that clones internal state
761
+ clone() {
762
+ return this._cloneInto();
763
+ }
764
+ }
765
+ function wrapConstructor(hashCons) {
766
+ const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
767
+ const tmp = hashCons();
768
+ hashC.outputLen = tmp.outputLen;
769
+ hashC.blockLen = tmp.blockLen;
770
+ hashC.create = () => hashCons();
771
+ return hashC;
772
+ }
773
+
774
+ // Polyfill for Safari 14
775
+ function setBigUint64(view, byteOffset, value, isLE) {
776
+ if (typeof view.setBigUint64 === 'function')
777
+ return view.setBigUint64(byteOffset, value, isLE);
778
+ const _32n = BigInt(32);
779
+ const _u32_max = BigInt(0xffffffff);
780
+ const wh = Number((value >> _32n) & _u32_max);
781
+ const wl = Number(value & _u32_max);
782
+ const h = isLE ? 4 : 0;
783
+ const l = isLE ? 0 : 4;
784
+ view.setUint32(byteOffset + h, wh, isLE);
785
+ view.setUint32(byteOffset + l, wl, isLE);
786
+ }
787
+ // Choice: a ? b : c
788
+ const Chi = (a, b, c) => (a & b) ^ (~a & c);
789
+ // Majority function, true if any two inpust is true
790
+ const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
791
+ /**
792
+ * Merkle-Damgard hash construction base class.
793
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
794
+ */
795
+ class HashMD extends Hash {
796
+ constructor(blockLen, outputLen, padOffset, isLE) {
797
+ super();
798
+ this.blockLen = blockLen;
799
+ this.outputLen = outputLen;
800
+ this.padOffset = padOffset;
801
+ this.isLE = isLE;
802
+ this.finished = false;
803
+ this.length = 0;
804
+ this.pos = 0;
805
+ this.destroyed = false;
806
+ this.buffer = new Uint8Array(blockLen);
807
+ this.view = createView(this.buffer);
808
+ }
809
+ update(data) {
810
+ exists(this);
811
+ const { view, buffer, blockLen } = this;
812
+ data = toBytes(data);
813
+ const len = data.length;
814
+ for (let pos = 0; pos < len;) {
815
+ const take = Math.min(blockLen - this.pos, len - pos);
816
+ // Fast path: we have at least one block in input, cast it to view and process
817
+ if (take === blockLen) {
818
+ const dataView = createView(data);
819
+ for (; blockLen <= len - pos; pos += blockLen)
820
+ this.process(dataView, pos);
821
+ continue;
822
+ }
823
+ buffer.set(data.subarray(pos, pos + take), this.pos);
824
+ this.pos += take;
825
+ pos += take;
826
+ if (this.pos === blockLen) {
827
+ this.process(view, 0);
828
+ this.pos = 0;
829
+ }
830
+ }
831
+ this.length += data.length;
832
+ this.roundClean();
833
+ return this;
834
+ }
835
+ digestInto(out) {
836
+ exists(this);
837
+ output(out, this);
838
+ this.finished = true;
839
+ // Padding
840
+ // We can avoid allocation of buffer for padding completely if it
841
+ // was previously not allocated here. But it won't change performance.
842
+ const { buffer, view, blockLen, isLE } = this;
843
+ let { pos } = this;
844
+ // append the bit '1' to the message
845
+ buffer[pos++] = 0b10000000;
846
+ this.buffer.subarray(pos).fill(0);
847
+ // we have less than padOffset left in buffer, so we cannot put length in
848
+ // current block, need process it and pad again
849
+ if (this.padOffset > blockLen - pos) {
850
+ this.process(view, 0);
851
+ pos = 0;
852
+ }
853
+ // Pad until full block byte with zeros
854
+ for (let i = pos; i < blockLen; i++)
855
+ buffer[i] = 0;
856
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
857
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
858
+ // So we just write lowest 64 bits of that value.
859
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
860
+ this.process(view, 0);
861
+ const oview = createView(out);
862
+ const len = this.outputLen;
863
+ // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
864
+ if (len % 4)
865
+ throw new Error('_sha2: outputLen should be aligned to 32bit');
866
+ const outLen = len / 4;
867
+ const state = this.get();
868
+ if (outLen > state.length)
869
+ throw new Error('_sha2: outputLen bigger than state');
870
+ for (let i = 0; i < outLen; i++)
871
+ oview.setUint32(4 * i, state[i], isLE);
872
+ }
873
+ digest() {
874
+ const { buffer, outputLen } = this;
875
+ this.digestInto(buffer);
876
+ const res = buffer.slice(0, outputLen);
877
+ this.destroy();
878
+ return res;
879
+ }
880
+ _cloneInto(to) {
881
+ to || (to = new this.constructor());
882
+ to.set(...this.get());
883
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
884
+ to.length = length;
885
+ to.pos = pos;
886
+ to.finished = finished;
887
+ to.destroyed = destroyed;
888
+ if (length % blockLen)
889
+ to.buffer.set(buffer);
890
+ return to;
891
+ }
892
+ }
893
+
894
+ // SHA2-256 need to try 2^128 hashes to execute birthday attack.
895
+ // BTC network is doing 2^67 hashes/sec as per early 2023.
896
+ // Round constants:
897
+ // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
898
+ // prettier-ignore
899
+ const SHA256_K = /* @__PURE__ */ new Uint32Array([
900
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
901
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
902
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
903
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
904
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
905
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
906
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
907
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
908
+ ]);
909
+ // Initial state:
910
+ // first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
911
+ // prettier-ignore
912
+ const SHA256_IV = /* @__PURE__ */ new Uint32Array([
913
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
914
+ ]);
915
+ // Temporary buffer, not used to store anything between runs
916
+ // Named this way because it matches specification.
917
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
918
+ class SHA256 extends HashMD {
919
+ constructor() {
920
+ super(64, 32, 8, false);
921
+ // We cannot use array here since array allows indexing by variable
922
+ // which means optimizer/compiler cannot use registers.
923
+ this.A = SHA256_IV[0] | 0;
924
+ this.B = SHA256_IV[1] | 0;
925
+ this.C = SHA256_IV[2] | 0;
926
+ this.D = SHA256_IV[3] | 0;
927
+ this.E = SHA256_IV[4] | 0;
928
+ this.F = SHA256_IV[5] | 0;
929
+ this.G = SHA256_IV[6] | 0;
930
+ this.H = SHA256_IV[7] | 0;
931
+ }
932
+ get() {
933
+ const { A, B, C, D, E, F, G, H } = this;
934
+ return [A, B, C, D, E, F, G, H];
935
+ }
936
+ // prettier-ignore
937
+ set(A, B, C, D, E, F, G, H) {
938
+ this.A = A | 0;
939
+ this.B = B | 0;
940
+ this.C = C | 0;
941
+ this.D = D | 0;
942
+ this.E = E | 0;
943
+ this.F = F | 0;
944
+ this.G = G | 0;
945
+ this.H = H | 0;
946
+ }
947
+ process(view, offset) {
948
+ // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
949
+ for (let i = 0; i < 16; i++, offset += 4)
950
+ SHA256_W[i] = view.getUint32(offset, false);
951
+ for (let i = 16; i < 64; i++) {
952
+ const W15 = SHA256_W[i - 15];
953
+ const W2 = SHA256_W[i - 2];
954
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
955
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
956
+ SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
957
+ }
958
+ // Compression function main loop, 64 rounds
959
+ let { A, B, C, D, E, F, G, H } = this;
960
+ for (let i = 0; i < 64; i++) {
961
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
962
+ const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
963
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
964
+ const T2 = (sigma0 + Maj(A, B, C)) | 0;
965
+ H = G;
966
+ G = F;
967
+ F = E;
968
+ E = (D + T1) | 0;
969
+ D = C;
970
+ C = B;
971
+ B = A;
972
+ A = (T1 + T2) | 0;
973
+ }
974
+ // Add the compressed chunk to the current hash value
975
+ A = (A + this.A) | 0;
976
+ B = (B + this.B) | 0;
977
+ C = (C + this.C) | 0;
978
+ D = (D + this.D) | 0;
979
+ E = (E + this.E) | 0;
980
+ F = (F + this.F) | 0;
981
+ G = (G + this.G) | 0;
982
+ H = (H + this.H) | 0;
983
+ this.set(A, B, C, D, E, F, G, H);
984
+ }
985
+ roundClean() {
986
+ SHA256_W.fill(0);
987
+ }
988
+ destroy() {
989
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
990
+ this.buffer.fill(0);
991
+ }
992
+ }
993
+ /**
994
+ * SHA2-256 hash function
995
+ * @param message - data that would be hashed
996
+ */
997
+ const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
998
+
623
999
  var Protocols;
624
1000
  (function (Protocols) {
625
1001
  Protocols["Relay"] = "relay";
@@ -715,6 +1091,213 @@ var EConnectionStateEvents;
715
1091
  EConnectionStateEvents["CONNECTION_STATUS"] = "waku:connection";
716
1092
  })(EConnectionStateEvents || (EConnectionStateEvents = {}));
717
1093
 
1094
+ /**
1095
+ * DefaultPubsubTopic is the default gossipsub topic to use for Waku.
1096
+ */
1097
+ /**
1098
+ * The default cluster ID for The Waku Network
1099
+ */
1100
+ const DEFAULT_CLUSTER_ID = 1;
1101
+
1102
+ /**
1103
+ * Turns a `Uint8Array` into a string.
1104
+ *
1105
+ * Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
1106
+ *
1107
+ * Also `ascii` which is similar to node's 'binary' encoding.
1108
+ */
1109
+ function toString(array, encoding = 'utf8') {
1110
+ const base = BASES[encoding];
1111
+ if (base == null) {
1112
+ throw new Error(`Unsupported encoding "${encoding}"`);
1113
+ }
1114
+ // strip multibase prefix
1115
+ return base.encoder.encode(array).substring(1);
1116
+ }
1117
+
1118
+ /**
1119
+ * Decode byte array to utf-8 string.
1120
+ */
1121
+ const bytesToUtf8 = (b) => toString(b, "utf8");
1122
+ /**
1123
+ * Encode utf-8 string to byte array.
1124
+ */
1125
+ const utf8ToBytes = (s) => fromString(s, "utf8");
1126
+ /**
1127
+ * Concatenate using Uint8Arrays as `Buffer` has a different behavior with `DataView`
1128
+ */
1129
+ function concat(byteArrays, totalLength) {
1130
+ const len = totalLength ?? byteArrays.reduce((acc, curr) => acc + curr.length, 0);
1131
+ const res = new Uint8Array(len);
1132
+ let offset = 0;
1133
+ for (const bytes of byteArrays) {
1134
+ res.set(bytes, offset);
1135
+ offset += bytes.length;
1136
+ }
1137
+ return res;
1138
+ }
1139
+
1140
+ const singleShardInfoToPubsubTopic = (shardInfo) => {
1141
+ if (shardInfo.shard === undefined)
1142
+ throw new Error("Invalid shard");
1143
+ return `/waku/2/rs/${shardInfo.clusterId ?? DEFAULT_CLUSTER_ID}/${shardInfo.shard}`;
1144
+ };
1145
+ const shardInfoToPubsubTopics = (shardInfo) => {
1146
+ if ("contentTopics" in shardInfo && shardInfo.contentTopics) {
1147
+ // Autosharding: explicitly defined content topics
1148
+ return Array.from(new Set(shardInfo.contentTopics.map((contentTopic) => contentTopicToPubsubTopic(contentTopic, shardInfo.clusterId))));
1149
+ }
1150
+ else if ("shards" in shardInfo) {
1151
+ // Static sharding
1152
+ if (shardInfo.shards === undefined)
1153
+ throw new Error("Invalid shard");
1154
+ return Array.from(new Set(shardInfo.shards.map((index) => `/waku/2/rs/${shardInfo.clusterId ?? DEFAULT_CLUSTER_ID}/${index}`)));
1155
+ }
1156
+ else if ("application" in shardInfo && "version" in shardInfo) {
1157
+ // Autosharding: single shard from application and version
1158
+ return [
1159
+ contentTopicToPubsubTopic(`/${shardInfo.application}/${shardInfo.version}/default/default`, shardInfo.clusterId)
1160
+ ];
1161
+ }
1162
+ else {
1163
+ throw new Error("Missing required configuration in shard parameters");
1164
+ }
1165
+ };
1166
+ const pubsubTopicToSingleShardInfo = (pubsubTopics) => {
1167
+ const parts = pubsubTopics.split("/");
1168
+ if (parts.length != 6 ||
1169
+ parts[1] !== "waku" ||
1170
+ parts[2] !== "2" ||
1171
+ parts[3] !== "rs")
1172
+ throw new Error("Invalid pubsub topic");
1173
+ const clusterId = parseInt(parts[4]);
1174
+ const shard = parseInt(parts[5]);
1175
+ if (isNaN(clusterId) || isNaN(shard))
1176
+ throw new Error("Invalid clusterId or shard");
1177
+ return {
1178
+ clusterId,
1179
+ shard
1180
+ };
1181
+ };
1182
+ /**
1183
+ * Given a string, will throw an error if it is not formatted as a valid content topic for autosharding based on https://rfc.vac.dev/spec/51/
1184
+ * @param contentTopic String to validate
1185
+ * @returns Object with each content topic field as an attribute
1186
+ */
1187
+ function ensureValidContentTopic(contentTopic) {
1188
+ const parts = contentTopic.split("/");
1189
+ if (parts.length < 5 || parts.length > 6) {
1190
+ throw Error("Content topic format is invalid");
1191
+ }
1192
+ // Validate generation field if present
1193
+ let generation = 0;
1194
+ if (parts.length == 6) {
1195
+ generation = parseInt(parts[1]);
1196
+ if (isNaN(generation)) {
1197
+ throw new Error("Invalid generation field in content topic");
1198
+ }
1199
+ if (generation > 0) {
1200
+ throw new Error("Generation greater than 0 is not supported");
1201
+ }
1202
+ }
1203
+ // Validate remaining fields
1204
+ const fields = parts.splice(-4);
1205
+ // Validate application field
1206
+ if (fields[0].length == 0) {
1207
+ throw new Error("Application field cannot be empty");
1208
+ }
1209
+ // Validate version field
1210
+ if (fields[1].length == 0) {
1211
+ throw new Error("Version field cannot be empty");
1212
+ }
1213
+ // Validate topic name field
1214
+ if (fields[2].length == 0) {
1215
+ throw new Error("Topic name field cannot be empty");
1216
+ }
1217
+ // Validate encoding field
1218
+ if (fields[3].length == 0) {
1219
+ throw new Error("Encoding field cannot be empty");
1220
+ }
1221
+ return {
1222
+ generation,
1223
+ application: fields[0],
1224
+ version: fields[1],
1225
+ topicName: fields[2],
1226
+ encoding: fields[3]
1227
+ };
1228
+ }
1229
+ /**
1230
+ * Given a string, determines which autoshard index to use for its pubsub topic.
1231
+ * Based on the algorithm described in the RFC: https://rfc.vac.dev/spec/51//#algorithm
1232
+ */
1233
+ function contentTopicToShardIndex(contentTopic, networkShards = 8) {
1234
+ const { application, version } = ensureValidContentTopic(contentTopic);
1235
+ const digest = sha256(concat([utf8ToBytes(application), utf8ToBytes(version)]));
1236
+ const dataview = new DataView(digest.buffer.slice(-8));
1237
+ return Number(dataview.getBigUint64(0, false) % BigInt(networkShards));
1238
+ }
1239
+ function contentTopicToPubsubTopic(contentTopic, clusterId = DEFAULT_CLUSTER_ID, networkShards = 8) {
1240
+ if (!contentTopic) {
1241
+ throw Error("Content topic must be specified");
1242
+ }
1243
+ const shardIndex = contentTopicToShardIndex(contentTopic, networkShards);
1244
+ return `/waku/2/rs/${clusterId}/${shardIndex}`;
1245
+ }
1246
+ /**
1247
+ * Used when creating encoders/decoders to determine which pubsub topic to use
1248
+ */
1249
+ function determinePubsubTopic(contentTopic, pubsubTopicShardInfo) {
1250
+ if (typeof pubsubTopicShardInfo == "string") {
1251
+ return pubsubTopicShardInfo;
1252
+ }
1253
+ return pubsubTopicShardInfo?.shard !== undefined
1254
+ ? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
1255
+ : contentTopicToPubsubTopic(contentTopic, pubsubTopicShardInfo?.clusterId ?? DEFAULT_CLUSTER_ID);
1256
+ }
1257
+ /**
1258
+ * Validates sharding configuration and sets defaults where possible.
1259
+ * @returns Validated sharding parameters, with any missing values set to defaults
1260
+ */
1261
+ const ensureShardingConfigured = (shardInfo) => {
1262
+ const clusterId = shardInfo.clusterId ?? DEFAULT_CLUSTER_ID;
1263
+ const shards = "shards" in shardInfo ? shardInfo.shards : [];
1264
+ const contentTopics = "contentTopics" in shardInfo ? shardInfo.contentTopics : [];
1265
+ const [application, version] = "application" in shardInfo && "version" in shardInfo
1266
+ ? [shardInfo.application, shardInfo.version]
1267
+ : [undefined, undefined];
1268
+ const isShardsConfigured = shards && shards.length > 0;
1269
+ const isContentTopicsConfigured = contentTopics && contentTopics.length > 0;
1270
+ const isApplicationVersionConfigured = application && version;
1271
+ if (isShardsConfigured) {
1272
+ return {
1273
+ shardingParams: { clusterId, shards },
1274
+ shardInfo: { clusterId, shards },
1275
+ pubsubTopics: shardInfoToPubsubTopics({ clusterId, shards })
1276
+ };
1277
+ }
1278
+ if (isContentTopicsConfigured) {
1279
+ const pubsubTopics = Array.from(new Set(contentTopics.map((topic) => contentTopicToPubsubTopic(topic, clusterId))));
1280
+ const shards = Array.from(new Set(contentTopics.map((topic) => contentTopicToShardIndex(topic))));
1281
+ return {
1282
+ shardingParams: { clusterId, contentTopics },
1283
+ shardInfo: { clusterId, shards },
1284
+ pubsubTopics
1285
+ };
1286
+ }
1287
+ if (isApplicationVersionConfigured) {
1288
+ const pubsubTopic = contentTopicToPubsubTopic(`/${application}/${version}/default/default`, clusterId);
1289
+ return {
1290
+ shardingParams: { clusterId, application, version },
1291
+ shardInfo: {
1292
+ clusterId,
1293
+ shards: [pubsubTopicToSingleShardInfo(pubsubTopic).shard]
1294
+ },
1295
+ pubsubTopics: [pubsubTopic]
1296
+ };
1297
+ }
1298
+ throw new Error("Missing minimum required configuration options for static sharding or autosharding.");
1299
+ };
1300
+
718
1301
  function getDefaultExportFromCjs (x) {
719
1302
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
720
1303
  }
@@ -1441,4 +2024,32 @@ var common = setup;
1441
2024
  var browserExports = browser.exports;
1442
2025
  var debug = /*@__PURE__*/getDefaultExportFromCjs(browserExports);
1443
2026
 
1444
- export { EConnectionStateEvents as E, ProtocolError as P, Tags as T, Protocols as a, EPeersByDiscoveryEvents as b, base2$1 as c, base8$1 as d, base10$1 as e, base16$1 as f, getDefaultExportFromCjs as g, base32$1 as h, identityBase as i, base36$1 as j, base58 as k, base64$1 as l, base256emoji$1 as m, debug as n };
2027
+ const APP_NAME = "waku";
2028
+ class Logger {
2029
+ _info;
2030
+ _warn;
2031
+ _error;
2032
+ static createDebugNamespace(level, prefix) {
2033
+ return prefix ? `${APP_NAME}:${level}:${prefix}` : `${APP_NAME}:${level}`;
2034
+ }
2035
+ constructor(prefix) {
2036
+ this._info = debug(Logger.createDebugNamespace("info", prefix));
2037
+ this._warn = debug(Logger.createDebugNamespace("warn", prefix));
2038
+ this._error = debug(Logger.createDebugNamespace("error", prefix));
2039
+ }
2040
+ get info() {
2041
+ return this._info;
2042
+ }
2043
+ get warn() {
2044
+ return this._warn;
2045
+ }
2046
+ get error() {
2047
+ return this._error;
2048
+ }
2049
+ log(level, ...args) {
2050
+ const logger = this[level];
2051
+ logger(...args);
2052
+ }
2053
+ }
2054
+
2055
+ export { EConnectionStateEvents as E, Logger as L, ProtocolError as P, Tags as T, allocUnsafe as a, alloc as b, Protocols as c, EPeersByDiscoveryEvents as d, determinePubsubTopic as e, fromString as f, getDefaultExportFromCjs as g, bytesToUtf8 as h, ensureShardingConfigured as i, pubsubTopicToSingleShardInfo as p, shardInfoToPubsubTopics as s, utf8ToBytes as u };