feat: remove BroadcastClient (#2408)

BREAKING CHANGE: `BroadcastClient` was removed. It was deprecated in 2.2
This commit is contained in:
Caleb Kniffen
2023-07-27 16:23:03 -05:00
parent 328987a0a3
commit 07afcea97d
4 changed files with 8 additions and 131 deletions

View File

@@ -1,84 +0,0 @@
import { Client, ClientOptions } from '.'
/**
* Client that can rely on multiple different servers.
*
* @deprecated since version 2.2.0.
* Will be deleted in version 3.0.0.
*
* Currently, this implementation does not provide better reliability.
* To get better reliability, implement reconnect/error handling logic
* and choose a reliable endpoint.
*
* If you need the ability to fall-back to different endpoints, consider
* using [xrpl-client](https://github.com/XRPL-Labs/xrpl-client/)
*
* @category Clients
*/
export class BroadcastClient extends Client {
private readonly clients: Client[]
/**
* Creates a new BroadcastClient.
*
* @category Constructor
* @param servers - An array of names of servers.
* @param options - Options for the clients.
*/
public constructor(servers: string[], options: ClientOptions = {}) {
super(servers[0], options)
const clients: Client[] = servers.map(
(server) => new Client(server, options),
)
this.clients = clients
this.getMethodNames().forEach((name: string) => {
this[name] = async (...args): Promise<unknown> =>
/* eslint-disable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call -- Generates types
from the Client */
Promise.race(clients.map(async (client) => client[name](...args)))
/* eslint-enable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call */
})
// connection methods must be overridden to apply to all client instances
this.connect = async (): Promise<void> => {
await Promise.all(clients.map(async (client) => client.connect()))
}
this.disconnect = async (): Promise<void> => {
await Promise.all(clients.map(async (client) => client.disconnect()))
}
this.isConnected = (): boolean =>
clients.map((client) => client.isConnected()).every(Boolean)
clients.forEach((client) => {
client.on('error', (errorCode, errorMessage, data) =>
this.emit('error', errorCode, errorMessage, data),
)
})
}
/**
* Gets the method names of all the methods of the client.
*
* @returns A list of the names of all the methods of the client.
*/
private getMethodNames(): string[] {
const methodNames: string[] = []
const firstClient = this.clients[0]
const methods = Object.getOwnPropertyNames(firstClient)
methods.push(
...Object.getOwnPropertyNames(Object.getPrototypeOf(firstClient)),
)
for (const name of methods) {
if (
typeof firstClient[name] === 'function' &&
name !== 'constructor' &&
name !== 'on'
) {
methodNames.push(name)
}
}
return methodNames
}
}

View File

@@ -1,6 +1,3 @@
// Broadcast client is experimental
export { BroadcastClient } from './client/BroadcastClient'
export { Client, ClientOptions } from './client' export { Client, ClientOptions } from './client'
export * from './models' export * from './models'

View File

@@ -1,6 +1,4 @@
import { Client } from '../src/client' import { Client } from '../src/client'
// eslint-disable-next-line import/no-deprecated -- Will remove in 3.0.0
import { BroadcastClient } from '../src/client/BroadcastClient'
import createMockRippled, { import createMockRippled, {
type MockedWebSocketServer, type MockedWebSocketServer,
@@ -9,8 +7,7 @@ import rippled from './fixtures/rippled'
import { destroyServer, getFreePort } from './testUtils' import { destroyServer, getFreePort } from './testUtils'
export interface XrplTestContext { export interface XrplTestContext {
// eslint-disable-next-line import/no-deprecated -- Will remove in 3.0.0 client: Client
client: Client | BroadcastClient
_mockedServerPort?: number _mockedServerPort?: number
mockRippled?: MockedWebSocketServer mockRippled?: MockedWebSocketServer
mocks?: MockedWebSocketServer[] mocks?: MockedWebSocketServer[]
@@ -38,32 +35,12 @@ async function setupMockRippledConnection(
return context.client.connect().then(() => context) return context.client.connect().then(() => context)
} }
async function setupMockRippledConnectionForBroadcast(
ports: number[],
): Promise<XrplTestContext> {
const servers = ports.map((port) => `ws://localhost:${port}`)
const context: XrplTestContext = {
mocks: ports.map((port) => createMockRippled(port)),
// eslint-disable-next-line import/no-deprecated -- Will remove in 3.0.0
client: new BroadcastClient(servers),
servers: ports,
}
return context.client.connect().then(() => context)
}
async function setupClient(): Promise<XrplTestContext> { async function setupClient(): Promise<XrplTestContext> {
return getFreePort().then(async (port) => { return getFreePort().then(async (port) => {
return setupMockRippledConnection(port) return setupMockRippledConnection(port)
}) })
} }
async function setupBroadcast(): Promise<XrplTestContext> {
return Promise.all([getFreePort(), getFreePort()]).then(async (ports) => {
return setupMockRippledConnectionForBroadcast(ports)
})
}
async function teardownClient( async function teardownClient(
incomingContext: XrplTestContext, incomingContext: XrplTestContext,
done?: () => void, done?: () => void,
@@ -98,4 +75,4 @@ async function teardownClient(
}) })
} }
export { setupClient, teardownClient, setupBroadcast, createMockRippled } export { setupClient, teardownClient, createMockRippled }

View File

@@ -1,6 +1,5 @@
import { assert } from 'chai' import { assert } from 'chai'
import type { Client } from '../../src/client'
import { import {
FaucetNetwork, FaucetNetwork,
FaucetNetworkPaths, FaucetNetworkPaths,
@@ -26,10 +25,7 @@ describe('Get Faucet host ', function () {
// @ts-expect-error Intentionally modifying private data for test // @ts-expect-error Intentionally modifying private data for test
testContext.client.connection.url = FaucetNetwork.Devnet testContext.client.connection.url = FaucetNetwork.Devnet
assert.strictEqual( assert.strictEqual(getFaucetHost(testContext.client), expectedFaucet)
getFaucetHost(testContext.client as Client),
expectedFaucet,
)
}) })
it('returns the Testnet host', function () { it('returns the Testnet host', function () {
@@ -37,10 +33,7 @@ describe('Get Faucet host ', function () {
// @ts-expect-error Intentionally modifying private data for test // @ts-expect-error Intentionally modifying private data for test
testContext.client.connection.url = FaucetNetwork.Testnet testContext.client.connection.url = FaucetNetwork.Testnet
assert.strictEqual( assert.strictEqual(getFaucetHost(testContext.client), expectedFaucet)
getFaucetHost(testContext.client as Client),
expectedFaucet,
)
}) })
it('returns the Testnet host with the XRPL Labs server', function () { it('returns the Testnet host with the XRPL Labs server', function () {
@@ -48,10 +41,7 @@ describe('Get Faucet host ', function () {
// @ts-expect-error Intentionally modifying private data for test // @ts-expect-error Intentionally modifying private data for test
testContext.client.connection.url = 'wss://testnet.xrpl-labs.com' testContext.client.connection.url = 'wss://testnet.xrpl-labs.com'
assert.strictEqual( assert.strictEqual(getFaucetHost(testContext.client), expectedFaucet)
getFaucetHost(testContext.client as Client),
expectedFaucet,
)
}) })
it('returns the Hooks V3 Testnet host', function () { it('returns the Hooks V3 Testnet host', function () {
@@ -59,10 +49,7 @@ describe('Get Faucet host ', function () {
// @ts-expect-error Intentionally modifying private data for test // @ts-expect-error Intentionally modifying private data for test
testContext.client.connection.url = FaucetNetwork.HooksV3Testnet testContext.client.connection.url = FaucetNetwork.HooksV3Testnet
assert.strictEqual( assert.strictEqual(getFaucetHost(testContext.client), expectedFaucet)
getFaucetHost(testContext.client as Client),
expectedFaucet,
)
}) })
it('returns the correct faucetPath for Devnet host', function () { it('returns the correct faucetPath for Devnet host', function () {
@@ -71,7 +58,7 @@ describe('Get Faucet host ', function () {
testContext.client.connection.url = FaucetNetwork.Devnet testContext.client.connection.url = FaucetNetwork.Devnet
assert.strictEqual( assert.strictEqual(
getDefaultFaucetPath(getFaucetHost(testContext.client as Client)), getDefaultFaucetPath(getFaucetHost(testContext.client)),
expectedFaucetPath, expectedFaucetPath,
) )
}) })
@@ -82,7 +69,7 @@ describe('Get Faucet host ', function () {
testContext.client.connection.url = FaucetNetwork.HooksV3Testnet testContext.client.connection.url = FaucetNetwork.HooksV3Testnet
assert.strictEqual( assert.strictEqual(
getDefaultFaucetPath(getFaucetHost(testContext.client as Client)), getDefaultFaucetPath(getFaucetHost(testContext.client)),
expectedFaucetPath, expectedFaucetPath,
) )
}) })