@libp2p/floodsub 10.1.45 → 10.1.46-6059227cb

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.
@@ -31,14 +31,227 @@
31
31
  * node.services.pubsub.publish('fruit', new TextEncoder().encode('banana'))
32
32
  * ```
33
33
  */
34
- import { multicodec } from './config.js';
35
- import type { PubSubInit, PubSub } from '@libp2p/interface';
36
- import type { PubSubComponents } from '@libp2p/pubsub';
37
- export { multicodec };
38
- export interface FloodSubInit extends PubSubInit {
39
- seenTTL?: number;
34
+ import { pubSubSymbol } from './constants.ts';
35
+ import type { ComponentLogger, PeerId, PrivateKey, PublicKey, TypedEventTarget } from '@libp2p/interface';
36
+ import type { Registrar } from '@libp2p/interface-internal';
37
+ export declare const protocol = "/floodsub/1.0.0";
38
+ /**
39
+ * On the producing side:
40
+ * - Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
41
+ *
42
+ * On the consuming side:
43
+ * - Enforce the fields to be present, reject otherwise.
44
+ * - Propagate only if the fields are valid and signature can be verified, reject otherwise.
45
+ */
46
+ export declare const StrictSign = "StrictSign";
47
+ /**
48
+ * On the producing side:
49
+ * - Build messages without the signature, key, from and seqno fields.
50
+ * - The corresponding protobuf key-value pairs are absent from the marshaled message, not just empty.
51
+ *
52
+ * On the consuming side:
53
+ * - Enforce the fields to be absent, reject otherwise.
54
+ * - Propagate only if the fields are absent, reject otherwise.
55
+ * - 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.
56
+ */
57
+ export declare const StrictNoSign = "StrictNoSign";
58
+ export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign;
59
+ export interface SignedMessage {
60
+ type: 'signed';
61
+ from: PeerId;
62
+ topic: string;
63
+ data: Uint8Array;
64
+ sequenceNumber: bigint;
65
+ signature: Uint8Array;
66
+ key: PublicKey;
67
+ }
68
+ export interface UnsignedMessage {
69
+ type: 'unsigned';
70
+ topic: string;
71
+ data: Uint8Array;
72
+ }
73
+ export type Message = SignedMessage | UnsignedMessage;
74
+ export interface Subscription {
75
+ topic: string;
76
+ subscribe: boolean;
77
+ }
78
+ export interface SubscriptionChangeData {
79
+ peerId: PeerId;
80
+ subscriptions: Subscription[];
40
81
  }
41
- export interface FloodSubComponents extends PubSubComponents {
82
+ export interface FloodSubEvents {
83
+ 'subscription-change': CustomEvent<SubscriptionChangeData>;
84
+ message: CustomEvent<Message>;
85
+ }
86
+ export interface PublishResult {
87
+ recipients: PeerId[];
88
+ }
89
+ export declare enum TopicValidatorResult {
90
+ /**
91
+ * The message is considered valid, and it should be delivered and forwarded to the network
92
+ */
93
+ Accept = "accept",
94
+ /**
95
+ * The message is neither delivered nor forwarded to the network
96
+ */
97
+ Ignore = "ignore",
98
+ /**
99
+ * The message is considered invalid, and it should be rejected
100
+ */
101
+ Reject = "reject"
102
+ }
103
+ export interface TopicValidatorFn {
104
+ (peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>;
105
+ }
106
+ export interface PeerStreamEvents {
107
+ 'stream:inbound': CustomEvent<never>;
108
+ 'stream:outbound': CustomEvent<never>;
109
+ close: CustomEvent<never>;
110
+ }
111
+ export { pubSubSymbol };
112
+ /**
113
+ * Returns true if the passed argument is a PubSub implementation
114
+ */
115
+ export declare function isPubSub(obj?: any): obj is FloodSub;
116
+ export interface FloodSub extends TypedEventTarget<FloodSubEvents> {
117
+ /**
118
+ * The global signature policy controls whether or not we sill send and receive
119
+ * signed or unsigned messages.
120
+ *
121
+ * Signed messages prevent spoofing message senders and should be preferred to
122
+ * using unsigned messages.
123
+ */
124
+ globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign;
125
+ /**
126
+ * A list of multicodecs that contain the pubsub protocol name.
127
+ */
128
+ protocols: string[];
129
+ /**
130
+ * Pubsub routers support message validators per topic, which will validate the message
131
+ * before its propagations. They are stored in a map where keys are the topic name and
132
+ * values are the validators.
133
+ *
134
+ * @example
135
+ *
136
+ * ```TypeScript
137
+ * const topic = 'topic'
138
+ * const validateMessage = (msgTopic, msg) => {
139
+ * const input = uint8ArrayToString(msg.data)
140
+ * const validInputs = ['a', 'b', 'c']
141
+ *
142
+ * if (!validInputs.includes(input)) {
143
+ * throw new Error('no valid input received')
144
+ * }
145
+ * }
146
+ * libp2p.pubsub.topicValidators.set(topic, validateMessage)
147
+ * ```
148
+ */
149
+ topicValidators: Map<string, TopicValidatorFn>;
150
+ getPeers(): PeerId[];
151
+ /**
152
+ * Gets a list of topics the node is subscribed to.
153
+ *
154
+ * ```TypeScript
155
+ * const topics = libp2p.pubsub.getTopics()
156
+ * ```
157
+ */
158
+ getTopics(): string[];
159
+ /**
160
+ * Subscribes to a pubsub topic.
161
+ *
162
+ * @example
163
+ *
164
+ * ```TypeScript
165
+ * const topic = 'topic'
166
+ * const handler = (msg) => {
167
+ * if (msg.topic === topic) {
168
+ * // msg.data - pubsub data received
169
+ * }
170
+ * }
171
+ *
172
+ * libp2p.pubsub.addEventListener('message', handler)
173
+ * libp2p.pubsub.subscribe(topic)
174
+ * ```
175
+ */
176
+ subscribe(topic: string): void;
177
+ /**
178
+ * Unsubscribes from a pubsub topic.
179
+ *
180
+ * @example
181
+ *
182
+ * ```TypeScript
183
+ * const topic = 'topic'
184
+ * const handler = (msg) => {
185
+ * // msg.data - pubsub data received
186
+ * }
187
+ *
188
+ * libp2p.pubsub.removeEventListener(topic handler)
189
+ * libp2p.pubsub.unsubscribe(topic)
190
+ * ```
191
+ */
192
+ unsubscribe(topic: string): void;
193
+ /**
194
+ * Gets a list of the PeerIds that are subscribed to one topic.
195
+ *
196
+ * @example
197
+ *
198
+ * ```TypeScript
199
+ * const peerIds = libp2p.pubsub.getSubscribers(topic)
200
+ * ```
201
+ */
202
+ getSubscribers(topic: string): PeerId[];
203
+ /**
204
+ * Publishes messages to the given topic.
205
+ *
206
+ * @example
207
+ *
208
+ * ```TypeScript
209
+ * const topic = 'topic'
210
+ * const data = uint8ArrayFromString('data')
211
+ *
212
+ * await libp2p.pubsub.publish(topic, data)
213
+ * ```
214
+ */
215
+ publish(topic: string, data?: Uint8Array): Promise<PublishResult>;
216
+ }
217
+ export interface FloodSubComponents {
218
+ peerId: PeerId;
219
+ privateKey: PrivateKey;
220
+ registrar: Registrar;
221
+ logger: ComponentLogger;
222
+ }
223
+ export interface FloodSubInit {
224
+ seenTTL?: number;
225
+ /**
226
+ * Override the protocol registered with the registrar
227
+ *
228
+ * @default ['/floodsub/1.0.0']
229
+ */
230
+ protocols?: string[];
231
+ /**
232
+ * defines how signatures should be handled
233
+ */
234
+ globalSignaturePolicy?: SignaturePolicy;
235
+ /**
236
+ * if can relay messages not subscribed
237
+ */
238
+ canRelayMessage?: boolean;
239
+ /**
240
+ * if publish should emit to self, if subscribed
241
+ */
242
+ emitSelf?: boolean;
243
+ /**
244
+ * handle this many incoming pubsub messages concurrently
245
+ */
246
+ messageProcessingConcurrency?: number;
247
+ /**
248
+ * How many parallel incoming streams to allow on the pubsub protocol per-connection
249
+ */
250
+ maxInboundStreams?: number;
251
+ /**
252
+ * How many parallel outgoing streams to allow on the pubsub protocol per-connection
253
+ */
254
+ maxOutboundStreams?: number;
42
255
  }
43
- export declare function floodsub(init?: FloodSubInit): (components: FloodSubComponents) => PubSub;
256
+ export declare function floodsub(init?: FloodSubInit): (components: FloodSubComponents) => FloodSub;
44
257
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAMH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,OAAO,KAAK,EAAU,UAAU,EAAuD,MAAM,EAAE,MAAM,mBAAmB,CAAA;AACxH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAGtD,OAAO,EAAE,UAAU,EAAE,CAAA;AAErB,MAAM,WAAW,YAAa,SAA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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,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,CAAA;CAClE;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,UAAU,CAAA;IACtB,SAAS,EAAE,SAAS,CAAA;IACpB,MAAM,EAAE,eAAe,CAAA;CACxB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IAEpB;;OAEG;IACH,qBAAqB,CAAC,EAAE,eAAe,CAAA;IAEvC;;OAEG;IACH,eAAe,CAAC,EAAE,OAAO,CAAA;IAEzB;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;IAElB;;OAEG;IACH,4BAA4B,CAAC,EAAE,MAAM,CAAA;IAErC;;OAEG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAE1B;;OAEG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAED,wBAAgB,QAAQ,CAAE,IAAI,GAAE,YAAiB,GAAG,CAAC,UAAU,EAAE,kBAAkB,KAAK,QAAQ,CAE/F"}
package/dist/src/index.js CHANGED
@@ -31,103 +31,52 @@
31
31
  * node.services.pubsub.publish('fruit', new TextEncoder().encode('banana'))
32
32
  * ```
