fix INVALID_PROTOCOL when saving http website

Using a https agent to fetch a http site causes this error:
TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:"
This commit is contained in:
Jan van Brügge
2025-07-14 00:42:59 +01:00
parent 936f7d9614
commit 327826d760
2 changed files with 14 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
import fetch from "node-fetch";
import https from "https";
import http from "http";
import { HttpsProxyAgent } from "https-proxy-agent";
import { SocksProxyAgent } from "socks-proxy-agent";
@@ -8,10 +9,12 @@ export default async function fetchTitleAndHeaders(url: string) {
return { title: "", headers: null };
try {
const httpsAgent = new https.Agent({
rejectUnauthorized:
process.env.IGNORE_UNAUTHORIZED_CA === "true" ? false : true,
});
const httpsAgent = url?.startsWith("http://")
? new http.Agent({})
: new https.Agent({
rejectUnauthorized:
process.env.IGNORE_UNAUTHORIZED_CA === "true" ? false : true,
});
// fetchOpts allows a proxy to be defined
let fetchOpts = {

View File

@@ -1,5 +1,6 @@
import fetch from "node-fetch";
import https from "https";
import http from "http";
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from "socks-proxy-agent";
@@ -7,10 +8,12 @@ export default async function fetchHeaders(url: string) {
if (process.env.IGNORE_URL_SIZE_LIMIT === "true") return null;
try {
const httpsAgent = new https.Agent({
rejectUnauthorized:
process.env.IGNORE_UNAUTHORIZED_CA === "true" ? false : true,
});
const httpsAgent = url.startsWith("http://")
? new http.Agent({})
: new https.Agent({
rejectUnauthorized:
process.env.IGNORE_UNAUTHORIZED_CA === "true" ? false : true,
});
let fetchOpts = {
method: "HEAD",