@libp2p/floodsub 10.1.46 → 11.0.0-049bfa0fa

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.
@@ -0,0 +1,6 @@
1
+ /**
2
+ * All Pubsub implementations must use this symbol as the name of a property
3
+ * with a boolean `true` value
4
+ */
5
+ export declare const pubSubSymbol: unique symbol;
6
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,YAAY,eAA+B,CAAA"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * All Pubsub implementations must use this symbol as the name of a property
3
+ * with a boolean `true` value
4
+ */
5
+ export const pubSubSymbol = Symbol.for('@libp2p/pubsub');
6
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAA"}
@@ -0,0 +1,205 @@
1
+ import { serviceCapabilities, serviceDependencies } from '@libp2p/interface';
2
+ import { PeerMap, PeerSet } from '@libp2p/peer-collections';
3
+ import { TypedEventEmitter } from 'main-event';
4
+ import Queue from 'p-queue';
5
+ import { SimpleTimeCache } from './cache.js';
6
+ import { pubSubSymbol } from './constants.ts';
7
+ import { PeerStreams } from './peer-streams.js';
8
+ import { StrictNoSign, StrictSign } from './index.js';
9
+ import type { FloodSubComponents, FloodSubEvents, FloodSubInit, FloodSub as FloodSubInterface, Message, PublishResult, TopicValidatorFn } from './index.js';
10
+ import type { Logger, Connection, PeerId, Stream } from '@libp2p/interface';
11
+ import type { Uint8ArrayList } from 'uint8arraylist';
12
+ export interface PubSubRPCMessage {
13
+ from?: Uint8Array;
14
+ topic?: string;
15
+ data?: Uint8Array;
16
+ sequenceNumber?: Uint8Array;
17
+ signature?: Uint8Array;
18
+ key?: Uint8Array;
19
+ }
20
+ export interface PubSubRPCSubscription {
21
+ subscribe?: boolean;
22
+ topic?: string;
23
+ }
24
+ export interface PubSubRPC {
25
+ subscriptions: PubSubRPCSubscription[];
26
+ messages: PubSubRPCMessage[];
27
+ }
28
+ /**
29
+ * PubSubBaseProtocol handles the peers and connections logic for pubsub routers
30
+ * and specifies the API that pubsub routers should have.
31
+ */
32
+ export declare class FloodSub extends TypedEventEmitter<FloodSubEvents> implements FloodSubInterface {
33
+ protected log: Logger;
34
+ started: boolean;
35
+ /**
36
+ * Map of topics to which peers are subscribed to
37
+ */
38
+ topics: Map<string, PeerSet>;
39
+ /**
40
+ * List of our subscriptions
41
+ */
42
+ subscriptions: Set<string>;
43
+ /**
44
+ * Map of peer streams
45
+ */
46
+ peers: PeerMap<PeerStreams>;
47
+ /**
48
+ * The signature policy to follow by default
49
+ */
50
+ globalSignaturePolicy: typeof StrictNoSign | typeof StrictSign;
51
+ /**
52
+ * If router can relay received messages, even if not subscribed
53
+ */
54
+ canRelayMessage: boolean;
55
+ /**
56
+ * if publish should emit to self, if subscribed
57
+ */
58
+ emitSelf: boolean;
59
+ /**
60
+ * Topic validator map
61
+ *
62
+ * Keyed by topic
63
+ * Topic validators are functions with the following input:
64
+ */
65
+ topicValidators: Map<string, TopicValidatorFn>;
66
+ queue: Queue;
67
+ protocols: string[];
68
+ components: FloodSubComponents;
69
+ private _registrarTopologyIds;
70
+ private readonly maxInboundStreams;
71
+ private readonly maxOutboundStreams;
72
+ seenCache: SimpleTimeCache<boolean>;
73
+ constructor(components: FloodSubComponents, init: FloodSubInit);
74
+ readonly [pubSubSymbol] = true;
75
+ readonly [Symbol.toStringTag] = "@libp2p/floodsub";
76
+ readonly [serviceCapabilities]: string[];
77
+ readonly [serviceDependencies]: string[];
78
+ /**
79
+ * Register the pubsub protocol onto the libp2p node.
80
+ */
81
+ start(): Promise<void>;
82
+ /**
83
+ * Unregister the pubsub protocol and the streams with other peers will be closed.
84
+ */
85
+ stop(): Promise<void>;
86
+ isStarted(): boolean;
87
+ /**
88
+ * On an inbound stream opened
89
+ */
90
+ protected _onIncomingStream(stream: Stream, connection: Connection): void;
91
+ /**
92
+ * Registrar notifies an established connection with pubsub protocol
93
+ */
94
+ protected _onPeerConnected(peerId: PeerId, conn: Connection): Promise<void>;
95
+ /**
96
+ * Registrar notifies a closing connection with pubsub protocol
97
+ */
98
+ protected _onPeerDisconnected(peerId: PeerId, conn?: Connection): void;
99
+ /**
100
+ * Notifies the router that a peer has been connected
101
+ */
102
+ addPeer(peerId: PeerId, protocol: string): PeerStreams;
103
+ /**
104
+ * Notifies the router that a peer has been disconnected
105
+ */
106
+ protected _removePeer(peerId: PeerId): PeerStreams | undefined;
107
+ /**
108
+ * Responsible for processing each RPC message received by other peers.
109
+ */
110
+ processMessages(peerId: PeerId, stream: AsyncIterable<Uint8ArrayList>, peerStreams: PeerStreams): Promise<void>;
111
+ /**
112
+ * Handles an rpc request from a peer
113
+ */
114
+ processRpc(from: PeerId, peerStreams: PeerStreams, rpc: PubSubRPC): Promise<boolean>;
115
+ /**
116
+ * Handles a subscription change from a peer
117
+ */
118
+ processRpcSubOpt(id: PeerId, subOpt: PubSubRPCSubscription): void;
119
+ /**
120
+ * Handles a message from a peer
121
+ */
122
+ processMessage(from: PeerId, msg: Message): Promise<void>;
123
+ /**
124
+ * The default msgID implementation
125
+ * Child class can override this.
126
+ */
127
+ getMsgId(msg: Message): Promise<Uint8Array> | Uint8Array;
128
+ /**
129
+ * Whether to accept a message from a peer
130
+ * Override to create a gray list
131
+ */
132
+ acceptFrom(id: PeerId): boolean;
133
+ /**
134
+ * Decode Uint8Array into an RPC object.
135
+ * This can be override to use a custom router protobuf.
136
+ */
137
+ decodeRpc(bytes: Uint8Array | Uint8ArrayList): PubSubRPC;
138
+ /**
139
+ * Encode RPC object into a Uint8Array.
140
+ * This can be override to use a custom router protobuf.
141
+ */
142
+ encodeRpc(rpc: PubSubRPC): Uint8Array;
143
+ /**
144
+ * Encode RPC object into a Uint8Array.
145
+ * This can be override to use a custom router protobuf.
146
+ */
147
+ encodeMessage(rpc: PubSubRPCMessage): Uint8Array;
148
+ /**
149
+ * Send an rpc object to a peer
150
+ */
151
+ send(peer: PeerId, data: {
152
+ messages?: Message[];
153
+ subscriptions?: string[];
154
+ subscribe?: boolean;
155
+ }): void;
156
+ /**
157
+ * Send an rpc object to a peer
158
+ */
159
+ sendRpc(peer: PeerId, rpc: PubSubRPC): void;
160
+ /**
161
+ * Validates the given message. The signature will be checked for authenticity.
162
+ * Throws an error on invalid messages
163
+ */
164
+ validate(from: PeerId, message: Message): Promise<void>;
165
+ /**
166
+ * Normalizes the message and signs it, if signing is enabled.
167
+ * Should be used by the routers to create the message to send.
168
+ */
169
+ buildMessage(message: {
170
+ from: PeerId;
171
+ topic: string;
172
+ data: Uint8Array;
173
+ sequenceNumber: bigint;
174
+ }): Promise<Message>;
175
+ /**
176
+ * Get a list of the peer-ids that are subscribed to one topic.
177
+ */
178
+ getSubscribers(topic: string): PeerId[];
179
+ /**
180
+ * Publishes messages to all subscribed peers
181
+ */
182
+ publish(topic: string, data?: Uint8Array): Promise<PublishResult>;
183
+ /**
184
+ * Overriding the implementation of publish should handle the appropriate algorithms for the publish/subscriber implementation.
185
+ * For example, a Floodsub implementation might simply publish each message to each topic for every peer.
186
+ *
187
+ * `sender` might be this peer, or we might be forwarding a message on behalf of another peer, in which case sender
188
+ * is the peer we received the message from, which may not be the peer the message was created by.
189
+ */
190
+ publishMessage(from: PeerId, message: Message): Promise<PublishResult>;
191
+ /**
192
+ * Subscribes to a given topic.
193
+ */
194
+ subscribe(topic: string): void;
195
+ /**
196
+ * Unsubscribe from the given topic
197
+ */
198
+ unsubscribe(topic: string): void;
199
+ /**
200
+ * Get the list of topics which the peer is subscribed to.
201
+ */
202
+ getTopics(): string[];
203
+ getPeers(): PeerId[];
204
+ }
205
+ //# source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