33
33
  */
34
- import { pubSubSymbol, serviceCapabilities, serviceDependencies } from '@libp2p/interface';
35
- import { PubSubBaseProtocol } from '@libp2p/pubsub';
36
- import { toString } from 'uint8arrays/to-string';
37
- import { SimpleTimeCache } from './cache.js';
38
- import { multicodec } from './config.js';
39
- import { RPC } from './message/rpc.js';
40
- export { multicodec };
34
+ import { pubSubSymbol } from "./constants.js";
35
+ import { FloodSub as FloodSubClass } from './floodsub.js';
36
+ export const protocol = '/floodsub/1.0.0';
41
37
  /**
42
- * FloodSub (aka dumbsub is an implementation of pubsub focused on
43
- * delivering an API for Publish/Subscribe, but with no CastTree Forming
44
- * (it just floods the network).
38
+ * On the producing side:
39
+ * - Build messages with the signature, key (from may be enough for certain inlineable public key types), from and seqno fields.
40
+ *
41
+ * On the consuming side:
42
+ * - Enforce the fields to be present, reject otherwise.
43
+ * - Propagate only if the fields are valid and signature can be verified, reject otherwise.
45
44
  */
46
- class FloodSub extends PubSubBaseProtocol {
47
- seenCache;
48
- constructor(components, init) {
49
- super(components, {
50
- ...init,
51
- canRelayMessage: true,
52
- multicodecs: [multicodec]
53
- });
54
- this.log = components.logger.forComponent('libp2p:floodsub');
55
- /**
56
- * Cache of seen messages
57
- *
58
- * @type {TimeCache}
59
- */
60
- this.seenCache = new SimpleTimeCache({
61
- validityMs: init?.seenTTL ?? 30000
62
- });
63
- }
64
- [pubSubSymbol] = true;
65
- [Symbol.toStringTag] = '@libp2p/floodsub';
66
- [serviceCapabilities] = [
67
- '@libp2p/pubsub'
68
- ];
69
- [serviceDependencies] = [
70
- '@libp2p/identify'
71
- ];
72
- /**
73
- * Decode a Uint8Array into an RPC object
74
- */
75
- decodeRpc(bytes) {
76
- return RPC.decode(bytes);
77
- }
45
+ export const StrictSign = 'StrictSign';
46
+ /**
47
+ * On the producing side:
48
+ * - Build messages without the signature, key, from and seqno fields.
49
+ * - The corresponding protobuf key-value pairs are absent from the marshaled message, not just empty.
50
+ *
51
+ * On the consuming side:
52
+ * - Enforce the fields to be absent, reject otherwise.
53
+ * - Propagate only if the fields are absent, reject otherwise.
54
+ * - 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.
55
+ */
56
+ export const StrictNoSign = 'StrictNoSign';
57
+ export var TopicValidatorResult;
58
+ (function (TopicValidatorResult) {
78
59
  /**
79
- * Encode an RPC object into a Uint8Array
60
+ * The message is considered valid, and it should be delivered and forwarded to the network
80
61
  */
81
- encodeRpc(rpc) {
82
- return RPC.encode(rpc);
83
- }
84
- decodeMessage(bytes) {
85
- return RPC.Message.decode(bytes);
86
- }
87
- encodeMessage(rpc) {
88
- return RPC.Message.encode(rpc);
89
- }
62
+ TopicValidatorResult["Accept"] = "accept";
90
63
  /**
91
- * Process incoming message
92
- * Extends base implementation to check router cache.
64
+ * The message is neither delivered nor forwarded to the network
93
65
  */
94
- async processMessage(from, message) {
95
- // Check if I've seen the message, if yes, ignore
96
- const seqno = await super.getMsgId(message);
97
- const msgIdStr = toString(seqno, 'base64');
98
- if (this.seenCache.has(msgIdStr)) {
99
- return;
100
- }
101
- this.seenCache.put(msgIdStr, true);
102
- await super.processMessage(from, message);
103
- }
66
+ TopicValidatorResult["Ignore"] = "ignore";
104
67
  /**
105
- * Publish message created. Forward it to the peers.
68
+ * The message is considered invalid, and it should be rejected
106
69
  */
107
- async publishMessage(from, message) {
108
- const peers = this.getSubscribers(message.topic);
109
- const recipients = [];
110
- if (peers == null || peers.length === 0) {
111
- this.log('no peers are subscribed to topic %s', message.topic);
112
- return { recipients };
113
- }
114
- peers.forEach(id => {
115
- if (this.components.peerId.equals(id)) {
116
- this.log('not sending message on topic %s to myself', message.topic);
117
- return;
118
- }
119
- if (id.equals(from)) {
120
- this.log('not sending message on topic %s to sender %p', message.topic, id);
121
- return;
122
- }
123
- this.log('publish msgs on topics %s %p', message.topic, id);
124
- recipients.push(id);
125
- this.send(id, { messages: [message] });
126
- });
127
- return { recipients };
128
- }
70
+ TopicValidatorResult["Reject"] = "reject";
71
+ })(TopicValidatorResult || (TopicValidatorResult = {}));
72
+ export { pubSubSymbol };
73
+ /**
74
+ * Returns true if the passed argument is a PubSub implementation
75
+ */
76
+ export function isPubSub(obj) {
77
+ return Boolean(obj?.[pubSubSymbol]);
129
78
  }
130
79
  export function floodsub(init = {}) {
131
- return (components) => new FloodSub(components, init);
80
+ return (components) => new FloodSubClass(components, init);
132
81
  }
133
82
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAC1F,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAKtC,OAAO,EAAE,UAAU,EAAE,CAAA;AAUrB;;;;GAIG;AACH,MAAM,QAAS,SAAQ,kBAAkB;IAChC,SAAS,CAA0B;IAE1C,YAAa,UAA8B,EAAE,IAAmB;QAC9D,KAAK,CAAC,UAAU,EAAE;YAChB,GAAG,IAAI;YACP,eAAe,EAAE,IAAI;YACrB,WAAW,EAAE,CAAC,UAAU,CAAC;SAC1B,CAAC,CAAA;QAEF,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,iBAAiB,CAAC,CAAA;QAE5D;;;;WAIG;QACH,IAAI,CAAC,SAAS,GAAG,IAAI,eAAe,CAAU;YAC5C,UAAU,EAAE,IAAI,EAAE,OAAO,IAAI,KAAK;SACnC,CAAC,CAAA;IACJ,CAAC;IAEQ,CAAC,YAAY,CAAC,GAAG,IAAI,CAAA;IAErB,CAAC,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,EAAE,QAAQ,IAAI,aAAa,EAAE,MAAM,eAAe,CAAA;AAIzD,MAAM,CAAC,MAAM,QAAQ,GAAG,iBAAiB,CAAA;AAEzC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,YAAY,CAAA;AAEtC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,cAAc,CAAA;AAyC1C,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;AAYD,OAAO,EAAE,YAAY,EAAE,CAAA;AAEvB;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAE,GAAS;IACjC,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,CAAC,CAAA;AACrC,CAAC;AAgKD,MAAM,UAAU,QAAQ,CAAE,OAAqB,EAAE;IAC/C,OAAO,CAAC,UAA8B,EAAE,EAAE,CAAC,IAAI,aAAa,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AAChF,CAAC"}
@@ -0,0 +1,71 @@
1
+ import { TypedEventEmitter } from 'main-event';
2
+ import { Uint8ArrayList } from 'uint8arraylist';
3
+ import type { PeerStreamEvents } from './index.ts';
4
+ import type { ComponentLogger, Stream, PeerId } from '@libp2p/interface';
5
+ import type { DecoderOptions as LpDecoderOptions } from 'it-length-prefixed';
6
+ import type { Pushable } from 'it-pushable';
7
+ export interface PeerStreamsInit {
8
+ id: PeerId;
9
+ protocol: string;
10
+ }
11
+ export interface PeerStreamsComponents {
12
+ logger: ComponentLogger;
13
+ }
14
+ export interface DecoderOptions extends LpDecoderOptions {
15
+ }
16
+ /**
17
+ * Thin wrapper around a peer's inbound / outbound pubsub streams
18
+ */
19
+ export declare class PeerStreams extends TypedEventEmitter<PeerStreamEvents> {
20
+ readonly id: PeerId;
21
+ readonly protocol: string;
22
+ /**
23
+ * Write stream - it's preferable to use the write method
24
+ */
25
+ outboundStream?: Pushable<Uint8ArrayList>;
26
+ /**
27
+ * Read stream
28
+ */
29
+ inboundStream?: AsyncIterable<Uint8ArrayList>;
30
+ /**
31
+ * The raw outbound stream, as retrieved from conn.newStream
32
+ */
33
+ private _rawOutboundStream?;
34
+ /**
35
+ * The raw inbound stream, as retrieved from the callback from libp2p.handle
36
+ */
37
+ private _rawInboundStream?;
38
+ /**
39
+ * An AbortController for controlled shutdown of the inbound stream
40
+ */
41
+ private readonly _inboundAbortController;
42
+ private closed;
43
+ private readonly log;
44
+ constructor(components: PeerStreamsComponents, init: PeerStreamsInit);
45
+ /**
46
+ * Do we have a connection to read from?
47
+ */
48
+ get isReadable(): boolean;
49
+ /**
50
+ * Do we have a connection to write on?
51
+ */
52
+ get isWritable(): boolean;
53
+ /**
54
+ * Send a message to this peer.
55
+ * Throws if there is no `stream` to write to available.
56
+ */
57
+ write(data: Uint8Array | Uint8ArrayList): void;
58
+ /**
59
+ * Attach a raw inbound stream and setup a read stream
60
+ */
61
+ attachInboundStream(stream: Stream, decoderOptions?: DecoderOptions): AsyncIterable<Uint8ArrayList>;
62
+ /**
63
+ * Attach a raw outbound stream and setup a write stream
64
+ */
65
+ attachOutboundStream(stream: Stream): Promise<Pushable<Uint8ArrayList>>;
66
+ /**
67
+ * Closes the open connection to peer
68
+ */
69
+ close(): void;
70
+ }
71
+ //# sourceMappingURL=peer-streams.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer-streams.d.ts","sourceRoot":"","sources":["../../src/peer-streams.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAE9C,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,KAAK,EAAE,eAAe,EAA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OAEG;IACG,oBAAoB,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;IAqD9E;;OAEG;IACH,KAAK,IAAK,IAAI;CAsBf"}
@@ -0,0 +1,154 @@
1
+ import { AbortError } from '@libp2p/interface';
2
+ import { pipe } from '@libp2p/utils';
3
+ import * as lp from 'it-length-prefixed';
4
+ import { pushable } from 'it-pushable';
5
+ import { TypedEventEmitter } from 'main-event';
6
+ import { pEvent } from 'p-event';
7
+ import { Uint8ArrayList } from 'uint8arraylist';
8
+ /**
9
+ * Thin wrapper around a peer's inbound / outbound pubsub streams
10
+ */
11
+ export class PeerStreams extends TypedEventEmitter {
12
+ id;
13
+ protocol;
14
+ /**
15
+ * Write stream - it's preferable to use the write method
16
+ */
17
+ outboundStream;
18
+ /**
19
+ * Read stream
20
+ */
21
+ inboundStream;
22
+ /**
23
+ * The raw outbound stream, as retrieved from conn.newStream
24
+ */
25
+ _rawOutboundStream;
26
+ /**
27
+ * The raw inbound stream, as retrieved from the callback from libp2p.handle
28
+ */
29
+ _rawInboundStream;
30
+ /**
31
+ * An AbortController for controlled shutdown of the inbound stream
32
+ */
33
+ _inboundAbortController;
34
+ closed;
35
+ log;
36
+ constructor(components, init) {
37
+ super();
38
+ this.log = components.logger.forComponent('libp2p-pubsub:peer-streams');
39
+ this.id = init.id;
40
+ this.protocol = init.protocol;
41
+ this._inboundAbortController = new AbortController();
42
+ this.closed = false;
43
+ }
44
+ /**
45
+ * Do we have a connection to read from?
46
+ */
47
+ get isReadable() {
48
+ return Boolean(this.inboundStream);
49
+ }
50
+ /**
51
+ * Do we have a connection to write on?
52
+ */
53
+ get isWritable() {
54
+ return Boolean(this.outboundStream);
55
+ }
56
+ /**
57
+ * Send a message to this peer.
58
+ * Throws if there is no `stream` to write to available.
59
+ */
60
+ write(data) {
61
+ if (this.outboundStream == null) {
62
+ const id = this.id.toString();
63
+ throw new Error('No writable connection to ' + id);
64
+ }
65
+ this.outboundStream.push(data instanceof Uint8Array ? new Uint8ArrayList(data) : data);
66
+ }
67
+ /**
68
+ * Attach a raw inbound stream and setup a read stream
69
+ */
70
+ attachInboundStream(stream, decoderOptions) {
71
+ const abortListener = () => {
72
+ stream.abort(new AbortError());
73
+ };
74
+ this._inboundAbortController.signal.addEventListener('abort', abortListener, {
75
+ once: true
76
+ });
77
+ // Create and attach a new inbound stream
78
+ // The inbound stream is:
79
+ // - abortable, set to only return on abort, rather than throw
80
+ // - transformed with length-prefix transform
81
+ this._rawInboundStream = stream;
82
+ this.inboundStream = pipe(this._rawInboundStream, (source) => lp.decode(source, decoderOptions));
83
+ this.dispatchEvent(new CustomEvent('stream:inbound'));
84
+ return this.inboundStream;
85
+ }
86
+ /**
87
+ * Attach a raw outbound stream and setup a write stream
88
+ */
89
+ async attachOutboundStream(stream) {
90
+ // If an outbound stream already exists, gently close it
91
+ const _prevStream = this.outboundStream;
92
+ if (this.outboundStream != null) {
93
+ // End the stream without emitting a close event
94
+ this.outboundStream.end();
95
+ }
96
+ this._rawOutboundStream = stream;
97
+ this.outboundStream = pushable({
98
+ onEnd: (shouldEmit) => {
99
+ // close writable side of the stream if it exists
100
+ this._rawOutboundStream?.close()
101
+ .catch(err => {
102
+ this.log('error closing outbound stream', err);
103
+ });
104
+ this._rawOutboundStream = undefined;
105
+ this.outboundStream = undefined;
106
+ if (shouldEmit != null) {
107
+ this.dispatchEvent(new CustomEvent('close'));
108
+ }
109
+ }
110
+ });
111
+ pipe(this.outboundStream, (source) => lp.encode(source), async (source) => {
112
+ for await (const buf of source) {
113
+ const sendMore = stream.send(buf);
114
+ if (sendMore === false) {
115
+ await pEvent(stream, 'drain', {
116
+ rejectionEvents: [
117
+ 'close'
118
+ ]
119
+ });
120
+ }
121
+ }
122
+ }).catch((err) => {
123
+ this.log.error(err);
124
+ });
125
+ // Only emit if the connection is new
126
+ if (_prevStream == null) {
127
+ this.dispatchEvent(new CustomEvent('stream:outbound'));
128
+ }
129
+ return this.outboundStream;
130
+ }
131
+ /**
132
+ * Closes the open connection to peer
133
+ */
134
+ close() {
135
+ if (this.closed) {
136
+ return;
137
+ }
138
+ this.closed = true;
139
+ // End the outbound stream
140
+ if (this.outboundStream != null) {
141
+ this.outboundStream.end();
142
+ }
143
+ // End the inbound stream
144
+ if (this.inboundStream != null) {
145
+ this._inboundAbortController.abort();
146
+ }
147
+ this._rawOutboundStream = undefined;
148
+ this.outboundStream = undefined;
149
+ this._rawInboundStream = undefined;
150
+ this.inboundStream = undefined;
151
+ this.dispatchEvent(new CustomEvent('close'));
152
+ }
153
+ }
154
+ //# sourceMappingURL=peer-streams.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer-streams.js","sourceRoot":"","sources":["../../src/peer-streams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;AACpC,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAA;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAmB/C;;GAEG;AACH,MAAM,OAAO,WAAY,SAAQ,iBAAmC;IAClD,EAAE,CAAQ;IACV,QAAQ,CAAQ;IAChC;;OAEG;IACI,cAAc,CAA2B;IAChD;;OAEG;IACI,aAAa,CAAgC;IACpD;;OAEG;IACK,kBAAkB,CAAS;IACnC;;OAEG;IACK,iBAAiB,CAAS;IAClC;;OAEG;IACc,uBAAuB,CAAiB;IACjD,MAAM,CAAS;IACN,GAAG,CAAQ;IAE5B,YAAa,UAAiC,EAAE,IAAqB;QACnE,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAA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,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC,CAAA;IAC9C,CAAC;CACF"}
@@ -0,0 +1,23 @@
1
+ import type { PubSubRPCMessage } from './floodsub.ts';
2
+ import type { SignedMessage } from './index.ts';
3
+ import type { PeerId, PrivateKey, PublicKey } from '@libp2p/interface';
4
+ export declare const SignPrefix: Uint8Array<ArrayBufferLike>;
5
+ /**
6
+ * Signs the provided message with the given `peerId`
7
+ */
8
+ export declare function signMessage(privateKey: PrivateKey, message: {
9
+ from: PeerId;
10
+ topic: string;
11
+ data: Uint8Array;
12
+ sequenceNumber: bigint;
13
+ }, encode: (rpc: PubSubRPCMessage) => Uint8Array): Promise<SignedMessage>;
14
+ /**
15
+ * Verifies the signature of the given message
16
+ */
17
+ export declare function verifySignature(message: SignedMessage, encode: (rpc: PubSubRPCMessage) => Uint8Array): Promise<boolean>;
18
+ /**
19
+ * Returns the PublicKey associated with the given message.
20
+ * If no valid PublicKey can be retrieved an error will be returned.
21
+ */
22
+ export declare function messagePublicKey(message: SignedMessage): PublicKey;
23
+ //# sourceMappingURL=sign.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sign.d.ts","sourceRoot":"","sources":["../../src/sign.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAEtE,eAAO,MAAM,UAAU,6BAAyC,CAAA;AAEhE;;GAEG;AACH,wBAAsB,WAAW,CAAE,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,UAAU,CAAC;IAAC,cAAc,EAAE,MAAM,CAAA;CAAE,EAAE,MAAM,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,CAoBpN;AAED;;GAEG;AACH,wBAAsB,eAAe,CAAE,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC,GAAG,EAAE,gBAAgB,KAAK,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CA4B9H;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAE,OAAO,EAAE,aAAa,GAAG,SAAS,CAoBnE"}