@waku/core 0.0.28-efe9b8d.0 → 0.0.29-3ec2344.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/CHANGELOG.md +30 -0
- package/bundle/{base_protocol-D0Zdzb-v.js → base_protocol-3fdjkRTX.js} +60 -18
- package/bundle/{browser-DoQRY-an.js → browser-H8Jgqifo.js} +5 -0
- package/bundle/{index-BJwgMx4y.js → index-BQ8SG1DC.js} +11 -20
- package/bundle/index.js +106 -362
- package/bundle/lib/base_protocol.js +3 -3
- package/bundle/lib/message/version_0.js +3 -3
- package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
- package/bundle/{version_0-C6o0DvNW.js → version_0-D_Nwtppu.js} +5 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/lib/filter/index.d.ts +13 -2
- package/dist/lib/filter/index.js +74 -259
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/keep_alive_manager.js +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 +4 -2
- package/dist/lib/stream_manager.js +58 -16
- package/dist/lib/stream_manager.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.js +1 -1
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/lib/filter/index.ts +138 -455
- package/src/lib/keep_alive_manager.ts +1 -1
- 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 +73 -23
- package/src/lib/wait_for_remote_peer.ts +1 -1
package/src/lib/store/index.ts
CHANGED
@@ -92,7 +92,13 @@ export class StoreCore extends BaseProtocol implements IStoreCore {
|
|
92
92
|
|
93
93
|
const historyRpcQuery = HistoryRpc.createQuery(queryOpts);
|
94
94
|
|
95
|
-
|
95
|
+
let stream;
|
96
|
+
try {
|
97
|
+
stream = await this.getStream(peer);
|
98
|
+
} catch (e) {
|
99
|
+
log.error("Failed to get stream", e);
|
100
|
+
break;
|
101
|
+
}
|
96
102
|
|
97
103
|
const res = await pipe(
|
98
104
|
[historyRpcQuery.encode()],
|
@@ -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,10 @@ 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
|
+
|
23
|
+
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
24
|
+
this.getStream = this.getStream.bind(this);
|
23
25
|
}
|
24
26
|
|
25
27
|
public async getStream(peer: Peer): Promise<Stream> {
|
@@ -27,47 +29,95 @@ export class StreamManager {
|
|
27
29
|
const streamPromise = this.streamPool.get(peerIdStr);
|
28
30
|
|
29
31
|
if (!streamPromise) {
|
30
|
-
return this.
|
32
|
+
return this.createStream(peer);
|
31
33
|
}
|
32
34
|
|
33
|
-
// We have the stream, let's remove it from the map
|
34
35
|
this.streamPool.delete(peerIdStr);
|
35
|
-
|
36
36
|
this.prepareNewStream(peer);
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
38
|
+
try {
|
39
|
+
const stream = await streamPromise;
|
40
|
+
if (stream && stream.status !== "closed") {
|
41
|
+
return stream;
|
42
|
+
}
|
43
|
+
} catch (error) {
|
44
|
+
this.log.error(`Failed to get stream for ${peerIdStr} -- `, error);
|
42
45
|
}
|
43
46
|
|
44
|
-
return
|
47
|
+
return this.createStream(peer);
|
48
|
+
}
|
49
|
+
|
50
|
+
private async createStream(peer: Peer): Promise<Stream> {
|
51
|
+
try {
|
52
|
+
return await this.newStream(peer);
|
53
|
+
} catch (error) {
|
54
|
+
throw new Error(
|
55
|
+
`Failed to create a new stream for ${peer.id.toString()} -- ` + error
|
56
|
+
);
|
57
|
+
}
|
45
58
|
}
|
46
59
|
|
47
|
-
private async newStream(peer: Peer): Promise<Stream> {
|
60
|
+
private async newStream(peer: Peer, retries = 0): Promise<Stream> {
|
48
61
|
const connections = this.getConnections(peer.id);
|
49
62
|
const connection = selectConnection(connections);
|
63
|
+
|
50
64
|
if (!connection) {
|
51
65
|
throw new Error("Failed to get a connection to the peer");
|
52
66
|
}
|
53
|
-
|
67
|
+
|
68
|
+
try {
|
69
|
+
return await connection.newStream(this.multicodec);
|
70
|
+
} catch (error) {
|
71
|
+
if (retries < MAX_RETRIES) {
|
72
|
+
const backoff = RETRY_BACKOFF_BASE * Math.pow(2, retries);
|
73
|
+
await new Promise((resolve) => setTimeout(resolve, backoff));
|
74
|
+
return this.newStream(peer, retries + 1);
|
75
|
+
}
|
76
|
+
throw error;
|
77
|
+
}
|
54
78
|
}
|
55
79
|
|
56
80
|
private prepareNewStream(peer: Peer): void {
|
57
|
-
const
|
58
|
-
|
81
|
+
const timeoutPromise = new Promise<void>((resolve) =>
|
82
|
+
setTimeout(resolve, CONNECTION_TIMEOUT)
|
83
|
+
);
|
84
|
+
|
85
|
+
const streamPromise = Promise.race([
|
86
|
+
this.newStream(peer),
|
87
|
+
timeoutPromise.then(() => {
|
88
|
+
throw new Error("Connection timeout");
|
89
|
+
})
|
90
|
+
]).catch((error) => {
|
59
91
|
this.log.error(
|
60
|
-
`Failed to prepare a new stream for ${peer.id.toString()}
|
92
|
+
`Failed to prepare a new stream for ${peer.id.toString()} -- `,
|
93
|
+
error
|
61
94
|
);
|
62
95
|
});
|
96
|
+
|
63
97
|
this.streamPool.set(peer.id.toString(), streamPromise);
|
64
98
|
}
|
65
99
|
|
66
100
|
private handlePeerUpdateStreamPool = (evt: CustomEvent<PeerUpdate>): void => {
|
67
|
-
const peer = evt.detail
|
101
|
+
const { peer } = evt.detail;
|
102
|
+
|
68
103
|
if (peer.protocols.includes(this.multicodec)) {
|
69
|
-
this.
|
70
|
-
|
104
|
+
const isConnected = this.isConnectedTo(peer.id);
|
105
|
+
|
106
|
+
if (isConnected) {
|
107
|
+
this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
|
108
|
+
this.prepareNewStream(peer);
|
109
|
+
} else {
|
110
|
+
const peerIdStr = peer.id.toString();
|
111
|
+
this.streamPool.delete(peerIdStr);
|
112
|
+
this.log.info(
|
113
|
+
`Removed pending stream for disconnected peer ${peerIdStr}`
|
114
|
+
);
|
115
|
+
}
|
71
116
|
}
|
72
117
|
};
|
118
|
+
|
119
|
+
private isConnectedTo(peerId: PeerId): boolean {
|
120
|
+
const connections = this.getConnections(peerId);
|
121
|
+
return connections.some((connection) => connection.status === "open");
|
122
|
+
}
|
73
123
|
}
|
@@ -70,7 +70,7 @@ export async function waitForRemotePeer(
|
|
70
70
|
if (!waku.filter)
|
71
71
|
throw new Error("Cannot wait for Filter peer: protocol not mounted");
|
72
72
|
promises.push(
|
73
|
-
waitForConnectedPeer(waku.filter, waku.libp2p.services.metadata)
|
73
|
+
waitForConnectedPeer(waku.filter.protocol, waku.libp2p.services.metadata)
|
74
74
|
);
|
75
75
|
}
|
76
76
|
|