-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttp-client.ts
More file actions
367 lines (314 loc) · 11.1 KB
/
Copy pathhttp-client.ts
File metadata and controls
367 lines (314 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import axios, { AxiosInstance, AxiosError } from 'axios';
import * as http from 'http';
import * as https from 'https';
import { Language } from '../types/index.js';
import { AuthError, RateLimitError, QuotaError, NetworkError, ConfigError, ValidationError } from '../utils/errors.js';
import { Logger } from '../utils/logger.js';
import { errorMessage } from '../utils/error-message.js';
import { VERSION } from '../version.js';
export const USER_AGENT = `deepl-cli/${VERSION} node/${process.versions.node}`;
export interface ProxyConfig {
protocol?: 'http' | 'https';
host: string;
port: number;
auth?: {
username: string;
password: string;
};
}
export interface DeepLClientOptions {
timeout?: number;
maxRetries?: number;
baseUrl?: string;
proxy?: ProxyConfig;
}
export function sanitizeUrl(url: string): string {
try {
const parsed = new URL(url);
if (parsed.username || parsed.password) {
parsed.username = '***';
parsed.password = '***';
}
return parsed.toString();
} catch {
return '[invalid URL]';
}
}
const FREE_API_URL = 'https://api-free.deepl.com';
const PRO_API_URL = 'https://api.deepl.com';
export function isFreeApiKey(apiKey: string): boolean {
return apiKey.endsWith(':fx');
}
const DEFAULT_TIMEOUT = 30000;
const DEFAULT_MAX_RETRIES = 3;
const MAX_SOCKETS = 10;
const MAX_FREE_SOCKETS = 10;
const KEEP_ALIVE_MSECS = 1000;
const RETRY_INITIAL_DELAY_MS = 1000;
const RETRY_MAX_DELAY_MS = 10000;
const RETRY_AFTER_MAX_SECONDS = 60;
export class HttpClient {
protected client: AxiosInstance;
protected maxRetries: number;
protected _lastTraceId?: string;
private static parseProxyFromEnv(): ProxyConfig | undefined {
const httpProxy = process.env['HTTP_PROXY'] ?? process.env['http_proxy'];
const httpsProxy = process.env['HTTPS_PROXY'] ?? process.env['https_proxy'];
const proxyUrl = httpsProxy ?? httpProxy;
if (!proxyUrl) return undefined;
try {
const url = new URL(proxyUrl);
const config: ProxyConfig = {
protocol: url.protocol.replace(':', '') as 'http' | 'https',
host: url.hostname,
port: parseInt(url.port || (url.protocol === 'https:' ? '443' : '80'), 10),
};
if (url.username && url.password) {
config.auth = {
username: url.username,
password: url.password,
};
}
return config;
} catch (error) {
throw new ConfigError(`Invalid proxy URL "${sanitizeUrl(proxyUrl)}": ${errorMessage(error)}`);
}
}
static validateConfig(apiKey: string, options: DeepLClientOptions = {}): void {
if (!apiKey || apiKey.trim() === '') {
throw new AuthError('API key is required');
}
if (!options.proxy) {
HttpClient.parseProxyFromEnv();
}
}
constructor(apiKey: string, options: DeepLClientOptions = {}) {
if (!apiKey || apiKey.trim() === '') {
throw new AuthError('API key is required');
}
const autoUrl = isFreeApiKey(apiKey) ? FREE_API_URL : PRO_API_URL;
const baseURL = options.baseUrl ?? autoUrl;
this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
const axiosConfig: Record<string, unknown> = {
baseURL,
timeout: options.timeout ?? DEFAULT_TIMEOUT,
headers: {
'Authorization': `DeepL-Auth-Key ${apiKey}`,
'User-Agent': USER_AGENT,
},
httpAgent: new http.Agent({
keepAlive: true,
keepAliveMsecs: KEEP_ALIVE_MSECS,
maxSockets: MAX_SOCKETS,
maxFreeSockets: MAX_FREE_SOCKETS,
timeout: options.timeout ?? DEFAULT_TIMEOUT,
}),
httpsAgent: new https.Agent({
keepAlive: true,
keepAliveMsecs: KEEP_ALIVE_MSECS,
maxSockets: MAX_SOCKETS,
maxFreeSockets: MAX_FREE_SOCKETS,
timeout: options.timeout ?? DEFAULT_TIMEOUT,
}),
};
const proxyConfig = options.proxy ?? HttpClient.parseProxyFromEnv();
if (proxyConfig) {
axiosConfig['proxy'] = {
protocol: proxyConfig.protocol,
host: proxyConfig.host,
port: proxyConfig.port,
...(proxyConfig.auth && { auth: proxyConfig.auth }),
};
}
this.client = axios.create(axiosConfig);
}
destroy(): void {
const httpAgent = this.client.defaults?.httpAgent as http.Agent | undefined;
const httpsAgent = this.client.defaults?.httpsAgent as https.Agent | undefined;
httpAgent?.destroy();
httpsAgent?.destroy();
}
get lastTraceId(): string | undefined {
return this._lastTraceId;
}
protected async makeRequest<T>(
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
path: string,
data?: Record<string, unknown>
): Promise<T> {
const buildConfig = (): Record<string, unknown> => {
if (method === 'GET') {
return { params: data };
} else if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
const formData = new URLSearchParams();
if (data) {
for (const [key, value] of Object.entries(data)) {
if (Array.isArray(value)) {
value.forEach(v => formData.append(key, String(v)));
} else {
formData.append(key, String(value));
}
}
}
return {
data: formData.toString(),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
};
}
return {};
};
return this.executeWithRetry<T>(method, path, buildConfig);
}
protected async makeJsonRequest<T, D = Record<string, unknown>>(
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
path: string,
data?: D,
params?: Record<string, string | number | boolean>
): Promise<T> {
const buildConfig = (): Record<string, unknown> => {
const config: Record<string, unknown> = {};
if (params) {
config['params'] = params;
}
if (method === 'GET') {
if (data) {
config['params'] = { ...params as Record<string, unknown>, ...data as Record<string, unknown> };
}
} else if (data !== undefined) {
config['data'] = data;
config['headers'] = {
'Content-Type': 'application/json',
};
}
return config;
};
return this.executeWithRetry<T>(method, path, buildConfig);
}
protected async makeRawRequest<T>(
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
path: string,
buildConfig: () => Record<string, unknown>
): Promise<T> {
return this.executeWithRetry<T>(method, path, buildConfig);
}
protected async executeWithRetry<T>(
method: string,
path: string,
buildConfig: () => Record<string, unknown>
): Promise<T> {
let lastError: Error | undefined;
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
try {
const config = buildConfig();
const requestStart = Date.now();
const response = await this.client.request<T>({
method,
url: path,
...config,
});
const requestElapsed = Date.now() - requestStart;
Logger.verbose(`[verbose] HTTP ${method} ${path} completed in ${requestElapsed}ms (status ${response.status})`);
const traceId = response.headers?.['x-trace-id'] as string | undefined;
if (traceId) {
this._lastTraceId = traceId;
}
return response.data;
} catch (error) {
lastError = error as Error;
if (this.isAxiosError(error)) {
const traceId = error.response?.headers?.['x-trace-id'] as string | undefined;
if (traceId) {
this._lastTraceId = traceId;
}
}
if (this.isAxiosError(error)) {
const status = error.response?.status;
if (status === 429 && attempt < this.maxRetries) {
const retryAfterDelay = this.parseRetryAfter(error.response?.headers?.['retry-after'] as string | undefined);
const delay = retryAfterDelay ?? Math.min(RETRY_INITIAL_DELAY_MS * Math.pow(2, attempt), RETRY_MAX_DELAY_MS);
await this.sleep(delay);
continue;
}
if (status && status >= 400 && status < 500) {
throw this.handleError(error);
}
}
if (attempt < this.maxRetries) {
const delay = Math.min(RETRY_INITIAL_DELAY_MS * Math.pow(2, attempt), RETRY_MAX_DELAY_MS);
await this.sleep(delay);
}
}
}
throw lastError ? this.handleError(lastError) : new NetworkError('Request failed after retries');
}
protected handleError(error: unknown): Error {
const traceIdSuffix = this._lastTraceId ? ` (Trace ID: ${this._lastTraceId})` : '';
if (this.isAxiosError(error)) {
const status = error.response?.status;
const responseData = error.response?.data as { message?: string } | undefined;
const message = responseData?.message ?? error.message;
switch (status) {
case 403:
return new AuthError(`Authentication failed: Invalid API key${traceIdSuffix}`);
case 456:
return new QuotaError(`Quota exceeded: Character limit reached${traceIdSuffix}`);
case 429:
return new RateLimitError(`Rate limit exceeded: Too many requests${traceIdSuffix}`);
case 503:
return new NetworkError(`Service temporarily unavailable: Please try again later${traceIdSuffix}`);
default:
if (status && status >= 500) {
return new NetworkError(`Server error (${status}): ${message}${traceIdSuffix}`);
}
if (!error.response && this.isNetworkLevelError(error)) {
return new NetworkError(`Network error: ${error.message}`);
}
return new ValidationError(`API error: ${message}${traceIdSuffix}`);
}
}
if (error instanceof Error) {
if (this.isNetworkLevelError(error)) {
return new NetworkError(`Network error: ${error.message}`);
}
return error;
}
return new NetworkError('Unknown error occurred');
}
private isNetworkLevelError(error: Error): boolean {
const msg = error.message.toLowerCase();
return msg.includes('econnrefused') ||
msg.includes('enotfound') ||
msg.includes('econnreset') ||
msg.includes('etimedout') ||
msg.includes('socket hang up');
}
protected normalizeLanguage(lang: string): Language {
return lang.toLowerCase() as Language;
}
protected isAxiosError(error: unknown): error is AxiosError {
return axios.isAxiosError(error);
}
protected parseRetryAfter(headerValue: string | undefined): number | undefined {
if (headerValue === undefined || headerValue === null) {
return undefined;
}
const seconds = Number(headerValue);
if (!isNaN(seconds) && isFinite(seconds)) {
const clamped = Math.max(0, Math.min(seconds, RETRY_AFTER_MAX_SECONDS));
return clamped * 1000;
}
const date = new Date(headerValue);
if (!isNaN(date.getTime())) {
const delayMs = date.getTime() - Date.now();
const delaySec = Math.max(0, delayMs / 1000);
const clamped = Math.min(delaySec, RETRY_AFTER_MAX_SECONDS);
return clamped * 1000;
}
return undefined;
}
protected sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}