Ensure that we don't wait forever for upgrading an inbound connection
to occur.
Note that transports should return an AbortableSource when passed an
AbortSignal so outbound connections to not need the same fix.
Also adds default timeouts for the ping, fetch, and identify protocols.
* feat: limit protocol streams per-connection
Uses the `maxInboundStreams` and `maxOutboundStreams` of the `registrar.handle`
opts to limit the number of concurrent streams open on each connection
on a per-protocol basis.
Both values default to 1 so some tuning will be necessary to set
appropriate values for some protocols.
* chore: make error codes consistent
* chore: fix up examples
Adds a config option to specify a maximum message size we'll accept
for an Identify message.
The default is 8KB, the same as go-libp2p - previously we fell back
to the default `maxMessageLength` option of `it-length-prefixed`
which is 4MB.
Also adds a default timeout for reading responses to identify
requests which is used if an AbortSignal is not passed in.
The default timeout also aligns with go-libp2p.
There are a few places in the codebase where we send/receive data from the network without timeouts/abort controllers which means the user has to wait for the underlying socket to timeout which can take a long time depending on the platform, if at all.
This change ensures we can time out while running identify (both flavours), ping and fetch and adds tests to ensure there are no regressions.
**Motivation**
In lodestar, when we handle "peer:connect" event, we dial the peer which gives another "peer:connect" event and it causes other issues
**Motivation**
In `onConnect` function, "peer:connect" event should be emitted after we add connection to the `connections` map so that when app dial the peer in "peer:connect" event handler, it uses the same/existing connection
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
We have a peerstore that keeps all data for all observed peers in memory with no eviction.
This is fine when you don't discover many peers but when using the DHT you encounter a significant number of peers so our peer storage grows and grows over time.
We have a persistent peer store, but it just periodically writes peers into the datastore to be read at startup, still keeping them in memory.
It also means a restart doesn't give you any temporary reprieve from the memory leak as the previously observed peer data is read into memory at startup.
This change refactors the peerstore to use a datastore by default, reading and writing peer info as it arrives. It can be configured with a MemoryDatastore if desired.
It was necessary to change the peerstore and *book interfaces to be asynchronous since the datastore api is asynchronous.
BREAKING CHANGE: `libp2p.handle`, `libp2p.registrar.register` and the peerstore methods have become async
BREAKING CHANGES:
top level types were updated, multiaddr@9.0.0 is used, dialer and keychain internal property names changed and connectionManager minPeers is not supported anymore
* feat: add uPnP nat manager
Adds a really basic nat manager that attempts to use UPnP to punch
a hole through your router for any IPV4 tcp addresses you have
configured.
Adds any configured addresses to the node's observed addresses list
and adds observed addresses to `libp2p.multiaddrs` so we exchange
them with peers when performing `identify` and people can dial you.
Adds configuration options under `config.nat`
Hole punching is async to not affect start up time.
Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>
* fix: replace node buffers with uint8arrays
Upgrades all deps and replaces all use of node Buffers with Uint8Arrays
BREAKING CHANGES:
- All deps used by this module now use Uint8Arrays in place of node Buffers
* chore: browser fixes
* chore: remove .only
* chore: stringify uint8array before parsing
* chore: update interop suite
* chore: remove ts from build command
* chore: update deps
* fix: update records to use uint8array
* chore: fix lint
* chore: update deps
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
BREAKING CHANGE: all API methods with peer-info parameters or return values were changed. You can check the API.md document, in order to check the new values to use
* feat: address and proto books
* chore: apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
* chore: minor fixes and initial tests added
* chore: integrate new peer-store with code using adapters for other modules
* chore: do not use peerstore.put on get-peer-info
* chore: apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
* chore: add new peer store tests
* chore: apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
* docs: fix spelling in api
* fix: dont create peerstore twice
* feat: add support for dial coalescing
* doc(fix): add setPeerValue to API TOC
* docs: add more jsdocs to dialer
* chore: remove old comment
* fix: ensure connections are closed
* fix: registrar.getConnections returns first open conn
* fix: directly set the closed status
* chore: remove unneeded log
* refactor: peerStore.put takes an options object
* refactor: add dialing over relay support
* chore: fix lint
* fix: dont clear listeners on close
* fix: if dial errors already have codes, just rethrow them
* fix: clear the registrar when libp2p stops
* fix: improve connection maintenance with circuit
* chore: correct feedback
* test: use chai as promised
* test(fix): reset multiaddrs on dial test