mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-13 18:32:12 +03:00
Production (open-sse/utils/socksConnectorWithFamily.ts, 4 sites): every cast was redundant — undici's buildConnector.BuildOptions already has `timeout?: number | null`, socks' SocksClientOptions has `timeout?: number`, and Agent.Options' `connect` / `connectTimeout` narrow to the connector's parameter types on their own. Behaviour unchanged; check:open-sse-typecheck stays at the frozen 5. Tests (51 sites): the socks-timeout mocks now carry the real types — the patched SocksClient.createConnection is typed as the static it replaces, the fake buildConnector returns buildConnector.connector, the proxy is a SocksProxy, the dynamic import is typed as the module it loads; the e2e suite passes a SocksProxy and Agent.Options and no longer casts undici's fetch init (its RequestInit already has `dispatcher`); the isFree suites narrow getCustomModels()' JSON to a declared row shape, feed deliberately-wrong values through `unknown`, and stop casting for zod's safeParse, which takes unknown. The six files' suppression entries are removed: 1238 → 1232 files, 5487 → 5432 suppressed. ESLint without the suppressions file reports 0 problems on all six; with it, no stale entry is left. The five suites pass (4, 2, 5, 4, 4).
132 lines
4.5 KiB
TypeScript
132 lines
4.5 KiB
TypeScript
import { describe, it, afterEach, beforeEach } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import type net from "node:net";
|
|
import { SocksClient, type SocksClientOptions, type SocksProxy } from "socks";
|
|
import type { buildConnector } from "undici";
|
|
|
|
// Lightweight oracle — node:test, no vi.mock.
|
|
// We patch SocksClient.createConnection (writable) and inject a fake
|
|
// buildConnector via the 5th param to capture the TLS timeout without
|
|
// mutating the read-only undici module.
|
|
|
|
describe("socks connectTimeout forwarder", () => {
|
|
let capturedTimeout: number | undefined = undefined;
|
|
let capturedTlsTimeout: number | null | undefined = undefined;
|
|
let capturedTlsUndefined = false;
|
|
let origCreateConnection: typeof SocksClient.createConnection;
|
|
|
|
beforeEach(() => {
|
|
origCreateConnection = SocksClient.createConnection;
|
|
capturedTimeout = undefined;
|
|
capturedTlsTimeout = undefined;
|
|
capturedTlsUndefined = false;
|
|
SocksClient.createConnection = (async (opts: SocksClientOptions) => {
|
|
capturedTimeout = opts?.timeout;
|
|
return { socket: { setNoDelay: () => ({ setNoDelay: () => {} }) } } as unknown as Awaited<
|
|
ReturnType<typeof origCreateConnection>
|
|
>;
|
|
}) as typeof SocksClient.createConnection;
|
|
});
|
|
|
|
afterEach(() => {
|
|
SocksClient.createConnection = origCreateConnection;
|
|
capturedTimeout = undefined;
|
|
capturedTlsTimeout = undefined;
|
|
capturedTlsUndefined = false;
|
|
});
|
|
|
|
function fakeBuildConnector(opts: buildConnector.BuildOptions = {}): buildConnector.connector {
|
|
if (opts && typeof opts.timeout !== "undefined") capturedTlsTimeout = opts.timeout;
|
|
else capturedTlsUndefined = true;
|
|
return (_options, cb) => cb(null, { setNoDelay: () => ({}) } as unknown as net.Socket);
|
|
}
|
|
|
|
async function driveConnector(args: {
|
|
family: 4 | 6 | null;
|
|
tlsOpts?: buildConnector.BuildOptions;
|
|
connectTimeout?: number;
|
|
protocol?: string;
|
|
hostname?: string;
|
|
port?: string;
|
|
}) {
|
|
const mod = (await import(
|
|
`../../open-sse/utils/socksConnectorWithFamily.ts?t=${Date.now()}-${Math.random()}`
|
|
)) as typeof import("../../open-sse/utils/socksConnectorWithFamily.ts");
|
|
const proxy: SocksProxy = { host: "1.2.3.4", port: 1080, type: 5 };
|
|
const tlsOpts = args.tlsOpts ?? {};
|
|
const connectTimeout = args.connectTimeout;
|
|
const connector = mod.socksConnectorWithFamily(
|
|
proxy,
|
|
args.family,
|
|
tlsOpts,
|
|
connectTimeout,
|
|
fakeBuildConnector
|
|
);
|
|
await new Promise<void>((resolve, reject) =>
|
|
connector(
|
|
{
|
|
protocol: args.protocol ?? "https:",
|
|
hostname: args.hostname ?? "example.com",
|
|
port: args.port ?? "443",
|
|
},
|
|
(err) => (err ? reject(err) : resolve())
|
|
)
|
|
);
|
|
return { capturedTimeout, capturedTlsTimeout, capturedTlsUndefined, mod, connector };
|
|
}
|
|
|
|
it("U1: Agent.connectTimeout → SocksClient.timeout + TLS timeout", async () => {
|
|
const { capturedTimeout: t, capturedTlsTimeout: tls } = await driveConnector({
|
|
family: 4,
|
|
tlsOpts: {},
|
|
connectTimeout: 5000,
|
|
protocol: "https:",
|
|
port: "443",
|
|
});
|
|
assert.equal(t, 5000);
|
|
assert.equal(tls, 5000);
|
|
});
|
|
|
|
it("U2: fallback sans connectTimeout → SOCKS_HANDSHAKE (TLS no timeout)", async () => {
|
|
const prev = process.env.SOCKS_HANDSHAKE_TIMEOUT_MS;
|
|
process.env.SOCKS_HANDSHAKE_TIMEOUT_MS = "7777";
|
|
try {
|
|
const { capturedTimeout: t, capturedTlsUndefined: tlsUndef } = await driveConnector({
|
|
family: 6,
|
|
tlsOpts: {},
|
|
connectTimeout: undefined,
|
|
protocol: "https:",
|
|
port: "443",
|
|
});
|
|
assert.equal(t, 7777);
|
|
assert.equal(tlsUndef, true);
|
|
} finally {
|
|
if (prev === undefined) delete process.env.SOCKS_HANDSHAKE_TIMEOUT_MS;
|
|
else process.env.SOCKS_HANDSHAKE_TIMEOUT_MS = prev;
|
|
}
|
|
});
|
|
|
|
it("U3: http (no TLS) still bounds SocksClient", async () => {
|
|
const { capturedTimeout: t } = await driveConnector({
|
|
family: null,
|
|
tlsOpts: {},
|
|
connectTimeout: 5000,
|
|
protocol: "http:",
|
|
port: "80",
|
|
});
|
|
assert.equal(t, 5000);
|
|
});
|
|
|
|
it("U4: connectTimeout=0 → SocksClient undefined (SOCKS defaults to 30s) + TLS timeout 0 (disabled)", async () => {
|
|
const { capturedTimeout: t, capturedTlsTimeout: tls } = await driveConnector({
|
|
family: 4,
|
|
tlsOpts: {},
|
|
connectTimeout: 0,
|
|
protocol: "https:",
|
|
port: "443",
|
|
});
|
|
assert.equal(t, undefined);
|
|
assert.equal(tls, 0);
|
|
});
|
|
});
|