diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index e31eb870..ff143ce7 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -445,13 +445,13 @@ const node = await Libp2p.create({ const Libp2p = require('libp2p') const TCP = require('libp2p-tcp') const MPLEX = require('libp2p-mplex') -const SECIO = require('libp2p-secio') +const { NOISE } = require('libp2p-noise') const node = await Libp2p.create({ modules: { transport: [TCP], streamMuxer: [MPLEX], - connEncryption: [SECIO] + connEncryption: [NOISE] }, config: { relay: { // Circuit Relay options (this config is part of libp2p core configurations) diff --git a/examples/pubsub/1.js b/examples/pubsub/1.js index 8c6fdfdb..41983896 100644 --- a/examples/pubsub/1.js +++ b/examples/pubsub/1.js @@ -43,6 +43,7 @@ const createNode = async () => { }) await node1.pubsub.subscribe(topic) + // Will not receive own published messages by default node2.pubsub.on(topic, (msg) => { console.log(`node2 received: ${uint8ArrayToString(msg.data)}`) }) diff --git a/examples/pubsub/README.md b/examples/pubsub/README.md index 17a896f3..2016b2c8 100644 --- a/examples/pubsub/README.md +++ b/examples/pubsub/README.md @@ -44,7 +44,6 @@ const node2 = nodes[1] // Add node's 2 data to the PeerStore node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs) - await node1.dial(node2.peerId) node1.pubsub.on(topic, (msg) => { @@ -52,6 +51,7 @@ node1.pubsub.on(topic, (msg) => { }) await node1.pubsub.subscribe(topic) +// Will not receive own published messages by default node2.pubsub.on(topic, (msg) => { console.log(`node2 received: ${uint8ArrayToString(msg.data)}`) }) @@ -68,25 +68,34 @@ The output of the program should look like: ``` > node 1.js connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 -node2 received: Bird bird bird, bird is the word! node1 received: Bird bird bird, bird is the word! -node2 received: Bird bird bird, bird is the word! node1 received: Bird bird bird, bird is the word! ``` -You can change the pubsub `emitSelf` option if you don't want the publishing node to receive its own messages. +You can change the pubsub `emitSelf` option if you want the publishing node to receive its own messages. ```JavaScript const defaults = { config: { pubsub: { enabled: true, - emitSelf: false + emitSelf: true } } } ``` +The output of the program should look like: + +``` +> node 1.js +connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 +node1 received: Bird bird bird, bird is the word! +node2 received: Bird bird bird, bird is the word! +node1 received: Bird bird bird, bird is the word! +node2 received: Bird bird bird, bird is the word! +``` + ## 2. Future work libp2p/IPFS PubSub is enabling a whole set of Distributed Real Time applications using CRDT (Conflict-Free Replicated Data Types). It is still going through heavy research (and hacking) and we invite you to join the conversation at [research-CRDT](https://github.com/ipfs/research-CRDT). Here is a list of some of the exciting examples: diff --git a/examples/pubsub/message-filtering/1.js b/examples/pubsub/message-filtering/1.js index 85d7bcf8..4d8a2c18 100644 --- a/examples/pubsub/message-filtering/1.js +++ b/examples/pubsub/message-filtering/1.js @@ -44,6 +44,7 @@ const createNode = async () => { //subscribe node1.pubsub.on(topic, (msg) => { + // Will not receive own published messages by default console.log(`node1 received: ${uint8ArrayToString(msg.data)}`) }) await node1.pubsub.subscribe(topic) diff --git a/examples/pubsub/message-filtering/README.md b/examples/pubsub/message-filtering/README.md index a9c0dad2..df990430 100644 --- a/examples/pubsub/message-filtering/README.md +++ b/examples/pubsub/message-filtering/README.md @@ -97,15 +97,12 @@ Result ``` > node 1.js ############## fruit banana ############## -node1 received: banana node2 received: banana node3 received: banana ############## fruit apple ############## -node1 received: apple node2 received: apple node3 received: apple ############## fruit car ############## -node1 received: car ############## fruit orange ############## node1 received: orange node2 received: orange diff --git a/package.json b/package.json index d732966a..a45e0d22 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "it-pipe": "^1.1.0", "it-protocol-buffers": "^0.2.0", "libp2p-crypto": "^0.18.0", - "libp2p-interfaces": "libp2p/js-libp2p-interfaces#feat/add-types-with-post-install", + "libp2p-interfaces": "^0.8.0", "libp2p-utils": "^0.2.2", "mafmt": "^8.0.0", "merge-options": "^2.0.0", diff --git a/src/circuit/circuit/stream-handler.js b/src/circuit/circuit/stream-handler.js index 6c334af7..1610666b 100644 --- a/src/circuit/circuit/stream-handler.js +++ b/src/circuit/circuit/stream-handler.js @@ -15,6 +15,9 @@ const { CircuitRelay: CircuitPB } = require('../protocol') * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream */ +/** + * @template T + */ class StreamHandler { /** * Create a stream handler for connection diff --git a/src/index.js b/src/index.js index c22b022d..1bb3b87f 100644 --- a/src/index.js +++ b/src/index.js @@ -80,11 +80,32 @@ const IDENTIFY_PROTOCOLS = IdentifyService.multicodecs * @property {Libp2pConfig} [config] * @property {PeerId} peerId * + * @typedef {Object} CreateOptions + * @property {PeerId} peerId + * * @extends {EventEmitter} * @fires Libp2p#error Emitted when an error occurs * @fires Libp2p#peer:discovery Emitted when a peer is discovered */ class Libp2p extends EventEmitter { + /** + * Like `new Libp2p(options)` except it will create a `PeerId` + * instance if one is not provided in options. + * + * @param {Libp2pOptions & CreateOptions} options - Libp2p configuration options + * @returns {Promise} + */ + static async create (options) { + if (options.peerId) { + return new Libp2p(options) + } + + const peerId = await PeerId.create() + + options.peerId = peerId + return new Libp2p(options) + } + /** * Libp2p node. * @@ -656,27 +677,4 @@ class Libp2p extends EventEmitter { } } -/** - * @typedef {Object} CreateOptions - * @property {PeerId} peerId - */ - -/** - * Like `new Libp2p(options)` except it will create a `PeerId` - * instance if one is not provided in options. - * - * @param {Libp2pOptions & CreateOptions} options - Libp2p configuration options - * @returns {Promise} - */ -Libp2p.create = async function create (options) { - if (options.peerId) { - return new Libp2p(options) - } - - const peerId = await PeerId.create() - - options.peerId = peerId - return new Libp2p(options) -} - module.exports = Libp2p diff --git a/src/peer-store/book.js b/src/peer-store/book.js index 727e74a8..14218e76 100644 --- a/src/peer-store/book.js +++ b/src/peer-store/book.js @@ -16,6 +16,7 @@ const passthrough = data => data /** * @template Data, GetData, EventData */ + class Book { /** * The Book is the skeleton for the PeerStore books. diff --git a/src/types.ts b/src/types.ts index de140d4b..a60d5260 100644 --- a/src/types.ts +++ b/src/types.ts @@ -80,7 +80,7 @@ export interface CircuitMessageProto extends MessageProto { STOP_DST_MULTIADDR_INVALID: STOP_DST_MULTIADDR_INVALID, STOP_RELAY_REFUSED: STOP_RELAY_REFUSED, MALFORMED_MESSAGE: MALFORMED_MESSAGE - } + }, Type: { HOP: HOP, STOP: STOP,