@waku/core 0.0.26 → 0.0.27

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 (55) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/bundle/{base_protocol-pDODy0G6.js → base_protocol-LhsIWF3-.js} +137 -89
  3. package/bundle/{browser-mTOOnVZp.js → browser-BQyFvtq6.js} +501 -700
  4. package/bundle/{index-cmONXM-V.js → index-8YyfzF9R.js} +96 -41
  5. package/bundle/index.js +3033 -21649
  6. package/bundle/lib/base_protocol.js +3 -3
  7. package/bundle/lib/message/version_0.js +3 -3
  8. package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
  9. package/bundle/{version_0-LQTFNC7k.js → version_0-FXfzO8Km.js} +1246 -2444
  10. package/dist/.tsbuildinfo +1 -1
  11. package/dist/index.d.ts +1 -5
  12. package/dist/index.js +1 -5
  13. package/dist/index.js.map +1 -1
  14. package/dist/lib/base_protocol.d.ts +13 -10
  15. package/dist/lib/base_protocol.js +38 -22
  16. package/dist/lib/base_protocol.js.map +1 -1
  17. package/dist/lib/connection_manager.d.ts +2 -2
  18. package/dist/lib/connection_manager.js +16 -6
  19. package/dist/lib/connection_manager.js.map +1 -1
  20. package/dist/lib/filter/index.d.ts +1 -1
  21. package/dist/lib/filter/index.js +140 -82
  22. package/dist/lib/filter/index.js.map +1 -1
  23. package/dist/lib/filterPeers.d.ts +8 -5
  24. package/dist/lib/filterPeers.js +12 -5
  25. package/dist/lib/filterPeers.js.map +1 -1
  26. package/dist/lib/keep_alive_manager.d.ts +2 -3
  27. package/dist/lib/keep_alive_manager.js.map +1 -1
  28. package/dist/lib/light_push/index.js +3 -6
  29. package/dist/lib/light_push/index.js.map +1 -1
  30. package/dist/lib/metadata/index.d.ts +2 -2
  31. package/dist/lib/metadata/index.js +25 -11
  32. package/dist/lib/metadata/index.js.map +1 -1
  33. package/dist/lib/store/index.js +1 -3
  34. package/dist/lib/store/index.js.map +1 -1
  35. package/dist/lib/stream_manager.d.ts +2 -2
  36. package/dist/lib/stream_manager.js.map +1 -1
  37. package/dist/lib/wait_for_remote_peer.d.ts +1 -1
  38. package/dist/lib/wait_for_remote_peer.js +40 -10
  39. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  40. package/package.json +14 -12
  41. package/src/index.ts +1 -6
  42. package/src/lib/base_protocol.ts +59 -34
  43. package/src/lib/connection_manager.ts +17 -10
  44. package/src/lib/filter/index.ts +228 -137
  45. package/src/lib/filterPeers.ts +15 -7
  46. package/src/lib/keep_alive_manager.ts +2 -3
  47. package/src/lib/light_push/index.ts +11 -10
  48. package/src/lib/metadata/index.ts +51 -19
  49. package/src/lib/store/index.ts +3 -6
  50. package/src/lib/stream_manager.ts +2 -3
  51. package/src/lib/wait_for_remote_peer.ts +58 -12
  52. package/dist/lib/waku.d.ts +0 -57
  53. package/dist/lib/waku.js +0 -130
  54. package/dist/lib/waku.js.map +0 -1
  55. package/src/lib/waku.ts +0 -214
@@ -1,9 +1,4 @@
1
- import { i as identityBase, j as base2, k as base8, l as base10, m as base16, n as base32, o as base36, p as base58, q as base64, r as base256emoji, s as debug } from './browser-mTOOnVZp.js';
2
-
3
- // @ts-check
4
-
5
-
6
- const bases = { ...identityBase, ...base2, ...base8, ...base10, ...base16, ...base32, ...base36, ...base58, ...base64, ...base256emoji };
1
+ import { i as identityBase, b as base2, c as base8, d as base10, e as base16, f as base32, h as base36, j as base58, k as base64, l as base256emoji, m as debug } from './browser-BQyFvtq6.js';
7
2
 
