@waku/core 0.0.27 → 0.0.28-b5e8b17.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.
@@ -1,5 +1,5 @@
1
- import { b as bytesToUtf8, L as Logger, k as ensureShardingConfigured } from './index-8YyfzF9R.js';
2
- import { T as Tags } from './browser-BQyFvtq6.js';
1
+ import { b as bytesToUtf8, L as Logger, k as ensureShardingConfigured } from './index-vlQahmUj.js';
2
+ import { T as Tags } from './browser-DoQRY-an.js';
3
3
 
4
4
  const decodeRelayShard = (bytes) => {
5
5
  // explicitly converting to Uint8Array to avoid Buffer
@@ -229,7 +229,6 @@ class StreamManager {
229
229
  };
230
230
  }
231
231
 
232
- const DEFAULT_NUM_PEERS_TO_USE = 3;
233
232
  /**
234
233
  * A class with predefined helpers, to be used as a base to implement Waku
235
234
  * Protocols.
@@ -242,7 +241,6 @@ class BaseProtocol {
242
241
  options;
243
242
  addLibp2pEventListener;
244
243
  removeLibp2pEventListener;
245
- numPeersToUse;
246
244
  streamManager;
247
245
  constructor(multicodec, components, log, pubsubTopics, options) {
248
246
  this.multicodec = multicodec;
@@ -250,7 +248,6 @@ class BaseProtocol {
250
248
  this.log = log;
251
249
  this.pubsubTopics = pubsubTopics;
252
250
  this.options = options;
253
- this.numPeersToUse = options?.numPeersToUse ?? DEFAULT_NUM_PEERS_TO_USE;
254
251
  this.addLibp2pEventListener = components.events.addEventListener.bind(components.events);
255
252
  this.removeLibp2pEventListener = components.events.removeEventListener.bind(components.events);
256
253
  this.streamManager = new StreamManager(multicodec, components.connectionManager.getConnections.bind(components.connectionManager), this.addLibp2pEventListener);
@@ -627,54 +627,59 @@ var Protocols;
627
627
  Protocols["LightPush"] = "lightpush";
628
628
  Protocols["Filter"] = "filter";
629
629
  })(Protocols || (Protocols = {}));
630
- var SendError;
631
- (function (SendError) {
630
+ var ProtocolError;
631
+ (function (ProtocolError) {
632
632
  /** Could not determine the origin of the fault. Best to check connectivity and try again */
633
- SendError["GENERIC_FAIL"] = "Generic error";
633
+ ProtocolError["GENERIC_FAIL"] = "Generic error";
634
634
  /**
635
635
  * Failure to protobuf encode the message. This is not recoverable and needs
636
636
  * further investigation.
637
637
  */
638
- SendError["ENCODE_FAILED"] = "Failed to encode";
638
+ ProtocolError["ENCODE_FAILED"] = "Failed to encode";
639
639
  /**
640
640
  * Failure to protobuf decode the message. May be due to a remote peer issue,
641
641
  * ensuring that messages are sent via several peer enable mitigation of this error.
642
642
  */
643
- SendError["DECODE_FAILED"] = "Failed to decode";
643
+ ProtocolError["DECODE_FAILED"] = "Failed to decode";
644
644
  /**
645
645
  * The message payload is empty, making the message invalid. Ensure that a non-empty
646
646
  * payload is set on the outgoing message.
647
647
  */
648
- SendError["EMPTY_PAYLOAD"] = "Payload is empty";
648
+ ProtocolError["EMPTY_PAYLOAD"] = "Payload is empty";
649
649
  /**
650
650
  * The message size is above the maximum message size allowed on the Waku Network.
651
651
  * Compressing the message or using an alternative strategy for large messages is recommended.
652
652
  */
653
- SendError["SIZE_TOO_BIG"] = "Size is too big";
653
+ ProtocolError["SIZE_TOO_BIG"] = "Size is too big";
654
654
  /**
655
655
  * The PubsubTopic passed to the send function is not configured on the Waku node.
656
656
  * Please ensure that the PubsubTopic is used when initializing the Waku node.
657
657
  */
