mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-19 19:55:51 +00:00
feat: remove BroadcastClient (#2408)
BREAKING CHANGE: `BroadcastClient` was removed. It was deprecated in 2.2
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,3 @@
|
||||
// Broadcast client is experimental
|
||||
export { BroadcastClient } from './client/BroadcastClient'
|
||||
|
||||
export { Client, ClientOptions } from './client'
|
||||
|
||||
export * from './models'
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
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, {
|
||||
type MockedWebSocketServer,
|
||||
@@ -9,8 +7,7 @@ import rippled from './fixtures/rippled'
|
||||
import { destroyServer, getFreePort } from './testUtils'
|
||||
|
||||
export interface XrplTestContext {
|
||||
// eslint-disable-next-line import/no-deprecated -- Will remove in 3.0.0
|
||||
client: Client | BroadcastClient
|
||||
client: Client
|
||||
_mockedServerPort?: number
|
||||
mockRippled?: MockedWebSocketServer
|
||||
mocks?: MockedWebSocketServer[]
|
||||
@@ -38,32 +35,12 @@ async function setupMockRippledConnection(
|
||||
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> {
|
||||
return getFreePort().then(async (port) => {
|
||||
return setupMockRippledConnection(port)
|
||||
})
|
||||
}
|
||||
|
||||
async function setupBroadcast(): Promise<XrplTestContext> {
|
||||
return Promise.all([getFreePort(), getFreePort()]).then(async (ports) => {
|
||||
return setupMockRippledConnectionForBroadcast(ports)
|
||||
})
|
||||
}
|
||||
|
||||
async function teardownClient(
|
||||
incomingContext: XrplTestContext,
|
||||
done?: () => void,
|
||||
@@ -98,4 +75,4 @@ async function teardownClient(
|
||||
})
|
||||
}
|
||||
|
||||
export { setupClient, teardownClient, setupBroadcast, createMockRippled }
|
||||
export { setupClient, teardownClient, createMockRippled }
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { assert } from 'chai'
|
||||
|
||||
import type { Client } from '../../src/client'
|
||||
import {
|
||||
FaucetNetwork,
|
||||
FaucetNetworkPaths,
|
||||
@@ -26,10 +25,7 @@ describe('Get Faucet host ', function () {
|
||||
// @ts-expect-error Intentionally modifying private data for test
|
||||
testContext.client.connection.url = FaucetNetwork.Devnet
|
||||
|
||||
assert.strictEqual(
|
||||
getFaucetHost(testContext.client as Client),
|
||||
expectedFaucet,
|
||||
)
|
||||
assert.strictEqual(getFaucetHost(testContext.client), expectedFaucet)
|
||||
})
|
||||
|
||||
it('returns the Testnet host', function () {
|
||||
@@ -37,10 +33,7 @@ describe('Get Faucet host ', function () {
|
||||
// @ts-expect-error Intentionally modifying private data for test
|
||||
testContext.client.connection.url = FaucetNetwork.Testnet
|
||||
|
||||
assert.strictEqual(
|
||||
getFaucetHost(testContext.client as Client),
|
||||
expectedFaucet,
|
||||
)
|
||||
assert.strictEqual(getFaucetHost(testContext.client), expectedFaucet)
|
||||
})
|
||||
|
||||
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
|
||||
testContext.client.connection.url = 'wss://testnet.xrpl-labs.com'
|
||||
|
||||
assert.strictEqual(
|
||||
getFaucetHost(testContext.client as Client),
|
||||
expectedFaucet,
|
||||
)
|
||||
assert.strictEqual(getFaucetHost(testContext.client), expectedFaucet)
|
||||
})
|
||||
|
||||
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
|
||||
testContext.client.connection.url = FaucetNetwork.HooksV3Testnet
|
||||
|
||||
assert.strictEqual(
|
||||
getFaucetHost(testContext.client as Client),
|
||||
expectedFaucet,
|
||||
)
|
||||
assert.strictEqual(getFaucetHost(testContext.client), expectedFaucet)
|
||||
})
|
||||
|
||||
it('returns the correct faucetPath for Devnet host', function () {
|
||||
@@ -71,7 +58,7 @@ describe('Get Faucet host ', function () {
|
||||
testContext.client.connection.url = FaucetNetwork.Devnet
|
||||
|
||||
assert.strictEqual(
|
||||
getDefaultFaucetPath(getFaucetHost(testContext.client as Client)),
|
||||
getDefaultFaucetPath(getFaucetHost(testContext.client)),
|
||||
expectedFaucetPath,
|
||||
)
|
||||
})
|
||||
@@ -82,7 +69,7 @@ describe('Get Faucet host ', function () {
|
||||
testContext.client.connection.url = FaucetNetwork.HooksV3Testnet
|
||||
|
||||
assert.strictEqual(
|
||||
getDefaultFaucetPath(getFaucetHost(testContext.client as Client)),
|
||||
getDefaultFaucetPath(getFaucetHost(testContext.client)),
|
||||
expectedFaucetPath,
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user