Skip to content

Commit

Permalink
chore: merge 0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Dec 10, 2020
2 parents cba25c8 + a279926 commit 1d3492f
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 35 deletions.
4 changes: 2 additions & 2 deletions doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions examples/pubsub/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`)
})
Expand Down
19 changes: 14 additions & 5 deletions examples/pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ 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) => {
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
})
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)}`)
})
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions examples/pubsub/message-filtering/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions examples/pubsub/message-filtering/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/circuit/circuit/stream-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ const handshake = require('it-handshake')
const { CircuitRelay: CircuitPB } = require('../protocol')

/**
<<<<<<< HEAD
* @typedef {import('../../types').CircuitRequest} CircuitRequest
* @typedef {import('../../types').CircuitMessage} CircuitMessage
=======
>>>>>>> 0.30.x
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
*/

/**
* @template T
*/
class StreamHandler {
/**
* Create a stream handler for connection
Expand Down
44 changes: 21 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<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.
*
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions src/peer-store/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const passthrough = data => data
/**
* @template Data, GetData, EventData
*/

class Book {
/**
* The Book is the skeleton for the PeerStore books.
Expand Down
17 changes: 17 additions & 0 deletions src/peer-store/metadata-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ class MetadataBook extends Book {
}

/**
<<<<<<< HEAD
=======
* Get the known data of a provided peer.
*
* @param {PeerId} peerId
* @returns {Map<string, Uint8Array>|undefined}
*/
get (peerId) {
if (!PeerId.isPeerId(peerId)) {
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
}

return this.data.get(peerId.toB58String())
}

/**
>>>>>>> 0.30.x
* Get specific metadata value, if it exists
*
* @param {PeerId} peerId
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface CircuitMessageProto extends MessageProto<CircuitMessage> {
STOP_DST_MULTIADDR_INVALID: STOP_DST_MULTIADDR_INVALID,
STOP_RELAY_REFUSED: STOP_RELAY_REFUSED,
MALFORMED_MESSAGE: MALFORMED_MESSAGE
}
},
Type: {
HOP: HOP,
STOP: STOP,
Expand Down

0 comments on commit 1d3492f

Please sign in to comment.