@npy/fetch 0.1.1 → 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.
- package/README.md +143 -50
- package/bun.lock +68 -0
- package/examples/custom-proxy-client.ts +32 -0
- package/examples/http-client.ts +47 -0
- package/examples/proxy.ts +16 -0
- package/examples/simple.ts +15 -0
- package/package.json +36 -25
- package/src/_internal/consts.ts +3 -0
- package/src/_internal/decode-stream-error.ts +16 -0
- package/src/_internal/error-mapping.ts +160 -0
- package/src/_internal/guards.ts +78 -0
- package/src/_internal/net.ts +173 -0
- package/src/_internal/promises.ts +22 -0
- package/src/_internal/streams.ts +52 -0
- package/src/_internal/symbols.ts +1 -0
- package/src/agent-pool.ts +157 -0
- package/src/agent.ts +408 -0
- package/src/body.ts +179 -0
- package/src/dialers/index.ts +3 -0
- package/src/dialers/proxy.ts +102 -0
- package/src/dialers/tcp.ts +162 -0
- package/src/encoding.ts +222 -0
- package/src/errors.ts +357 -0
- package/src/fetch.ts +626 -0
- package/src/http-client.ts +111 -0
- package/src/index.ts +14 -0
- package/src/io/_utils.ts +82 -0
- package/src/io/buf-writer.ts +183 -0
- package/src/io/io.ts +322 -0
- package/src/io/readers.ts +576 -0
- package/src/io/writers.ts +331 -0
- package/src/types/agent.ts +98 -0
- package/src/types/{dialer.d.cts → dialer.ts} +22 -9
- package/src/types/index.ts +2 -0
- package/tests/agent-pool.test.ts +111 -0
- package/tests/agent.test.ts +134 -0
- package/tests/body.test.ts +228 -0
- package/tests/errors.test.ts +152 -0
- package/tests/fetch.test.ts +421 -0
- package/tests/io-options.test.ts +127 -0
- package/tests/multipart.test.ts +348 -0
- package/tests/test-utils.ts +335 -0
- package/tsconfig.json +15 -0
- package/LICENSE +0 -21
- package/_internal/consts.cjs +0 -4
- package/_internal/consts.js +0 -4
- package/_internal/error-adapters.cjs +0 -146
- package/_internal/error-adapters.js +0 -142
- package/_internal/guards.cjs +0 -24
- package/_internal/guards.js +0 -17
- package/_internal/net.cjs +0 -95
- package/_internal/net.js +0 -92
- package/_internal/promises.cjs +0 -18
- package/_internal/promises.js +0 -18
- package/_internal/streams.cjs +0 -37
- package/_internal/streams.js +0 -36
- package/_virtual/_rolldown/runtime.cjs +0 -23
- package/agent-pool.cjs +0 -78
- package/agent-pool.js +0 -77
- package/agent.cjs +0 -257
- package/agent.js +0 -256
- package/body.cjs +0 -154
- package/body.js +0 -151
- package/dialers/proxy.cjs +0 -49
- package/dialers/proxy.js +0 -48
- package/dialers/tcp.cjs +0 -70
- package/dialers/tcp.js +0 -67
- package/encoding.cjs +0 -95
- package/encoding.js +0 -91
- package/errors.cjs +0 -275
- package/errors.js +0 -259
- package/fetch.cjs +0 -117
- package/fetch.js +0 -115
- package/http-client.cjs +0 -33
- package/http-client.js +0 -33
- package/index.cjs +0 -45
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -9
- package/io/_utils.cjs +0 -56
- package/io/_utils.js +0 -51
- package/io/buf-writer.cjs +0 -149
- package/io/buf-writer.js +0 -148
- package/io/io.cjs +0 -135
- package/io/io.js +0 -134
- package/io/readers.cjs +0 -377
- package/io/readers.js +0 -373
- package/io/writers.cjs +0 -191
- package/io/writers.js +0 -190
- package/src/_internal/consts.d.cts +0 -3
- package/src/_internal/consts.d.ts +0 -3
- package/src/_internal/error-adapters.d.cts +0 -22
- package/src/_internal/error-adapters.d.ts +0 -22
- package/src/_internal/guards.d.cts +0 -13
- package/src/_internal/guards.d.ts +0 -13
- package/src/_internal/net.d.cts +0 -12
- package/src/_internal/net.d.ts +0 -12
- package/src/_internal/promises.d.cts +0 -1
- package/src/_internal/promises.d.ts +0 -1
- package/src/_internal/streams.d.cts +0 -21
- package/src/_internal/streams.d.ts +0 -21
- package/src/agent-pool.d.cts +0 -2
- package/src/agent-pool.d.ts +0 -2
- package/src/agent.d.cts +0 -3
- package/src/agent.d.ts +0 -3
- package/src/body.d.cts +0 -23
- package/src/body.d.ts +0 -23
- package/src/dialers/index.d.cts +0 -3
- package/src/dialers/index.d.ts +0 -3
- package/src/dialers/proxy.d.cts +0 -19
- package/src/dialers/proxy.d.ts +0 -19
- package/src/dialers/tcp.d.cts +0 -36
- package/src/dialers/tcp.d.ts +0 -36
- package/src/encoding.d.cts +0 -24
- package/src/encoding.d.ts +0 -24
- package/src/errors.d.cts +0 -110
- package/src/errors.d.ts +0 -110
- 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/_utils.d.cts +0 -10
- package/src/io/_utils.d.ts +0 -10
- package/src/io/buf-writer.d.cts +0 -13
- package/src/io/buf-writer.d.ts +0 -13
- package/src/io/io.d.cts +0 -5
- package/src/io/io.d.ts +0 -5
- package/src/io/readers.d.cts +0 -199
- package/src/io/readers.d.ts +0 -199
- package/src/io/writers.d.cts +0 -22
- package/src/io/writers.d.ts +0 -22
- package/src/types/agent.d.cts +0 -128
- package/src/types/agent.d.ts +0 -128
- package/src/types/dialer.d.ts +0 -27
- package/src/types/index.d.cts +0 -2
- package/src/types/index.d.ts +0 -2
- package/tests/test-utils.d.cts +0 -8
- package/tests/test-utils.d.ts +0 -8
package/io/readers.js
DELETED
|
@@ -1,373 +0,0 @@
|
|
|
1
|
-
import { CRLF_BYTES } from "../_internal/consts.js";
|
|
2
|
-
import { parseMaxBytes } from "./_utils.js";
|
|
3
|
-
import { Bytes, DelimiterCodec, read } from "@fuman/io";
|
|
4
|
-
import { ConnectionClosedError } from "@fuman/net";
|
|
5
|
-
//#region src/io/readers.ts
|
|
6
|
-
var invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/g;
|
|
7
|
-
function sanitizeHeaderValue(v) {
|
|
8
|
-
return v.replace(invalidHeaderCharRegex, (m) => encodeURI(m));
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* CRLF-delimited reader that preserves over-reads in an internal buffer.
|
|
12
|
-
*
|
|
13
|
-
* Key property: once you finish reading headers, any bytes already read beyond
|
|
14
|
-
* the header terminator stay buffered and will be returned by read().
|
|
15
|
-
*/
|
|
16
|
-
var LineReader = class {
|
|
17
|
-
#src;
|
|
18
|
-
#buf;
|
|
19
|
-
#codec = new DelimiterCodec(CRLF_BYTES, { strategy: "discard" });
|
|
20
|
-
#eof = false;
|
|
21
|
-
#highWaterMark;
|
|
22
|
-
#maxBufferedBytes;
|
|
23
|
-
#maxLineSize;
|
|
24
|
-
#closed = false;
|
|
25
|
-
close;
|
|
26
|
-
/**
|
|
27
|
-
* LineReader configuration.
|
|
28
|
-
*
|
|
29
|
-
* @namespace LineReader
|
|
30
|
-
*/
|
|
31
|
-
static Options;
|
|
32
|
-
constructor(src, opts = {}) {
|
|
33
|
-
this.#src = src;
|
|
34
|
-
this.#buf = Bytes.alloc(opts.bufferSize);
|
|
35
|
-
this.#highWaterMark = opts.highWaterMark ?? 16 * 1024;
|
|
36
|
-
this.#maxBufferedBytes = opts.maxBufferedBytes ?? 256 * 1024;
|
|
37
|
-
this.#maxLineSize = opts.maxLineSize ?? 64 * 1024;
|
|
38
|
-
this.close = this.#close.bind(this);
|
|
39
|
-
}
|
|
40
|
-
async read(into) {
|
|
41
|
-
if (this.#closed) return 0;
|
|
42
|
-
if (this.#buf.available > 0) {
|
|
43
|
-
const n = Math.min(into.length, this.#buf.available);
|
|
44
|
-
into.set(this.#buf.readSync(n));
|
|
45
|
-
this.#buf.reclaim();
|
|
46
|
-
return n;
|
|
47
|
-
}
|
|
48
|
-
if (this.#eof) return 0;
|
|
49
|
-
try {
|
|
50
|
-
return await this.#src.read(into);
|
|
51
|
-
} catch (e) {
|
|
52
|
-
if (e instanceof ConnectionClosedError) {
|
|
53
|
-
this.#eof = true;
|
|
54
|
-
return 0;
|
|
55
|
-
}
|
|
56
|
-
throw e;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
async readLine() {
|
|
60
|
-
if (this.#closed) return null;
|
|
61
|
-
for (;;) {
|
|
62
|
-
const frame = this.#codec.decode(this.#buf, this.#eof);
|
|
63
|
-
if (frame !== null) {
|
|
64
|
-
this.#buf.reclaim();
|
|
65
|
-
return read.rawString(frame, frame.length);
|
|
66
|
-
}
|
|
67
|
-
if (this.#eof) return null;
|
|
68
|
-
if (this.#buf.available > this.#maxLineSize) throw new Error(`line too large (> ${this.#maxLineSize} bytes)`);
|
|
69
|
-
if (this.#buf.available > this.#maxBufferedBytes) throw new Error(`buffer too large while searching for delimiter (> ${this.#maxBufferedBytes} bytes)`);
|
|
70
|
-
await this.#pull();
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
async readHeaders(opts = {}) {
|
|
74
|
-
const maxHeaderSize = opts.maxHeaderSize ?? 64 * 1024;
|
|
75
|
-
const acc = /* @__PURE__ */ new Map();
|
|
76
|
-
const validator = new Headers();
|
|
77
|
-
let lastKey = null;
|
|
78
|
-
let firstLine = true;
|
|
79
|
-
let consumed = 0;
|
|
80
|
-
for (;;) {
|
|
81
|
-
const line = await this.readLine();
|
|
82
|
-
if (line === null) throw new Error("Unexpected EOF while reading HTTP headers");
|
|
83
|
-
consumed += line.length + 2;
|
|
84
|
-
if (consumed > maxHeaderSize) throw new Error(`HTTP headers too large (> ${maxHeaderSize} bytes)`);
|
|
85
|
-
if (line === "") break;
|
|
86
|
-
if (firstLine && (line[0] === " " || line[0] === " ")) throw new Error(`malformed HTTP header initial line: ${line}`);
|
|
87
|
-
firstLine = false;
|
|
88
|
-
if (line[0] === " " || line[0] === " ") {
|
|
89
|
-
if (!lastKey) throw new Error(`malformed HTTP header continuation line: ${line}`);
|
|
90
|
-
const arr = acc.get(lastKey);
|
|
91
|
-
if (!arr || arr.length === 0) throw new Error(`malformed HTTP header continuation line: ${line}`);
|
|
92
|
-
const piece = sanitizeHeaderValue(line.trim());
|
|
93
|
-
arr[arr.length - 1] = `${arr[arr.length - 1]} ${piece}`.trim();
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
const idx = line.indexOf(":");
|
|
97
|
-
if (idx === -1) throw new Error(`malformed HTTP header line: ${line}`);
|
|
98
|
-
const rawName = line.slice(0, idx).trim();
|
|
99
|
-
if (rawName === "") {
|
|
100
|
-
lastKey = null;
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
const name = rawName.toLowerCase();
|
|
104
|
-
const value = sanitizeHeaderValue(line.slice(idx + 1).trim());
|
|
105
|
-
try {
|
|
106
|
-
validator.append(name, value);
|
|
107
|
-
} catch {
|
|
108
|
-
lastKey = null;
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
const arr = acc.get(name);
|
|
112
|
-
if (arr) arr.push(value);
|
|
113
|
-
else acc.set(name, [value]);
|
|
114
|
-
lastKey = name;
|
|
115
|
-
}
|
|
116
|
-
const headers = new Headers();
|
|
117
|
-
for (const [k, values] of acc) for (const v of values) try {
|
|
118
|
-
headers.append(k, v);
|
|
119
|
-
} catch {}
|
|
120
|
-
return headers;
|
|
121
|
-
}
|
|
122
|
-
async #pull() {
|
|
123
|
-
const into = this.#buf.writeSync(this.#highWaterMark);
|
|
124
|
-
try {
|
|
125
|
-
const n = await this.#src.read(into);
|
|
126
|
-
this.#buf.disposeWriteSync(n);
|
|
127
|
-
if (n === 0) this.#eof = true;
|
|
128
|
-
} catch (e) {
|
|
129
|
-
this.#buf.disposeWriteSync(0);
|
|
130
|
-
if (e instanceof ConnectionClosedError) {
|
|
131
|
-
this.#eof = true;
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
throw e;
|
|
135
|
-
} finally {
|
|
136
|
-
this.#buf.reclaim();
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
async #close() {
|
|
140
|
-
if (this.#closed) return;
|
|
141
|
-
this.#closed = true;
|
|
142
|
-
try {
|
|
143
|
-
await this.#src.close();
|
|
144
|
-
} finally {
|
|
145
|
-
this.#closed = true;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
/**
|
|
150
|
-
* Body reader that streams bytes with either a fixed Content-Length or until EOF.
|
|
151
|
-
*/
|
|
152
|
-
var BodyReader = class {
|
|
153
|
-
#src;
|
|
154
|
-
#remaining;
|
|
155
|
-
#maxResponseSize;
|
|
156
|
-
#readSoFar = 0;
|
|
157
|
-
#closed = false;
|
|
158
|
-
close;
|
|
159
|
-
/**
|
|
160
|
-
* BodyReader configuration.
|
|
161
|
-
*
|
|
162
|
-
* @namespace BodyReader
|
|
163
|
-
*/
|
|
164
|
-
static Options;
|
|
165
|
-
constructor(src, contentLength, opts = {}) {
|
|
166
|
-
this.#src = src;
|
|
167
|
-
this.#remaining = contentLength >= 0 ? contentLength : null;
|
|
168
|
-
this.#maxResponseSize = parseMaxBytes(opts.maxBodySize);
|
|
169
|
-
if (this.#maxResponseSize != null && this.#remaining != null && this.#remaining > this.#maxResponseSize) throw new Error(`body too large: content-length=${this.#remaining} > maxResponseSize=${this.#maxResponseSize}`);
|
|
170
|
-
this.close = this.#close.bind(this);
|
|
171
|
-
}
|
|
172
|
-
async read(into) {
|
|
173
|
-
if (this.#closed) return 0;
|
|
174
|
-
if (this.#remaining === 0) return 0;
|
|
175
|
-
let max = into.length;
|
|
176
|
-
if (this.#remaining != null) max = Math.min(max, this.#remaining);
|
|
177
|
-
if (this.#maxResponseSize != null) {
|
|
178
|
-
const remainingLimit = this.#maxResponseSize - this.#readSoFar;
|
|
179
|
-
if (remainingLimit <= 0) throw new Error(`body too large (> ${this.#maxResponseSize} bytes)`);
|
|
180
|
-
max = Math.min(max, remainingLimit);
|
|
181
|
-
}
|
|
182
|
-
if (max === 0) return 0;
|
|
183
|
-
const view = max === into.length ? into : into.subarray(0, max);
|
|
184
|
-
let n = 0;
|
|
185
|
-
try {
|
|
186
|
-
n = await this.#src.read(view);
|
|
187
|
-
} catch (e) {
|
|
188
|
-
if (e instanceof ConnectionClosedError) n = 0;
|
|
189
|
-
else throw e;
|
|
190
|
-
}
|
|
191
|
-
if (n === 0) {
|
|
192
|
-
if (this.#remaining != null) throw new Error("Unexpected EOF while reading fixed-length body");
|
|
193
|
-
return 0;
|
|
194
|
-
}
|
|
195
|
-
this.#readSoFar += n;
|
|
196
|
-
if (this.#remaining != null) this.#remaining -= n;
|
|
197
|
-
return n;
|
|
198
|
-
}
|
|
199
|
-
async #close() {
|
|
200
|
-
if (this.#closed) return;
|
|
201
|
-
this.#closed = true;
|
|
202
|
-
try {
|
|
203
|
-
await this.#src.close();
|
|
204
|
-
} finally {
|
|
205
|
-
this.#closed = true;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
/**
|
|
210
|
-
* RFC 7230 chunked transfer-coding decoder.
|
|
211
|
-
*/
|
|
212
|
-
var ChunkedBodyReader = class {
|
|
213
|
-
#src;
|
|
214
|
-
#buf;
|
|
215
|
-
#codec = new DelimiterCodec(CRLF_BYTES, { strategy: "discard" });
|
|
216
|
-
#highWaterMark;
|
|
217
|
-
#maxLineSize;
|
|
218
|
-
#maxChunkSize;
|
|
219
|
-
#maxResponseSize;
|
|
220
|
-
#readSoFar = 0;
|
|
221
|
-
#eof = false;
|
|
222
|
-
#closed = false;
|
|
223
|
-
#state = { kind: "size" };
|
|
224
|
-
close;
|
|
225
|
-
/**
|
|
226
|
-
* ChunkedBodyReader configuration.
|
|
227
|
-
*
|
|
228
|
-
* @namespace ChunkedBodyReader
|
|
229
|
-
*/
|
|
230
|
-
static Options;
|
|
231
|
-
constructor(src, opts = {}) {
|
|
232
|
-
this.#src = src;
|
|
233
|
-
this.#buf = Bytes.alloc(opts.bufferSize);
|
|
234
|
-
this.#highWaterMark = opts.highWaterMark ?? 16 * 1024;
|
|
235
|
-
this.#maxLineSize = opts.maxLineSize ?? 64 * 1024;
|
|
236
|
-
this.#maxChunkSize = opts.maxChunkSize ?? 16 * 1024 * 1024;
|
|
237
|
-
this.#maxResponseSize = parseMaxBytes(opts.maxBodySize);
|
|
238
|
-
this.close = this.#close.bind(this);
|
|
239
|
-
}
|
|
240
|
-
async read(into) {
|
|
241
|
-
if (this.#closed) return 0;
|
|
242
|
-
for (;;) {
|
|
243
|
-
if (this.#state.kind === "done") return 0;
|
|
244
|
-
let view = into;
|
|
245
|
-
if (this.#maxResponseSize != null) {
|
|
246
|
-
const remainingLimit = this.#maxResponseSize - this.#readSoFar;
|
|
247
|
-
if (remainingLimit <= 0) throw new Error(`body too large (> ${this.#maxResponseSize} bytes)`);
|
|
248
|
-
if (view.length > remainingLimit) view = view.subarray(0, remainingLimit);
|
|
249
|
-
}
|
|
250
|
-
if (view.length === 0) return 0;
|
|
251
|
-
if (this.#state.kind === "data") {
|
|
252
|
-
if (this.#state.remaining === 0) {
|
|
253
|
-
this.#state = { kind: "crlf" };
|
|
254
|
-
continue;
|
|
255
|
-
}
|
|
256
|
-
if (this.#buf.available > 0) {
|
|
257
|
-
const n = Math.min(view.length, this.#state.remaining, this.#buf.available);
|
|
258
|
-
view.set(this.#buf.readSync(n));
|
|
259
|
-
this.#buf.reclaim();
|
|
260
|
-
this.#readSoFar += n;
|
|
261
|
-
this.#state = {
|
|
262
|
-
kind: "data",
|
|
263
|
-
remaining: this.#state.remaining - n
|
|
264
|
-
};
|
|
265
|
-
return n;
|
|
266
|
-
}
|
|
267
|
-
const max = Math.min(view.length, this.#state.remaining);
|
|
268
|
-
const slice = max === view.length ? view : view.subarray(0, max);
|
|
269
|
-
const n = await this.#readFromSrc(slice);
|
|
270
|
-
if (n === 0) throw new Error("Unexpected EOF while reading chunked body");
|
|
271
|
-
this.#readSoFar += n;
|
|
272
|
-
this.#state = {
|
|
273
|
-
kind: "data",
|
|
274
|
-
remaining: this.#state.remaining - n
|
|
275
|
-
};
|
|
276
|
-
return n;
|
|
277
|
-
}
|
|
278
|
-
if (this.#state.kind === "size") {
|
|
279
|
-
const line = await this.#readLine();
|
|
280
|
-
if (line === null) throw new Error("Unexpected EOF while reading chunk size");
|
|
281
|
-
const semi = line.indexOf(";");
|
|
282
|
-
const token = (semi === -1 ? line : line.slice(0, semi)).trim();
|
|
283
|
-
if (token === "") throw new Error(`invalid chunk size line: ${line}`);
|
|
284
|
-
const size = Number.parseInt(token, 16);
|
|
285
|
-
if (!Number.isFinite(size) || Number.isNaN(size) || size < 0) throw new Error(`invalid chunk size: ${token}`);
|
|
286
|
-
if (size > this.#maxChunkSize) throw new Error(`chunk too large (> ${this.#maxChunkSize} bytes)`);
|
|
287
|
-
if (this.#maxResponseSize != null) {
|
|
288
|
-
if (size > this.#maxResponseSize - this.#readSoFar) throw new Error(`body too large (> ${this.#maxResponseSize} bytes)`);
|
|
289
|
-
}
|
|
290
|
-
this.#state = size === 0 ? { kind: "trailers" } : {
|
|
291
|
-
kind: "data",
|
|
292
|
-
remaining: size
|
|
293
|
-
};
|
|
294
|
-
continue;
|
|
295
|
-
}
|
|
296
|
-
if (this.#state.kind === "crlf") {
|
|
297
|
-
await this.#consumeCrlf();
|
|
298
|
-
this.#state = { kind: "size" };
|
|
299
|
-
continue;
|
|
300
|
-
}
|
|
301
|
-
if (this.#state.kind === "trailers") for (;;) {
|
|
302
|
-
const line = await this.#readLine();
|
|
303
|
-
if (line === null) throw new Error("Unexpected EOF while reading chunked trailers");
|
|
304
|
-
if (line === "") {
|
|
305
|
-
this.#state = { kind: "done" };
|
|
306
|
-
return 0;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
async #readFromSrc(into) {
|
|
312
|
-
if (this.#eof) return 0;
|
|
313
|
-
try {
|
|
314
|
-
const n = await this.#src.read(into);
|
|
315
|
-
if (n === 0) this.#eof = true;
|
|
316
|
-
return n;
|
|
317
|
-
} catch (e) {
|
|
318
|
-
if (e instanceof ConnectionClosedError) {
|
|
319
|
-
this.#eof = true;
|
|
320
|
-
return 0;
|
|
321
|
-
}
|
|
322
|
-
throw e;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
async #pull() {
|
|
326
|
-
const into = this.#buf.writeSync(this.#highWaterMark);
|
|
327
|
-
try {
|
|
328
|
-
const n = await this.#readFromSrc(into);
|
|
329
|
-
this.#buf.disposeWriteSync(n);
|
|
330
|
-
} catch (e) {
|
|
331
|
-
this.#buf.disposeWriteSync(0);
|
|
332
|
-
if (e instanceof ConnectionClosedError) {
|
|
333
|
-
this.#eof = true;
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
throw e;
|
|
337
|
-
} finally {
|
|
338
|
-
this.#buf.reclaim();
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
async #readLine() {
|
|
342
|
-
for (;;) {
|
|
343
|
-
const frame = this.#codec.decode(this.#buf, this.#eof);
|
|
344
|
-
if (frame !== null) {
|
|
345
|
-
this.#buf.reclaim();
|
|
346
|
-
return read.rawString(frame, frame.length);
|
|
347
|
-
}
|
|
348
|
-
if (this.#eof) return null;
|
|
349
|
-
if (this.#buf.available > this.#maxLineSize) throw new Error(`chunk line too large (> ${this.#maxLineSize} bytes)`);
|
|
350
|
-
await this.#pull();
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
async #consumeCrlf() {
|
|
354
|
-
while (this.#buf.available < 2) {
|
|
355
|
-
if (this.#eof) throw new Error("Unexpected EOF while reading chunk terminator");
|
|
356
|
-
await this.#pull();
|
|
357
|
-
}
|
|
358
|
-
const two = this.#buf.readSync(2);
|
|
359
|
-
this.#buf.reclaim();
|
|
360
|
-
if (two[0] !== CRLF_BYTES[0] || two[1] !== CRLF_BYTES[1]) throw new Error("Invalid chunked encoding: missing CRLF after chunk data");
|
|
361
|
-
}
|
|
362
|
-
async #close() {
|
|
363
|
-
if (this.#closed) return;
|
|
364
|
-
this.#closed = true;
|
|
365
|
-
try {
|
|
366
|
-
await this.#src.close();
|
|
367
|
-
} finally {
|
|
368
|
-
this.#closed = true;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
};
|
|
372
|
-
//#endregion
|
|
373
|
-
export { BodyReader, ChunkedBodyReader, LineReader, sanitizeHeaderValue };
|
package/io/writers.cjs
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
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
|
-
const require_streams = require("../_internal/streams.cjs");
|
|
6
|
-
const require__utils = require("./_utils.cjs");
|
|
7
|
-
const require_readers = require("./readers.cjs");
|
|
8
|
-
const require_buf_writer = require("./buf-writer.cjs");
|
|
9
|
-
let _fuman_node = require("@fuman/node");
|
|
10
|
-
let _fuman_io = require("@fuman/io");
|
|
11
|
-
//#region src/io/writers.ts
|
|
12
|
-
function toRequestTarget(url) {
|
|
13
|
-
return (url.pathname?.startsWith("/") ? url.pathname : "/") + (url.search || "");
|
|
14
|
-
}
|
|
15
|
-
function encodeHead(into, head) {
|
|
16
|
-
_fuman_io.write.rawString(into, `${head.method.toUpperCase()} ${head.target} HTTP/1.1\r
|
|
17
|
-
`);
|
|
18
|
-
for (const [k, v] of head.headers) _fuman_io.write.rawString(into, `${k}: ${require_readers.sanitizeHeaderValue(v)}\r
|
|
19
|
-
`);
|
|
20
|
-
_fuman_io.write.rawString(into, "\r\n");
|
|
21
|
-
}
|
|
22
|
-
function prepareBody(headers, init) {
|
|
23
|
-
const state = require_body.extractBody(init);
|
|
24
|
-
if (state.body != null && !headers.has("content-type")) headers.set("content-type", state.contentType ?? "application/octet-stream");
|
|
25
|
-
const body = state.body;
|
|
26
|
-
if (body == null) return { kind: "none" };
|
|
27
|
-
if (body instanceof Uint8Array) return {
|
|
28
|
-
kind: "bytes",
|
|
29
|
-
bytes: body,
|
|
30
|
-
length: body.byteLength
|
|
31
|
-
};
|
|
32
|
-
if (require_guards.isReadableStream(body)) return {
|
|
33
|
-
kind: "stream",
|
|
34
|
-
stream: body,
|
|
35
|
-
length: state.contentLength
|
|
36
|
-
};
|
|
37
|
-
return {
|
|
38
|
-
kind: "stream",
|
|
39
|
-
stream: (0, _fuman_node.nodeReadableToWeb)(body),
|
|
40
|
-
length: state.contentLength
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
function finalizeDelimitation(headers, body) {
|
|
44
|
-
if (body.kind === "none") return { chunked: false };
|
|
45
|
-
const te = require__utils.parseTransferEncoding(headers);
|
|
46
|
-
if (te.has) {
|
|
47
|
-
headers.delete("content-length");
|
|
48
|
-
if (!te.chunked) {
|
|
49
|
-
const tokens = [...te.codings, "chunked"].filter(Boolean);
|
|
50
|
-
headers.set("transfer-encoding", tokens.join(", "));
|
|
51
|
-
}
|
|
52
|
-
return { chunked: true };
|
|
53
|
-
}
|
|
54
|
-
const knownLength = body.kind === "bytes" ? body.length : body.length ?? null;
|
|
55
|
-
if (knownLength != null) {
|
|
56
|
-
const existing = require__utils.parseContentLength(headers);
|
|
57
|
-
if (existing != null && existing !== knownLength) throw new Error(`Conflicting content-length: header=${existing} body=${knownLength}`);
|
|
58
|
-
if (existing == null) headers.set("content-length", String(knownLength));
|
|
59
|
-
return { chunked: false };
|
|
60
|
-
}
|
|
61
|
-
if (require__utils.parseContentLength(headers) != null) return { chunked: false };
|
|
62
|
-
headers.set("transfer-encoding", "chunked");
|
|
63
|
-
headers.delete("content-length");
|
|
64
|
-
return { chunked: true };
|
|
65
|
-
}
|
|
66
|
-
function createBufferedConnWriter(dst, opts) {
|
|
67
|
-
const bufferSize = opts.highWaterMark ?? 16 * 1024;
|
|
68
|
-
const directWriteThreshold = opts.directWriteThreshold ?? 64 * 1024;
|
|
69
|
-
const bufWriter = new require_buf_writer.BufWriter(dst, bufferSize);
|
|
70
|
-
const flush = async () => {
|
|
71
|
-
await bufWriter.flush();
|
|
72
|
-
};
|
|
73
|
-
const writeBytes = async (bytes) => {
|
|
74
|
-
if (bytes.length === 0) return;
|
|
75
|
-
if (bytes.length >= directWriteThreshold) {
|
|
76
|
-
await bufWriter.flush();
|
|
77
|
-
await dst.write(bytes);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
await bufWriter.write(bytes);
|
|
81
|
-
};
|
|
82
|
-
const writeRawString = async (str) => {
|
|
83
|
-
if (str.length === 0) return;
|
|
84
|
-
_fuman_io.write.rawString(bufWriter, str);
|
|
85
|
-
};
|
|
86
|
-
return {
|
|
87
|
-
flush,
|
|
88
|
-
writeBytes,
|
|
89
|
-
writeRawString,
|
|
90
|
-
directWriteThreshold
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
async function writeBody(dst, body, chunked, opts, signal) {
|
|
94
|
-
const bw = createBufferedConnWriter(dst, opts);
|
|
95
|
-
const writeChunk = async (chunk) => {
|
|
96
|
-
if (signal?.aborted) throw signal.reason ?? /* @__PURE__ */ new Error("Request aborted");
|
|
97
|
-
if (!chunked) {
|
|
98
|
-
await bw.writeBytes(chunk);
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (chunk.length === 0) return;
|
|
102
|
-
await bw.writeRawString(chunk.length.toString(16));
|
|
103
|
-
await bw.writeRawString("\r\n");
|
|
104
|
-
await bw.writeBytes(chunk);
|
|
105
|
-
await bw.writeRawString("\r\n");
|
|
106
|
-
};
|
|
107
|
-
if (body.kind === "bytes") await writeChunk(body.bytes);
|
|
108
|
-
else for await (const chunk of body.stream) await writeChunk(chunk);
|
|
109
|
-
if (chunked) await bw.writeRawString(`0\r
|
|
110
|
-
\r
|
|
111
|
-
`);
|
|
112
|
-
await bw.flush();
|
|
113
|
-
}
|
|
114
|
-
async function writeCoalesced(dst, scratch, head, bodyBytes, chunked) {
|
|
115
|
-
scratch.reset();
|
|
116
|
-
encodeHead(scratch, head);
|
|
117
|
-
if (!chunked) {
|
|
118
|
-
_fuman_io.write.bytes(scratch, bodyBytes);
|
|
119
|
-
await dst.write(scratch.result());
|
|
120
|
-
scratch.reset();
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
_fuman_io.write.rawString(scratch, bodyBytes.length.toString(16));
|
|
124
|
-
_fuman_io.write.rawString(scratch, "\r\n");
|
|
125
|
-
_fuman_io.write.bytes(scratch, bodyBytes);
|
|
126
|
-
_fuman_io.write.rawString(scratch, `\r
|
|
127
|
-
0\r
|
|
128
|
-
\r
|
|
129
|
-
`);
|
|
130
|
-
await dst.write(scratch.result());
|
|
131
|
-
scratch.reset();
|
|
132
|
-
}
|
|
133
|
-
function createRequestWriter(dst, opts = {}) {
|
|
134
|
-
const scratch = _fuman_io.Bytes.alloc(opts.highWaterMark ?? 16 * 1024);
|
|
135
|
-
const write = async (req) => {
|
|
136
|
-
if (req.signal?.aborted) throw req.signal.reason ?? /* @__PURE__ */ new Error("Request aborted");
|
|
137
|
-
const method = req.method.toUpperCase();
|
|
138
|
-
const headers = req.headers ? new Headers(req.headers) : new Headers();
|
|
139
|
-
const url = req.url;
|
|
140
|
-
if (!headers.has("host")) headers.set("host", url.host);
|
|
141
|
-
if (!headers.has("date")) headers.set("date", (/* @__PURE__ */ new Date()).toUTCString());
|
|
142
|
-
const target = toRequestTarget(url);
|
|
143
|
-
let body = prepareBody(headers, req.body ?? null);
|
|
144
|
-
const ceRaw = headers.get("content-encoding") ?? void 0;
|
|
145
|
-
const ceEncoders = require_encoding.createEncoders(ceRaw);
|
|
146
|
-
if (body.kind !== "none" && ceEncoders.length > 0) {
|
|
147
|
-
body = {
|
|
148
|
-
kind: "stream",
|
|
149
|
-
stream: require_encoding.encodeStream(body.kind === "stream" ? body.stream : require_streams.bytesToStream(body.bytes), ceRaw),
|
|
150
|
-
length: null
|
|
151
|
-
};
|
|
152
|
-
headers.delete("content-length");
|
|
153
|
-
}
|
|
154
|
-
const teInfo = require__utils.parseTransferEncoding(headers);
|
|
155
|
-
if (body.kind !== "none" && teInfo.has && teInfo.codings.length > 0) {
|
|
156
|
-
body = {
|
|
157
|
-
kind: "stream",
|
|
158
|
-
stream: require_encoding.encodeStream(body.kind === "stream" ? body.stream : require_streams.bytesToStream(body.bytes), teInfo.codings),
|
|
159
|
-
length: null
|
|
160
|
-
};
|
|
161
|
-
headers.delete("content-length");
|
|
162
|
-
}
|
|
163
|
-
const { chunked } = finalizeDelimitation(headers, body);
|
|
164
|
-
const head = {
|
|
165
|
-
method,
|
|
166
|
-
target,
|
|
167
|
-
headers
|
|
168
|
-
};
|
|
169
|
-
if (body.kind === "bytes") {
|
|
170
|
-
const max = opts.coalesceBodyMaxBytes ?? 64 * 1024;
|
|
171
|
-
if (body.bytes.length <= max) {
|
|
172
|
-
await writeCoalesced(dst, scratch, head, body.bytes, chunked);
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
scratch.reset();
|
|
177
|
-
encodeHead(scratch, head);
|
|
178
|
-
await dst.write(scratch.result());
|
|
179
|
-
scratch.reset();
|
|
180
|
-
if (body.kind === "none") return;
|
|
181
|
-
if (body.kind === "bytes" && !chunked) {
|
|
182
|
-
if (req.signal?.aborted) throw req.signal.reason ?? /* @__PURE__ */ new Error("Request aborted");
|
|
183
|
-
await dst.write(body.bytes);
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
await writeBody(dst, body, chunked, opts, req.signal);
|
|
187
|
-
};
|
|
188
|
-
return { write };
|
|
189
|
-
}
|
|
190
|
-
//#endregion
|
|
191
|
-
exports.createRequestWriter = createRequestWriter;
|