Skip to content

Commit f29b73f

Browse files
committed
refactor!: extract UPnP NAT into separate module (#2217)
Splits out UPnP NAT service module. BREAKING CHANGE: imports from `libp2p/upnp-nat` should be updated to `@libp2p/upnp-nat`
1 parent ee2ed58 commit f29b73f

21 files changed

+513
-316
lines changed

.release-please.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"packages/transport-webrtc": {},
3030
"packages/transport-websockets": {},
3131
"packages/transport-webtransport": {},
32+
"packages/upnp-nat": {},
3233
"packages/utils": {}
3334
}
3435
}

doc/CONFIGURATION.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ If your router supports this, libp2p can be configured to use it as follows:
954954

955955
```js
956956
import { createLibp2p } from 'libp2p'
957-
import { uPnPNATService } from 'libp2p/upnp-nat'
957+
import { uPnPNATService } from '@libp2p/upnp-nat'
958958

959959
const node = await createLibp2p({
960960
services: {

doc/migrations/v0.46-v1.0.0.md

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ A migration guide for refactoring your application code from libp2p `v0.46` to `
77

88
- [AutoNAT](#autonat)
99
- [KeyChain](#keychain)
10+
- [UPnPNat](#upnpnat)
1011
- [Pnet](#pnet)
1112
- [Metrics](#metrics)
1213

@@ -69,6 +70,34 @@ const libp2p = await createLibp2p({
6970
const keychain: Keychain = libp2p.services.keychain
7071
```
7172

73+
## UPnPNat
74+
75+
The UPnPNat service module is now published in its own package.
76+
77+
```ts
78+
import { createLibp2p } from 'libp2p'
79+
import { uPnPNATService } from 'libp2p/upnp-nat'
80+
81+
const node = await createLibp2p({
82+
services: {
83+
uPnPNAT: uPnPNATService()
84+
}
85+
})
86+
```
87+
88+
**After**
89+
90+
```ts
91+
import { createLibp2p } from 'libp2p'
92+
import { uPnPNAT } from '@libp2p/upnp-nat'
93+
94+
const node = await createLibp2p({
95+
services: {
96+
uPnPNAT: uPnPNAT()
97+
}
98+
})
99+
```
100+
72101
## Pnet
73102

74103
The pnet module is now published in its own package.

packages/interface/src/errors.ts

+1
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ export class InvalidCryptoTransmissionError extends Error {
6969
// Error codes
7070

7171
export const ERR_TIMEOUT = 'ERR_TIMEOUT'
72+
export const ERR_INVALID_PARAMETERS = 'ERR_INVALID_PARAMETERS'

packages/interface/src/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,21 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
609609
services: T
610610
}
611611

612+
/**
613+
* Metadata about the current node
614+
*/
615+
export interface NodeInfo {
616+
/**
617+
* The implementation name
618+
*/
619+
name: string
620+
621+
/**
622+
* The implementation version
623+
*/
624+
version: string
625+
}
626+
612627
/**
613628
* An object that contains an AbortSignal as
614629
* the optional `signal` property.

packages/libp2p/package.json

-5
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@
7171
"./ping": {
7272
"types": "./dist/src/ping/index.d.ts",
7373
"import": "./dist/src/ping/index.js"
74-
},
75-
"./upnp-nat": {
76-
"types": "./dist/src/upnp-nat/index.d.ts",
77-
"import": "./dist/src/upnp-nat/index.js"
7874
}
7975
},
8076
"eslintConfig": {
@@ -111,7 +107,6 @@
111107
"test:interop": "aegir test -t node -f dist/test/interop.js"
112108
},
113109
"dependencies": {
114-
"@achingbrain/nat-port-mapper": "^1.0.9",
115110
"@libp2p/crypto": "^2.0.8",
116111
"@libp2p/interface": "^0.1.6",
117112
"@libp2p/interface-internal": "^0.1.9",

packages/libp2p/src/components.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CodeError } from '@libp2p/interface/errors'
22
import { isStartable, type Startable } from '@libp2p/interface/startable'
33
import { defaultLogger } from '@libp2p/logger'
4-
import type { Libp2pEvents, ComponentLogger } from '@libp2p/interface'
4+
import type { Libp2pEvents, ComponentLogger, NodeInfo } from '@libp2p/interface'
55
import type { ConnectionProtector } from '@libp2p/interface/connection'
66
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
77
import type { ContentRouting } from '@libp2p/interface/content-routing'
@@ -19,6 +19,7 @@ import type { Datastore } from 'interface-datastore'
1919

2020
export interface Components extends Record<string, any>, Startable {
2121
peerId: PeerId
22+
nodeInfo: NodeInfo
2223
logger: ComponentLogger
2324
events: TypedEventTarget<Libp2pEvents>
2425
addressManager: AddressManager
@@ -37,6 +38,7 @@ export interface Components extends Record<string, any>, Startable {
3738

3839
export interface ComponentsInit {
3940
peerId?: PeerId
41+
nodeInfo?: NodeInfo
4042
logger?: ComponentLogger
4143
events?: TypedEventTarget<Libp2pEvents>
4244
addressManager?: AddressManager

packages/libp2p/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { AddressManagerInit } from './address-manager/index.js'
1919
import type { Components } from './components.js'
2020
import type { ConnectionManagerInit } from './connection-manager/index.js'
2121
import type { TransportManagerInit } from './transport-manager.js'
22-
import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger } from '@libp2p/interface'
22+
import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger, NodeInfo } from '@libp2p/interface'
2323
import type { ConnectionProtector } from '@libp2p/interface/connection'
2424
import type { ConnectionEncrypter } from '@libp2p/interface/connection-encrypter'
2525
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
@@ -46,6 +46,11 @@ export interface Libp2pInit<T extends ServiceMap = { x: Record<string, unknown>
4646
*/
4747
peerId: PeerId
4848

49+
/**
50+
* Metadata about the node - implementation name, version number, etc
51+
*/
52+
nodeInfo: NodeInfo
53+
4954
/**
5055
* Addresses for transport listening and to advertise to the network
5156
*/

packages/libp2p/src/libp2p.ts

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { DefaultPeerRouting } from './peer-routing.js'
2424
import { DefaultRegistrar } from './registrar.js'
2525
import { DefaultTransportManager } from './transport-manager.js'
2626
import { DefaultUpgrader } from './upgrader.js'
27+
import * as pkg from './version.js'
2728
import type { Components } from './components.js'
2829
import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
2930
import type { Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger } from '@libp2p/interface'
@@ -75,6 +76,10 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
7576
this.services = {}
7677
const components = this.components = defaultComponents({
7778
peerId: init.peerId,
79+
nodeInfo: init.nodeInfo ?? {
80+
name: pkg.name,
81+
version: pkg.version
82+
},
7883
logger: this.logger,
7984
events,
8085
datastore: init.datastore ?? new MemoryDatastore(),

0 commit comments

Comments
 (0)