@npy/fetch 0.1.2 → 0.1.3

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.
Files changed (142) hide show
  1. package/README.md +143 -50
  2. package/bun.lock +68 -0
  3. package/examples/custom-proxy-client.ts +32 -0
  4. package/examples/http-client.ts +47 -0
  5. package/examples/proxy.ts +16 -0
  6. package/examples/simple.ts +15 -0
  7. package/package.json +25 -30
  8. package/src/_internal/consts.ts +3 -0
  9. package/{_internal/decode-stream-error.d.cts → src/_internal/decode-stream-error.ts} +7 -2
  10. package/src/_internal/error-mapping.ts +160 -0
  11. package/src/_internal/guards.ts +78 -0
  12. package/src/_internal/net.ts +173 -0
  13. package/src/_internal/promises.ts +22 -0
  14. package/src/_internal/streams.ts +52 -0
  15. package/src/_internal/symbols.ts +1 -0
  16. package/src/agent-pool.ts +157 -0
  17. package/src/agent.ts +408 -0
  18. package/src/body.ts +179 -0
  19. package/src/dialers/index.ts +3 -0
  20. package/src/dialers/proxy.ts +102 -0
  21. package/src/dialers/tcp.ts +162 -0
  22. package/src/encoding.ts +222 -0
  23. package/src/errors.ts +357 -0
  24. package/src/fetch.ts +626 -0
  25. package/src/http-client.ts +111 -0
  26. package/src/index.ts +14 -0
  27. package/src/io/_utils.ts +82 -0
  28. package/src/io/buf-writer.ts +183 -0
  29. package/src/io/io.ts +322 -0
  30. package/src/io/readers.ts +576 -0
  31. package/src/io/writers.ts +331 -0
  32. package/{types/agent.d.cts → src/types/agent.ts} +47 -21
  33. package/{types/dialer.d.cts → src/types/dialer.ts} +19 -9
  34. package/src/types/index.ts +2 -0
  35. package/tests/agent-pool.test.ts +111 -0
  36. package/tests/agent.test.ts +134 -0
  37. package/tests/body.test.ts +228 -0
  38. package/tests/errors.test.ts +152 -0
  39. package/tests/fetch.test.ts +421 -0
  40. package/tests/io-options.test.ts +127 -0
  41. package/tests/multipart.test.ts +348 -0
  42. package/tests/test-utils.ts +335 -0
  43. package/tsconfig.json +15 -0
  44. package/LICENSE +0 -21
  45. package/_internal/consts.cjs +0 -4
  46. package/_internal/consts.d.cts +0 -3
  47. package/_internal/consts.d.ts +0 -3
  48. package/_internal/consts.js +0 -4
  49. package/_internal/decode-stream-error.cjs +0 -18
  50. package/_internal/decode-stream-error.d.ts +0 -11
  51. package/_internal/decode-stream-error.js +0 -18
  52. package/_internal/error-mapping.cjs +0 -44
  53. package/_internal/error-mapping.d.cts +0 -15
  54. package/_internal/error-mapping.d.ts +0 -15
  55. package/_internal/error-mapping.js +0 -41
  56. package/_internal/guards.cjs +0 -23
  57. package/_internal/guards.d.cts +0 -15
  58. package/_internal/guards.d.ts +0 -15
  59. package/_internal/guards.js +0 -15
  60. package/_internal/net.cjs +0 -95
  61. package/_internal/net.d.cts +0 -11
  62. package/_internal/net.d.ts +0 -11
  63. package/_internal/net.js +0 -92
  64. package/_internal/promises.cjs +0 -18
  65. package/_internal/promises.d.cts +0 -1
  66. package/_internal/promises.d.ts +0 -1
  67. package/_internal/promises.js +0 -18
  68. package/_internal/streams.cjs +0 -37
  69. package/_internal/streams.d.cts +0 -21
  70. package/_internal/streams.d.ts +0 -21
  71. package/_internal/streams.js +0 -36
  72. package/_internal/symbols.cjs +0 -4
  73. package/_internal/symbols.d.cts +0 -1
  74. package/_internal/symbols.d.ts +0 -1
  75. package/_internal/symbols.js +0 -4
  76. package/_virtual/_rolldown/runtime.cjs +0 -23
  77. package/agent-pool.cjs +0 -96
  78. package/agent-pool.d.cts +0 -2
  79. package/agent-pool.d.ts +0 -2
  80. package/agent-pool.js +0 -95
  81. package/agent.cjs +0 -260
  82. package/agent.d.cts +0 -3
  83. package/agent.d.ts +0 -3
  84. package/agent.js +0 -259
  85. package/body.cjs +0 -105
  86. package/body.d.cts +0 -12
  87. package/body.d.ts +0 -12
  88. package/body.js +0 -102
  89. package/dialers/index.d.cts +0 -3
  90. package/dialers/index.d.ts +0 -3
  91. package/dialers/proxy.cjs +0 -56
  92. package/dialers/proxy.d.cts +0 -27
  93. package/dialers/proxy.d.ts +0 -27
  94. package/dialers/proxy.js +0 -55
  95. package/dialers/tcp.cjs +0 -92
  96. package/dialers/tcp.d.cts +0 -57
  97. package/dialers/tcp.d.ts +0 -57
  98. package/dialers/tcp.js +0 -89
  99. package/encoding.cjs +0 -114
  100. package/encoding.d.cts +0 -35
  101. package/encoding.d.ts +0 -35
  102. package/encoding.js +0 -110
  103. package/errors.cjs +0 -275
  104. package/errors.d.cts +0 -110
  105. package/errors.d.ts +0 -110
  106. package/errors.js +0 -259
  107. package/fetch.cjs +0 -353
  108. package/fetch.d.cts +0 -58
  109. package/fetch.d.ts +0 -58
  110. package/fetch.js +0 -350
  111. package/http-client.cjs +0 -75
  112. package/http-client.d.cts +0 -39
  113. package/http-client.d.ts +0 -39
  114. package/http-client.js +0 -75
  115. package/index.cjs +0 -49
  116. package/index.d.cts +0 -14
  117. package/index.d.ts +0 -14
  118. package/index.js +0 -11
  119. package/io/_utils.cjs +0 -56
  120. package/io/_utils.d.cts +0 -10
  121. package/io/_utils.d.ts +0 -10
  122. package/io/_utils.js +0 -51
  123. package/io/buf-writer.cjs +0 -149
  124. package/io/buf-writer.d.cts +0 -13
  125. package/io/buf-writer.d.ts +0 -13
  126. package/io/buf-writer.js +0 -148
  127. package/io/io.cjs +0 -199
  128. package/io/io.d.cts +0 -5
  129. package/io/io.d.ts +0 -5
  130. package/io/io.js +0 -198
  131. package/io/readers.cjs +0 -337
  132. package/io/readers.d.cts +0 -69
  133. package/io/readers.d.ts +0 -69
  134. package/io/readers.js +0 -333
  135. package/io/writers.cjs +0 -196
  136. package/io/writers.d.cts +0 -22
  137. package/io/writers.d.ts +0 -22
  138. package/io/writers.js +0 -195
  139. package/types/agent.d.ts +0 -72
  140. package/types/dialer.d.ts +0 -30
  141. package/types/index.d.cts +0 -2
  142. package/types/index.d.ts +0 -2
