@libp2p/interface 2.11.0-87bc8d4fb → 2.11.0-9a9b11fd4

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.
Files changed (72) hide show
  1. package/dist/index.min.js +1 -1
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/connection-encrypter.d.ts +17 -15
  4. package/dist/src/connection-encrypter.d.ts.map +1 -1
  5. package/dist/src/connection-gater.d.ts +1 -2
  6. package/dist/src/connection-gater.d.ts.map +1 -1
  7. package/dist/src/connection-protector.d.ts +9 -0
  8. package/dist/src/connection-protector.d.ts.map +1 -0
  9. package/dist/src/connection-protector.js +2 -0
  10. package/dist/src/connection-protector.js.map +1 -0
  11. package/dist/src/connection.d.ts +36 -233
  12. package/dist/src/connection.d.ts.map +1 -1
  13. package/dist/src/connection.js.map +1 -1
  14. package/dist/src/errors.d.ts +14 -0
  15. package/dist/src/errors.d.ts.map +1 -1
  16. package/dist/src/errors.js +20 -0
  17. package/dist/src/errors.js.map +1 -1
  18. package/dist/src/events.d.ts +26 -0
  19. package/dist/src/events.d.ts.map +1 -0
  20. package/dist/src/events.js +36 -0
  21. package/dist/src/events.js.map +1 -0
  22. package/dist/src/index.d.ts +35 -5
  23. package/dist/src/index.d.ts.map +1 -1
  24. package/dist/src/index.js +7 -3
  25. package/dist/src/index.js.map +1 -1
  26. package/dist/src/message-stream.d.ts +159 -0
  27. package/dist/src/message-stream.d.ts.map +1 -0
  28. package/dist/src/message-stream.js +2 -0
  29. package/dist/src/message-stream.js.map +1 -0
  30. package/dist/src/metrics.d.ts +2 -2
  31. package/dist/src/metrics.d.ts.map +1 -1
  32. package/dist/src/multiaddr-connection.d.ts +25 -0
  33. package/dist/src/multiaddr-connection.d.ts.map +1 -0
  34. package/dist/src/multiaddr-connection.js +2 -0
  35. package/dist/src/multiaddr-connection.js.map +1 -0
  36. package/dist/src/peer-store.d.ts +0 -4
  37. package/dist/src/peer-store.d.ts.map +1 -1
  38. package/dist/src/stream-handler.d.ts +12 -13
  39. package/dist/src/stream-handler.d.ts.map +1 -1
  40. package/dist/src/stream-muxer.d.ts +113 -30
  41. package/dist/src/stream-muxer.d.ts.map +1 -1
  42. package/dist/src/stream.d.ts +63 -0
  43. package/dist/src/stream.d.ts.map +1 -0
  44. package/dist/src/stream.js +2 -0
  45. package/dist/src/stream.js.map +1 -0
  46. package/dist/src/topology.d.ts +3 -3
  47. package/dist/src/topology.d.ts.map +1 -1
  48. package/dist/src/transport.d.ts +20 -13
  49. package/dist/src/transport.d.ts.map +1 -1
  50. package/dist/src/transport.js.map +1 -1
  51. package/package.json +4 -6
  52. package/src/connection-encrypter.ts +19 -16
  53. package/src/connection-gater.ts +1 -2
  54. package/src/connection-protector.ts +9 -0
  55. package/src/connection.ts +38 -270
  56. package/src/errors.ts +24 -0
  57. package/src/events.ts +44 -0
  58. package/src/index.ts +38 -5
  59. package/src/message-stream.ts +183 -0
  60. package/src/metrics.ts +2 -2
  61. package/src/multiaddr-connection.ts +27 -0
  62. package/src/peer-store.ts +0 -5
  63. package/src/stream-handler.ts +14 -15
  64. package/src/stream-muxer.ts +122 -30
  65. package/src/stream.ts +70 -0
  66. package/src/topology.ts +3 -3
  67. package/src/transport.ts +25 -14
  68. package/dist/src/pubsub.d.ts +0 -248
  69. package/dist/src/pubsub.d.ts.map +0 -1
  70. package/dist/src/pubsub.js +0 -47
  71. package/dist/src/pubsub.js.map +0 -1
  72. package/src/pubsub.ts +0 -286