8
3
  /**
9
4
  * To guarantee Uint8Array semantics, convert nodejs Buffers
@@ -38,6 +33,8 @@ function allocUnsafe(size = 0) {
38
33
  return new Uint8Array(size);
39
34
  }
40
35
 
36
+ const bases = { ...identityBase, ...base2, ...base8, ...base10, ...base16, ...base32, ...base36, ...base58, ...base64, ...base256emoji };
37
+
41
38
  function createCodec(name, prefix, encode, decode) {
42
39
  return {
43
40
  name,
@@ -83,25 +80,6 @@ const BASES = {
83
80
  ...bases
84
81
  };
85
82
 
86
- /**
87
- * Turns a `Uint8Array` into a string.
88
- *
89
- * Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
90
- *
91
- * Also `ascii` which is similar to node's 'binary' encoding.
92
- */
93
- function toString(array, encoding = 'utf8') {
94
- const base = BASES[encoding];
95
- if (base == null) {
96
- throw new Error(`Unsupported encoding "${encoding}"`);
97
- }
98
- if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) {
99
- return globalThis.Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8');
100
- }
101
- // strip multibase prefix
102
- return base.encoder.encode(array).substring(1);
103
- }
104
-
105
83
  /**
106
84
  * Create a `Uint8Array` from the passed string
107
85
  *
@@ -121,13 +99,13 @@ function fromString(string, encoding = 'utf8') {
121
99
  return base.decoder.decode(`${base.prefix}${string}`); // eslint-disable-line @typescript-eslint/restrict-template-expressions
122
100
  }
123
101
 
124
- /**
125
- * DefaultPubsubTopic is the default gossipsub topic to use for Waku.
126
- */
127
- const DefaultPubsubTopic = "/waku/2/default-waku/proto";
128
-
102
+ // copied from utils
103
+ function isBytes$1(a) {
104
+ return (a instanceof Uint8Array ||
105
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
106
+ }
129
107
  function bytes(b, ...lengths) {
130
- if (!(b instanceof Uint8Array))
108
+ if (!isBytes$1(b))
131
109
  throw new Error('Expected Uint8Array');
132
110
  if (lengths.length > 0 && !lengths.includes(b.length))
133
111
  throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
@@ -152,14 +130,19 @@ function output(out, instance) {
152
130
  // For node.js, package.json#exports field mapping rewrites import
153
131
  // from `crypto` to `cryptoNode`, which imports native module.
154
132
  // Makes the utils un-importable in browsers without a bundler.
155
- // Once node.js 18 is deprecated, we can just drop the import.
156
- const u8a = (a) => a instanceof Uint8Array;
133
+ // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
134
+ function isBytes(a) {
135
+ return (a instanceof Uint8Array ||
136
+ (a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
137
+ }
157
138
  // Cast array to view
158
139
  const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
159
140
  // The rotate right (circular right shift) operation for uint32
160
141
  const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
161
142
  // big-endian hardware is rare. Just in case someone still decides to run hashes:
162
143
  // early-throw an error because we don't support BE yet.
144
+ // Other libraries would silently corrupt the data instead of throwing an error,
145
+ // when they don't support it.
163
146
  const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
164
147
  if (!isLE)
165
148
  throw new Error('Non little-endian hardware is not supported');
@@ -179,7 +162,7 @@ function utf8ToBytes$1(str) {
179
162
  function toBytes(data) {
180
163
  if (typeof data === 'string')
181
164
  data = utf8ToBytes$1(data);
182
- if (!u8a(data))
165
+ if (!isBytes(data))
183
166
  throw new Error(`expected Uint8Array, got ${typeof data}`);
184
167
  return data;
185
168
  }
@@ -419,6 +402,34 @@ class SHA256 extends SHA2 {
419
402
  */
420
403
  const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
421
404
 
405
+ /**
406
+ * DefaultPubsubTopic is the default gossipsub topic to use for Waku.
407
+ */
408
+ const DefaultPubsubTopic = "/waku/2/default-waku/proto";
409
+ /**
410
+ * The default cluster ID for The Waku Network
411
+ */
412
+ const DEFAULT_CLUSTER_ID = 1;
413
+
414
+ /**
415
+ * Turns a `Uint8Array` into a string.
416
+ *
417
+ * Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
418
+ *
419
+ * Also `ascii` which is similar to node's 'binary' encoding.
420
+ */
421
+ function toString(array, encoding = 'utf8') {
422
+ const base = BASES[encoding];
423
+ if (base == null) {
424
+ throw new Error(`Unsupported encoding "${encoding}"`);
425
+ }
426
+ if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) {
427
+ return globalThis.Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8');
428
+ }
429
+ // strip multibase prefix
430
+ return base.encoder.encode(array).substring(1);
431
+ }
432
+
422
433
  /**
423
434
  * Decode byte array to utf-8 string.
424
435
  */
@@ -447,9 +458,7 @@ const singleShardInfoToPubsubTopic = (shardInfo) => {
447
458
  return `/waku/2/rs/${shardInfo.clusterId}/${shardInfo.shard}`;
448
459
  };
449
460
  const shardInfoToPubsubTopics = (shardInfo) => {
450
- if (shardInfo.clusterId === undefined)
451
- throw new Error("Cluster ID must be specified");
452
- if ("contentTopics" in shardInfo) {
461
+ if ("contentTopics" in shardInfo && shardInfo.contentTopics) {
453
462
  // Autosharding: explicitly defined content topics
454
463
  return Array.from(new Set(shardInfo.contentTopics.map((contentTopic) => contentTopicToPubsubTopic(contentTopic, shardInfo.clusterId))));
455
464
  }
@@ -457,14 +466,17 @@ const shardInfoToPubsubTopics = (shardInfo) => {
457
466
  // Static sharding
458
467
  if (shardInfo.shards === undefined)
459
468
  throw new Error("Invalid shard");
460
- return Array.from(new Set(shardInfo.shards.map((index) => `/waku/2/rs/${shardInfo.clusterId}/${index}`)));
469
+ return Array.from(new Set(shardInfo.shards.map((index) => `/waku/2/rs/${shardInfo.clusterId ?? DEFAULT_CLUSTER_ID}/${index}`)));
461
470
  }
462
- else {
471
+ else if ("application" in shardInfo && "version" in shardInfo) {
463
472
  // Autosharding: single shard from application and version
464
473
  return [
465
474
  contentTopicToPubsubTopic(`/${shardInfo.application}/${shardInfo.version}/default/default`)
466
475
  ];
467
476
  }
477
+ else {
478
+ throw new Error("Missing required configuration in shard parameters");
479
+ }
468
480
  };
469
481
  const pubsubTopicToSingleShardInfo = (pubsubTopics) => {
470
482
  const parts = pubsubTopics.split("/");
@@ -544,7 +556,7 @@ function contentTopicToShardIndex(contentTopic, networkShards = 8) {
544
556
  const dataview = new DataView(digest.buffer.slice(-8));
545
557
  return Number(dataview.getBigUint64(0, false) % BigInt(networkShards));
546
558
  }
547
- function contentTopicToPubsubTopic(contentTopic, clusterId = 1, networkShards = 8) {
559
+ function contentTopicToPubsubTopic(contentTopic, clusterId = DEFAULT_CLUSTER_ID, networkShards = 8) {
548
560
  const shardIndex = contentTopicToShardIndex(contentTopic, networkShards);
549
561
  return `/waku/2/rs/${clusterId}/${shardIndex}`;
550
562
  }
@@ -563,6 +575,49 @@ function determinePubsubTopic(contentTopic, pubsubTopicShardInfo = DefaultPubsub
563
575
  : DefaultPubsubTopic;
564
576
  }
565
577
  }
578
+ /**
579
+ * Validates sharding configuration and sets defaults where possible.
580
+ * @returns Validated sharding parameters, with any missing values set to defaults
581
+ */
582
+ const ensureShardingConfigured = (shardInfo) => {
583
+ const clusterId = shardInfo.clusterId ?? DEFAULT_CLUSTER_ID;
584
+ const shards = "shards" in shardInfo ? shardInfo.shards : [];
585
+ const contentTopics = "contentTopics" in shardInfo ? shardInfo.contentTopics : [];
586
+ const [application, version] = "application" in shardInfo && "version" in shardInfo
587
+ ? [shardInfo.application, shardInfo.version]
588
+ : [undefined, undefined];
589
+ const isShardsConfigured = shards && shards.length > 0;
590
+ const isContentTopicsConfigured = contentTopics && contentTopics.length > 0;
591
+ const isApplicationVersionConfigured = application && version;
592
+ if (isShardsConfigured) {
593
+ return {
594
+ shardingParams: { clusterId, shards },
595
+ shardInfo: { clusterId, shards },
596
+ pubsubTopics: shardInfoToPubsubTopics({ clusterId, shards })
597
+ };
598
+ }
599
+ if (isContentTopicsConfigured) {
600
+ const pubsubTopics = Array.from(new Set(contentTopics.map((topic) => contentTopicToPubsubTopic(topic, clusterId))));
601
+ const shards = Array.from(new Set(contentTopics.map((topic) => contentTopicToShardIndex(topic))));
602
+ return {
603
+ shardingParams: { clusterId, contentTopics },
604
+ shardInfo: { clusterId, shards },
605
+ pubsubTopics
606
+ };
607
+ }
608
+ if (isApplicationVersionConfigured) {
609
+ const pubsubTopic = contentTopicToPubsubTopic(`/${application}/${version}/default/default`);
610
+ return {
611
+ shardingParams: { clusterId, application, version },
612
+ shardInfo: {
613
+ clusterId,
614
+ shards: [pubsubTopicToSingleShardInfo(pubsubTopic).shard]
615
+ },
616
+ pubsubTopics: [pubsubTopic]
617
+ };
618
+ }
619
+ throw new Error("Missing minimum required configuration options for static sharding or autosharding.");
620
+ };
566
621
 
567
622
  const APP_NAME = "waku";
568
623
  class Logger {
@@ -592,4 +647,4 @@ class Logger {
592
647
  }
593
648
  }
594
649
 
595
- export { DefaultPubsubTopic as D, Logger as L, allocUnsafe as a, asUint8Array as b, bases as c, alloc as d, singleShardInfoToPubsubTopic as e, fromString as f, ensurePubsubTopicIsConfigured as g, sha256 as h, concat as i, determinePubsubTopic as j, bytesToUtf8 as k, pubsubTopicToSingleShardInfo as p, shardInfoToPubsubTopics as s, toString as t, utf8ToBytes as u };
650
+ export { DefaultPubsubTopic as D, Logger as L, asUint8Array as a, bytesToUtf8 as b, concat as c, allocUnsafe as d, alloc as e, singleShardInfoToPubsubTopic as f, ensurePubsubTopicIsConfigured as g, shardInfoToPubsubTopics as h, fromString as i, determinePubsubTopic as j, ensureShardingConfigured as k, pubsubTopicToSingleShardInfo as p, sha256 as s, utf8ToBytes as u };