mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-23 05:51:37 +00:00
feat: convert to typescript (#1172)
Converts this module to typescript. - Ecosystem modules renamed from (e.g.) `libp2p-tcp` to `@libp2p/tcp` - Ecosystem module now have named exports - Configuration has been updated, now pass instances of modules instead of classes: - Some configuration keys have been renamed to make them more descriptive. `transport` -> `transports`, `connEncryption` -> `connectionEncryption`. In general where we pass multiple things, the key is now plural, e.g. `streamMuxer` -> `streamMuxers`, `contentRouting` -> `contentRouters`, etc. Where we are configuring a singleton the config key is singular, e.g. `connProtector` -> `connectionProtector` etc. - Properties of the `modules` config key have been moved to the root - Properties of the `config` config key have been moved to the root ```js // before import Libp2p from 'libp2p' import TCP from 'libp2p-tcp' await Libp2p.create({ modules: { transport: [ TCP ], } config: { transport: { [TCP.tag]: { foo: 'bar' } }, relay: { enabled: true, hop: { enabled: true, active: true } } } }) ``` ```js // after import { createLibp2p } from 'libp2p' import { TCP } from '@libp2p/tcp' await createLibp2p({ transports: [ new TCP({ foo: 'bar' }) ], relay: { enabled: true, hop: { enabled: true, active: true } } }) ``` - Use of `enabled` flag has been reduced - previously you could pass a module but disable it with config. Now if you don't want a feature, just don't pass an implementation. Eg: ```js // before await Libp2p.create({ modules: { transport: [ TCP ], pubsub: Gossipsub }, config: { pubsub: { enabled: false } } }) ``` ```js // after await createLibp2p({ transports: [ new TCP() ] }) ``` - `.multiaddrs` renamed to `.getMultiaddrs()` because it's not a property accessor, work is done by that method to calculate announce addresses, observed addresses, etc - `/p2p/${peerId}` is now appended to all addresses returned by `.getMultiaddrs()` so they can be used opaquely (every consumer has to append the peer ID to the address to actually use it otherwise). If you need low-level unadulterated addresses, call methods on the address manager. BREAKING CHANGE: types are no longer hand crafted, this module is now ESM only
This commit is contained in:
@ -1,25 +1,23 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const Libp2p = require('../../../')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('@chainsafe/libp2p-noise')
|
||||
const Gossipsub = require('@achingbrain/libp2p-gossipsub')
|
||||
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
|
||||
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
|
||||
import { createLibp2p } from 'libp2p'
|
||||
import { TCP } from '@libp2p/tcp'
|
||||
import { Mplex } from '@libp2p/mplex'
|
||||
import { Noise } from '@chainsafe/libp2p-noise'
|
||||
import { Gossipsub } from '@achingbrain/libp2p-gossipsub'
|
||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
||||
import { CustomEvent } from '@libp2p/interfaces'
|
||||
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
const node = await createLibp2p({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Mplex],
|
||||
connEncryption: [NOISE],
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
transports: [new TCP()],
|
||||
streamMuxers: [new Mplex()],
|
||||
connectionEncryption: [new Noise()],
|
||||
pubsub: new Gossipsub()
|
||||
})
|
||||
|
||||
await node.start()
|
||||
@ -36,28 +34,26 @@ const createNode = async () => {
|
||||
])
|
||||
|
||||
// node1 conect to node2 and node2 conect to node3
|
||||
await node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
await node1.peerStore.addressBook.set(node2.peerId, node2.getMultiaddrs())
|
||||
await node1.dial(node2.peerId)
|
||||
|
||||
await node2.peerStore.addressBook.set(node3.peerId, node3.multiaddrs)
|
||||
await node2.peerStore.addressBook.set(node3.peerId, node3.getMultiaddrs())
|
||||
await node2.dial(node3.peerId)
|
||||
|
||||
//subscribe
|
||||
node1.pubsub.on(topic, (msg) => {
|
||||
node1.pubsub.addEventListener(topic, (evt) => {
|
||||
// Will not receive own published messages by default
|
||||
console.log(`node1 received: ${uint8ArrayToString(msg.data)}`)
|
||||
console.log(`node1 received: ${uint8ArrayToString(evt.detail.data)}`)
|
||||
})
|
||||
await node1.pubsub.subscribe(topic)
|
||||
|
||||
node2.pubsub.on(topic, (msg) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(msg.data)}`)
|
||||
node2.pubsub.addEventListener(topic, (evt) => {
|
||||
console.log(`node2 received: ${uint8ArrayToString(evt.detail.data)}`)
|
||||
})
|
||||
await node2.pubsub.subscribe(topic)
|
||||
|
||||
node3.pubsub.on(topic, (msg) => {
|
||||
console.log(`node3 received: ${uint8ArrayToString(msg.data)}`)
|
||||
node3.pubsub.addEventListener(topic, (evt) => {
|
||||
console.log(`node3 received: ${uint8ArrayToString(evt.detail.data)}`)
|
||||
})
|
||||
await node3.pubsub.subscribe(topic)
|
||||
|
||||
const validateFruit = (msgTopic, msg) => {
|
||||
const fruit = uint8ArrayToString(msg.data)
|
||||
@ -79,7 +75,7 @@ const createNode = async () => {
|
||||
// car is not a fruit !
|
||||
setInterval(() => {
|
||||
console.log('############## fruit ' + myFruits[count] + ' ##############')
|
||||
node1.pubsub.publish(topic, uint8ArrayFromString(myFruits[count]))
|
||||
node1.pubsub.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString(myFruits[count]) }))
|
||||
count++
|
||||
if (count == myFruits.length) {
|
||||
count = 0
|
||||
|
@ -7,19 +7,23 @@ To prevent undesired data from being propagated on the network, we can apply a f
|
||||
First, let's update our libp2p configuration with a pubsub implementation.
|
||||
|
||||
```JavaScript
|
||||
const Libp2p = require('libp2p')
|
||||
const Gossipsub = require('libp2p-gossipsub')
|
||||
import { createLibp2p } from 'libp2p'
|
||||
import { Gossipsub } from 'libp2p-gossipsub'
|
||||
|
||||
const node = await Libp2p.create({
|
||||
const node = await createLibp2p({
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [ TCP ],
|
||||
streamMuxer: [ Mplex ],
|
||||
connEncryption: [ NOISE ],
|
||||
pubsub: Gossipsub
|
||||
}
|
||||
transports: [
|
||||
new TCP()
|
||||
],
|
||||
streamMuxers: [
|
||||
new Mplex()
|
||||
],
|
||||
connectionEncryption: [
|
||||
new Noise()
|
||||
],
|
||||
pubsub: new Gossipsub()
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
'use strict'
|
||||
import path from 'path'
|
||||
import execa from 'execa'
|
||||
import pDefer from 'p-defer'
|
||||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const path = require('path')
|
||||
const execa = require('execa')
|
||||
const pDefer = require('p-defer')
|
||||
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
|
||||
const stdout = [
|
||||
{
|
||||
@ -24,7 +25,7 @@ const stdout = [
|
||||
},
|
||||
]
|
||||
|
||||
async function test () {
|
||||
export async function test () {
|
||||
const defer = pDefer()
|
||||
let topicCount = 0
|
||||
let topicMessageCount = 0
|
||||
@ -63,5 +64,3 @@ async function test () {
|
||||
await defer.promise
|
||||
proc.kill()
|
||||
}
|
||||
|
||||
module.exports = test
|
||||
|
Reference in New Issue
Block a user