chore: merge 0.30

This commit is contained in:
Vasco Santos 2020-12-10 14:59:55 +01:00
commit 2e7f49ebbd
10 changed files with 45 additions and 35 deletions

View File

@ -445,13 +445,13 @@ const node = await Libp2p.create({
const Libp2p = require('libp2p') const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp') const TCP = require('libp2p-tcp')
const MPLEX = require('libp2p-mplex') const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio') const { NOISE } = require('libp2p-noise')
const node = await Libp2p.create({ const node = await Libp2p.create({
modules: { modules: {
transport: [TCP], transport: [TCP],
streamMuxer: [MPLEX], streamMuxer: [MPLEX],
connEncryption: [SECIO] connEncryption: [NOISE]
}, },
config: { config: {
relay: { // Circuit Relay options (this config is part of libp2p core configurations) relay: { // Circuit Relay options (this config is part of libp2p core configurations)

View File

@ -43,6 +43,7 @@ const createNode = async () => {
}) })
await node1.pubsub.subscribe(topic) await node1.pubsub.subscribe(topic)
// Will not receive own published messages by default
node2.pubsub.on(topic, (msg) => { node2.pubsub.on(topic, (msg) => {
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`) console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
}) })

View File

@ -44,7 +44,6 @@ const node2 = nodes[1]
// Add node's 2 data to the PeerStore // Add node's 2 data to the PeerStore
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs) node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
await node1.dial(node2.peerId) await node1.dial(node2.peerId)
node1.pubsub.on(topic, (msg) => { node1.pubsub.on(topic, (msg) => {
@ -52,6 +51,7 @@ node1.pubsub.on(topic, (msg) => {
}) })
await node1.pubsub.subscribe(topic) await node1.pubsub.subscribe(topic)
// Will not receive own published messages by default
node2.pubsub.on(topic, (msg) => { node2.pubsub.on(topic, (msg) => {
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`) console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
}) })
@ -68,25 +68,34 @@ The output of the program should look like:
``` ```
> node 1.js > node 1.js
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
node2 received: Bird bird bird, bird is the word!
node1 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! 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 ```JavaScript
const defaults = { const defaults = {
config: { config: {
pubsub: { pubsub: {
enabled: true, 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 ## 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: 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:

View File

@ -44,6 +44,7 @@ const createNode = async () => {
//subscribe //subscribe
node1.pubsub.on(topic, (msg) => { node1.pubsub.on(topic, (msg) => {
// Will not receive own published messages by default
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`) console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
}) })
await node1.pubsub.subscribe(topic) await node1.pubsub.subscribe(topic)

View File

@ -97,15 +97,12 @@ Result
``` ```
> node 1.js > node 1.js
############## fruit banana ############## ############## fruit banana ##############
node1 received: banana
node2 received: banana node2 received: banana
node3 received: banana node3 received: banana
############## fruit apple ############## ############## fruit apple ##############
node1 received: apple
node2 received: apple node2 received: apple
node3 received: apple node3 received: apple
############## fruit car ############## ############## fruit car ##############
node1 received: car
############## fruit orange ############## ############## fruit orange ##############
node1 received: orange node1 received: orange
node2 received: orange node2 received: orange

View File

@ -70,7 +70,7 @@
"it-pipe": "^1.1.0", "it-pipe": "^1.1.0",
"it-protocol-buffers": "^0.2.0", "it-protocol-buffers": "^0.2.0",
"libp2p-crypto": "^0.18.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", "libp2p-utils": "^0.2.2",
"mafmt": "^8.0.0", "mafmt": "^8.0.0",
"merge-options": "^2.0.0", "merge-options": "^2.0.0",

View File

@ -15,6 +15,9 @@ const { CircuitRelay: CircuitPB } = require('../protocol')
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
*/ */
/**
* @template T
*/
class StreamHandler { class StreamHandler {
/** /**
* Create a stream handler for connection * Create a stream handler for connection

View File

@ -80,11 +80,32 @@ const IDENTIFY_PROTOCOLS = IdentifyService.multicodecs
* @property {Libp2pConfig} [config] * @property {Libp2pConfig} [config]
* @property {PeerId} peerId * @property {PeerId} peerId
* *
* @typedef {Object} CreateOptions
* @property {PeerId} peerId
*
* @extends {EventEmitter} * @extends {EventEmitter}
* @fires Libp2p#error Emitted when an error occurs * @fires Libp2p#error Emitted when an error occurs
* @fires Libp2p#peer:discovery Emitted when a peer is discovered * @fires Libp2p#peer:discovery Emitted when a peer is discovered
*/ */
class Libp2p extends EventEmitter { 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<Libp2p>}
*/
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. * 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>}
*/
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 module.exports = Libp2p

View File

@ -16,6 +16,7 @@ const passthrough = data => data
/** /**
* @template Data, GetData, EventData * @template Data, GetData, EventData
*/ */
class Book { class Book {
/** /**
* The Book is the skeleton for the PeerStore books. * The Book is the skeleton for the PeerStore books.

View File

@ -80,7 +80,7 @@ export interface CircuitMessageProto extends MessageProto<CircuitMessage> {
STOP_DST_MULTIADDR_INVALID: STOP_DST_MULTIADDR_INVALID, STOP_DST_MULTIADDR_INVALID: STOP_DST_MULTIADDR_INVALID,
STOP_RELAY_REFUSED: STOP_RELAY_REFUSED, STOP_RELAY_REFUSED: STOP_RELAY_REFUSED,
MALFORMED_MESSAGE: MALFORMED_MESSAGE MALFORMED_MESSAGE: MALFORMED_MESSAGE
} },
Type: { Type: {
HOP: HOP, HOP: HOP,
STOP: STOP, STOP: STOP,