@waku/core 0.0.30-00c77c6.0 → 0.0.30-42126a6.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.
Files changed (34) hide show
  1. package/bundle/{base_protocol-kugTP2Op.js → base_protocol-C6HnrRx8.js} +53 -20
  2. package/bundle/{browser-B9234RhB.js → index-DnW8ifxc.js} +621 -5
  3. package/bundle/index.js +48 -28
  4. package/bundle/lib/base_protocol.js +2 -3
  5. package/bundle/lib/message/version_0.js +2 -3
  6. package/bundle/{version_0-BoLZMvhu.js → version_0-DQ9xsSLk.js} +1 -1
  7. package/dist/.tsbuildinfo +1 -1
  8. package/dist/lib/connection_manager.d.ts +3 -2
  9. package/dist/lib/connection_manager.js +16 -16
  10. package/dist/lib/connection_manager.js.map +1 -1
  11. package/dist/lib/filter/index.js +5 -1
  12. package/dist/lib/filter/index.js.map +1 -1
  13. package/dist/lib/light_push/index.js +3 -3
  14. package/dist/lib/light_push/index.js.map +1 -1
  15. package/dist/lib/metadata/index.js +11 -1
  16. package/dist/lib/metadata/index.js.map +1 -1
  17. package/dist/lib/store/index.js +8 -1
  18. package/dist/lib/store/index.js.map +1 -1
  19. package/dist/lib/stream_manager.d.ts +5 -4
  20. package/dist/lib/stream_manager.js +52 -18
  21. package/dist/lib/stream_manager.js.map +1 -1
  22. package/package.json +1 -1
  23. package/src/lib/connection_manager.ts +28 -28
  24. package/src/lib/filter/index.ts +7 -3
  25. package/src/lib/light_push/index.ts +4 -7
  26. package/src/lib/metadata/index.ts +10 -1
  27. package/src/lib/store/index.ts +7 -1
  28. package/src/lib/stream_manager.ts +66 -25
  29. package/bundle/index-egXdK_Fb.js +0 -614
  30. package/bundle/lib/predefined_bootstrap_nodes.js +0 -81
  31. package/dist/lib/predefined_bootstrap_nodes.d.ts +0 -34
  32. package/dist/lib/predefined_bootstrap_nodes.js +0 -54
  33. package/dist/lib/predefined_bootstrap_nodes.js.map +0 -1
  34. package/src/lib/predefined_bootstrap_nodes.ts +0 -68
@@ -1,5 +1,4 @@
1
- import { c as bytesToUtf8, L as Logger, e as ensureShardingConfigured } from './index-egXdK_Fb.js';
2
- import { T as Tags } from './browser-B9234RhB.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
@@ -175,6 +174,9 @@ function filterPeersByDiscovery(peers, numPeers, maxBootstrapPeers) {
175
174
  return selectedPeers;
176
175
  }
177
176
 
177
+ const CONNECTION_TIMEOUT = 5_000;
178
+ const RETRY_BACKOFF_BASE = 1_000;
179
+ const MAX_RETRIES = 3;
178
180
  class StreamManager {
179
181
  multicodec;
180
182
  getConnections;
@@ -186,47 +188,78 @@ class StreamManager {
186
188
  this.getConnections = getConnections;
187
189
  this.addEventListener = addEventListener;
188
190
  this.log = new Logger(`stream-manager:${multicodec}`);
189
- this.addEventListener("peer:update", this.handlePeerUpdateStreamPool.bind(this));
190
- this.getStream = this.getStream.bind(this);
191
191
  this.streamPool = new Map();
192
+ this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
192
193
  }
193
194
  async getStream(peer) {
194
195
  const peerIdStr = peer.id.toString();
195
196
  const streamPromise = this.streamPool.get(peerIdStr);
196
197
  if (!streamPromise) {
197
- return this.newStream(peer); // fallback by creating a new stream on the spot
198
+ return this.createStream(peer);
198
199
  }
199
- // We have the stream, let's remove it from the map
200
200
  this.streamPool.delete(peerIdStr);
201
- this.prepareNewStream(peer);
202
- const stream = await streamPromise;
203
- if (!stream || stream.status === "closed") {
204
- return this.newStream(peer); // fallback by creating a new stream on the spot
201
+ this.prepareStream(peer);
202
+ try {
203
+ const stream = await streamPromise;
204
+ if (stream && stream.status !== "closed") {
205
+ return stream;
206
+ }
207
+ }
208
+ catch (error) {
209
+ this.log.warn(`Failed to get stream for ${peerIdStr} -- `, error);
210
+ this.log.warn("Attempting to create a new stream for the peer");
205
211
  }
206
- return stream;
212
+ return this.createStream(peer);
207
213
  }
208
- async newStream(peer) {
214
+ async createStream(peer, retries = 0) {
209
215
  const connections = this.getConnections(peer.id);
210
216
  const connection = selectConnection(connections);
211
217
  if (!connection) {
212
218
  throw new Error("Failed to get a connection to the peer");
213
219
  }
214
- return connection.newStream(this.multicodec);
220
+ try {
221
+ return await connection.newStream(this.multicodec);
222
+ }
223
+ catch (error) {
224
+ if (retries < MAX_RETRIES) {
225
+ const backoff = RETRY_BACKOFF_BASE * Math.pow(2, retries);
226
+ await new Promise((resolve) => setTimeout(resolve, backoff));
227
+ return this.createStream(peer, retries + 1);
228
+ }
229
+ throw new Error(`Failed to create a new stream for ${peer.id.toString()} -- ` + error);
230
+ }
215
231
  }
216
- prepareNewStream(peer) {
217
- const streamPromise = this.newStream(peer).catch(() => {
218
- // No error thrown as this call is not triggered by the user
219
- this.log.error(`Failed to prepare a new stream for ${peer.id.toString()}`);
232
+ prepareStream(peer) {
233
+ const timeoutPromise = new Promise((resolve) => setTimeout(resolve, CONNECTION_TIMEOUT));
234
+ const streamPromise = Promise.race([
235
+ this.createStream(peer),
236
+ timeoutPromise.then(() => {
237
+ throw new Error("Connection timeout");
238
+ })
239
+ ]).catch((error) => {
240
+ this.log.error(`Failed to prepare a new stream for ${peer.id.toString()} -- `, error);
220
241
  });
221
242
  this.streamPool.set(peer.id.toString(), streamPromise);
222
243
  }
223
244
  handlePeerUpdateStreamPool = (evt) => {
224
- const peer = evt.detail.peer;
245
+ const { peer } = evt.detail;
225
246
  if (peer.protocols.includes(this.multicodec)) {
226
- this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
227
- this.prepareNewStream(peer);
247
+ const isConnected = this.isConnectedTo(peer.id);
248
+ if (isConnected) {
249
+ this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
250
+ this.prepareStream(peer);
251
+ }
252
+ else {
253
+ const peerIdStr = peer.id.toString();
254
+ this.streamPool.delete(peerIdStr);
255
+ this.log.info(`Removed pending stream for disconnected peer ${peerIdStr}`);
256
+ }
228
257
  }
229
258
  };
259
+ isConnectedTo(peerId) {
260
+ const connections = this.getConnections(peerId);
261
+ return connections.some((connection) => connection.status === "open");
262
+ }
230
263
  }
231
264
 
232
265
  /**