@npy/fetch 0.1.1 → 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/_internal/decode-stream-error.cjs +18 -0
- package/_internal/decode-stream-error.d.cts +11 -0
- package/_internal/decode-stream-error.d.ts +11 -0
- package/_internal/decode-stream-error.js +18 -0
- package/_internal/error-mapping.cjs +44 -0
- package/_internal/error-mapping.d.cts +15 -0
- package/_internal/error-mapping.d.ts +15 -0
- package/_internal/error-mapping.js +41 -0
- package/_internal/guards.cjs +5 -6
- package/{src/_internal → _internal}/guards.d.cts +2 -0
- package/{src/_internal → _internal}/guards.d.ts +2 -0
- package/_internal/guards.js +5 -7
- package/{src/_internal → _internal}/net.d.cts +1 -2
- package/{src/_internal → _internal}/net.d.ts +1 -2
- package/_internal/symbols.cjs +4 -0
- package/_internal/symbols.d.cts +1 -0
- package/_internal/symbols.d.ts +1 -0
- package/_internal/symbols.js +4 -0
- package/agent-pool.cjs +23 -5
- package/agent-pool.d.cts +2 -0
- package/agent-pool.d.ts +2 -0
- package/agent-pool.js +23 -5
- package/agent.cjs +17 -14
- package/agent.js +17 -14
- package/body.cjs +10 -59
- package/body.d.cts +12 -0
- package/body.d.ts +12 -0
- package/body.js +11 -60
- package/dialers/proxy.cjs +7 -0
- package/{src/dialers → dialers}/proxy.d.cts +11 -3
- package/{src/dialers → dialers}/proxy.d.ts +11 -3
- package/dialers/proxy.js +7 -0
- package/dialers/tcp.cjs +22 -0
- package/{src/dialers → dialers}/tcp.d.cts +23 -2
- package/{src/dialers → dialers}/tcp.d.ts +23 -2
- package/dialers/tcp.js +22 -0
- package/encoding.cjs +32 -13
- package/encoding.d.cts +35 -0
- package/encoding.d.ts +35 -0
- package/encoding.js +32 -13
- package/fetch.cjs +279 -43
- package/fetch.d.cts +58 -0
- package/fetch.d.ts +58 -0
- package/fetch.js +278 -43
- package/http-client.cjs +47 -5
- package/http-client.d.cts +39 -0
- package/http-client.d.ts +39 -0
- package/http-client.js +47 -5
- package/index.cjs +7 -3
- package/index.d.cts +14 -1
- package/index.d.ts +14 -1
- package/index.js +6 -4
- package/io/io.cjs +68 -4
- package/{src/io → io}/io.d.cts +1 -1
- package/{src/io → io}/io.d.ts +1 -1
- package/io/io.js +68 -4
- package/io/readers.cjs +14 -54
- package/io/readers.d.cts +69 -0
- package/io/readers.d.ts +69 -0
- package/io/readers.js +14 -54
- package/io/writers.cjs +10 -5
- package/{src/io → io}/writers.d.cts +1 -1
- package/{src/io → io}/writers.d.ts +1 -1
- package/io/writers.js +11 -6
- package/package.json +18 -2
- package/types/agent.d.cts +72 -0
- package/types/agent.d.ts +72 -0
- package/{src/types → types}/dialer.d.cts +3 -0
- package/{src/types → types}/dialer.d.ts +3 -0
- package/_internal/error-adapters.cjs +0 -146
- package/_internal/error-adapters.js +0 -142
- package/src/_internal/error-adapters.d.cts +0 -22
- package/src/_internal/error-adapters.d.ts +0 -22
- package/src/agent-pool.d.cts +0 -2
- package/src/agent-pool.d.ts +0 -2
- package/src/body.d.cts +0 -23
- package/src/body.d.ts +0 -23
- package/src/encoding.d.cts +0 -24
- package/src/encoding.d.ts +0 -24
- package/src/fetch.d.cts +0 -36
- package/src/fetch.d.ts +0 -36
- package/src/http-client.d.cts +0 -23
- package/src/http-client.d.ts +0 -23
- package/src/index.d.cts +0 -7
- package/src/index.d.ts +0 -7
- package/src/io/readers.d.cts +0 -199
- package/src/io/readers.d.ts +0 -199
- package/src/types/agent.d.cts +0 -128
- package/src/types/agent.d.ts +0 -128
- package/tests/test-utils.d.cts +0 -8
- package/tests/test-utils.d.ts +0 -8
- /package/{src/_internal → _internal}/consts.d.cts +0 -0
- /package/{src/_internal → _internal}/consts.d.ts +0 -0
- /package/{src/_internal → _internal}/promises.d.cts +0 -0
- /package/{src/_internal → _internal}/promises.d.ts +0 -0
- /package/{src/_internal → _internal}/streams.d.cts +0 -0
- /package/{src/_internal → _internal}/streams.d.ts +0 -0
- /package/{src/agent.d.cts → agent.d.cts} +0 -0
- /package/{src/agent.d.ts → agent.d.ts} +0 -0
- /package/{src/dialers → dialers}/index.d.cts +0 -0
- /package/{src/dialers → dialers}/index.d.ts +0 -0
- /package/{src/errors.d.cts → errors.d.cts} +0 -0
- /package/{src/errors.d.ts → errors.d.ts} +0 -0
- /package/{src/io → io}/_utils.d.cts +0 -0
- /package/{src/io → io}/_utils.d.ts +0 -0
- /package/{src/io → io}/buf-writer.d.cts +0 -0
- /package/{src/io → io}/buf-writer.d.ts +0 -0
- /package/{src/types → types}/index.d.cts +0 -0
- /package/{src/types → types}/index.d.ts +0 -0
package/io/writers.cjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
-
const require_guards = require("../_internal/guards.cjs");
|
|
3
|
-
const require_body = require("../body.cjs");
|
|
4
|
-
const require_encoding = require("../encoding.cjs");
|
|
5
2
|
const require_streams = require("../_internal/streams.cjs");
|
|
3
|
+
const require_encoding = require("../encoding.cjs");
|
|
6
4
|
const require__utils = require("./_utils.cjs");
|
|
7
5
|
const require_readers = require("./readers.cjs");
|
|
6
|
+
const require_guards = require("../_internal/guards.cjs");
|
|
7
|
+
const require_body = require("../body.cjs");
|
|
8
8
|
const require_buf_writer = require("./buf-writer.cjs");
|
|
9
9
|
let _fuman_node = require("@fuman/node");
|
|
10
10
|
let _fuman_io = require("@fuman/io");
|
|
@@ -34,6 +34,11 @@ function prepareBody(headers, init) {
|
|
|
34
34
|
stream: body,
|
|
35
35
|
length: state.contentLength
|
|
36
36
|
};
|
|
37
|
+
if (require_guards.isFumanReadable(body)) return {
|
|
38
|
+
kind: "stream",
|
|
39
|
+
stream: (0, _fuman_io.fumanReadableToWeb)(body),
|
|
40
|
+
length: state.contentLength
|
|
41
|
+
};
|
|
37
42
|
return {
|
|
38
43
|
kind: "stream",
|
|
39
44
|
stream: (0, _fuman_node.nodeReadableToWeb)(body),
|
|
@@ -64,7 +69,7 @@ function finalizeDelimitation(headers, body) {
|
|
|
64
69
|
return { chunked: true };
|
|
65
70
|
}
|
|
66
71
|
function createBufferedConnWriter(dst, opts) {
|
|
67
|
-
const bufferSize = opts.
|
|
72
|
+
const bufferSize = opts.writeBufferSize ?? 16 * 1024;
|
|
68
73
|
const directWriteThreshold = opts.directWriteThreshold ?? 64 * 1024;
|
|
69
74
|
const bufWriter = new require_buf_writer.BufWriter(dst, bufferSize);
|
|
70
75
|
const flush = async () => {
|
|
@@ -131,7 +136,7 @@ async function writeCoalesced(dst, scratch, head, bodyBytes, chunked) {
|
|
|
131
136
|
scratch.reset();
|
|
132
137
|
}
|
|
133
138
|
function createRequestWriter(dst, opts = {}) {
|
|
134
|
-
const scratch = _fuman_io.Bytes.alloc(opts.
|
|
139
|
+
const scratch = _fuman_io.Bytes.alloc(opts.writeBufferSize ?? 16 * 1024);
|
|
135
140
|
const write = async (req) => {
|
|
136
141
|
if (req.signal?.aborted) throw req.signal.reason ?? /* @__PURE__ */ new Error("Request aborted");
|
|
137
142
|
const method = req.method.toUpperCase();
|
package/io/writers.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { isReadableStream } from "../_internal/guards.js";
|
|
2
|
-
import { extractBody } from "../body.js";
|
|
3
|
-
import { createEncoders, encodeStream } from "../encoding.js";
|
|
4
1
|
import { bytesToStream } from "../_internal/streams.js";
|
|
2
|
+
import { createEncoders, encodeStream } from "../encoding.js";
|
|
5
3
|
import { parseContentLength, parseTransferEncoding } from "./_utils.js";
|
|
6
4
|
import { sanitizeHeaderValue } from "./readers.js";
|
|
5
|
+
import { isFumanReadable, isReadableStream } from "../_internal/guards.js";
|
|
6
|
+
import { extractBody } from "../body.js";
|
|
7
7
|
import { BufWriter } from "./buf-writer.js";
|
|
8
8
|
import { nodeReadableToWeb } from "@fuman/node";
|
|
9
|
-
import { Bytes, write } from "@fuman/io";
|
|
9
|
+
import { Bytes, fumanReadableToWeb, write } from "@fuman/io";
|
|
10
10
|
//#region src/io/writers.ts
|
|
11
11
|
function toRequestTarget(url) {
|
|
12
12
|
return (url.pathname?.startsWith("/") ? url.pathname : "/") + (url.search || "");
|
|
@@ -33,6 +33,11 @@ function prepareBody(headers, init) {
|
|
|
33
33
|
stream: body,
|
|
34
34
|
length: state.contentLength
|
|
35
35
|
};
|
|
36
|
+
if (isFumanReadable(body)) return {
|
|
37
|
+
kind: "stream",
|
|
38
|
+
stream: fumanReadableToWeb(body),
|
|
39
|
+
length: state.contentLength
|
|
40
|
+
};
|
|
36
41
|
return {
|
|
37
42
|
kind: "stream",
|
|
38
43
|
stream: nodeReadableToWeb(body),
|
|
@@ -63,7 +68,7 @@ function finalizeDelimitation(headers, body) {
|
|
|
63
68
|
return { chunked: true };
|
|
64
69
|
}
|
|
65
70
|
function createBufferedConnWriter(dst, opts) {
|
|
66
|
-
const bufferSize = opts.
|
|
71
|
+
const bufferSize = opts.writeBufferSize ?? 16 * 1024;
|
|
67
72
|
const directWriteThreshold = opts.directWriteThreshold ?? 64 * 1024;
|
|
68
73
|
const bufWriter = new BufWriter(dst, bufferSize);
|
|
69
74
|
const flush = async () => {
|
|
@@ -130,7 +135,7 @@ async function writeCoalesced(dst, scratch, head, bodyBytes, chunked) {
|
|
|
130
135
|
scratch.reset();
|
|
131
136
|
}
|
|
132
137
|
function createRequestWriter(dst, opts = {}) {
|
|
133
|
-
const scratch = Bytes.alloc(opts.
|
|
138
|
+
const scratch = Bytes.alloc(opts.writeBufferSize ?? 16 * 1024);
|
|
134
139
|
const write = async (req) => {
|
|
135
140
|
if (req.signal?.aborted) throw req.signal.reason ?? /* @__PURE__ */ new Error("Request aborted");
|
|
136
141
|
const method = req.method.toUpperCase();
|
package/package.json
CHANGED
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@npy/fetch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
|
+
"description": "HTTP/1.1 client built from raw TCP sockets with fetch-compatible primitives and proxy support.",
|
|
5
6
|
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"fetch",
|
|
9
|
+
"http",
|
|
10
|
+
"http-client",
|
|
11
|
+
"proxy",
|
|
12
|
+
"socks",
|
|
13
|
+
"socks5",
|
|
14
|
+
"tcp",
|
|
15
|
+
"node",
|
|
16
|
+
"bun"
|
|
17
|
+
],
|
|
6
18
|
"scripts": {},
|
|
7
19
|
"dependencies": {
|
|
8
20
|
"@fuman/io": "^0.0.19",
|
|
9
21
|
"@fuman/net": "^0.0.19",
|
|
10
22
|
"@fuman/node": "^0.0.19",
|
|
11
23
|
"@fuman/utils": "^0.0.19",
|
|
12
|
-
"@npy/proxy-kit": "0.1.
|
|
24
|
+
"@npy/proxy-kit": "0.1.2",
|
|
13
25
|
"bytes": "^3.1.2",
|
|
14
26
|
"generic-pool": "^3.9.0"
|
|
15
27
|
},
|
|
@@ -26,10 +38,14 @@
|
|
|
26
38
|
}
|
|
27
39
|
},
|
|
28
40
|
"sideEffects": false,
|
|
41
|
+
"homepage": "https://github.com/neeopy/npy",
|
|
29
42
|
"repository": {
|
|
30
43
|
"type": "git",
|
|
31
44
|
"url": "git+https://github.com/neeopy/npy.git"
|
|
32
45
|
},
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/neeopy/npy/issues"
|
|
48
|
+
},
|
|
33
49
|
"files": [
|
|
34
50
|
"**/*"
|
|
35
51
|
]
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { BodyInit } from '../body';
|
|
2
|
+
import { LineReader, Readers } from '../io/readers';
|
|
3
|
+
import { Writers } from '../io/writers';
|
|
4
|
+
import { Dialer } from './dialer';
|
|
5
|
+
export interface Agent {
|
|
6
|
+
[Symbol.dispose](): void;
|
|
7
|
+
close(): void;
|
|
8
|
+
readonly hostname: string;
|
|
9
|
+
readonly port: number;
|
|
10
|
+
/**
|
|
11
|
+
* Sends a single HTTP request and returns the raw {@link Response}.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* The returned response body preserves the advanced error mapping of this library.
|
|
15
|
+
* Limits configured through reader options are enforced while the body is consumed.
|
|
16
|
+
*/
|
|
17
|
+
send(options: Agent.SendOptions): Promise<Response>;
|
|
18
|
+
whenIdle(): Promise<void>;
|
|
19
|
+
readonly isIdle: boolean;
|
|
20
|
+
readonly lastUsed: number;
|
|
21
|
+
}
|
|
22
|
+
export declare namespace Agent {
|
|
23
|
+
interface ConnectOptions {
|
|
24
|
+
timeout?: number;
|
|
25
|
+
keepAlive?: boolean | null;
|
|
26
|
+
noDelay?: boolean;
|
|
27
|
+
}
|
|
28
|
+
type ReaderOptions = Readers.Options & LineReader.ReadHeadersOptions;
|
|
29
|
+
type WriterOptions = Writers.Options;
|
|
30
|
+
interface IOOptions {
|
|
31
|
+
reader?: ReaderOptions;
|
|
32
|
+
writer?: WriterOptions;
|
|
33
|
+
}
|
|
34
|
+
interface Options {
|
|
35
|
+
connect?: ConnectOptions;
|
|
36
|
+
io?: IOOptions;
|
|
37
|
+
}
|
|
38
|
+
interface SendOptions {
|
|
39
|
+
url: string | URL;
|
|
40
|
+
method: string;
|
|
41
|
+
headers?: Headers;
|
|
42
|
+
body?: BodyInit | null;
|
|
43
|
+
signal?: AbortSignal;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export interface AgentPool {
|
|
47
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
readonly hostname: string;
|
|
50
|
+
readonly port: number;
|
|
51
|
+
send(options: Agent.SendOptions): Promise<Response>;
|
|
52
|
+
}
|
|
53
|
+
export declare namespace AgentPool {
|
|
54
|
+
interface Options {
|
|
55
|
+
dialer?: Dialer;
|
|
56
|
+
poolMaxIdlePerHost?: number;
|
|
57
|
+
poolMaxPerHost?: number;
|
|
58
|
+
poolIdleTimeout?: number | false;
|
|
59
|
+
connect?: Agent.ConnectOptions;
|
|
60
|
+
io?: Agent.IOOptions;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export interface AgentConnectOptions extends Agent.ConnectOptions {
|
|
64
|
+
}
|
|
65
|
+
export interface AgentPoolOptions extends AgentPool.Options {
|
|
66
|
+
}
|
|
67
|
+
export interface SendOptions extends Agent.SendOptions {
|
|
68
|
+
}
|
|
69
|
+
export interface AgentIOOptions extends Agent.IOOptions {
|
|
70
|
+
}
|
|
71
|
+
export type AgentReaderOptions = Agent.ReaderOptions;
|
|
72
|
+
export type AgentWriterOptions = Agent.WriterOptions;
|
package/types/agent.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { BodyInit } from '../body';
|
|
2
|
+
import { LineReader, Readers } from '../io/readers';
|
|
3
|
+
import { Writers } from '../io/writers';
|
|
4
|
+
import { Dialer } from './dialer';
|
|
5
|
+
export interface Agent {
|
|
6
|
+
[Symbol.dispose](): void;
|
|
7
|
+
close(): void;
|
|
8
|
+
readonly hostname: string;
|
|
9
|
+
readonly port: number;
|
|
10
|
+
/**
|
|
11
|
+
* Sends a single HTTP request and returns the raw {@link Response}.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* The returned response body preserves the advanced error mapping of this library.
|
|
15
|
+
* Limits configured through reader options are enforced while the body is consumed.
|
|
16
|
+
*/
|
|
17
|
+
send(options: Agent.SendOptions): Promise<Response>;
|
|
18
|
+
whenIdle(): Promise<void>;
|
|
19
|
+
readonly isIdle: boolean;
|
|
20
|
+
readonly lastUsed: number;
|
|
21
|
+
}
|
|
22
|
+
export declare namespace Agent {
|
|
23
|
+
interface ConnectOptions {
|
|
24
|
+
timeout?: number;
|
|
25
|
+
keepAlive?: boolean | null;
|
|
26
|
+
noDelay?: boolean;
|
|
27
|
+
}
|
|
28
|
+
type ReaderOptions = Readers.Options & LineReader.ReadHeadersOptions;
|
|
29
|
+
type WriterOptions = Writers.Options;
|
|
30
|
+
interface IOOptions {
|
|
31
|
+
reader?: ReaderOptions;
|
|
32
|
+
writer?: WriterOptions;
|
|
33
|
+
}
|
|
34
|
+
interface Options {
|
|
35
|
+
connect?: ConnectOptions;
|
|
36
|
+
io?: IOOptions;
|
|
37
|
+
}
|
|
38
|
+
interface SendOptions {
|
|
39
|
+
url: string | URL;
|
|
40
|
+
method: string;
|
|
41
|
+
headers?: Headers;
|
|
42
|
+
body?: BodyInit | null;
|
|
43
|
+
signal?: AbortSignal;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export interface AgentPool {
|
|
47
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
readonly hostname: string;
|
|
50
|
+
readonly port: number;
|
|
51
|
+
send(options: Agent.SendOptions): Promise<Response>;
|
|
52
|
+
}
|
|
53
|
+
export declare namespace AgentPool {
|
|
54
|
+
interface Options {
|
|
55
|
+
dialer?: Dialer;
|
|
56
|
+
poolMaxIdlePerHost?: number;
|
|
57
|
+
poolMaxPerHost?: number;
|
|
58
|
+
poolIdleTimeout?: number | false;
|
|
59
|
+
connect?: Agent.ConnectOptions;
|
|
60
|
+
io?: Agent.IOOptions;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export interface AgentConnectOptions extends Agent.ConnectOptions {
|
|
64
|
+
}
|
|
65
|
+
export interface AgentPoolOptions extends AgentPool.Options {
|
|
66
|
+
}
|
|
67
|
+
export interface SendOptions extends Agent.SendOptions {
|
|
68
|
+
}
|
|
69
|
+
export interface AgentIOOptions extends Agent.IOOptions {
|
|
70
|
+
}
|
|
71
|
+
export type AgentReaderOptions = Agent.ReaderOptions;
|
|
72
|
+
export type AgentWriterOptions = Agent.WriterOptions;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { ITcpConnection, ITlsConnection } from '@fuman/net';
|
|
2
2
|
import { NodeTlsConnectOptions } from '@fuman/node';
|
|
3
|
+
/**
|
|
4
|
+
* Transport abstraction responsible for opening a connection to an HTTP target.
|
|
5
|
+
*/
|
|
3
6
|
export interface Dialer {
|
|
4
7
|
dial(target: Dialer.Target, options?: Dialer.Options): Promise<Dialer.ConnectionLike>;
|
|
5
8
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { ITcpConnection, ITlsConnection } from '@fuman/net';
|
|
2
2
|
import { NodeTlsConnectOptions } from '@fuman/node';
|
|
3
|
+
/**
|
|
4
|
+
* Transport abstraction responsible for opening a connection to an HTTP target.
|
|
5
|
+
*/
|
|
3
6
|
export interface Dialer {
|
|
4
7
|
dial(target: Dialer.Target, options?: Dialer.Options): Promise<Dialer.ConnectionLike>;
|
|
5
8
|
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
const require_errors = require("../errors.cjs");
|
|
2
|
-
//#region src/_internal/error-adapters.ts
|
|
3
|
-
function getErrorChain(error, maxDepth = 8) {
|
|
4
|
-
const chain = [];
|
|
5
|
-
const seen = /* @__PURE__ */ new Set();
|
|
6
|
-
let current = error;
|
|
7
|
-
let depth = 0;
|
|
8
|
-
while (current instanceof Error && depth < maxDepth && !seen.has(current)) {
|
|
9
|
-
const entry = current;
|
|
10
|
-
chain.push(entry);
|
|
11
|
-
seen.add(current);
|
|
12
|
-
current = entry.cause;
|
|
13
|
-
depth += 1;
|
|
14
|
-
}
|
|
15
|
-
return chain;
|
|
16
|
-
}
|
|
17
|
-
function unknownToError(error) {
|
|
18
|
-
if (error instanceof Error) return error;
|
|
19
|
-
if (typeof error === "string") return new Error(error);
|
|
20
|
-
return new Error("Unknown error", { cause: error });
|
|
21
|
-
}
|
|
22
|
-
function isAbortLike(error) {
|
|
23
|
-
if (!error || typeof error !== "object") return false;
|
|
24
|
-
const name = "name" in error ? error.name : void 0;
|
|
25
|
-
return name === "AbortError" || name === "TimeoutError";
|
|
26
|
-
}
|
|
27
|
-
function defaultAbortDomException() {
|
|
28
|
-
return new DOMException("This operation was aborted", "AbortError");
|
|
29
|
-
}
|
|
30
|
-
function getRawAbortReason(signal) {
|
|
31
|
-
return signal?.reason ?? defaultAbortDomException();
|
|
32
|
-
}
|
|
33
|
-
function toAbortCause(signal, fallback) {
|
|
34
|
-
return unknownToError(signal?.reason ?? fallback ?? defaultAbortDomException());
|
|
35
|
-
}
|
|
36
|
-
function isTimeoutReason(reason) {
|
|
37
|
-
return !!reason && typeof reason === "object" && "name" in reason && reason.name === "TimeoutError";
|
|
38
|
-
}
|
|
39
|
-
function looksLikeDecodeError(error) {
|
|
40
|
-
const chain = getErrorChain(error);
|
|
41
|
-
for (const entry of chain) {
|
|
42
|
-
const code = typeof entry.code === "string" ? entry.code.toLowerCase() : "";
|
|
43
|
-
const text = `${entry.name} ${entry.message} ${code}`.toLowerCase();
|
|
44
|
-
if (code === "z_data_error" || code === "z_buf_error" || code === "z_stream_error" || text.includes("decode") || text.includes("decompress") || text.includes("encoding") || text.includes("gzip") || text.includes("deflate") || text.includes("brotli") || text.includes("incorrect header check") || text.includes("invalid block type") || text.includes("invalid distance") || text.includes("invalid stored block") || text.includes("unexpected end of file") || text.includes("unexpected end of stream") || text.includes("header check")) return true;
|
|
45
|
-
}
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
function mapAdvancedConnectError(error, { signal, context, timedOut }) {
|
|
49
|
-
if (error instanceof require_errors.FetchError) return error;
|
|
50
|
-
if (timedOut || isTimeoutReason(signal?.reason)) return new require_errors.ConnectTimeoutError(unknownToError(error), context, "Connection timeout");
|
|
51
|
-
if (signal?.aborted || isAbortLike(error)) return new require_errors.RequestAbortedError(toAbortCause(signal, error), context, "The request was aborted while connecting");
|
|
52
|
-
return new require_errors.ConnectionError(unknownToError(error), context, "Connection failed");
|
|
53
|
-
}
|
|
54
|
-
function mapAdvancedSendError(error, { signal, context, phase }) {
|
|
55
|
-
if (error instanceof require_errors.FetchError) return error;
|
|
56
|
-
if (signal?.aborted || isAbortLike(error)) return new require_errors.RequestAbortedError(toAbortCause(signal, error), context, phase === "body" ? "The request was aborted while reading the response body" : "The request was aborted");
|
|
57
|
-
const cause = unknownToError(error);
|
|
58
|
-
if (phase === "request") return new require_errors.RequestWriteError(cause, context);
|
|
59
|
-
if (phase === "response") return new require_errors.ResponseHeaderError(cause, context);
|
|
60
|
-
if (looksLikeDecodeError(cause)) return new require_errors.ResponseDecodeError(cause, context);
|
|
61
|
-
return new require_errors.ResponseBodyError(cause, context);
|
|
62
|
-
}
|
|
63
|
-
function wrapResponseBodyErrors(response, mapError) {
|
|
64
|
-
const source = response.body;
|
|
65
|
-
if (!source) return response;
|
|
66
|
-
const reader = source.getReader();
|
|
67
|
-
let pending = null;
|
|
68
|
-
const wrapped = new ReadableStream({
|
|
69
|
-
type: "bytes",
|
|
70
|
-
async pull(controller) {
|
|
71
|
-
try {
|
|
72
|
-
const byob = controller.byobRequest;
|
|
73
|
-
if (byob?.view) {
|
|
74
|
-
const target = new Uint8Array(byob.view.buffer, byob.view.byteOffset, byob.view.byteLength);
|
|
75
|
-
if (target.byteLength === 0) {
|
|
76
|
-
byob.respond(0);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
let written = 0;
|
|
80
|
-
if (pending && pending.byteLength > 0) {
|
|
81
|
-
const n = Math.min(target.byteLength, pending.byteLength);
|
|
82
|
-
target.set(pending.subarray(0, n), written);
|
|
83
|
-
written += n;
|
|
84
|
-
pending = n === pending.byteLength ? null : pending.subarray(n);
|
|
85
|
-
}
|
|
86
|
-
while (written === 0) {
|
|
87
|
-
const { done, value } = await reader.read();
|
|
88
|
-
if (done) {
|
|
89
|
-
byob.respond(0);
|
|
90
|
-
controller.close();
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
if (!value || value.byteLength === 0) continue;
|
|
94
|
-
const n = Math.min(target.byteLength - written, value.byteLength);
|
|
95
|
-
target.set(value.subarray(0, n), written);
|
|
96
|
-
written += n;
|
|
97
|
-
if (n < value.byteLength) pending = value.subarray(n);
|
|
98
|
-
}
|
|
99
|
-
byob.respond(written);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (pending && pending.byteLength > 0) {
|
|
103
|
-
const chunk = pending;
|
|
104
|
-
pending = null;
|
|
105
|
-
controller.enqueue(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
const { done, value } = await reader.read();
|
|
109
|
-
if (done) {
|
|
110
|
-
controller.close();
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
if (value && value.byteLength > 0) controller.enqueue(value);
|
|
114
|
-
} catch (error) {
|
|
115
|
-
controller.error(mapError(error));
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
async cancel(reason) {
|
|
119
|
-
pending = null;
|
|
120
|
-
await reader.cancel(reason);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
return new Response(wrapped, {
|
|
124
|
-
status: response.status,
|
|
125
|
-
statusText: response.statusText,
|
|
126
|
-
headers: new Headers(response.headers)
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
function toWebFetchError(error, signal) {
|
|
130
|
-
if (signal?.aborted) return getRawAbortReason(signal);
|
|
131
|
-
if (error instanceof DOMException || error instanceof TypeError) return error;
|
|
132
|
-
const cause = error instanceof Error ? error : unknownToError(error);
|
|
133
|
-
return new TypeError("fetch failed", { cause });
|
|
134
|
-
}
|
|
135
|
-
function toWebBodyReadError(error, signal) {
|
|
136
|
-
if (signal?.aborted) return getRawAbortReason(signal);
|
|
137
|
-
if (error instanceof DOMException || error instanceof TypeError || error instanceof RangeError) return error;
|
|
138
|
-
const cause = error instanceof Error ? error : unknownToError(error);
|
|
139
|
-
return new TypeError("Failed to read response body", { cause });
|
|
140
|
-
}
|
|
141
|
-
//#endregion
|
|
142
|
-
exports.mapAdvancedConnectError = mapAdvancedConnectError;
|
|
143
|
-
exports.mapAdvancedSendError = mapAdvancedSendError;
|
|
144
|
-
exports.toWebBodyReadError = toWebBodyReadError;
|
|
145
|
-
exports.toWebFetchError = toWebFetchError;
|
|
146
|
-
exports.wrapResponseBodyErrors = wrapResponseBodyErrors;
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { ConnectTimeoutError, ConnectionError, FetchError, RequestAbortedError, RequestWriteError, ResponseBodyError, ResponseDecodeError, ResponseHeaderError } from "../errors.js";
|
|
2
|
-
//#region src/_internal/error-adapters.ts
|
|
3
|
-
function getErrorChain(error, maxDepth = 8) {
|
|
4
|
-
const chain = [];
|
|
5
|
-
const seen = /* @__PURE__ */ new Set();
|
|
6
|
-
let current = error;
|
|
7
|
-
let depth = 0;
|
|
8
|
-
while (current instanceof Error && depth < maxDepth && !seen.has(current)) {
|
|
9
|
-
const entry = current;
|
|
10
|
-
chain.push(entry);
|
|
11
|
-
seen.add(current);
|
|
12
|
-
current = entry.cause;
|
|
13
|
-
depth += 1;
|
|
14
|
-
}
|
|
15
|
-
return chain;
|
|
16
|
-
}
|
|
17
|
-
function unknownToError(error) {
|
|
18
|
-
if (error instanceof Error) return error;
|
|
19
|
-
if (typeof error === "string") return new Error(error);
|
|
20
|
-
return new Error("Unknown error", { cause: error });
|
|
21
|
-
}
|
|
22
|
-
function isAbortLike(error) {
|
|
23
|
-
if (!error || typeof error !== "object") return false;
|
|
24
|
-
const name = "name" in error ? error.name : void 0;
|
|
25
|
-
return name === "AbortError" || name === "TimeoutError";
|
|
26
|
-
}
|
|
27
|
-
function defaultAbortDomException() {
|
|
28
|
-
return new DOMException("This operation was aborted", "AbortError");
|
|
29
|
-
}
|
|
30
|
-
function getRawAbortReason(signal) {
|
|
31
|
-
return signal?.reason ?? defaultAbortDomException();
|
|
32
|
-
}
|
|
33
|
-
function toAbortCause(signal, fallback) {
|
|
34
|
-
return unknownToError(signal?.reason ?? fallback ?? defaultAbortDomException());
|
|
35
|
-
}
|
|
36
|
-
function isTimeoutReason(reason) {
|
|
37
|
-
return !!reason && typeof reason === "object" && "name" in reason && reason.name === "TimeoutError";
|
|
38
|
-
}
|
|
39
|
-
function looksLikeDecodeError(error) {
|
|
40
|
-
const chain = getErrorChain(error);
|
|
41
|
-
for (const entry of chain) {
|
|
42
|
-
const code = typeof entry.code === "string" ? entry.code.toLowerCase() : "";
|
|
43
|
-
const text = `${entry.name} ${entry.message} ${code}`.toLowerCase();
|
|
44
|
-
if (code === "z_data_error" || code === "z_buf_error" || code === "z_stream_error" || text.includes("decode") || text.includes("decompress") || text.includes("encoding") || text.includes("gzip") || text.includes("deflate") || text.includes("brotli") || text.includes("incorrect header check") || text.includes("invalid block type") || text.includes("invalid distance") || text.includes("invalid stored block") || text.includes("unexpected end of file") || text.includes("unexpected end of stream") || text.includes("header check")) return true;
|
|
45
|
-
}
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
function mapAdvancedConnectError(error, { signal, context, timedOut }) {
|
|
49
|
-
if (error instanceof FetchError) return error;
|
|
50
|
-
if (timedOut || isTimeoutReason(signal?.reason)) return new ConnectTimeoutError(unknownToError(error), context, "Connection timeout");
|
|
51
|
-
if (signal?.aborted || isAbortLike(error)) return new RequestAbortedError(toAbortCause(signal, error), context, "The request was aborted while connecting");
|
|
52
|
-
return new ConnectionError(unknownToError(error), context, "Connection failed");
|
|
53
|
-
}
|
|
54
|
-
function mapAdvancedSendError(error, { signal, context, phase }) {
|
|
55
|
-
if (error instanceof FetchError) return error;
|
|
56
|
-
if (signal?.aborted || isAbortLike(error)) return new RequestAbortedError(toAbortCause(signal, error), context, phase === "body" ? "The request was aborted while reading the response body" : "The request was aborted");
|
|
57
|
-
const cause = unknownToError(error);
|
|
58
|
-
if (phase === "request") return new RequestWriteError(cause, context);
|
|
59
|
-
if (phase === "response") return new ResponseHeaderError(cause, context);
|
|
60
|
-
if (looksLikeDecodeError(cause)) return new ResponseDecodeError(cause, context);
|
|
61
|
-
return new ResponseBodyError(cause, context);
|
|
62
|
-
}
|
|
63
|
-
function wrapResponseBodyErrors(response, mapError) {
|
|
64
|
-
const source = response.body;
|
|
65
|
-
if (!source) return response;
|
|
66
|
-
const reader = source.getReader();
|
|
67
|
-
let pending = null;
|
|
68
|
-
const wrapped = new ReadableStream({
|
|
69
|
-
type: "bytes",
|
|
70
|
-
async pull(controller) {
|
|
71
|
-
try {
|
|
72
|
-
const byob = controller.byobRequest;
|
|
73
|
-
if (byob?.view) {
|
|
74
|
-
const target = new Uint8Array(byob.view.buffer, byob.view.byteOffset, byob.view.byteLength);
|
|
75
|
-
if (target.byteLength === 0) {
|
|
76
|
-
byob.respond(0);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
let written = 0;
|
|
80
|
-
if (pending && pending.byteLength > 0) {
|
|
81
|
-
const n = Math.min(target.byteLength, pending.byteLength);
|
|
82
|
-
target.set(pending.subarray(0, n), written);
|
|
83
|
-
written += n;
|
|
84
|
-
pending = n === pending.byteLength ? null : pending.subarray(n);
|
|
85
|
-
}
|
|
86
|
-
while (written === 0) {
|
|
87
|
-
const { done, value } = await reader.read();
|
|
88
|
-
if (done) {
|
|
89
|
-
byob.respond(0);
|
|
90
|
-
controller.close();
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
if (!value || value.byteLength === 0) continue;
|
|
94
|
-
const n = Math.min(target.byteLength - written, value.byteLength);
|
|
95
|
-
target.set(value.subarray(0, n), written);
|
|
96
|
-
written += n;
|
|
97
|
-
if (n < value.byteLength) pending = value.subarray(n);
|
|
98
|
-
}
|
|
99
|
-
byob.respond(written);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (pending && pending.byteLength > 0) {
|
|
103
|
-
const chunk = pending;
|
|
104
|
-
pending = null;
|
|
105
|
-
controller.enqueue(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
const { done, value } = await reader.read();
|
|
109
|
-
if (done) {
|
|
110
|
-
controller.close();
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
if (value && value.byteLength > 0) controller.enqueue(value);
|
|
114
|
-
} catch (error) {
|
|
115
|
-
controller.error(mapError(error));
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
async cancel(reason) {
|
|
119
|
-
pending = null;
|
|
120
|
-
await reader.cancel(reason);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
return new Response(wrapped, {
|
|
124
|
-
status: response.status,
|
|
125
|
-
statusText: response.statusText,
|
|
126
|
-
headers: new Headers(response.headers)
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
function toWebFetchError(error, signal) {
|
|
130
|
-
if (signal?.aborted) return getRawAbortReason(signal);
|
|
131
|
-
if (error instanceof DOMException || error instanceof TypeError) return error;
|
|
132
|
-
const cause = error instanceof Error ? error : unknownToError(error);
|
|
133
|
-
return new TypeError("fetch failed", { cause });
|
|
134
|
-
}
|
|
135
|
-
function toWebBodyReadError(error, signal) {
|
|
136
|
-
if (signal?.aborted) return getRawAbortReason(signal);
|
|
137
|
-
if (error instanceof DOMException || error instanceof TypeError || error instanceof RangeError) return error;
|
|
138
|
-
const cause = error instanceof Error ? error : unknownToError(error);
|
|
139
|
-
return new TypeError("Failed to read response body", { cause });
|
|
140
|
-
}
|
|
141
|
-
//#endregion
|
|
142
|
-
export { mapAdvancedConnectError, mapAdvancedSendError, toWebBodyReadError, toWebFetchError, wrapResponseBodyErrors };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { FetchError, FetchErrorContext } from '../errors';
|
|
2
|
-
export interface ErrorMappingContext extends FetchErrorContext {
|
|
3
|
-
}
|
|
4
|
-
export declare function unknownToError(error: unknown): Error;
|
|
5
|
-
export declare function isAbortLike(error: unknown): boolean;
|
|
6
|
-
export declare function defaultAbortDomException(): DOMException;
|
|
7
|
-
export declare function getRawAbortReason(signal?: AbortSignal): unknown;
|
|
8
|
-
export declare function toAbortCause(signal?: AbortSignal, fallback?: unknown): Error;
|
|
9
|
-
export declare function isTimeoutReason(reason: unknown): boolean;
|
|
10
|
-
export declare function mapAdvancedConnectError(error: unknown, { signal, context, timedOut, }: {
|
|
11
|
-
signal?: AbortSignal;
|
|
12
|
-
context?: ErrorMappingContext;
|
|
13
|
-
timedOut?: boolean;
|
|
14
|
-
}): FetchError;
|
|
15
|
-
export declare function mapAdvancedSendError(error: unknown, { signal, context, phase, }: {
|
|
16
|
-
signal?: AbortSignal;
|
|
17
|
-
context?: ErrorMappingContext;
|
|
18
|
-
phase: "request" | "response" | "body";
|
|
19
|
-
}): FetchError;
|
|
20
|
-
export declare function wrapResponseBodyErrors(response: Response, mapError: (error: unknown) => unknown): Response;
|
|
21
|
-
export declare function toWebFetchError(error: unknown, signal?: AbortSignal): unknown;
|
|
22
|
-
export declare function toWebBodyReadError(error: unknown, signal?: AbortSignal): unknown;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { FetchError, FetchErrorContext } from '../errors';
|
|
2
|
-
export interface ErrorMappingContext extends FetchErrorContext {
|
|
3
|
-
}
|
|
4
|
-
export declare function unknownToError(error: unknown): Error;
|
|
5
|
-
export declare function isAbortLike(error: unknown): boolean;
|
|
6
|
-
export declare function defaultAbortDomException(): DOMException;
|
|
7
|
-
export declare function getRawAbortReason(signal?: AbortSignal): unknown;
|
|
8
|
-
export declare function toAbortCause(signal?: AbortSignal, fallback?: unknown): Error;
|
|
9
|
-
export declare function isTimeoutReason(reason: unknown): boolean;
|
|
10
|
-
export declare function mapAdvancedConnectError(error: unknown, { signal, context, timedOut, }: {
|
|
11
|
-
signal?: AbortSignal;
|
|
12
|
-
context?: ErrorMappingContext;
|
|
13
|
-
timedOut?: boolean;
|
|
14
|
-
}): FetchError;
|
|
15
|
-
export declare function mapAdvancedSendError(error: unknown, { signal, context, phase, }: {
|
|
16
|
-
signal?: AbortSignal;
|
|
17
|
-
context?: ErrorMappingContext;
|
|
18
|
-
phase: "request" | "response" | "body";
|
|
19
|
-
}): FetchError;
|
|
20
|
-
export declare function wrapResponseBodyErrors(response: Response, mapError: (error: unknown) => unknown): Response;
|
|
21
|
-
export declare function toWebFetchError(error: unknown, signal?: AbortSignal): unknown;
|
|
22
|
-
export declare function toWebBodyReadError(error: unknown, signal?: AbortSignal): unknown;
|
package/src/agent-pool.d.cts
DELETED
package/src/agent-pool.d.ts
DELETED