"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const node_perf_hooks_1 = require("node:perf_hooks");
const crypto_1 = require("crypto");
function sha256(str) {
    return (0, crypto_1.createHash)("sha256").update(str).digest("hex");
}
/*
Maps objects returned by node:perf_hooks into our model StdRequest
*/
function reqToStdRequest(nodeRequest, nodeResponse) {
    return {
        method: nodeRequest.method,
        url: nodeRequest.url,
        headers: nodeRequest.headers,
        body: nodeRequest.body != null ? nodeRequest.body : {},
        fileName: sha256(`${nodeRequest.method}${nodeRequest.url}`),
        response: nodeResponse,
    };
}
/* Executed for each PerformanceEntry, includes more than http entries so we have to filter */
const onObserverEntry = (items, _observer) => {
    const entries = items.getEntries();
    for (const entry of entries) {
        if (entry.entryType === "http" || entry.entryType === "http2") {
            const httpEntry = entry;
            const req = httpEntry.detail?.req;
            const res = httpEntry.detail?.res;
            if (res != null && req != null) {
                global.bridgeTestsRequests.push(reqToStdRequest(req, res));
            }
        }
    }
};
exports.default = () => {
    global.bridgeTestsRequests = [];
    // We start sniffing traffic, after all tests we will create a mock for each entry
    global.bridgeTestsObserver = new node_perf_hooks_1.PerformanceObserver(onObserverEntry);
    global.bridgeTestsObserver.observe({ entryTypes: ["http", "http2"] });
    // eslint-disable-next-line no-console
    console.log("Observer started, starting network sniffing");
};
//# sourceMappingURL=bridgeSetupUpdateMocks.js.map