mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-01-31 06:13:12 +00:00
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
|
|
import axios from 'axios';
|
||
|
|
import debug from 'debug';
|
||
|
|
|
||
|
|
const dbg = debug('waku:discovery');
|
||
|
|
|
||
|
|
/**
|
||
|
|
* GET list of nodes from remote HTTP host.
|
||
|
|
*
|
||
|
|
* @param path The property path to access the node list. The result should be
|
||
|
|
* a string, a string array or an object. If the result is an object then the
|
||
|
|
* values of the objects are used as multiaddresses.
|
||
|
|
* @param url Remote host containing bootstrap peers in JSON format.
|
||
|
|
*
|
||
|
|
* @returns An array of multiaddresses.
|
||
|
|
* @throws If the remote host is unreachable or the response cannot be parsed
|
||
|
|
* according to the passed _path_.
|
||
|
|
*/
|
||
|
|
export async function getBootstrapNodes(
|
||
|
|
path: string[] = ['fleets', 'wakuv2.prod', 'waku-websocket'],
|
||
|
|
url = 'https://fleets.status.im/'
|
||
|
|
): Promise<string[]> {
|
||
|
|
const res = await axios.get(url, {
|
||
|
|
headers: { 'Content-Type': 'application/json' },
|
||
|
|
});
|
||
|
|
|
||
|
|
let nodes = res.data;
|
||
|
|
|
||
|
|
for (const prop of path) {
|
||
|
|
if (nodes[prop] === undefined) {
|
||
|
|
dbg(
|
||
|
|
`Failed to retrieve bootstrap nodes: ${prop} does not exist on `,
|
||
|
|
nodes
|
||
|
|
);
|
||
|
|
throw `Failed to retrieve bootstrap nodes: ${prop} does not exist on ${JSON.stringify(
|
||
|
|
nodes
|
||
|
|
)}`;
|
||
|
|
}
|
||
|
|
nodes = nodes[prop];
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Array.isArray(nodes)) {
|
||
|
|
return nodes;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (typeof nodes === 'string') {
|
||
|
|
return [nodes];
|
||
|
|
}
|
||
|
|
|
||
|
|
if (typeof nodes === 'object') {
|
||
|
|
return Object.values(nodes);
|
||
|
|
}
|
||
|
|
|
||
|
|
throw `Failed to retrieve bootstrap nodes: response format is not supported: ${JSON.stringify(
|
||
|
|
nodes
|
||
|
|
)}`;
|
||
|
|
}
|