Skip to content

Commit 53a9be5

Browse files
authored
fix: export ping service capabilities (#3075)
Inform other components that this module can provide ping services.
1 parent da7353a commit 53a9be5

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

packages/protocol-ping/src/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,21 @@
2222
* ```
2323
*/
2424

25-
import { PingService as PingServiceClass } from './ping.js'
25+
import { Ping as PingClass } from './ping.js'
2626
import type { AbortOptions, ComponentLogger, PeerId } from '@libp2p/interface'
2727
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
2828
import type { Multiaddr } from '@multiformats/multiaddr'
2929

30-
export interface PingService {
30+
export interface Ping {
3131
ping(peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<number>
3232
}
3333

34-
export interface PingServiceInit {
34+
/**
35+
* @deprecated Use the `Ping` export instead
36+
*/
37+
export type PingService = Ping
38+
39+
export interface PingInit {
3540
protocolPrefix?: string
3641
maxInboundStreams?: number
3742
maxOutboundStreams?: number
@@ -43,14 +48,19 @@ export interface PingServiceInit {
4348
timeout?: number
4449
}
4550

46-
export interface PingServiceComponents {
51+
/**
52+
* @deprecated Use the `PingInit` export instead
53+
*/
54+
export type PingServiceInit = PingInit
55+
56+
export interface PingComponents {
4757
registrar: Registrar
4858
connectionManager: ConnectionManager
4959
logger: ComponentLogger
5060
}
5161

52-
export function ping (init: PingServiceInit = {}): (components: PingServiceComponents) => PingService {
53-
return (components) => new PingServiceClass(components, init)
62+
export function ping (init: PingInit = {}): (components: PingComponents) => Ping {
63+
return (components) => new PingClass(components, init)
5464
}
5565

5666
export { PING_PROTOCOL } from './constants.js'

packages/protocol-ping/src/ping.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { randomBytes } from '@libp2p/crypto'
2-
import { ProtocolError, TimeoutError, setMaxListeners } from '@libp2p/interface'
2+
import { ProtocolError, TimeoutError, serviceCapabilities, setMaxListeners } from '@libp2p/interface'
33
import { byteStream } from 'it-byte-stream'
44
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
55
import { PROTOCOL_PREFIX, PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION, TIMEOUT, MAX_INBOUND_STREAMS, MAX_OUTBOUND_STREAMS } from './constants.js'
6-
import type { PingServiceComponents, PingServiceInit, PingService as PingServiceInterface } from './index.js'
6+
import type { PingComponents, PingInit, Ping as PingInterface } from './index.js'
77
import type { AbortOptions, Logger, Stream, PeerId, Startable, IncomingStreamData } from '@libp2p/interface'
88
import type { Multiaddr } from '@multiformats/multiaddr'
99

10-
export class PingService implements Startable, PingServiceInterface {
10+
export class Ping implements Startable, PingInterface {
1111
public readonly protocol: string
12-
private readonly components: PingServiceComponents
12+
private readonly components: PingComponents
1313
private started: boolean
1414
private readonly timeout: number
1515
private readonly maxInboundStreams: number
1616
private readonly maxOutboundStreams: number
1717
private readonly runOnLimitedConnection: boolean
1818
private readonly log: Logger
1919

20-
constructor (components: PingServiceComponents, init: PingServiceInit = {}) {
20+
constructor (components: PingComponents, init: PingInit = {}) {
2121
this.components = components
2222
this.log = components.logger.forComponent('libp2p:ping')
2323
this.started = false
@@ -32,6 +32,10 @@ export class PingService implements Startable, PingServiceInterface {
3232

3333
readonly [Symbol.toStringTag] = '@libp2p/ping'
3434

35+
readonly [serviceCapabilities]: string[] = [
36+
'@libp2p/ping'
37+
]
38+
3539
async start (): Promise<void> {
3640
await this.components.registrar.handle(this.protocol, this.handleMessage, {
3741
maxInboundStreams: this.maxInboundStreams,

packages/protocol-ping/test/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { duplexPair } from 'it-pair/duplex'
1111
import pDefer from 'p-defer'
1212
import { stubInterface, type StubbedInstance } from 'sinon-ts'
1313
import { PING_PROTOCOL } from '../src/constants.js'
14-
import { PingService } from '../src/ping.js'
14+
import { Ping } from '../src/ping.js'
1515
import type { ComponentLogger, Stream, Connection } from '@libp2p/interface'
1616
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
1717

@@ -34,7 +34,7 @@ function echoStream (): StubbedInstance<Stream> {
3434

3535
describe('ping', () => {
3636
let components: StubbedPingServiceComponents
37-
let ping: PingService
37+
let ping: Ping
3838

3939
beforeEach(async () => {
4040
components = {
@@ -43,7 +43,7 @@ describe('ping', () => {
4343
logger: defaultLogger()
4444
}
4545

46-
ping = new PingService(components, {
46+
ping = new Ping(components, {
4747
timeout: 50
4848
})
4949

0 commit comments

Comments
 (0)