stealth-fetch 0.1.2
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 +183 -0
- package/dist/client.d.ts +99 -0
- package/dist/client.js +1117 -0
- package/dist/compat/web.d.ts +15 -0
- package/dist/compat/web.js +31 -0
- package/dist/connection-pool.d.ts +39 -0
- package/dist/connection-pool.js +84 -0
- package/dist/dns-cache.d.ts +25 -0
- package/dist/dns-cache.js +44 -0
- package/dist/http1/chunked.d.ts +35 -0
- package/dist/http1/chunked.js +87 -0
- package/dist/http1/client.d.ts +28 -0
- package/dist/http1/client.js +289 -0
- package/dist/http1/parser.d.ts +29 -0
- package/dist/http1/parser.js +78 -0
- package/dist/http2/client.d.ts +64 -0
- package/dist/http2/client.js +97 -0
- package/dist/http2/connection.d.ts +125 -0
- package/dist/http2/connection.js +666 -0
- package/dist/http2/constants.d.ts +72 -0
- package/dist/http2/constants.js +74 -0
- package/dist/http2/flow-control.d.ts +32 -0
- package/dist/http2/flow-control.js +76 -0
- package/dist/http2/framer.d.ts +47 -0
- package/dist/http2/framer.js +133 -0
- package/dist/http2/hpack.d.ts +54 -0
- package/dist/http2/hpack.js +186 -0
- package/dist/http2/parser.d.ts +35 -0
- package/dist/http2/parser.js +72 -0
- package/dist/http2/stream.d.ts +72 -0
- package/dist/http2/stream.js +252 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +33 -0
- package/dist/protocol-cache.d.ts +14 -0
- package/dist/protocol-cache.js +29 -0
- package/dist/socket/adapter.d.ts +59 -0
- package/dist/socket/adapter.js +145 -0
- package/dist/socket/nat64.d.ts +69 -0
- package/dist/socket/nat64.js +196 -0
- package/dist/socket/tls.d.ts +28 -0
- package/dist/socket/tls.js +33 -0
- package/dist/socket/wasm-pkg/wasm_tls.d.ts +107 -0
- package/dist/socket/wasm-pkg/wasm_tls.js +568 -0
- package/dist/socket/wasm-pkg/wasm_tls_bg.wasm +0 -0
- package/dist/socket/wasm-pkg/wasm_tls_bg.wasm.d.ts +20 -0
- package/dist/socket/wasm-tls-adapter.d.ts +40 -0
- package/dist/socket/wasm-tls-adapter.js +102 -0
- package/dist/socket/wasm-tls-bridge.d.ts +30 -0
- package/dist/socket/wasm-tls-bridge.js +187 -0
- package/dist/utils/headers.d.ts +21 -0
- package/dist/utils/headers.js +36 -0
- package/dist/utils/url.d.ts +16 -0
- package/dist/utils/url.js +12 -0
- package/package.json +87 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 0XwX
|
|
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,183 @@
|
|
|
1
|
+
# stealth-fetch
|
|
2
|
+
|
|
3
|
+
HTTP/1.1 + HTTP/2 client for Cloudflare Workers built on `cloudflare:sockets`.
|
|
4
|
+
It avoids automatic `cf-*` header injection by using raw TCP sockets.
|
|
5
|
+
|
|
6
|
+
## Highlights
|
|
7
|
+
|
|
8
|
+
- HTTP/1.1 + HTTP/2 with ALPN negotiation
|
|
9
|
+
- WASM TLS (rustls) for TLS 1.2/1.3 + ALPN control
|
|
10
|
+
- HTTP/2 connection pooling and protocol cache
|
|
11
|
+
- NAT64 fallback for blocked outbound connections
|
|
12
|
+
- Redirects, retries, and timeouts
|
|
13
|
+
- Raw headers preserved (order + multi-value)
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add stealth-fetch
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { request } from "stealth-fetch";
|
|
25
|
+
|
|
26
|
+
const response = await request("https://api.example.com/v1/data", {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: {
|
|
29
|
+
Authorization: "Bearer sk-...",
|
|
30
|
+
"Content-Type": "application/json",
|
|
31
|
+
},
|
|
32
|
+
body: JSON.stringify({
|
|
33
|
+
model: "gpt-4",
|
|
34
|
+
messages: [{ role: "user", content: "Hello" }],
|
|
35
|
+
}),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const data = await response.json();
|
|
39
|
+
console.log(data);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Web Response Compatibility
|
|
43
|
+
|
|
44
|
+
If you need a standard Web `Response` object (with `bodyUsed`, `clone`, `text`,
|
|
45
|
+
`json`, etc.), convert with `toWebResponse()`:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { request, toWebResponse } from "stealth-fetch";
|
|
49
|
+
|
|
50
|
+
const resp = await request("https://httpbin.org/headers", { protocol: "h2" });
|
|
51
|
+
const webResp = toWebResponse(resp);
|
|
52
|
+
const text = await webResp.text();
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Note: don’t call `resp.text()/json()/arrayBuffer()` before converting, or the
|
|
56
|
+
body stream will already be consumed.
|
|
57
|
+
|
|
58
|
+
If you need a pre-cloned pair (using `ReadableStream.tee()`), pass
|
|
59
|
+
`{ tee: true }`:
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const { response, clone } = toWebResponse(resp, { tee: true });
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## API
|
|
66
|
+
|
|
67
|
+
### `request(url, options?)`
|
|
68
|
+
|
|
69
|
+
Returns `Promise<HttpResponse>`.
|
|
70
|
+
|
|
71
|
+
**Options**
|
|
72
|
+
|
|
73
|
+
| Option | Type | Default | Description |
|
|
74
|
+
| ---------------- | ------------------------------------------------------------ | ---------- | ---------------------------------------------------------------------------------------------------------------------- |
|
|
75
|
+
| `method` | `string` | `'GET'` | HTTP method |
|
|
76
|
+
| `headers` | `Record<string, string>` | `{}` | Request headers |
|
|
77
|
+
| `body` | `string \| Uint8Array \| ReadableStream<Uint8Array> \| null` | `null` | Request body |
|
|
78
|
+
| `protocol` | `'h2' \| 'http/1.1' \| 'auto'` | `'auto'` | Protocol selection |
|
|
79
|
+
| `timeout` | `number` | `30000` | Overall timeout from call until response headers (includes retries/redirects) |
|
|
80
|
+
| `headersTimeout` | `number` | — | Timeout waiting for response headers |
|
|
81
|
+
| `bodyTimeout` | `number` | — | Idle timeout waiting for response body data |
|
|
82
|
+
| `signal` | `AbortSignal` | — | Cancellation signal |
|
|
83
|
+
| `redirect` | `'follow' \| 'manual'` | `'follow'` | Redirect handling |
|
|
84
|
+
| `maxRedirects` | `number` | `5` | Max redirects to follow |
|
|
85
|
+
| `decompress` | `boolean` | `true` | Auto-decompress gzip/deflate responses |
|
|
86
|
+
| `compressBody` | `boolean` | `false` | Gzip-compress request body (Uint8Array > 1KB) |
|
|
87
|
+
| `strategy` | `'compat' \| 'fast-h1'` | `'compat'` | `compat`: ALPN + protocol cache (h2 supported); `fast-h1`: platform TLS for non-CF, WASM TLS h1 for CF (faster, no h2) |
|
|
88
|
+
|
|
89
|
+
**Response**
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
interface HttpResponse {
|
|
93
|
+
status: number;
|
|
94
|
+
statusText: string;
|
|
95
|
+
headers: Record<string, string>;
|
|
96
|
+
rawHeaders: ReadonlyArray<[string, string]>;
|
|
97
|
+
protocol: "h2" | "http/1.1";
|
|
98
|
+
body: ReadableStream<Uint8Array>;
|
|
99
|
+
|
|
100
|
+
text(): Promise<string>;
|
|
101
|
+
json(): Promise<unknown>;
|
|
102
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
103
|
+
getSetCookie(): string[];
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Advanced APIs
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import {
|
|
111
|
+
Http2Client,
|
|
112
|
+
Http2Connection,
|
|
113
|
+
http1Request,
|
|
114
|
+
clearPool,
|
|
115
|
+
preconnect,
|
|
116
|
+
createWasmTLSSocket,
|
|
117
|
+
} from "stealth-fetch";
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
- `Http2Client` — HTTP/2 client with stream multiplexing
|
|
121
|
+
- `Http2Connection` — Low-level HTTP/2 connection
|
|
122
|
+
- `http1Request(socket, request)` — HTTP/1.1 over a raw socket
|
|
123
|
+
- `clearPool()` — Clear the HTTP/2 connection pool
|
|
124
|
+
- `preconnect(hostname, port?)` — Pre-establish an HTTP/2 connection
|
|
125
|
+
- `createWasmTLSSocket(hostname, port, alpnList)` — WASM TLS socket with ALPN
|
|
126
|
+
|
|
127
|
+
## Differences From `fetch`
|
|
128
|
+
|
|
129
|
+
- `fetch` injects `cf-*` headers in Workers, this library does not.
|
|
130
|
+
- `fetch` exposes standard `Request/Response` objects; this library returns a
|
|
131
|
+
custom `HttpResponse` (use `toWebResponse()` if you need a Web `Response`).
|
|
132
|
+
- Protocol control (force `h1`/`h2`, ALPN, NAT64) is supported here.
|
|
133
|
+
|
|
134
|
+
## NAT64 Fallback Notes
|
|
135
|
+
|
|
136
|
+
NAT64 is a best-effort fallback. Public NAT64 gateways can be unstable or
|
|
137
|
+
blocked depending on region and routing, so some connections may fail. If you
|
|
138
|
+
rely on NAT64, plan for retries or fallback behavior in your application.
|
|
139
|
+
|
|
140
|
+
## Requirements
|
|
141
|
+
|
|
142
|
+
- Cloudflare Workers runtime
|
|
143
|
+
- `nodejs_compat` compatibility flag
|
|
144
|
+
|
|
145
|
+
## Example Worker
|
|
146
|
+
|
|
147
|
+
The repo includes a minimal worker at `examples/worker.ts` with endpoints:
|
|
148
|
+
|
|
149
|
+
- `/http1`
|
|
150
|
+
- `/http2`
|
|
151
|
+
- `/auto`
|
|
152
|
+
- `/fetch`
|
|
153
|
+
- `/single?url=&mode=auto|h1|h2|fetch`
|
|
154
|
+
|
|
155
|
+
`wrangler.toml` points to `examples/worker.ts` as the entry.
|
|
156
|
+
|
|
157
|
+
## Development
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
pnpm dev # Local dev server (wrangler)
|
|
161
|
+
pnpm test:run # Run tests once
|
|
162
|
+
pnpm test # Run tests in watch mode
|
|
163
|
+
pnpm type-check # TypeScript type check
|
|
164
|
+
pnpm build # Build to dist/
|
|
165
|
+
pnpm lint # ESLint
|
|
166
|
+
pnpm format # Prettier
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Contributing
|
|
170
|
+
|
|
171
|
+
See `CONTRIBUTING.md` for commit message rules and dev notes.
|
|
172
|
+
|
|
173
|
+
## Building WASM TLS
|
|
174
|
+
|
|
175
|
+
Requires Rust toolchain with `wasm-pack` and `wasm32-unknown-unknown` target:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
pnpm build:wasm
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## License
|
|
182
|
+
|
|
183
|
+
[MIT](LICENSE)
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
interface RetryOptions {
|
|
2
|
+
/** Max retry attempts (default: 2) */
|
|
3
|
+
limit?: number;
|
|
4
|
+
/** HTTP methods to retry (default: GET, HEAD, OPTIONS, PUT, DELETE) */
|
|
5
|
+
methods?: string[];
|
|
6
|
+
/** HTTP status codes to retry on (default: [408, 413, 429, 500, 502, 503, 504]) */
|
|
7
|
+
statusCodes?: number[];
|
|
8
|
+
/** Max retry delay in ms (default: 30000) */
|
|
9
|
+
maxDelay?: number;
|
|
10
|
+
/** Base delay for exponential backoff in ms — delay = baseDelay * 2^attempt (default: 1000) */
|
|
11
|
+
baseDelay?: number;
|
|
12
|
+
}
|
|
13
|
+
interface RequestOptions {
|
|
14
|
+
method?: string;
|
|
15
|
+
headers?: Record<string, string> | Headers;
|
|
16
|
+
body?: Uint8Array | string | ReadableStream<Uint8Array> | null;
|
|
17
|
+
/** Protocol selection: 'h2', 'http/1.1', or 'auto' (default) */
|
|
18
|
+
protocol?: "h2" | "http/1.1" | "auto";
|
|
19
|
+
/** Request timeout in ms (default: 30000). Covers from call to response headers (includes retries/redirects). */
|
|
20
|
+
timeout?: number;
|
|
21
|
+
/** Timeout waiting for response headers in ms. */
|
|
22
|
+
headersTimeout?: number;
|
|
23
|
+
/** Timeout waiting for response body data in ms (idle). */
|
|
24
|
+
bodyTimeout?: number;
|
|
25
|
+
/** AbortSignal to cancel the request */
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
/** Redirect handling: 'follow' (default) or 'manual' */
|
|
28
|
+
redirect?: "follow" | "manual";
|
|
29
|
+
/** Maximum number of redirects to follow (default: 5) */
|
|
30
|
+
maxRedirects?: number;
|
|
31
|
+
/** Retry configuration. Set to 0 or false to disable. Default: 0 (no retry). */
|
|
32
|
+
retry?: number | RetryOptions | false;
|
|
33
|
+
/** Auto-decompress gzip/deflate response body. Default: true */
|
|
34
|
+
decompress?: boolean;
|
|
35
|
+
/** Compress request body with gzip (Uint8Array > 1KB only). Default: false */
|
|
36
|
+
compressBody?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Connection strategy: 'compat' (default) uses ALPN negotiation + protocol cache;
|
|
39
|
+
* 'fast-h1' uses platform TLS for non-CF, WASM TLS h1-only for CF (faster, no h2).
|
|
40
|
+
*/
|
|
41
|
+
strategy?: "compat" | "fast-h1";
|
|
42
|
+
}
|
|
43
|
+
interface HttpResponse {
|
|
44
|
+
status: number;
|
|
45
|
+
statusText: string;
|
|
46
|
+
headers: Record<string, string>;
|
|
47
|
+
/** Raw headers preserving original order and multi-values */
|
|
48
|
+
rawHeaders: ReadonlyArray<[string, string]>;
|
|
49
|
+
protocol: "h2" | "http/1.1";
|
|
50
|
+
body: ReadableStream<Uint8Array>;
|
|
51
|
+
/** Read body as text */
|
|
52
|
+
text(): Promise<string>;
|
|
53
|
+
/** Read body as JSON */
|
|
54
|
+
json(): Promise<unknown>;
|
|
55
|
+
/** Read body as ArrayBuffer */
|
|
56
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
57
|
+
/** Get all Set-Cookie header values as separate strings */
|
|
58
|
+
getSetCookie(): string[];
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Pre-establish an HTTP/2 connection and place it in the pool.
|
|
62
|
+
* Subsequent requests to the same origin will reuse this connection,
|
|
63
|
+
* avoiding TCP + TLS + SETTINGS handshake latency (400-600ms).
|
|
64
|
+
*
|
|
65
|
+
* If the origin is behind CF CDN, automatically uses NAT64.
|
|
66
|
+
* H1-only servers are connected and immediately closed (no pool benefit).
|
|
67
|
+
*/
|
|
68
|
+
declare function preconnect(hostname: string, port?: number): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Send an HTTP request over a raw socket, bypassing Cloudflare's cf-* headers.
|
|
71
|
+
*
|
|
72
|
+
* If the direct connection is blocked by CF Workers' outbound restrictions,
|
|
73
|
+
* automatically falls back to NAT64 to reach the target.
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const response = await request('https://api.openai.com/v1/chat/completions', {
|
|
77
|
+
* method: 'POST',
|
|
78
|
+
* headers: { 'Authorization': 'Bearer sk-...' },
|
|
79
|
+
* body: JSON.stringify({ model: 'gpt-4', messages: [...] })
|
|
80
|
+
* });
|
|
81
|
+
* const data = await response.json();
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
declare function request(url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
85
|
+
declare function normalizeHeaders(headers?: Record<string, string> | Headers): Record<string, string>;
|
|
86
|
+
interface NormalizedRetry {
|
|
87
|
+
limit: number;
|
|
88
|
+
methods: Set<string>;
|
|
89
|
+
statusCodes: Set<number>;
|
|
90
|
+
maxDelay: number;
|
|
91
|
+
baseDelay: number;
|
|
92
|
+
}
|
|
93
|
+
declare function normalizeRetry(retry: number | RetryOptions | false | undefined): NormalizedRetry | null;
|
|
94
|
+
declare function calculateRetryDelay(retryAfterHeader: string | undefined, attempt: number, config: {
|
|
95
|
+
baseDelay: number;
|
|
96
|
+
maxDelay: number;
|
|
97
|
+
}): number;
|
|
98
|
+
|
|
99
|
+
export { type HttpResponse, type RequestOptions, type RetryOptions, calculateRetryDelay, normalizeHeaders, normalizeRetry, preconnect, request };
|