@waku/core 0.0.33-00c3261.0 → 0.0.33-252ec59.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,6 +1,5 @@
1
1
  import { Logger } from "@waku/utils";
2
- import { selectConnection } from "./utils.js";
3
- const CONNECTION_TIMEOUT = 5_000;
2
+ import { selectOpenConnection } from "./utils.js";
4
3
  export class StreamManager {
5
4
  multicodec;
6
5
  getConnections;
@@ -18,9 +17,11 @@ export class StreamManager {
18
17
  async getStream(peer) {
19
18
  const peerId = peer.id.toString();
20
19
  const scheduledStream = this.streamPool.get(peerId);
21
- this.streamPool.delete(peerId);
22
- await scheduledStream;
23
- const stream = this.getStreamForCodec(peer.id);
20
+ if (scheduledStream) {
21
+ this.streamPool.delete(peerId);
22
+ await scheduledStream;
23
+ }
24
+ const stream = this.getOpenStreamForCodec(peer.id);
24
25
  if (stream) {
25
26
  this.log.info(`Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
26
27
  return stream;
@@ -29,7 +30,7 @@ export class StreamManager {
29
30
  }
30
31
  async createStream(peer, retries = 0) {
31
32
  const connections = this.getConnections(peer.id);
32
- const connection = selectConnection(connections);
33
+ const connection = selectOpenConnection(connections);
33
34
  if (!connection) {
34
35
  throw new Error(`Failed to get a connection to the peer peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
35
36
  }
@@ -40,6 +41,7 @@ export class StreamManager {
40
41
  this.log.info(`Attempting to create a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
41
42
  stream = await connection.newStream(this.multicodec);
42
43
  this.log.info(`Created stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
44
+ break;
43
45
  }
44
46
  catch (error) {
45
47
  lastError = error;
@@ -61,16 +63,20 @@ export class StreamManager {
61
63
  this.ongoingCreation.add(peerId);
62
64
  await this.createStream(peer);
63
65
  }
66
+ catch (error) {
67
+ this.log.error(`Failed to createStreamWithLock:`, error);
68
+ }
64
69
  finally {
65
70
  this.ongoingCreation.delete(peerId);
66
71
  }
72
+ return;
67
73
  }
68
74
  handlePeerUpdateStreamPool = (evt) => {
69
75
  const { peer } = evt.detail;
70
76
  if (!peer.protocols.includes(this.multicodec)) {
71
77
  return;
72
78
  }
73
- const stream = this.getStreamForCodec(peer.id);
79
+ const stream = this.getOpenStreamForCodec(peer.id);
74
80
  if (stream) {
75
81
  return;
76
82
  }
@@ -78,19 +84,23 @@ export class StreamManager {
78
84
  };
79
85
  scheduleNewStream(peer) {
80
86
  this.log.info(`Scheduling creation of a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
81
- const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve(undefined), CONNECTION_TIMEOUT));
82
- const streamPromise = Promise.race([
83
- this.createStreamWithLock(peer),
84
- timeoutPromise
85
- ]);
86
- this.streamPool.set(peer.id.toString(), streamPromise);
87
+ // abandon previous attempt
88
+ if (this.streamPool.has(peer.id.toString())) {
89
+ this.streamPool.delete(peer.id.toString());
90
+ }
91
+ this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
87
92
  }
88
- getStreamForCodec(peerId) {
89
- const connection = this.getConnections(peerId).find((c) => c.status === "open");
93
+ getOpenStreamForCodec(peerId) {
94
+ const connections = this.getConnections(peerId);
95
+ const connection = selectOpenConnection(connections);
90
96
  if (!connection) {
91
97
  return;
92
98
  }
93
99
  const stream = connection.streams.find((s) => s.protocol === this.multicodec);
100
+ const isStreamUnusable = ["done", "closed", "closing"].includes(stream?.writeStatus || "");
101
+ if (isStreamUnusable) {
102
+ return;
103
+ }
94
104
  return stream;
95
105
  }
96
106
  }
@@ -1 +1 @@
1
- {"version":3,"file":"stream_manager.js","sourceRoot":"","sources":["../../../src/lib/stream_manager/stream_manager.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,kBAAk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F,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,QAAQ,CAC7D,MAAM,EAAE,WAAW,IAAI,EAAE,CAC1B,CAAC;QACF,IAAI,gBAAgB,EAAE,CAAC;YACrB,OAAO;QACT,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
@@ -1,2 +1,2 @@
1
1
  import type { Connection } from "@libp2p/interface";
2
- export declare function selectConnection(connections: Connection[]): Connection | undefined;
2
+ export declare function selectOpenConnection(connections: Connection[]): Connection | undefined;
@@ -1,19 +1,7 @@
1
- export function selectConnection(connections) {
2
- if (!connections.length)
3
- return;
4
- if (connections.length === 1)
5
- return connections[0];
6
- let latestConnection;
7
- connections.forEach((connection) => {
8
- if (connection.status === "open") {
9
- if (!latestConnection) {
10
- latestConnection = connection;
11
- }
12
- else if (connection.timeline.open > latestConnection.timeline.open) {
13
- latestConnection = connection;
14
- }
15
- }
16
- });
17
- return latestConnection;
1
+ export function selectOpenConnection(connections) {
2
+ return connections
3
+ .filter((c) => c.status === "open")
4
+ .sort((left, right) => right.timeline.open - left.timeline.open)
5
+ .at(0);
18
6
  }
19
7
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/stream_manager/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,gBAAgB,CAC9B,WAAyB;IAEzB,IAAI,CAAC,WAAW,CAAC,MAAM;QAAE,OAAO;IAChC,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,WAAW,CAAC,CAAC,CAAC,CAAC;IAEpD,IAAI,gBAAwC,CAAC;IAE7C,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QACjC,IAAI,UAAU,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;YACjC,IAAI,CAAC,gBAAgB,EAAE,CAAC;gBACtB,gBAAgB,GAAG,UAAU,CAAC;YAChC,CAAC;iBAAM,IAAI,UAAU,CAAC,QAAQ,CAAC,IAAI,GAAG,gBAAgB,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;gBACrE,gBAAgB,GAAG,UAAU,CAAC;YAChC,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,gBAAgB,CAAC;AAC1B,CAAC"}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/lib/stream_manager/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,oBAAoB,CAClC,WAAyB;IAEzB,OAAO,WAAW;SACf,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,MAAM,CAAC;SAClC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;SAC/D,EAAE,CAAC,CAAC,CAAC,CAAC;AACX,CAAC"}
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@waku/core","version":"0.0.33-00c3261.0","description":"TypeScript implementation of the Waku v2 protocol","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"},"./lib/message/version_0":{"types":"./dist/lib/message/version_0.d.ts","import":"./dist/lib/message/version_0.js"},"./lib/base_protocol":{"types":"./dist/lib/base_protocol.d.ts","import":"./dist/lib/base_protocol.js"}},"typesVersions":{"*":{"lib/*":["dist/lib/*"],"constants/*":["dist/constants/*"]}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/core#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralised","communication","web3","ethereum","dapps"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha","test:browser":"NODE_ENV=test karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=20"},"dependencies":{"@libp2p/ping":"^1.1.2","@waku/enr":"0.0.27-00c3261.0","@waku/interfaces":"0.0.28-00c3261.0","@waku/proto":"0.0.9-00c3261.0","@waku/utils":"0.0.21-00c3261.0","debug":"^4.3.4","it-all":"^3.0.4","it-length-prefixed":"^9.0.4","it-pipe":"^3.0.1","p-event":"^6.0.1","uint8arraylist":"^2.4.3","uuid":"^9.0.0"},"devDependencies":{"@multiformats/multiaddr":"^12.0.0","@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.11","@types/debug":"^4.1.12","@types/mocha":"^10.0.6","@types/uuid":"^9.0.8","@waku/build-utils":"*","chai":"^4.3.10","cspell":"^8.6.1","fast-check":"^3.19.0","ignore-loader":"^0.1.2","isomorphic-fetch":"^3.0.0","mocha":"^10.3.0","npm-run-all":"^4.1.5","process":"^0.11.10","rollup":"^4.12.0"},"peerDependencies":{"@multiformats/multiaddr":"^12.0.0","libp2p":"^1.8.1"},"peerDependenciesMeta":{"@multiformats/multiaddr":{"optional":true},"libp2p":{"optional":true}},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
1
+ {"name":"@waku/core","version":"0.0.33-252ec59.0","description":"TypeScript implementation of the Waku v2 protocol","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"},"./lib/message/version_0":{"types":"./dist/lib/message/version_0.d.ts","import":"./dist/lib/message/version_0.js"},"./lib/base_protocol":{"types":"./dist/lib/base_protocol.d.ts","import":"./dist/lib/base_protocol.js"}},"typesVersions":{"*":{"lib/*":["dist/lib/*"],"constants/*":["dist/constants/*"]}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/core#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralised","communication","web3","ethereum","dapps"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha","test:browser":"NODE_ENV=test karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=20"},"dependencies":{"@libp2p/ping":"^1.1.2","@waku/enr":"0.0.27-252ec59.0","@waku/interfaces":"0.0.28-252ec59.0","@waku/proto":"0.0.9-252ec59.0","@waku/utils":"0.0.21-252ec59.0","debug":"^4.3.4","it-all":"^3.0.4","it-length-prefixed":"^9.0.4","it-pipe":"^3.0.1","p-event":"^6.0.1","uint8arraylist":"^2.4.3","uuid":"^9.0.0"},"devDependencies":{"@multiformats/multiaddr":"^12.0.0","@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.11","@types/debug":"^4.1.12","@types/mocha":"^10.0.6","@types/uuid":"^9.0.8","@waku/build-utils":"*","chai":"^4.3.10","sinon":"^18.0.0","cspell":"^8.6.1","fast-check":"^3.19.0","ignore-loader":"^0.1.2","isomorphic-fetch":"^3.0.0","mocha":"^10.3.0","npm-run-all":"^4.1.5","process":"^0.11.10","rollup":"^4.12.0"},"peerDependencies":{"@multiformats/multiaddr":"^12.0.0","libp2p":"^1.8.1"},"peerDependenciesMeta":{"@multiformats/multiaddr":{"optional":true},"libp2p":{"optional":true}},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
@@ -1,16 +1,12 @@
1
1
  import type { Libp2p } from "@libp2p/interface";
2
- import type { Peer, PeerStore, Stream } from "@libp2p/interface";
2
+ import type { Peer, Stream } from "@libp2p/interface";
3
3
  import type {
4
4
  IBaseProtocolCore,
5
5
  Libp2pComponents,
6
6
  PubsubTopic
7
7
  } from "@waku/interfaces";
8
- import { Logger, pubsubTopicsToShardInfo } from "@waku/utils";
9
- import {
10
- getConnectedPeersForProtocolAndShard,
11
- getPeersForProtocol,
12
- sortPeersByLatency
13
- } from "@waku/utils/libp2p";
8
+ import { Logger } from "@waku/utils";
9
+ import { getPeersForProtocol, sortPeersByLatency } from "@waku/utils/libp2p";
14
10
 
15
11
  import { filterPeersByDiscovery } from "./filterPeers.js";
16
12
  import { StreamManager } from "./stream_manager/index.js";
@@ -26,7 +22,7 @@ export class BaseProtocol implements IBaseProtocolCore {
26
22
 
27
23
  protected constructor(
28
24
  public multicodec: string,
29
- private components: Libp2pComponents,
25
+ protected components: Libp2pComponents,
30
26
  private log: Logger,
31
27
  public readonly pubsubTopics: PubsubTopic[]
32
28
  ) {
@@ -50,25 +46,22 @@ export class BaseProtocol implements IBaseProtocolCore {
50
46
  return this.streamManager.getStream(peer);
51
47
  }
52
48
 
53
- public get peerStore(): PeerStore {
54
- return this.components.peerStore;
55
- }
56
-
57
49
  /**
58
50
  * Returns known peers from the address book (`libp2p.peerStore`) that support
59
51
  * the class protocol. Waku may or may not be currently connected to these
60
52
  * peers.
61
53
  */
62
54
  public async allPeers(): Promise<Peer[]> {
63
- return getPeersForProtocol(this.peerStore, [this.multicodec]);
55
+ return getPeersForProtocol(this.components.peerStore, [this.multicodec]);
64
56
  }
65
57
 
66
58
  public async connectedPeers(): Promise<Peer[]> {
67
59
  const peers = await this.allPeers();
68
60
  return peers.filter((peer) => {
69
- return (
70
- this.components.connectionManager.getConnections(peer.id).length > 0
61
+ const connections = this.components.connectionManager.getConnections(
62
+ peer.id
71
63
  );
64
+ return connections.length > 0;
72
65
  });
73
66
  }
74
67
 
@@ -77,9 +70,8 @@ export class BaseProtocol implements IBaseProtocolCore {
77
70
  *
78
71
  * @param numPeers - The total number of peers to retrieve. If 0, all peers are returned.
79
72
  * @param maxBootstrapPeers - The maximum number of bootstrap peers to retrieve.
80
-
81
- * @returns A list of peers that support the protocol sorted by latency.
82
- */
73
+ * @returns A list of peers that support the protocol sorted by latency. By default, returns all peers available, including bootstrap.
74
+ */
83
75
  public async getPeers(
84
76
  {
85
77
  numPeers,
@@ -88,29 +80,23 @@ export class BaseProtocol implements IBaseProtocolCore {
88
80
  numPeers: number;
89
81
  maxBootstrapPeers: number;
90
82
  } = {
91
- maxBootstrapPeers: 1,
83
+ maxBootstrapPeers: 0,
92
84
  numPeers: 0
93
85
  }
94
86
  ): Promise<Peer[]> {
95
87
  // Retrieve all connected peers that support the protocol & shard (if configured)
96
- const connectedPeersForProtocolAndShard =
97
- await getConnectedPeersForProtocolAndShard(
98
- this.components.connectionManager.getConnections(),
99
- this.peerStore,
100
- [this.multicodec],
101
- pubsubTopicsToShardInfo(this.pubsubTopics)
102
- );
88
+ const allAvailableConnectedPeers = await this.connectedPeers();
103
89
 
104
90
  // Filter the peers based on discovery & number of peers requested
105
91
  const filteredPeers = filterPeersByDiscovery(
106
- connectedPeersForProtocolAndShard,
92
+ allAvailableConnectedPeers,
107
93
  numPeers,
108
94
  maxBootstrapPeers
109
95
  );
110
96
 
111
97
  // Sort the peers by latency
112
98
  const sortedFilteredPeers = await sortPeersByLatency(
113
- this.peerStore,
99
+ this.components.peerStore,
114
100
  filteredPeers
115
101
  );
116
102
 
@@ -37,6 +37,7 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
37
37
  wakuMessage: WakuMessage,
38
38
  peerIdStr: string
39
39
  ) => Promise<void>,
40
+ private handleError: (error: Error) => Promise<void>,
40
41
  public readonly pubsubTopics: PubsubTopic[],
41
42
  libp2p: Libp2p
42
43
  ) {
@@ -301,8 +302,18 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore {
301
302
  () => {
302
303
  log.info("Receiving pipe closed.");
303
304
  },
304
- (e) => {
305
- log.error("Error with receiving pipe", e);
305
+ async (e) => {
306
+ log.error(
307
+ "Error with receiving pipe",
308
+ e,
309
+ " -- ",
310
+ "on peer ",
311
+ connection.remotePeer.toString(),
312
+ " -- ",
313
+ "stream ",
314
+ stream
315
+ );
316
+ await this.handleError(e);
306
317
  }
307
318
  );
308
319
  } catch (e) {
@@ -45,7 +45,7 @@ class Metadata extends BaseProtocol implements IMetadata {
45
45
  pubsubTopicsToShardInfo(this.pubsubTopics)
46
46
  );
47
47
 
48
- const peer = await this.peerStore.get(peerId);
48
+ const peer = await this.libp2pComponents.peerStore.get(peerId);
49
49
  if (!peer) {
50
50
  return {
51
51
  shardInfo: null,
@@ -1,16 +1,8 @@
1
- import type {
2
- Connection,
3
- Peer,
4
- PeerId,
5
- PeerUpdate,
6
- Stream
7
- } from "@libp2p/interface";
1
+ import type { Peer, PeerId, PeerUpdate, Stream } from "@libp2p/interface";
8
2
  import type { Libp2p } from "@waku/interfaces";
9
3
  import { Logger } from "@waku/utils";
10
4
 
11
- import { selectConnection } from "./utils.js";
12
-
13
- const CONNECTION_TIMEOUT = 5_000;
5
+ import { selectOpenConnection } from "./utils.js";
14
6
 
15
7
  export class StreamManager {
16
8
  private readonly log: Logger;
@@ -19,9 +11,9 @@ export class StreamManager {
19
11
  private streamPool: Map<string, Promise<void>> = new Map();
20
12
 
21
13
  public constructor(
22
- public multicodec: string,
23
- public getConnections: Libp2p["getConnections"],
24
- public addEventListener: Libp2p["addEventListener"]
14
+ private multicodec: string,
15
+ private getConnections: Libp2p["getConnections"],
16
+ private addEventListener: Libp2p["addEventListener"]
25
17
  ) {
26
18
  this.log = new Logger(`stream-manager:${multicodec}`);
27
19
  this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
@@ -31,10 +23,13 @@ export class StreamManager {
31
23
  const peerId = peer.id.toString();
32
24
 
33
25
  const scheduledStream = this.streamPool.get(peerId);
34
- this.streamPool.delete(peerId);
35
- await scheduledStream;
36
26
 
37
- const stream = this.getStreamForCodec(peer.id);
27
+ if (scheduledStream) {
28
+ this.streamPool.delete(peerId);
29
+ await scheduledStream;
30
+ }
31
+
32
+ const stream = this.getOpenStreamForCodec(peer.id);
38
33
 
39
34
  if (stream) {
40
35
  this.log.info(
@@ -48,7 +43,7 @@ export class StreamManager {
48
43
 
49
44
  private async createStream(peer: Peer, retries = 0): Promise<Stream> {
50
45
  const connections = this.getConnections(peer.id);
51
- const connection = selectConnection(connections);
46
+ const connection = selectOpenConnection(connections);
52
47
 
53
48
  if (!connection) {
54
49
  throw new Error(
@@ -68,6 +63,7 @@ export class StreamManager {
68
63
  this.log.info(
69
64
  `Created stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
70
65
  );
66
+ break;
71
67
  } catch (error) {
72
68
  lastError = error;
73
69
  }
@@ -96,9 +92,13 @@ export class StreamManager {
96
92
  try {
97
93
  this.ongoingCreation.add(peerId);
98
94
  await this.createStream(peer);
95
+ } catch (error) {
96
+ this.log.error(`Failed to createStreamWithLock:`, error);
99
97
  } finally {
100
98
  this.ongoingCreation.delete(peerId);
101
99
  }
100
+
101
+ return;
102
102
  }
103
103
 
104
104
  private handlePeerUpdateStreamPool = (evt: CustomEvent<PeerUpdate>): void => {
@@ -108,7 +108,7 @@ export class StreamManager {
108
108
  return;
109
109
  }
110
110
 
111
- const stream = this.getStreamForCodec(peer.id);
111
+ const stream = this.getOpenStreamForCodec(peer.id);
112
112
 
113
113
  if (stream) {
114
114
  return;
@@ -122,22 +122,17 @@ export class StreamManager {
122
122
  `Scheduling creation of a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`
123
123
  );
124
124
 
125
- const timeoutPromise = new Promise<undefined>((resolve) =>
126
- setTimeout(() => resolve(undefined), CONNECTION_TIMEOUT)
127
- );
128
-
129
- const streamPromise = Promise.race([
130
- this.createStreamWithLock(peer),
131
- timeoutPromise
132
- ]);
125
+ // abandon previous attempt
126
+ if (this.streamPool.has(peer.id.toString())) {
127
+ this.streamPool.delete(peer.id.toString());
128
+ }
133
129
 
134
- this.streamPool.set(peer.id.toString(), streamPromise);
130
+ this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
135
131
  }
136
132
 
137
- private getStreamForCodec(peerId: PeerId): Stream | undefined {
138
- const connection: Connection | undefined = this.getConnections(peerId).find(
139
- (c) => c.status === "open"
140
- );
133
+ private getOpenStreamForCodec(peerId: PeerId): Stream | undefined {
134
+ const connections = this.getConnections(peerId);
135
+ const connection = selectOpenConnection(connections);
141
136
 
142
137
  if (!connection) {
143
138
  return;
@@ -147,6 +142,13 @@ export class StreamManager {
147
142
  (s) => s.protocol === this.multicodec
148
143
  );
149
144
 
145
+ const isStreamUnusable = ["done", "closed", "closing"].includes(
146
+ stream?.writeStatus || ""
147
+ );
148
+ if (isStreamUnusable) {
149
+ return;
150
+ }
151
+
150
152
  return stream;
151
153
  }
152
154
  }
@@ -1,22 +1,10 @@
1
1
  import type { Connection } from "@libp2p/interface";
2
2
 
3
- export function selectConnection(
3
+ export function selectOpenConnection(
4
4
  connections: Connection[]
5
5
  ): Connection | undefined {
6
- if (!connections.length) return;
7
- if (connections.length === 1) return connections[0];
8
-
9
- let latestConnection: Connection | undefined;
10
-
11
- connections.forEach((connection) => {
12
- if (connection.status === "open") {
13
- if (!latestConnection) {
14
- latestConnection = connection;
15
- } else if (connection.timeline.open > latestConnection.timeline.open) {
16
- latestConnection = connection;
17
- }
18
- }
19
- });
20
-
21
- return latestConnection;
6
+ return connections
7
+ .filter((c) => c.status === "open")
8
+ .sort((left, right) => right.timeline.open - left.timeline.open)
9
+ .at(0);
22
10
  }