import "../../node-polyfill-fetch";
import { filterReqHeaders } from "./utils";
export 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 = 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;
};
export 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