fixed up some stuff
This commit is contained in:
parent
81d88357ca
commit
6cb6eb192c
3 changed files with 93 additions and 92 deletions
|
|
@ -7,6 +7,7 @@ import type {
|
|||
import { HttpError } from './types.js';
|
||||
import { ProxyManager } from './proxy-manager.js';
|
||||
import got from 'got';
|
||||
import { clear } from 'node:console';
|
||||
|
||||
export class HttpClient {
|
||||
private readonly config: HttpClientConfig;
|
||||
|
|
@ -43,6 +44,7 @@ export class HttpClient {
|
|||
*/
|
||||
async request<T = any>(config: RequestConfig): Promise<HttpResponse<T>> {
|
||||
const finalConfig = this.mergeConfig(config);
|
||||
const startTime = Date.now();
|
||||
this.logger?.debug('Making HTTP request', {
|
||||
method: finalConfig.method,
|
||||
url: finalConfig.url,
|
||||
|
|
@ -55,11 +57,14 @@ export class HttpClient {
|
|||
const useBunFetch = !proxy || ProxyManager.shouldUseBunFetch(proxy);
|
||||
|
||||
const response = await this.makeRequest<T>(finalConfig, useBunFetch);
|
||||
|
||||
const responseTime = Date.now() - startTime;
|
||||
response.responseTime = responseTime;
|
||||
|
||||
this.logger?.debug('HTTP request successful', {
|
||||
method: finalConfig.method,
|
||||
url: finalConfig.url,
|
||||
status: response.status,
|
||||
responseTime: responseTime,
|
||||
});
|
||||
|
||||
return response;
|
||||
|
|
@ -79,28 +84,41 @@ export class HttpClient {
|
|||
private async makeRequest<T>(config: RequestConfig, useBunFetch: boolean): Promise<HttpResponse<T>> {
|
||||
const timeout = config.timeout ?? this.config.timeout ?? 30000;
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
||||
|
||||
// Create timeout promise that rejects with proper error
|
||||
let timeoutId: NodeJS.Timeout | undefined;
|
||||
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||
timeoutId = setTimeout(() => {
|
||||
controller.abort();
|
||||
reject(new HttpError(`Request timeout after ${timeout}ms`));
|
||||
}, timeout);
|
||||
});
|
||||
|
||||
// Create request promise (don't await here!)
|
||||
const requestPromise = useBunFetch
|
||||
? this.fetchRequest<T>(config, controller.signal)
|
||||
: this.gotRequest<T>(config, controller.signal);
|
||||
|
||||
try {
|
||||
const response = useBunFetch
|
||||
? await this.fetchRequest<T>(config, controller.signal)
|
||||
: await this.gotRequest<T>(config, controller.signal);
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
return response;
|
||||
// Race the promises
|
||||
const result = await Promise.race([requestPromise, timeoutPromise]);
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
return result;
|
||||
} catch (error) {
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
// Unified timeout error handling
|
||||
if (controller.signal.aborted) {
|
||||
throw new HttpError(`Request timeout after ${timeout}ms`);
|
||||
}
|
||||
if ((error as any).name === 'TimeoutError') {
|
||||
throw new HttpError(`Request timeout after ${timeout}ms`);
|
||||
// If it's our timeout error, handle it
|
||||
if (error instanceof HttpError && error.message.includes('timeout')) {
|
||||
this.logger?.warn('Request timed out', {
|
||||
method: config.method,
|
||||
url: config.url,
|
||||
timeout: timeout,
|
||||
});
|
||||
throw error; // Re-throw the timeout error
|
||||
}
|
||||
|
||||
throw error; // Re-throw other errors as-is
|
||||
// Handle other errors (network, parsing, etc.)
|
||||
throw error;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Bun fetch implementation (simplified)
|
||||
|
|
@ -108,9 +126,9 @@ export class HttpClient {
|
|||
private async fetchRequest<T>(config: RequestConfig, signal: AbortSignal): Promise<HttpResponse<T>> {
|
||||
try {
|
||||
|
||||
// const options = this.buildFetchOptions(config, signal);
|
||||
const options = this.buildFetchOptions(config, signal);
|
||||
// console.log('Using fetch with proxy:', config.proxy);
|
||||
const options = config.proxy? { proxy: config.proxy.protocol + '://' + config.proxy?.host + ':' + config.proxy?.port } : {}//this.buildGotOptions(config, signal);
|
||||
// const options = config.proxy? { proxy: config.proxy.protocol + '://' + config.proxy?.host + ':' + config.proxy?.port } : {}//this.buildGotOptions(config, signal);
|
||||
|
||||
this.logger?.debug('Making request with fetch: ', { url: config.url, options })
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ export interface HttpResponse<T = any> {
|
|||
status: number;
|
||||
headers: Record<string, string>;
|
||||
ok: boolean;
|
||||
responseTime?: number;
|
||||
}
|
||||
|
||||
export class HttpError extends Error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue