Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions packages/client/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ export class MainAPI {
await ResponseError.assertStatus(response, 200)
return response.json()
}

public peers(): Promise<PeerJson[]> {
return getPeers(this.http)
}
}

async function handleQueryResponse(resp: Response): Promise<dm.QueryResponse> {
Expand Down Expand Up @@ -248,21 +252,11 @@ 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<PeerJson[]> {
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 `<pub key multihash>@<socket addr>`
(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 }
})
},
)
/**
* @deprecated use {@linkcode MainAPI#peers}
*/
public peers(): Promise<PeerJson[]> {
return getPeers(this.http)
}

public async metrics(): Promise<string> {
Expand All @@ -271,3 +265,19 @@ export class TelemetryAPI {
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 `<pub key multihash>@<socket addr>`
(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 }
})
},
)
}
2 changes: 1 addition & 1 deletion tests/node/tests/client-apis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,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()),
Expand Down