mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 18:42:15 +00:00
fix: add transport manager to exports map and fix docs (#1182)
Addresses PR comments from #1172 - fixes syntax of examples in docs, adds the transport manager to the exports map and renames fault tolerance enum for consistency.
This commit is contained in:
parent
8cca8e4bfc
commit
cc60cfde1a
@ -246,8 +246,8 @@ import { GossipSub } from 'libp2p-gossipsub'
|
|||||||
|
|
||||||
const node = await createLibp2p({
|
const node = await createLibp2p({
|
||||||
transports: [
|
transports: [
|
||||||
TCP,
|
new TCP(),
|
||||||
new WS() // It can take instances too!
|
new WS()
|
||||||
],
|
],
|
||||||
streamMuxers: [new Mplex()],
|
streamMuxers: [new Mplex()],
|
||||||
connectionEncryption: [new Noise()],
|
connectionEncryption: [new Noise()],
|
||||||
@ -697,8 +697,7 @@ import { createLibp2p } from 'libp2p'
|
|||||||
import { TCP } from '@libp2p/tcp'
|
import { TCP } from '@libp2p/tcp'
|
||||||
import { Mplex } from '@libp2p/mplex'
|
import { Mplex } from '@libp2p/mplex'
|
||||||
import { Noise } from '@chainsafe/libp2p-noise'
|
import { Noise } from '@chainsafe/libp2p-noise'
|
||||||
|
import { FaultTolerance } from 'libp2p/transport-manager'
|
||||||
const { FaultTolerance } from 'libp2p/src/transport-manager')
|
|
||||||
|
|
||||||
const node = await createLibp2p({
|
const node = await createLibp2p({
|
||||||
transports: [new TCP()],
|
transports: [new TCP()],
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
Sometimes you may need to wrap an existing duplex stream in order to perform incoming and outgoing [transforms](#transform) on data. This type of wrapping is commonly used in stream encryption/decryption. Using [it-pair][it-pair] and [it-pipe][it-pipe], we can do this rather easily, given an existing [duplex iterable](#duplex).
|
Sometimes you may need to wrap an existing duplex stream in order to perform incoming and outgoing [transforms](#transform) on data. This type of wrapping is commonly used in stream encryption/decryption. Using [it-pair][it-pair] and [it-pipe][it-pipe], we can do this rather easily, given an existing [duplex iterable](#duplex).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const duplexPair from 'it-pair/duplex')
|
import { duplexPair } from 'it-pair/duplex'
|
||||||
import { pipe } from 'it-pipe'
|
import { pipe } from 'it-pipe'
|
||||||
|
|
||||||
// Wrapper is what we will write and read from
|
// Wrapper is what we will write and read from
|
||||||
|
@ -49,13 +49,13 @@ Protocol registration is very similar to how it previously was, however, the han
|
|||||||
|
|
||||||
**Before**
|
**Before**
|
||||||
```js
|
```js
|
||||||
const pull from 'pull-stream')
|
const pull = require('pull-stream')
|
||||||
libp2p.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
libp2p.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||||
```
|
```
|
||||||
|
|
||||||
**After**
|
**After**
|
||||||
```js
|
```js
|
||||||
const pipe from 'it-pipe')
|
const pipe = require('it-pipe')
|
||||||
libp2p.handle(['/echo/1.0.0'], ({ protocol, stream }) => pipe(stream, stream))
|
libp2p.handle(['/echo/1.0.0'], ({ protocol, stream }) => pipe(stream, stream))
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ libp2p.handle(['/echo/1.0.0'], ({ protocol, stream }) => pipe(stream, stream))
|
|||||||
|
|
||||||
**Before**
|
**Before**
|
||||||
```js
|
```js
|
||||||
const pull from 'pull-stream')
|
const pull = require('pull-stream')
|
||||||
libp2p.dialProtocol(peerInfo, '/echo/1.0.0', (err, conn) => {
|
libp2p.dialProtocol(peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(
|
pull(
|
||||||
@ -82,7 +82,7 @@ libp2p.dialProtocol(peerInfo, '/echo/1.0.0', (err, conn) => {
|
|||||||
|
|
||||||
**After**
|
**After**
|
||||||
```js
|
```js
|
||||||
const pipe from 'it-pipe')
|
const pipe = require('it-pipe')
|
||||||
const { protocol, stream } = await libp2p.dialProtocol(peerInfo, '/echo/1.0.0')
|
const { protocol, stream } = await libp2p.dialProtocol(peerInfo, '/echo/1.0.0')
|
||||||
await pipe(
|
await pipe(
|
||||||
['hey'],
|
['hey'],
|
||||||
|
@ -59,6 +59,9 @@
|
|||||||
},
|
},
|
||||||
"./pnet/generate": {
|
"./pnet/generate": {
|
||||||
"import": "./dist/src/pnet/key-generator.js"
|
"import": "./dist/src/pnet/key-generator.js"
|
||||||
|
},
|
||||||
|
"./transport-manager": {
|
||||||
|
"import": "./dist/src/transport-manager.js"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
|
@ -4,7 +4,7 @@ import * as Constants from './constants.js'
|
|||||||
import { AGENT_VERSION } from './identify/consts.js'
|
import { AGENT_VERSION } from './identify/consts.js'
|
||||||
import * as RelayConstants from './circuit/constants.js'
|
import * as RelayConstants from './circuit/constants.js'
|
||||||
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
||||||
import { FAULT_TOLERANCE } from './transport-manager.js'
|
import { FaultTolerance } from './transport-manager.js'
|
||||||
import type { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
import type { Libp2pInit } from './index.js'
|
import type { Libp2pInit } from './index.js'
|
||||||
import { codes, messages } from './errors.js'
|
import { codes, messages } from './errors.js'
|
||||||
@ -26,7 +26,7 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|||||||
},
|
},
|
||||||
connectionGater: {},
|
connectionGater: {},
|
||||||
transportManager: {
|
transportManager: {
|
||||||
faultTolerance: FAULT_TOLERANCE.FATAL_ALL
|
faultTolerance: FaultTolerance.FATAL_ALL
|
||||||
},
|
},
|
||||||
dialer: {
|
dialer: {
|
||||||
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { createLibp2pNode } from './libp2p.js'
|
import { createLibp2pNode } from './libp2p.js'
|
||||||
import type { AbortOptions, EventEmitter, RecursivePartial, Startable } from '@libp2p/interfaces'
|
import type { AbortOptions, EventEmitter, RecursivePartial, Startable } from '@libp2p/interfaces'
|
||||||
import type { Multiaddr } from '@multiformats/multiaddr'
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
||||||
import type { FAULT_TOLERANCE } from './transport-manager.js'
|
import type { FaultTolerance } from './transport-manager.js'
|
||||||
import type { HostProperties } from './identify/index.js'
|
import type { HostProperties } from './identify/index.js'
|
||||||
import type { DualDHT } from '@libp2p/interfaces/dht'
|
import type { DualDHT } from '@libp2p/interfaces/dht'
|
||||||
import type { Datastore } from 'interface-datastore'
|
import type { Datastore } from 'interface-datastore'
|
||||||
@ -95,7 +95,7 @@ export interface ConnectionManagerConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface TransportManagerConfig {
|
export interface TransportManagerConfig {
|
||||||
faultTolerance?: FAULT_TOLERANCE
|
faultTolerance?: FaultTolerance
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PeerStoreConfig {
|
export interface PeerStoreConfig {
|
||||||
|
@ -12,14 +12,14 @@ import { trackedMap } from '@libp2p/tracked-map'
|
|||||||
const log = logger('libp2p:transports')
|
const log = logger('libp2p:transports')
|
||||||
|
|
||||||
export interface TransportManagerInit {
|
export interface TransportManagerInit {
|
||||||
faultTolerance?: FAULT_TOLERANCE
|
faultTolerance?: FaultTolerance
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DefaultTransportManager extends EventEmitter<TransportManagerEvents> implements TransportManager, Startable {
|
export class DefaultTransportManager extends EventEmitter<TransportManagerEvents> implements TransportManager, Startable {
|
||||||
private readonly components: Components
|
private readonly components: Components
|
||||||
private readonly transports: Map<string, Transport>
|
private readonly transports: Map<string, Transport>
|
||||||
private readonly listeners: Map<string, Listener[]>
|
private readonly listeners: Map<string, Listener[]>
|
||||||
private readonly faultTolerance: FAULT_TOLERANCE
|
private readonly faultTolerance: FaultTolerance
|
||||||
private started: boolean
|
private started: boolean
|
||||||
|
|
||||||
constructor (components: Components, init: TransportManagerInit = {}) {
|
constructor (components: Components, init: TransportManagerInit = {}) {
|
||||||
@ -33,7 +33,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
|
|||||||
metric: 'listeners',
|
metric: 'listeners',
|
||||||
metrics: this.components.getMetrics()
|
metrics: this.components.getMetrics()
|
||||||
})
|
})
|
||||||
this.faultTolerance = init.faultTolerance ?? FAULT_TOLERANCE.FATAL_ALL
|
this.faultTolerance = init.faultTolerance ?? FaultTolerance.FATAL_ALL
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -215,7 +215,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
|
|||||||
// listening on remote addresses as they may be offline. We could then potentially
|
// listening on remote addresses as they may be offline. We could then potentially
|
||||||
// just wait for any (`p-any`) listener to succeed on each transport before returning
|
// just wait for any (`p-any`) listener to succeed on each transport before returning
|
||||||
const isListening = results.find(r => r.isFulfilled)
|
const isListening = results.find(r => r.isFulfilled)
|
||||||
if ((isListening == null) && this.faultTolerance !== FAULT_TOLERANCE.NO_FATAL) {
|
if ((isListening == null) && this.faultTolerance !== FaultTolerance.NO_FATAL) {
|
||||||
throw errCode(new Error(`Transport (${key}) could not listen on any available address`), codes.ERR_NO_VALID_ADDRESSES)
|
throw errCode(new Error(`Transport (${key}) could not listen on any available address`), codes.ERR_NO_VALID_ADDRESSES)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -224,7 +224,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
|
|||||||
// means we were given addresses we do not have transports for
|
// means we were given addresses we do not have transports for
|
||||||
if (couldNotListen.length === this.transports.size) {
|
if (couldNotListen.length === this.transports.size) {
|
||||||
const message = `no valid addresses were provided for transports [${couldNotListen.join(', ')}]`
|
const message = `no valid addresses were provided for transports [${couldNotListen.join(', ')}]`
|
||||||
if (this.faultTolerance === FAULT_TOLERANCE.FATAL_ALL) {
|
if (this.faultTolerance === FaultTolerance.FATAL_ALL) {
|
||||||
throw errCode(new Error(message), codes.ERR_NO_VALID_ADDRESSES)
|
throw errCode(new Error(message), codes.ERR_NO_VALID_ADDRESSES)
|
||||||
}
|
}
|
||||||
log(`libp2p in dial mode only: ${message}`)
|
log(`libp2p in dial mode only: ${message}`)
|
||||||
@ -266,7 +266,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
|
|||||||
/**
|
/**
|
||||||
* Enum Transport Manager Fault Tolerance values
|
* Enum Transport Manager Fault Tolerance values
|
||||||
*/
|
*/
|
||||||
export enum FAULT_TOLERANCE {
|
export enum FaultTolerance {
|
||||||
/**
|
/**
|
||||||
* should be used for failing in any listen circumstance
|
* should be used for failing in any listen circumstance
|
||||||
*/
|
*/
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { expect } from 'aegir/utils/chai.js'
|
import { expect } from 'aegir/utils/chai.js'
|
||||||
import { DefaultAddressManager } from '../../src/address-manager/index.js'
|
import { DefaultAddressManager } from '../../src/address-manager/index.js'
|
||||||
import { DefaultTransportManager, FAULT_TOLERANCE } from '../../src/transport-manager.js'
|
import { DefaultTransportManager, FaultTolerance } from '../../src/transport-manager.js'
|
||||||
import { TCP } from '@libp2p/tcp'
|
import { TCP } from '@libp2p/tcp'
|
||||||
import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
|
import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
|
||||||
import { NatManager } from '../../src/nat-manager.js'
|
import { NatManager } from '../../src/nat-manager.js'
|
||||||
@ -30,7 +30,7 @@ describe('Nat Manager (TCP)', () => {
|
|||||||
})
|
})
|
||||||
components.setAddressManager(new DefaultAddressManager(components, { listen: addrs }))
|
components.setAddressManager(new DefaultAddressManager(components, { listen: addrs }))
|
||||||
components.setTransportManager(new DefaultTransportManager(components, {
|
components.setTransportManager(new DefaultTransportManager(components, {
|
||||||
faultTolerance: FAULT_TOLERANCE.NO_FATAL
|
faultTolerance: FaultTolerance.NO_FATAL
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const natManager = new NatManager(components, {
|
const natManager = new NatManager(components, {
|
||||||
|
@ -7,7 +7,7 @@ import { WebSockets } from '@libp2p/websockets'
|
|||||||
import * as filters from '@libp2p/websockets/filters'
|
import * as filters from '@libp2p/websockets/filters'
|
||||||
import { NOISE } from '@chainsafe/libp2p-noise'
|
import { NOISE } from '@chainsafe/libp2p-noise'
|
||||||
import { DefaultAddressManager } from '../../src/address-manager/index.js'
|
import { DefaultAddressManager } from '../../src/address-manager/index.js'
|
||||||
import { DefaultTransportManager, FAULT_TOLERANCE } from '../../src/transport-manager.js'
|
import { DefaultTransportManager, FaultTolerance } from '../../src/transport-manager.js'
|
||||||
import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
|
import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
|
||||||
import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js'
|
import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js'
|
||||||
import { codes as ErrorCodes } from '../../src/errors.js'
|
import { codes as ErrorCodes } from '../../src/errors.js'
|
||||||
@ -123,7 +123,7 @@ describe('libp2p.transportManager (dial only)', () => {
|
|||||||
listen: ['/ip4/127.0.0.1/tcp/0']
|
listen: ['/ip4/127.0.0.1/tcp/0']
|
||||||
},
|
},
|
||||||
transportManager: {
|
transportManager: {
|
||||||
faultTolerance: FAULT_TOLERANCE.NO_FATAL
|
faultTolerance: FaultTolerance.NO_FATAL
|
||||||
},
|
},
|
||||||
transports: [
|
transports: [
|
||||||
new WebSockets()
|
new WebSockets()
|
||||||
@ -143,7 +143,7 @@ describe('libp2p.transportManager (dial only)', () => {
|
|||||||
listen: ['/ip4/127.0.0.1/tcp/12345/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit']
|
listen: ['/ip4/127.0.0.1/tcp/12345/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit']
|
||||||
},
|
},
|
||||||
transportManager: {
|
transportManager: {
|
||||||
faultTolerance: FAULT_TOLERANCE.NO_FATAL
|
faultTolerance: FaultTolerance.NO_FATAL
|
||||||
},
|
},
|
||||||
transports: [
|
transports: [
|
||||||
new WebSockets()
|
new WebSockets()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user