export async function getListOfTargets()

in src/utils.ts [214:245]


export async function getListOfTargets(hostname: string, port: number, useHttps: boolean): Promise<IRemoteTargetJson[]> {
    const checkDiscoveryEndpoint = (uri: string) => {
        return fetchUri(uri, { headers: { Host: 'localhost' } });
    };

    const protocol = (useHttps ? 'https' : 'http');

    let jsonResponse = null;
    for (const endpoint of ['/json/list', '/json']) {
        try {
            jsonResponse = await checkDiscoveryEndpoint(`${protocol}://${hostname}:${port}${endpoint}`);
            if (jsonResponse) {
                break;
            }
        } catch (e) {
            // localhost might not be ready as the user might not have a server running
            // user may also have changed settings making the endpoint invalid
        }
    }

    let result: IRemoteTargetJson[] = [];
    try {
        result = jsonResponse ? JSON.parse(jsonResponse) as IRemoteTargetJson[] : [];
    } catch (e) {
        void ErrorReporter.showErrorDialog({
            errorCode: ErrorCodes.Error,
            title: 'Error while parsing the list of targets.',
            message: e,
        });
    }
    return result;
}