hyperttp 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Hyperttp/Core/CacheManager.d.ts +63 -40
- package/dist/Hyperttp/Core/CacheManager.d.ts.map +1 -1
- package/dist/Hyperttp/Core/CacheManager.js +64 -62
- package/dist/Hyperttp/Core/CacheManager.js.map +1 -1
- package/dist/Hyperttp/Core/HttpClientImproved.d.ts +55 -33
- package/dist/Hyperttp/Core/HttpClientImproved.d.ts.map +1 -1
- package/dist/Hyperttp/Core/HttpClientImproved.js +307 -211
- package/dist/Hyperttp/Core/HttpClientImproved.js.map +1 -1
- package/dist/Hyperttp/Core/InterceptorManager.d.ts +11 -11
- package/dist/Hyperttp/Core/InterceptorManager.d.ts.map +1 -1
- package/dist/Hyperttp/Core/InterceptorManager.js +10 -10
- package/dist/Hyperttp/Core/InterceptorManager.js.map +1 -1
- package/dist/Hyperttp/Core/MetricsManager.d.ts +33 -42
- package/dist/Hyperttp/Core/MetricsManager.d.ts.map +1 -1
- package/dist/Hyperttp/Core/MetricsManager.js +147 -58
- package/dist/Hyperttp/Core/MetricsManager.js.map +1 -1
- package/dist/Hyperttp/Core/QueueManager.d.ts +6 -4
- package/dist/Hyperttp/Core/QueueManager.d.ts.map +1 -1
- package/dist/Hyperttp/Core/QueueManager.js +36 -34
- package/dist/Hyperttp/Core/QueueManager.js.map +1 -1
- package/dist/Hyperttp/Core/RateLimiter.d.ts +29 -36
- package/dist/Hyperttp/Core/RateLimiter.d.ts.map +1 -1
- package/dist/Hyperttp/Core/RateLimiter.js +96 -36
- package/dist/Hyperttp/Core/RateLimiter.js.map +1 -1
- package/dist/Hyperttp/Core/RequestBuilder.d.ts +3 -1
- package/dist/Hyperttp/Core/RequestBuilder.d.ts.map +1 -1
- package/dist/Hyperttp/Core/RequestBuilder.js +10 -3
- package/dist/Hyperttp/Core/RequestBuilder.js.map +1 -1
- package/dist/Hyperttp/Core/RequestExecutor.d.ts +7 -34
- package/dist/Hyperttp/Core/RequestExecutor.d.ts.map +1 -1
- package/dist/Hyperttp/Core/RequestExecutor.js +120 -114
- package/dist/Hyperttp/Core/RequestExecutor.js.map +1 -1
- package/dist/Hyperttp/Core/ResponseConverter.d.ts +23 -0
- package/dist/Hyperttp/Core/ResponseConverter.d.ts.map +1 -0
- package/dist/Hyperttp/Core/ResponseConverter.js +368 -0
- package/dist/Hyperttp/Core/ResponseConverter.js.map +1 -0
- package/dist/Hyperttp/Core/index.d.ts +7 -10
- package/dist/Hyperttp/Core/index.d.ts.map +1 -1
- package/dist/Hyperttp/Core/index.js +26 -15
- package/dist/Hyperttp/Core/index.js.map +1 -1
- package/dist/Hyperttp/UrlExtractor.d.ts +1 -1
- package/dist/Hyperttp/UrlExtractor.d.ts.map +1 -1
- package/dist/Hyperttp/index.d.ts +1 -3
- package/dist/Hyperttp/index.d.ts.map +1 -1
- package/dist/Hyperttp/index.js +7 -8
- package/dist/Hyperttp/index.js.map +1 -1
- package/dist/Types/cache.d.ts +10 -0
- package/dist/Types/cache.d.ts.map +1 -0
- package/dist/Types/cache.js +3 -0
- package/dist/Types/cache.js.map +1 -0
- package/dist/Types/errors.d.ts +15 -0
- package/dist/Types/errors.d.ts.map +1 -0
- package/dist/Types/errors.js +34 -0
- package/dist/Types/errors.js.map +1 -0
- package/dist/Types/http-client.d.ts +39 -0
- package/dist/Types/http-client.d.ts.map +1 -0
- package/dist/Types/http-client.js +3 -0
- package/dist/Types/http-client.js.map +1 -0
- package/dist/Types/http.d.ts +5 -0
- package/dist/Types/http.d.ts.map +1 -0
- package/dist/Types/http.js +3 -0
- package/dist/Types/http.js.map +1 -0
- package/dist/Types/index.d.ts +12 -127
- package/dist/Types/index.d.ts.map +1 -1
- package/dist/Types/index.js +12 -39
- package/dist/Types/index.js.map +1 -1
- package/dist/Types/interceptors.d.ts +13 -0
- package/dist/Types/interceptors.d.ts.map +1 -0
- package/dist/Types/interceptors.js +3 -0
- package/dist/Types/interceptors.js.map +1 -0
- package/dist/Types/metrics.d.ts +67 -0
- package/dist/Types/metrics.d.ts.map +1 -0
- package/dist/Types/metrics.js +3 -0
- package/dist/Types/metrics.js.map +1 -0
- package/dist/Types/options.d.ts +233 -0
- package/dist/Types/options.d.ts.map +1 -0
- package/dist/Types/options.js +3 -0
- package/dist/Types/options.js.map +1 -0
- package/dist/Types/queue.d.ts +8 -0
- package/dist/Types/queue.d.ts.map +1 -0
- package/dist/Types/queue.js +3 -0
- package/dist/Types/queue.js.map +1 -0
- package/dist/Types/request.d.ts +148 -9
- package/dist/Types/request.d.ts.map +1 -1
- package/dist/Types/response.d.ts +28 -0
- package/dist/Types/response.d.ts.map +1 -0
- package/dist/Types/response.js +3 -0
- package/dist/Types/response.js.map +1 -0
- package/dist/Types/stream.d.ts +39 -0
- package/dist/Types/stream.d.ts.map +1 -0
- package/dist/Types/stream.js +3 -0
- package/dist/Types/stream.js.map +1 -0
- package/dist/Types/url-extractor.d.ts +10 -0
- package/dist/Types/url-extractor.d.ts.map +1 -0
- package/dist/Types/url-extractor.js +3 -0
- package/dist/Types/url-extractor.js.map +1 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/dist/index.js.map +1 -1
- package/package.json +7 -5
- package/dist/Hyperttp/Core/ResponseTransformer.d.ts +0 -35
- package/dist/Hyperttp/Core/ResponseTransformer.d.ts.map +0 -1
- package/dist/Hyperttp/Core/ResponseTransformer.js +0 -171
- package/dist/Hyperttp/Core/ResponseTransformer.js.map +0 -1
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { Agent } from "undici";
|
|
2
|
-
import { RetryOptions, LogLevel, RequestMetrics } from "../../Types";
|
|
3
2
|
import { InterceptorManager } from "./InterceptorManager";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* @ru Основной движок, отвечающий за низкоуровневое выполнение HTTP-запросов, повторы и логику редиректов.
|
|
8
|
-
*/
|
|
3
|
+
import { RetryOptions } from "../../Types/options";
|
|
4
|
+
import { LogLevel } from "../../Types/http";
|
|
5
|
+
import { RequestMetrics } from "../../Types/metrics";
|
|
9
6
|
export declare class RequestExecutor {
|
|
10
7
|
private agent;
|
|
11
8
|
private interceptors;
|
|
12
9
|
private options;
|
|
10
|
+
private readonly redirectStatusCodes;
|
|
13
11
|
constructor(agent: Agent, interceptors: InterceptorManager, options: {
|
|
14
12
|
timeout: number;
|
|
15
13
|
maxRetries: number;
|
|
@@ -19,36 +17,11 @@ export declare class RequestExecutor {
|
|
|
19
17
|
verbose?: boolean;
|
|
20
18
|
logger?: (level: LogLevel, message: string, meta?: any) => void;
|
|
21
19
|
});
|
|
22
|
-
/**
|
|
23
|
-
* @en Internal logger wrapper.
|
|
24
|
-
* @ru Внутренняя обертка для логирования.
|
|
25
|
-
*/
|
|
26
|
-
private log;
|
|
27
|
-
/**
|
|
28
|
-
* @en Calculates the delay before the next retry using Exponential Backoff and Jitter.
|
|
29
|
-
* @ru Вычисляет задержку перед следующим повтором, используя экспоненциальный рост и Jitter (джиттер).
|
|
30
|
-
*/
|
|
31
20
|
private calcDelay;
|
|
32
|
-
/** @en Simple async sleep helper. */
|
|
33
21
|
private sleep;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
*/
|
|
38
|
-
private parseRetryAfterMs;
|
|
39
|
-
/**
|
|
40
|
-
* @en Executes an HTTP request with full retry and redirect lifecycle management.
|
|
41
|
-
* @ru Выполняет HTTP-запрос с полным циклом управления повторами и редиректами.
|
|
42
|
-
* @param method - HTTP method
|
|
43
|
-
* @param url - Destination URL
|
|
44
|
-
* @param headers - Request headers
|
|
45
|
-
* @param body - Request payload
|
|
46
|
-
* @param metrics - Performance metrics object to update
|
|
47
|
-
* @param signal - External AbortSignal for user cancellation
|
|
48
|
-
* @param redirects - Internal redirect counter
|
|
49
|
-
* @param attempt - Internal retry attempt counter
|
|
50
|
-
*/
|
|
51
|
-
execute(method: string, url: string, headers: Record<string, string>, body: string | Buffer | undefined, metrics?: RequestMetrics, signal?: AbortSignal, redirects?: number, attempt?: number): Promise<{
|
|
22
|
+
private drainBody;
|
|
23
|
+
private executeCore;
|
|
24
|
+
execute(method: string, url: string, headers: Record<string, string>, body: string | Buffer | undefined, metrics?: RequestMetrics, signal?: AbortSignal): Promise<{
|
|
52
25
|
status: number;
|
|
53
26
|
headers: Record<string, any>;
|
|
54
27
|
body: any;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RequestExecutor.d.ts","sourceRoot":"","sources":["../../../src/Hyperttp/Core/RequestExecutor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,
|
|
1
|
+
{"version":3,"file":"RequestExecutor.d.ts","sourceRoot":"","sources":["../../../src/Hyperttp/Core/RequestExecutor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAUrD,qBAAa,eAAe;IAIxB,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,YAAY;IACpB,OAAO,CAAC,OAAO;IALjB,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAsC;gBAGhE,KAAK,EAAE,KAAK,EACZ,YAAY,EAAE,kBAAkB,EAChC,OAAO,EAAE;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,UAAU,EAAE,MAAM,CAAC;QACnB,eAAe,EAAE,OAAO,CAAC;QACzB,YAAY,EAAE,MAAM,CAAC;QACrB,YAAY,EAAE,YAAY,CAAC;QAC3B,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC;KACjE;IAGH,OAAO,CAAC,SAAS;IAMjB,OAAO,CAAC,KAAK;YAIC,SAAS;YAoBT,WAAW;IAsLnB,OAAO,CACX,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC/B,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,EACjC,OAAO,CAAC,EAAE,cAAc,EACxB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC;QACT,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAC7B,IAAI,EAAE,GAAG,CAAC;QACV,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;CAWH"}
|
|
@@ -2,159 +2,165 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RequestExecutor = void 0;
|
|
4
4
|
const undici_1 = require("undici");
|
|
5
|
-
const
|
|
6
|
-
/**
|
|
7
|
-
* @class RequestExecutor
|
|
8
|
-
* @en The core engine responsible for low-level HTTP execution, retries, and redirect logic.
|
|
9
|
-
* @ru Основной движок, отвечающий за низкоуровневое выполнение HTTP-запросов, повторы и логику редиректов.
|
|
10
|
-
*/
|
|
5
|
+
const errors_1 = require("../../Types/errors");
|
|
11
6
|
class RequestExecutor {
|
|
12
7
|
agent;
|
|
13
8
|
interceptors;
|
|
14
9
|
options;
|
|
10
|
+
redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
|
|
15
11
|
constructor(agent, interceptors, options) {
|
|
16
12
|
this.agent = agent;
|
|
17
13
|
this.interceptors = interceptors;
|
|
18
14
|
this.options = options;
|
|
19
15
|
}
|
|
20
|
-
/**
|
|
21
|
-
* @en Internal logger wrapper.
|
|
22
|
-
* @ru Внутренняя обертка для логирования.
|
|
23
|
-
*/
|
|
24
|
-
log(level, msg, meta) {
|
|
25
|
-
if (this.options.verbose && this.options.logger) {
|
|
26
|
-
this.options.logger(level, msg, meta);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* @en Calculates the delay before the next retry using Exponential Backoff and Jitter.
|
|
31
|
-
* @ru Вычисляет задержку перед следующим повтором, используя экспоненциальный рост и Jitter (джиттер).
|
|
32
|
-
*/
|
|
33
16
|
calcDelay(attempt) {
|
|
34
17
|
const { baseDelay, maxDelay, jitter } = this.options.retryOptions;
|
|
35
18
|
const base = Math.min(baseDelay * Math.pow(2, attempt), maxDelay);
|
|
36
|
-
// Jitter помогает избежать "эффекта стада", распределяя запросы во времени
|
|
37
19
|
return jitter ? base * (0.75 + Math.random() * 0.5) : base;
|
|
38
20
|
}
|
|
39
|
-
/** @en Simple async sleep helper. */
|
|
40
21
|
sleep(ms) {
|
|
41
22
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
42
23
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
24
|
+
async drainBody(body) {
|
|
25
|
+
if (!body)
|
|
26
|
+
return;
|
|
27
|
+
try {
|
|
28
|
+
if (typeof body.dump === "function") {
|
|
29
|
+
await body.dump();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (typeof body.resume === "function") {
|
|
33
|
+
body.resume();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (typeof body.destroy === "function") {
|
|
37
|
+
body.destroy();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
/* ignore */
|
|
42
|
+
}
|
|
58
43
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
* @param metrics - Performance metrics object to update
|
|
67
|
-
* @param signal - External AbortSignal for user cancellation
|
|
68
|
-
* @param redirects - Internal redirect counter
|
|
69
|
-
* @param attempt - Internal retry attempt counter
|
|
70
|
-
*/
|
|
71
|
-
async execute(method, url, headers, body, metrics, signal, redirects = 0, attempt = 0) {
|
|
44
|
+
async executeCore(method, url, headers, body, metrics, signal, parser) {
|
|
45
|
+
let currentUrl = url;
|
|
46
|
+
let currentMethod = method;
|
|
47
|
+
let currentHeaders = headers;
|
|
48
|
+
let currentBody = body;
|
|
49
|
+
let redirects = 0;
|
|
50
|
+
let attempt = 0;
|
|
72
51
|
const timeoutController = new AbortController();
|
|
73
52
|
const timer = setTimeout(() => timeoutController.abort(), this.options.timeout);
|
|
74
53
|
const abortHandler = () => timeoutController.abort();
|
|
75
54
|
if (signal) {
|
|
76
55
|
if (signal.aborted) {
|
|
77
56
|
clearTimeout(timer);
|
|
78
|
-
throw new
|
|
57
|
+
throw new errors_1.HttpClientError("Request aborted by user", "ABORTED", 0, undefined, url, method);
|
|
79
58
|
}
|
|
80
|
-
signal.addEventListener("abort", abortHandler);
|
|
59
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
81
60
|
}
|
|
82
61
|
try {
|
|
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
|
-
|
|
62
|
+
while (true) {
|
|
63
|
+
try {
|
|
64
|
+
const config = await this.interceptors.applyRequest({
|
|
65
|
+
url: currentUrl,
|
|
66
|
+
method: currentMethod,
|
|
67
|
+
headers: currentHeaders,
|
|
68
|
+
body: currentBody,
|
|
69
|
+
});
|
|
70
|
+
const res = await (0, undici_1.request)(config.url, {
|
|
71
|
+
method: config.method,
|
|
72
|
+
headers: config.headers,
|
|
73
|
+
body: config.body,
|
|
74
|
+
dispatcher: this.agent,
|
|
75
|
+
signal: timeoutController.signal,
|
|
76
|
+
});
|
|
77
|
+
const status = res.statusCode;
|
|
78
|
+
const resHeaders = res.headers;
|
|
79
|
+
if (this.options.followRedirects &&
|
|
80
|
+
this.redirectStatusCodes.has(status)) {
|
|
81
|
+
if (redirects >= this.options.maxRedirects) {
|
|
82
|
+
await this.drainBody(res.body);
|
|
83
|
+
throw new errors_1.HttpClientError("Too many redirects", "TOO_MANY_REDIRECTS", status);
|
|
84
|
+
}
|
|
85
|
+
const location = resHeaders.location;
|
|
86
|
+
if (location) {
|
|
87
|
+
await this.drainBody(res.body);
|
|
88
|
+
const nextUrl = new URL(location, config.url).toString();
|
|
89
|
+
const nextMethod = status === 303 ? "GET" : currentMethod;
|
|
90
|
+
currentUrl = nextUrl;
|
|
91
|
+
currentMethod = nextMethod;
|
|
92
|
+
currentBody = nextMethod === "GET" ? undefined : currentBody;
|
|
93
|
+
if (nextMethod === "GET") {
|
|
94
|
+
if (currentHeaders["content-type"] ||
|
|
95
|
+
currentHeaders["Content-Type"] ||
|
|
96
|
+
currentHeaders["content-length"] ||
|
|
97
|
+
currentHeaders["Content-Length"]) {
|
|
98
|
+
const nextHeaders = { ...currentHeaders };
|
|
99
|
+
delete nextHeaders["content-type"];
|
|
100
|
+
delete nextHeaders["Content-Type"];
|
|
101
|
+
delete nextHeaders["content-length"];
|
|
102
|
+
delete nextHeaders["Content-Length"];
|
|
103
|
+
currentHeaders = nextHeaders;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
redirects++;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
110
109
|
}
|
|
111
|
-
|
|
110
|
+
if (status >= 500) {
|
|
111
|
+
if (attempt < this.options.maxRetries) {
|
|
112
|
+
if (metrics)
|
|
113
|
+
metrics.retries += 1;
|
|
114
|
+
await this.drainBody(res.body);
|
|
115
|
+
const delay = this.calcDelay(attempt);
|
|
116
|
+
if (delay > 0)
|
|
117
|
+
await this.sleep(delay);
|
|
118
|
+
attempt++;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
throw new errors_1.HttpClientError(`HTTP ${status}`, "HTTP_ERROR", status, undefined, config.url, currentMethod);
|
|
122
|
+
}
|
|
123
|
+
const transformed = await this.interceptors.applyResponse({
|
|
124
|
+
status,
|
|
125
|
+
headers: resHeaders,
|
|
126
|
+
body: res.body,
|
|
127
|
+
url: config.url,
|
|
128
|
+
});
|
|
129
|
+
const parsed = await parser(transformed);
|
|
130
|
+
return {
|
|
131
|
+
status: transformed.status,
|
|
132
|
+
headers: transformed.headers,
|
|
133
|
+
body: parsed,
|
|
134
|
+
url: transformed.url,
|
|
135
|
+
};
|
|
112
136
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
137
|
+
catch (err) {
|
|
138
|
+
if (err?.name === "AbortError") {
|
|
139
|
+
if (signal?.aborted) {
|
|
140
|
+
throw new errors_1.HttpClientError("Request aborted by user", "ABORTED", 0, err, url, method);
|
|
141
|
+
}
|
|
142
|
+
throw new errors_1.TimeoutError(url, this.options.timeout);
|
|
118
143
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
144
|
+
if (attempt < this.options.maxRetries &&
|
|
145
|
+
(err?.code === "ECONNREFUSED" || err?.code === "ETIMEDOUT")) {
|
|
146
|
+
metrics && (metrics.retries += 1);
|
|
147
|
+
await this.sleep(this.calcDelay(attempt));
|
|
148
|
+
attempt++;
|
|
149
|
+
continue;
|
|
124
150
|
}
|
|
125
|
-
|
|
126
|
-
return this.execute(method, url, headers, body, metrics, signal, redirects, attempt + 1);
|
|
151
|
+
throw err;
|
|
127
152
|
}
|
|
128
153
|
}
|
|
129
|
-
return await this.interceptors.applyResponse({
|
|
130
|
-
status: res.statusCode,
|
|
131
|
-
headers: res.headers,
|
|
132
|
-
body: res.body,
|
|
133
|
-
url: config.url,
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
catch (err) {
|
|
137
|
-
clearTimeout(timer);
|
|
138
|
-
if (err.name === "AbortError") {
|
|
139
|
-
if (signal?.aborted)
|
|
140
|
-
throw new Types_1.HttpClientError("Request aborted by user", "ABORTED", 0, err, url, method);
|
|
141
|
-
throw new Types_1.TimeoutError(url, this.options.timeout);
|
|
142
|
-
}
|
|
143
|
-
if (attempt < this.options.maxRetries &&
|
|
144
|
-
(err.code === "ECONNREFUSED" || err.code === "ETIMEDOUT")) {
|
|
145
|
-
if (metrics) {
|
|
146
|
-
metrics.retries += 1;
|
|
147
|
-
}
|
|
148
|
-
await this.sleep(this.calcDelay(attempt));
|
|
149
|
-
return this.execute(method, url, headers, body, metrics, signal, redirects, attempt + 1);
|
|
150
|
-
}
|
|
151
|
-
throw err;
|
|
152
154
|
}
|
|
153
155
|
finally {
|
|
156
|
+
clearTimeout(timer);
|
|
154
157
|
if (signal)
|
|
155
158
|
signal.removeEventListener("abort", abortHandler);
|
|
156
159
|
}
|
|
157
160
|
}
|
|
161
|
+
async execute(method, url, headers, body, metrics, signal) {
|
|
162
|
+
return this.executeCore(method, url, headers, body, metrics, signal, async (res) => res.body);
|
|
163
|
+
}
|
|
158
164
|
}
|
|
159
165
|
exports.RequestExecutor = RequestExecutor;
|
|
160
166
|
//# sourceMappingURL=RequestExecutor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RequestExecutor.js","sourceRoot":"","sources":["../../../src/Hyperttp/Core/RequestExecutor.ts"],"names":[],"mappings":";;;AAAA,mCAAwC;
|
|
1
|
+
{"version":3,"file":"RequestEx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MAAM,GAAG,CAAC;gBACZ,CAAC;YACH,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,IAAI,MAAM;gBAAE,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;IAED,KAAK,CAAC,OAAO,CACX,MAAc,EACd,GAAW,EACX,OAA+B,EAC/B,IAAiC,EACjC,OAAwB,EACxB,MAAoB;QAOpB,OAAO,IAAI,CAAC,WAAW,CACrB,MAAM,EACN,GAAG,EACH,OAAO,EACP,IAAI,EACJ,OAAO,EACP,MAAM,EACN,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CACxB,CAAC;IACJ,CAAC;CACF;AA5PD,0CA4PC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ResponseType, SourceType } from "../../Types/http";
|
|
2
|
+
import { ConversionMeta } from "../../Types/request";
|
|
3
|
+
import { ResponseConverterOptions, ParsedResponse } from "../../Types/response";
|
|
4
|
+
export declare class ResponseConverter {
|
|
5
|
+
private readonly options;
|
|
6
|
+
private readonly xmlParser;
|
|
7
|
+
private readonly xmlBuilder;
|
|
8
|
+
constructor(options?: ResponseConverterOptions);
|
|
9
|
+
readBody(body: any): Promise<Buffer>;
|
|
10
|
+
decodeBody(body: Buffer, encoding?: string, charset?: BufferEncoding): Promise<string>;
|
|
11
|
+
detectSourceType(contentType?: string, text?: string, url?: string): SourceType;
|
|
12
|
+
convert(body: Buffer, targetType: ResponseType, meta?: ConversionMeta): Promise<ParsedResponse>;
|
|
13
|
+
private toAuto;
|
|
14
|
+
private toJson;
|
|
15
|
+
private toXml;
|
|
16
|
+
private toHtml;
|
|
17
|
+
private htmlToJson;
|
|
18
|
+
private safeJsonParse;
|
|
19
|
+
private normalizeResponseShape;
|
|
20
|
+
private escapeXml;
|
|
21
|
+
toBuffer(input: any): Buffer;
|
|
22
|
+
}
|
|
23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