Skip to content

Commit

Permalink
refactor!: extract UPnP NAT into separate module (#2217)
Browse files Browse the repository at this point in the history
Splits out UPnP NAT service module.

BREAKING CHANGE: imports from `libp2p/upnp-nat` should be updated to `@libp2p/upnp-nat`
  • Loading branch information
achingbrain committed Nov 15, 2023
1 parent ee2ed58 commit f29b73f
Show file tree
Hide file tree
Showing 21 changed files with 513 additions and 316 deletions.
1 change: 1 addition & 0 deletions .release-please.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"packages/transport-webrtc": {},
"packages/transport-websockets": {},
"packages/transport-webtransport": {},
"packages/upnp-nat": {},
"packages/utils": {}
}
}
2 changes: 1 addition & 1 deletion doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ If your router supports this, libp2p can be configured to use it as follows:

```js
import { createLibp2p } from 'libp2p'
import { uPnPNATService } from 'libp2p/upnp-nat'
import { uPnPNATService } from '@libp2p/upnp-nat'

const node = await createLibp2p({
services: {
Expand Down
29 changes: 29 additions & 0 deletions doc/migrations/v0.46-v1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ A migration guide for refactoring your application code from libp2p `v0.46` to `

- [AutoNAT](#autonat)
- [KeyChain](#keychain)
- [UPnPNat](#upnpnat)
- [Pnet](#pnet)
- [Metrics](#metrics)

Expand Down Expand Up @@ -69,6 +70,34 @@ const libp2p = await createLibp2p({
const keychain: Keychain = libp2p.services.keychain
```

## UPnPNat

The UPnPNat service module is now published in its own package.

```ts
import { createLibp2p } from 'libp2p'
import { uPnPNATService } from 'libp2p/upnp-nat'

const node = await createLibp2p({
services: {
uPnPNAT: uPnPNATService()
}
})
```

**After**

```ts
import { createLibp2p } from 'libp2p'
import { uPnPNAT } from '@libp2p/upnp-nat'

const node = await createLibp2p({
services: {
uPnPNAT: uPnPNAT()
}
})
```

## Pnet

The pnet module is now published in its own package.
Expand Down
1 change: 1 addition & 0 deletions packages/interface/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ export class InvalidCryptoTransmissionError extends Error {
// Error codes

export const ERR_TIMEOUT = 'ERR_TIMEOUT'
export const ERR_INVALID_PARAMETERS = 'ERR_INVALID_PARAMETERS'
15 changes: 15 additions & 0 deletions packages/interface/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,21 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
services: T
}

/**
* Metadata about the current node
*/
export interface NodeInfo {
/**
* The implementation name
*/
name: string

/**
* The implementation version
*/
version: string
}

/**
* An object that contains an AbortSignal as
* the optional `signal` property.
Expand Down
5 changes: 0 additions & 5 deletions packages/libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@
"./ping": {
"types": "./dist/src/ping/index.d.ts",
"import": "./dist/src/ping/index.js"
},
"./upnp-nat": {
"types": "./dist/src/upnp-nat/index.d.ts",
"import": "./dist/src/upnp-nat/index.js"
}
},
"eslintConfig": {
Expand Down Expand Up @@ -111,7 +107,6 @@
"test:interop": "aegir test -t node -f dist/test/interop.js"
},
"dependencies": {
"@achingbrain/nat-port-mapper": "^1.0.9",
"@libp2p/crypto": "^2.0.8",
"@libp2p/interface": "^0.1.6",
"@libp2p/interface-internal": "^0.1.9",
Expand Down
4 changes: 3 additions & 1 deletion packages/libp2p/src/components.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CodeError } from '@libp2p/interface/errors'
import { isStartable, type Startable } from '@libp2p/interface/startable'
import { defaultLogger } from '@libp2p/logger'
import type { Libp2pEvents, ComponentLogger } from '@libp2p/interface'
import type { Libp2pEvents, ComponentLogger, NodeInfo } from '@libp2p/interface'
import type { ConnectionProtector } from '@libp2p/interface/connection'
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
import type { ContentRouting } from '@libp2p/interface/content-routing'
Expand All @@ -19,6 +19,7 @@ import type { Datastore } from 'interface-datastore'

export interface Components extends Record<string, any>, Startable {
peerId: PeerId
nodeInfo: NodeInfo
logger: ComponentLogger
events: TypedEventTarget<Libp2pEvents>
addressManager: AddressManager
Expand All @@ -37,6 +38,7 @@ export interface Components extends Record<string, any>, Startable {

export interface ComponentsInit {
peerId?: PeerId
nodeInfo?: NodeInfo
logger?: ComponentLogger
events?: TypedEventTarget<Libp2pEvents>
addressManager?: AddressManager
Expand Down
7 changes: 6 additions & 1 deletion packages/libp2p/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { AddressManagerInit } from './address-manager/index.js'
import type { Components } from './components.js'
import type { ConnectionManagerInit } from './connection-manager/index.js'
import type { TransportManagerInit } from './transport-manager.js'
import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger } from '@libp2p/interface'
import type { Libp2p, ServiceMap, RecursivePartial, ComponentLogger, NodeInfo } from '@libp2p/interface'
import type { ConnectionProtector } from '@libp2p/interface/connection'
import type { ConnectionEncrypter } from '@libp2p/interface/connection-encrypter'
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
Expand All @@ -46,6 +46,11 @@ export interface Libp2pInit<T extends ServiceMap = { x: Record<string, unknown>
*/
peerId: PeerId

/**
* Metadata about the node - implementation name, version number, etc
*/
nodeInfo: NodeInfo

/**
* Addresses for transport listening and to advertise to the network
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/libp2p/src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { DefaultPeerRouting } from './peer-routing.js'
import { DefaultRegistrar } from './registrar.js'
import { DefaultTransportManager } from './transport-manager.js'
import { DefaultUpgrader } from './upgrader.js'
import * as pkg from './version.js'
import type { Components } from './components.js'
import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
import type { Libp2pEvents, PendingDial, ServiceMap, AbortOptions, ComponentLogger, Logger } from '@libp2p/interface'
Expand Down Expand Up @@ -75,6 +76,10 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
this.services = {}
const components = this.components = defaultComponents({
peerId: init.peerId,
nodeInfo: init.nodeInfo ?? {
name: pkg.name,
version: pkg.version
},
logger: this.logger,
events,
datastore: init.datastore ?? new MemoryDatastore(),
Expand Down
Loading

0 comments on commit f29b73f

Please sign in to comment.