@waku/core 0.0.5 → 0.0.7
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 +51 -3
- package/bundle/index.js +31146 -26342
- package/bundle/lib/message/topic_only_message.js +33 -0
- package/bundle/lib/message/version_0.js +133 -0
- package/bundle/lib/predefined_bootstrap_nodes.js +24 -4
- package/bundle/{message-049c8b67.js → peer_exchange-53df2b11.js} +3579 -1007
- package/dist/index.d.ts +14 -13
- package/dist/index.js +11 -10
- package/dist/index.js.map +1 -1
- package/dist/lib/{waku_filter → filter}/filter_rpc.d.ts +1 -1
- package/dist/lib/{waku_filter → filter}/filter_rpc.js +1 -1
- package/dist/lib/filter/filter_rpc.js.map +1 -0
- package/dist/lib/filter/index.d.ts +25 -0
- package/dist/lib/{waku_filter → filter}/index.js +14 -9
- package/dist/lib/filter/index.js.map +1 -0
- package/dist/lib/light_push/index.d.ts +22 -0
- package/dist/lib/{waku_light_push → light_push}/index.js +14 -9
- package/dist/lib/light_push/index.js.map +1 -0
- package/dist/lib/{waku_light_push → light_push}/push_rpc.d.ts +1 -1
- package/dist/lib/{waku_light_push → light_push}/push_rpc.js +1 -1
- package/dist/lib/light_push/push_rpc.js.map +1 -0
- package/dist/lib/message/topic_only_message.d.ts +16 -0
- package/dist/lib/{waku_message → message}/topic_only_message.js +2 -1
- package/dist/lib/message/topic_only_message.js.map +1 -0
- package/dist/lib/message/version_0.d.ts +54 -0
- package/dist/lib/{waku_message → message}/version_0.js +41 -6
- package/dist/lib/message/version_0.js.map +1 -0
- package/dist/lib/predefined_bootstrap_nodes.d.ts +1 -2
- package/dist/lib/predefined_bootstrap_nodes.js +0 -2
- package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
- package/dist/lib/{waku_relay → relay}/constants.d.ts +0 -0
- package/dist/lib/{waku_relay → relay}/constants.js +0 -0
- package/dist/lib/relay/constants.js.map +1 -0
- package/dist/lib/relay/index.d.ts +24 -0
- package/dist/lib/{waku_relay → relay}/index.js +9 -6
- package/dist/lib/relay/index.js.map +1 -0
- package/dist/lib/{waku_store → store}/history_rpc.d.ts +1 -1
- package/dist/lib/{waku_store → store}/history_rpc.js +1 -1
- package/dist/lib/store/history_rpc.js.map +1 -0
- package/dist/lib/store/index.d.ts +68 -0
- package/dist/lib/{waku_store → store}/index.js +41 -16
- package/dist/lib/store/index.js.map +1 -0
- package/dist/lib/to_proto_message.d.ts +3 -3
- package/dist/lib/to_proto_message.js +1 -0
- package/dist/lib/to_proto_message.js.map +1 -1
- package/dist/lib/wait_for_remote_peer.js +14 -5
- package/dist/lib/wait_for_remote_peer.js.map +1 -1
- package/dist/lib/waku.d.ts +17 -15
- package/dist/lib/waku.js +49 -41
- package/dist/lib/waku.js.map +1 -1
- package/package.json +28 -31
- package/src/index.ts +25 -14
- package/src/lib/{waku_filter → filter}/filter_rpc.ts +1 -2
- package/src/lib/{waku_filter → filter}/index.ts +51 -30
- package/src/lib/{waku_light_push → light_push}/index.ts +38 -19
- package/src/lib/{waku_light_push → light_push}/push_rpc.ts +1 -2
- package/src/lib/{waku_message → message}/topic_only_message.ts +12 -7
- package/src/lib/{waku_message → message}/version_0.ts +59 -18
- package/src/lib/predefined_bootstrap_nodes.ts +2 -6
- package/src/lib/{waku_relay → relay}/constants.ts +0 -0
- package/src/lib/{waku_relay → relay}/index.ts +30 -26
- package/src/lib/{waku_store → store}/history_rpc.ts +1 -2
- package/src/lib/{waku_store → store}/index.ts +93 -28
- package/src/lib/to_proto_message.ts +5 -5
- package/src/lib/wait_for_remote_peer.ts +19 -7
- package/src/lib/waku.ts +78 -51
- package/bundle/browser-1e1a2f27.js +0 -722
- package/bundle/events-158407bb.js +0 -1929
- package/bundle/index-64ce43f0.js +0 -69
- package/bundle/index-8710041d.js +0 -2962
- package/bundle/index-a67d7136.js +0 -4059
- package/bundle/lib/peer_discovery_static_list.js +0 -89
- package/bundle/lib/wait_for_remote_peer.js +0 -326
- package/bundle/lib/waku_message/topic_only_message.js +0 -3
- package/bundle/lib/waku_message/version_0.js +0 -317
- package/bundle/random_subset-75d1c511.js +0 -26
- package/bundle/topic_only_message-5ad3a869.js +0 -82
- package/dist/lib/multiaddr_to_peer_info.d.ts +0 -3
- package/dist/lib/multiaddr_to_peer_info.js +0 -15
- package/dist/lib/multiaddr_to_peer_info.js.map +0 -1
- package/dist/lib/peer_discovery_static_list.d.ts +0 -44
- package/dist/lib/peer_discovery_static_list.js +0 -72
- package/dist/lib/peer_discovery_static_list.js.map +0 -1
- package/dist/lib/select_connection.d.ts +0 -2
- package/dist/lib/select_connection.js +0 -19
- package/dist/lib/select_connection.js.map +0 -1
- package/dist/lib/select_peer.d.ts +0 -15
- package/dist/lib/select_peer.js +0 -59
- package/dist/lib/select_peer.js.map +0 -1
- package/dist/lib/waku_filter/filter_rpc.js.map +0 -1
- package/dist/lib/waku_filter/index.d.ts +0 -50
- package/dist/lib/waku_filter/index.js.map +0 -1
- package/dist/lib/waku_light_push/index.d.ts +0 -38
- package/dist/lib/waku_light_push/index.js.map +0 -1
- package/dist/lib/waku_light_push/push_rpc.js.map +0 -1
- package/dist/lib/waku_message/topic_only_message.d.ts +0 -15
- package/dist/lib/waku_message/topic_only_message.js.map +0 -1
- package/dist/lib/waku_message/version_0.d.ts +0 -27
- package/dist/lib/waku_message/version_0.js.map +0 -1
- package/dist/lib/waku_relay/constants.js.map +0 -1
- package/dist/lib/waku_relay/index.d.ts +0 -66
- package/dist/lib/waku_relay/index.js.map +0 -1
- package/dist/lib/waku_store/history_rpc.js.map +0 -1
- package/dist/lib/waku_store/index.d.ts +0 -126
- package/dist/lib/waku_store/index.js.map +0 -1
- package/dist/proto/filter.d.ts +0 -65
- package/dist/proto/filter.js +0 -425
- package/dist/proto/filter.js.map +0 -1
- package/dist/proto/light_push.d.ts +0 -57
- package/dist/proto/light_push.js +0 -369
- package/dist/proto/light_push.js.map +0 -1
- package/dist/proto/message.d.ts +0 -29
- package/dist/proto/message.js +0 -215
- package/dist/proto/message.js.map +0 -1
- package/dist/proto/store.d.ts +0 -104
- package/dist/proto/store.js +0 -602
- package/dist/proto/store.js.map +0 -1
- package/dist/proto/topic_only_message.d.ts +0 -10
- package/dist/proto/topic_only_message.js +0 -46
- package/dist/proto/topic_only_message.js.map +0 -1
- package/src/lib/multiaddr_to_peer_info.ts +0 -17
- package/src/lib/peer_discovery_static_list.ts +0 -118
- package/src/lib/select_connection.ts +0 -24
- package/src/lib/select_peer.ts +0 -77
- package/src/proto/filter.ts +0 -602
- package/src/proto/light_push.ts +0 -526
- package/src/proto/message.ts +0 -304
- package/src/proto/store.ts +0 -844
- package/src/proto/topic_only_message.ts +0 -67
@@ -1,89 +0,0 @@
|
|
1
|
-
import { p as peerIdFromString, E as EventEmitter, C as CustomEvent } from '../events-158407bb.js';
|
2
|
-
import { m as multiaddr } from '../index-8710041d.js';
|
3
|
-
import { d as debug } from '../browser-1e1a2f27.js';
|
4
|
-
import { g as getPseudoRandomSubset } from '../random_subset-75d1c511.js';
|
5
|
-
import '../index-64ce43f0.js';
|
6
|
-
|
7
|
-
const symbol = Symbol.for('@libp2p/peer-discovery');
|
8
|
-
|
9
|
-
function multiaddrsToPeerInfo(mas) {
|
10
|
-
return mas
|
11
|
-
.map((ma) => {
|
12
|
-
const peerIdStr = ma.getPeerId();
|
13
|
-
const protocols = [];
|
14
|
-
return {
|
15
|
-
id: peerIdStr ? peerIdFromString(peerIdStr) : null,
|
16
|
-
multiaddrs: [ma.decapsulateCode(421)],
|
17
|
-
protocols,
|
18
|
-
};
|
19
|
-
})
|
20
|
-
.filter((peerInfo) => peerInfo.id !== null);
|
21
|
-
}
|
22
|
-
|
23
|
-
const log = debug("waku:peer-discovery-static-list");
|
24
|
-
/**
|
25
|
-
* Pass a list of multiaddr that will be used to bootstrap a node.
|
26
|
-
*/
|
27
|
-
class PeerDiscoveryStaticPeers extends EventEmitter {
|
28
|
-
/**
|
29
|
-
* @param peers Multiaddrs of peers to connect to.
|
30
|
-
* @param opts
|
31
|
-
*/
|
32
|
-
constructor(peers, opts) {
|
33
|
-
super();
|
34
|
-
this.interval = opts?.interval ?? PeerDiscoveryStaticPeers.DefaultInterval;
|
35
|
-
const maxPeers = opts?.maxPeers ?? peers?.length;
|
36
|
-
const peerMas = peers.map((peer) => {
|
37
|
-
if (typeof peer === "string") {
|
38
|
-
return multiaddr(peer);
|
39
|
-
}
|
40
|
-
else {
|
41
|
-
return peer;
|
42
|
-
}
|
43
|
-
});
|
44
|
-
this.peers = multiaddrsToPeerInfo(getPseudoRandomSubset(peerMas, maxPeers));
|
45
|
-
log("Use provided list of peers (reduced to maxPeers)", this.peers.map((ma) => ma.toString()));
|
46
|
-
}
|
47
|
-
/**
|
48
|
-
* Start emitting static peers.
|
49
|
-
*/
|
50
|
-
start() {
|
51
|
-
this._startTimer();
|
52
|
-
}
|
53
|
-
_startTimer() {
|
54
|
-
if (this.peers) {
|
55
|
-
log("Starting to emit static peers.");
|
56
|
-
if (this.timer != null) {
|
57
|
-
return;
|
58
|
-
}
|
59
|
-
this.timer = setInterval(() => this._returnPeers(), this.interval);
|
60
|
-
this._returnPeers();
|
61
|
-
}
|
62
|
-
}
|
63
|
-
_returnPeers() {
|
64
|
-
if (this.timer == null) {
|
65
|
-
return;
|
66
|
-
}
|
67
|
-
this.peers.forEach((peerData) => {
|
68
|
-
this.dispatchEvent(new CustomEvent("peer", { detail: peerData }));
|
69
|
-
});
|
70
|
-
}
|
71
|
-
/**
|
72
|
-
* Stop emitting peers.
|
73
|
-
*/
|
74
|
-
stop() {
|
75
|
-
if (this.timer != null) {
|
76
|
-
clearInterval(this.timer);
|
77
|
-
}
|
78
|
-
this.timer = undefined;
|
79
|
-
}
|
80
|
-
get [symbol]() {
|
81
|
-
return true;
|
82
|
-
}
|
83
|
-
get [Symbol.toStringTag]() {
|
84
|
-
return "@waku/peer-discovery-static-list";
|
85
|
-
}
|
86
|
-
}
|
87
|
-
PeerDiscoveryStaticPeers.DefaultInterval = 200;
|
88
|
-
|
89
|
-
export { PeerDiscoveryStaticPeers };
|
@@ -1,326 +0,0 @@
|
|
1
|
-
import { P as Protocols, S as StoreCodec, L as LightPushCodec, F as FilterCodec } from '../index-a67d7136.js';
|
2
|
-
import { d as debug } from '../browser-1e1a2f27.js';
|
3
|
-
import '../message-049c8b67.js';
|
4
|
-
import '../index-64ce43f0.js';
|
5
|
-
|
6
|
-
class TimeoutError extends Error {
|
7
|
-
constructor(message) {
|
8
|
-
super(message);
|
9
|
-
this.name = 'TimeoutError';
|
10
|
-
}
|
11
|
-
}
|
12
|
-
|
13
|
-
/**
|
14
|
-
An error to be thrown when the request is aborted by AbortController.
|
15
|
-
DOMException is thrown instead of this Error when DOMException is available.
|
16
|
-
*/
|
17
|
-
class AbortError extends Error {
|
18
|
-
constructor(message) {
|
19
|
-
super();
|
20
|
-
this.name = 'AbortError';
|
21
|
-
this.message = message;
|
22
|
-
}
|
23
|
-
}
|
24
|
-
|
25
|
-
/**
|
26
|
-
TODO: Remove AbortError and just throw DOMException when targeting Node 18.
|
27
|
-
*/
|
28
|
-
const getDOMException = errorMessage => globalThis.DOMException === undefined ?
|
29
|
-
new AbortError(errorMessage) :
|
30
|
-
new DOMException(errorMessage);
|
31
|
-
|
32
|
-
/**
|
33
|
-
TODO: Remove below function and just 'reject(signal.reason)' when targeting Node 18.
|
34
|
-
*/
|
35
|
-
const getAbortedReason = signal => {
|
36
|
-
const reason = signal.reason === undefined ?
|
37
|
-
getDOMException('This operation was aborted.') :
|
38
|
-
signal.reason;
|
39
|
-
|
40
|
-
return reason instanceof Error ? reason : getDOMException(reason);
|
41
|
-
};
|
42
|
-
|
43
|
-
function pTimeout(promise, milliseconds, fallback, options) {
|
44
|
-
let timer;
|
45
|
-
|
46
|
-
const cancelablePromise = new Promise((resolve, reject) => {
|
47
|
-
if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {
|
48
|
-
throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
|
49
|
-
}
|
50
|
-
|
51
|
-
if (milliseconds === Number.POSITIVE_INFINITY) {
|
52
|
-
resolve(promise);
|
53
|
-
return;
|
54
|
-
}
|
55
|
-
|
56
|
-
options = {
|
57
|
-
customTimers: {setTimeout, clearTimeout},
|
58
|
-
...options
|
59
|
-
};
|
60
|
-
|
61
|
-
if (options.signal) {
|
62
|
-
const {signal} = options;
|
63
|
-
if (signal.aborted) {
|
64
|
-
reject(getAbortedReason(signal));
|
65
|
-
}
|
66
|
-
|
67
|
-
signal.addEventListener('abort', () => {
|
68
|
-
reject(getAbortedReason(signal));
|
69
|
-
});
|
70
|
-
}
|
71
|
-
|
72
|
-
timer = options.customTimers.setTimeout.call(undefined, () => {
|
73
|
-
if (typeof fallback === 'function') {
|
74
|
-
try {
|
75
|
-
resolve(fallback());
|
76
|
-
} catch (error) {
|
77
|
-
reject(error);
|
78
|
-
}
|
79
|
-
|
80
|
-
return;
|
81
|
-
}
|
82
|
-
|
83
|
-
const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${milliseconds} milliseconds`;
|
84
|
-
const timeoutError = fallback instanceof Error ? fallback : new TimeoutError(message);
|
85
|
-
|
86
|
-
if (typeof promise.cancel === 'function') {
|
87
|
-
promise.cancel();
|
88
|
-
}
|
89
|
-
|
90
|
-
reject(timeoutError);
|
91
|
-
}, milliseconds);
|
92
|
-
|
93
|
-
(async () => {
|
94
|
-
try {
|
95
|
-
resolve(await promise);
|
96
|
-
} catch (error) {
|
97
|
-
reject(error);
|
98
|
-
} finally {
|
99
|
-
options.customTimers.clearTimeout.call(undefined, timer);
|
100
|
-
}
|
101
|
-
})();
|
102
|
-
});
|
103
|
-
|
104
|
-
cancelablePromise.clear = () => {
|
105
|
-
clearTimeout(timer);
|
106
|
-
timer = undefined;
|
107
|
-
};
|
108
|
-
|
109
|
-
return cancelablePromise;
|
110
|
-
}
|
111
|
-
|
112
|
-
const normalizeEmitter = emitter => {
|
113
|
-
const addListener = emitter.on || emitter.addListener || emitter.addEventListener;
|
114
|
-
const removeListener = emitter.off || emitter.removeListener || emitter.removeEventListener;
|
115
|
-
|
116
|
-
if (!addListener || !removeListener) {
|
117
|
-
throw new TypeError('Emitter is not compatible');
|
118
|
-
}
|
119
|
-
|
120
|
-
return {
|
121
|
-
addListener: addListener.bind(emitter),
|
122
|
-
removeListener: removeListener.bind(emitter),
|
123
|
-
};
|
124
|
-
};
|
125
|
-
|
126
|
-
function pEventMultiple(emitter, event, options) {
|
127
|
-
let cancel;
|
128
|
-
const returnValue = new Promise((resolve, reject) => {
|
129
|
-
options = {
|
130
|
-
rejectionEvents: ['error'],
|
131
|
-
multiArgs: false,
|
132
|
-
resolveImmediately: false,
|
133
|
-
...options,
|
134
|
-
};
|
135
|
-
|
136
|
-
if (!(options.count >= 0 && (options.count === Number.POSITIVE_INFINITY || Number.isInteger(options.count)))) {
|
137
|
-
throw new TypeError('The `count` option should be at least 0 or more');
|
138
|
-
}
|
139
|
-
|
140
|
-
// Allow multiple events
|
141
|
-
const events = [event].flat();
|
142
|
-
|
143
|
-
const items = [];
|
144
|
-
const {addListener, removeListener} = normalizeEmitter(emitter);
|
145
|
-
|
146
|
-
const onItem = (...arguments_) => {
|
147
|
-
const value = options.multiArgs ? arguments_ : arguments_[0];
|
148
|
-
|
149
|
-
// eslint-disable-next-line unicorn/no-array-callback-reference
|
150
|
-
if (options.filter && !options.filter(value)) {
|
151
|
-
return;
|
152
|
-
}
|
153
|
-
|
154
|
-
items.push(value);
|
155
|
-
|
156
|
-
if (options.count === items.length) {
|
157
|
-
cancel();
|
158
|
-
resolve(items);
|
159
|
-
}
|
160
|
-
};
|
161
|
-
|
162
|
-
const rejectHandler = error => {
|
163
|
-
cancel();
|
164
|
-
reject(error);
|
165
|
-
};
|
166
|
-
|
167
|
-
cancel = () => {
|
168
|
-
for (const event of events) {
|
169
|
-
removeListener(event, onItem);
|
170
|
-
}
|
171
|
-
|
172
|
-
for (const rejectionEvent of options.rejectionEvents) {
|
173
|
-
removeListener(rejectionEvent, rejectHandler);
|
174
|
-
}
|
175
|
-
};
|
176
|
-
|
177
|
-
for (const event of events) {
|
178
|
-
addListener(event, onItem);
|
179
|
-
}
|
180
|
-
|
181
|
-
for (const rejectionEvent of options.rejectionEvents) {
|
182
|
-
addListener(rejectionEvent, rejectHandler);
|
183
|
-
}
|
184
|
-
|
185
|
-
if (options.resolveImmediately) {
|
186
|
-
resolve(items);
|
187
|
-
}
|
188
|
-
});
|
189
|
-
|
190
|
-
returnValue.cancel = cancel;
|
191
|
-
|
192
|
-
if (typeof options.timeout === 'number') {
|
193
|
-
const timeout = pTimeout(returnValue, options.timeout);
|
194
|
-
timeout.cancel = cancel;
|
195
|
-
return timeout;
|
196
|
-
}
|
197
|
-
|
198
|
-
return returnValue;
|
199
|
-
}
|
200
|
-
|
201
|
-
function pEvent(emitter, event, options) {
|
202
|
-
if (typeof options === 'function') {
|
203
|
-
options = {filter: options};
|
204
|
-
}
|
205
|
-
|
206
|
-
options = {
|
207
|
-
...options,
|
208
|
-
count: 1,
|
209
|
-
resolveImmediately: false,
|
210
|
-
};
|
211
|
-
|
212
|
-
const arrayPromise = pEventMultiple(emitter, event, options);
|
213
|
-
const promise = arrayPromise.then(array => array[0]); // eslint-disable-line promise/prefer-await-to-then
|
214
|
-
promise.cancel = arrayPromise.cancel;
|
215
|
-
|
216
|
-
return promise;
|
217
|
-
}
|
218
|
-
|
219
|
-
const log = debug("waku:wait-for-remote-peer");
|
220
|
-
/**
|
221
|
-
* Wait for a remote peer to be ready given the passed protocols.
|
222
|
-
* Must be used after attempting to connect to nodes, using
|
223
|
-
* {@link index.waku.WakuNode.dial} or a bootstrap method with
|
224
|
-
* {@link lib/create_waku.createLightNode}.
|
225
|
-
*
|
226
|
-
* If the passed protocols is a GossipSub protocol, then it resolves only once
|
227
|
-
* a peer is in a mesh, to help ensure that other peers will send and receive
|
228
|
-
* message to us.
|
229
|
-
*
|
230
|
-
* @param waku The Waku Node
|
231
|
-
* @param protocols The protocols that need to be enabled by remote peers.
|
232
|
-
* @param timeoutMs A timeout value in milliseconds..
|
233
|
-
*
|
234
|
-
* @returns A promise that **resolves** if all desired protocols are fulfilled by
|
235
|
-
* remote nodes, **rejects** if the timeoutMs is reached.
|
236
|
-
* @throws If passing a protocol that is not mounted
|
237
|
-
* @default Wait for remote peers with protocols enabled locally and no time out is applied.
|
238
|
-
*/
|
239
|
-
async function waitForRemotePeer(waku, protocols, timeoutMs) {
|
240
|
-
protocols = protocols ?? getEnabledProtocols(waku);
|
241
|
-
if (!waku.isStarted())
|
242
|
-
return Promise.reject("Waku node is not started");
|
243
|
-
const promises = [];
|
244
|
-
if (protocols.includes(Protocols.Relay)) {
|
245
|
-
if (!waku.relay)
|
246
|
-
throw new Error("Cannot wait for Relay peer: protocol not mounted");
|
247
|
-
promises.push(waitForGossipSubPeerInMesh(waku.relay));
|
248
|
-
}
|
249
|
-
if (protocols.includes(Protocols.Store)) {
|
250
|
-
if (!waku.store)
|
251
|
-
throw new Error("Cannot wait for Store peer: protocol not mounted");
|
252
|
-
promises.push(waitForConnectedPeer(waku.store, [StoreCodec]));
|
253
|
-
}
|
254
|
-
if (protocols.includes(Protocols.LightPush)) {
|
255
|
-
if (!waku.lightPush)
|
256
|
-
throw new Error("Cannot wait for LightPush peer: protocol not mounted");
|
257
|
-
promises.push(waitForConnectedPeer(waku.lightPush, [LightPushCodec]));
|
258
|
-
}
|
259
|
-
if (protocols.includes(Protocols.Filter)) {
|
260
|
-
if (!waku.filter)
|
261
|
-
throw new Error("Cannot wait for Filter peer: protocol not mounted");
|
262
|
-
promises.push(waitForConnectedPeer(waku.filter, [FilterCodec]));
|
263
|
-
}
|
264
|
-
if (timeoutMs) {
|
265
|
-
await rejectOnTimeout(Promise.all(promises), timeoutMs, "Timed out waiting for a remote peer.");
|
266
|
-
}
|
267
|
-
else {
|
268
|
-
await Promise.all(promises);
|
269
|
-
}
|
270
|
-
}
|
271
|
-
/**
|
272
|
-
* Wait for a peer with the given protocol to be connected.
|
273
|
-
*/
|
274
|
-
async function waitForConnectedPeer(waku, codecs) {
|
275
|
-
const peers = await waku.peers();
|
276
|
-
if (peers.length) {
|
277
|
-
log(`${codecs} peer found: `, peers[0].id.toString());
|
278
|
-
return;
|
279
|
-
}
|
280
|
-
await new Promise((resolve) => {
|
281
|
-
const cb = (evt) => {
|
282
|
-
for (const codec of codecs) {
|
283
|
-
if (evt.detail.protocols.includes(codec)) {
|
284
|
-
log("Resolving for", codec, evt.detail.protocols);
|
285
|
-
waku.libp2p.peerStore.removeEventListener("change:protocols", cb);
|
286
|
-
resolve();
|
287
|
-
break;
|
288
|
-
}
|
289
|
-
}
|
290
|
-
};
|
291
|
-
waku.libp2p.peerStore.addEventListener("change:protocols", cb);
|
292
|
-
});
|
293
|
-
}
|
294
|
-
/**
|
295
|
-
* Wait for a peer with the given protocol to be connected and in the gossipsub
|
296
|
-
* mesh.
|
297
|
-
*/
|
298
|
-
async function waitForGossipSubPeerInMesh(waku) {
|
299
|
-
let peers = waku.getMeshPeers();
|
300
|
-
while (peers.length == 0) {
|
301
|
-
await pEvent(waku, "gossipsub:heartbeat");
|
302
|
-
peers = waku.getMeshPeers();
|
303
|
-
}
|
304
|
-
}
|
305
|
-
const awaitTimeout = (ms, rejectReason) => new Promise((_resolve, reject) => setTimeout(() => reject(rejectReason), ms));
|
306
|
-
async function rejectOnTimeout(promise, timeoutMs, rejectReason) {
|
307
|
-
await Promise.race([promise, awaitTimeout(timeoutMs, rejectReason)]);
|
308
|
-
}
|
309
|
-
function getEnabledProtocols(waku) {
|
310
|
-
const protocols = [];
|
311
|
-
if (waku.relay) {
|
312
|
-
protocols.push(Protocols.Relay);
|
313
|
-
}
|
314
|
-
if (waku.filter) {
|
315
|
-
protocols.push(Protocols.Filter);
|
316
|
-
}
|
317
|
-
if (waku.store) {
|
318
|
-
protocols.push(Protocols.Store);
|
319
|
-
}
|
320
|
-
if (waku.lightPush) {
|
321
|
-
protocols.push(Protocols.LightPush);
|
322
|
-
}
|
323
|
-
return protocols;
|
324
|
-
}
|
325
|
-
|
326
|
-
export { waitForRemotePeer };
|