import {Context, logger} from "mslib";
import HttpUtils from './HttpUtils';

export async function registerCDPWS(inspectPort: number, appPort: number, contextPath: string, localIp: string) {

    const clientId = Context.getEnv('DEBUG_CLIENT_ID');
    //获取当前debuggerWS的wsUrl
    const data = (await HttpUtils.get("http://127.0.0.1:" + inspectPort + "/json/list")).data;
    const target = data[0].webSocketDebuggerUrl;
    const hendLen = ("ws://127.0.0.1:" + inspectPort + "/").length;
    const wsId = target.substring(hendLen, target.length);
    const wsUrl = "ws://" + localIp + ":" + inspectPort + "/" + wsId;
    const redisKey = "k8s.route.nodejs.ws:" + clientId;

    logger.info("CDP Started [" + wsUrl + "]");
    //上报Ready

    const postData = {} as any;
    const routes = {} as any;
    postData["clientId"] = clientId;
    postData["routes"] = routes;
    routes["k8s.route.nodejs:" + clientId] = "http://" + localIp + ":" + appPort + "/" + contextPath;
    routes[redisKey] = wsUrl;

    return postData
}