658
- SendError["TOPIC_NOT_CONFIGURED"] = "Topic not configured";
658
+ ProtocolError["TOPIC_NOT_CONFIGURED"] = "Topic not configured";
659
659
  /**
660
660
  * Failure to find a peer with suitable protocols. This may due to a connection issue.
661
661
  * Mitigation can be: retrying after a given time period, display connectivity issue
662
662
  * to user or listening for `peer:connected:bootstrap` or `peer:connected:peer-exchange`
663
663
  * on the connection manager before retrying.
664
664
  */
665
- SendError["NO_PEER_AVAILABLE"] = "No peer available";
665
+ ProtocolError["NO_PEER_AVAILABLE"] = "No peer available";
666
666
  /**
667
667
  * The remote peer did not behave as expected. Mitigation for `NO_PEER_AVAILABLE`
668
668
  * or `DECODE_FAILED` can be used.
669
669
  */
670
- SendError["REMOTE_PEER_FAULT"] = "Remote peer fault";
670
+ ProtocolError["REMOTE_PEER_FAULT"] = "Remote peer fault";
671
671
  /**
672
672
  * The remote peer rejected the message. Information provided by the remote peer
673
673
  * is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE`
674
674
  * or `DECODE_FAILED` can be used.
675
675
  */
676
- SendError["REMOTE_PEER_REJECTED"] = "Remote peer rejected";
677
- })(SendError || (SendError = {}));
676
+ ProtocolError["REMOTE_PEER_REJECTED"] = "Remote peer rejected";
677
+ /**
678
+ * The protocol request timed out without a response. This may be due to a connection issue.
679
+ * Mitigation can be: retrying after a given time period
680
+ */
681
+ ProtocolError["REQUEST_TIMEOUT"] = "Request timeout";
682
+ })(ProtocolError || (ProtocolError = {}));
678
683
 
679
684
  var PageDirection;
