Skip to content

Commit a35d6fc

Browse files
committed
fix: start listener before AfterStart
Signed-off-by: Sacha Froment <[email protected]>
1 parent 7caee9f commit a35d6fc

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

packages/libp2p/src/transport-manager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ export class DefaultTransportManager implements TransportManager, Startable {
6767
return this.started
6868
}
6969

70-
start (): void {
70+
async start (): Promise<void> {
7171
this.started = true
72-
}
73-
74-
async afterStart (): Promise<void> {
7572
// Listen on the provided transports for the provided addresses
7673
const addrs = this.components.addressManager.getListenAddrs()
7774

7875
await this.listen(addrs)
7976
}
8077

78+
async afterStart (): Promise<void> {
79+
}
80+
8181
/**
8282
* Stops all listeners
8383
*/

packages/transport-websockets/test/node.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import { multiaddr } from '@multiformats/multiaddr'
99
import { WebSockets, WebSocketsSecure } from '@multiformats/multiaddr-matcher'
1010
import { expect } from 'aegir/chai'
1111
import { isLoopbackAddr } from 'is-loopback-addr'
12+
import { createLibp2p } from 'libp2p'
1213
import { pEvent } from 'p-event'
1314
import pWaitFor from 'p-wait-for'
1415
import Sinon from 'sinon'
1516
import { stubInterface } from 'sinon-ts'
1617
import * as filters from '../src/filters.js'
1718
import { webSockets } from '../src/index.js'
18-
import type { Connection, Libp2pEvents, Listener, Transport, Upgrader, TLSCertificate } from '@libp2p/interface'
19+
import type { Connection, Libp2pEvents, Listener, Transport, Upgrader, TLSCertificate, PeerDiscoveryEvents, PeerDiscovery, Startable } from '@libp2p/interface'
20+
import type { AddressManager } from '@libp2p/interface-internal'
1921
import type { StubbedInstance } from 'sinon-ts'
2022

2123
describe('instantiate the transport', () => {
@@ -746,3 +748,45 @@ describe('auto-tls (IPv6)', () => {
746748
expect(wsOptions.port).to.equal(wssOptions.port)
747749
})
748750
})
751+
752+
class TestPeerDiscovery extends TypedEventEmitter<PeerDiscoveryEvents> implements PeerDiscovery, Startable {
753+
private readonly addressManager: AddressManager
754+
755+
constructor (components: {
756+
addressManager: AddressManager
757+
}) {
758+
super()
759+
this.addressManager = components.addressManager
760+
}
761+
762+
async start (): Promise<void> {
763+
}
764+
765+
async stop (): Promise<void> { }
766+
767+
afterStart (): void {
768+
this.addressManager.getAddresses()
769+
}
770+
771+
readonly [Symbol.toStringTag] = '@libp2p/test-peer-discovery'
772+
}
773+
774+
describe('discovery-websockets', () => {
775+
it('should discover peers over websockets', async () => {
776+
const libp2p = await createLibp2p({
777+
addresses: {
778+
listen: [
779+
'/ip4/0.0.0.0/tcp/0/ws'
780+
]
781+
},
782+
peerDiscovery: [(component: {
783+
addressManager: AddressManager
784+
}) => new TestPeerDiscovery(component)],
785+
transports: [
786+
webSockets()
787+
]
788+
})
789+
790+
await libp2p.start()
791+
})
792+
})

0 commit comments

Comments
 (0)