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
2022-03-28 14:30:27 +01:00
|
|
|
import { createLibp2pNode } from './libp2p.js'
|
|
|
|
import type { AbortOptions, EventEmitter, RecursivePartial, Startable } from '@libp2p/interfaces'
|
|
|
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
2022-03-29 15:39:50 +01:00
|
|
|
import type { FaultTolerance } from './transport-manager.js'
|
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
2022-03-28 14:30:27 +01:00
|
|
|
import type { HostProperties } from './identify/index.js'
|
|
|
|
import type { DualDHT } from '@libp2p/interfaces/dht'
|
|
|
|
import type { Datastore } from 'interface-datastore'
|
|
|
|
import type { PeerStore, PeerStoreInit } from '@libp2p/interfaces/peer-store'
|
|
|
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
|
|
import type { AutoRelayConfig, RelayAdvertiseConfig } from './circuit/index.js'
|
|
|
|
import type { PeerDiscovery } from '@libp2p/interfaces/peer-discovery'
|
|
|
|
import type { Connection, ConnectionGater, ConnectionProtector, ProtocolStream } from '@libp2p/interfaces/connection'
|
|
|
|
import type { Transport } from '@libp2p/interfaces/transport'
|
|
|
|
import type { StreamMuxerFactory } from '@libp2p/interfaces/stream-muxer'
|
|
|
|
import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypter'
|
|
|
|
import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
|
|
|
|
import type { ContentRouting } from '@libp2p/interfaces/content-routing'
|
|
|
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
2022-04-02 10:11:01 +01:00
|
|
|
import type { ConnectionManager, Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
|
|
|
|
import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
|
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
2022-03-28 14:30:27 +01:00
|
|
|
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
|
|
import type { DialerInit } from '@libp2p/interfaces/dialer'
|
|
|
|
import type { KeyChain } from './keychain/index.js'
|
|
|
|
|
|
|
|
export interface PersistentPeerStoreOptions {
|
|
|
|
threshold?: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DEKConfig {
|
|
|
|
keyLength: number
|
|
|
|
iterationCount: number
|
|
|
|
salt: string
|
|
|
|
hash: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface KeychainConfig {
|
|
|
|
pass?: string
|
|
|
|
dek?: DEKConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MetricsConfig {
|
|
|
|
enabled?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface HopConfig {
|
|
|
|
enabled?: boolean
|
|
|
|
active?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RelayConfig {
|
|
|
|
enabled: boolean
|
|
|
|
advertise: RelayAdvertiseConfig
|
|
|
|
hop: HopConfig
|
|
|
|
autoRelay: AutoRelayConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface NatManagerConfig {
|
|
|
|
enabled: boolean
|
|
|
|
externalAddress?: string
|
|
|
|
localAddress?: string
|
|
|
|
description?: string
|
|
|
|
ttl?: number
|
|
|
|
keepAlive: boolean
|
|
|
|
gateway?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AddressesConfig {
|
|
|
|
listen: string[]
|
|
|
|
announce: string[]
|
|
|
|
noAnnounce: string[]
|
|
|
|
announceFilter: (multiaddrs: Multiaddr[]) => Multiaddr[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ConnectionManagerConfig {
|
|
|
|
/**
|
|
|
|
* If true, try to connect to all discovered peers up to the connection manager limit
|
|
|
|
*/
|
|
|
|
autoDial?: boolean
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum number of connections to keep open
|
|
|
|
*/
|
|
|
|
maxConnections: number
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The minimum number of connections to keep open
|
|
|
|
*/
|
|
|
|
minConnections: number
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How long to wait between attempting to keep our number of concurrent connections
|
|
|
|
* above minConnections
|
|
|
|
*/
|
|
|
|
autoDialInterval: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TransportManagerConfig {
|
2022-03-29 15:39:50 +01:00
|
|
|
faultTolerance?: FaultTolerance
|
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
2022-03-28 14:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface PeerStoreConfig {
|
|
|
|
persistence?: boolean
|
|
|
|
threshold?: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface PeerRoutingConfig {
|
|
|
|
refreshManager: RefreshManagerConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RefreshManagerConfig {
|
|
|
|
enabled?: boolean
|
|
|
|
interval: number
|
|
|
|
bootDelay: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Libp2pInit {
|
|
|
|
peerId: PeerId
|
|
|
|
host: HostProperties
|
|
|
|
addresses: AddressesConfig
|
|
|
|
connectionManager: ConnectionManagerConfig
|
|
|
|
connectionGater: Partial<ConnectionGater>
|
|
|
|
transportManager: TransportManagerConfig
|
|
|
|
datastore: Datastore
|
|
|
|
dialer: DialerInit
|
|
|
|
metrics: MetricsInit
|
|
|
|
peerStore: PeerStoreInit
|
|
|
|
peerRouting: PeerRoutingConfig
|
|
|
|
keychain: KeychainConfig
|
|
|
|
protocolPrefix: string
|
|
|
|
nat: NatManagerConfig
|
|
|
|
relay: RelayConfig
|
|
|
|
|
|
|
|
transports: Transport[]
|
|
|
|
streamMuxers?: StreamMuxerFactory[]
|
|
|
|
connectionEncryption?: ConnectionEncrypter[]
|
|
|
|
peerDiscovery?: PeerDiscovery[]
|
|
|
|
peerRouters?: PeerRouting[]
|
|
|
|
contentRouters?: ContentRouting[]
|
|
|
|
dht?: DualDHT
|
|
|
|
pubsub?: PubSub
|
|
|
|
connectionProtector?: ConnectionProtector
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Libp2pEvents {
|
|
|
|
'peer:discovery': CustomEvent<PeerInfo>
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
|
|
|
|
peerId: PeerId
|
|
|
|
peerStore: PeerStore
|
|
|
|
peerRouting: PeerRouting
|
|
|
|
contentRouting: ContentRouting
|
|
|
|
keychain: KeyChain
|
|
|
|
connectionManager: ConnectionManager
|
2022-04-02 10:11:01 +01:00
|
|
|
registrar: Registrar
|
|
|
|
metrics?: Metrics
|
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
2022-03-28 14:30:27 +01:00
|
|
|
|
|
|
|
pubsub?: PubSub
|
|
|
|
dht?: DualDHT
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load keychain keys from the datastore.
|
|
|
|
* Imports the private key as 'self', if needed.
|
|
|
|
*/
|
|
|
|
loadKeychain: () => Promise<void>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a deduplicated list of peer advertising multiaddrs by concatenating
|
|
|
|
* the listen addresses used by transports with any configured
|
|
|
|
* announce addresses as well as observed addresses reported by peers.
|
|
|
|
*
|
|
|
|
* If Announce addrs are specified, configured listen addresses will be
|
|
|
|
* ignored though observed addresses will still be included.
|
|
|
|
*/
|
|
|
|
getMultiaddrs: () => Multiaddr[]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of all connections this node has open, optionally filtering
|
|
|
|
* by a PeerId
|
|
|
|
*/
|
|
|
|
getConnections: (peerId?: PeerId) => Connection[]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of all peers we currently have a connection open to
|
|
|
|
*/
|
|
|
|
getPeers: () => PeerId[]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dials to the provided peer. If successful, the known metadata of the
|
|
|
|
* peer will be added to the nodes `peerStore`
|
|
|
|
*/
|
|
|
|
dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise<Connection>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dials to the provided peer and tries to handshake with the given protocols in order.
|
|
|
|
* If successful, the known metadata of the peer will be added to the nodes `peerStore`,
|
|
|
|
* and the `MuxedStream` will be returned together with the successful negotiated protocol.
|
|
|
|
*/
|
|
|
|
dialProtocol: (peer: PeerId | Multiaddr, protocols: string | string[], options?: AbortOptions) => Promise<ProtocolStream>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects all connections to the given `peer`
|
|
|
|
*/
|
|
|
|
hangUp: (peer: PeerId | Multiaddr | string) => Promise<void>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the `handler` for each protocol
|
|
|
|
*/
|
|
|
|
handle: (protocol: string | string[], handler: StreamHandler) => Promise<void>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the handler for each protocol. The protocol
|
|
|
|
* will no longer be supported on streams.
|
|
|
|
*/
|
|
|
|
unhandle: (protocols: string[] | string) => Promise<void>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pings the given peer in order to obtain the operation latency
|
|
|
|
*/
|
|
|
|
ping: (peer: Multiaddr |PeerId) => Promise<number>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a request to fetch the value associated with the given key from the given peer.
|
|
|
|
*/
|
|
|
|
fetch: (peer: PeerId | Multiaddr | string, key: string) => Promise<Uint8Array | null>
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Libp2pOptions = RecursivePartial<Libp2pInit>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new instance of the Libp2p interface, generating a new PeerId
|
|
|
|
* if one is not passed as part of the options.
|
|
|
|
*/
|
|
|
|
export async function createLibp2p (options: Libp2pOptions): Promise<Libp2p> {
|
|
|
|
return await createLibp2pNode(options)
|
|
|
|
}
|