@libp2p/floodsub 1.0.0 → 1.0.1

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.
@@ -1,10 +1,10 @@
1
- import { PubsubBaseProtocol } from '@libp2p/pubsub';
1
+ import { PubSubBaseProtocol } from '@libp2p/pubsub';
2
2
  import { multicodec } from './config.js';
3
3
  import { SimpleTimeCache } from './cache.js';
4
- import type { PubSub, PubSubEvents, PubSubOptions, Message } from '@libp2p/interfaces/pubsub';
4
+ import type { PubSub, PubSubEvents, PubSubInit, Message, PubSubRPC, PubSubRPCMessage } from '@libp2p/interfaces/pubsub';
5
5
  import type { PeerId } from '@libp2p/interfaces/peer-id';
6
6
  export { multicodec };
7
- export interface FloodSubOptions extends PubSubOptions {
7
+ export interface FloodSubInit extends PubSubInit {
8
8
  seenTTL?: number;
9
9
  }
10
10
  /**
@@ -12,9 +12,19 @@ export interface FloodSubOptions extends PubSubOptions {
12
12
  * delivering an API for Publish/Subscribe, but with no CastTree Forming
13
13
  * (it just floods the network).
14
14
  */
15
- export declare class FloodSub<EventMap extends PubSubEvents = PubSubEvents> extends PubsubBaseProtocol<EventMap> implements PubSub<EventMap & PubSubEvents> {
15
+ export declare class FloodSub<EventMap extends PubSubEvents = PubSubEvents> extends PubSubBaseProtocol<EventMap> implements PubSub<EventMap & PubSubEvents> {
16
16
  seenCache: SimpleTimeCache<boolean>;
17
- constructor(options: FloodSubOptions);
17
+ constructor(init?: FloodSubInit);
18
+ /**
19
+ * Decode a Uint8Array into an RPC object
20
+ */
21
+ decodeRpc(bytes: Uint8Array): PubSubRPC;
22
+ /**
23
+ * Encode an RPC object into a Uint8Array
24
+ */
25
+ encodeRpc(rpc: PubSubRPC): Uint8Array;
26
+ decodeMessage(bytes: Uint8Array): PubSubRPCMessage;
27
+ encodeMessage(rpc: PubSubRPCMessage): Uint8Array;
18
28
  /**
19
29
  * Process incoming message
20
30
  * Extends base implementation to check router cache.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;AAC7F,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAIxD,OAAO,EAAE,UAAU,EAAE,CAAA;AAErB,MAAM,WAAW,eAAgB,SAAQ,aAAa;IACpD,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;;;;GAIG;AACH,qBAAa,QAAQ,CAAE,QAAQ,SAAS,YAAY,GAAG,YAAY,CAAE,SAAQ,kBAAkB,CAAC,QAAQ,CAAE,YAAW,MAAM,CAAC,QAAQ,GAAG,YAAY,CAAC;IAC3I,SAAS,EAAE,eAAe,CAAC,OAAO,CAAC,CAAA;gBAE7B,OAAO,EAAE,eAAe;IAkBrC;;;OAGG;IACG,cAAc,CAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;IAcpD;;OAEG;IACG,cAAc,CAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;CAwBrD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAA;AACvH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AAMxD,OAAO,EAAE,UAAU,EAAE,CAAA;AAErB,MAAM,WAAW,YAAa,SAAQ,UAAU;IAC9C,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;;;;GAIG;AACH,qBAAa,QAAQ,CAAE,QAAQ,SAAS,YAAY,GAAG,YAAY,CAAE,SAAQ,kBAAkB,CAAC,QAAQ,CAAE,YAAW,MAAM,CAAC,QAAQ,GAAG,YAAY,CAAC;IAC3I,SAAS,EAAE,eAAe,CAAC,OAAO,CAAC,CAAA;gBAE7B,IAAI,CAAC,EAAE,YAAY;IAiBhC;;OAEG;IACH,SAAS,CAAE,KAAK,EAAE,UAAU,GAAG,SAAS;IAIxC;;OAEG;IACH,SAAS,CAAE,GAAG,EAAE,SAAS,GAAG,UAAU;IAItC,aAAa,CAAE,KAAK,EAAE,UAAU,GAAG,gBAAgB;IAInD,aAAa,CAAE,GAAG,EAAE,gBAAgB,GAAG,UAAU;IAIjD;;;OAGG;IACG,cAAc,CAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;IAcpD;;OAEG;IACG,cAAc,CAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;CAwBrD"}
package/dist/src/index.js CHANGED
@@ -1,19 +1,20 @@
1
1
  import { toString } from 'uint8arrays/to-string';
2
- import { PubsubBaseProtocol } from '@libp2p/pubsub';
2
+ import { PubSubBaseProtocol } from '@libp2p/pubsub';
3
3
  import { multicodec } from './config.js';
4
4
  import { SimpleTimeCache } from './cache.js';
5
- const debugName = 'libp2p:floodsub';
5
+ import { logger } from '@libp2p/logger';
6
+ import { RPC } from './message/rpc.js';
7
+ const log = logger('libp2p:floodsub');
6
8
  export { multicodec };
7
9
  /**
8
10
  * FloodSub (aka dumbsub is an implementation of pubsub focused on
9
11
  * delivering an API for Publish/Subscribe, but with no CastTree Forming
10
12
  * (it just floods the network).
11
13
  */
12
- export class FloodSub extends PubsubBaseProtocol {
13
- constructor(options) {
14
+ export class FloodSub extends PubSubBaseProtocol {
15
+ constructor(init) {
14
16
  super({
15
- ...options,
16
- debugName: debugName,
17
+ ...init,
17
18
  canRelayMessage: true,
18
19
  multicodecs: [multicodec]
19
20
  });
@@ -23,9 +24,27 @@ export class FloodSub extends PubsubBaseProtocol {
23
24
  * @type {TimeCache}
24
25
  */
25
26
  this.seenCache = new SimpleTimeCache({
26
- validityMs: options.seenTTL ?? 30000
27
+ validityMs: init?.seenTTL ?? 30000
27
28
  });
28
29
  }
30
+ /**
31
+ * Decode a Uint8Array into an RPC object
32
+ */
33
+ decodeRpc(bytes) {
34
+ return RPC.decode(bytes);
35
+ }
36
+ /**
37
+ * Encode an RPC object into a Uint8Array
38
+ */
39
+ encodeRpc(rpc) {
40
+ return RPC.encode(rpc).finish();
41
+ }
42
+ decodeMessage(bytes) {
43
+ return RPC.Message.decode(bytes);
44
+ }
45
+ encodeMessage(rpc) {
46
+ return RPC.Message.encode(rpc).finish();
47
+ }
29
48
  /**
30
49
  * Process incoming message
31
50
  * Extends base implementation to check router cache.
@@ -46,19 +65,19 @@ export class FloodSub extends PubsubBaseProtocol {
46
65
  async publishMessage(from, message) {
47
66
  const peers = this.getSubscribers(message.topic);
48
67
  if (peers == null || peers.length === 0) {
49
- this.log('no peers are subscribed to topic %s', message.topic);
68
+ log('no peers are subscribed to topic %s', message.topic);
50
69
  return;
51
70
  }
52
71
  peers.forEach(id => {
53
- if (this.peerId.equals(id)) {
54
- this.log('not sending message on topic %s to myself', message.topic);
72
+ if (this.components.getPeerId().equals(id)) {
73
+ log('not sending message on topic %s to myself', message.topic);
55
74
  return;
56
75
  }
57
76
  if (id.equals(from)) {
58
- this.log('not sending message on topic %s to sender %p', message.topic, id);
77
+ log('not sending message on topic %s to sender %p', message.topic, id);
59
78
  return;
60
79
  }
61
- this.log('publish msgs on topics %s %p', message.topic, id);
80
+ log('publish msgs on topics %s %p', message.topic, id);
62
81
  this.send(id, { messages: [message] });
63
82
  });
64
83
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