@waku/core 0.0.33-aeb05cd.0 → 0.0.33-b7bdb60.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-BDjsZTsQ.js +275 -0
- package/bundle/{index-tdQNdKHx.js → index-CeEH6b9b.js} +24 -450
- package/bundle/index.js +99 -419
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-BrbNEwD-.js → version_0-BYg0O3M-.js} +451 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/base_protocol.d.ts +2 -3
- package/dist/lib/base_protocol.js +7 -10
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/filter/index.d.ts +1 -2
- package/dist/lib/filter/index.js +1 -4
- package/dist/lib/filter/index.js.map +1 -1
- package/dist/lib/stream_manager/stream_manager.d.ts +12 -9
- package/dist/lib/stream_manager/stream_manager.js +87 -56
- package/dist/lib/stream_manager/stream_manager.js.map +1 -1
- package/dist/lib/stream_manager/utils.d.ts +1 -1
- package/dist/lib/stream_manager/utils.js +5 -17
- package/dist/lib/stream_manager/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -3
- package/src/lib/base_protocol.ts +8 -22
- package/src/lib/filter/index.ts +0 -2
- package/src/lib/stream_manager/stream_manager.ts +124 -66
- package/src/lib/stream_manager/utils.ts +5 -17
- package/bundle/base_protocol-0xHh0_Hq.js +0 -334
- package/dist/lib/wait_for_remote_peer.d.ts +0 -22
- package/dist/lib/wait_for_remote_peer.js +0 -142
- package/dist/lib/wait_for_remote_peer.js.map +0 -1
- package/src/lib/wait_for_remote_peer.ts +0 -200
@@ -1,200 +0,0 @@
|
|
1
|
-
import type { IdentifyResult } from "@libp2p/interface";
|
2
|
-
import type {
|
3
|
-
IBaseProtocolCore,
|
4
|
-
IMetadata,
|
5
|
-
IRelay,
|
6
|
-
Waku
|
7
|
-
} from "@waku/interfaces";
|
8
|
-
import { Protocols } from "@waku/interfaces";
|
9
|
-
import { Logger } from "@waku/utils";
|
10
|
-
import { pEvent } from "p-event";
|
11
|
-
const log = new Logger("wait-for-remote-peer");
|
12
|
-
|
13
|
-
//TODO: move this function within the Waku class: https://github.com/waku-org/js-waku/issues/1761
|
14
|
-
/**
|
15
|
-
* Wait for a remote peer to be ready given the passed protocols.
|
16
|
-
* Must be used after attempting to connect to nodes, using
|
17
|
-
* {@link @waku/sdk!WakuNode.dial} or a bootstrap method with
|
18
|
-
* {@link @waku/sdk!createLightNode}.
|
19
|
-
*
|
20
|
-
* If the passed protocols is a GossipSub protocol, then it resolves only once
|
21
|
-
* a peer is in a mesh, to help ensure that other peers will send and receive
|
22
|
-
* message to us.
|
23
|
-
*
|
24
|
-
* @param waku The Waku Node
|
25
|
-
* @param protocols The protocols that need to be enabled by remote peers.
|
26
|
-
* @param timeoutMs A timeout value in milliseconds..
|
27
|
-
*
|
28
|
-
* @returns A promise that **resolves** if all desired protocols are fulfilled by
|
29
|
-
* remote nodes, **rejects** if the timeoutMs is reached.
|
30
|
-
* @throws If passing a protocol that is not mounted
|
31
|
-
* @default Wait for remote peers with protocols enabled locally and no time out is applied.
|
32
|
-
*/
|
33
|
-
export async function waitForRemotePeer(
|
34
|
-
waku: Waku,
|
35
|
-
protocols?: Protocols[],
|
36
|
-
timeoutMs?: number
|
37
|
-
): Promise<void> {
|
38
|
-
protocols = protocols ?? getEnabledProtocols(waku);
|
39
|
-
|
40
|
-
if (!waku.isStarted()) return Promise.reject("Waku node is not started");
|
41
|
-
|
42
|
-
const promises = [];
|
43
|
-
|
44
|
-
if (protocols.includes(Protocols.Relay)) {
|
45
|
-
if (!waku.relay)
|
46
|
-
throw new Error("Cannot wait for Relay peer: protocol not mounted");
|
47
|
-
promises.push(waitForGossipSubPeerInMesh(waku.relay));
|
48
|
-
}
|
49
|
-
|
50
|
-
if (protocols.includes(Protocols.Store)) {
|
51
|
-
if (!waku.store)
|
52
|
-
throw new Error("Cannot wait for Store peer: protocol not mounted");
|
53
|
-
promises.push(
|
54
|
-
waitForConnectedPeer(waku.store.protocol, waku.libp2p.services.metadata)
|
55
|
-
);
|
56
|
-
}
|
57
|
-
|
58
|
-
if (protocols.includes(Protocols.LightPush)) {
|
59
|
-
if (!waku.lightPush)
|
60
|
-
throw new Error("Cannot wait for LightPush peer: protocol not mounted");
|
61
|
-
promises.push(
|
62
|
-
waitForConnectedPeer(
|
63
|
-
waku.lightPush.protocol,
|
64
|
-
waku.libp2p.services.metadata
|
65
|
-
)
|
66
|
-
);
|
67
|
-
}
|
68
|
-
|
69
|
-
if (protocols.includes(Protocols.Filter)) {
|
70
|
-
if (!waku.filter)
|
71
|
-
throw new Error("Cannot wait for Filter peer: protocol not mounted");
|
72
|
-
promises.push(
|
73
|
-
waitForConnectedPeer(waku.filter.protocol, waku.libp2p.services.metadata)
|
74
|
-
);
|
75
|
-
}
|
76
|
-
|
77
|
-
if (timeoutMs) {
|
78
|
-
await rejectOnTimeout(
|
79
|
-
Promise.all(promises),
|
80
|
-
timeoutMs,
|
81
|
-
"Timed out waiting for a remote peer."
|
82
|
-
);
|
83
|
-
} else {
|
84
|
-
await Promise.all(promises);
|
85
|
-
}
|
86
|
-
}
|
87
|
-
|
88
|
-
//TODO: move this function within protocol SDK class: https://github.com/waku-org/js-waku/issues/1761
|
89
|
-
/**
|
90
|
-
* Wait for a peer with the given protocol to be connected.
|
91
|
-
* If sharding is enabled on the node, it will also wait for the peer to be confirmed by the metadata service.
|
92
|
-
*/
|
93
|
-
async function waitForConnectedPeer(
|
94
|
-
protocol: IBaseProtocolCore,
|
95
|
-
metadataService?: IMetadata
|
96
|
-
): Promise<void> {
|
97
|
-
const codec = protocol.multicodec;
|
98
|
-
const peers = await protocol.connectedPeers();
|
99
|
-
|
100
|
-
if (peers.length) {
|
101
|
-
if (!metadataService) {
|
102
|
-
log.info(`${codec} peer found: `, peers[0].id.toString());
|
103
|
-
return;
|
104
|
-
}
|
105
|
-
|
106
|
-
// once a peer is connected, we need to confirm the metadata handshake with at least one of those peers if sharding is enabled
|
107
|
-
try {
|
108
|
-
await Promise.any(
|
109
|
-
peers.map((peer) => metadataService.confirmOrAttemptHandshake(peer.id))
|
110
|
-
);
|
111
|
-
return;
|
112
|
-
} catch (e) {
|
113
|
-
if ((e as any).code === "ERR_CONNECTION_BEING_CLOSED")
|
114
|
-
log.error(
|
115
|
-
`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`
|
116
|
-
);
|
117
|
-
|
118
|
-
log.error(`Error waiting for handshake confirmation: ${e}`);
|
119
|
-
}
|
120
|
-
}
|
121
|
-
|
122
|
-
log.info(`Waiting for ${codec} peer`);
|
123
|
-
|
124
|
-
// else we'll just wait for the next peer to connect
|
125
|
-
await new Promise<void>((resolve) => {
|
126
|
-
const cb = (evt: CustomEvent<IdentifyResult>): void => {
|
127
|
-
if (evt.detail?.protocols?.includes(codec)) {
|
128
|
-
if (metadataService) {
|
129
|
-
metadataService
|
130
|
-
.confirmOrAttemptHandshake(evt.detail.peerId)
|
131
|
-
.then(() => {
|
132
|
-
protocol.removeLibp2pEventListener("peer:identify", cb);
|
133
|
-
resolve();
|
134
|
-
})
|
135
|
-
.catch((e) => {
|
136
|
-
if (e.code === "ERR_CONNECTION_BEING_CLOSED")
|
137
|
-
log.error(
|
138
|
-
`Connection with the peer was closed and possibly because it's on a different shard. Error: ${e}`
|
139
|
-
);
|
140
|
-
|
141
|
-
log.error(`Error waiting for handshake confirmation: ${e}`);
|
142
|
-
});
|
143
|
-
} else {
|
144
|
-
protocol.removeLibp2pEventListener("peer:identify", cb);
|
145
|
-
resolve();
|
146
|
-
}
|
147
|
-
}
|
148
|
-
};
|
149
|
-
protocol.addLibp2pEventListener("peer:identify", cb);
|
150
|
-
});
|
151
|
-
}
|
152
|
-
|
153
|
-
/**
|
154
|
-
* Wait for at least one peer with the given protocol to be connected and in the gossipsub
|
155
|
-
* mesh for all pubsubTopics.
|
156
|
-
*/
|
157
|
-
async function waitForGossipSubPeerInMesh(waku: IRelay): Promise<void> {
|
158
|
-
let peers = waku.getMeshPeers();
|
159
|
-
const pubsubTopics = waku.pubsubTopics;
|
160
|
-
|
161
|
-
for (const topic of pubsubTopics) {
|
162
|
-
while (peers.length == 0) {
|
163
|
-
await pEvent(waku.gossipSub, "gossipsub:heartbeat");
|
164
|
-
peers = waku.getMeshPeers(topic);
|
165
|
-
}
|
166
|
-
}
|
167
|
-
}
|
168
|
-
|
169
|
-
const awaitTimeout = (ms: number, rejectReason: string): Promise<void> =>
|
170
|
-
new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
|
171
|
-
|
172
|
-
async function rejectOnTimeout<T>(
|
173
|
-
promise: Promise<T>,
|
174
|
-
timeoutMs: number,
|
175
|
-
rejectReason: string
|
176
|
-
): Promise<void> {
|
177
|
-
await Promise.race([promise, awaitTimeout(timeoutMs, rejectReason)]);
|
178
|
-
}
|
179
|
-
|
180
|
-
function getEnabledProtocols(waku: Waku): Protocols[] {
|
181
|
-
const protocols = [];
|
182
|
-
|
183
|
-
if (waku.relay) {
|
184
|
-
protocols.push(Protocols.Relay);
|
185
|
-
}
|
186
|
-
|
187
|
-
if (waku.filter) {
|
188
|
-
protocols.push(Protocols.Filter);
|
189
|
-
}
|
190
|
-
|
191
|
-
if (waku.store) {
|
192
|
-
protocols.push(Protocols.Store);
|
193
|
-
}
|
194
|
-
|
195
|
-
if (waku.lightPush) {
|
196
|
-
protocols.push(Protocols.LightPush);
|
197
|
-
}
|
198
|
-
|
199
|
-
return protocols;
|
200
|
-
}
|