@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.
- package/bundle/{base_protocol-kugTP2Op.js → base_protocol-C6HnrRx8.js} +53 -20
- package/bundle/{browser-B9234RhB.js → index-DnW8ifxc.js} +621 -5
- package/bundle/index.js +48 -28
- package/bundle/lib/base_protocol.js +2 -3
- package/bundle/lib/message/version_0.js +2 -3
- package/bundle/{version_0-BoLZMvhu.js → version_0-DQ9xsSLk.js} +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/lib/connection_manager.d.ts +3 -2
- package/dist/lib/connection_manager.js +16 -16
- package/dist/lib/connection_manager.js.map +1 -1
- package/dist/lib/filter/index.js +5 -1
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/light_push/index.js +3 -3
- package/dist/lib/light_push/index.js.map +1 -1
- package/dist/lib/metadata/index.js +11 -1
- package/dist/lib/metadata/index.js.map +1 -1
- package/dist/lib/store/index.js +8 -1
- package/dist/lib/store/index.js.map +1 -1
- package/dist/lib/stream_manager.d.ts +5 -4
- package/dist/lib/stream_manager.js +52 -18
- package/dist/lib/stream_manager.js.map +1 -1
- package/package.json +1 -1
- package/src/lib/connection_manager.ts +28 -28
- package/src/lib/filter/index.ts +7 -3
- package/src/lib/light_push/index.ts +4 -7
- package/src/lib/metadata/index.ts +10 -1
- package/src/lib/store/index.ts +7 -1
- package/src/lib/stream_manager.ts +66 -25
- package/bundle/index-egXdK_Fb.js +0 -614
- package/bundle/lib/predefined_bootstrap_nodes.js +0 -81
- package/dist/lib/predefined_bootstrap_nodes.d.ts +0 -34
- package/dist/lib/predefined_bootstrap_nodes.js +0 -54
- package/dist/lib/predefined_bootstrap_nodes.js.map +0 -1
- package/src/lib/predefined_bootstrap_nodes.ts +0 -68
@@ -1,11 +1,15 @@
|
|
1
1
|
import type { PeerUpdate, Stream } from "@libp2p/interface";
|
2
|
-
import { Peer } from "@libp2p/interface";
|
2
|
+
import type { Peer, PeerId } from "@libp2p/interface";
|
3
3
|
import { Libp2p } from "@waku/interfaces";
|
4
4
|
import { Logger } from "@waku/utils";
|
5
5
|
import { selectConnection } from "@waku/utils/libp2p";
|
6
6
|
|
7
|
+
const CONNECTION_TIMEOUT = 5_000;
|
8
|
+
const RETRY_BACKOFF_BASE = 1_000;
|
9
|
+
const MAX_RETRIES = 3;
|
10
|
+
|
7
11
|
export class StreamManager {
|
8
|
-
private streamPool: Map<string, Promise<Stream | void>>;
|
12
|
+
private readonly streamPool: Map<string, Promise<Stream | void>>;
|
9
13
|
private readonly log: Logger;
|
10
14
|
|
11
15
|
constructor(
|
@@ -14,12 +18,8 @@ export class StreamManager {
|
|
14
18
|
public addEventListener: Libp2p["addEventListener"]
|
15
19
|
) {
|
16
20
|
this.log = new Logger(`stream-manager:${multicodec}`);
|
17
|
-
this.addEventListener(
|
18
|
-
"peer:update",
|
19
|
-
this.handlePeerUpdateStreamPool.bind(this)
|
20
|
-
);
|
21
|
-
this.getStream = this.getStream.bind(this);
|
22
21
|
this.streamPool = new Map();
|
22
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
23
23
|
}
|
24
24
|
|
25
25
|
public async getStream(peer: Peer): Promise<Stream> {
|
@@ -27,47 +27,88 @@ export class StreamManager {
|
|
27
27
|
const streamPromise = this.streamPool.get(peerIdStr);
|
28
28
|
|
29
29
|
if (!streamPromise) {
|
30
|
-
return this.
|
30
|
+
return this.createStream(peer);
|
31
31
|
}
|
32
32
|
|
33
|
-
// We have the stream, let's remove it from the map
|
34
33
|
this.streamPool.delete(peerIdStr);
|
34
|
+
this.prepareStream(peer);
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
try {
|
37
|
+
const stream = await streamPromise;
|
38
|
+
if (stream && stream.status !== "closed") {
|
39
|
+
return stream;
|
40
|
+
}
|
41
|
+
} catch (error) {
|
42
|
+
this.log.warn(`Failed to get stream for ${peerIdStr} -- `, error);
|
43
|
+
this.log.warn("Attempting to create a new stream for the peer");
|
42
44
|
}
|
43
45
|
|
44
|
-
return
|
46
|
+
return this.createStream(peer);
|
45
47
|
}
|
46
48
|
|
47
|
-
private async
|
49
|
+
private async createStream(peer: Peer, retries = 0): Promise<Stream> {
|
48
50
|
const connections = this.getConnections(peer.id);
|
49
51
|
const connection = selectConnection(connections);
|
52
|
+
|
50
53
|
if (!connection) {
|
51
54
|
throw new Error("Failed to get a connection to the peer");
|
52
55
|
}
|
53
|
-
|
56
|
+
|
57
|
+
try {
|
58
|
+
return await connection.newStream(this.multicodec);
|
59
|
+
} catch (error) {
|
60
|
+
if (retries < MAX_RETRIES) {
|
61
|
+
const backoff = RETRY_BACKOFF_BASE * Math.pow(2, retries);
|
62
|
+
await new Promise((resolve) => setTimeout(resolve, backoff));
|
63
|
+
return this.createStream(peer, retries + 1);
|
64
|
+
}
|
65
|
+
throw new Error(
|
66
|
+
`Failed to create a new stream for ${peer.id.toString()} -- ` + error
|
67
|
+
);
|
68
|
+
}
|
54
69
|
}
|
55
70
|
|
56
|
-
private
|
57
|
-
const
|
58
|
-
|
71
|
+
private prepareStream(peer: Peer): void {
|
72
|
+
const timeoutPromise = new Promise<void>((resolve) =>
|
73
|
+
setTimeout(resolve, CONNECTION_TIMEOUT)
|
74
|
+
);
|
75
|
+
|
76
|
+
const streamPromise = Promise.race([
|
77
|
+
this.createStream(peer),
|
78
|
+
timeoutPromise.then(() => {
|
79
|
+
throw new Error("Connection timeout");
|
80
|
+
})
|
81
|
+
]).catch((error) => {
|
59
82
|
this.log.error(
|
60
|
-
`Failed to prepare a new stream for ${peer.id.toString()}
|
83
|
+
`Failed to prepare a new stream for ${peer.id.toString()} -- `,
|
84
|
+
error
|
61
85
|
);
|
62
86
|
});
|
87
|
+
|
63
88
|
this.streamPool.set(peer.id.toString(), streamPromise);
|
64
89
|
}
|
65
90
|
|
66
91
|
private handlePeerUpdateStreamPool = (evt: CustomEvent<PeerUpdate>): void => {
|
67
|
-
const peer = evt.detail
|
92
|
+
const { peer } = evt.detail;
|
93
|
+
|
68
94
|
if (peer.protocols.includes(this.multicodec)) {
|
69
|
-
this.
|
70
|
-
|
95
|
+
const isConnected = this.isConnectedTo(peer.id);
|
96
|
+
|
97
|
+
if (isConnected) {
|
98
|
+
this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
|
99
|
+
this.prepareStream(peer);
|
100
|
+
} else {
|
101
|
+
const peerIdStr = peer.id.toString();
|
102
|
+
this.streamPool.delete(peerIdStr);
|
103
|
+
this.log.info(
|
104
|
+
`Removed pending stream for disconnected peer ${peerIdStr}`
|
105
|
+
);
|
106
|
+
}
|
71
107
|
}
|
72
108
|
};
|
109
|
+
|
110
|
+
private isConnectedTo(peerId: PeerId): boolean {
|
111
|
+
const connections = this.getConnections(peerId);
|
112
|
+
return connections.some((connection) => connection.status === "open");
|
113
|
+
}
|
73
114
|
}
|