From c4dc4536e11211e87b0274e1499124610817b7a6 Mon Sep 17 00:00:00 2001 From: 0x009922 <43530070+0x009922@users.noreply.github.com> Date: Fri, 9 May 2025 11:50:35 +0900 Subject: [PATCH 1/2] refactor(client): move `.peers()` method from TelemetryAPI to MainAPI Signed-off-by: 0x009922 <43530070+0x009922@users.noreply.github.com> --- packages/client/api.ts | 33 ++++++++++++++-------------- tests/node/tests/client-apis.spec.ts | 2 +- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/client/api.ts b/packages/client/api.ts index dd4f50bf..546997de 100644 --- a/packages/client/api.ts +++ b/packages/client/api.ts @@ -207,6 +207,22 @@ export class MainAPI { await ResponseError.assertStatus(response, 200) return response.json() } + + public async peers(): Promise { + const response = await this.http.getFetch()(urlJoinPath(this.http.toriiBaseURL, ENDPOINT_PEERS)) + await ResponseError.assertStatus(response, 200) + return response.json().then( + // array of strings in format `@` + (ids: string[]) => { + assert(Array.isArray(ids)) + return ids.map((id) => { + assert(typeof id === 'string') + const [pubkey, address] = id.split('@') + return { id: dm.PublicKey.fromMultihash(pubkey), address } + }) + }, + ) + } } async function handleQueryResponse(resp: Response): Promise { @@ -248,23 +264,6 @@ export class TelemetryAPI { return response.arrayBuffer().then((buffer) => getCodec(dm.Status).decode(new Uint8Array(buffer))) } - // TODO: move once metrics are updated - public async peers(): Promise { - const response = await this.http.getFetch()(urlJoinPath(this.http.toriiBaseURL, ENDPOINT_PEERS)) - await ResponseError.assertStatus(response, 200) - return response.json().then( - // array of strings in format `@` - (ids: string[]) => { - assert(Array.isArray(ids)) - return ids.map((id) => { - assert(typeof id === 'string') - const [pubkey, address] = id.split('@') - return { id: dm.PublicKey.fromMultihash(pubkey), address } - }) - }, - ) - } - public async metrics(): Promise { const response = await this.http.getFetch()(urlJoinPath(this.http.toriiBaseURL, ENDPOINT_METRICS)) await ResponseError.assertStatus(response, 200) diff --git a/tests/node/tests/client-apis.spec.ts b/tests/node/tests/client-apis.spec.ts index c01b28cc..ac84c17e 100644 --- a/tests/node/tests/client-apis.spec.ts +++ b/tests/node/tests/client-apis.spec.ts @@ -108,7 +108,7 @@ describe('Telemetry API methods', () => { test('peers (network of 4)', async () => { const { peers } = await useNetwork({ peers: 4, seed: new Uint8Array(Buffer.from('deadbeef', 'hex')) }) - const peersData = await peers[0].client.api.telemetry.peers() + const peersData = await peers[0].client.api.peers() expect(peersData.map((x) => x.id.multihash())).contain.all.members( peers.slice(1).map((x) => x.keypair.publicKey().multihash()), From 3a97e29f784bf6c5bc21d6ead6cac78f60d0382e Mon Sep 17 00:00:00 2001 From: 0x009922 <43530070+0x009922@users.noreply.github.com> Date: Fri, 9 May 2025 12:03:06 +0900 Subject: [PATCH 2/2] refactor: leave previous method, but deprecate Signed-off-by: 0x009922 <43530070+0x009922@users.noreply.github.com> --- packages/client/api.ts | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/packages/client/api.ts b/packages/client/api.ts index 546997de..d9f79c71 100644 --- a/packages/client/api.ts +++ b/packages/client/api.ts @@ -208,20 +208,8 @@ export class MainAPI { return response.json() } - public async peers(): Promise { - const response = await this.http.getFetch()(urlJoinPath(this.http.toriiBaseURL, ENDPOINT_PEERS)) - await ResponseError.assertStatus(response, 200) - return response.json().then( - // array of strings in format `@` - (ids: string[]) => { - assert(Array.isArray(ids)) - return ids.map((id) => { - assert(typeof id === 'string') - const [pubkey, address] = id.split('@') - return { id: dm.PublicKey.fromMultihash(pubkey), address } - }) - }, - ) + public peers(): Promise { + return getPeers(this.http) } } @@ -264,9 +252,32 @@ export class TelemetryAPI { return response.arrayBuffer().then((buffer) => getCodec(dm.Status).decode(new Uint8Array(buffer))) } + /** + * @deprecated use {@linkcode MainAPI#peers} + */ + public peers(): Promise { + return getPeers(this.http) + } + public async metrics(): Promise { const response = await this.http.getFetch()(urlJoinPath(this.http.toriiBaseURL, ENDPOINT_METRICS)) await ResponseError.assertStatus(response, 200) return response.text() } } + +async function getPeers(http: HttpTransport) { + const response = await http.getFetch()(urlJoinPath(http.toriiBaseURL, ENDPOINT_PEERS)) + await ResponseError.assertStatus(response, 200) + return response.json().then( + // array of strings in format `@` + (ids: string[]) => { + assert(Array.isArray(ids)) + return ids.map((id) => { + assert(typeof id === 'string') + const [pubkey, address] = id.split('@') + return { id: dm.PublicKey.fromMultihash(pubkey), address } + }) + }, + ) +}