@npy/fetch 0.1.3 → 0.1.4

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 (141) hide show
  1. package/LICENSE +21 -0
  2. package/_internal/consts.cjs +4 -0
  3. package/_internal/consts.d.cts +3 -0
  4. package/_internal/consts.d.ts +3 -0
  5. package/_internal/consts.js +4 -0
  6. package/_internal/decode-stream-error.cjs +18 -0
  7. package/{src/_internal/decode-stream-error.ts → _internal/decode-stream-error.d.cts} +2 -7
  8. package/_internal/decode-stream-error.d.ts +11 -0
  9. package/_internal/decode-stream-error.js +18 -0
  10. package/_internal/error-mapping.cjs +44 -0
  11. package/_internal/error-mapping.d.cts +15 -0
  12. package/_internal/error-mapping.d.ts +15 -0
  13. package/_internal/error-mapping.js +41 -0
  14. package/_internal/guards.cjs +23 -0
  15. package/_internal/guards.d.cts +15 -0
  16. package/_internal/guards.d.ts +15 -0
  17. package/_internal/guards.js +15 -0
  18. package/_internal/net.cjs +95 -0
  19. package/_internal/net.d.cts +11 -0
  20. package/_internal/net.d.ts +11 -0
  21. package/_internal/net.js +92 -0
  22. package/_internal/promises.cjs +18 -0
  23. package/_internal/promises.d.cts +1 -0
  24. package/_internal/promises.d.ts +1 -0
  25. package/_internal/promises.js +18 -0
  26. package/_internal/streams.cjs +37 -0
  27. package/_internal/streams.d.cts +21 -0
  28. package/_internal/streams.d.ts +21 -0
  29. package/_internal/streams.js +36 -0
  30. package/_internal/symbols.cjs +4 -0
  31. package/_internal/symbols.d.cts +1 -0
  32. package/_internal/symbols.d.ts +1 -0
  33. package/_internal/symbols.js +4 -0
  34. package/_virtual/_rolldown/runtime.cjs +23 -0
  35. package/agent-pool.cjs +96 -0
  36. package/agent-pool.d.cts +2 -0
  37. package/agent-pool.d.ts +2 -0
  38. package/agent-pool.js +95 -0
  39. package/agent.cjs +260 -0
  40. package/agent.d.cts +3 -0
  41. package/agent.d.ts +3 -0
  42. package/agent.js +259 -0
  43. package/body.cjs +105 -0
  44. package/body.d.cts +12 -0
  45. package/body.d.ts +12 -0
  46. package/body.js +102 -0
  47. package/dialers/index.d.cts +3 -0
  48. package/dialers/index.d.ts +3 -0
  49. package/dialers/proxy.cjs +56 -0
  50. package/dialers/proxy.d.cts +27 -0
  51. package/dialers/proxy.d.ts +27 -0
  52. package/dialers/proxy.js +55 -0
  53. package/dialers/tcp.cjs +92 -0
  54. package/dialers/tcp.d.cts +57 -0
  55. package/dialers/tcp.d.ts +57 -0
  56. package/dialers/tcp.js +89 -0
  57. package/encoding.cjs +114 -0
  58. package/encoding.d.cts +35 -0
  59. package/encoding.d.ts +35 -0
  60. package/encoding.js +110 -0
  61. package/errors.cjs +275 -0
  62. package/errors.d.cts +110 -0
  63. package/errors.d.ts +110 -0
  64. package/errors.js +259 -0
  65. package/fetch.cjs +353 -0
  66. package/fetch.d.cts +58 -0
  67. package/fetch.d.ts +58 -0
  68. package/fetch.js +350 -0
  69. package/http-client.cjs +75 -0
  70. package/http-client.d.cts +39 -0
  71. package/http-client.d.ts +39 -0
  72. package/http-client.js +75 -0
  73. package/index.cjs +49 -0
  74. package/index.d.cts +14 -0
  75. package/index.d.ts +14 -0
  76. package/index.js +11 -0
  77. package/io/_utils.cjs +56 -0
  78. package/io/_utils.d.cts +10 -0
  79. package/io/_utils.d.ts +10 -0
  80. package/io/_utils.js +51 -0
  81. package/io/buf-writer.cjs +149 -0
  82. package/io/buf-writer.d.cts +13 -0
  83. package/io/buf-writer.d.ts +13 -0
  84. package/io/buf-writer.js +148 -0
  85. package/io/io.cjs +199 -0
  86. package/io/io.d.cts +5 -0
  87. package/io/io.d.ts +5 -0
  88. package/io/io.js +198 -0
  89. package/io/readers.cjs +337 -0
  90. package/io/readers.d.cts +69 -0
  91. package/io/readers.d.ts +69 -0
  92. package/io/readers.js +333 -0
  93. package/io/writers.cjs +196 -0
  94. package/io/writers.d.cts +22 -0
  95. package/io/writers.d.ts +22 -0
  96. package/io/writers.js +195 -0
  97. package/package.json +30 -25
  98. package/{src/types/agent.ts → types/agent.d.cts} +21 -47
  99. package/types/agent.d.ts +72 -0
  100. package/{src/types/dialer.ts → types/dialer.d.cts} +9 -19
  101. package/types/dialer.d.ts +30 -0
  102. package/types/index.d.cts +2 -0
  103. package/types/index.d.ts +2 -0
  104. package/bun.lock +0 -68
  105. package/examples/custom-proxy-client.ts +0 -32
  106. package/examples/http-client.ts +0 -47
  107. package/examples/proxy.ts +0 -16
  108. package/examples/simple.ts +0 -15
  109. package/src/_internal/consts.ts +0 -3
  110. package/src/_internal/error-mapping.ts +0 -160
  111. package/src/_internal/guards.ts +0 -78
  112. package/src/_internal/net.ts +0 -173
  113. package/src/_internal/promises.ts +0 -22
  114. package/src/_internal/streams.ts +0 -52
  115. package/src/_internal/symbols.ts +0 -1
  116. package/src/agent-pool.ts +0 -157
  117. package/src/agent.ts +0 -408
  118. package/src/body.ts +0 -179
  119. package/src/dialers/index.ts +0 -3
  120. package/src/dialers/proxy.ts +0 -102
  121. package/src/dialers/tcp.ts +0 -162
  122. package/src/encoding.ts +0 -222
  123. package/src/errors.ts +0 -357
  124. package/src/fetch.ts +0 -626
  125. package/src/http-client.ts +0 -111
  126. package/src/index.ts +0 -14
  127. package/src/io/_utils.ts +0 -82
  128. package/src/io/buf-writer.ts +0 -183
  129. package/src/io/io.ts +0 -322
  130. package/src/io/readers.ts +0 -576
  131. package/src/io/writers.ts +0 -331
  132. package/src/types/index.ts +0 -2
  133. package/tests/agent-pool.test.ts +0 -111
  134. package/tests/agent.test.ts +0 -134
  135. package/tests/body.test.ts +0 -228
  136. package/tests/errors.test.ts +0 -152
  137. package/tests/fetch.test.ts +0 -421
  138. package/tests/io-options.test.ts +0 -127
  139. package/tests/multipart.test.ts +0 -348
  140. package/tests/test-utils.ts +0 -335
  141. package/tsconfig.json +0 -15