package/io/writers.js DELETED
@@ -1,195 +0,0 @@
1
- import { bytesToStream } from "../_internal/streams.js";
2
- import { createEncoders, encodeStream } from "../encoding.js";
3
- import { parseContentLength, parseTransferEncoding } from "./_utils.js";
4
- import { sanitizeHeaderValue } from "./readers.js";
5
- import { isFumanReadable, isReadableStream } from "../_internal/guards.js";
6
- import { extractBody } from "../body.js";
7
- import { BufWriter } from "./buf-writer.js";
8
- import { nodeReadableToWeb } from "@fuman/node";
9
- import { Bytes, fumanReadableToWeb, write } from "@fuman/io";
10
- //#region src/io/writers.ts
11
- function toRequestTarget(url) {
12
- return (url.pathname?.startsWith("/") ? url.pathname : "/") + (url.search || "");
13
- }
14
- function encodeHead(into, head) {
15
- write.rawString(into, `${head.method.toUpperCase()} ${head.target} HTTP/1.1\r
16
- `);
17
- for (const [k, v] of head.headers) write.rawString(into, `${k}: ${sanitizeHeaderValue(v)}\r
18
- `);
19
- write.rawString(into, "\r\n");
20
- }
21
- function prepareBody(headers, init) {
22
- const state = extractBody(init);
23
- if (state.body != null && !headers.has("content-type")) headers.set("content-type", state.contentType ?? "application/octet-stream");
24
- const body = state.body;
25
- if (body == null) return { kind: "none" };
26
- if (body instanceof Uint8Array) return {
27
- kind: "bytes",
28
- bytes: body,
29
- length: body.byteLength
30
- };
31
- if (isReadableStream(body)) return {
32
- kind: "stream",
33
- stream: body,
34
- length: state.contentLength
35
- };
36
- if (isFumanReadable(body)) return {
37
- kind: "stream",
38
- stream: fumanReadableToWeb(body),
39
- length: state.contentLength
40
- };
41
- return {
42
- kind: "stream",
43
- stream: nodeReadableToWeb(body),
44
- length: state.contentLength
45
- };
46
- }
47
- function finalizeDelimitation(headers, body) {
48
- if (body.kind === "none") return { chunked: false };
49
- const te = parseTransferEncoding(headers);
50
- if (te.has) {
51
- headers.delete("content-length");
52
- if (!te.chunked) {
53
- const tokens = [...te.codings, "chunked"].filter(Boolean);
54
- headers.set("transfer-encoding", tokens.join(", "));
55
- }
56
- return { chunked: true };
57
- }
58
- const knownLength = body.kind === "bytes" ? body.length : body.length ?? null;
59
- if (knownLength != null) {
60
- const existing = parseContentLength(headers);
61
- if (existing != null && existing !== knownLength) throw new Error(`Conflicting content-length: header=${existing} body=${knownLength}`);
62
- if (existing == null) headers.set("content-length", String(knownLength));
63
- return { chunked: false };
64
- }
65
- if (parseContentLength(headers) != null) return { chunked: false };
66
- headers.set("transfer-encoding", "chunked");
67
- headers.delete("content-length");
68
- return { chunked: true };
69
- }
70
- function createBufferedConnWriter(dst, opts) {
71
- const bufferSize = opts.writeBufferSize ?? 16 * 1024;
72
- const directWriteThreshold = opts.directWriteThreshold ?? 64 * 1024;
73
- const bufWriter = new BufWriter(dst, bufferSize);
74
- const flush = async () => {
75
- await bufWriter.flush();
76
- };
77
- const writeBytes = async (bytes) => {
78
- if (bytes.length === 0) return;
79
- if (bytes.length >= directWriteThreshold) {
80
- await bufWriter.flush();
81
- await dst.write(bytes);
82
- return;
83
- }
84
- await bufWriter.write(bytes);
85
- };
86
- const writeRawString = async (str) => {
87
- if (str.length === 0) return;
88
- write.rawString(bufWriter, str);
89
- };
90
- return {
91
- flush,
92
- writeBytes,
93
- writeRawString,
94
- directWriteThreshold
95
- };
96
- }
97
- async function writeBody(dst, body, chunked, opts, signal) {
98
- const bw = createBufferedConnWriter(dst, opts);
99
- const writeChunk = async (chunk) => {
100
- if (signal?.aborted) throw signal.reason ?? /* @__PURE__ */ new Error("Request aborted");
101
- if (!chunked) {
102
- await bw.writeBytes(chunk);
103
- return;
104
- }
105
- if (chunk.length === 0) return;
106
- await bw.writeRawString(chunk.length.toString(16));
107
- await bw.writeRawString("\r\n");
108
- await bw.writeBytes(chunk);
109
- await bw.writeRawString("\r\n");
110
- };
111
- if (body.kind === "bytes") await writeChunk(body.bytes);
112
- else for await (const chunk of body.stream) await writeChunk(chunk);
113
- if (chunked) await bw.writeRawString(`0\r
114
- \r
115
- `);
116
- await bw.flush();
117
- }
118
- async function writeCoalesced(dst, scratch, head, bodyBytes, chunked) {
119
- scratch.reset();
120
- encodeHead(scratch, head);
121
- if (!chunked) {
122
- write.bytes(scratch, bodyBytes);
123
- await dst.write(scratch.result());
124
- scratch.reset();
125
- return;
126
- }
127
- write.rawString(scratch, bodyBytes.length.toString(16));
128
- write.rawString(scratch, "\r\n");
129
- write.bytes(scratch, bodyBytes);
130
- write.rawString(scratch, `\r
131
- 0\r
132
- \r
133
- `);
134
- await dst.write(scratch.result());
135
- scratch.reset();
136
- }
137
- function createRequestWriter(dst, opts = {}) {
138
- const scratch = Bytes.alloc(opts.writeBufferSize ?? 16 * 1024);
139
- const write = async (req) => {
140
- if (req.signal?.aborted) throw req.signal.reason ?? /* @__PURE__ */ new Error("Request aborted");
141
- const method = req.method.toUpperCase();
142
- const headers = req.headers ? new Headers(req.headers) : new Headers();
143
- const url = req.url;
144
- if (!headers.has("host")) headers.set("host", url.host);
145
- if (!headers.has("date")) headers.set("date", (/* @__PURE__ */ new Date()).toUTCString());
146
- const target = toRequestTarget(url);
147
- let body = prepareBody(headers, req.body ?? null);
148
- const ceRaw = headers.get("content-encoding") ?? void 0;
149
- const ceEncoders = createEncoders(ceRaw);
150
- if (body.kind !== "none" && ceEncoders.length > 0) {
151
- body = {
152
- kind: "stream",
153
- stream: encodeStream(body.kind === "stream" ? body.stream : bytesToStream(body.bytes), ceRaw),
154
- length: null
155
- };
156
- headers.delete("content-length");
157
- }
158
- const teInfo = parseTransferEncoding(headers);
159
- if (body.kind !== "none" && teInfo.has && teInfo.codings.length > 0) {
160
- body = {
161
- kind: "stream",
162
- stream: encodeStream(body.kind === "stream" ? body.stream : bytesToStream(body.bytes), teInfo.codings),
163
- length: null
164
- };
165
- headers.delete("content-length");
166
- }
167
- const { chunked } = finalizeDelimitation(headers, body);
168
- const head = {
169
- method,
170
- target,
171
- headers
172
- };
173
- if (body.kind === "bytes") {
174
- const max = opts.coalesceBodyMaxBytes ?? 64 * 1024;
175
- if (body.bytes.length <= max) {
176
- await writeCoalesced(dst, scratch, head, body.bytes, chunked);
177
- return;
178
- }
179
- }
180
- scratch.reset();
181
- encodeHead(scratch, head);
182
- await dst.write(scratch.result());
183
- scratch.reset();
184
- if (body.kind === "none") return;
185
- if (body.kind === "bytes" && !chunked) {
186
- if (req.signal?.aborted) throw req.signal.reason ?? /* @__PURE__ */ new Error("Request aborted");
187
- await dst.write(body.bytes);
188
- return;
189
- }
190
- await writeBody(dst, body, chunked, opts, req.signal);
191
- };
192
- return { write };
193
- }
194
- //#endregion
195
- export { createRequestWriter };
package/types/agent.d.ts DELETED
@@ -1,72 +0,0 @@
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/dialer.d.ts DELETED
@@ -1,30 +0,0 @@
1
- import { ITcpConnection, ITlsConnection } from '@fuman/net';
2
- import { NodeTlsConnectOptions } from '@fuman/node';
3
- /**
4
- * Transport abstraction responsible for opening a connection to an HTTP target.
5
- */
6
- export interface Dialer {
7
- dial(target: Dialer.Target, options?: Dialer.Options): Promise<Dialer.ConnectionLike>;
8
- }
9
- export declare namespace Dialer {
10
- type ConnectionLike = ITcpConnection | ITlsConnection;
11
- interface Target {
12
- address: string;
13
- port: number;
14
- secure: boolean;
15
- /** Server Name Indication (TLS). Defaults to the host when applicable. */
16
- sni?: string;
17
- /** Defaults to ["http/1.1"] when omitted by the dialer implementation. */
18
- alpnProtocols?: string[];
19
- /** Extra Node.js TLS options (minVersion, servername, etc.). */
20
- extraOptions?: NodeTlsConnectOptions["extraOptions"];
21
- }
22
- interface Options {
23
- signal?: AbortSignal;
24
- }
25
- }
26
- /** Back-compat */
27
- export type ConnectionLike = Dialer.ConnectionLike;
28
- /** Back-compat */
29
- export interface DialTarget extends Dialer.Target {
30
- }
package/types/index.d.cts DELETED
@@ -1,2 +0,0 @@
1
- export * from './agent';
2
- export * from './dialer';
package/types/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './agent';
2
- export * from './dialer';