Skip to content

Commit c8b71d8

Browse files
committed
refactor!: remove isStarted method from Startable (#2145)
We have an `isStarted` method on the `Startable` interface but we only really use it in tests. Our implementations tend to guard on being started twice so it just adds noise to every implementation. BREAKING CHANGE: the `isStarted` method has been removed from the `Startable` interface
1 parent d56fa0e commit c8b71d8

File tree

9 files changed

+169
-245
lines changed

9 files changed

+169
-245
lines changed

packages/interface-compliance-tests/src/pubsub/api.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export default (common: TestSetup<PubSub, PubSubArgs>): void => {
4848

4949
await start(...Object.values(components))
5050

51-
expect(pubsub.isStarted()).to.equal(true)
5251
expect(components.registrar.register).to.have.property('callCount', 1)
5352
})
5453

@@ -62,7 +61,6 @@ export default (common: TestSetup<PubSub, PubSubArgs>): void => {
6261
await start(...Object.values(components))
6362
await stop(...Object.values(components))
6463

65-
expect(pubsub.isStarted()).to.equal(false)
6664
expect(components.registrar.unregister).to.have.property('callCount', 1)
6765
})
6866

packages/interface/src/startable.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* Implemented by components that have a lifecycle
33
*/
44
export interface Startable {
5-
isStarted(): boolean
6-
75
/**
86
* If implemented, this method will be invoked before the start method.
97
*

packages/libp2p/test/core/start.spec.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/libp2p/test/identify/service.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ describe('identify', () => {
153153

154154
// Wait for identify to finish
155155
await identityServiceIdentifySpy.firstCall.returnValue
156-
sinon.stub(libp2p, 'isStarted').returns(true)
157156

158157
// Cause supported protocols to change
159158
await libp2p.handle('/echo/2.0.0', () => {})
@@ -251,7 +250,6 @@ describe('identify', () => {
251250

252251
// Wait for identify to finish
253252
await identityServiceIdentifySpy.firstCall.returnValue
254-
sinon.stub(libp2p, 'isStarted').returns(true)
255253

256254
await libp2p.peerStore.merge(libp2p.peerId, {
257255
multiaddrs: [multiaddr('/ip4/180.0.0.1/tcp/15001/ws')]

packages/libp2p/test/transports/transport-manager.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ describe('libp2p.transportManager (dial only)', () => {
124124
start: false
125125
})
126126

127-
expect(libp2p.isStarted()).to.be.false()
128127
await expect(libp2p.start()).to.eventually.be.rejected
129128
.with.property('code', ErrorCodes.ERR_NO_VALID_ADDRESSES)
130129
})
@@ -147,7 +146,6 @@ describe('libp2p.transportManager (dial only)', () => {
147146
start: false
148147
})
149148

150-
expect(libp2p.isStarted()).to.be.false()
151149
await expect(libp2p.start()).to.eventually.be.undefined()
152150
})
153151

@@ -169,7 +167,6 @@ describe('libp2p.transportManager (dial only)', () => {
169167
start: false
170168
})
171169

172-
expect(libp2p.isStarted()).to.be.false()
173170
await expect(libp2p.start()).to.eventually.be.undefined()
174171
})
175172
})

packages/peer-discovery-mdns/src/index.ts

Lines changed: 3 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -76,186 +76,9 @@
7676
* ```
7777
*/
7878

79-
import { CustomEvent, TypedEventEmitter } from '@libp2p/interface/events'
80-
import { peerDiscovery } from '@libp2p/interface/peer-discovery'
81-
import { logger } from '@libp2p/logger'
82-
import multicastDNS from 'multicast-dns'
83-
import * as query from './query.js'
84-
import { stringGen } from './utils.js'
85-
import type { PeerDiscovery, PeerDiscoveryEvents } from '@libp2p/interface/peer-discovery'
86-
import type { PeerInfo } from '@libp2p/interface/peer-info'
87-
import type { Startable } from '@libp2p/interface/src/startable.js'
88-
import type { AddressManager } from '@libp2p/interface-internal/address-manager'
89-
90-
const log = logger('libp2p:mdns')
91-
92-
export interface MulticastDNSInit {
93-
/**
94-
* (true/false) announce our presence through mDNS, default `true`
95-
*/
96-
broadcast?: boolean
97-
98-
/**
99-
* query interval, default 10 \* 1000 (10 seconds)
100-
*/
101-
interval?: number
102-
103-
/**
104-
* name of the service announce , default '_p2p._udp.local\`
105-
*/
106-
serviceTag?: string
107-
/**
108-
* Peer name to announce (should not be peeer id), default random string
109-
*/
110-
peerName?: string
111-
112-
/**
113-
* UDP port to broadcast to
114-
*/
115-
port?: number
116-
117-
/**
118-
* UDP IP to broadcast to
119-
*/
120-
ip?: string
121-
}
122-
123-
export interface MulticastDNSComponents {
124-
addressManager: AddressManager
125-
}
126-
127-
class MulticastDNS extends TypedEventEmitter<PeerDiscoveryEvents> implements PeerDiscovery, Startable {
128-
public mdns?: multicastDNS.MulticastDNS
129-
130-
private readonly broadcast: boolean
131-
private readonly interval: number
132-
private readonly serviceTag: string
133-
private readonly peerName: string
134-
private readonly port: number
135-
private readonly ip: string
136-
private _queryInterval: ReturnType<typeof setInterval> | null
137-
private readonly components: MulticastDNSComponents
138-
139-
constructor (components: MulticastDNSComponents, init: MulticastDNSInit = {}) {
140-
super()
141-
142-
this.broadcast = init.broadcast !== false
143-
this.interval = init.interval ?? (1e3 * 10)
144-
this.serviceTag = init.serviceTag ?? '_p2p._udp.local'
145-
this.ip = init.ip ?? '224.0.0.251'
146-
this.peerName = init.peerName ?? stringGen(63)
147-
// 63 is dns label limit
148-
if (this.peerName.length >= 64) {
149-
throw new Error('Peer name should be less than 64 chars long')
150-
}
151-
this.port = init.port ?? 5353
152-
this.components = components
153-
this._queryInterval = null
154-
this._onMdnsQuery = this._onMdnsQuery.bind(this)
155-
this._onMdnsResponse = this._onMdnsResponse.bind(this)
156-
this._onMdnsWarning = this._onMdnsWarning.bind(this)
157-
this._onMdnsError = this._onMdnsError.bind(this)
158-
}
159-
160-
readonly [peerDiscovery] = this
161-
162-
readonly [Symbol.toStringTag] = '@libp2p/mdns'
163-
164-
isStarted (): boolean {
165-
return Boolean(this.mdns)
166-
}
167-
168-
/**
169-
* Start sending queries to the LAN.
170-
*
171-
* @returns {void}
172-
*/
173-
async start (): Promise<void> {
174-
if (this.mdns != null) {
175-
return
176-
}
177-
178-
this.mdns = multicastDNS({ port: this.port, ip: this.ip })
179-
this.mdns.on('query', this._onMdnsQuery)
180-
this.mdns.on('response', this._onMdnsResponse)
181-
this.mdns.on('warning', this._onMdnsWarning)
182-
this.mdns.on('error', this._onMdnsError)
183-
184-
this._queryInterval = query.queryLAN(this.mdns, this.serviceTag, this.interval)
185-
}
186-
187-
_onMdnsQuery (event: multicastDNS.QueryPacket): void {
188-
if (this.mdns == null) {
189-
return
190-
}
191-
192-
log.trace('received incoming mDNS query')
193-
query.gotQuery(
194-
event,
195-
this.mdns,
196-
this.peerName,
197-
this.components.addressManager.getAddresses(),
198-
this.serviceTag,
199-
this.broadcast)
200-
}
201-
202-
_onMdnsResponse (event: multicastDNS.ResponsePacket): void {
203-
log.trace('received mDNS query response')
204-
205-
try {
206-
const foundPeer = query.gotResponse(event, this.peerName, this.serviceTag)
207-
208-
if (foundPeer != null) {
209-
log('discovered peer in mDNS query response %p', foundPeer.id)
210-
211-
this.dispatchEvent(new CustomEvent<PeerInfo>('peer', {
212-
detail: foundPeer
213-
}))
214-
}
215-
} catch (err) {
216-
log.error('Error processing peer response', err)
217-
}
218-
}
219-
220-
_onMdnsWarning (err: Error): void {
221-
log.error('mdns warning', err)
222-
}
223-
224-
_onMdnsError (err: Error): void {
225-
log.error('mdns error', err)
226-
}
227-
228-
/**
229-
* Stop sending queries to the LAN.
230-
*
231-
* @returns {Promise}
232-
*/
233-
async stop (): Promise<void> {
234-
if (this.mdns == null) {
235-
return
236-
}
237-
238-
this.mdns.removeListener('query', this._onMdnsQuery)
239-
this.mdns.removeListener('response', this._onMdnsResponse)
240-
this.mdns.removeListener('warning', this._onMdnsWarning)
241-
this.mdns.removeListener('error', this._onMdnsError)
242-
243-
if (this._queryInterval != null) {
244-
clearInterval(this._queryInterval)
245-
this._queryInterval = null
246-
}
247-
248-
await new Promise<void>((resolve) => {
249-
if (this.mdns != null) {
250-
this.mdns.destroy(resolve)
251-
} else {
252-
resolve()
253-
}
254-
})
255-
256-
this.mdns = undefined
257-
}
258-
}
79+
import { MulticastDNS } from './mdns.js'
80+
import type { MulticastDNSInit, MulticastDNSComponents } from './mdns.js'
81+
import type { PeerDiscovery } from '@libp2p/interface/peer-discovery'
25982

26083
export function mdns (init: MulticastDNSInit = {}): (components: MulticastDNSComponents) => PeerDiscovery {
26184
return (components: MulticastDNSComponents) => new MulticastDNS(components, init)

0 commit comments

Comments
 (0)