@waku/core 0.0.33-09028e7.0 → 0.0.33-11bf1c1.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-CS0EDeEY.js → base_protocol-Dzv-QHPR.js} +93 -78
- package/bundle/{index-BVysxsMu.js → index-BIW3qNYx.js} +19 -12
- package/bundle/index.js +45 -419
- package/bundle/lib/base_protocol.js +2 -2
- package/bundle/lib/message/version_0.js +2 -2
- package/bundle/{version_0-C5ObpJ_0.js → version_0-CdmZMfkQ.js} +10 -4
- 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 +1 -1
- package/dist/lib/base_protocol.js +1 -5
- package/dist/lib/base_protocol.js.map +1 -1
- package/dist/lib/filter/index.d.ts +1 -2
- package/dist/lib/filter/index.js +2 -5
- 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 +1 -7
- package/src/lib/filter/index.ts +2 -10
- package/src/lib/stream_manager/stream_manager.ts +124 -66
- package/src/lib/stream_manager/utils.ts +5 -17
- 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,4 +1,4 @@
|
|
1
|
-
import { g as bytesToUtf8, T as Tags, L as Logger } from './index-
|
1
|
+
import { g as bytesToUtf8, T as Tags, L as Logger } from './index-BIW3qNYx.js';
|
2
2
|
|
3
3
|
/**
|
4
4
|
* Function to sort peers by latency from lowest to highest
|
@@ -80,110 +80,129 @@ function filterPeersByDiscovery(peers, numPeers, maxBootstrapPeers) {
|
|
80
80
|
return selectedPeers;
|
81
81
|
}
|
82
82
|
|
83
|
-
function
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
let latestConnection;
|
89
|
-
connections.forEach((connection) => {
|
90
|
-
if (connection.status === "open") {
|
91
|
-
if (!latestConnection) {
|
92
|
-
latestConnection = connection;
|
93
|
-
}
|
94
|
-
else if (connection.timeline.open > latestConnection.timeline.open) {
|
95
|
-
latestConnection = connection;
|
96
|
-
}
|
97
|
-
}
|
98
|
-
});
|
99
|
-
return latestConnection;
|
83
|
+
function selectOpenConnection(connections) {
|
84
|
+
return connections
|
85
|
+
.filter((c) => c.status === "open")
|
86
|
+
.sort((left, right) => right.timeline.open - left.timeline.open)
|
87
|
+
.at(0);
|
100
88
|
}
|
101
89
|
|
102
|
-
const
|
103
|
-
const RETRY_BACKOFF_BASE = 1_000;
|
104
|
-
const MAX_RETRIES = 3;
|
90
|
+
const STREAM_LOCK_KEY = "consumed";
|
105
91
|
class StreamManager {
|
106
92
|
multicodec;
|
107
93
|
getConnections;
|
108
94
|
addEventListener;
|
109
|
-
streamPool;
|
110
95
|
log;
|
96
|
+
ongoingCreation = new Set();
|
97
|
+
streamPool = new Map();
|
111
98
|
constructor(multicodec, getConnections, addEventListener) {
|
112
99
|
this.multicodec = multicodec;
|
113
100
|
this.getConnections = getConnections;
|
114
101
|
this.addEventListener = addEventListener;
|
115
102
|
this.log = new Logger(`stream-manager:${multicodec}`);
|
116
|
-
this.streamPool = new Map();
|
117
103
|
this.addEventListener("peer:update", this.handlePeerUpdateStreamPool);
|
118
104
|
}
|
119
105
|
async getStream(peer) {
|
120
|
-
const
|
121
|
-
const
|
122
|
-
if (
|
123
|
-
|
106
|
+
const peerId = peer.id.toString();
|
107
|
+
const scheduledStream = this.streamPool.get(peerId);
|
108
|
+
if (scheduledStream) {
|
109
|
+
this.streamPool.delete(peerId);
|
110
|
+
await scheduledStream;
|
124
111
|
}
|
125
|
-
this.
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
return stream;
|
131
|
-
}
|
132
|
-
}
|
133
|
-
catch (error) {
|
134
|
-
this.log.warn(`Failed to get stream for ${peerIdStr} -- `, error);
|
135
|
-
this.log.warn("Attempting to create a new stream for the peer");
|
112
|
+
let stream = this.getOpenStreamForCodec(peer.id);
|
113
|
+
if (stream) {
|
114
|
+
this.log.info(`Found existing stream peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
115
|
+
this.lockStream(peer.id.toString(), stream);
|
116
|
+
return stream;
|
136
117
|
}
|
137
|
-
|
118
|
+
stream = await this.createStream(peer);
|
119
|
+
this.lockStream(peer.id.toString(), stream);
|
120
|
+
return stream;
|
138
121
|
}
|
139
122
|
async createStream(peer, retries = 0) {
|
140
123
|
const connections = this.getConnections(peer.id);
|
141
|
-
const connection =
|
124
|
+
const connection = selectOpenConnection(connections);
|
142
125
|
if (!connection) {
|
143
|
-
throw new Error(
|
126
|
+
throw new Error(`Failed to get a connection to the peer peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
127
|
+
}
|
128
|
+
let lastError;
|
129
|
+
let stream;
|
130
|
+
for (let i = 0; i < retries + 1; i++) {
|
131
|
+
try {
|
132
|
+
this.log.info(`Attempting to create a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
133
|
+
stream = await connection.newStream(this.multicodec);
|
134
|
+
this.log.info(`Created stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
135
|
+
break;
|
136
|
+
}
|
137
|
+
catch (error) {
|
138
|
+
lastError = error;
|
139
|
+
}
|
140
|
+
}
|
141
|
+
if (!stream) {
|
142
|
+
throw new Error(`Failed to create a new stream for ${peer.id.toString()} -- ` +
|
143
|
+
lastError);
|
144
|
+
}
|
145
|
+
return stream;
|
146
|
+
}
|
147
|
+
async createStreamWithLock(peer) {
|
148
|
+
const peerId = peer.id.toString();
|
149
|
+
if (this.ongoingCreation.has(peerId)) {
|
150
|
+
this.log.info(`Skipping creation of a stream due to lock for peerId=${peerId} multicodec=${this.multicodec}`);
|
151
|
+
return;
|
144
152
|
}
|
145
153
|
try {
|
146
|
-
|
154
|
+
this.ongoingCreation.add(peerId);
|
155
|
+
await this.createStream(peer);
|
147
156
|
}
|
148
157
|
catch (error) {
|
149
|
-
|
150
|
-
const backoff = RETRY_BACKOFF_BASE * Math.pow(2, retries);
|
151
|
-
await new Promise((resolve) => setTimeout(resolve, backoff));
|
152
|
-
return this.createStream(peer, retries + 1);
|
153
|
-
}
|
154
|
-
throw new Error(`Failed to create a new stream for ${peer.id.toString()} -- ` + error);
|
158
|
+
this.log.error(`Failed to createStreamWithLock:`, error);
|
155
159
|
}
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
this.createStream(peer),
|
161
|
-
timeoutPromise.then(() => {
|
162
|
-
throw new Error("Connection timeout");
|
163
|
-
})
|
164
|
-
]).catch((error) => {
|
165
|
-
this.log.error(`Failed to prepare a new stream for ${peer.id.toString()} -- `, error);
|
166
|
-
});
|
167
|
-
this.streamPool.set(peer.id.toString(), streamPromise);
|
160
|
+
finally {
|
161
|
+
this.ongoingCreation.delete(peerId);
|
162
|
+
}
|
163
|
+
return;
|
168
164
|
}
|
169
165
|
handlePeerUpdateStreamPool = (evt) => {
|
170
166
|
const { peer } = evt.detail;
|
171
|
-
if (peer.protocols.includes(this.multicodec)) {
|
172
|
-
|
173
|
-
if (isConnected) {
|
174
|
-
this.log.info(`Preemptively opening a stream to ${peer.id.toString()}`);
|
175
|
-
this.prepareStream(peer);
|
176
|
-
}
|
177
|
-
else {
|
178
|
-
const peerIdStr = peer.id.toString();
|
179
|
-
this.streamPool.delete(peerIdStr);
|
180
|
-
this.log.info(`Removed pending stream for disconnected peer ${peerIdStr}`);
|
181
|
-
}
|
167
|
+
if (!peer.protocols.includes(this.multicodec)) {
|
168
|
+
return;
|
182
169
|
}
|
170
|
+
const stream = this.getOpenStreamForCodec(peer.id);
|
171
|
+
if (stream) {
|
172
|
+
return;
|
173
|
+
}
|
174
|
+
this.scheduleNewStream(peer);
|
183
175
|
};
|
184
|
-
|
176
|
+
scheduleNewStream(peer) {
|
177
|
+
this.log.info(`Scheduling creation of a stream for peerId=${peer.id.toString()} multicodec=${this.multicodec}`);
|
178
|
+
// abandon previous attempt
|
179
|
+
if (this.streamPool.has(peer.id.toString())) {
|
180
|
+
this.streamPool.delete(peer.id.toString());
|
181
|
+
}
|
182
|
+
this.streamPool.set(peer.id.toString(), this.createStreamWithLock(peer));
|
183
|
+
}
|
184
|
+
getOpenStreamForCodec(peerId) {
|
185
185
|
const connections = this.getConnections(peerId);
|
186
|
-
|
186
|
+
const connection = selectOpenConnection(connections);
|
187
|
+
if (!connection) {
|
188
|
+
return;
|
189
|
+
}
|
190
|
+
const stream = connection.streams.find((s) => s.protocol === this.multicodec);
|
191
|
+
if (!stream) {
|
192
|
+
return;
|
193
|
+
}
|
194
|
+
const isStreamUnusable = ["done", "closed", "closing"].includes(stream.writeStatus || "");
|
195
|
+
if (isStreamUnusable || this.isStreamLocked(stream)) {
|
196
|
+
return;
|
197
|
+
}
|
198
|
+
return stream;
|
199
|
+
}
|
200
|
+
lockStream(peerId, stream) {
|
201
|
+
this.log.info(`Locking stream for peerId:${peerId}\tstreamId:${stream.id}`);
|
202
|
+
stream.metadata[STREAM_LOCK_KEY] = true;
|
203
|
+
}
|
204
|
+
isStreamLocked(stream) {
|
205
|
+
return !!stream.metadata[STREAM_LOCK_KEY];
|
187
206
|
}
|
188
207
|
}
|
189
208
|
|
@@ -211,7 +230,6 @@ class BaseProtocol {
|
|
211
230
|
async getStream(peer) {
|
212
231
|
return this.streamManager.getStream(peer);
|
213
232
|
}
|
214
|
-
//TODO: move to SDK
|
215
233
|
/**
|
216
234
|
* Returns known peers from the address book (`libp2p.peerStore`) that support
|
217
235
|
* the class protocol. Waku may or may not be currently connected to these
|
@@ -220,13 +238,10 @@ class BaseProtocol {
|
|
220
238
|
async allPeers() {
|
221
239
|
return getPeersForProtocol(this.components.peerStore, [this.multicodec]);
|
222
240
|
}
|
223
|
-
async connectedPeers(
|
241
|
+
async connectedPeers() {
|
224
242
|
const peers = await this.allPeers();
|
225
243
|
return peers.filter((peer) => {
|
226
244
|
const connections = this.components.connectionManager.getConnections(peer.id);
|
227
|
-
if (withOpenStreams) {
|
228
|
-
return connections.some((c) => c.streams.some((s) => s.protocol === this.multicodec));
|
229
|
-
}
|
230
245
|
return connections.length > 0;
|
231
246
|
});
|
232
247
|
}
|
@@ -236,11 +236,12 @@ class Decoder {
|
|
236
236
|
constructor(name, prefix, baseDecode) {
|
237
237
|
this.name = name;
|
238
238
|
this.prefix = prefix;
|
239
|
+
const prefixCodePoint = prefix.codePointAt(0);
|
239
240
|
/* c8 ignore next 3 */
|
240
|
-
if (
|
241
|
+
if (prefixCodePoint === undefined) {
|
241
242
|
throw new Error('Invalid prefix character');
|
242
243
|
}
|
243
|
-
this.prefixCodePoint =
|
244
|
+
this.prefixCodePoint = prefixCodePoint;
|
244
245
|
this.baseDecode = baseDecode;
|
245
246
|
}
|
246
247
|
decode(text) {
|
@@ -444,7 +445,14 @@ var base2$1 = /*#__PURE__*/Object.freeze({
|
|
444
445
|
|
445
446
|
const alphabet = Array.from('🚀🪐☄🛰🌌🌑🌒🌓🌔🌕🌖🌗🌘🌍🌏🌎🐉☀💻🖥💾💿😂❤😍🤣😊🙏💕😭😘👍😅👏😁🔥🥰💔💖💙😢🤔😆🙄💪😉☺👌🤗💜😔😎😇🌹🤦🎉💞✌✨🤷😱😌🌸🙌😋💗💚😏💛🙂💓🤩😄😀🖤😃💯🙈👇🎶😒🤭❣😜💋👀😪😑💥🙋😞😩😡🤪👊🥳😥🤤👉💃😳✋😚😝😴🌟😬🙃🍀🌷😻😓⭐✅🥺🌈😈🤘💦✔😣🏃💐☹🎊💘😠☝😕🌺🎂🌻😐🖕💝🙊😹🗣💫💀👑🎵🤞😛🔴😤🌼😫⚽🤙☕🏆🤫👈😮🙆🍻🍃🐶💁😲🌿🧡🎁⚡🌞🎈❌✊👋😰🤨😶🤝🚶💰🍓💢🤟🙁🚨💨🤬✈🎀🍺🤓😙💟🌱😖👶🥴▶➡❓💎💸⬇😨🌚🦋😷🕺⚠🙅😟😵👎🤲🤠🤧📌🔵💅🧐🐾🍒😗🤑🌊🤯🐷☎💧😯💆👆🎤🙇🍑❄🌴💣🐸💌📍🥀🤢👅💡💩👐📸👻🤐🤮🎼🥵🚩🍎🍊👼💍📣🥂');
|
446
447
|
const alphabetBytesToChars = (alphabet.reduce((p, c, i) => { p[i] = c; return p; }, ([])));
|
447
|
-
const alphabetCharsToBytes = (alphabet.reduce((p, c, i) => {
|
448
|
+
const alphabetCharsToBytes = (alphabet.reduce((p, c, i) => {
|
449
|
+
const codePoint = c.codePointAt(0);
|
450
|
+
if (codePoint == null) {
|
451
|
+
throw new Error(`Invalid character: ${c}`);
|
452
|
+
}
|
453
|
+
p[codePoint] = i;
|
454
|
+
return p;
|
455
|
+
}, ([])));
|
448
456
|
function encode(data) {
|
449
457
|
return data.reduce((p, c) => {
|
450
458
|
p += alphabetBytesToChars[c];
|
@@ -454,8 +462,12 @@ function encode(data) {
|
|
454
462
|
function decode(str) {
|
455
463
|
const byts = [];
|
456
464
|
for (const char of str) {
|
457
|
-
const
|
458
|
-
if (
|
465
|
+
const codePoint = char.codePointAt(0);
|
466
|
+
if (codePoint == null) {
|
467
|
+
throw new Error(`Invalid character: ${char}`);
|
468
|
+
}
|
469
|
+
const byt = alphabetCharsToBytes[codePoint];
|
470
|
+
if (byt == null) {
|
459
471
|
throw new Error(`Non-base256emoji character: ${char}`);
|
460
472
|
}
|
461
473
|
byts.push(byt);
|
@@ -895,7 +907,7 @@ function requireMs () {
|
|
895
907
|
* @api public
|
896
908
|
*/
|
897
909
|
|
898
|
-
ms = function(val, options) {
|
910
|
+
ms = function (val, options) {
|
899
911
|
options = options || {};
|
900
912
|
var type = typeof val;
|
901
913
|
if (type === 'string' && val.length > 0) {
|
@@ -1588,7 +1600,6 @@ var debug = /*@__PURE__*/getDefaultExportFromCjs(browserExports);
|
|
1588
1600
|
|
1589
1601
|
const APP_NAME = "waku";
|
1590
1602
|
class Logger {
|
1591
|
-
_debug;
|
1592
1603
|
_info;
|
1593
1604
|
_warn;
|
1594
1605
|
_error;
|
@@ -1596,14 +1607,10 @@ class Logger {
|
|
1596
1607
|
return prefix ? `${APP_NAME}:${level}:${prefix}` : `${APP_NAME}:${level}`;
|
1597
1608
|
}
|
1598
1609
|
constructor(prefix) {
|
1599
|
-
this._debug = debug(Logger.createDebugNamespace("debug", prefix));
|
1600
1610
|
this._info = debug(Logger.createDebugNamespace("info", prefix));
|
1601
1611
|
this._warn = debug(Logger.createDebugNamespace("warn", prefix));
|
1602
1612
|
this._error = debug(Logger.createDebugNamespace("error", prefix));
|
1603
1613
|
}
|
1604
|
-
get debug() {
|
1605
|
-
return this._debug;
|
1606
|
-
}
|
1607
1614
|
get info() {
|
1608
1615
|
return this._info;
|
1609
1616
|
}
|
@@ -1619,4 +1626,4 @@ class Logger {
|
|
1619
1626
|
}
|
1620
1627
|
}
|
1621
1628
|
|
1622
|
-
export { EPeersByDiscoveryEvents as E, HealthStatus as H, Logger as L, ProtocolError as P, Tags as T, allocUnsafe as a, alloc as b,
|
1629
|
+
export { EPeersByDiscoveryEvents as E, HealthStatus as H, Logger as L, ProtocolError as P, Tags as T, allocUnsafe as a, alloc as b, EConnectionStateEvents as c, Protocols as d, concat as e, fromString as f, bytesToUtf8 as g, utf8ToBytes as u };
|