Files
SillyTavern/src/request-proxy.js
T
DeathStalker471 b1ef254f78 fix: disable HTTP keepAlive (Node 18 behavior) with a config toggle (#5519)
* implement disable keepalive, handle request-proxy and config logic

* Invert keep-alive boolean setting

* fix: clean-up server.js diff

* fix: boolean flag type

* feat: disable keep-alive by default

---------

Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
2026-04-24 22:53:35 +03:00

55 lines
1.8 KiB
JavaScript

import process from 'node:process';
import http from 'node:http';
import https from 'node:https';
import { ProxyAgent } from 'proxy-agent';
import { isValidUrl, color } from './util.js';
const LOG_HEADER = '[Request Proxy]';
/**
* Initialize request proxy.
* @param {ProxySettings} settings Proxy settings.
* @typedef {object} ProxySettings
* @property {boolean} enabled Whether proxy is enabled.
* @property {string} url Proxy URL.
* @property {string[]} bypass List of URLs to bypass proxy.
* @property {boolean} enableKeepAlive Enable HTTP/HTTPS keep-alive.
*/
export default function initRequestProxy({ enabled, url, bypass, enableKeepAlive }) {
try {
// No proxy is enabled, so return
if (!enabled) {
return;
}
if (!url) {
console.error(color.red(LOG_HEADER), 'No proxy URL provided');
return;
}
if (!isValidUrl(url)) {
console.error(color.red(LOG_HEADER), 'Invalid proxy URL provided');
return;
}
// ProxyAgent uses proxy-from-env under the hood
// Reference: https://github.com/Rob--W/proxy-from-env
process.env.all_proxy = url;
if (Array.isArray(bypass) && bypass.length > 0) {
process.env.no_proxy = bypass.join(',');
}
const proxyAgentOptions = enableKeepAlive ? { keepAlive: true } : { keepAlive: false };
const proxyAgent = new ProxyAgent(proxyAgentOptions);
http.globalAgent = proxyAgent;
https.globalAgent = proxyAgent;
console.info();
console.info(color.green(LOG_HEADER), 'Proxy URL is used:', color.blue(url));
console.info();
} catch (error) {
console.error(color.red(LOG_HEADER), 'Failed to initialize request proxy:', error);
}
}