@@ -1,248 +0,0 @@
1
- import type { Stream } from './connection.js';
2
- import type { PublicKey } from './keys.js';
3
- import type { PeerId } from './peer-id.js';
4
- import type { Pushable } from 'it-pushable';
5
- import type { TypedEventTarget } from 'main-event';
6
- import type { Uint8ArrayList } from 'uint8arraylist';
7
- /**
8
- * On the producing side:
9
- * - Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
10
- *
11
- * On the consuming side:
12
- * - Enforce the fields to be present, reject otherwise.
13
- * - Propagate only if the fields are valid and signature can be verified, reject otherwise.
14
- */
15
- export declare const StrictSign = "StrictSign";
16
- /**
17
- * On the producing side:
18
- * - Build messages without the signature, key, from and seqno fields.
19
- * - The corresponding protobuf key-value pairs are absent from the marshaled message, not just empty.
20
- *
21
- * On the consuming side:
22
- * - Enforce the fields to be absent, reject otherwise.
23
- * - Propagate only if the fields are absent, reject otherwise.
24
- * - A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
25
- */
26
- export declare const StrictNoSign = "StrictNoSign";
27
- export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign;
28
- export interface SignedMessage {
29
- type: 'signed';
30
- from: PeerId;
31
- topic: string;
32
- data: Uint8Array;
33
- sequenceNumber: bigint;
34
- signature: Uint8Array;
35
- key: PublicKey;
36
- }
37
- export interface UnsignedMessage {
38
- type: 'unsigned';
39
- topic: string;
40
- data: Uint8Array;
41
- }
42
- export type Message = SignedMessage | UnsignedMessage;
43
- export interface PubSubRPCMessage {
44
- from?: Uint8Array;
45
- topic?: string;
46
- data?: Uint8Array;
47
- sequenceNumber?: Uint8Array;
48
- signature?: Uint8Array;
49
- key?: Uint8Array;
50
- }
51
- export interface PubSubRPCSubscription {
52
- subscribe?: boolean;
53
- topic?: string;
54
- }
55
- export interface PubSubRPC {
56
- subscriptions: PubSubRPCSubscription[];
57
- messages: PubSubRPCMessage[];
58
- }
59
- export interface PeerStreams extends TypedEventTarget<PeerStreamEvents> {
60
- id: PeerId;
61
- protocol: string;
62
- outboundStream?: Pushable<Uint8ArrayList>;
63
- inboundStream?: AsyncIterable<Uint8ArrayList>;
64
- isWritable: boolean;
65
- close(): void;
66
- write(buf: Uint8Array | Uint8ArrayList): void;
67
- attachInboundStream(stream: Stream): AsyncIterable<Uint8ArrayList>;
68
- attachOutboundStream(stream: Stream): Promise<Pushable<Uint8ArrayList>>;
69
- }
70
- export interface PubSubInit {
71
- enabled?: boolean;
72
- multicodecs?: string[];
73
- /**
74
- * defines how signatures should be handled
75
- */
76
- globalSignaturePolicy?: SignaturePolicy;
77
- /**
78
- * if can relay messages not subscribed
79
- */
80
- canRelayMessage?: boolean;
81
- /**
82
- * if publish should emit to self, if subscribed
83
- */
84
- emitSelf?: boolean;
85
- /**
86
- * handle this many incoming pubsub messages concurrently
87
- */
88
- messageProcessingConcurrency?: number;
89
- /**
90
- * How many parallel incoming streams to allow on the pubsub protocol per-connection
91
- */
92
- maxInboundStreams?: number;
93
- /**
94
- * How many parallel outgoing streams to allow on the pubsub protocol per-connection
95
- */
96
- maxOutboundStreams?: number;
97
- }
98
- export interface Subscription {
99
- topic: string;
100
- subscribe: boolean;
101
- }
102
- export interface SubscriptionChangeData {
103
- peerId: PeerId;
104
- subscriptions: Subscription[];
105
- }
106
- export interface PubSubEvents {
107
- 'subscription-change': CustomEvent<SubscriptionChangeData>;
108
- message: CustomEvent<Message>;
109
- }
110
- export interface PublishResult {
111
- recipients: PeerId[];
112
- }
113
- export declare enum TopicValidatorResult {
114
- /**
115
- * The message is considered valid, and it should be delivered and forwarded to the network
116
- */
117
- Accept = "accept",
118
- /**
119
- * The message is neither delivered nor forwarded to the network
120
- */
121
- Ignore = "ignore",
122
- /**
123
- * The message is considered invalid, and it should be rejected
124
- */
125
- Reject = "reject"
126
- }
127
- export interface TopicValidatorFn {
128
- (peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>;
129
- }
130
- /**
131
- * @deprecated This will be removed from `@libp2p/interface` in a future release, pubsub implementations should declare their own types
132
- */
133
- export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends TypedEventTarget<Events> {
134
- /**
135
- * The global signature policy controls whether or not we sill send and receive
136
- * signed or unsigned messages.
137
- *
138
- * Signed messages prevent spoofing message senders and should be preferred to
139
- * using unsigned messages.
140
- */
141
- globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign;
142
- /**
143
- * A list of multicodecs that contain the pubsub protocol name.
144
- */
145
- multicodecs: string[];
146
- /**
147
- * Pubsub routers support message validators per topic, which will validate the message
148
- * before its propagations. They are stored in a map where keys are the topic name and
149
- * values are the validators.
150
- *
151
- * @example
152
- *
153
- * ```TypeScript
154
- * const topic = 'topic'
155
- * const validateMessage = (msgTopic, msg) => {
156
- * const input = uint8ArrayToString(msg.data)
157
- * const validInputs = ['a', 'b', 'c']
158
- *
159
- * if (!validInputs.includes(input)) {
160
- * throw new Error('no valid input received')
161
- * }
162
- * }
163
- * libp2p.pubsub.topicValidators.set(topic, validateMessage)
164
- * ```
165
- */
166
- topicValidators: Map<string, TopicValidatorFn>;
167
- getPeers(): PeerId[];
168
- /**
169
- * Gets a list of topics the node is subscribed to.
170
- *
171
- * ```TypeScript
172
- * const topics = libp2p.pubsub.getTopics()
173
- * ```
174
- */
175
- getTopics(): string[];
176
- /**
177
- * Subscribes to a pubsub topic.
178
- *
179
- * @example
180
- *
181
- * ```TypeScript
182
- * const topic = 'topic'
183
- * const handler = (msg) => {
184
- * if (msg.topic === topic) {
185
- * // msg.data - pubsub data received
186
- * }
187
- * }
188
- *
189
- * libp2p.pubsub.addEventListener('message', handler)
190
- * libp2p.pubsub.subscribe(topic)
191
- * ```
192
- */
193
- subscribe(topic: string): void;
194
- /**
195
- * Unsubscribes from a pubsub topic.
196
- *
197
- * @example
198
- *
199
- * ```TypeScript
200
- * const topic = 'topic'
201
- * const handler = (msg) => {
202
- * // msg.data - pubsub data received
203
- * }
204
- *
205
- * libp2p.pubsub.removeEventListener(topic handler)
206
- * libp2p.pubsub.unsubscribe(topic)
207
- * ```
208
- */
209
- unsubscribe(topic: string): void;
210
- /**
211
- * Gets a list of the PeerIds that are subscribed to one topic.
212
- *
213
- * @example
214
- *
215
- * ```TypeScript
216
- * const peerIds = libp2p.pubsub.getSubscribers(topic)
217
- * ```
218
- */
219
- getSubscribers(topic: string): PeerId[];
220
- /**
221
- * Publishes messages to the given topic.
222
- *
223
- * @example
224
- *
225
- * ```TypeScript
226
- * const topic = 'topic'
227
- * const data = uint8ArrayFromString('data')
228
- *
229
- * await libp2p.pubsub.publish(topic, data)
230
- * ```
231
- */
232
- publish(topic: string, data: Uint8Array): Promise<PublishResult>;
233
- }
234
- export interface PeerStreamEvents {
235
- 'stream:inbound': CustomEvent<never>;
236
- 'stream:outbound': CustomEvent<never>;
237
- close: CustomEvent<never>;
238
- }
239
- /**
240
- * All Pubsub implementations must use this symbol as the name of a property
241
- * with a boolean `true` value
242
- */
243
- export declare const pubSubSymbol: unique symbol;
244
- /**
245
- * Returns true if the passed argument is a PubSub implementation
246
- */
247
- export declare function isPubSub(obj?: any): obj is PubSub;
248
- //# sourceMappingURL=pubsub.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pubsub.d.ts","sourceRoot":"","sources":["../../src/pubsub.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAC1C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,eAAe,CAAA;AAEtC;;;;;;;;;GASG;AACH,eAAO,MAAM,YAAY,iBAAiB,CAAA;AAE1C,MAAM,MAAM,eAAe,GAAG,OAAO,UAAU,GAAG,OAAO,YAAY,CAAA;AAErE,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,QAAQ,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,UAAU,CAAA;IAChB,cAAc,EAAE,MAAM,CAAA;IACtB,SAAS,EAAE,UAAU,CAAA;IACrB,GAAG,EAAE,SAAS,CAAA;CACf;AAED,MAAM,WA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E,CAAA;IAErB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAA;IAE9C,QAAQ,IAAI,MAAM,EAAE,CAAA;IAEpB;;;;;;OAMG;IACH,SAAS,IAAI,MAAM,EAAE,CAAA;IAErB;;;;;;;;;;;;;;;;OAgBG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IAE9B;;;;;;;;;;;;;;OAcG;IACH,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IAEhC;;;;;;;;OAQG;IACH,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAEvC;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,CAAA;CACjE;AAED,MAAM,WAAW,gBAAgB;IAC/B,gBAAgB,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;IACpC,iBAAiB,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;IACrC,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;CAC1B;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,eAA+B,CAAA;AAExD;;GAEG;AACH,wBAAgB,QAAQ,CAAE,GAAG,CAAC,EAAE,GAAG,GAAG,GAAG,IAAI,MAAM,CAElD"}
@@ -1,47 +0,0 @@
1
- /**
2
- * On the producing side:
3
- * - Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
4
- *
5
- * On the consuming side:
6
- * - Enforce the fields to be present, reject otherwise.
7
- * - Propagate only if the fields are valid and signature can be verified, reject otherwise.
8
- */
9
- export const StrictSign = 'StrictSign';
10
- /**
11
- * On the producing side:
12
- * - Build messages without the signature, key, from and seqno fields.
13
- * - The corresponding protobuf key-value pairs are absent from the marshaled message, not just empty.
14
- *
15
- * On the consuming side:
16
- * - Enforce the fields to be absent, reject otherwise.
17
- * - Propagate only if the fields are absent, reject otherwise.
18
- * - A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
19
- */
20
- export const StrictNoSign = 'StrictNoSign';
21
- export var TopicValidatorResult;
22
- (function (TopicValidatorResult) {
23
- /**
24
- * The message is considered valid, and it should be delivered and forwarded to the network
25
- */
26
- TopicValidatorResult["Accept"] = "accept";
27
- /**
28
- * The message is neither delivered nor forwarded to the network
29
- */
30
- TopicValidatorResult["Ignore"] = "ignore";
31
- /**
32
- * The message is considered invalid, and it should be rejected
33
- */
34
- TopicValidatorResult["Reject"] = "reject";
35
- })(TopicValidatorResult || (TopicValidatorResult = {}));
36
- /**
37
- * All Pubsub implementations must use this symbol as the name of a property
38
- * with a boolean `true` value
39
- */
40
- export const pubSubSymbol = Symbol.for('@libp2p/pubsub');
41
- /**
42
- * Returns true if the passed argument is a PubSub implementation
43
- */
44
- export function isPubSub(obj) {
45
- return Boolean(obj?.[pubSubSymbol]);
46
- }
47
- //# sourceMappingURL=pubsub.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pubsub.js","sourceRoot":"","sources":["../../src/pubsub.ts"],"names":[],"mappings":"AAOA;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,YAAY,CAAA;AAEtC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,cAAc,CAAA;AA6G1C,MAAM,CAAN,IAAY,oBAaX;AAbD,WAAY,oBAAoB;IAC9B;;OAEG;IACH,yCAAiB,CAAA;IACjB;;OAEG;IACH,yCAAiB,CAAA;IACjB;;OAEG;IACH,yCAAiB,CAAA;AACnB,CAAC,EAbW,oBAAoB,KAApB,oBAAoB,QAa/B;AA6HD;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAA;AAExD;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAE,GAAS;IACjC,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,CAAC,CAAA;AACrC,CAAC"}
package/src/pubsub.ts DELETED
@@ -1,286 +0,0 @@
1
- import type { Stream } from './connection.js'
2
- import type { PublicKey } from './keys.js'
3
- import type { PeerId } from './peer-id.js'
4
- import type { Pushable } from 'it-pushable'
5
- import type { TypedEventTarget } from 'main-event'
6
- import type { Uint8ArrayList } from 'uint8arraylist'
7
-
8
- /**
9
- * On the producing side:
10
- * - Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
11
- *
12
- * On the consuming side:
13
- * - Enforce the fields to be present, reject otherwise.
14
- * - Propagate only if the fields are valid and signature can be verified, reject otherwise.
15
- */
16
- export const StrictSign = 'StrictSign'
17
-
18
- /**
19
- * On the producing side:
20
- * - Build messages without the signature, key, from and seqno fields.
21
- * - The corresponding protobuf key-value pairs are absent from the marshaled message, not just empty.
22
- *
23
- * On the consuming side:
24
- * - Enforce the fields to be absent, reject otherwise.
25
- * - Propagate only if the fields are absent, reject otherwise.
26
- * - A message_id function will not be able to use the above fields, and should instead rely on the data field. A commonplace strategy is to calculate a hash.
27
- */
28
- export const StrictNoSign = 'StrictNoSign'
29
-
30
- export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign
31
-
32
- export interface SignedMessage {
33
- type: 'signed'
34
- from: PeerId
35
- topic: string
36
- data: Uint8Array
37
- sequenceNumber: bigint
38
- signature: Uint8Array
39
- key: PublicKey
40
- }
41
-
42
- export interface UnsignedMessage {
43
- type: 'unsigned'
44
- topic: string
45
- data: Uint8Array
46
- }
47
-
48
- export type Message = SignedMessage | UnsignedMessage
49
-
50
- export interface PubSubRPCMessage {
51
- from?: Uint8Array
52
- topic?: string
53
- data?: Uint8Array
54
- sequenceNumber?: Uint8Array
55
- signature?: Uint8Array
56
- key?: Uint8Array
57
- }
58
-
59
- export interface PubSubRPCSubscription {
60
- subscribe?: boolean
61
- topic?: string
62
- }
63
-
64
- export interface PubSubRPC {
65
- subscriptions: PubSubRPCSubscription[]
66
- messages: PubSubRPCMessage[]
67
- }
68
-
69
- export interface PeerStreams extends TypedEventTarget<PeerStreamEvents> {
70
- id: PeerId
71
- protocol: string
72
- outboundStream?: Pushable<Uint8ArrayList>
73
- inboundStream?: AsyncIterable<Uint8ArrayList>
74
- isWritable: boolean
75
-
76
- close(): void
77
- write(buf: Uint8Array | Uint8ArrayList): void
78
- attachInboundStream(stream: Stream): AsyncIterable<Uint8ArrayList>
79
- attachOutboundStream(stream: Stream): Promise<Pushable<Uint8ArrayList>>
80
- }
81
-
82
- export interface PubSubInit {
83
- enabled?: boolean
84
-
85
- multicodecs?: string[]
86
-
87
- /**
88
- * defines how signatures should be handled
89
- */
90
- globalSignaturePolicy?: SignaturePolicy
91
-
92
- /**
93
- * if can relay messages not subscribed
94
- */
95
- canRelayMessage?: boolean
96
-
97
- /**
98
- * if publish should emit to self, if subscribed
99
- */
100
- emitSelf?: boolean
101
-
102
- /**
103
- * handle this many incoming pubsub messages concurrently
104
- */
105
- messageProcessingConcurrency?: number
106
-
107
- /**
108
- * How many parallel incoming streams to allow on the pubsub protocol per-connection
109
- */
110
- maxInboundStreams?: number
111
-
112
- /**
113
- * How many parallel outgoing streams to allow on the pubsub protocol per-connection
114
- */
115
- maxOutboundStreams?: number
116
- }
117
-
118
- export interface Subscription {
119
- topic: string
120
- subscribe: boolean
121
- }
122
-
123
- export interface SubscriptionChangeData {
124
- peerId: PeerId
125
- subscriptions: Subscription[]
126
- }
127
-
128
- export interface PubSubEvents {
129
- 'subscription-change': CustomEvent<SubscriptionChangeData>
130
- message: CustomEvent<Message>
131
- }
132
-
133
- export interface PublishResult {
134
- recipients: PeerId[]
135
- }
136
-
137
- export enum TopicValidatorResult {
138
- /**
139
- * The message is considered valid, and it should be delivered and forwarded to the network
140
- */
141
- Accept = 'accept',
142
- /**
143
- * The message is neither delivered nor forwarded to the network
144
- */
145
- Ignore = 'ignore',
146
- /**
147
- * The message is considered invalid, and it should be rejected
148
- */
149
- Reject = 'reject'
150
- }
151
-
152
- export interface TopicValidatorFn {
153
- (peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>
154
- }
155
-
156
- /**
157
- * @deprecated This will be removed from `@libp2p/interface` in a future release, pubsub implementations should declare their own types
158
- */
159
- export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends TypedEventTarget<Events> {
160
- /**
161
- * The global signature policy controls whether or not we sill send and receive
162
- * signed or unsigned messages.
163
- *
164
- * Signed messages prevent spoofing message senders and should be preferred to
165
- * using unsigned messages.
166
- */
167
- globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign
168
-
169
- /**
170
- * A list of multicodecs that contain the pubsub protocol name.
171
- */
172
- multicodecs: string[]
173
-
174
- /**
175
- * Pubsub routers support message validators per topic, which will validate the message
176
- * before its propagations. They are stored in a map where keys are the topic name and
177
- * values are the validators.
178
- *
179
- * @example
180
- *
181
- * ```TypeScript
182
- * const topic = 'topic'
183
- * const validateMessage = (msgTopic, msg) => {
184
- * const input = uint8ArrayToString(msg.data)
185
- * const validInputs = ['a', 'b', 'c']
186
- *
187
- * if (!validInputs.includes(input)) {
188
- * throw new Error('no valid input received')
189
- * }
190
- * }
191
- * libp2p.pubsub.topicValidators.set(topic, validateMessage)
192
- * ```
193
- */
194
- topicValidators: Map<string, TopicValidatorFn>
195
-
196
- getPeers(): PeerId[]
197
-
198
- /**
199
- * Gets a list of topics the node is subscribed to.
200
- *
201
- * ```TypeScript
202
- * const topics = libp2p.pubsub.getTopics()
203
- * ```
204
- */
205
- getTopics(): string[]
206
-
207
- /**
208
- * Subscribes to a pubsub topic.
209
- *
210
- * @example
211
- *
212
- * ```TypeScript
213
- * const topic = 'topic'
214
- * const handler = (msg) => {
215
- * if (msg.topic === topic) {
216
- * // msg.data - pubsub data received
217
- * }
218
- * }
219
- *
220
- * libp2p.pubsub.addEventListener('message', handler)
221
- * libp2p.pubsub.subscribe(topic)
222
- * ```
223
- */
224
- subscribe(topic: string): void
225
-
226
- /**
227
- * Unsubscribes from a pubsub topic.
228
- *
229
- * @example
230
- *
231
- * ```TypeScript
232
- * const topic = 'topic'
233
- * const handler = (msg) => {
234
- * // msg.data - pubsub data received
235
- * }
236
- *
237
- * libp2p.pubsub.removeEventListener(topic handler)
238
- * libp2p.pubsub.unsubscribe(topic)
239
- * ```
240
- */
241
- unsubscribe(topic: string): void
242
-
243
- /**
244
- * Gets a list of the PeerIds that are subscribed to one topic.
245
- *
246
- * @example
247
- *
248
- * ```TypeScript
249
- * const peerIds = libp2p.pubsub.getSubscribers(topic)
250
- * ```
251
- */
252
- getSubscribers(topic: string): PeerId[]
253
-
254
- /**
255
- * Publishes messages to the given topic.
256
- *
257
- * @example
258
- *
259
- * ```TypeScript
260
- * const topic = 'topic'
261
- * const data = uint8ArrayFromString('data')
262
- *
263
- * await libp2p.pubsub.publish(topic, data)
264
- * ```
265
- */
266
- publish(topic: string, data: Uint8Array): Promise<PublishResult>
267
- }
268
-
269
- export interface PeerStreamEvents {
270
- 'stream:inbound': CustomEvent<never>
271
- 'stream:outbound': CustomEvent<never>
272
- close: CustomEvent<never>
273
- }
274
-
275
- /**
276
- * All Pubsub implementations must use this symbol as the name of a property
277
- * with a boolean `true` value
278
- */
279
- export const pubSubSymbol = Symbol.for('@libp2p/pubsub')
280
-
281
- /**
282
- * Returns true if the passed argument is a PubSub implementation
283
- */
284
- export function isPubSub (obj?: any): obj is PubSub {
285
- return Boolean(obj?.[pubSubSymbol])
286
- }