Skip to content

Commit 06e5891

Browse files
committed
fix: add transport manager to exports map and fix docs
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.
1 parent 8cca8e4 commit 06e5891

File tree

9 files changed

+26
-24
lines changed

9 files changed

+26
-24
lines changed

doc/CONFIGURATION.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ import { GossipSub } from 'libp2p-gossipsub'
246246

247247
const node = await createLibp2p({
248248
transports: [
249-
TCP,
250-
new WS() // It can take instances too!
249+
new TCP(),
250+
new WS()
251251
],
252252
streamMuxers: [new Mplex()],
253253
connectionEncryption: [new Noise()],
@@ -697,8 +697,7 @@ import { createLibp2p } from 'libp2p'
697697
import { TCP } from '@libp2p/tcp'
698698
import { Mplex } from '@libp2p/mplex'
699699
import { Noise } from '@chainsafe/libp2p-noise'
700-
701-
const { FaultTolerance } from 'libp2p/src/transport-manager')
700+
import { FaultTolerance } from 'libp2p/transport-manager'
702701

703702
const node = await createLibp2p({
704703
transports: [new TCP()],

doc/STREAMING_ITERABLES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
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).
2323

2424
```js
25-
const duplexPair from 'it-pair/duplex')
25+
import { duplexPair } from 'it-pair/duplex'
2626
import { pipe } from 'it-pipe'
2727

2828
// Wrapper is what we will write and read from

doc/migrations/v0.26-v0.27.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ Protocol registration is very similar to how it previously was, however, the han
4949

5050
**Before**
5151
```js
52-
const pull from 'pull-stream')
52+
const pull = require('pull-stream')
5353
libp2p.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
5454
```
5555

5656
**After**
5757
```js
58-
const pipe from 'it-pipe')
58+
const pipe = require('it-pipe')
5959
libp2p.handle(['/echo/1.0.0'], ({ protocol, stream }) => pipe(stream, stream))
6060
```
6161

@@ -65,7 +65,7 @@ libp2p.handle(['/echo/1.0.0'], ({ protocol, stream }) => pipe(stream, stream))
6565

6666
**Before**
6767
```js
68-
const pull from 'pull-stream')
68+
const pull = require('pull-stream')
6969
libp2p.dialProtocol(peerInfo, '/echo/1.0.0', (err, conn) => {
7070
if (err) { throw err }
7171
pull(
@@ -82,7 +82,7 @@ libp2p.dialProtocol(peerInfo, '/echo/1.0.0', (err, conn) => {
8282

8383
**After**
8484
```js
85-
const pipe from 'it-pipe')
85+
const pipe = require('it-pipe')
8686
const { protocol, stream } = await libp2p.dialProtocol(peerInfo, '/echo/1.0.0')
8787
await pipe(
8888
['hey'],

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
},
6060
"./pnet/generate": {
6161
"import": "./dist/src/pnet/key-generator.js"
62+
},
63+
"./transport-manager": {
64+
"import": "./dist/src/transport-manager.js"
6265
}
6366
},
6467
"eslintConfig": {

src/config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as Constants from './constants.js'
44
import { AGENT_VERSION } from './identify/consts.js'
55
import * as RelayConstants from './circuit/constants.js'
66
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
7-
import { FAULT_TOLERANCE } from './transport-manager.js'
7+
import { FaultTolerance } from './transport-manager.js'
88
import type { Multiaddr } from '@multiformats/multiaddr'
99
import type { Libp2pInit } from './index.js'
1010
import { codes, messages } from './errors.js'
@@ -26,7 +26,7 @@ const DefaultConfig: Partial<Libp2pInit> = {
2626
},
2727
connectionGater: {},
2828
transportManager: {
29-
faultTolerance: FAULT_TOLERANCE.FATAL_ALL
29+
faultTolerance: FaultTolerance.FATAL_ALL
3030
},
3131
dialer: {
3232
maxParallelDials: Constants.MAX_PARALLEL_DIALS,

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLibp2pNode } from './libp2p.js'
22
import type { AbortOptions, EventEmitter, RecursivePartial, Startable } from '@libp2p/interfaces'
33
import type { Multiaddr } from '@multiformats/multiaddr'
4-
import type { FAULT_TOLERANCE } from './transport-manager.js'
4+
import type { FaultTolerance } from './transport-manager.js'
55
import type { HostProperties } from './identify/index.js'
66
import type { DualDHT } from '@libp2p/interfaces/dht'
77
import type { Datastore } from 'interface-datastore'
@@ -95,7 +95,7 @@ export interface ConnectionManagerConfig {
9595
}
9696

9797
export interface TransportManagerConfig {
98-
faultTolerance?: FAULT_TOLERANCE
98+
faultTolerance?: FaultTolerance
9999
}
100100

101101
export interface PeerStoreConfig {

src/transport-manager.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import { trackedMap } from '@libp2p/tracked-map'
1212
const log = logger('libp2p:transports')
1313

1414
export interface TransportManagerInit {
15-
faultTolerance?: FAULT_TOLERANCE
15+
faultTolerance?: FaultTolerance
1616
}
1717

1818
export class DefaultTransportManager extends EventEmitter<TransportManagerEvents> implements TransportManager, Startable {
1919
private readonly components: Components
2020
private readonly transports: Map<string, Transport>
2121
private readonly listeners: Map<string, Listener[]>
22-
private readonly faultTolerance: FAULT_TOLERANCE
22+
private readonly faultTolerance: FaultTolerance
2323
private started: boolean
2424

2525
constructor (components: Components, init: TransportManagerInit = {}) {
@@ -33,7 +33,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
3333
metric: 'listeners',
3434
metrics: this.components.getMetrics()
3535
})
36-
this.faultTolerance = init.faultTolerance ?? FAULT_TOLERANCE.FATAL_ALL
36+
this.faultTolerance = init.faultTolerance ?? FaultTolerance.FATAL_ALL
3737
}
3838

3939
/**
@@ -215,7 +215,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
215215
// listening on remote addresses as they may be offline. We could then potentially
216216
// just wait for any (`p-any`) listener to succeed on each transport before returning
217217
const isListening = results.find(r => r.isFulfilled)
218-
if ((isListening == null) && this.faultTolerance !== FAULT_TOLERANCE.NO_FATAL) {
218+
if ((isListening == null) && this.faultTolerance !== FaultTolerance.NO_FATAL) {
219219
throw errCode(new Error(`Transport (${key}) could not listen on any available address`), codes.ERR_NO_VALID_ADDRESSES)
220220
}
221221
}
@@ -224,7 +224,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
224224
// means we were given addresses we do not have transports for
225225
if (couldNotListen.length === this.transports.size) {
226226
const message = `no valid addresses were provided for transports [${couldNotListen.join(', ')}]`
227-
if (this.faultTolerance === FAULT_TOLERANCE.FATAL_ALL) {
227+
if (this.faultTolerance === FaultTolerance.FATAL_ALL) {
228228
throw errCode(new Error(message), codes.ERR_NO_VALID_ADDRESSES)
229229
}
230230
log(`libp2p in dial mode only: ${message}`)
@@ -266,7 +266,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
266266
/**
267267
* Enum Transport Manager Fault Tolerance values
268268
*/
269-
export enum FAULT_TOLERANCE {
269+
export enum FaultTolerance {
270270
/**
271271
* should be used for failing in any listen circumstance
272272
*/

test/nat-manager/nat-manager.node.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { expect } from 'aegir/utils/chai.js'
44
import { DefaultAddressManager } from '../../src/address-manager/index.js'
5-
import { DefaultTransportManager, FAULT_TOLERANCE } from '../../src/transport-manager.js'
5+
import { DefaultTransportManager, FaultTolerance } from '../../src/transport-manager.js'
66
import { TCP } from '@libp2p/tcp'
77
import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
88
import { NatManager } from '../../src/nat-manager.js'
@@ -30,7 +30,7 @@ describe('Nat Manager (TCP)', () => {
3030
})
3131
components.setAddressManager(new DefaultAddressManager(components, { listen: addrs }))
3232
components.setTransportManager(new DefaultTransportManager(components, {
33-
faultTolerance: FAULT_TOLERANCE.NO_FATAL
33+
faultTolerance: FaultTolerance.NO_FATAL
3434
}))
3535

3636
const natManager = new NatManager(components, {

test/transports/transport-manager.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { WebSockets } from '@libp2p/websockets'
77
import * as filters from '@libp2p/websockets/filters'
88
import { NOISE } from '@chainsafe/libp2p-noise'
99
import { DefaultAddressManager } from '../../src/address-manager/index.js'
10-
import { DefaultTransportManager, FAULT_TOLERANCE } from '../../src/transport-manager.js'
10+
import { DefaultTransportManager, FaultTolerance } from '../../src/transport-manager.js'
1111
import { mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
1212
import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js'
1313
import { codes as ErrorCodes } from '../../src/errors.js'
@@ -123,7 +123,7 @@ describe('libp2p.transportManager (dial only)', () => {
123123
listen: ['/ip4/127.0.0.1/tcp/0']
124124
},
125125
transportManager: {
126-
faultTolerance: FAULT_TOLERANCE.NO_FATAL
126+
faultTolerance: FaultTolerance.NO_FATAL
127127
},
128128
transports: [
129129
new WebSockets()
@@ -143,7 +143,7 @@ describe('libp2p.transportManager (dial only)', () => {
143143
listen: ['/ip4/127.0.0.1/tcp/12345/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit']
144144
},
145145
transportManager: {
146-
faultTolerance: FAULT_TOLERANCE.NO_FATAL
146+
faultTolerance: FaultTolerance.NO_FATAL
147147
},
148148
transports: [
149149
new WebSockets()

0 commit comments

Comments
 (0)