@peerbit/stream 1.0.20 → 2.0.2

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.
@@ -1,4 +1,4 @@
1
- import { EventEmitter } from "@libp2p/interface/events";
1
+ import { TypedEventEmitter } from "@libp2p/interface";
2
2
  import Queue from "p-queue";
3
3
  import type { PeerId } from "@libp2p/interface/peer-id";
4
4
  import type { Connection } from "@libp2p/interface/connection";
@@ -6,19 +6,21 @@ import type { Pushable } from "it-pushable";
6
6
  import type { Stream } from "@libp2p/interface/connection";
7
7
  import { Uint8ArrayList } from "uint8arraylist";
8
8
  import { Routes } from "./routes.js";
9
- import { PeerMap } from "./peer-map.js";
10
9
  import type { IncomingStreamData, Registrar } from "@libp2p/interface-internal/registrar";
11
10
  import type { AddressManager } from "@libp2p/interface-internal/address-manager";
12
11
  import type { ConnectionManager } from "@libp2p/interface-internal/connection-manager";
13
12
  import { PeerStore } from "@libp2p/interface/peer-store";
14
13
  import { waitFor } from "@peerbit/time";
15
14
  import { PublicSignKey, SignatureWithKey } from "@peerbit/crypto";
15
+ import { Components } from "libp2p/components";
16
+ import type { TypedEventTarget } from "@libp2p/interface";
16
17
  export type SignaturePolicy = "StictSign" | "StrictNoSign";
17
18
  import { logger } from "./logger.js";
18
- import { Cache } from "@peerbit/cache";
19
19
  export { logger };
20
+ import { Cache } from "@peerbit/cache";
20
21
  import type { Libp2pEvents } from "@libp2p/interface";
