I am trying to override the createConnection field of https.requests, but can't figure out a way to do so without certificate errors.
When the createConnection field is not set, the requests works without any certificate errors.
When using net.createConnection, it yields a socket error.
When using tls.conenct, it yields a self signed cert error.
When using options._defaultAgent.createConnection, it also yield a self signed cert error.
When rejectUnauthorized is set to true, the request works with tls.connect and _defaultAgent.createConnection. But as the target is google with valid certificate, I don't get why I would need to disable that verification.
Eventually, I'd like to mutate options, but for now, I am just trying to log it without breaking the request.
Any pointers?
node v20.12.0, typescript v5.4.5.
import * as https from "https";import type { RequestOptions } from "https";import type { ClientRequestArgs } from "http";import type { Duplex } from "node:stream";import type { LookupOptions, LookupAddress } from "dns";import * as tls from "tls";import * as net from "net";const options: RequestOptions = { hostname: "www.google.com", port: 443, path: "/", method: "GET", // rejectUnauthorized: false, // createConnection: tls.connect, // Self signed error. // createConnection: net.createConnection, // Socket hung error. // createConnection: (options: ClientRequestArgs, oncreate: (err: Error | null, socket: Duplex) => void): Duplex | null | undefined => { // return (options._defaultAgent as any).createConnection(options, oncreate); // },};https .request(options, (res) => { let data = ""; res.on("data", (chunk) => { data += chunk; }); res.on("end", () => { console.log("StatusCode:", res.statusCode); console.log("Body:", data); }); }) .on("error", (err) => { console.log("Error: ", err); }) .end();