This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.ts
269 lines (234 loc) · 6.58 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import type { Stream } from '@libp2p/interface-connection'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { EventEmitter } from '@libp2p/interfaces/events'
import type { Pushable } from 'it-pushable'
import type { Uint8ArrayList } from 'uint8arraylist'
/**
* On the producing side:
* * Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
*
* On the consuming side:
* * Enforce the fields to be present, reject otherwise.
* * Propagate only if the fields are valid and signature can be verified, reject otherwise.
*/
export const StrictSign = 'StrictSign'
/**
* On the producing side:
* * Build messages without the signature, key, from and seqno fields.
* * The corresponding protobuf key-value pairs are absent from the marshalled message, not just empty.
*
* On the consuming side:
* * Enforce the fields to be absent, reject otherwise.
* * Propagate only if the fields are absent, reject otherwise.
* * 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.
*/
export const StrictNoSign = 'StrictNoSign'
export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign
export interface SignedMessage {
type: 'signed'
from: PeerId
topic: string
data: Uint8Array
sequenceNumber: bigint
signature: Uint8Array
key: Uint8Array
}
export interface UnsignedMessage {
type: 'unsigned'
topic: string
data: Uint8Array
}
export type Message = SignedMessage | UnsignedMessage
export interface PubSubRPCMessage {
from?: Uint8Array
topic?: string
data?: Uint8Array
sequenceNumber?: Uint8Array
signature?: Uint8Array
key?: Uint8Array
}
export interface PubSubRPCSubscription {
subscribe?: boolean
topic?: string
}
export interface PubSubRPC {
subscriptions: PubSubRPCSubscription[]
messages: PubSubRPCMessage[]
}
export interface PeerStreams extends EventEmitter<PeerStreamEvents> {
id: PeerId
protocol: string
outboundStream?: Pushable<Uint8ArrayList>
inboundStream?: AsyncIterable<Uint8ArrayList>
isWritable: boolean
close: () => void
write: (buf: Uint8Array | Uint8ArrayList) => void
attachInboundStream: (stream: Stream) => AsyncIterable<Uint8ArrayList>
attachOutboundStream: (stream: Stream) => Promise<Pushable<Uint8ArrayList>>
}
export interface PubSubInit {
enabled?: boolean
multicodecs?: string[]
/**
* defines how signatures should be handled
*/
globalSignaturePolicy?: SignaturePolicy
/**
* if can relay messages not subscribed
*/
canRelayMessage?: boolean
/**
* if publish should emit to self, if subscribed
*/
emitSelf?: boolean
/**
* handle this many incoming pubsub messages concurrently
*/
messageProcessingConcurrency?: number
/**
* How many parallel incoming streams to allow on the pubsub protocol per-connection
*/
maxInboundStreams?: number
/**
* How many parallel outgoing streams to allow on the pubsub protocol per-connection
*/
maxOutboundStreams?: number
}
interface Subscription {
topic: string
subscribe: boolean
}
export interface SubscriptionChangeData {
peerId: PeerId
subscriptions: Subscription[]
}
export interface PubSubEvents {
'subscription-change': CustomEvent<SubscriptionChangeData>
'message': CustomEvent<Message>
}
export interface PublishResult {
recipients: PeerId[]
}
export enum TopicValidatorResult {
/**
* The message is considered valid, and it should be delivered and forwarded to the network
*/
Accept = 'accept',
/**
* The message is neither delivered nor forwarded to the network
*/
Ignore = 'ignore',
/**
* The message is considered invalid, and it should be rejected
*/
Reject = 'reject'
}
export interface TopicValidatorFn {
(peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>
}
export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends EventEmitter<Events> {
/**
* The global signature policy controls whether or not we sill send and receive
* signed or unsigned messages.
*
* Signed messages prevent spoofing message senders and should be preferred to
* using unsigned messages.
*/
globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign
/**
* A list of multicodecs that contain the pubsub protocol name.
*/
multicodecs: string[]
/**
* Pubsub routers support message validators per topic, which will validate the message
* before its propagations. They are stored in a map where keys are the topic name and
* values are the validators.
*
* @example
*
* ```js
* const topic = 'topic'
* const validateMessage = (msgTopic, msg) => {
* const input = uint8ArrayToString(msg.data)
* const validInputs = ['a', 'b', 'c']
*
* if (!validInputs.includes(input)) {
* throw new Error('no valid input received')
* }
* }
* libp2p.pubsub.topicValidators.set(topic, validateMessage)
* ```
*/
topicValidators: Map<string, TopicValidatorFn>
getPeers: () => PeerId[]
/**
* Gets a list of topics the node is subscribed to.
*
* ```js
* const topics = libp2p.pubsub.getTopics()
* ```
*/
getTopics: () => string[]
/**
* Subscribes to a pubsub topic.
*
* @example
*
* ```js
* const topic = 'topic'
* const handler = (msg) => {
* if (msg.topic === topic) {
* // msg.data - pubsub data received
* }
* }
*
* libp2p.pubsub.addEventListener('message', handler)
* libp2p.pubsub.subscribe(topic)
* ```
*/
subscribe: (topic: string) => void
/**
* Unsubscribes from a pubsub topic.
*
* @example
*
* ```js
* const topic = 'topic'
* const handler = (msg) => {
* // msg.data - pubsub data received
* }
*
* libp2p.pubsub.removeEventListener(topic handler)
* libp2p.pubsub.unsubscribe(topic)
* ```
*/
unsubscribe: (topic: string) => void
/**
* Gets a list of the PeerIds that are subscribed to one topic.
*
* @example
*
* ```js
* const peerIds = libp2p.pubsub.getSubscribers(topic)
* ```
*/
getSubscribers: (topic: string) => PeerId[]
/**
* Publishes messages to the given topic.
*
* @example
*
* ```js
* const topic = 'topic'
* const data = uint8ArrayFromString('data')
*
* await libp2p.pubsub.publish(topic, data)
* ```
*/
publish: (topic: string, data: Uint8Array) => Promise<PublishResult>
}
export interface PeerStreamEvents {
'stream:inbound': CustomEvent<never>
'stream:outbound': CustomEvent<never>
'close': CustomEvent<never>
}