package/io/io.cjs ADDED
@@ -0,0 +1,199 @@
1
+ const require_streams = require("../_internal/streams.cjs");
2
+ const require_encoding = require("../encoding.cjs");
3
+ const require__utils = require("./_utils.cjs");
4
+ const require_readers = require("./readers.cjs");
5
+ const require_writers = require("./writers.cjs");
6
+ //#region src/io/io.ts
7
+ function parseStatusLine(line) {
8
+ const m = /^HTTP\/(\d+)\.(\d+)\s+(\d{3})(?:\s+(.*))?$/.exec(line);
9
+ if (!m) throw new Error(`Invalid HTTP status line: ${line}`);
10
+ const major = Number(m[1]);
11
+ const minor = Number(m[2]);
12
+ const status = Number(m[3]);
13
+ if (!Number.isFinite(major) || !Number.isFinite(minor) || !Number.isFinite(status)) throw new Error(`Invalid HTTP status line: ${line}`);
14
+ return {
15
+ major,
16
+ minor,
17
+ status,
18
+ statusText: m[4] ?? ""
19
+ };
20
+ }
21
+ function toArrayBufferBytes(bytes) {
22
+ if (bytes.buffer instanceof ArrayBuffer) return bytes;
23
+ return new Uint8Array(bytes);
24
+ }
25
+ function wrapStreamErrors(source, mapError) {
26
+ const reader = source.getReader();
27
+ let pending = null;
28
+ return new ReadableStream({
29
+ type: "bytes",
30
+ async pull(controller) {
31
+ try {
32
+ const byob = controller.byobRequest;
33
+ if (byob?.view) {
34
+ const target = new Uint8Array(byob.view.buffer, byob.view.byteOffset, byob.view.byteLength);
35
+ if (target.byteLength === 0) {
36
+ byob.respond(0);
37
+ return;
38
+ }
39
+ let written = 0;
40
+ if (pending && pending.byteLength > 0) {
41
+ const n = Math.min(target.byteLength, pending.byteLength);
42
+ target.set(pending.subarray(0, n), written);
43
+ written += n;
44
+ pending = n === pending.byteLength ? null : pending.subarray(n);
45
+ }
46
+ while (written === 0) {
47
+ const { done, value } = await reader.read();
48
+ if (done) {
49
+ byob.respond(0);
50
+ controller.close();
51
+ return;
52
+ }
53
+ if (!value || value.byteLength === 0) continue;
54
+ const n = Math.min(target.byteLength - written, value.byteLength);
55
+ target.set(value.subarray(0, n), written);
56
+ written += n;
57
+ if (n < value.byteLength) pending = value.subarray(n);
58
+ }
59
+ byob.respond(written);
60
+ return;
61
+ }
62
+ if (pending && pending.byteLength > 0) {
63
+ const chunk = pending;
64
+ pending = null;
65
+ controller.enqueue(toArrayBufferBytes(chunk));
66
+ return;
67
+ }
68
+ const { done, value } = await reader.read();
69
+ if (done) {
70
+ controller.close();
71
+ return;
72
+ }
73
+ if (value && value.byteLength > 0) controller.enqueue(toArrayBufferBytes(value));
74
+ } catch (error) {
75
+ controller.error(mapError(error));
76
+ }
77
+ },
78
+ async cancel(reason) {
79
+ pending = null;
80
+ await reader.cancel(reason);
81
+ }
82
+ });
83
+ }
84
+ async function readResponse(conn, options = {}, shouldIgnoreBody, onDone, mapBodyError) {
85
+ const lr = new require_readers.LineReader(conn, options);
86
+ const finalize = (() => {
87
+ let called = false;
88
+ return (reusable) => {
89
+ if (called) return;
90
+ called = true;
91
+ queueMicrotask(() => onDone?.(reusable));
92
+ };
93
+ })();
94
+ let statusLine;
95
+ let headers;
96
+ for (;;) {
97
+ const line = await lr.readLine();
98
+ if (line === null) throw new Error("Unexpected EOF while reading status line");
99
+ const parsed = parseStatusLine(line);
100
+ const hdrs = await lr.readHeaders(options);
101
+ if (parsed.status >= 100 && parsed.status < 200) {
102
+ if (parsed.status === 101) throw new Error("HTTP/1.1 protocol upgrades not supported");
103
+ continue;
104
+ }
105
+ statusLine = parsed;
106
+ headers = hdrs;
107
+ break;
108
+ }
109
+ const { major, minor, status, statusText } = statusLine;
110
+ const connectionTokens = require__utils.splitTokens(headers.get("connection"));
111
+ const hasClose = connectionTokens.includes("close");
112
+ const hasKeepAlive = connectionTokens.includes("keep-alive");
113
+ const keepAliveOk = !(major === 1 && minor === 0) || hasKeepAlive;
114
+ const ignoreBody = shouldIgnoreBody(status);
115
+ const te = require__utils.parseTransferEncoding(headers);
116
+ let chunked = false;
117
+ let contentLength = null;
118
+ if (!ignoreBody) if (te.has) {
119
+ chunked = te.chunked;
120
+ contentLength = null;
121
+ headers.delete("content-length");
122
+ } else {
123
+ chunked = false;
124
+ contentLength = require__utils.parseContentLength(headers);
125
+ }
126
+ const reusable = keepAliveOk && !hasClose && (ignoreBody || chunked || contentLength != null);
127
+ if (ignoreBody) {
128
+ finalize(reusable);
129
+ return new Response(null, {
130
+ status,
131
+ statusText,
132
+ headers
133
+ });
134
+ }
135
+ if (chunked) headers.delete("content-length");
136
+ const reader = chunked ? new require_readers.ChunkedBodyReader(lr, options) : new require_readers.BodyReader(lr, contentLength, options);
137
+ let body = new ReadableStream({
138
+ type: "bytes",
139
+ async pull(controller) {
140
+ const byob = controller.byobRequest;
141
+ const view = byob?.view ? new Uint8Array(byob.view.buffer, byob.view.byteOffset, byob.view.byteLength) : new Uint8Array(new ArrayBuffer(options.readChunkSize ?? 16 * 1024));
142
+ try {
143
+ const n = await reader.read(view);
144
+ if (n === 0) {
145
+ byob?.respond(0);
146
+ controller.close();
147
+ finalize(reusable);
148
+ return;
149
+ }
150
+ if (byob) byob.respond(n);
151
+ else controller.enqueue(view.subarray(0, n));
152
+ } catch (err) {
153
+ controller.error(err);
154
+ finalize(false);
155
+ }
156
+ },
157
+ cancel() {
158
+ finalize(false);
159
+ }
160
+ });
161
+ try {
162
+ if (te.has && te.codings.length > 0) {
163
+ const decodedTe = require_encoding.decodeStream(body, te.codings);
164
+ if (decodedTe !== body) {
165
+ if (te.chunked) headers.set("transfer-encoding", "chunked");
166
+ else headers.delete("transfer-encoding");
167
+ headers.delete("content-length");
168
+ }
169
+ body = decodedTe;
170
+ }
171
+ const decompress = options.decompress !== false;
172
+ const contentEncoding = headers.get("content-encoding") ?? void 0;
173
+ if (decompress && contentEncoding) {
174
+ const decodedCe = require_encoding.decodeStream(body, contentEncoding);
175
+ if (decodedCe !== body) {
176
+ headers.delete("content-encoding");
177
+ headers.delete("content-length");
178
+ }
179
+ body = decodedCe;
180
+ }
181
+ const maxDecoded = require__utils.parseMaxBytes(options.maxDecodedBodySize);
182
+ if (maxDecoded != null) body = body.pipeThrough(new require_streams.MaxBytesTransformStream(maxDecoded));
183
+ if (mapBodyError != null) body = wrapStreamErrors(body, mapBodyError);
184
+ } catch (err) {
185
+ finalize(false);
186
+ throw err;
187
+ }
188
+ return new Response(body, {
189
+ status,
190
+ statusText,
191
+ headers
192
+ });
193
+ }
194
+ async function writeRequest(conn, req, options = {}) {
195
+ await require_writers.createRequestWriter(conn, options).write(req);
196
+ }
197
+ //#endregion
198
+ exports.readResponse = readResponse;
199
+ exports.writeRequest = writeRequest;
package/io/io.d.cts ADDED
@@ -0,0 +1,5 @@
1
+ import { IConnection } from '@fuman/net';
2
+ import { LineReader, Readers } from './readers';
3
+ import { Writers } from './writers';
4
+ export declare function readResponse(conn: IConnection<unknown>, options: (Readers.Options & LineReader.ReadHeadersOptions) | undefined, shouldIgnoreBody: (status: number) => boolean, onDone?: (reusable: boolean) => void, mapBodyError?: (err: unknown) => unknown): Promise<Response>;
5
+ export declare function writeRequest(conn: IConnection<unknown>, req: Writers.Request, options?: Writers.Options): Promise<void>;
package/io/io.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { IConnection } from '@fuman/net';
2
+ import { LineReader, Readers } from './readers';
3
+ import { Writers } from './writers';
4
+ export declare function readResponse(conn: IConnection<unknown>, options: (Readers.Options & LineReader.ReadHeadersOptions) | undefined, shouldIgnoreBody: (status: number) => boolean, onDone?: (reusable: boolean) => void, mapBodyError?: (err: unknown) => unknown): Promise<Response>;
5
+ export declare function writeRequest(conn: IConnection<unknown>, req: Writers.Request, options?: Writers.Options): Promise<void>;
package/io/io.js ADDED
@@ -0,0 +1,198 @@
1
+ import { MaxBytesTransformStream } from "../_internal/streams.js";
2
+ import { decodeStream } from "../encoding.js";
3
+ import { parseContentLength, parseMaxBytes, parseTransferEncoding, splitTokens } from "./_utils.js";
4
+ import { BodyReader, ChunkedBodyReader, LineReader } from "./readers.js";
5
+ import { createRequestWriter } from "./writers.js";
6
+ //#region src/io/io.ts
7
+ function parseStatusLine(line) {
8
+ const m = /^HTTP\/(\d+)\.(\d+)\s+(\d{3})(?:\s+(.*))?$/.exec(line);
9
+ if (!m) throw new Error(`Invalid HTTP status line: ${line}`);
10
+ const major = Number(m[1]);
11
+ const minor = Number(m[2]);
12
+ const status = Number(m[3]);
13
+ if (!Number.isFinite(major) || !Number.isFinite(minor) || !Number.isFinite(status)) throw new Error(`Invalid HTTP status line: ${line}`);
14
+ return {
15
+ major,
16
+ minor,
17
+ status,
18
+ statusText: m[4] ?? ""
19
+ };
20
+ }
21
+ function toArrayBufferBytes(bytes) {
22
+ if (bytes.buffer instanceof ArrayBuffer) return bytes;
23
+ return new Uint8Array(bytes);
24
+ }
25
+ function wrapStreamErrors(source, mapError) {
26
+ const reader = source.getReader();
27
+ let pending = null;
28
+ return new ReadableStream({
29
+ type: "bytes",
30
+ async pull(controller) {
31
+ try {
32
+ const byob = controller.byobRequest;
33
+ if (byob?.view) {
34
+ const target = new Uint8Array(byob.view.buffer, byob.view.byteOffset, byob.view.byteLength);
35
+ if (target.byteLength === 0) {
36
+ byob.respond(0);
37
+ return;
38
+ }
39
+ let written = 0;
40
+ if (pending && pending.byteLength > 0) {
41
+ const n = Math.min(target.byteLength, pending.byteLength);
42
+ target.set(pending.subarray(0, n), written);
43
+ written += n;
44
+ pending = n === pending.byteLength ? null : pending.subarray(n);
45
+ }
46
+ while (written === 0) {
47
+ const { done, value } = await reader.read();
48
+ if (done) {
49
+ byob.respond(0);
50
+ controller.close();
51
+ return;
52
+ }
53
+ if (!value || value.byteLength === 0) continue;
54
+ const n = Math.min(target.byteLength - written, value.byteLength);
55
+ target.set(value.subarray(0, n), written);
56
+ written += n;
57
+ if (n < value.byteLength) pending = value.subarray(n);
58
+ }
59
+ byob.respond(written);
60
+ return;
61
+ }
62
+ if (pending && pending.byteLength > 0) {
63
+ const chunk = pending;
64
+ pending = null;
65
+ controller.enqueue(toArrayBufferBytes(chunk));
66
+ return;
67
+ }
68
+ const { done, value } = await reader.read();
69
+ if (done) {
70
+ controller.close();
71
+ return;
72
+ }
73
+ if (value && value.byteLength > 0) controller.enqueue(toArrayBufferBytes(value));
74
+ } catch (error) {
75
+ controller.error(mapError(error));
76
+ }
77
+ },
78
+ async cancel(reason) {
79
+ pending = null;
80
+ await reader.cancel(reason);
81
+ }
82
+ });
83
+ }
84
+ async function readResponse(conn, options = {}, shouldIgnoreBody, onDone, mapBodyError) {
85
+ const lr = new LineReader(conn, options);
86
+ const finalize = (() => {
87
+ let called = false;
88
+ return (reusable) => {
89
+ if (called) return;
90
+ called = true;
91
+ queueMicrotask(() => onDone?.(reusable));
92
+ };
93
+ })();
94
+ let statusLine;
95
+ let headers;
96
+ for (;;) {
97
+ const line = await lr.readLine();
98
+ if (line === null) throw new Error("Unexpected EOF while reading status line");
99
+ const parsed = parseStatusLine(line);
100
+ const hdrs = await lr.readHeaders(options);
101
+ if (parsed.status >= 100 && parsed.status < 200) {
102
+ if (parsed.status === 101) throw new Error("HTTP/1.1 protocol upgrades not supported");
103
+ continue;
104
+ }
105
+ statusLine = parsed;
106
+ headers = hdrs;
107
+ break;
108
+ }
109
+ const { major, minor, status, statusText } = statusLine;
110
+ const connectionTokens = splitTokens(headers.get("connection"));
111
+ const hasClose = connectionTokens.includes("close");
112
+ const hasKeepAlive = connectionTokens.includes("keep-alive");
113
+ const keepAliveOk = !(major === 1 && minor === 0) || hasKeepAlive;
114
+ const ignoreBody = shouldIgnoreBody(status);
115
+ const te = parseTransferEncoding(headers);
116
+ let chunked = false;
117
+ let contentLength = null;
118
+ if (!ignoreBody) if (te.has) {
119
+ chunked = te.chunked;
120
+ contentLength = null;
121
+ headers.delete("content-length");
122
+ } else {
123
+ chunked = false;
124
+ contentLength = parseContentLength(headers);
125
+ }
126
+ const reusable = keepAliveOk && !hasClose && (ignoreBody || chunked || contentLength != null);
127
+ if (ignoreBody) {
128
+ finalize(reusable);
129
+ return new Response(null, {
130
+ status,
131
+ statusText,
132
+ headers
133
+ });
134
+ }
135
+ if (chunked) headers.delete("content-length");
136
+ const reader = chunked ? new ChunkedBodyReader(lr, options) : new BodyReader(lr, contentLength, options);
137
+ let body = new ReadableStream({
138
+ type: "bytes",
139
+ async pull(controller) {
140
+ const byob = controller.byobRequest;
141
+ const view = byob?.view ? new Uint8Array(byob.view.buffer, byob.view.byteOffset, byob.view.byteLength) : new Uint8Array(new ArrayBuffer(options.readChunkSize ?? 16 * 1024));
142
+ try {
143
+ const n = await reader.read(view);
144
+ if (n === 0) {
145
+ byob?.respond(0);
146
+ controller.close();
147
+ finalize(reusable);
148
+ return;
149
+ }
150
+ if (byob) byob.respond(n);
151
+ else controller.enqueue(view.subarray(0, n));
152
+ } catch (err) {
153
+ controller.error(err);
154
+ finalize(false);
155
+ }
156
+ },
157
+ cancel() {
158
+ finalize(false);
159
+ }
160
+ });
161
+ try {
162
+ if (te.has && te.codings.length > 0) {
163
+ const decodedTe = decodeStream(body, te.codings);
164
+ if (decodedTe !== body) {
165
+ if (te.chunked) headers.set("transfer-encoding", "chunked");
166
+ else headers.delete("transfer-encoding");
167
+ headers.delete("content-length");
168
+ }
169
+ body = decodedTe;
170
+ }
171
+ const decompress = options.decompress !== false;
172
+ const contentEncoding = headers.get("content-encoding") ?? void 0;
173
+ if (decompress && contentEncoding) {
174
+ const decodedCe = decodeStream(body, contentEncoding);
175
+ if (decodedCe !== body) {
176
+ headers.delete("content-encoding");
177
+ headers.delete("content-length");
178
+ }
179
+ body = decodedCe;
180
+ }
181
+ const maxDecoded = parseMaxBytes(options.maxDecodedBodySize);
182
+ if (maxDecoded != null) body = body.pipeThrough(new MaxBytesTransformStream(maxDecoded));
183
+ if (mapBodyError != null) body = wrapStreamErrors(body, mapBodyError);
184
+ } catch (err) {
185
+ finalize(false);
186
+ throw err;
187
+ }
188
+ return new Response(body, {
189
+ status,
190
+ statusText,
191
+ headers
192
+ });
193
+ }
194
+ async function writeRequest(conn, req, options = {}) {
195
+ await createRequestWriter(conn, options).write(req);
196
+ }
197
+ //#endregion
198
+ export { readResponse, writeRequest };