@peerbit/stream 4.0.5-efee9d3 → 4.0.6-171d517
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/README.md +11 -1
- package/dist/benchmark/transfer.js +12 -12
- package/dist/benchmark/transfer.js.map +1 -1
- package/dist/src/index.d.ts +14 -31
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +59 -70
- package/dist/src/index.js.map +1 -1
- package/dist/src/logger.d.ts.map +1 -1
- package/dist/src/logger.js.map +1 -1
- package/dist/src/pushable-lanes.d.ts +1 -0
- package/dist/src/pushable-lanes.d.ts.map +1 -1
- package/dist/src/pushable-lanes.js +4 -4
- package/dist/src/pushable-lanes.js.map +1 -1
- package/dist/src/routes.d.ts.map +1 -1
- package/dist/src/routes.js +5 -2
- package/dist/src/routes.js.map +1 -1
- package/package.json +12 -11
- package/src/index.ts +219 -217
- package/src/logger.ts +1 -1
- package/src/pushable-lanes.ts +9 -8
- package/src/routes.ts +6 -4
package/README.md
CHANGED
|
@@ -1,2 +1,12 @@
|
|
|
1
1
|
# Direct stream
|
|
2
|
-
Data streaming protocol
|
|
2
|
+
Data streaming protocol for delivering messages efficiently to peers with redundancy and failure recovery modes.
|
|
3
|
+
|
|
4
|
+
## Features
|
|
5
|
+
- Packet prioritization. Data packages can be sent with different priority. That allows some traffic to pass through the traffic faster than other. This is useful in case of congestion
|
|
6
|
+
- Mode'd delivery. Packages can be sent with different delivery mode 'Acknowledge' (wait for acknowledgement from all known subscribers), 'Seek' (find new subscribers for a topic), 'Silent' (just deliver).
|
|
7
|
+
- Redundancy in data delivery. Data packages can be sent with different redundancy. This means that you can choose to send some packages only in one path (the fastest) or send a package in the 'N' fastest paths (this gives you a redundancy degree of 'N'). This feature is useful when you want to adapt delivery for unstable networks or when you want to make sure that some messages are delivered with high probability (without waiting for Acknowledgemts and retry)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
Protocol specification (TODO)
|
|
11
|
+
|
|
12
|
+
Currently the protocol itself is not specificed more than as in this implementation with messages types found [here](https://github.com/dao-xyz/peerbit/blob/master/packages/transport/stream-interface/src/messages.ts) and [here](https://github.com/dao-xyz/peerbit/blob/aa577a5e5b2b4920662de5e0efb92fb97a5dc63c/packages/transport/stream/src/index.ts#L1057).
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { tcp } from "@libp2p/tcp";
|
|
2
2
|
import { TestSession } from "@peerbit/libp2p-test-utils";
|
|
3
|
-
import {
|
|
3
|
+
import { SeekDelivery } from "@peerbit/stream-interface";
|
|
4
4
|
import { waitForResolved } from "@peerbit/time";
|
|
5
|
-
import
|
|
5
|
+
import B from "benchmark";
|
|
6
6
|
import crypto from "crypto";
|
|
7
|
-
import {
|
|
8
|
-
// Run with "node --loader ts-node/esm ./
|
|
7
|
+
import { DirectStream, waitForPeers, } from "../src/index.js";
|
|
8
|
+
// Run with "node --loader ts-node/esm ./benchmark/transfer.ts"
|
|
9
9
|
// size: 100byte x 1,727 ops/sec ±2.61% (83 runs sampled)
|
|
10
10
|
// size: 1kb x 1,727 ops/sec ±2.61% (83 runs sampled)
|
|
11
11
|
// size: 1000kb x 104 ops/sec ±2.46% (83 runs sampled)
|
|
@@ -13,13 +13,13 @@ class TestStreamImpl extends DirectStream {
|
|
|
13
13
|
constructor(c) {
|
|
14
14
|
super(c, ["bench/0.0.0"], {
|
|
15
15
|
canRelayMessage: true,
|
|
16
|
-
connectionManager: false
|
|
16
|
+
connectionManager: false,
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
const session = await TestSession.disconnected(4, {
|
|
21
21
|
transports: [tcp()],
|
|
22
|
-
services: { directstream: (c) => new TestStreamImpl(c) }
|
|
22
|
+
services: { directstream: (c) => new TestStreamImpl(c) },
|
|
23
23
|
});
|
|
24
24
|
/*
|
|
25
25
|
┌─┐
|
|
@@ -39,7 +39,7 @@ const session = await TestSession.disconnected(4, {
|
|
|
39
39
|
await session.connect([
|
|
40
40
|
[session.peers[0], session.peers[1]],
|
|
41
41
|
[session.peers[1], session.peers[2]],
|
|
42
|
-
[session.peers[2], session.peers[3]]
|
|
42
|
+
[session.peers[2], session.peers[3]],
|
|
43
43
|
]);
|
|
44
44
|
const stream = (i) => session.peers[i].services.directstream;
|
|
45
45
|
await waitForPeers(stream(0), stream(1));
|
|
@@ -48,8 +48,8 @@ await waitForPeers(stream(2), stream(3));
|
|
|
48
48
|
stream(0).publish(new Uint8Array([123]), {
|
|
49
49
|
mode: new SeekDelivery({
|
|
50
50
|
redundancy: 1,
|
|
51
|
-
to: [stream(session.peers.length - 1).publicKey]
|
|
52
|
-
})
|
|
51
|
+
to: [stream(session.peers.length - 1).publicKey],
|
|
52
|
+
}),
|
|
53
53
|
});
|
|
54
54
|
await waitForResolved(() => stream(0).routes.isReachable(stream(0).publicKeyHash, stream(3).publicKeyHash));
|
|
55
55
|
let suite = new B.Suite();
|
|
@@ -64,7 +64,7 @@ for (const size of sizes) {
|
|
|
64
64
|
const small = crypto.randomBytes(size); // 1kb
|
|
65
65
|
msgMap.set(msgIdFn(small), deferred);
|
|
66
66
|
stream(0).publish(small, {
|
|
67
|
-
to: [stream(session.peers.length - 1).publicKey]
|
|
67
|
+
to: [stream(session.peers.length - 1).publicKey],
|
|
68
68
|
});
|
|
69
69
|
},
|
|
70
70
|
setup: () => {
|
|
@@ -76,7 +76,7 @@ for (const size of sizes) {
|
|
|
76
76
|
},
|
|
77
77
|
teardown: () => {
|
|
78
78
|
stream(session.peers.length - 1).removeEventListener("data", listener);
|
|
79
|
-
}
|
|
79
|
+
},
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
82
|
suite
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transfer.js","sourceRoot":"","sources":["../../benchmark/transfer.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"transfer.js","sourceRoot":"","sources":["../../benchmark/transfer.ts"],"names":[],"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AC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;KACD,EAAE,CAAC,UAAU,EAAE,UAAqB,GAAG,IAAW;IAClD,OAAO,CAAC,IAAI,EAAE,CAAC;AAChB,CAAC,CAAC;KACD,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,26 +1,17 @@
|
|
|
1
1
|
import { TypedEventEmitter } from "@libp2p/interface";
|
|
2
|
+
import type { Connection, Libp2pEvents, PeerId, PeerStore, Stream, TypedEventTarget } from "@libp2p/interface";
|
|
3
|
+
import type { AddressManager, ConnectionManager, IncomingStreamData, Registrar } from "@libp2p/interface-internal";
|
|
4
|
+
import { Cache } from "@peerbit/cache";
|
|
5
|
+
import { PublicSignKey, type SignatureWithKey } from "@peerbit/crypto";
|
|
6
|
+
import { ACK, AcknowledgeDelivery, AnyWhere, DataMessage, Goodbye, type IdentificationOptions, Message, MultiAddrinfo, type PriorityOptions, SeekDelivery, SilentDelivery, type StreamEvents, type WaitForPeer, type WithMode, type WithTo } from "@peerbit/stream-interface";
|
|
7
|
+
import type { Components } from "libp2p/components";
|
|
2
8
|
import Queue from "p-queue";
|
|
3
|
-
import type { PeerId } from "@libp2p/interface";
|
|
4
|
-
import type { Connection } from "@libp2p/interface";
|
|
5
|
-
import { type PushableLanes } from "./pushable-lanes.js";
|
|
6
|
-
import type { Stream } from "@libp2p/interface";
|
|
7
9
|
import { Uint8ArrayList } from "uint8arraylist";
|
|
8
|
-
import { Routes } from "./routes.js";
|
|
9
|
-
import type { IncomingStreamData, Registrar } from "@libp2p/interface-internal";
|
|
10
|
-
import type { AddressManager } from "@libp2p/interface-internal";
|
|
11
|
-
import type { ConnectionManager } from "@libp2p/interface-internal";
|
|
12
|
-
import { type PeerStore } from "@libp2p/interface";
|
|
13
|
-
import { PublicSignKey, SignatureWithKey } from "@peerbit/crypto";
|
|
14
|
-
import type { Components } from "libp2p/components";
|
|
15
|
-
import type { TypedEventTarget } from "@libp2p/interface";
|
|
16
10
|
import { logger } from "./logger.js";
|
|
17
|
-
|
|
18
|
-
import {
|
|
19
|
-
import type { Libp2pEvents } from "@libp2p/interface";
|
|
20
|
-
import { Message as Message, DataMessage, type WaitForPeer, ACK, SeekDelivery, AcknowledgeDelivery, SilentDelivery, Goodbye, type StreamEvents, AnyWhere, type WithTo, type WithMode, type PriorityOptions } from "@peerbit/stream-interface";
|
|
21
|
-
import { MultiAddrinfo } from "@peerbit/stream-interface";
|
|
11
|
+
import { type PushableLanes } from "./pushable-lanes.js";
|
|
12
|
+
import { Routes } from "./routes.js";
|
|
22
13
|
import { BandwidthTracker } from "./stats.js";
|
|
23
|
-
|
|
14
|
+
export { logger };
|
|
24
15
|
export { BandwidthTracker };
|
|
25
16
|
export interface PeerStreamsInit {
|
|
26
17
|
peerId: PeerId;
|
|
@@ -108,6 +99,11 @@ type ConnectionManagerOptions = {
|
|
|
108
99
|
dialer?: DialerOptions;
|
|
109
100
|
pruner?: PrunerOptions;
|
|
110
101
|
};
|
|
102
|
+
export type ConnectionManagerArguments = (Partial<Pick<ConnectionManagerOptions, "minConnections">> & Partial<Pick<ConnectionManagerOptions, "maxConnections">> & {
|
|
103
|
+
pruner?: Partial<PrunerOptions> | false;
|
|
104
|
+
} & {
|
|
105
|
+
dialer?: Partial<DialerOptions> | false;
|
|
106
|
+
}) | false;
|
|
111
107
|
export type DirectStreamOptions = {
|
|
112
108
|
canRelayMessage?: boolean;
|
|
113
109
|
messageProcessingConcurrency?: number;
|
|
@@ -126,11 +122,6 @@ export interface DirectStreamComponents extends Components {
|
|
|
126
122
|
peerStore: PeerStore;
|
|
127
123
|
events: TypedEventTarget<Libp2pEvents>;
|
|
128
124
|
}
|
|
129
|
-
export type ConnectionManagerArguments = (Partial<Pick<ConnectionManagerOptions, "minConnections">> & Partial<Pick<ConnectionManagerOptions, "maxConnections">> & {
|
|
130
|
-
pruner?: Partial<PrunerOptions> | false;
|
|
131
|
-
} & {
|
|
132
|
-
dialer?: Partial<DialerOptions> | false;
|
|
133
|
-
}) | false;
|
|
134
125
|
export declare abstract class DirectStream<Events extends {
|
|
135
126
|
[s: string]: any;
|
|
136
127
|
} = StreamEvents> extends TypedEventEmitter<Events> implements WaitForPeer {
|
|
@@ -192,15 +183,7 @@ export declare abstract class DirectStream<Events extends {
|
|
|
192
183
|
protected onPeerDisconnected(peerId: PeerId, conn?: Connection): Promise<void>;
|
|
193
184
|
removePeerFromRoutes(hash: string, deleteIfNeighbour?: boolean): void;
|
|
194
185
|
addRouteConnection(from: string, neighbour: string, target: PublicSignKey, distance: number, session: number, remoteSession: number): void;
|
|
195
|
-
/**
|
|
196
|
-
* invoked when a new peer becomes reachable
|
|
197
|
-
* @param publicKeyHash
|
|
198
|
-
*/
|
|
199
186
|
onPeerReachable(publicKey: PublicSignKey): void;
|
|
200
|
-
/**
|
|
201
|
-
* invoked when a new peer becomes unreachable
|
|
202
|
-
* @param publicKeyHash
|
|
203
|
-
*/
|
|
204
187
|
onPeerUnreachable(hash: string): void;
|
|
205
188
|
updateSession(key: PublicSignKey, session?: number): void;
|
|
206
189
|
invalidateSession(key: string): void;
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,KAAK,EACX,UAAU,EACV,YAAY,EACZ,MAAM,EACN,SAAS,EACT,MAAM,EACN,gBAAgB,EAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EACX,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,SAAS,EACT,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EA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AAW,CAAC,EAC7C,OAAO,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,IAAI,CAAC;IAoIV,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa;IA4B1D,OAAO,CACZ,IAAI,EAAE,MAAM,GAAG,aAAa,EAC5B,OAAO,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,WAAW,CAAC;QAAC,SAAS,CAAC,EAAE,OAAO,CAAA;KAAE;IAsD1E,IAAI,OAAO,IAAI,OAAO,CAErB;IAGD,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC;IAyBhC,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IAoBvC,cAAc,IAAI,MAAM;CAQxB;AAED,eAAO,MAAM,YAAY,YACf;IACR,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACzD,MAAM,EAAE,MAAM,CAAC;CACf,EAAE,kBAUH,CAAC"}
|
package/dist/src/index.js
CHANGED
|
@@ -1,24 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { CustomEvent, TypedEventEmitter } from "@libp2p/interface";
|
|
2
|
+
import { multiaddr } from "@multiformats/multiaddr";
|
|
3
|
+
import { Cache } from "@peerbit/cache";
|
|
4
|
+
import { PublicSignKey, getKeypairFromPeerId, getPublicKeyFromPeerId, ready, sha256Base64, toBase64, } from "@peerbit/crypto";
|
|
5
|
+
import { ACK, AcknowledgeDelivery, AnyWhere, DataMessage, DeliveryError, Goodbye, Message, MessageHeader, MultiAddrinfo, NotStartedError, SeekDelivery, SilentDelivery, TracedDelivery, deliveryModeHasReceiver, getMsgId, } from "@peerbit/stream-interface";
|
|
6
|
+
import { AbortError, TimeoutError, delay, waitFor } from "@peerbit/time";
|
|
6
7
|
import { abortableSource } from "abortable-iterator";
|
|
7
8
|
import * as lp from "it-length-prefixed";
|
|
8
|
-
import {
|
|
9
|
-
import {} from "@libp2p/interface";
|
|
9
|
+
import { pipe } from "it-pipe";
|
|
10
10
|
import pDefer from "p-defer";
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
import { multiaddr } from "@multiformats/multiaddr";
|
|
11
|
+
import Queue from "p-queue";
|
|
12
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
|
14
13
|
import { logger } from "./logger.js";
|
|
15
|
-
|
|
16
|
-
import {
|
|
17
|
-
import { ready } from "@peerbit/crypto";
|
|
18
|
-
import { Message as Message, DataMessage, getMsgId, ACK, SeekDelivery, AcknowledgeDelivery, SilentDelivery, MessageHeader, Goodbye, TracedDelivery, AnyWhere, NotStartedError, deliveryModeHasReceiver, DeliveryError } from "@peerbit/stream-interface";
|
|
19
|
-
import { MultiAddrinfo } from "@peerbit/stream-interface";
|
|
14
|
+
import { pushableLanes } from "./pushable-lanes.js";
|
|
15
|
+
import { MAX_ROUTE_DISTANCE, Routes } from "./routes.js";
|
|
20
16
|
import { BandwidthTracker } from "./stats.js";
|
|
21
|
-
|
|
17
|
+
export { logger };
|
|
22
18
|
export { BandwidthTracker }; // might be useful for others
|
|
23
19
|
const logError = (e) => {
|
|
24
20
|
return logger.error(e?.message);
|
|
@@ -114,41 +110,40 @@ export class PeerStreams extends TypedEventEmitter {
|
|
|
114
110
|
this.usedBandWidthTracker.add(data.byteLength);
|
|
115
111
|
this.outboundStream.push(data instanceof Uint8Array ? data : data.subarray(), this.outboundStream.getReadableLength(0) === 0
|
|
116
112
|
? 0
|
|
117
|
-
: getLaneFromPriority(priority)
|
|
118
|
-
);
|
|
113
|
+
: getLaneFromPriority(priority));
|
|
119
114
|
}
|
|
120
115
|
async waitForWrite(bytes, priority = 0) {
|
|
121
116
|
if (this.closed) {
|
|
122
|
-
logger.error("Failed to send to stream: " + this.peerId + ". Closed");
|
|
117
|
+
logger.error("Failed to send to stream: " + this.peerId.toString() + ". Closed");
|
|
123
118
|
return;
|
|
124
119
|
}
|
|
125
120
|
if (!this.isWritable) {
|
|
126
121
|
// Catch the event where the outbound stream is attach, but also abort if we shut down
|
|
127
|
-
const outboundPromise = new Promise((
|
|
128
|
-
const
|
|
122
|
+
const outboundPromise = new Promise((resolve, reject) => {
|
|
123
|
+
const resolveClear = () => {
|
|
129
124
|
this.removeEventListener("stream:outbound", listener);
|
|
130
125
|
clearTimeout(timer);
|
|
131
|
-
|
|
126
|
+
resolve();
|
|
132
127
|
};
|
|
133
|
-
const
|
|
128
|
+
const rejectClear = (err) => {
|
|
134
129
|
this.removeEventListener("stream:outbound", listener);
|
|
135
130
|
clearTimeout(timer);
|
|
136
|
-
|
|
131
|
+
reject(err);
|
|
137
132
|
};
|
|
138
133
|
const timer = setTimeout(() => {
|
|
139
|
-
|
|
134
|
+
rejectClear(new Error("Timed out"));
|
|
140
135
|
}, 3 * 1000); // TODO if this timeout > 10s we run into issues in the tests when running in CI
|
|
141
136
|
const abortHandler = () => {
|
|
142
137
|
this.removeEventListener("close", abortHandler);
|
|
143
|
-
|
|
138
|
+
rejectClear(new AbortError("Closed"));
|
|
144
139
|
};
|
|
145
140
|
this.addEventListener("close", abortHandler);
|
|
146
141
|
const listener = () => {
|
|
147
|
-
|
|
142
|
+
resolveClear();
|
|
148
143
|
};
|
|
149
144
|
this.addEventListener("stream:outbound", listener);
|
|
150
145
|
if (this.isWritable) {
|
|
151
|
-
|
|
146
|
+
resolveClear();
|
|
152
147
|
}
|
|
153
148
|
});
|
|
154
149
|
await outboundPromise
|
|
@@ -182,7 +177,7 @@ export class PeerStreams extends TypedEventEmitter {
|
|
|
182
177
|
returnOnAbort: true,
|
|
183
178
|
onReturnError: (err) => {
|
|
184
179
|
logger.error("Inbound stream error", err?.message);
|
|
185
|
-
}
|
|
180
|
+
},
|
|
186
181
|
});
|
|
187
182
|
this.dispatchEvent(new CustomEvent("stream:inbound"));
|
|
188
183
|
return this.inboundStream;
|
|
@@ -270,7 +265,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
270
265
|
constructor(components, multicodecs, options) {
|
|
271
266
|
super();
|
|
272
267
|
this.components = components;
|
|
273
|
-
const { canRelayMessage = true, messageProcessingConcurrency = 10, maxInboundStreams, maxOutboundStreams, connectionManager, routeSeekInterval = ROUTE_UPDATE_DELAY_FACTOR, seekTimeout = SEEK_DELIVERY_TIMEOUT, routeMaxRetentionPeriod = ROUTE_MAX_RETANTION_PERIOD } = options || {};
|
|
268
|
+
const { canRelayMessage = true, messageProcessingConcurrency = 10, maxInboundStreams, maxOutboundStreams, connectionManager, routeSeekInterval = ROUTE_UPDATE_DELAY_FACTOR, seekTimeout = SEEK_DELIVERY_TIMEOUT, routeMaxRetentionPeriod = ROUTE_MAX_RETANTION_PERIOD, } = options || {};
|
|
274
269
|
const signKey = getKeypairFromPeerId(components.peerId);
|
|
275
270
|
this.seekTimeout = seekTimeout;
|
|
276
271
|
this.sign = signKey.sign.bind(signKey);
|
|
@@ -298,7 +293,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
298
293
|
maxConnections: Number.MAX_SAFE_INTEGER,
|
|
299
294
|
minConnections: 0,
|
|
300
295
|
dialer: undefined,
|
|
301
|
-
pruner: undefined
|
|
296
|
+
pruner: undefined,
|
|
302
297
|
};
|
|
303
298
|
}
|
|
304
299
|
else {
|
|
@@ -316,21 +311,21 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
316
311
|
connectionTimeout: DEFAULT_PRUNED_CONNNECTIONS_TIMEOUT,
|
|
317
312
|
interval: DEFAULT_PRUNE_CONNECTIONS_INTERVAL,
|
|
318
313
|
maxBuffer: MAX_QUEUED_BYTES,
|
|
319
|
-
...connectionManager?.pruner
|
|
314
|
+
...connectionManager?.pruner,
|
|
320
315
|
}
|
|
321
|
-
: undefined
|
|
316
|
+
: undefined,
|
|
322
317
|
};
|
|
323
318
|
}
|
|
324
319
|
this.recentDials = this.connectionManagerOptions.dialer
|
|
325
320
|
? new Cache({
|
|
326
321
|
ttl: this.connectionManagerOptions.dialer.retryDelay,
|
|
327
|
-
max: 1e3
|
|
322
|
+
max: 1e3,
|
|
328
323
|
})
|
|
329
324
|
: undefined;
|
|
330
325
|
this.prunedConnectionsCache = this.connectionManagerOptions.pruner
|
|
331
326
|
? new Cache({
|
|
332
327
|
max: 1e6,
|
|
333
|
-
ttl: this.connectionManagerOptions.pruner.connectionTimeout
|
|
328
|
+
ttl: this.connectionManagerOptions.pruner.connectionTimeout,
|
|
334
329
|
})
|
|
335
330
|
: undefined;
|
|
336
331
|
}
|
|
@@ -343,7 +338,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
343
338
|
this.closeController = new AbortController();
|
|
344
339
|
this.routes = new Routes(this.publicKeyHash, {
|
|
345
340
|
routeMaxRetentionPeriod: this.routeMaxRetentionPeriod,
|
|
346
|
-
signal: this.closeController.signal
|
|
341
|
+
signal: this.closeController.signal,
|
|
347
342
|
});
|
|
348
343
|
this.started = true;
|
|
349
344
|
this.stopping = false;
|
|
@@ -353,14 +348,14 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
353
348
|
await Promise.all(this.multicodecs.map((multicodec) => this.components.registrar.handle(multicodec, this._onIncomingStream, {
|
|
354
349
|
maxInboundStreams: this.maxInboundStreams,
|
|
355
350
|
maxOutboundStreams: this.maxOutboundStreams,
|
|
356
|
-
runOnTransientConnection: false
|
|
351
|
+
runOnTransientConnection: false,
|
|
357
352
|
})));
|
|
358
353
|
// register protocol with topology
|
|
359
354
|
// Topology callbacks called on connection manager changes
|
|
360
355
|
this._registrarTopologyIds = await Promise.all(this.multicodecs.map((multicodec) => this.components.registrar.register(multicodec, {
|
|
361
356
|
onConnect: this.onPeerConnected.bind(this),
|
|
362
357
|
onDisconnect: this.onPeerDisconnected.bind(this),
|
|
363
|
-
notifyOnTransient: false
|
|
358
|
+
notifyOnTransient: false,
|
|
364
359
|
})));
|
|
365
360
|
// All existing connections are like new ones for us. To deduplication on remotes so we only resuse one connection for this protocol (we could be connected with many connections)
|
|
366
361
|
const peerToConnections = new Map();
|
|
@@ -565,8 +560,8 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
565
560
|
leaving: [peerKeyHash],
|
|
566
561
|
header: new MessageHeader({
|
|
567
562
|
session: this.session,
|
|
568
|
-
mode: new SilentDelivery({ to: dependent, redundancy: 2 })
|
|
569
|
-
})
|
|
563
|
+
mode: new SilentDelivery({ to: dependent, redundancy: 2 }),
|
|
564
|
+
}),
|
|
570
565
|
}).sign(this.sign));
|
|
571
566
|
}
|
|
572
567
|
this.checkIsAlive([peerKeyHash]);
|
|
@@ -595,24 +590,16 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
595
590
|
}
|
|
596
591
|
}
|
|
597
592
|
}
|
|
598
|
-
/**
|
|
599
|
-
* invoked when a new peer becomes reachable
|
|
600
|
-
* @param publicKeyHash
|
|
601
|
-
*/
|
|
602
593
|
onPeerReachable(publicKey) {
|
|
603
594
|
// override this fn
|
|
604
595
|
this.dispatchEvent(new CustomEvent("peer:reachable", { detail: publicKey }));
|
|
605
596
|
}
|
|
606
|
-
/**
|
|
607
|
-
* invoked when a new peer becomes unreachable
|
|
608
|
-
* @param publicKeyHash
|
|
609
|
-
*/
|
|
610
597
|
onPeerUnreachable(hash) {
|
|
611
598
|
// override this fn
|
|
612
599
|
this.dispatchEvent(
|
|
613
600
|
// TODO types
|
|
614
601
|
new CustomEvent("peer:unreachable", {
|
|
615
|
-
detail: this.peerKeyHashToPublicKey.get(hash)
|
|
602
|
+
detail: this.peerKeyHashToPublicKey.get(hash),
|
|
616
603
|
}));
|
|
617
604
|
}
|
|
618
605
|
updateSession(key, session) {
|
|
@@ -627,7 +614,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
627
614
|
this.dispatchEvent(
|
|
628
615
|
// TODO types
|
|
629
616
|
new CustomEvent("peer:session", {
|
|
630
|
-
detail: key
|
|
617
|
+
detail: key,
|
|
631
618
|
}));
|
|
632
619
|
}
|
|
633
620
|
/**
|
|
@@ -649,12 +636,12 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
649
636
|
peerId,
|
|
650
637
|
publicKey,
|
|
651
638
|
protocol,
|
|
652
|
-
connId
|
|
639
|
+
connId,
|
|
653
640
|
});
|
|
654
641
|
this.peers.set(publicKeyHash, peerStreams);
|
|
655
642
|
this.updateSession(publicKey, -1);
|
|
656
643
|
peerStreams.addEventListener("close", () => this._removePeer(publicKey), {
|
|
657
|
-
once: true
|
|
644
|
+
once: true,
|
|
658
645
|
});
|
|
659
646
|
return peerStreams;
|
|
660
647
|
}
|
|
@@ -731,7 +718,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
731
718
|
// Ensure the message is valid before processing it
|
|
732
719
|
const message = Message.from(msg);
|
|
733
720
|
this.dispatchEvent(new CustomEvent("message", {
|
|
734
|
-
detail: message
|
|
721
|
+
detail: message,
|
|
735
722
|
}));
|
|
736
723
|
if (message instanceof DataMessage) {
|
|
737
724
|
// DONT await this since it might introduce a dead-lock
|
|
@@ -787,7 +774,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
787
774
|
await this.maybeAcknowledgeMessage(peerStream, message, seenBefore);
|
|
788
775
|
if (seenBefore === 0 && message.data) {
|
|
789
776
|
this.dispatchEvent(new CustomEvent("data", {
|
|
790
|
-
detail: message
|
|
777
|
+
detail: message,
|
|
791
778
|
}));
|
|
792
779
|
}
|
|
793
780
|
}
|
|
@@ -805,7 +792,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
805
792
|
// DONT await this since it might introduce a dead-lock
|
|
806
793
|
if (message.header.mode instanceof SeekDelivery) {
|
|
807
794
|
if (seenBefore < message.header.mode.redundancy) {
|
|
808
|
-
const to = [...this.peers.values()].filter((x) => !message.header.signatures?.publicKeys.find((y) => y.equals(x.publicKey)) && x
|
|
795
|
+
const to = [...this.peers.values()].filter((x) => !message.header.signatures?.publicKeys.find((y) => y.equals(x.publicKey)) && x !== peerStream);
|
|
809
796
|
if (to.length > 0) {
|
|
810
797
|
this.relayMessage(from, message, to);
|
|
811
798
|
}
|
|
@@ -853,8 +840,8 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
853
840
|
? new MultiAddrinfo(this.components.addressManager
|
|
854
841
|
.getAddresses()
|
|
855
842
|
.map((x) => x.toString()))
|
|
856
|
-
: undefined
|
|
857
|
-
})
|
|
843
|
+
: undefined,
|
|
844
|
+
}),
|
|
858
845
|
}).sign(this.sign), [peerStream]);
|
|
859
846
|
}
|
|
860
847
|
}
|
|
@@ -943,8 +930,8 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
943
930
|
return this.publish(undefined, {
|
|
944
931
|
mode: new SeekDelivery({
|
|
945
932
|
to: remotes,
|
|
946
|
-
redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY
|
|
947
|
-
})
|
|
933
|
+
redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY,
|
|
934
|
+
}),
|
|
948
935
|
})
|
|
949
936
|
.then(() => true)
|
|
950
937
|
.catch((e) => {
|
|
@@ -973,7 +960,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
973
960
|
? options.mode
|
|
974
961
|
: new SilentDelivery({
|
|
975
962
|
to: options.to,
|
|
976
|
-
redundancy: DEFAULT_SILENT_MESSAGE_REDUDANCY
|
|
963
|
+
redundancy: DEFAULT_SILENT_MESSAGE_REDUDANCY,
|
|
977
964
|
});
|
|
978
965
|
if (mode instanceof AcknowledgeDelivery ||
|
|
979
966
|
mode instanceof SilentDelivery ||
|
|
@@ -994,7 +981,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
994
981
|
!this.routes.isUpToDate(hash, neighbourRoutes)) {
|
|
995
982
|
mode = new SeekDelivery({
|
|
996
983
|
to: mode.to,
|
|
997
|
-
redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY
|
|
984
|
+
redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY,
|
|
998
985
|
});
|
|
999
986
|
break;
|
|
1000
987
|
}
|
|
@@ -1006,8 +993,8 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1006
993
|
id: options.id,
|
|
1007
994
|
mode,
|
|
1008
995
|
session: this.session,
|
|
1009
|
-
priority: options.priority
|
|
1010
|
-
})
|
|
996
|
+
priority: options.priority,
|
|
997
|
+
}),
|
|
1011
998
|
});
|
|
1012
999
|
// TODO allow messages to also be sent unsigned (signaturePolicy property)
|
|
1013
1000
|
await message.sign(this.sign);
|
|
@@ -1017,7 +1004,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1017
1004
|
* Publishes messages to all peers
|
|
1018
1005
|
*/
|
|
1019
1006
|
async publish(data, options = {
|
|
1020
|
-
mode: new SeekDelivery({ redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY })
|
|
1007
|
+
mode: new SeekDelivery({ redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY }),
|
|
1021
1008
|
}) {
|
|
1022
1009
|
if (!this.started) {
|
|
1023
1010
|
throw new NotStartedError();
|
|
@@ -1081,7 +1068,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1081
1068
|
const haveReceivers = messageToSet.size > 0;
|
|
1082
1069
|
if (haveReceivers && this.peers.size === 0) {
|
|
1083
1070
|
return {
|
|
1084
|
-
promise: Promise.reject(new DeliveryError("Cannnot deliver message to peers because there are no peers to deliver to"))
|
|
1071
|
+
promise: Promise.reject(new DeliveryError("Cannnot deliver message to peers because there are no peers to deliver to")),
|
|
1085
1072
|
};
|
|
1086
1073
|
}
|
|
1087
1074
|
const deliveryDeferredPromise = pDefer();
|
|
@@ -1129,7 +1116,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1129
1116
|
}
|
|
1130
1117
|
if (!hasAll && willGetAllAcknowledgements) {
|
|
1131
1118
|
deliveryDeferredPromise.reject(new DeliveryError(`Failed to get message ${idString} ${filterMessageForSeenCounter} ${[
|
|
1132
|
-
...messageToSet
|
|
1119
|
+
...messageToSet,
|
|
1133
1120
|
]} delivery acknowledges from all nodes (${fastestNodesReached.size}/${messageToSet.size}). Mode: ${message.header.mode.constructor.name}. Redundancy: ${message.header.mode["redundancy"]}`));
|
|
1134
1121
|
}
|
|
1135
1122
|
else {
|
|
@@ -1185,7 +1172,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1185
1172
|
clear: () => {
|
|
1186
1173
|
clear();
|
|
1187
1174
|
deliveryDeferredPromise.resolve();
|
|
1188
|
-
}
|
|
1175
|
+
},
|
|
1189
1176
|
});
|
|
1190
1177
|
return deliveryDeferredPromise;
|
|
1191
1178
|
}
|
|
@@ -1291,7 +1278,9 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1291
1278
|
.map((x) => multiaddr(x));
|
|
1292
1279
|
if (addresses.length > 0) {
|
|
1293
1280
|
try {
|
|
1294
|
-
await this.components.connectionManager.openConnection(
|
|
1281
|
+
await this.components.connectionManager.openConnection([
|
|
1282
|
+
addresses[addresses.length - 1],
|
|
1283
|
+
]);
|
|
1295
1284
|
}
|
|
1296
1285
|
catch (error) {
|
|
1297
1286
|
logger.info("Failed to connect directly to: " +
|
|
@@ -1314,7 +1303,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1314
1303
|
return true;
|
|
1315
1304
|
}, {
|
|
1316
1305
|
signal: options?.signal,
|
|
1317
|
-
timeout: options?.timeout ?? 10 * 1000
|
|
1306
|
+
timeout: options?.timeout ?? 10 * 1000,
|
|
1318
1307
|
});
|
|
1319
1308
|
}
|
|
1320
1309
|
catch (error) {
|
|
@@ -1331,7 +1320,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1331
1320
|
// Dontwait for readlable https://github.com/libp2p/js-libp2p/issues/2321
|
|
1332
1321
|
await waitFor(() => /* stream.isReadable && */ stream.isWritable, {
|
|
1333
1322
|
signal: options?.signal,
|
|
1334
|
-
timeout: options?.timeout ?? 10 * 1000
|
|
1323
|
+
timeout: options?.timeout ?? 10 * 1000,
|
|
1335
1324
|
});
|
|
1336
1325
|
}
|
|
1337
1326
|
catch (error) {
|