| 1 | import { globalAgent } from "https"; |
| 2 | import * as fs from "node:fs"; |
| 3 | import tls from "node:tls"; |
| 4 | |
| 5 | import { RequestOptions } from "@continuedev/config-types"; |
| 6 | import * as followRedirects from "follow-redirects"; |
| 7 | import { HttpProxyAgent } from "http-proxy-agent"; |
| 8 | import { HttpsProxyAgent } from "https-proxy-agent"; |
| 9 | import fetch, { RequestInit, Response } from "node-fetch"; |
| 10 | |
| 11 | const { http, https } = (followRedirects as any).default; |
| 12 | |
| 13 | export async function fetchwithRequestOptions( |
| 14 | url_: URL | string, |
| 15 | init?: RequestInit, |
| 16 | requestOptions?: RequestOptions, |
| 17 | ): Promise<Response> { |
| 18 | const url = typeof url_ === "string" ? new URL(url_) : url_; |
| 19 | if (url.host === "localhost") { |
| 20 | url.host = "127.0.0.1"; |
| 21 | } |
| 22 | |
| 23 | const TIMEOUT = 7200; // 7200 seconds = 2 hours |
| 24 | |
| 25 | let globalCerts: string[] = []; |
| 26 | if (process.env.IS_BINARY) { |
| 27 | if (Array.isArray(globalAgent.options.ca)) { |
| 28 | globalCerts = [...globalAgent.options.ca.map((cert) => cert.toString())]; |
| 29 | } else if (typeof globalAgent.options.ca !== "undefined") { |
| 30 | globalCerts.push(globalAgent.options.ca.toString()); |
| 31 | } |
| 32 | } |
| 33 | const ca = Array.from(new Set([...tls.rootCertificates, ...globalCerts])); |
| 34 | const customCerts = |
| 35 | typeof requestOptions?.caBundlePath === "string" |
| 36 | ? [requestOptions?.caBundlePath] |
| 37 | : requestOptions?.caBundlePath; |
| 38 | if (customCerts) { |
| 39 | ca.push( |
| 40 | ...customCerts.map((customCert) => fs.readFileSync(customCert, "utf8")), |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | const timeout = (requestOptions?.timeout ?? TIMEOUT) * 1000; // measured in ms |
| 45 | |
| 46 | const agentOptions: { [key: string]: any } = { |
| 47 | ca, |
| 48 | rejectUnauthorized: requestOptions?.verifySsl, |
| 49 | timeout, |
| 50 | sessionTimeout: timeout, |
| 51 | keepAlive: true, |
| 52 | keepAliveMsecs: timeout, |
| 53 | }; |
| 54 | |
| 55 | // Handle ClientCertificateOptions |
| 56 | if (requestOptions?.clientCertificate) { |
| 57 | agentOptions.cert = fs.readFileSync( |
| 58 | requestOptions.clientCertificate.cert, |
| 59 | "utf8", |
| 60 | ); |
| 61 | agentOptions.key = fs.readFileSync( |
| 62 | requestOptions.clientCertificate.key, |
| 63 | "utf8", |
| 64 | ); |
| 65 | if (requestOptions.clientCertificate.passphrase) { |
| 66 | agentOptions.passphrase = requestOptions.clientCertificate.passphrase; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | const proxy = requestOptions?.proxy; |
| 71 | |
| 72 | // Create agent |
| 73 | const protocol = url.protocol === "https:" ? https : http; |
| 74 | const agent = |
| 75 | proxy && !requestOptions?.noProxy?.includes(url.hostname) |
| 76 | ? protocol === https |
| 77 | ? new HttpsProxyAgent(proxy, agentOptions) |
| 78 | : new HttpProxyAgent(proxy, agentOptions) |
| 79 | : new protocol.Agent(agentOptions); |
| 80 | |
| 81 | let headers: { [key: string]: string } = {}; |
| 82 | for (const [key, value] of Object.entries(init?.headers || {})) { |
| 83 | headers[key] = value as string; |
| 84 | } |
| 85 | headers = { |
| 86 | ...headers, |
| 87 | ...requestOptions?.headers, |
| 88 | }; |
| 89 | |
| 90 | // Replace localhost with 127.0.0.1 |
| 91 | if (url.hostname === "localhost") { |
| 92 | url.hostname = "127.0.0.1"; |
| 93 | } |
| 94 | |
| 95 | // add extra body properties if provided |
| 96 | let updatedBody: string | undefined = undefined; |
| 97 | try { |
| 98 | if (requestOptions?.extraBodyProperties && typeof init?.body === "string") { |
| 99 | const parsedBody = JSON.parse(init.body); |
| 100 | updatedBody = JSON.stringify({ |
| 101 | ...parsedBody, |
| 102 | ...requestOptions.extraBodyProperties, |
| 103 | }); |
| 104 | } |
| 105 | } catch (e) { |
| 106 | console.log("Unable to parse HTTP request body: ", e); |
| 107 | } |
| 108 | |
| 109 | // fetch the request with the provided options |
| 110 | const resp = await fetch(url, { |
| 111 | ...init, |
| 112 | body: updatedBody ?? init?.body, |
| 113 | headers: headers, |
| 114 | agent: agent, |
| 115 | }); |
| 116 | |
| 117 | if (!resp.ok) { |
| 118 | const requestId = resp.headers.get("x-request-id"); |
| 119 | if (requestId) { |
| 120 | console.log(`Request ID: ${requestId}, Status: ${resp.status}`); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return resp; |
| 125 | } |