@peerbit/stream 4.0.5 → 4.0.6-2b760c3
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.d.ts +2 -0
- package/dist/benchmark/transfer.d.ts.map +1 -0
- package/dist/benchmark/transfer.js +90 -0
- package/dist/benchmark/transfer.js.map +1 -0
- package/{lib/esm → dist/src}/index.d.ts +19 -36
- package/dist/src/index.d.ts.map +1 -0
- package/{lib/esm → dist/src}/index.js +61 -70
- package/dist/src/index.js.map +1 -0
- package/{lib/esm → dist/src}/logger.d.ts +1 -0
- package/dist/src/logger.d.ts.map +1 -0
- package/{lib/esm → dist/src}/logger.js.map +1 -1
- package/{lib/esm → dist/src}/pushable-lanes.d.ts +2 -0
- package/dist/src/pushable-lanes.d.ts.map +1 -0
- package/{lib/esm → dist/src}/pushable-lanes.js +4 -4
- package/dist/src/pushable-lanes.js.map +1 -0
- package/{lib/esm → dist/src}/routes.d.ts +2 -1
- package/dist/src/routes.d.ts.map +1 -0
- package/{lib/esm → dist/src}/routes.js +8 -5
- package/{lib/esm → dist/src}/routes.js.map +1 -1
- package/{lib/esm → dist/src}/stats.d.ts +1 -0
- package/dist/src/stats.d.ts.map +1 -0
- package/package.json +93 -67
- package/src/index.ts +184 -186
- package/src/logger.ts +1 -0
- package/src/pushable-lanes.ts +9 -8
- package/src/routes.ts +9 -7
- package/LICENSE +0 -202
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/pushable-lanes.js.map +0 -1
- /package/{lib/esm → dist/src}/logger.js +0 -0
- /package/{lib/esm → dist/src}/stats.js +0 -0
- /package/{lib/esm → dist/src}/stats.js.map +0 -0
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).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transfer.d.ts","sourceRoot":"","sources":["../../benchmark/transfer.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { tcp } from "@libp2p/tcp";
|
|
2
|
+
import { TestSession } from "@peerbit/libp2p-test-utils";
|
|
3
|
+
import { SeekDelivery } from "@peerbit/stream-interface";
|
|
4
|
+
import { waitForResolved } from "@peerbit/time";
|
|
5
|
+
import B from "benchmark";
|
|
6
|
+
import crypto from "crypto";
|
|
7
|
+
import { DirectStream, waitForPeers, } from "../src/index.js";
|
|
8
|
+
// Run with "node --loader ts-node/esm ./benchmark/transfer.ts"
|
|
9
|
+
// size: 100byte x 1,727 ops/sec ±2.61% (83 runs sampled)
|
|
10
|
+
// size: 1kb x 1,727 ops/sec ±2.61% (83 runs sampled)
|
|
11
|
+
// size: 1000kb x 104 ops/sec ±2.46% (83 runs sampled)
|
|
12
|
+
class TestStreamImpl extends DirectStream {
|
|
13
|
+
constructor(c) {
|
|
14
|
+
super(c, ["bench/0.0.0"], {
|
|
15
|
+
canRelayMessage: true,
|
|
16
|
+
connectionManager: false,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const session = await TestSession.disconnected(4, {
|
|
21
|
+
transports: [tcp()],
|
|
22
|
+
services: { directstream: (c) => new TestStreamImpl(c) },
|
|
23
|
+
});
|
|
24
|
+
/*
|
|
25
|
+
┌─┐
|
|
26
|
+
│1│
|
|
27
|
+
└┬┘
|
|
28
|
+
┌▽┐
|
|
29
|
+
│2│
|
|
30
|
+
└┬┘
|
|
31
|
+
┌▽┐
|
|
32
|
+
│3│
|
|
33
|
+
└┬┘
|
|
34
|
+
┌▽┐
|
|
35
|
+
│4│
|
|
36
|
+
└─┘
|
|
37
|
+
|
|
38
|
+
*/
|
|
39
|
+
await session.connect([
|
|
40
|
+
[session.peers[0], session.peers[1]],
|
|
41
|
+
[session.peers[1], session.peers[2]],
|
|
42
|
+
[session.peers[2], session.peers[3]],
|
|
43
|
+
]);
|
|
44
|
+
const stream = (i) => session.peers[i].services.directstream;
|
|
45
|
+
await waitForPeers(stream(0), stream(1));
|
|
46
|
+
await waitForPeers(stream(1), stream(2));
|
|
47
|
+
await waitForPeers(stream(2), stream(3));
|
|
48
|
+
stream(0).publish(new Uint8Array([123]), {
|
|
49
|
+
mode: new SeekDelivery({
|
|
50
|
+
redundancy: 1,
|
|
51
|
+
to: [stream(session.peers.length - 1).publicKey],
|
|
52
|
+
}),
|
|
53
|
+
});
|
|
54
|
+
await waitForResolved(() => stream(0).routes.isReachable(stream(0).publicKeyHash, stream(3).publicKeyHash));
|
|
55
|
+
let suite = new B.Suite();
|
|
56
|
+
let listener = undefined;
|
|
57
|
+
const msgMap = new Map();
|
|
58
|
+
const msgIdFn = (msg) => crypto.createHash("sha1").update(msg.subarray(0, 20)).digest("base64");
|
|
59
|
+
const sizes = [100, 1e3, 1e6];
|
|
60
|
+
for (const size of sizes) {
|
|
61
|
+
suite = suite.add("size: " + size / 1e3 + "kb", {
|
|
62
|
+
defer: true,
|
|
63
|
+
fn: (deferred) => {
|
|
64
|
+
const small = crypto.randomBytes(size); // 1kb
|
|
65
|
+
msgMap.set(msgIdFn(small), deferred);
|
|
66
|
+
stream(0).publish(small, {
|
|
67
|
+
to: [stream(session.peers.length - 1).publicKey],
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
setup: () => {
|
|
71
|
+
listener = (msg) => {
|
|
72
|
+
msgMap.get(msgIdFn(msg.detail.data)).resolve();
|
|
73
|
+
};
|
|
74
|
+
stream(session.peers.length - 1).addEventListener("data", listener);
|
|
75
|
+
msgMap.clear();
|
|
76
|
+
},
|
|
77
|
+
teardown: () => {
|
|
78
|
+
stream(session.peers.length - 1).removeEventListener("data", listener);
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
suite
|
|
83
|
+
.on("cycle", (event) => {
|
|
84
|
+
console.log(String(event.target));
|
|
85
|
+
})
|
|
86
|
+
.on("complete", function (...args) {
|
|
87
|
+
session.stop();
|
|
88
|
+
})
|
|
89
|
+
.run({ async: true });
|
|
90
|
+
//# sourceMappingURL=transfer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transfer.js","sourceRoot":"","sources":["../../benchmark/transfer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,CAAC,MAAM,WAAW,CAAC;AAC1B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EACN,YAAY,EAEZ,YAAY,GACZ,MAAM,iBAAiB,CAAC;AAEzB,+DAA+D;AAE/D,yDAA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/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 { 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 { PeerStore } from "@libp2p/interface";
|
|
13
|
-
import { waitFor } from "@peerbit/time";
|
|
14
|
-
import { PublicSignKey, SignatureWithKey } from "@peerbit/crypto";
|
|
15
|
-
import { Components } from "libp2p/components";
|
|
16
|
-
import type { TypedEventTarget } from "@libp2p/interface";
|
|
17
10
|
import { logger } from "./logger.js";
|
|
18
|
-
|
|
19
|
-
import {
|
|
20
|
-
import type { Libp2pEvents } from "@libp2p/interface";
|
|
21
|
-
import { Message as Message, DataMessage, WaitForPeer, ACK, SeekDelivery, AcknowledgeDelivery, SilentDelivery, Goodbye, StreamEvents, AnyWhere, WithTo, WithMode, PriorityOptions } from "@peerbit/stream-interface";
|
|
22
|
-
import { MultiAddrinfo } from "@peerbit/stream-interface";
|
|
11
|
+
import { type PushableLanes } from "./pushable-lanes.js";
|
|
12
|
+
import { Routes } from "./routes.js";
|
|
23
13
|
import { BandwidthTracker } from "./stats.js";
|
|
24
|
-
|
|
14
|
+
export { logger };
|
|
25
15
|
export { BandwidthTracker };
|
|
26
16
|
export interface PeerStreamsInit {
|
|
27
17
|
peerId: PeerId;
|
|
@@ -109,6 +99,11 @@ type ConnectionManagerOptions = {
|
|
|
109
99
|
dialer?: DialerOptions;
|
|
110
100
|
pruner?: PrunerOptions;
|
|
111
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;
|
|
112
107
|
export type DirectStreamOptions = {
|
|
113
108
|
canRelayMessage?: boolean;
|
|
114
109
|
messageProcessingConcurrency?: number;
|
|
@@ -127,11 +122,6 @@ export interface DirectStreamComponents extends Components {
|
|
|
127
122
|
peerStore: PeerStore;
|
|
128
123
|
events: TypedEventTarget<Libp2pEvents>;
|
|
129
124
|
}
|
|
130
|
-
export type ConnectionManagerArguments = (Partial<Pick<ConnectionManagerOptions, "minConnections">> & Partial<Pick<ConnectionManagerOptions, "maxConnections">> & {
|
|
131
|
-
pruner?: Partial<PrunerOptions> | false;
|
|
132
|
-
} & {
|
|
133
|
-
dialer?: Partial<DialerOptions> | false;
|
|
134
|
-
}) | false;
|
|
135
125
|
export declare abstract class DirectStream<Events extends {
|
|
136
126
|
[s: string]: any;
|
|
137
127
|
} = StreamEvents> extends TypedEventEmitter<Events> implements WaitForPeer {
|
|
@@ -193,15 +183,7 @@ export declare abstract class DirectStream<Events extends {
|
|
|
193
183
|
protected onPeerDisconnected(peerId: PeerId, conn?: Connection): Promise<void>;
|
|
194
184
|
removePeerFromRoutes(hash: string, deleteIfNeighbour?: boolean): void;
|
|
195
185
|
addRouteConnection(from: string, neighbour: string, target: PublicSignKey, distance: number, session: number, remoteSession: number): void;
|
|
196
|
-
/**
|
|
197
|
-
* invoked when a new peer becomes reachable
|
|
198
|
-
* @param publicKeyHash
|
|
199
|
-
*/
|
|
200
186
|
onPeerReachable(publicKey: PublicSignKey): void;
|
|
201
|
-
/**
|
|
202
|
-
* invoked when a new peer becomes unreachable
|
|
203
|
-
* @param publicKeyHash
|
|
204
|
-
*/
|
|
205
187
|
onPeerUnreachable(hash: string): void;
|
|
206
188
|
updateSession(key: PublicSignKey, session?: number): void;
|
|
207
189
|
invalidateSession(key: string): void;
|
|
@@ -213,7 +195,7 @@ export declare abstract class DirectStream<Events extends {
|
|
|
213
195
|
/**
|
|
214
196
|
* Notifies the router that a peer has been disconnected
|
|
215
197
|
*/
|
|
216
|
-
protected _removePeer(publicKey: PublicSignKey): Promise<PeerStreams
|
|
198
|
+
protected _removePeer(publicKey: PublicSignKey): Promise<PeerStreams>;
|
|
217
199
|
/**
|
|
218
200
|
* Responsible for processing each RPC message received by other peers.
|
|
219
201
|
*/
|
|
@@ -228,12 +210,12 @@ export declare abstract class DirectStream<Events extends {
|
|
|
228
210
|
*/
|
|
229
211
|
processMessage(from: PublicSignKey, peerStream: PeerStreams, msg: Uint8ArrayList): Promise<void>;
|
|
230
212
|
shouldIgnore(message: DataMessage, seenBefore: number): boolean;
|
|
231
|
-
onDataMessage(from: PublicSignKey, peerStream: PeerStreams, message: DataMessage, seenBefore: number): Promise<boolean
|
|
213
|
+
onDataMessage(from: PublicSignKey, peerStream: PeerStreams, message: DataMessage, seenBefore: number): Promise<boolean>;
|
|
232
214
|
verifyAndProcess(message: Message<any>): Promise<boolean>;
|
|
233
215
|
maybeAcknowledgeMessage(peerStream: PeerStreams, message: DataMessage | Goodbye, seenBefore: number): Promise<void>;
|
|
234
216
|
private _onDataMessage;
|
|
235
|
-
onAck(publicKey: PublicSignKey, peerStream: PeerStreams, messageBytes: Uint8ArrayList | Uint8Array, message: ACK): Promise<
|
|
236
|
-
onGoodBye(publicKey: PublicSignKey, peerStream: PeerStreams, messageBytes: Uint8ArrayList | Uint8Array, message: Goodbye): Promise<
|
|
217
|
+
onAck(publicKey: PublicSignKey, peerStream: PeerStreams, messageBytes: Uint8ArrayList | Uint8Array, message: ACK): Promise<boolean>;
|
|
218
|
+
onGoodBye(publicKey: PublicSignKey, peerStream: PeerStreams, messageBytes: Uint8ArrayList | Uint8Array, message: Goodbye): Promise<boolean>;
|
|
237
219
|
private maybeDeleteRemoteRoutes;
|
|
238
220
|
private checkIsAlive;
|
|
239
221
|
createMessage(data: Uint8Array | Uint8ArrayList | undefined, options: (WithTo | WithMode) & PriorityOptions & IdentificationOptions): Promise<DataMessage<SilentDelivery | AcknowledgeDelivery | SeekDelivery | AnyWhere>>;
|
|
@@ -260,3 +242,4 @@ export declare const waitForPeers: (...libs: {
|
|
|
260
242
|
waitFor: (peer: PeerId | PublicSignKey) => Promise<void>;
|
|
261
243
|
peerId: PeerId;
|
|
262
244
|
}[]) => Promise<void>;
|
|
245
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EACN,aAAa,EACb,KAAK,gBAAgB,EAMrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,GAAG,EACH,mBAAmB,EACnB,QAAQ,EACR,WAAW,EAEX,OAAO,EACP,K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,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"}
|
|
@@ -1,22 +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 { pipe } from "it-pipe";
|
|
9
10
|
import pDefer from "p-defer";
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
12
|
-
import { multiaddr } from "@multiformats/multiaddr";
|
|
11
|
+
import Queue from "p-queue";
|
|
12
|
+
import { Uint8ArrayList } from "uint8arraylist";
|
|
13
13
|
import { logger } from "./logger.js";
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
import { ready } from "@peerbit/crypto";
|
|
17
|
-
import { Message as Message, DataMessage, getMsgId, ACK, SeekDelivery, AcknowledgeDelivery, SilentDelivery, MessageHeader, Goodbye, TracedDelivery, AnyWhere, NotStartedError, deliveryModeHasReceiver, DeliveryError } from "@peerbit/stream-interface";
|
|
18
|
-
import { MultiAddrinfo } from "@peerbit/stream-interface";
|
|
14
|
+
import { pushableLanes } from "./pushable-lanes.js";
|
|
15
|
+
import { MAX_ROUTE_DISTANCE, Routes } from "./routes.js";
|
|
19
16
|
import { BandwidthTracker } from "./stats.js";
|
|
17
|
+
export { logger };
|
|
20
18
|
export { BandwidthTracker }; // might be useful for others
|
|
21
19
|
const logError = (e) => {
|
|
22
20
|
return logger.error(e?.message);
|
|
@@ -112,41 +110,40 @@ export class PeerStreams extends TypedEventEmitter {
|
|
|
112
110
|
this.usedBandWidthTracker.add(data.byteLength);
|
|
113
111
|
this.outboundStream.push(data instanceof Uint8Array ? data : data.subarray(), this.outboundStream.getReadableLength(0) === 0
|
|
114
112
|
? 0
|
|
115
|
-
: getLaneFromPriority(priority)
|
|
116
|
-
);
|
|
113
|
+
: getLaneFromPriority(priority));
|
|
117
114
|
}
|
|
118
115
|
async waitForWrite(bytes, priority = 0) {
|
|
119
116
|
if (this.closed) {
|
|
120
|
-
logger.error("Failed to send to stream: " + this.peerId + ". Closed");
|
|
117
|
+
logger.error("Failed to send to stream: " + this.peerId.toString() + ". Closed");
|
|
121
118
|
return;
|
|
122
119
|
}
|
|
123
120
|
if (!this.isWritable) {
|
|
124
121
|
// Catch the event where the outbound stream is attach, but also abort if we shut down
|
|
125
|
-
const outboundPromise = new Promise((
|
|
126
|
-
const
|
|
122
|
+
const outboundPromise = new Promise((resolve, reject) => {
|
|
123
|
+
const resolveClear = () => {
|
|
127
124
|
this.removeEventListener("stream:outbound", listener);
|
|
128
125
|
clearTimeout(timer);
|
|
129
|
-
|
|
126
|
+
resolve();
|
|
130
127
|
};
|
|
131
|
-
const
|
|
128
|
+
const rejectClear = (err) => {
|
|
132
129
|
this.removeEventListener("stream:outbound", listener);
|
|
133
130
|
clearTimeout(timer);
|
|
134
|
-
|
|
131
|
+
reject(err);
|
|
135
132
|
};
|
|
136
133
|
const timer = setTimeout(() => {
|
|
137
|
-
|
|
134
|
+
rejectClear(new Error("Timed out"));
|
|
138
135
|
}, 3 * 1000); // TODO if this timeout > 10s we run into issues in the tests when running in CI
|
|
139
136
|
const abortHandler = () => {
|
|
140
137
|
this.removeEventListener("close", abortHandler);
|
|
141
|
-
|
|
138
|
+
rejectClear(new AbortError("Closed"));
|
|
142
139
|
};
|
|
143
140
|
this.addEventListener("close", abortHandler);
|
|
144
141
|
const listener = () => {
|
|
145
|
-
|
|
142
|
+
resolveClear();
|
|
146
143
|
};
|
|
147
144
|
this.addEventListener("stream:outbound", listener);
|
|
148
145
|
if (this.isWritable) {
|
|
149
|
-
|
|
146
|
+
resolveClear();
|
|
150
147
|
}
|
|
151
148
|
});
|
|
152
149
|
await outboundPromise
|
|
@@ -180,7 +177,7 @@ export class PeerStreams extends TypedEventEmitter {
|
|
|
180
177
|
returnOnAbort: true,
|
|
181
178
|
onReturnError: (err) => {
|
|
182
179
|
logger.error("Inbound stream error", err?.message);
|
|
183
|
-
}
|
|
180
|
+
},
|
|
184
181
|
});
|
|
185
182
|
this.dispatchEvent(new CustomEvent("stream:inbound"));
|
|
186
183
|
return this.inboundStream;
|
|
@@ -268,7 +265,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
268
265
|
constructor(components, multicodecs, options) {
|
|
269
266
|
super();
|
|
270
267
|
this.components = components;
|
|
271
|
-
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 || {};
|
|
272
269
|
const signKey = getKeypairFromPeerId(components.peerId);
|
|
273
270
|
this.seekTimeout = seekTimeout;
|
|
274
271
|
this.sign = signKey.sign.bind(signKey);
|
|
@@ -296,7 +293,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
296
293
|
maxConnections: Number.MAX_SAFE_INTEGER,
|
|
297
294
|
minConnections: 0,
|
|
298
295
|
dialer: undefined,
|
|
299
|
-
pruner: undefined
|
|
296
|
+
pruner: undefined,
|
|
300
297
|
};
|
|
301
298
|
}
|
|
302
299
|
else {
|
|
@@ -314,21 +311,21 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
314
311
|
connectionTimeout: DEFAULT_PRUNED_CONNNECTIONS_TIMEOUT,
|
|
315
312
|
interval: DEFAULT_PRUNE_CONNECTIONS_INTERVAL,
|
|
316
313
|
maxBuffer: MAX_QUEUED_BYTES,
|
|
317
|
-
...connectionManager?.pruner
|
|
314
|
+
...connectionManager?.pruner,
|
|
318
315
|
}
|
|
319
|
-
: undefined
|
|
316
|
+
: undefined,
|
|
320
317
|
};
|
|
321
318
|
}
|
|
322
319
|
this.recentDials = this.connectionManagerOptions.dialer
|
|
323
320
|
? new Cache({
|
|
324
321
|
ttl: this.connectionManagerOptions.dialer.retryDelay,
|
|
325
|
-
max: 1e3
|
|
322
|
+
max: 1e3,
|
|
326
323
|
})
|
|
327
324
|
: undefined;
|
|
328
325
|
this.prunedConnectionsCache = this.connectionManagerOptions.pruner
|
|
329
326
|
? new Cache({
|
|
330
327
|
max: 1e6,
|
|
331
|
-
ttl: this.connectionManagerOptions.pruner.connectionTimeout
|
|
328
|
+
ttl: this.connectionManagerOptions.pruner.connectionTimeout,
|
|
332
329
|
})
|
|
333
330
|
: undefined;
|
|
334
331
|
}
|
|
@@ -341,7 +338,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
341
338
|
this.closeController = new AbortController();
|
|
342
339
|
this.routes = new Routes(this.publicKeyHash, {
|
|
343
340
|
routeMaxRetentionPeriod: this.routeMaxRetentionPeriod,
|
|
344
|
-
signal: this.closeController.signal
|
|
341
|
+
signal: this.closeController.signal,
|
|
345
342
|
});
|
|
346
343
|
this.started = true;
|
|
347
344
|
this.stopping = false;
|
|
@@ -351,14 +348,14 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
351
348
|
await Promise.all(this.multicodecs.map((multicodec) => this.components.registrar.handle(multicodec, this._onIncomingStream, {
|
|
352
349
|
maxInboundStreams: this.maxInboundStreams,
|
|
353
350
|
maxOutboundStreams: this.maxOutboundStreams,
|
|
354
|
-
runOnTransientConnection: false
|
|
351
|
+
runOnTransientConnection: false,
|
|
355
352
|
})));
|
|
356
353
|
// register protocol with topology
|
|
357
354
|
// Topology callbacks called on connection manager changes
|
|
358
355
|
this._registrarTopologyIds = await Promise.all(this.multicodecs.map((multicodec) => this.components.registrar.register(multicodec, {
|
|
359
356
|
onConnect: this.onPeerConnected.bind(this),
|
|
360
357
|
onDisconnect: this.onPeerDisconnected.bind(this),
|
|
361
|
-
notifyOnTransient: false
|
|
358
|
+
notifyOnTransient: false,
|
|
362
359
|
})));
|
|
363
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)
|
|
364
361
|
const peerToConnections = new Map();
|
|
@@ -419,7 +416,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
419
416
|
for (const peerStreams of this.peers.values()) {
|
|
420
417
|
await peerStreams.close();
|
|
421
418
|
}
|
|
422
|
-
for (const [
|
|
419
|
+
for (const [_k, v] of this.healthChecks) {
|
|
423
420
|
clearTimeout(v);
|
|
424
421
|
}
|
|
425
422
|
this.healthChecks.clear();
|
|
@@ -429,7 +426,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
429
426
|
this.seenCache.clear();
|
|
430
427
|
this.routes.clear();
|
|
431
428
|
this.peerKeyHashToPublicKey.clear();
|
|
432
|
-
for (const [
|
|
429
|
+
for (const [_k, v] of this._ackCallbacks) {
|
|
433
430
|
v.clear();
|
|
434
431
|
}
|
|
435
432
|
this._ackCallbacks.clear();
|
|
@@ -563,8 +560,8 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
563
560
|
leaving: [peerKeyHash],
|
|
564
561
|
header: new MessageHeader({
|
|
565
562
|
session: this.session,
|
|
566
|
-
mode: new SilentDelivery({ to: dependent, redundancy: 2 })
|
|
567
|
-
})
|
|
563
|
+
mode: new SilentDelivery({ to: dependent, redundancy: 2 }),
|
|
564
|
+
}),
|
|
568
565
|
}).sign(this.sign));
|
|
569
566
|
}
|
|
570
567
|
this.checkIsAlive([peerKeyHash]);
|
|
@@ -593,24 +590,16 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
593
590
|
}
|
|
594
591
|
}
|
|
595
592
|
}
|
|
596
|
-
/**
|
|
597
|
-
* invoked when a new peer becomes reachable
|
|
598
|
-
* @param publicKeyHash
|
|
599
|
-
*/
|
|
600
593
|
onPeerReachable(publicKey) {
|
|
601
594
|
// override this fn
|
|
602
595
|
this.dispatchEvent(new CustomEvent("peer:reachable", { detail: publicKey }));
|
|
603
596
|
}
|
|
604
|
-
/**
|
|
605
|
-
* invoked when a new peer becomes unreachable
|
|
606
|
-
* @param publicKeyHash
|
|
607
|
-
*/
|
|
608
597
|
onPeerUnreachable(hash) {
|
|
609
598
|
// override this fn
|
|
610
599
|
this.dispatchEvent(
|
|
611
600
|
// TODO types
|
|
612
601
|
new CustomEvent("peer:unreachable", {
|
|
613
|
-
detail: this.peerKeyHashToPublicKey.get(hash)
|
|
602
|
+
detail: this.peerKeyHashToPublicKey.get(hash),
|
|
614
603
|
}));
|
|
615
604
|
}
|
|
616
605
|
updateSession(key, session) {
|
|
@@ -625,7 +614,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
625
614
|
this.dispatchEvent(
|
|
626
615
|
// TODO types
|
|
627
616
|
new CustomEvent("peer:session", {
|
|
628
|
-
detail: key
|
|
617
|
+
detail: key,
|
|
629
618
|
}));
|
|
630
619
|
}
|
|
631
620
|
/**
|
|
@@ -647,12 +636,12 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
647
636
|
peerId,
|
|
648
637
|
publicKey,
|
|
649
638
|
protocol,
|
|
650
|
-
connId
|
|
639
|
+
connId,
|
|
651
640
|
});
|
|
652
641
|
this.peers.set(publicKeyHash, peerStreams);
|
|
653
642
|
this.updateSession(publicKey, -1);
|
|
654
643
|
peerStreams.addEventListener("close", () => this._removePeer(publicKey), {
|
|
655
|
-
once: true
|
|
644
|
+
once: true,
|
|
656
645
|
});
|
|
657
646
|
return peerStreams;
|
|
658
647
|
}
|
|
@@ -729,7 +718,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
729
718
|
// Ensure the message is valid before processing it
|
|
730
719
|
const message = Message.from(msg);
|
|
731
720
|
this.dispatchEvent(new CustomEvent("message", {
|
|
732
|
-
detail: message
|
|
721
|
+
detail: message,
|
|
733
722
|
}));
|
|
734
723
|
if (message instanceof DataMessage) {
|
|
735
724
|
// DONT await this since it might introduce a dead-lock
|
|
@@ -785,7 +774,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
785
774
|
await this.maybeAcknowledgeMessage(peerStream, message, seenBefore);
|
|
786
775
|
if (seenBefore === 0 && message.data) {
|
|
787
776
|
this.dispatchEvent(new CustomEvent("data", {
|
|
788
|
-
detail: message
|
|
777
|
+
detail: message,
|
|
789
778
|
}));
|
|
790
779
|
}
|
|
791
780
|
}
|
|
@@ -803,7 +792,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
803
792
|
// DONT await this since it might introduce a dead-lock
|
|
804
793
|
if (message.header.mode instanceof SeekDelivery) {
|
|
805
794
|
if (seenBefore < message.header.mode.redundancy) {
|
|
806
|
-
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);
|
|
807
796
|
if (to.length > 0) {
|
|
808
797
|
this.relayMessage(from, message, to);
|
|
809
798
|
}
|
|
@@ -851,8 +840,8 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
851
840
|
? new MultiAddrinfo(this.components.addressManager
|
|
852
841
|
.getAddresses()
|
|
853
842
|
.map((x) => x.toString()))
|
|
854
|
-
: undefined
|
|
855
|
-
})
|
|
843
|
+
: undefined,
|
|
844
|
+
}),
|
|
856
845
|
}).sign(this.sign), [peerStream]);
|
|
857
846
|
}
|
|
858
847
|
}
|
|
@@ -941,8 +930,8 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
941
930
|
return this.publish(undefined, {
|
|
942
931
|
mode: new SeekDelivery({
|
|
943
932
|
to: remotes,
|
|
944
|
-
redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY
|
|
945
|
-
})
|
|
933
|
+
redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY,
|
|
934
|
+
}),
|
|
946
935
|
})
|
|
947
936
|
.then(() => true)
|
|
948
937
|
.catch((e) => {
|
|
@@ -971,7 +960,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
971
960
|
? options.mode
|
|
972
961
|
: new SilentDelivery({
|
|
973
962
|
to: options.to,
|
|
974
|
-
redundancy: DEFAULT_SILENT_MESSAGE_REDUDANCY
|
|
963
|
+
redundancy: DEFAULT_SILENT_MESSAGE_REDUDANCY,
|
|
975
964
|
});
|
|
976
965
|
if (mode instanceof AcknowledgeDelivery ||
|
|
977
966
|
mode instanceof SilentDelivery ||
|
|
@@ -992,7 +981,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
992
981
|
!this.routes.isUpToDate(hash, neighbourRoutes)) {
|
|
993
982
|
mode = new SeekDelivery({
|
|
994
983
|
to: mode.to,
|
|
995
|
-
redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY
|
|
984
|
+
redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY,
|
|
996
985
|
});
|
|
997
986
|
break;
|
|
998
987
|
}
|
|
@@ -1004,8 +993,8 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1004
993
|
id: options.id,
|
|
1005
994
|
mode,
|
|
1006
995
|
session: this.session,
|
|
1007
|
-
priority: options.priority
|
|
1008
|
-
})
|
|
996
|
+
priority: options.priority,
|
|
997
|
+
}),
|
|
1009
998
|
});
|
|
1010
999
|
// TODO allow messages to also be sent unsigned (signaturePolicy property)
|
|
1011
1000
|
await message.sign(this.sign);
|
|
@@ -1015,7 +1004,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1015
1004
|
* Publishes messages to all peers
|
|
1016
1005
|
*/
|
|
1017
1006
|
async publish(data, options = {
|
|
1018
|
-
mode: new SeekDelivery({ redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY })
|
|
1007
|
+
mode: new SeekDelivery({ redundancy: DEFAULT_SEEK_MESSAGE_REDUDANCY }),
|
|
1019
1008
|
}) {
|
|
1020
1009
|
if (!this.started) {
|
|
1021
1010
|
throw new NotStartedError();
|
|
@@ -1079,7 +1068,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1079
1068
|
const haveReceivers = messageToSet.size > 0;
|
|
1080
1069
|
if (haveReceivers && this.peers.size === 0) {
|
|
1081
1070
|
return {
|
|
1082
|
-
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")),
|
|
1083
1072
|
};
|
|
1084
1073
|
}
|
|
1085
1074
|
const deliveryDeferredPromise = pDefer();
|
|
@@ -1127,7 +1116,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1127
1116
|
}
|
|
1128
1117
|
if (!hasAll && willGetAllAcknowledgements) {
|
|
1129
1118
|
deliveryDeferredPromise.reject(new DeliveryError(`Failed to get message ${idString} ${filterMessageForSeenCounter} ${[
|
|
1130
|
-
...messageToSet
|
|
1119
|
+
...messageToSet,
|
|
1131
1120
|
]} delivery acknowledges from all nodes (${fastestNodesReached.size}/${messageToSet.size}). Mode: ${message.header.mode.constructor.name}. Redundancy: ${message.header.mode["redundancy"]}`));
|
|
1132
1121
|
}
|
|
1133
1122
|
else {
|
|
@@ -1183,7 +1172,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1183
1172
|
clear: () => {
|
|
1184
1173
|
clear();
|
|
1185
1174
|
deliveryDeferredPromise.resolve();
|
|
1186
|
-
}
|
|
1175
|
+
},
|
|
1187
1176
|
});
|
|
1188
1177
|
return deliveryDeferredPromise;
|
|
1189
1178
|
}
|
|
@@ -1289,7 +1278,9 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1289
1278
|
.map((x) => multiaddr(x));
|
|
1290
1279
|
if (addresses.length > 0) {
|
|
1291
1280
|
try {
|
|
1292
|
-
await this.components.connectionManager.openConnection(
|
|
1281
|
+
await this.components.connectionManager.openConnection([
|
|
1282
|
+
addresses[addresses.length - 1],
|
|
1283
|
+
]);
|
|
1293
1284
|
}
|
|
1294
1285
|
catch (error) {
|
|
1295
1286
|
logger.info("Failed to connect directly to: " +
|
|
@@ -1312,7 +1303,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1312
1303
|
return true;
|
|
1313
1304
|
}, {
|
|
1314
1305
|
signal: options?.signal,
|
|
1315
|
-
timeout: options?.timeout ?? 10 * 1000
|
|
1306
|
+
timeout: options?.timeout ?? 10 * 1000,
|
|
1316
1307
|
});
|
|
1317
1308
|
}
|
|
1318
1309
|
catch (error) {
|
|
@@ -1329,7 +1320,7 @@ export class DirectStream extends TypedEventEmitter {
|
|
|
1329
1320
|
// Dontwait for readlable https://github.com/libp2p/js-libp2p/issues/2321
|
|
1330
1321
|
await waitFor(() => /* stream.isReadable && */ stream.isWritable, {
|
|
1331
1322
|
signal: options?.signal,
|
|
1332
|
-
timeout: options?.timeout ?? 10 * 1000
|
|
1323
|
+
timeout: options?.timeout ?? 10 * 1000,
|
|
1333
1324
|
});
|
|
1334
1325
|
}
|
|
1335
1326
|
catch (error) {
|