"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
0 && (module.exports = {
    invokeRequest: null,
    pipeReadable: null
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    invokeRequest: function() {
        return invokeRequest;
    },
    pipeReadable: function() {
        return pipeReadable;
    }
});
require("../../node-polyfill-fetch");
const _utils = require("./utils");
const invokeRequest = async (targetUrl, requestInit, readableBody)=>{
    // force to 127.0.0.1 as IPC always runs on this hostname
    // to avoid localhost issues
    const parsedTargetUrl = new URL(targetUrl);
    parsedTargetUrl.hostname = "127.0.0.1";
    const invokeHeaders = (0, _utils.filterReqHeaders)({
        "cache-control": "",
        ...requestInit.headers
    });
    const invokeRes = await fetch(parsedTargetUrl.toString(), {
        headers: invokeHeaders,
        method: requestInit.method,
        redirect: "manual",
        ...requestInit.method !== "GET" && requestInit.method !== "HEAD" && readableBody ? {
            body: readableBody,
            duplex: "half"
        } : {},
        next: {
            // @ts-ignore
            internal: true
        }
    });
    return invokeRes;
};
async function pipeReadable(readable, writable) {
    const reader = readable.getReader();
    async function doRead() {
        const item = await reader.read();
        if (item == null ? void 0 : item.value) {
            writable.write(Buffer.from(item == null ? void 0 : item.value));
            if ("flush" in writable) {
                writable.flush();
            }
        }
        if (!(item == null ? void 0 : item.done)) {
            return doRead();
        }
    }
    await doRead();
    writable.end();
}

//# sourceMappingURL=invoke-request.js.map