680
685
  (function (PageDirection) {
@@ -1426,4 +1431,4 @@ var common = setup;
1426
1431
  var browserExports = browser.exports;
1427
1432
  var debug = /*@__PURE__*/getDefaultExportFromCjs(browserExports);
1428
1433
 
1429
- export { EConnectionStateEvents as E, Protocols as P, SendError as S, Tags as T, EPeersByDiscoveryEvents as a, base2$1 as b, base8$1 as c, base10$1 as d, base16$1 as e, base32$1 as f, getDefaultExportFromCjs as g, base36$1 as h, identityBase as i, base58 as j, base64$1 as k, base256emoji$1 as l, debug as m };
1434
+ 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 };
@@ -1,4 +1,4 @@
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';
1
+ import { i as identityBase, c as base2, d as base8, e as base10, f as base16, h as base32, j as base36, k as base58, l as base64, m as base256emoji, n as debug } from './browser-DoQRY-an.js';
2
2
 
3
3
  /**
4
4
  * To guarantee Uint8Array semantics, convert nodejs Buffers
@@ -494,6 +494,8 @@ const pubsubTopicToSingleShardInfo = (pubsubTopics) => {
494
494
  shard
495
495
  };
496
496
  };
497
+ //TODO: move part of BaseProtocol instead of utils
498
+ // return `ProtocolError.TOPIC_NOT_CONFIGURED` instead of throwing
497
499
  function ensurePubsubTopicIsConfigured(pubsubTopic, configuredTopics) {
498
500
  if (!configuredTopics.includes(pubsubTopic)) {
499
501
  throw new Error(`Pubsub topic ${pubsubTopic} has not been configured on this instance. Configured topics are: ${configuredTopics}. Please update your configuration by passing in the topic during Waku node instantiation.`);
package/bundle/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { v as version_0, e as encodingLength, a as encode$1, d as decode$1, M as MessagePush, F as FilterSubscribeRequest, b as FilterSubscribeResponse$1, P as PushRpc$1, c as PushResponse, H as HistoryRpc$1, f as PagingInfo, g as HistoryResponse, h as createEncoder, W as WakuMetadataResponse, i as WakuMetadataRequest } from './version_0-FXfzO8Km.js';
2
- export { j as createDecoder } from './version_0-FXfzO8Km.js';
3
- import { g as getDefaultExportFromCjs, S as SendError, P as Protocols, E as EConnectionStateEvents, T as Tags, a as EPeersByDiscoveryEvents } from './browser-BQyFvtq6.js';
4
- import { b as bytesToUtf8, u as utf8ToBytes, c as concat$1, s as sha256, a as asUint8Array, d as allocUnsafe, e as alloc, L as Logger, f as singleShardInfoToPubsubTopic, g as ensurePubsubTopicIsConfigured, D as DefaultPubsubTopic, p as pubsubTopicToSingleShardInfo, h as shardInfoToPubsubTopics } from './index-8YyfzF9R.js';
5
- import { B as BaseProtocol, d as decodeRelayShard, e as encodeRelayShard } from './base_protocol-LhsIWF3-.js';
6
- export { S as StreamManager } from './base_protocol-LhsIWF3-.js';
1
+ import { v as version_0, e as encodingLength, a as encode$1, d as decode$1, M as MessagePush, F as FilterSubscribeRequest, b as FilterSubscribeResponse$1, P as PushRpc$1, c as PushResponse, H as HistoryRpc$1, f as PagingInfo, g as HistoryResponse, h as createEncoder, W as WakuMetadataResponse, i as WakuMetadataRequest } from './version_0-DiakMc1A.js';
2
+ export { j as createDecoder } from './version_0-DiakMc1A.js';
3
+ import { g as getDefaultExportFromCjs, P as ProtocolError, a as Protocols, E as EConnectionStateEvents, T as Tags, b as EPeersByDiscoveryEvents } from './browser-DoQRY-an.js';
4
+ import { b as bytesToUtf8, u as utf8ToBytes, c as concat$1, s as sha256, a as asUint8Array, d as allocUnsafe, e as alloc, L as Logger, f as singleShardInfoToPubsubTopic, g as ensurePubsubTopicIsConfigured, D as DefaultPubsubTopic, p as pubsubTopicToSingleShardInfo, h as shardInfoToPubsubTopics } from './index-vlQahmUj.js';
5
+ import { B as BaseProtocol, d as decodeRelayShard, e as encodeRelayShard } from './base_protocol-BCwLeb-A.js';
6
+ export { S as StreamManager } from './base_protocol-BCwLeb-A.js';
7
7
 
8
8
  function isDefined(value) {
9
9
  return Boolean(value);
@@ -1827,6 +1827,7 @@ class Subscription {
1827
1827
  }
1828
1828
  }
1829
1829
  }
1830
+ const DEFAULT_NUM_PEERS = 3;
1830
1831
  class Filter extends BaseProtocol {
1831
1832
  activeSubscriptions = new Map();
1832
1833
  getActiveSubscription(pubsubTopic) {
@@ -1836,8 +1837,11 @@ class Filter extends BaseProtocol {
1836
1837
  this.activeSubscriptions.set(pubsubTopic, subscription);
1837
1838
  return subscription;
1838
1839
  }
1840
+ //TODO: Remove when FilterCore and FilterSDK are introduced
1841
+ numPeersToUse;
1839
1842
  constructor(libp2p, options) {
1840
1843
  super(FilterCodecs.SUBSCRIBE, libp2p.components, log$6, options.pubsubTopics, options);
1844
+ this.numPeersToUse = options?.numPeersToUse ?? DEFAULT_NUM_PEERS;
1841
1845
  libp2p.handle(FilterCodecs.PUSH, this.onRequest.bind(this)).catch((e) => {
1842
1846
  log$6.error("Failed to register ", FilterCodecs.PUSH, e);
1843
1847
  });
@@ -1990,122 +1994,125 @@ const LightPushCodec = "/vac/waku/lightpush/2.0.0-beta1";
1990
1994
  /**
1991
1995
  * Implements the [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
1992
1996
  */
1993
- class LightPush extends BaseProtocol {
1997
+ class LightPushCore extends BaseProtocol {
1994
1998
  constructor(libp2p, options) {
1995
1999
  super(LightPushCodec, libp2p.components, log$5, options.pubsubTopics, options);
1996
2000
  }
1997
- async preparePushMessage(encoder, message, pubsubTopic) {
2001
+ async preparePushMessage(encoder, message) {
1998
2002
  try {
1999
2003
  if (!message.payload || message.payload.length === 0) {
2000
2004
  log$5.error("Failed to send waku light push: payload is empty");
2001
- return { query: null, error: SendError.EMPTY_PAYLOAD };
2005
+ return { query: null, error: ProtocolError.EMPTY_PAYLOAD };
2002
2006
  }
2003
2007
  if (!(await isMessageSizeUnderCap(encoder, message))) {
2004
2008
  log$5.error("Failed to send waku light push: message is bigger than 1MB");
2005
- return { query: null, error: SendError.SIZE_TOO_BIG };
2009
+ return { query: null, error: ProtocolError.SIZE_TOO_BIG };
2006
2010
  }
2007
2011
  const protoMessage = await encoder.toProtoObj(message);
2008
2012
  if (!protoMessage) {
2009
2013
  log$5.error("Failed to encode to protoMessage, aborting push");
2010
2014
  return {
2011
2015
  query: null,
2012
- error: SendError.ENCODE_FAILED
2016
+ error: ProtocolError.ENCODE_FAILED
2013
2017
  };
2014
2018
  }
2015
- const query = PushRpc.createRequest(protoMessage, pubsubTopic);
2019
+ const query = PushRpc.createRequest(protoMessage, encoder.pubsubTopic);
2016
2020
  return { query, error: null };
2017
2021
  }
2018
2022
  catch (error) {
2019
2023
  log$5.error("Failed to prepare push message", error);
2020
2024
  return {
2021
2025
  query: null,
2022
- error: SendError.GENERIC_FAIL
2026
+ error: ProtocolError.GENERIC_FAIL
2023
2027
  };
2024
2028
  }
2025
2029
  }
2026
- async send(encoder, message) {
2027
- const { pubsubTopic } = encoder;
2028
- ensurePubsubTopicIsConfigured(pubsubTopic, this.pubsubTopics);
2029
- const recipients = [];
2030
- const { query, error: preparationError } = await this.preparePushMessage(encoder, message, pubsubTopic);
2030
+ async send(encoder, message, peer) {
2031
+ const { query, error: preparationError } = await this.preparePushMessage(encoder, message);
2031
2032
  if (preparationError || !query) {
2032
2033
  return {
2033
- recipients,
2034
- errors: [preparationError]
2034
+ success: null,
2035
+ failure: {
2036
+ error: preparationError,
2037
+ peerId: peer.id
2038
+ }
2035
2039
  };
2036
2040
  }
2037
- const peers = await this.getPeers({
2038
- maxBootstrapPeers: 1,
2039
- numPeers: this.numPeersToUse
2040
- });
2041
- if (!peers.length) {
2041
+ let stream;
2042
+ try {
2043
+ stream = await this.getStream(peer);
2044
+ }
2045
+ catch (err) {
2046
+ log$5.error(`Failed to get a stream for remote peer${peer.id.toString()}`, err);
2042
2047
  return {
2043
- recipients,
2044
- errors: [SendError.NO_PEER_AVAILABLE]
2048
+ success: null,
2049
+ failure: {
2050
+ error: ProtocolError.REMOTE_PEER_FAULT,
2051
+ peerId: peer.id
2052
+ }
2045
2053
  };
2046
2054
  }
2047
- const promises = peers.map(async (peer) => {
2048
- let stream;
2049
- try {
2050
- stream = await this.getStream(peer);
2051
- }
2052
- catch (err) {
2053
- log$5.error(`Failed to get a stream for remote peer${peer.id.toString()}`, err);
2054
- return { recipients, error: SendError.REMOTE_PEER_FAULT };
2055
- }
2056
- let res;
2057
- try {
2058
- res = await pipe([query.encode()], encode, stream, decode, async (source) => await all(source));
2059
- }
2060
- catch (err) {
2061
- log$5.error("Failed to send waku light push request", err);
2062
- return { recipients, error: SendError.GENERIC_FAIL };
2063
- }
2064
- const bytes = new Uint8ArrayList();
2065
- res.forEach((chunk) => {
2066
- bytes.append(chunk);
2067
- });
2068
- let response;
2069
- try {
2070
- response = PushRpc.decode(bytes).response;
2071
- }
2072
- catch (err) {
2073
- log$5.error("Failed to decode push reply", err);
2074
- return { recipients, error: SendError.DECODE_FAILED };
2075
- }
2076
- if (!response) {
2077
- log$5.error("Remote peer fault: No response in PushRPC");
2078
- return { recipients, error: SendError.REMOTE_PEER_FAULT };
2079
- }
2080
- if (!response.isSuccess) {
2081
- log$5.error("Remote peer rejected the message: ", response.info);
2082
- return { recipients, error: SendError.REMOTE_PEER_REJECTED };
2083
- }
2084
- recipients.some((recipient) => recipient.equals(peer.id)) ||
2085
- recipients.push(peer.id);
2086
- return { recipients };
2055
+ let res;
2056
+ try {
2057
+ res = await pipe([query.encode()], encode, stream, decode, async (source) => await all(source));
2058
+ }
2059
+ catch (err) {
2060
+ log$5.error("Failed to send waku light push request", err);
2061
+ return {
2062
+ success: null,
2063
+ failure: {
2064
+ error: ProtocolError.GENERIC_FAIL,
2065
+ peerId: peer.id
2066
+ }
2067
+ };
2068
+ }
2069
+ const bytes = new Uint8ArrayList();
2070
+ res.forEach((chunk) => {
2071
+ bytes.append(chunk);
2087
2072
  });
2088
- const results = await Promise.allSettled(promises);
2089
- // TODO: handle renewing faulty peers with new peers (https://github.com/waku-org/js-waku/issues/1463)
2090
- const errors = results
2091
- .filter((result) => result.status === "fulfilled")
2092
- .map((result) => result.value.error)
2093
- .filter((error) => error !== undefined);
2094
- return {
2095
- recipients,
2096
- errors
2097
- };
2073
+ let response;
2074
+ try {
2075
+ response = PushRpc.decode(bytes).response;
2076
+ }
2077
+ catch (err) {
2078
+ log$5.error("Failed to decode push reply", err);
2079
+ return {
2080
+ success: null,
2081
+ failure: {
2082
+ error: ProtocolError.DECODE_FAILED,
2083
+ peerId: peer.id
2084
+ }
2085
+ };
2086
+ }
2087
+ if (!response) {
2088
+ log$5.error("Remote peer fault: No response in PushRPC");
2089
+ return {
2090
+ success: null,
2091
+ failure: {
2092
+ error: ProtocolError.REMOTE_PEER_FAULT,
2093
+ peerId: peer.id
2094
+ }
2095
+ };
2096
+ }
2097
+ if (!response.isSuccess) {
2098
+ log$5.error("Remote peer rejected the message: ", response.info);
2099
+ return {
2100
+ success: null,
2101
+ failure: {
2102
+ error: ProtocolError.REMOTE_PEER_REJECTED,
2103
+ peerId: peer.id
2104
+ }
2105
+ };
2106
+ }
2107
+ return { success: peer.id, failure: null };
2098
2108
  }
2099
2109
  }
2100
- function wakuLightPush(init = {}) {
2101
- return (libp2p) => new LightPush(libp2p, init);
2102
- }
2103
2110
 
2104
2111
  var index$1 = /*#__PURE__*/Object.freeze({
2105
2112
  __proto__: null,
2106
2113
  LightPushCodec: LightPushCodec,
2107
- get PushResponse () { return PushResponse; },
2108
- wakuLightPush: wakuLightPush
2114
+ LightPushCore: LightPushCore,
2115
+ get PushResponse () { return PushResponse; }
2109
2116
  });
2110
2117
 
2111
2118
  const EmptyMessage = {
@@ -2684,6 +2691,7 @@ function pEvent(emitter, event, options) {
2684
2691
  }
2685
2692
 
2686
2693
  const log$3 = new Logger("wait-for-remote-peer");
2694
+ //TODO: move this function within the Waku class: https://github.com/waku-org/js-waku/issues/1761
2687
2695
  /**
2688
2696
  * Wait for a remote peer to be ready given the passed protocols.
2689
2697
  * Must be used after attempting to connect to nodes, using
@@ -2721,7 +2729,7 @@ async function waitForRemotePeer(waku, protocols, timeoutMs) {
2721
2729
  if (protocols.includes(Protocols.LightPush)) {
2722
2730
  if (!waku.lightPush)
2723
2731
  throw new Error("Cannot wait for LightPush peer: protocol not mounted");
2724
- promises.push(waitForConnectedPeer(waku.lightPush, waku.libp2p.services.metadata));
2732
+ promises.push(waitForConnectedPeer(waku.lightPush.protocol, waku.libp2p.services.metadata));
2725
2733
  }
2726
2734
  if (protocols.includes(Protocols.Filter)) {
2727
2735
  if (!waku.filter)
@@ -2735,6 +2743,7 @@ async function waitForRemotePeer(waku, protocols, timeoutMs) {
2735
2743
  await Promise.all(promises);
2736
2744
  }
2737
2745
  }
2746
+ //TODO: move this function within protocol SDK class: https://github.com/waku-org/js-waku/issues/1761
2738
2747
  /**
2739
2748
  * Wait for a peer with the given protocol to be connected.
2740
2749
  * If sharding is enabled on the node, it will also wait for the peer to be confirmed by the metadata service.
@@ -2820,12 +2829,31 @@ function getEnabledProtocols(waku) {
2820
2829
  return protocols;
2821
2830
  }
2822
2831
 
2832
+ /** Noop for browser compatibility */
2833
+ function setMaxListeners$1() { }
2834
+
2835
+ // create a setMaxListeners that doesn't break browser usage
2836
+ const setMaxListeners = (n, ...eventTargets) => {
2837
+ try {
2838
+ setMaxListeners$1(n, ...eventTargets);
2839
+ }
2840
+ catch {
2841
+ // swallow error, gulp
2842
+ }
2843
+ };
2844
+
2823
2845
  /**
2824
2846
  * An implementation of a typed event target
2825
2847
  * etc
2826
2848
  */
2827
2849
  class TypedEventEmitter extends EventTarget {
2828
2850
  #listeners = new Map();
2851
+ constructor() {
2852
+ super();
2853
+ // silence MaxListenersExceededWarning warning on Node.js, this is a red
2854
+ // herring almost all of the time
2855
+ setMaxListeners(Infinity, this);
2856
+ }
2829
2857
  listenerCount(type) {
2830
2858
  const listeners = this.#listeners.get(type);
2831
2859
  if (listeners == null) {
@@ -3400,7 +3428,7 @@ const MetadataCodec = "/vac/waku/metadata/1.0.0";
3400
3428
  class Metadata extends BaseProtocol {
3401
3429
  shardInfo;
3402
3430
  libp2pComponents;
3403
- handshakesConfirmed = new Set();
3431
+ handshakesConfirmed = new Map();
3404
3432
  constructor(shardInfo, libp2p) {
3405
3433
  super(MetadataCodec, libp2p.components, log, shardInfoToPubsubTopics(shardInfo));
3406
3434
  this.shardInfo = shardInfo;
@@ -3417,8 +3445,11 @@ class Metadata extends BaseProtocol {
3417
3445
  const { stream, connection } = streamData;
3418
3446
  const encodedShardInfo = WakuMetadataResponse.encode(this.shardInfo);
3419
3447
  const encodedResponse = await pipe([encodedShardInfo], encode, stream, decode, async (source) => await all(source));
3420
- const remoteShardInfoResponse = this.decodeMetadataResponse(encodedResponse);
3421
- await this.savePeerShardInfo(connection.remotePeer, remoteShardInfoResponse);
3448
+ const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
3449
+ if (error) {
3450
+ return;
3451
+ }
3452
+ await this.savePeerShardInfo(connection.remotePeer, shardInfo);
3422
3453
  }
3423
3454
  catch (error) {
3424
3455
  log.error("Error handling metadata request", error);
@@ -3431,19 +3462,35 @@ class Metadata extends BaseProtocol {
3431
3462
  const request = WakuMetadataRequest.encode(this.shardInfo);
3432
3463
  const peer = await this.peerStore.get(peerId);
3433
3464
  if (!peer) {
3434
- throw new Error(`Peer ${peerId.toString()} not found`);
3465
+ return {
3466
+ shardInfo: null,
3467
+ error: ProtocolError.NO_PEER_AVAILABLE
3468
+ };
3435
3469
  }
3436
3470
  const stream = await this.getStream(peer);
3437
3471
  const encodedResponse = await pipe([request], encode, stream, decode, async (source) => await all(source));
3438
- const decodedResponse = this.decodeMetadataResponse(encodedResponse);
3439
- await this.savePeerShardInfo(peerId, decodedResponse);
3440
- return decodedResponse;
3472
+ const { error, shardInfo } = this.decodeMetadataResponse(encodedResponse);
3473
+ if (error) {
3474
+ return {
3475
+ shardInfo: null,
3476
+ error
3477
+ };
3478
+ }
3479
+ await this.savePeerShardInfo(peerId, shardInfo);
3480
+ return {
3481
+ shardInfo,
3482
+ error: null
3483
+ };
3441
3484
  }
3442
3485
  async confirmOrAttemptHandshake(peerId) {
3443
- if (this.handshakesConfirmed.has(peerId.toString()))
3444
- return;
3445
- await this.query(peerId);
3446
- return;
3486
+ const shardInfo = this.handshakesConfirmed.get(peerId.toString());
3487
+ if (shardInfo) {
3488
+ return {
3489
+ shardInfo,
3490
+ error: null
3491
+ };
3492
+ }
3493
+ return await this.query(peerId);
3447
3494
  }
3448
3495
  decodeMetadataResponse(encodedResponse) {
3449
3496
  const bytes = new Uint8ArrayList();
@@ -3451,9 +3498,17 @@ class Metadata extends BaseProtocol {
3451
3498
  bytes.append(chunk);
3452
3499
  });
3453
3500
  const response = WakuMetadataResponse.decode(bytes);
3454
- if (!response)
3501
+ if (!response) {
3455
3502
  log.error("Error decoding metadata response");
3456
- return response;
3503
+ return {
3504
+ shardInfo: null,
3505
+ error: ProtocolError.DECODE_FAILED
3506
+ };
3507
+ }
3508
+ return {
3509
+ shardInfo: response,
3510
+ error: null
3511
+ };
3457
3512
  }
3458
3513
  async savePeerShardInfo(peerId, shardInfo) {
3459
3514
  // add or update the shardInfo to peer store
@@ -3462,11 +3517,11 @@ class Metadata extends BaseProtocol {
3462
3517
  shardInfo: encodeRelayShard(shardInfo)
3463
3518
  }
3464
3519
  });
3465
- this.handshakesConfirmed.add(peerId.toString());
3520
+ this.handshakesConfirmed.set(peerId.toString(), shardInfo);
3466
3521
  }
3467
3522
  }
3468
3523
  function wakuMetadata(shardInfo) {
3469
3524
  return (components) => new Metadata(shardInfo, components);
3470
3525
  }
3471
3526
 
3472
- export { ConnectionManager, FilterCodecs, KeepAliveManager, LightPushCodec, MetadataCodec, PageDirection, createCursor, createEncoder, index$3 as message, waitForRemotePeer, wakuFilter, wakuLightPush, wakuMetadata, wakuStore, index$2 as waku_filter, index$1 as waku_light_push, index as waku_store };
3527
+ export { ConnectionManager, FilterCodecs, KeepAliveManager, LightPushCodec, LightPushCore, MetadataCodec, PageDirection, createCursor, createEncoder, index$3 as message, waitForRemotePeer, wakuFilter, wakuMetadata, wakuStore, index$2 as waku_filter, index$1 as waku_light_push, index as waku_store };
@@ -1,3 +1,3 @@
1
- import '../index-8YyfzF9R.js';
2
- import '../browser-BQyFvtq6.js';
3
- export { B as BaseProtocol } from '../base_protocol-LhsIWF3-.js';
1
+ import '../index-vlQahmUj.js';
2
+ import '../browser-DoQRY-an.js';
3
+ export { B as BaseProtocol } from '../base_protocol-BCwLeb-A.js';
@@ -1,3 +1,3 @@
1
- export { D as DecodedMessage, k as Decoder, E as Encoder, V as Version, j as createDecoder, h as createEncoder, m as proto } from '../../version_0-FXfzO8Km.js';
2
- import '../../index-8YyfzF9R.js';
3
- import '../../browser-BQyFvtq6.js';
1
+ export { D as DecodedMessage, k as Decoder, E as Encoder, V as Version, j as createDecoder, h as createEncoder, m as proto } from '../../version_0-DiakMc1A.js';
2
+ import '../../index-vlQahmUj.js';
3
+ import '../../browser-DoQRY-an.js';
@@ -1,4 +1,4 @@
1
- import '../browser-BQyFvtq6.js';
1
+ import '../browser-DoQRY-an.js';
2
2
 
3
3
  /**
4
4
  * Return pseudo random subset of the input.
@@ -1,4 +1,4 @@
1
- import { d as allocUnsafe, i as fromString, L as Logger, j as determinePubsubTopic } from './index-8YyfzF9R.js';
1
+ import { d as allocUnsafe, i as fromString, L as Logger, j as determinePubsubTopic } from './index-vlQahmUj.js';
2
2
 
3
3
  /* eslint-disable no-fallthrough */
4
4
  const N1 = Math.pow(2, 7);