@libp2p/interface 0.0.1-05abd49f → 0.0.1-06f4901a

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