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,15 +1,16 @@
|
||||
'use strict'
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const { Multiaddr } = require('multiaddr')
|
||||
const createLibp2p = require('./libp2p')
|
||||
const { stdinToStream, streamToConsole } = require('./stream')
|
||||
import { Multiaddr } from '@multiformats/multiaddr'
|
||||
import { createLibp2p } from './libp2p.js'
|
||||
import { stdinToStream, streamToConsole } from './stream.js'
|
||||
import { createFromJSON } from '@libp2p/peer-id-factory'
|
||||
import peerIdDialerJson from './peer-id-dialer.js'
|
||||
import peerIdListenerJson from './peer-id-listener.js'
|
||||
|
||||
async function run () {
|
||||
const [idDialer, idListener] = await Promise.all([
|
||||
PeerId.createFromJSON(require('./peer-id-dialer')),
|
||||
PeerId.createFromJSON(require('./peer-id-listener'))
|
||||
createFromJSON(peerIdDialerJson),
|
||||
createFromJSON(peerIdListenerJson)
|
||||
])
|
||||
|
||||
// Create a new libp2p node on localhost with a randomly chosen port
|
||||
@ -25,12 +26,12 @@ async function run () {
|
||||
|
||||
// Output this node's address
|
||||
console.log('Dialer ready, listening on:')
|
||||
nodeDialer.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + idDialer.toB58String())
|
||||
nodeDialer.getMultiaddrs().forEach((ma) => {
|
||||
console.log(ma.toString())
|
||||
})
|
||||
|
||||
// Dial to the remote peer (the "listener")
|
||||
const listenerMa = new Multiaddr(`/ip4/127.0.0.1/tcp/10333/p2p/${idListener.toB58String()}`)
|
||||
const listenerMa = new Multiaddr(`/ip4/127.0.0.1/tcp/10333/p2p/${idListener.toString()}`)
|
||||
const { stream } = await nodeDialer.dialProtocol(listenerMa, '/chat/1.0.0')
|
||||
|
||||
console.log('Dialer dialed to listener on protocol: /chat/1.0.0')
|
||||
|
@ -1,22 +1,23 @@
|
||||
'use strict'
|
||||
import { TCP } from '@libp2p/tcp'
|
||||
import { WebSockets } from '@libp2p/websockets'
|
||||
import { Mplex } from '@libp2p/mplex'
|
||||
import { Noise } from '@chainsafe/libp2p-noise'
|
||||
import defaultsDeep from '@nodeutils/defaults-deep'
|
||||
import { createLibp2p as create } from 'libp2p'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const WS = require('libp2p-websockets')
|
||||
const mplex = require('libp2p-mplex')
|
||||
const { NOISE } = require('@chainsafe/libp2p-noise')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
async function createLibp2p(_options) {
|
||||
export async function createLibp2p(_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [TCP, WS],
|
||||
streamMuxer: [mplex],
|
||||
connEncryption: [NOISE],
|
||||
},
|
||||
transports: [
|
||||
new TCP(),
|
||||
new WebSockets()
|
||||
],
|
||||
streamMuxers: [
|
||||
new Mplex()
|
||||
],
|
||||
connectionEncryption: [
|
||||
new Noise()
|
||||
]
|
||||
}
|
||||
|
||||
return libp2p.create(defaultsDeep(_options, defaults))
|
||||
return create(defaultsDeep(_options, defaults))
|
||||
}
|
||||
|
||||
module.exports = createLibp2p
|
||||
|
@ -1,13 +1,13 @@
|
||||
'use strict'
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const createLibp2p = require('./libp2p.js')
|
||||
const { stdinToStream, streamToConsole } = require('./stream')
|
||||
import { createLibp2p } from './libp2p.js'
|
||||
import { stdinToStream, streamToConsole } from './stream.js'
|
||||
import { createFromJSON } from '@libp2p/peer-id-factory'
|
||||
import peerIdListenerJson from './peer-id-listener.js'
|
||||
|
||||
async function run () {
|
||||
// Create a new libp2p node with the given multi-address
|
||||
const idListener = await PeerId.createFromJSON(require('./peer-id-listener'))
|
||||
const idListener = await createFromJSON(peerIdListenerJson)
|
||||
const nodeListener = await createLibp2p({
|
||||
peerId: idListener,
|
||||
addresses: {
|
||||
@ -16,8 +16,9 @@ async function run () {
|
||||
})
|
||||
|
||||
// Log a message when a remote peer connects to us
|
||||
nodeListener.connectionManager.on('peer:connect', (connection) => {
|
||||
console.log('connected to: ', connection.remotePeer.toB58String())
|
||||
nodeListener.connectionManager.addEventListener('peer:connect', (evt) => {
|
||||
const connection = evt.detail
|
||||
console.log('connected to: ', connection.remotePeer.toString())
|
||||
})
|
||||
|
||||
// Handle messages for the protocol
|
||||
@ -33,8 +34,8 @@ async function run () {
|
||||
|
||||
// Output listen addresses to the console
|
||||
console.log('Listener ready, listening on:')
|
||||
nodeListener.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + idListener.toB58String())
|
||||
nodeListener.getMultiaddrs().forEach((ma) => {
|
||||
console.log(ma.toString())
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
{
|
||||
export default {
|
||||
"id": "Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP",
|
||||
"privKey": "CAASpwkwggSjAgEAAoIBAQCaNSDOjPz6T8HZsf7LDpxiQRiN2OjeyIHUS05p8QWOr3EFUCFsC31R4moihE5HN+FxNalUyyFZU//yjf1pdnlMJqrVByJSMa+y2y4x2FucpoCAO97Tx+iWzwlZ2UXEUXM1Y81mhPbeWXy+wP2xElTgIER0Tsn/thoA0SD2u9wJuVvM7dB7cBcHYmqV6JH+KWCedRTum6O1BssqP/4Lbm2+rkrbZ4+oVRoU2DRLoFhKqwqLtylrbuj4XOI3XykMXV5+uQXz1JzubNOB9lsc6K+eRC+w8hhhDuFMgzkZ4qomCnx3uhO67KaICd8yqqBa6PJ/+fBM5Xk4hjyR40bwcf41AgMBAAECggEAZnrCJ6IYiLyyRdr9SbKXCNDb4YByGYPEi/HT1aHgIJfFE1PSMjxcdytxfyjP4JJpVtPjiT9JFVU2ddoYu5qJN6tGwjVwgJEWg1UXmPaAw1T/drjS94kVsAs82qICtFmwp52Apg3dBZ0Qwq/8qE1XbG7lLyohIbfCBiL0tiPYMfkcsN9gnFT/kFCX0LVs2pa9fHCRMY9rqCc4/rWJa1w8sMuQ23y4lDaxKF9OZVvOHFQkbBDrkquWHE4r55fchCz/rJklkPJUNENuncBRu0/2X+p4IKFD1DnttXNwb8j4LPiSlLro1T0hiUr5gO2QmdYwXFF63Q3mjQy0+5I4eNbjjQK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pH5I/KvdYDW3N4g6zOD2mZNp5y1gHeXINHWzMF596O72/6cxwyiXV1eJ000k1NVnUyrPjXtqWdVLRk5IU1LFpoQoXZU5X1hKj1a2qt/lZfH5eOrF/ramHcwhrYYw1txf8JHXWO/bbNnyemTHAvutZpTNrsWATfAgMBAAE="
|
@ -1,15 +1,19 @@
|
||||
'use strict'
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const pipe = require('it-pipe')
|
||||
const lp = require('it-length-prefixed')
|
||||
import { pipe } from 'it-pipe'
|
||||
import * as lp from 'it-length-prefixed'
|
||||
import map from 'it-map'
|
||||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
||||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
||||
|
||||
function stdinToStream(stream) {
|
||||
export function stdinToStream(stream) {
|
||||
// Read utf-8 from stdin
|
||||
process.stdin.setEncoding('utf8')
|
||||
pipe(
|
||||
// Read from stdin (the source)
|
||||
process.stdin,
|
||||
// Turn strings into buffers
|
||||
(source) => map(source, (string) => uint8ArrayFromString(string)),
|
||||
// Encode with length prefix (so receiving side knows how much data is coming)
|
||||
lp.encode(),
|
||||
// Write to the stream (the sink)
|
||||
@ -17,12 +21,14 @@ function stdinToStream(stream) {
|
||||
)
|
||||
}
|
||||
|
||||
function streamToConsole(stream) {
|
||||
export function streamToConsole(stream) {
|
||||
pipe(
|
||||
// Read from the stream (the source)
|
||||
stream.source,
|
||||
// Decode length-prefixed data
|
||||
lp.decode(),
|
||||
// Turn buffers into strings
|
||||
(source) => map(source, (buf) => uint8ArrayToString(buf)),
|
||||
// Sink function
|
||||
async function (source) {
|
||||
// For each chunk of data
|
||||
@ -33,8 +39,3 @@ function streamToConsole(stream) {
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
stdinToStream,
|
||||
streamToConsole
|
||||
}
|
||||
|
Reference in New Issue
Block a user