21
- import { PeerEvents, Message as Message, Goodbye, Hello, DataMessage, PingPong, WaitForPeer } from "@peerbit/stream-interface";
22
+ import { Message as Message, DataMessage, WaitForPeer, ACK, SeekDelivery, AcknowledgeDelivery, SilentDelivery, Goodbye, StreamEvents, AnyWhere } from "@peerbit/stream-interface";
23
+ import { MultiAddrinfo } from "@peerbit/stream-interface";
22
24
  export interface PeerStreamsInit {
23
25
  peerId: PeerId;
24
26
  publicKey: PublicSignKey;
@@ -30,10 +32,16 @@ export interface PeerStreamEvents {
30
32
  "stream:outbound": CustomEvent<never>;
31
33
  close: CustomEvent<never>;
32
34
  }
35
+ type WithTo = {
36
+ to?: (string | PublicSignKey | PeerId)[] | Set<string>;
37
+ };
38
+ type WithMode = {
39
+ mode?: SilentDelivery | SeekDelivery | AcknowledgeDelivery | AnyWhere;
40
+ };
33
41
  /**
34
42
  * Thin wrapper around a peer's inbound / outbound pubsub streams
35
43
  */
36
- export declare class PeerStreams extends EventEmitter<PeerStreamEvents> {
44
+ export declare class PeerStreams extends TypedEventEmitter<PeerStreamEvents> {
37
45
  counter: number;
38
46
  readonly peerId: PeerId;
39
47
  readonly publicKey: PublicSignKey;
@@ -41,7 +49,7 @@ export declare class PeerStreams extends EventEmitter<PeerStreamEvents> {
41
49
  /**
42
50
  * Write stream - it's preferable to use the write method
43
51
  */
44
- outboundStream?: Pushable<Uint8ArrayList>;
52
+ outboundStream?: Pushable<Uint8Array>;
45
53
  /**
46
54
  * Read stream
47
55
  */
@@ -59,12 +67,9 @@ export declare class PeerStreams extends EventEmitter<PeerStreamEvents> {
59
67
  */
60
68
  private readonly inboundAbortController;
61
69
  private closed;
62
- pingJob: {
63
- resolve: () => void;
64
- abort: () => void;
65
- };
66
- pingLatency: number | undefined;
67
70
  connId: string;
71
+ seekedOnce: boolean;
72
+ private usedBandWidthTracker;
68
73
  constructor(init: PeerStreamsInit);
69
74
  /**
70
75
  * Do we have a connection to read from?
@@ -74,6 +79,7 @@ export declare class PeerStreams extends EventEmitter<PeerStreamEvents> {
74
79
  * Do we have a connection to write on?
75
80
  */
76
81
  get isWritable(): boolean;
82
+ get usedBandwidth(): number;
77
83
  /**
78
84
  * Send a message to this peer.
79
85
  * Throws if there is no `stream` to write to available.
@@ -87,47 +93,55 @@ export declare class PeerStreams extends EventEmitter<PeerStreamEvents> {
87
93
  /**
88
94
  * Attach a raw outbound stream and setup a write stream
89
95
  */
90
- attachOutboundStream(stream: Stream): Promise<Pushable<Uint8ArrayList, void, unknown>>;
96
+ attachOutboundStream(stream: Stream): Promise<Pushable<Uint8Array, void, unknown>>;
91
97
  /**
92
98
  * Closes the open connection to peer
93
99
  */
94
100
  close(): Promise<void>;
95
101
  }
96
- export interface MessageEvents {
97
- message: CustomEvent<Message>;
98
- }
99
- export interface StreamEvents extends PeerEvents, MessageEvents {
100
- data: CustomEvent<DataMessage>;
101
- }
102
- export type ConnectionManagerOptions = {
103
- autoDial?: boolean;
104
- retryDelay?: number;
102
+ type DialerOptions = {
103
+ retryDelay: number;
104
+ };
105
+ type PrunerOptions = {
106
+ interval: number;
107
+ bandwidth?: number;
108
+ maxBuffer?: number;
109
+ connectionTimeout: number;
110
+ };
111
+ type ConnectionManagerOptions = {
112
+ minConnections: number;
113
+ maxConnections: number;
114
+ dialer?: DialerOptions;
115
+ pruner?: PrunerOptions;
105
116
  };
106
117
  export type DirectStreamOptions = {
107
118
  canRelayMessage?: boolean;
108
- emitSelf?: boolean;
109
119
  messageProcessingConcurrency?: number;
110
120
  maxInboundStreams?: number;
111
121
  maxOutboundStreams?: number;
112
122
  signaturePolicy?: SignaturePolicy;
113
- pingInterval?: number | null;
114
- connectionManager?: ConnectionManagerOptions;
123
+ connectionManager?: ConnectionManagerArguments;
124
+ routeSeekInterval?: number;
125
+ seekTimeout?: number;
115
126
  };
116
- import { Components } from "libp2p/components";
117
127
  export interface DirectStreamComponents extends Components {
118
128
  peerId: PeerId;
119
129
  addressManager: AddressManager;
120
130
  registrar: Registrar;
121
131
  connectionManager: ConnectionManager;
122
132
  peerStore: PeerStore;
123
- events: EventEmitter<Libp2pEvents>;
133
+ events: TypedEventTarget<Libp2pEvents>;
124
134
  }
135
+ export type ConnectionManagerArguments = (Partial<Pick<ConnectionManagerOptions, "minConnections">> & Partial<Pick<ConnectionManagerOptions, "maxConnections">> & {
136
+ pruner?: Partial<PrunerOptions> | false;
137
+ } & {
138
+ dialer?: Partial<DialerOptions> | false;
139
+ }) | false;
125
140
  export declare abstract class DirectStream<Events extends {
126
141
  [s: string]: any;
127
- } = StreamEvents> extends EventEmitter<Events> implements WaitForPeer {
142
+ } = StreamEvents> extends TypedEventEmitter<Events> implements WaitForPeer {
128
143
  readonly components: DirectStreamComponents;
129
144
  peerId: PeerId;
130
- peerIdStr: string;
131
145
  publicKey: PublicSignKey;
132
146
  publicKeyHash: string;
133
147
  sign: (bytes: Uint8Array) => Promise<SignatureWithKey>;
@@ -135,9 +149,8 @@ export declare abstract class DirectStream<Events extends {
135
149
  /**
136
150
  * Map of peer streams
137
151
  */
138
- peers: PeerMap<PeerStreams>;
152
+ peers: Map<string, PeerStreams>;
139
153
  peerKeyHashToPublicKey: Map<string, PublicSignKey>;
140
- peerIdToPublicKey: Map<string, PublicSignKey>;
141
154
  routes: Routes;
142
155
  /**
143
156
  * If router can relay received messages, even if not subscribed
@@ -147,22 +160,21 @@ export declare abstract class DirectStream<Events extends {
147
160
  * if publish should emit to self, if subscribed
148
161
  */
149
162
  signaturePolicy: SignaturePolicy;
150
- emitSelf: boolean;
151
163
  queue: Queue;
152
164
  multicodecs: string[];
153
- seenCache: Cache;
154
- earlyGoodbyes: Map<string, Goodbye>;
155
- helloMap: Map<string, Map<string, Hello>>;
156
- multiaddrsMap: Map<string, string[]>;
165
+ seenCache: Cache<number>;
157
166
  private _registrarTopologyIds;
158
167
  private readonly maxInboundStreams?;
159
168
  private readonly maxOutboundStreams?;
160
- private topology;
161
- private pingJobPromise;
162
- private pingJob;
163
- private pingInterval;
164
- private connectionManagerOptions;
165
- private recentDials;
169
+ connectionManagerOptions: ConnectionManagerOptions;
170
+ private recentDials?;
171
+ private healthChecks;
172
+ private pruneConnectionsTimeout;
173
+ private prunedConnectionsCache?;
174
+ routeSeekInterval: number;
175
+ seekTimeout: number;
176
+ closeController: AbortController;
177
+ private _ackCallbacks;
166
178
  constructor(components: DirectStreamComponents, multicodecs: string[], options?: DirectStreamOptions);
167
179
  start(): Promise<void>;
168
180
  /**
@@ -177,15 +189,13 @@ export declare abstract class DirectStream<Events extends {
177
189
  /**
178
190
  * Registrar notifies an established connection with protocol
179
191
  */
180
- onPeerConnected(peerId: PeerId, conn: Connection, properties?: {
181
- fromExisting?: boolean;
182
- }): Promise<any[] | undefined>;
183
- private addRouteConnection;
184
- removeRouteConnection(from: PublicSignKey, to: PublicSignKey): void;
192
+ onPeerConnected(peerId: PeerId, connection: Connection): Promise<void>;
185
193
  /**
186
194
  * Registrar notifies a closing connection with pubsub protocol
187
195
  */
188
196
  protected onPeerDisconnected(peerId: PeerId, conn?: Connection): Promise<void>;
197
+ removeRouteConnection(hash: string, neigbour: boolean): void;
198
+ addRouteConnection(from: string, neighbour: string, target: PublicSignKey, distance: number, session: number, pending?: boolean): void;
189
199
  /**
190
200
  * invoked when a new peer becomes reachable
191
201
  * @param publicKeyHash
@@ -195,7 +205,7 @@ export declare abstract class DirectStream<Events extends {
195
205
  * invoked when a new peer becomes unreachable
196
206
  * @param publicKeyHash
197
207
  */
198
- onPeerUnreachable(publicKey: PublicSignKey): void;
208
+ onPeerUnreachable(hash: string): void;
199
209
  /**
200
210
  * Notifies the router that a peer has been connected
201
211
  */
@@ -207,39 +217,40 @@ export declare abstract class DirectStream<Events extends {
207
217
  /**
208
218
  * Responsible for processing each RPC message received by other peers.
209
219
  */
210
- processMessages(peerId: PeerId, stream: AsyncIterable<Uint8ArrayList>, peerStreams: PeerStreams): Promise<void>;
220
+ processMessages(peerId: PublicSignKey, stream: AsyncIterable<Uint8ArrayList>, peerStreams: PeerStreams): Promise<void>;
211
221
  /**
212
222
  * Handles an rpc request from a peer
213
223
  */
214
- processRpc(from: PeerId, peerStreams: PeerStreams, message: Uint8ArrayList): Promise<boolean>;
224
+ processRpc(from: PublicSignKey, peerStreams: PeerStreams, message: Uint8ArrayList): Promise<boolean>;
225
+ private modifySeenCache;
215
226
  /**
216
227
  * Handles a message from a peer
217
228
  */
218
- processMessage(from: PeerId, peerStream: PeerStreams, msg: Uint8ArrayList): Promise<void>;
219
- onDataMessage(from: PeerId, peerStream: PeerStreams, message: DataMessage): Promise<boolean>;
220
- onHello(from: PeerId, peerStream: PeerStreams, message: Hello): Promise<boolean>;
221
- onGoodbye(from: PeerId, peerStream: PeerStreams, message: Goodbye): Promise<boolean>;
222
- onPing(from: PeerId, peerStream: PeerStreams, message: PingPong): Promise<void>;
223
- ping(stream: PeerStreams): Promise<number | undefined>;
224
- /**
225
- * Whether to accept a message from a peer
226
- * Override to create a graylist
227
- */
228
- acceptFrom(id: PeerId): boolean;
229
- createMessage(data: Uint8Array | Uint8ArrayList, options?: {
230
- to?: (string | PublicSignKey | PeerId)[] | Set<string>;
231
- }): Promise<DataMessage>;
229
+ processMessage(from: PublicSignKey, peerStream: PeerStreams, msg: Uint8ArrayList): Promise<void>;
230
+ shouldIgnore(message: DataMessage, seenBefore: number): boolean;
231
+ onDataMessage(from: PublicSignKey, peerStream: PeerStreams, message: DataMessage, seenBefore: number): Promise<boolean | undefined>;
232
+ maybeVerifyMessage(message: DataMessage): Promise<boolean>;
233
+ acknowledgeMessage(peerStream: PeerStreams, message: DataMessage, seenBefore: number): Promise<void>;
234
+ private _onDataMessage;
235
+ onAck(publicKey: PublicSignKey, peerStream: PeerStreams, messageBytes: Uint8ArrayList | Uint8Array, message: ACK): Promise<false | undefined>;
236
+ onGoodBye(publicKey: PublicSignKey, peerStream: PeerStreams, messageBytes: Uint8ArrayList | Uint8Array, message: Goodbye): Promise<false | undefined>;
237
+ createMessage(data: Uint8Array | Uint8ArrayList | undefined, options: WithTo | WithMode): Promise<DataMessage<SilentDelivery | SeekDelivery | AcknowledgeDelivery | AnyWhere>>;
232
238
  /**
233
239
  * Publishes messages to all peers
234
240
  */
235
- publish(data: Uint8Array | Uint8ArrayList, options?: {
236
- to?: (string | PublicSignKey | PeerId)[] | Set<string>;
237
- }): Promise<Uint8Array>;
238
- hello(data?: Uint8Array): Promise<void>;
239
- relayMessage(from: PeerId, message: Message, to?: PeerStreams[] | PeerMap<PeerStreams>): Promise<void>;
240
- publishMessage(from: PeerId, message: Message, to?: PeerStreams[] | PeerMap<PeerStreams>, relayed?: boolean): Promise<void>;
241
- maybeConnectDirectly(path: string[]): Promise<void>;
242
- waitFor(peer: PeerId | PublicSignKey): Promise<void>;
241
+ publish(data: Uint8Array | Uint8ArrayList | undefined, options?: WithMode | WithTo): Promise<Uint8Array>;
242
+ relayMessage(from: PublicSignKey, message: Message, to?: PeerStreams[] | Map<string, PeerStreams>): Promise<void>;
243
+ private createDeliveryPromise;
244
+ publishMessage(from: PublicSignKey, message: Message, to?: PeerStreams[] | Map<string, PeerStreams>, relayed?: boolean): Promise<void>;
245
+ maybeConnectDirectly(toHash: string, origin: MultiAddrinfo): Promise<void>;
246
+ waitFor(peer: PeerId | PublicSignKey, options?: {
247
+ signal: AbortSignal;
248
+ }): Promise<void>;
249
+ get pending(): boolean;
250
+ lastQueuedBytes: number;
251
+ maybePruneConnections(): Promise<void>;
252
+ pruneConnections(): Promise<void>;
253
+ getQueuedBytes(): number;
243
254
  }
244
255
  export declare const waitForPeers: (...libs: {
245
256
  waitFor: (peer: PeerId | PublicSignKey) => Promise<void>;