@hyperttp/transport-undici 0.1.0
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/LICENSE +21 -0
- package/README.md +108 -0
- package/index.d.ts +2 -0
- package/index.d.ts.map +1 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/package.json +40 -0
- package/types/index.d.ts +80 -0
- package/types/index.d.ts.map +1 -0
- package/types/index.js +2 -0
- package/types/index.js.map +1 -0
- package/undici.d.ts +64 -0
- package/undici.d.ts.map +1 -0
- package/undici.js +369 -0
- package/undici.js.map +1 -0
- package/utils/helper.d.ts +13 -0
- package/utils/helper.d.ts.map +1 -0
- package/utils/helper.js +103 -0
- package/utils/helper.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 IT IF OR
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# @hyperttp/transport-undici
|
|
2
|
+
|
|
3
|
+
A high-performance network transport layer for the `hyperttp` HTTP client, built directly on top of the low-level
|
|
4
|
+
**Undici Dispatch API**. Engineered specifically for Node.js environments with extreme throughput (RPS) requirements and
|
|
5
|
+
ultra-low latency demands.
|
|
6
|
+
|
|
7
|
+
## ✨ Features
|
|
8
|
+
|
|
9
|
+
- **Undici Dispatch API**: Bypasses the overhead of the standard `fetch` API and Node.js streams by
|
|
10
|
+
collecting chunk buffers directly via low-level pool lifecycle events.
|
|
11
|
+
- **Ultra-Stable p99 Latency**: Drastically reduces Garbage Collector (GC) pressure by optimizing `AbortSignal`
|
|
12
|
+
lifecycle management and minimizing closure allocations per request.
|
|
13
|
+
- **Native Policy Integration**: End-to-end,
|
|
14
|
+
out-of-the-box support for `hyperttp` core mechanisms including Retry Policies,
|
|
15
|
+
smart Redirect Policies, and custom network timeouts.
|
|
16
|
+
- **Safe Event Loop**:
|
|
17
|
+
Isolated abort logic guarantees that connection teardowns never yield unhandled promise rejections or dangling timer macro-tasks.
|
|
18
|
+
|
|
19
|
+
## 📊 Performance (Node.js v24)
|
|
20
|
+
|
|
21
|
+
Benchmark results processing **20,000 requests** (Concurrency: 200) against a local JSON endpoint:
|
|
22
|
+
|
|
23
|
+
| Client | Throughput (RPS) | Latency Avg | p50 | p99 | Peak Heap |
|
|
24
|
+
| :-------------------------------------------- | :--------------- | :---------- | :--------- | :---------- | :---------- |
|
|
25
|
+
| **@hyperttp/core (with Undici Transport)** 🚀 | **22.48K rps** | **8.81ms** | **7.47ms** | **21.10ms** | **49.8 MB** |
|
|
26
|
+
| Pure `undici` | 17.50K rps | 11.36ms | 10.31ms | 39.45ms | 68.0 MB |
|
|
27
|
+
| `axios` | 5.47K rps | 36.35ms | 34.61ms | 62.03ms | 125.0 MB |
|
|
28
|
+
|
|
29
|
+
## 📦 Installation
|
|
30
|
+
|
|
31
|
+
Since this transport is optional, you need to add it to your project manually:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
bun add @hyperttp/transport-undici
|
|
35
|
+
# or
|
|
36
|
+
npm install @hyperttp/transport-undici
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 🚀 Usage
|
|
41
|
+
|
|
42
|
+
### Basic Initialization with Core
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { HyperClient } from "@hyperttp/core";
|
|
46
|
+
import { UndiciTransport } from "@hyperttp/transport-undici";
|
|
47
|
+
|
|
48
|
+
const client = new HyperClient({
|
|
49
|
+
baseUrl: "https://api.example.com",
|
|
50
|
+
transport: new UndiciTransport({
|
|
51
|
+
network: {
|
|
52
|
+
maxConcurrent: 500, // Maximum concurrent sockets
|
|
53
|
+
pipelining: 8, // Request pipelining depth per socket
|
|
54
|
+
keepAliveTimeout: 30000, // Socket keep-alive timeout in ms
|
|
55
|
+
},
|
|
56
|
+
retry: {
|
|
57
|
+
maxRetries: 3,
|
|
58
|
+
retryStatuses: [502, 503, 504],
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const response = await client.request({
|
|
64
|
+
url: "/v1/users",
|
|
65
|
+
method: "GET",
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const users = await response.json();
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Using an External (Custom) Dispatcher
|
|
72
|
+
|
|
73
|
+
If your application already manages a global `undici` Agent or Pool
|
|
74
|
+
(e.g., for proxy configurations or Unix domain sockets), you can inject it directly:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { Pool } from "undici";
|
|
78
|
+
import { UndiciTransport } from "@hyperttp/transport-undici";
|
|
79
|
+
|
|
80
|
+
const customPool = new Pool("http://localhost:3000", {
|
|
81
|
+
connections: 100,
|
|
82
|
+
connect: { rejectUnauthorized: false },
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const transport = new UndiciTransport({
|
|
86
|
+
dispatcher: customPool, // Injecting the existing instance
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
> ⚠️ **Note:** When a `dispatcher` is provided from an external context,
|
|
91
|
+
> `transport.close()` and `transport.destroy()` calls within `hyperttp`
|
|
92
|
+
> are ignored to prevent side effects in the parent environment.
|
|
93
|
+
> Managing the pool lifecycle remains the responsibility of your application architecture.
|
|
94
|
+
|
|
95
|
+
## 🛠 Abort & Timeout Architecture
|
|
96
|
+
|
|
97
|
+
The transport utilizes an atomic `combineSignal` utility that couples the user's external
|
|
98
|
+
`AbortSignal` with an internal task-limiting timer:
|
|
99
|
+
|
|
100
|
+
1. **Dispatch Handler Level**: During the `onResponseData` streaming phase, if the abort signal gets triggered,
|
|
101
|
+
the underlying socket is immediately terminated via `controller.abort()`.
|
|
102
|
+
2. **Error Policy Level**:
|
|
103
|
+
Low-level header timeouts (`UND_ERR_HEADERS_TIMEOUT`) and body timeouts (`UND_ERR_BODY_TIMEOUT`)
|
|
104
|
+
are gracefully normalized into standard `AbortError` instances while preserving the original `cause` for diagnostics.
|
|
105
|
+
|
|
106
|
+
## 📄 License
|
|
107
|
+
|
|
108
|
+
MIT
|
package/index.d.ts
ADDED
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
|
package/index.js
ADDED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hyperttp/transport-undici",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "High-performance, extensible HTTP client for undici",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./index.d.ts",
|
|
9
|
+
"import": "./index.js",
|
|
10
|
+
"require": "./index.js"
|
|
11
|
+
},
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"http",
|
|
16
|
+
"client",
|
|
17
|
+
"high-performance",
|
|
18
|
+
"undici",
|
|
19
|
+
"fetch"
|
|
20
|
+
],
|
|
21
|
+
"author": "dirold2",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/IT-IF-OR/hyperttp-transport-undici.git"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"typescript": "^6",
|
|
29
|
+
"@hyperttp/types": "^0.1.1"
|
|
30
|
+
},
|
|
31
|
+
"peerDependenciesMeta": {
|
|
32
|
+
"typescript": {
|
|
33
|
+
"optional": true
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"undici": "^8.3.0"
|
|
38
|
+
},
|
|
39
|
+
"private": false
|
|
40
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { HttpClientOptions, RetryOptions } from "@hyperttp/types";
|
|
2
|
+
/**
|
|
3
|
+
* @ru Конфигурация сетевого транспорта, расширяющая базовые опции клиента.
|
|
4
|
+
* @en Transport layer configuration expanding the core HTTP client options.
|
|
5
|
+
*/
|
|
6
|
+
export interface TransportConfig extends HttpClientOptions {
|
|
7
|
+
/**
|
|
8
|
+
* @ru Опции политики повторных запросов (ретраев).
|
|
9
|
+
* @en Retry policy behavior and strategy configurations.
|
|
10
|
+
*/
|
|
11
|
+
retry?: RetryOptions;
|
|
12
|
+
/**
|
|
13
|
+
* @ru Низкоуровневые параметры тюнинга сетевых соединений сокетов.
|
|
14
|
+
* @en Low-level parameters optimized for underlying network socket connection tuning.
|
|
15
|
+
*/
|
|
16
|
+
network?: {
|
|
17
|
+
/**
|
|
18
|
+
* @ru Максимальное количество параллельно открытых сокетов в пуле.
|
|
19
|
+
* @en Maximum concurrent open sockets allocated within the connection pool.
|
|
20
|
+
*/
|
|
21
|
+
maxConcurrent?: number;
|
|
22
|
+
/**
|
|
23
|
+
* @ru Глубина конвейеризации HTTP-запросов (pipelining) внутри одного сокета.
|
|
24
|
+
* @en HTTP pipelining factor specifying the maximum pending requests per single socket.
|
|
25
|
+
*/
|
|
26
|
+
pipelining?: number;
|
|
27
|
+
/**
|
|
28
|
+
* @ru Время удержания простаивающего сокета в миллисекундах (Keep-Alive).
|
|
29
|
+
* @en Inactivity timeout in milliseconds keeping idle pool sockets alive.
|
|
30
|
+
*/
|
|
31
|
+
keepAliveTimeout?: number;
|
|
32
|
+
/**
|
|
33
|
+
* @ru Предельное время ожидания ответа на запрос (таймаут операции) в миллисекундах.
|
|
34
|
+
* @en High-level timeout threshold in milliseconds covering the entire lifecycle of a request.
|
|
35
|
+
*/
|
|
36
|
+
timeout?: number;
|
|
37
|
+
/**
|
|
38
|
+
* @ru Флаг автоматического следования HTTP-редиректам (3xx коды).
|
|
39
|
+
* @en Determines whether the engine should transparently follow HTTP redirect signatures (3xx).
|
|
40
|
+
*/
|
|
41
|
+
followRedirects?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* @ru Максимально допустимое количество последовательных перенаправлений.
|
|
44
|
+
* @en Boundary limit protecting execution context from infinite redirect cycles.
|
|
45
|
+
*/
|
|
46
|
+
maxRedirects?: number;
|
|
47
|
+
/**
|
|
48
|
+
* @ru Проверка валидности SSL/TLS сертификатов удаленного сервера.
|
|
49
|
+
* @en Determines whether to verify the remote host's SSL/TLS certificate integrity chain.
|
|
50
|
+
*/
|
|
51
|
+
rejectUnauthorized?: boolean;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @ru Внутренний атомарный результат низкоуровневой диспетчеризации запроса.
|
|
56
|
+
* @en Internal atomic result produced by the low-level network dispatch driver.
|
|
57
|
+
*/
|
|
58
|
+
export type DispatchResult = {
|
|
59
|
+
/**
|
|
60
|
+
* @ru HTTP статус-код ответа сервера.
|
|
61
|
+
* @en HTTP numeric status code returned by the remote origin.
|
|
62
|
+
*/
|
|
63
|
+
status: number;
|
|
64
|
+
/**
|
|
65
|
+
* @ru Нормализованные заголовки ответа.
|
|
66
|
+
* @en Normalized dictionary representation containing response headers.
|
|
67
|
+
*/
|
|
68
|
+
headers: Record<string, string>;
|
|
69
|
+
/**
|
|
70
|
+
* @ru Сырой буфер бинарных данных тела ответа.
|
|
71
|
+
* @en Raw binary buffer containing the fully aggregated response payload.
|
|
72
|
+
*/
|
|
73
|
+
body: Buffer;
|
|
74
|
+
/**
|
|
75
|
+
* @ru Финальный абсолютный URL (с учетом возможных редиректов).
|
|
76
|
+
* @en Absolute target URL pointing to where the response was ultimately extracted from.
|
|
77
|
+
*/
|
|
78
|
+
url: string;
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEvE;;;GAGG;AACH,MAAM,WAAW,eAAgB,SAAQ,iBAAiB;IACxD;;;OAGG;IACH,KAAK,CAAC,EAAE,YAAY,CAAC;IAErB;;;OAGG;IACH,OAAO,CAAC,EAAE;QACR;;;WAGG;QACH,aAAa,CAAC,EAAE,MAAM,CAAC;QAEvB;;;WAGG;QACH,UAAU,CAAC,EAAE,MAAM,CAAC;QAEpB;;;WAGG;QACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;QAE1B;;;WAGG;QACH,OAAO,CAAC,EAAE,MAAM,CAAC;QAEjB;;;WAGG;QACH,eAAe,CAAC,EAAE,OAAO,CAAC;QAE1B;;;WAGG;QACH,YAAY,CAAC,EAAE,MAAM,CAAC;QAEtB;;;WAGG;QACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;KAC9B,CAAC;CACH;AAED;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEhC;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAC;CACb,CAAC"}
|
package/types/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
|
package/undici.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Dispatcher } from "undici";
|
|
2
|
+
import type { HyperTransport, RetryOptions, TransportRequest, TransportResponse } from "@hyperttp/types";
|
|
3
|
+
import type { DispatchResult, TransportConfig } from "./types/index.js";
|
|
4
|
+
declare module "@hyperttp/types" {
|
|
5
|
+
interface HttpClientOptions {
|
|
6
|
+
/**
|
|
7
|
+
* @ru Базовый URL для резолва относительных путей запросов.
|
|
8
|
+
* @en Base URL used to resolve relative request paths.
|
|
9
|
+
*/
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
/**
|
|
12
|
+
* @ru Экземпляр диспетчера Undici (Pool, Agent, Client) для низкоуровневой настройки сетевого пула.
|
|
13
|
+
* @en An Undici dispatcher instance (Pool, Agent, or Client) for low-level network pool tuning.
|
|
14
|
+
*/
|
|
15
|
+
dispatcher?: Dispatcher;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export declare function normalizeHeaders(headers: unknown): Record<string, string>;
|
|
19
|
+
export declare function isRedirect(status: number): boolean;
|
|
20
|
+
export declare function shouldRetry(status: number, retryOptions: RetryOptions): boolean;
|
|
21
|
+
export declare function calcDelay(attempt: number, retryOptions: RetryOptions): number;
|
|
22
|
+
export declare function drainBody(body: unknown): Promise<void>;
|
|
23
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
24
|
+
export declare function combineSignal(signal: AbortSignal | undefined, timeoutMs: number): {
|
|
25
|
+
signal?: AbortSignal;
|
|
26
|
+
cancelTimer: () => void;
|
|
27
|
+
isTimeoutAbort: () => boolean;
|
|
28
|
+
};
|
|
29
|
+
export declare class UndiciDispatchHandler implements Dispatcher.DispatchHandler {
|
|
30
|
+
private readonly resolve;
|
|
31
|
+
private readonly reject;
|
|
32
|
+
private readonly url;
|
|
33
|
+
private readonly signal?;
|
|
34
|
+
private statusCode;
|
|
35
|
+
private headers;
|
|
36
|
+
private chunks;
|
|
37
|
+
constructor(resolve: (value: DispatchResult) => void, reject: (reason: Error) => void, url: string, signal?: AbortSignal | undefined);
|
|
38
|
+
onRequestStart(_controller: Dispatcher.DispatchController, _context: unknown): void;
|
|
39
|
+
onResponseStart(_controller: Dispatcher.DispatchController, statusCode: number, headers: unknown, _statusMessage?: string): void;
|
|
40
|
+
onResponseData(controller: Dispatcher.DispatchController, chunk: Buffer): void;
|
|
41
|
+
onResponseEnd(_controller: Dispatcher.DispatchController, _trailers: Record<string, string>): void;
|
|
42
|
+
onResponseError(_controller: Dispatcher.DispatchController, error: Error): void;
|
|
43
|
+
}
|
|
44
|
+
export declare class UndiciTransport implements HyperTransport {
|
|
45
|
+
config: TransportConfig;
|
|
46
|
+
private readonly pool;
|
|
47
|
+
private readonly isExternal;
|
|
48
|
+
constructor(config: TransportConfig);
|
|
49
|
+
private get baseUrl();
|
|
50
|
+
private get timeout();
|
|
51
|
+
private get followRedirects();
|
|
52
|
+
private get maxRedirects();
|
|
53
|
+
private get retryOptions();
|
|
54
|
+
private get maxRetries();
|
|
55
|
+
execute(req: TransportRequest): Promise<TransportResponse>;
|
|
56
|
+
private executeWithPolicy;
|
|
57
|
+
private dispatchOnce;
|
|
58
|
+
private createResponse;
|
|
59
|
+
private isAbortError;
|
|
60
|
+
private getErrorCode;
|
|
61
|
+
close(): Promise<void>;
|
|
62
|
+
destroy(): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=undici.d.ts.map
|
package/undici.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"undici.d.ts","sourceRoot":"","sources":["../src/undici.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,UAAU,EAAE,MAAM,QAAQ,CAAC;AAE1C,OAAO,KAAK,EACV,cAAc,EAEd,YAAY,EACZ,gBAAgB,EAChB,iBAAiB,EAElB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAaxE,OAAO,QAAQ,iBAAiB,CAAC;IAC/B,UAAU,iBAAiB;QACzB;;;WAGG;QACH,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB;;;WAGG;QACH,UAAU,CAAC,EAAE,UAAU,CAAC;KACzB;CACF;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAiCzE;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAElD;AAED,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,YAAY,GACzB,OAAO,CAOT;AAED,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY,GAAG,MAAM,CAK7E;AAED,wBAAsB,SAAS,CAAC,IAAI,EAAE,OAAO,G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IAYA,eAAe,CACpB,WAAW,EAAE,UAAU,CAAC,kBAAkB,EAC1C,KAAK,EAAE,KAAK,GACX,IAAI;CAGR;AAED,qBAAa,eAAgB,YAAW,cAAc;IAC7C,MAAM,EAAE,eAAe,CAAC;IAC/B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAa;IAClC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAU;gBAEzB,MAAM,EAAE,eAAe;IAanC,OAAO,KAAK,OAAO,GAElB;IAED,OAAO,KAAK,OAAO,GAElB;IAED,OAAO,KAAK,eAAe,GAE1B;IAED,OAAO,KAAK,YAAY,GAEvB;IAED,OAAO,KAAK,YAAY,GAEvB;IAED,OAAO,KAAK,UAAU,GAErB;IAEY,OAAO,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC;YAUzD,iBAAiB;YA4GjB,YAAY;IAiC1B,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,YAAY;IAWpB,OAAO,CAAC,YAAY;IAMP,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAKtB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAItC"}
|
package/undici.js
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { Pool, Dispatcher } from "undici";
|
|
2
|
+
import { Readable } from "node:stream";
|
|
3
|
+
const REDIRECT_STATUS_CODES = new Set([301, 302, 303, 307, 308]);
|
|
4
|
+
const DEFAULT_RETRY_STATUS_CODES = [502, 503, 504];
|
|
5
|
+
const RETRYABLE_ERROR_CODES = new Set([
|
|
6
|
+
"ECONNREFUSED",
|
|
7
|
+
"ETIMEDOUT",
|
|
8
|
+
"ECONNRESET",
|
|
9
|
+
"EPIPE",
|
|
10
|
+
"UND_ERR_SOCKET",
|
|
11
|
+
]);
|
|
12
|
+
export function normalizeHeaders(headers) {
|
|
13
|
+
const out = Object.create(null);
|
|
14
|
+
if (!headers)
|
|
15
|
+
return out;
|
|
16
|
+
if (Array.isArray(headers)) {
|
|
17
|
+
for (let i = 0; i < headers.length; i += 2) {
|
|
18
|
+
const key = headers[i].toLowerCase();
|
|
19
|
+
if (!key)
|
|
20
|
+
continue;
|
|
21
|
+
const value = headers[i + 1];
|
|
22
|
+
if (value === undefined || value === null)
|
|
23
|
+
continue;
|
|
24
|
+
out[key] = typeof value === "string" ? value : String(value);
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
const headerObj = headers;
|
|
29
|
+
for (const [key, val] of Object.entries(headerObj)) {
|
|
30
|
+
if (val === undefined)
|
|
31
|
+
continue;
|
|
32
|
+
const lower = key.toLowerCase();
|
|
33
|
+
if (Array.isArray(val)) {
|
|
34
|
+
out[lower] = lower === "set-cookie" ? val.join("\n") : val.join(", ");
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
out[lower] = String(val);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
export function isRedirect(status) {
|
|
43
|
+
return REDIRECT_STATUS_CODES.has(status);
|
|
44
|
+
}
|
|
45
|
+
export function shouldRetry(status, retryOptions) {
|
|
46
|
+
const codes = retryOptions.retryStatusCodes;
|
|
47
|
+
if (codes && codes.length > 0) {
|
|
48
|
+
return codes.includes(status);
|
|
49
|
+
}
|
|
50
|
+
return DEFAULT_RETRY_STATUS_CODES.includes(status);
|
|
51
|
+
}
|
|
52
|
+
export function calcDelay(attempt, retryOptions) {
|
|
53
|
+
const { baseDelay = 1000, maxDelay = 10000, jitter = true } = retryOptions;
|
|
54
|
+
const base = Math.min(baseDelay * 2 ** attempt, maxDelay);
|
|
55
|
+
return jitter ? base * (0.75 + Math.random() * 0.5) : base;
|
|
56
|
+
}
|
|
57
|
+
export async function drainBody(body) {
|
|
58
|
+
if (!body || typeof body !== "object")
|
|
59
|
+
return;
|
|
60
|
+
try {
|
|
61
|
+
const stream = body;
|
|
62
|
+
if (typeof stream.dump === "function") {
|
|
63
|
+
await stream.dump();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (typeof stream.resume === "function") {
|
|
67
|
+
stream.resume();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (typeof stream.destroy === "function") {
|
|
71
|
+
stream.destroy();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// ignore disposal noise
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export function sleep(ms) {
|
|
79
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
80
|
+
}
|
|
81
|
+
export function combineSignal(signal, timeoutMs) {
|
|
82
|
+
if (timeoutMs <= 0) {
|
|
83
|
+
return {
|
|
84
|
+
signal,
|
|
85
|
+
cancelTimer: () => { },
|
|
86
|
+
isTimeoutAbort: () => false,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
const timeoutController = new AbortController();
|
|
90
|
+
const timer = setTimeout(() => timeoutController.abort(), timeoutMs);
|
|
91
|
+
let combinedSignal;
|
|
92
|
+
if (signal) {
|
|
93
|
+
if (typeof AbortSignal.any === "function") {
|
|
94
|
+
combinedSignal = AbortSignal.any([signal, timeoutController.signal]);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
signal.addEventListener("abort", () => timeoutController.abort(), {
|
|
98
|
+
once: true,
|
|
99
|
+
});
|
|
100
|
+
combinedSignal = timeoutController.signal;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
combinedSignal = timeoutController.signal;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
signal: combinedSignal,
|
|
108
|
+
cancelTimer: () => clearTimeout(timer),
|
|
109
|
+
isTimeoutAbort: () => timeoutController.signal.aborted,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
class UndiciTransportResponse {
|
|
113
|
+
status;
|
|
114
|
+
headers;
|
|
115
|
+
url;
|
|
116
|
+
body;
|
|
117
|
+
baseUrl;
|
|
118
|
+
_rawBody;
|
|
119
|
+
_cachedText;
|
|
120
|
+
_cachedJson;
|
|
121
|
+
_cachedJsonReady = false;
|
|
122
|
+
constructor(result) {
|
|
123
|
+
this.status = result.status;
|
|
124
|
+
this.headers = result.headers;
|
|
125
|
+
this.url = result.url;
|
|
126
|
+
this._rawBody = result.body;
|
|
127
|
+
this.body = Readable.from([
|
|
128
|
+
result.body,
|
|
129
|
+
]);
|
|
130
|
+
this.baseUrl = result.url;
|
|
131
|
+
}
|
|
132
|
+
async text() {
|
|
133
|
+
if (this._cachedText === undefined) {
|
|
134
|
+
this._cachedText = this._rawBody.toString("utf-8");
|
|
135
|
+
}
|
|
136
|
+
return this._cachedText;
|
|
137
|
+
}
|
|
138
|
+
async json() {
|
|
139
|
+
if (this._cachedJsonReady) {
|
|
140
|
+
return this._cachedJson;
|
|
141
|
+
}
|
|
142
|
+
const text = await this.text();
|
|
143
|
+
if (!text.trim()) {
|
|
144
|
+
this._cachedJson = null;
|
|
145
|
+
this._cachedJsonReady = true;
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
this._cachedJson = JSON.parse(text);
|
|
149
|
+
this._cachedJsonReady = true;
|
|
150
|
+
return this._cachedJson;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export class UndiciDispatchHandler {
|
|
154
|
+
resolve;
|
|
155
|
+
reject;
|
|
156
|
+
url;
|
|
157
|
+
signal;
|
|
158
|
+
statusCode = 200;
|
|
159
|
+
headers = Object.create(null);
|
|
160
|
+
chunks = [];
|
|
161
|
+
constructor(resolve, reject, url, signal) {
|
|
162
|
+
this.resolve = resolve;
|
|
163
|
+
this.reject = reject;
|
|
164
|
+
this.url = url;
|
|
165
|
+
this.signal = signal;
|
|
166
|
+
}
|
|
167
|
+
onRequestStart(_controller, _context) { }
|
|
168
|
+
onResponseStart(_controller, statusCode, headers, _statusMessage) {
|
|
169
|
+
this.statusCode = statusCode;
|
|
170
|
+
this.headers = normalizeHeaders(headers);
|
|
171
|
+
}
|
|
172
|
+
onResponseData(controller, chunk) {
|
|
173
|
+
if (this.signal?.aborted) {
|
|
174
|
+
const abortError = new Error("The operation was aborted.");
|
|
175
|
+
abortError.name = "AbortError";
|
|
176
|
+
controller.abort(abortError);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
this.chunks.push(chunk);
|
|
180
|
+
}
|
|
181
|
+
onResponseEnd(_controller, _trailers) {
|
|
182
|
+
const body = this.chunks.length === 0 ? Buffer.alloc(0) : Buffer.concat(this.chunks);
|
|
183
|
+
this.resolve({
|
|
184
|
+
status: this.statusCode,
|
|
185
|
+
headers: this.headers,
|
|
186
|
+
body,
|
|
187
|
+
url: this.url,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
onResponseError(_controller, error) {
|
|
191
|
+
this.reject(error);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export class UndiciTransport {
|
|
195
|
+
config;
|
|
196
|
+
pool;
|
|
197
|
+
isExternal;
|
|
198
|
+
constructor(config) {
|
|
199
|
+
this.config = config;
|
|
200
|
+
this.isExternal = config.dispatcher !== undefined;
|
|
201
|
+
this.pool =
|
|
202
|
+
config.dispatcher ??
|
|
203
|
+
new Pool(this.baseUrl, {
|
|
204
|
+
connections: config.network?.maxConcurrent ?? 500,
|
|
205
|
+
pipelining: config.network?.pipelining ?? 8,
|
|
206
|
+
keepAliveTimeout: config.network?.keepAliveTimeout ?? 30000,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
get baseUrl() {
|
|
210
|
+
return this.config.baseUrl ?? "http://localhost:3000";
|
|
211
|
+
}
|
|
212
|
+
get timeout() {
|
|
213
|
+
return this.config.network?.timeout ?? 30000;
|
|
214
|
+
}
|
|
215
|
+
get followRedirects() {
|
|
216
|
+
return this.config.network?.followRedirects ?? true;
|
|
217
|
+
}
|
|
218
|
+
get maxRedirects() {
|
|
219
|
+
return this.config.network?.maxRedirects ?? 5;
|
|
220
|
+
}
|
|
221
|
+
get retryOptions() {
|
|
222
|
+
return this.config.retry ?? {};
|
|
223
|
+
}
|
|
224
|
+
get maxRetries() {
|
|
225
|
+
return this.config.retry?.maxRetries ?? 3;
|
|
226
|
+
}
|
|
227
|
+
async execute(req) {
|
|
228
|
+
return this.executeWithPolicy({
|
|
229
|
+
url: req.url,
|
|
230
|
+
method: req.method,
|
|
231
|
+
headers: { ...req.headers }, // Защита от мутаций оригинального объекта
|
|
232
|
+
body: req.body,
|
|
233
|
+
signal: req.signal,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
async executeWithPolicy(req) {
|
|
237
|
+
let currentUrl = req.url;
|
|
238
|
+
let currentMethod = req.method;
|
|
239
|
+
let currentHeaders = req.headers;
|
|
240
|
+
let currentBody = req.body;
|
|
241
|
+
let redirects = 0;
|
|
242
|
+
let attempt = 0;
|
|
243
|
+
while (true) {
|
|
244
|
+
const { signal, cancelTimer, isTimeoutAbort } = combineSignal(req.signal, this.timeout);
|
|
245
|
+
try {
|
|
246
|
+
const result = await this.dispatchOnce({
|
|
247
|
+
...req,
|
|
248
|
+
url: currentUrl,
|
|
249
|
+
method: currentMethod,
|
|
250
|
+
headers: currentHeaders,
|
|
251
|
+
body: currentBody,
|
|
252
|
+
signal,
|
|
253
|
+
});
|
|
254
|
+
if (this.followRedirects && isRedirect(result.status)) {
|
|
255
|
+
if (redirects >= this.maxRedirects) {
|
|
256
|
+
await drainBody(result.body);
|
|
257
|
+
throw new Error("Too many redirects");
|
|
258
|
+
}
|
|
259
|
+
const location = result.headers.location;
|
|
260
|
+
if (location) {
|
|
261
|
+
await drainBody(result.body);
|
|
262
|
+
const nextUrl = new URL(location, result.url).toString();
|
|
263
|
+
let nextMethod = currentMethod;
|
|
264
|
+
if (result.status === 303 ||
|
|
265
|
+
((result.status === 301 || result.status === 302) &&
|
|
266
|
+
currentMethod === "POST")) {
|
|
267
|
+
nextMethod = "GET";
|
|
268
|
+
}
|
|
269
|
+
currentUrl = nextUrl;
|
|
270
|
+
currentMethod = nextMethod;
|
|
271
|
+
currentBody = nextMethod === "GET" ? undefined : currentBody;
|
|
272
|
+
if (nextMethod === "GET") {
|
|
273
|
+
const nextHeaders = { ...currentHeaders };
|
|
274
|
+
delete nextHeaders["content-type"];
|
|
275
|
+
delete nextHeaders["content-length"];
|
|
276
|
+
currentHeaders = nextHeaders;
|
|
277
|
+
}
|
|
278
|
+
redirects += 1;
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (shouldRetry(result.status, this.retryOptions)) {
|
|
283
|
+
if (attempt < this.maxRetries) {
|
|
284
|
+
await drainBody(result.body);
|
|
285
|
+
await sleep(calcDelay(attempt, this.retryOptions));
|
|
286
|
+
attempt += 1;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return this.createResponse(result);
|
|
291
|
+
}
|
|
292
|
+
catch (err) {
|
|
293
|
+
if (this.isAbortError(err)) {
|
|
294
|
+
if (req.signal?.aborted) {
|
|
295
|
+
throw err;
|
|
296
|
+
}
|
|
297
|
+
if (isTimeoutAbort()) {
|
|
298
|
+
throw new Error(`Request timeout after ${this.timeout}ms`, {
|
|
299
|
+
cause: err,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
throw new Error("Transport closed or aborted", { cause: err });
|
|
303
|
+
}
|
|
304
|
+
const code = this.getErrorCode(err);
|
|
305
|
+
if (attempt < this.maxRetries &&
|
|
306
|
+
code &&
|
|
307
|
+
RETRYABLE_ERROR_CODES.has(code)) {
|
|
308
|
+
await sleep(calcDelay(attempt, this.retryOptions));
|
|
309
|
+
attempt += 1;
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
throw err;
|
|
313
|
+
}
|
|
314
|
+
finally {
|
|
315
|
+
cancelTimer();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
async dispatchOnce(req) {
|
|
320
|
+
const fullUrl = new URL(req.url, this.baseUrl);
|
|
321
|
+
const dispatchOptions = {
|
|
322
|
+
origin: fullUrl.origin,
|
|
323
|
+
path: fullUrl.pathname + fullUrl.search,
|
|
324
|
+
method: req.method,
|
|
325
|
+
headers: req.headers,
|
|
326
|
+
body: req.body,
|
|
327
|
+
signal: req.signal,
|
|
328
|
+
};
|
|
329
|
+
if (req.signal?.aborted) {
|
|
330
|
+
const abortError = new Error("The operation was aborted.");
|
|
331
|
+
abortError.name = "AbortError";
|
|
332
|
+
throw abortError;
|
|
333
|
+
}
|
|
334
|
+
return new Promise((resolve, reject) => {
|
|
335
|
+
// Полностью полагаемся на внутренний механизм отмены undici через dispatchOptions.signal.
|
|
336
|
+
// Это предотвращает двойной reject и утечки памяти.
|
|
337
|
+
this.pool.dispatch(dispatchOptions, new UndiciDispatchHandler(resolve, reject, fullUrl.toString(), req.signal));
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
createResponse(result) {
|
|
341
|
+
return new UndiciTransportResponse(result);
|
|
342
|
+
}
|
|
343
|
+
isAbortError(err) {
|
|
344
|
+
if (!err || typeof err !== "object")
|
|
345
|
+
return false;
|
|
346
|
+
const name = err.name;
|
|
347
|
+
const code = err.code;
|
|
348
|
+
return (name === "AbortError" ||
|
|
349
|
+
code === "UND_ERR_HEADERS_TIMEOUT" ||
|
|
350
|
+
code === "UND_ERR_BODY_TIMEOUT");
|
|
351
|
+
}
|
|
352
|
+
getErrorCode(err) {
|
|
353
|
+
if (!err || typeof err !== "object")
|
|
354
|
+
return undefined;
|
|
355
|
+
const code = err.code;
|
|
356
|
+
return typeof code === "string" ? code : undefined;
|
|
357
|
+
}
|
|
358
|
+
async close() {
|
|
359
|
+
if (this.isExternal)
|
|
360
|
+
return;
|
|
361
|
+
await this.pool.close();
|
|
362
|
+
}
|
|
363
|
+
async destroy() {
|
|
364
|
+
if (this.isExternal)
|
|
365
|
+
return;
|
|
366
|
+
await this.pool.destroy();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
//# sourceMappingURL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AAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IACrD,CAAC;IAEM,KAAK,CAAC,KAAK;QAChB,IAAI,IAAI,CAAC,UAAU;YAAE,OAAO;QAC5B,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;IAEM,KAAK,CAAC,OAAO;QAClB,IAAI,IAAI,CAAC,UAAU;YAAE,OAAO;QAC5B,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;IAC5B,CAAC;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RetryOptions } from "@hyperttp/types";
|
|
2
|
+
export declare function normalizeHeaders(headers: unknown): Record<string, string>;
|
|
3
|
+
export declare function isRedirect(status: number): boolean;
|
|
4
|
+
export declare function shouldRetry(status: number, retryOptions: RetryOptions): boolean;
|
|
5
|
+
export declare function calcDelay(attempt: number, retryOptions: RetryOptions): number;
|
|
6
|
+
export declare function drainBody(body: unknown): Promise<void>;
|
|
7
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
8
|
+
export declare function combineSignal(signal: AbortSignal | undefined, timeoutMs: number): {
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
cancelTimer: () => void;
|
|
11
|
+
isTimeoutAbort: () => boolean;
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/utils/helper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAKpD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAiCzE;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAElD;AAED,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,YAAY,GACzB,OAAO,CAOT;AAED,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,YAAY,GAAG,MAAM,CAK7E;AAED,wBAAsB,SAAS,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAsB5D;AAED,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/C;AAED,wBAAgB,aAAa,CAC3B,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,SAAS,EAAE,MAAM,GAChB;IACD,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,WAAW,EAAE,MAAM,IAAI,CAAC;IACxB,cAAc,EAAE,MAAM,OAAO,CAAC;CAC/B,CA+BA"}
|
package/utils/helper.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
const REDIRECT_STATUS_CODES = new Set([301, 302, 303, 307, 308]);
|
|
2
|
+
const DEFAULT_RETRY_STATUS_CODES = [502, 503, 504];
|
|
3
|
+
export function normalizeHeaders(headers) {
|
|
4
|
+
const out = Object.create(null);
|
|
5
|
+
if (!headers)
|
|
6
|
+
return out;
|
|
7
|
+
if (Array.isArray(headers)) {
|
|
8
|
+
for (let i = 0; i < headers.length; i += 2) {
|
|
9
|
+
const key = headers[i].toLowerCase();
|
|
10
|
+
if (!key)
|
|
11
|
+
continue;
|
|
12
|
+
const value = headers[i + 1];
|
|
13
|
+
if (value === undefined || value === null)
|
|
14
|
+
continue;
|
|
15
|
+
out[key] = typeof value === "string" ? value : String(value);
|
|
16
|
+
}
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
const headerObj = headers;
|
|
20
|
+
for (const [key, val] of Object.entries(headerObj)) {
|
|
21
|
+
if (val === undefined)
|
|
22
|
+
continue;
|
|
23
|
+
const lower = key.toLowerCase();
|
|
24
|
+
if (Array.isArray(val)) {
|
|
25
|
+
out[lower] = lower === "set-cookie" ? val.join("\n") : val.join(", ");
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
out[lower] = String(val);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
export function isRedirect(status) {
|
|
34
|
+
return REDIRECT_STATUS_CODES.has(status);
|
|
35
|
+
}
|
|
36
|
+
export function shouldRetry(status, retryOptions) {
|
|
37
|
+
const codes = retryOptions.retryStatusCodes;
|
|
38
|
+
if (codes && codes.length > 0) {
|
|
39
|
+
return codes.includes(status);
|
|
40
|
+
}
|
|
41
|
+
return DEFAULT_RETRY_STATUS_CODES.includes(status);
|
|
42
|
+
}
|
|
43
|
+
export function calcDelay(attempt, retryOptions) {
|
|
44
|
+
const { baseDelay = 1000, maxDelay = 10000, jitter = true } = retryOptions;
|
|
45
|
+
const base = Math.min(baseDelay * 2 ** attempt, maxDelay);
|
|
46
|
+
return jitter ? base * (0.75 + Math.random() * 0.5) : base;
|
|
47
|
+
}
|
|
48
|
+
export async function drainBody(body) {
|
|
49
|
+
if (!body || typeof body !== "object")
|
|
50
|
+
return;
|
|
51
|
+
try {
|
|
52
|
+
const stream = body;
|
|
53
|
+
if (typeof stream.dump === "function") {
|
|
54
|
+
await stream.dump();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (typeof stream.resume === "function") {
|
|
58
|
+
stream.resume();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (typeof stream.destroy === "function") {
|
|
62
|
+
stream.destroy();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// ignore disposal noise
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export function sleep(ms) {
|
|
70
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
71
|
+
}
|
|
72
|
+
export function combineSignal(signal, timeoutMs) {
|
|
73
|
+
if (timeoutMs <= 0) {
|
|
74
|
+
return {
|
|
75
|
+
signal,
|
|
76
|
+
cancelTimer: () => { },
|
|
77
|
+
isTimeoutAbort: () => false,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
const timeoutController = new AbortController();
|
|
81
|
+
const timer = setTimeout(() => timeoutController.abort(), timeoutMs);
|
|
82
|
+
let combinedSignal;
|
|
83
|
+
if (signal) {
|
|
84
|
+
if (typeof AbortSignal.any === "function") {
|
|
85
|
+
combinedSignal = AbortSignal.any([signal, timeoutController.signal]);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
signal.addEventListener("abort", () => timeoutController.abort(), {
|
|
89
|
+
once: true,
|
|
90
|
+
});
|
|
91
|
+
combinedSignal = timeoutController.signal;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
combinedSignal = timeoutController.signal;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
signal: combinedSignal,
|
|
99
|
+
cancelTimer: () => clearTimeout(timer),
|
|
100
|
+
isTimeoutAbort: () => timeoutController.signal.aborted,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../src/utils/helper.ts"],"names":[],"mappings":"AAEA,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AACjE,MAAM,0BAA0B,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAEnD,MAAM,UAAU,gBAAgB,CAAC,OAAgB;IAC/C,MAAM,GAAG,GAA2B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAExD,IAAI,CAAC,OAAO;QAAE,OAAO,GAAG,CAAC;IAEzB,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CA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