@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.
- package/LICENSE +21 -0
- package/_internal/consts.cjs +4 -0
- package/_internal/consts.d.cts +3 -0
- package/_internal/consts.d.ts +3 -0
- package/_internal/consts.js +4 -0
- package/_internal/decode-stream-error.cjs +18 -0
- package/{src/_internal/decode-stream-error.ts → _internal/decode-stream-error.d.cts} +2 -7
- 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 +23 -0
- package/_internal/guards.d.cts +15 -0
- package/_internal/guards.d.ts +15 -0
- package/_internal/guards.js +15 -0
- package/_internal/net.cjs +95 -0
- package/_internal/net.d.cts +11 -0
- package/_internal/net.d.ts +11 -0
- package/_internal/net.js +92 -0
- package/_internal/promises.cjs +18 -0
- package/_internal/promises.d.cts +1 -0
- package/_internal/promises.d.ts +1 -0
- package/_internal/promises.js +18 -0
- package/_internal/streams.cjs +37 -0
- package/_internal/streams.d.cts +21 -0
- package/_internal/streams.d.ts +21 -0
- package/_internal/streams.js +36 -0
- 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/_virtual/_rolldown/runtime.cjs +23 -0
- package/agent-pool.cjs +96 -0
- package/agent-pool.d.cts +2 -0
- package/agent-pool.d.ts +2 -0
- package/agent-pool.js +95 -0
- package/agent.cjs +260 -0
- package/agent.d.cts +3 -0
- package/agent.d.ts +3 -0
- package/agent.js +259 -0
- package/body.cjs +105 -0
- package/body.d.cts +12 -0
- package/body.d.ts +12 -0
- package/body.js +102 -0
- package/dialers/index.d.cts +3 -0
- package/dialers/index.d.ts +3 -0
- package/dialers/proxy.cjs +56 -0
- package/dialers/proxy.d.cts +27 -0
- package/dialers/proxy.d.ts +27 -0
- package/dialers/proxy.js +55 -0
- package/dialers/tcp.cjs +92 -0
- package/dialers/tcp.d.cts +57 -0
- package/dialers/tcp.d.ts +57 -0
- package/dialers/tcp.js +89 -0
- package/encoding.cjs +114 -0
- package/encoding.d.cts +35 -0
- package/encoding.d.ts +35 -0
- package/encoding.js +110 -0
- package/errors.cjs +275 -0
- package/errors.d.cts +110 -0
- package/errors.d.ts +110 -0
- package/errors.js +259 -0
- package/fetch.cjs +353 -0
- package/fetch.d.cts +58 -0
- package/fetch.d.ts +58 -0
- package/fetch.js +350 -0
- package/http-client.cjs +75 -0
- package/http-client.d.cts +39 -0
- package/http-client.d.ts +39 -0
- package/http-client.js +75 -0
- package/index.cjs +49 -0
- package/index.d.cts +14 -0
- package/index.d.ts +14 -0
- package/index.js +11 -0
- package/io/_utils.cjs +56 -0
- package/io/_utils.d.cts +10 -0
- package/io/_utils.d.ts +10 -0
- package/io/_utils.js +51 -0
- package/io/buf-writer.cjs +149 -0
- package/io/buf-writer.d.cts +13 -0
- package/io/buf-writer.d.ts +13 -0
- package/io/buf-writer.js +148 -0
- package/io/io.cjs +199 -0
- package/io/io.d.cts +5 -0
- package/io/io.d.ts +5 -0
- package/io/io.js +198 -0
- package/io/readers.cjs +337 -0
- package/io/readers.d.cts +69 -0
- package/io/readers.d.ts +69 -0
- package/io/readers.js +333 -0
- package/io/writers.cjs +196 -0
- package/io/writers.d.cts +22 -0
- package/io/writers.d.ts +22 -0
- package/io/writers.js +195 -0
- package/package.json +30 -25
- package/{src/types/agent.ts → types/agent.d.cts} +21 -47
- package/types/agent.d.ts +72 -0
- package/{src/types/dialer.ts → types/dialer.d.cts} +9 -19
- package/types/dialer.d.ts +30 -0
- package/types/index.d.cts +2 -0
- package/types/index.d.ts +2 -0
- package/bun.lock +0 -68
- package/examples/custom-proxy-client.ts +0 -32
- package/examples/http-client.ts +0 -47
- package/examples/proxy.ts +0 -16
- package/examples/simple.ts +0 -15
- package/src/_internal/consts.ts +0 -3
- package/src/_internal/error-mapping.ts +0 -160
- package/src/_internal/guards.ts +0 -78
- package/src/_internal/net.ts +0 -173
- package/src/_internal/promises.ts +0 -22
- package/src/_internal/streams.ts +0 -52
- package/src/_internal/symbols.ts +0 -1
- package/src/agent-pool.ts +0 -157
- package/src/agent.ts +0 -408
- package/src/body.ts +0 -179
- package/src/dialers/index.ts +0 -3
- package/src/dialers/proxy.ts +0 -102
- package/src/dialers/tcp.ts +0 -162
- package/src/encoding.ts +0 -222
- package/src/errors.ts +0 -357
- package/src/fetch.ts +0 -626
- package/src/http-client.ts +0 -111
- package/src/index.ts +0 -14
- package/src/io/_utils.ts +0 -82
- package/src/io/buf-writer.ts +0 -183
- package/src/io/io.ts +0 -322
- package/src/io/readers.ts +0 -576
- package/src/io/writers.ts +0 -331
- package/src/types/index.ts +0 -2
- package/tests/agent-pool.test.ts +0 -111
- package/tests/agent.test.ts +0 -134
- package/tests/body.test.ts +0 -228
- package/tests/errors.test.ts +0 -152
- package/tests/fetch.test.ts +0 -421
- package/tests/io-options.test.ts +0 -127
- package/tests/multipart.test.ts +0 -348
- package/tests/test-utils.ts +0 -335
- package/tsconfig.json +0 -15
package/io/readers.cjs
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
const require__utils = require("./_utils.cjs");
|
|
3
|
+
const require_consts = require("../_internal/consts.cjs");
|
|
4
|
+
let _fuman_io = require("@fuman/io");
|
|
5
|
+
let _fuman_net = require("@fuman/net");
|
|
6
|
+
//#region src/io/readers.ts
|
|
7
|
+
var invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/g;
|
|
8
|
+
function sanitizeHeaderValue(v) {
|
|
9
|
+
return v.replace(invalidHeaderCharRegex, (m) => encodeURI(m));
|
|
10
|
+
}
|
|
11
|
+
var LineReader = class {
|
|
12
|
+
#src;
|
|
13
|
+
#buf;
|
|
14
|
+
#codec = new _fuman_io.DelimiterCodec(require_consts.CRLF_BYTES, { strategy: "discard" });
|
|
15
|
+
#eof = false;
|
|
16
|
+
#readChunkSize;
|
|
17
|
+
#maxBufferedBytes;
|
|
18
|
+
#maxLineSize;
|
|
19
|
+
#closed = false;
|
|
20
|
+
close;
|
|
21
|
+
static Options;
|
|
22
|
+
constructor(src, opts = {}) {
|
|
23
|
+
this.#src = src;
|
|
24
|
+
this.#buf = _fuman_io.Bytes.alloc(opts.bufferSize);
|
|
25
|
+
this.#readChunkSize = opts.readChunkSize ?? 16 * 1024;
|
|
26
|
+
this.#maxBufferedBytes = opts.maxBufferedBytes ?? 256 * 1024;
|
|
27
|
+
this.#maxLineSize = opts.maxLineSize ?? 64 * 1024;
|
|
28
|
+
this.close = this.#close.bind(this);
|
|
29
|
+
}
|
|
30
|
+
async read(into) {
|
|
31
|
+
if (this.#closed) return 0;
|
|
32
|
+
if (this.#buf.available > 0) {
|
|
33
|
+
const n = Math.min(into.length, this.#buf.available);
|
|
34
|
+
into.set(this.#buf.readSync(n));
|
|
35
|
+
this.#buf.reclaim();
|
|
36
|
+
return n;
|
|
37
|
+
}
|
|
38
|
+
if (this.#eof) return 0;
|
|
39
|
+
try {
|
|
40
|
+
return await this.#src.read(into);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
if (e instanceof _fuman_net.ConnectionClosedError) {
|
|
43
|
+
this.#eof = true;
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
throw e;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async readLine() {
|
|
50
|
+
if (this.#closed) return null;
|
|
51
|
+
for (;;) {
|
|
52
|
+
const frame = this.#codec.decode(this.#buf, this.#eof);
|
|
53
|
+
if (frame !== null) {
|
|
54
|
+
this.#buf.reclaim();
|
|
55
|
+
return _fuman_io.read.rawString(frame, frame.length);
|
|
56
|
+
}
|
|
57
|
+
if (this.#eof) return null;
|
|
58
|
+
if (this.#buf.available > this.#maxLineSize) throw new Error(`line too large (> ${this.#maxLineSize} bytes)`);
|
|
59
|
+
if (this.#buf.available > this.#maxBufferedBytes) throw new Error(`buffer too large while searching for delimiter (> ${this.#maxBufferedBytes} bytes)`);
|
|
60
|
+
await this.#pull();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async readHeaders(opts = {}) {
|
|
64
|
+
const maxHeaderSize = opts.maxHeaderSize ?? 64 * 1024;
|
|
65
|
+
const acc = /* @__PURE__ */ new Map();
|
|
66
|
+
const validator = new Headers();
|
|
67
|
+
let lastKey = null;
|
|
68
|
+
let firstLine = true;
|
|
69
|
+
let consumed = 0;
|
|
70
|
+
for (;;) {
|
|
71
|
+
const line = await this.readLine();
|
|
72
|
+
if (line === null) throw new Error("Unexpected EOF while reading HTTP headers");
|
|
73
|
+
consumed += line.length + 2;
|
|
74
|
+
if (consumed > maxHeaderSize) throw new Error(`HTTP headers too large (> ${maxHeaderSize} bytes)`);
|
|
75
|
+
if (line === "") break;
|
|
76
|
+
if (firstLine && (line[0] === " " || line[0] === " ")) throw new Error(`malformed HTTP header initial line: ${line}`);
|
|
77
|
+
firstLine = false;
|
|
78
|
+
if (line[0] === " " || line[0] === " ") {
|
|
79
|
+
if (!lastKey) throw new Error(`malformed HTTP header continuation line: ${line}`);
|
|
80
|
+
const arr = acc.get(lastKey);
|
|
81
|
+
if (!arr || arr.length === 0) throw new Error(`malformed HTTP header continuation line: ${line}`);
|
|
82
|
+
const piece = sanitizeHeaderValue(line.trim());
|
|
83
|
+
arr[arr.length - 1] = `${arr[arr.length - 1]} ${piece}`.trim();
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const idx = line.indexOf(":");
|
|
87
|
+
if (idx === -1) throw new Error(`malformed HTTP header line: ${line}`);
|
|
88
|
+
const rawName = line.slice(0, idx).trim();
|
|
89
|
+
if (rawName === "") {
|
|
90
|
+
lastKey = null;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const name = rawName.toLowerCase();
|
|
94
|
+
const value = sanitizeHeaderValue(line.slice(idx + 1).trim());
|
|
95
|
+
try {
|
|
96
|
+
validator.append(name, value);
|
|
97
|
+
} catch {
|
|
98
|
+
lastKey = null;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const arr = acc.get(name);
|
|
102
|
+
if (arr) arr.push(value);
|
|
103
|
+
else acc.set(name, [value]);
|
|
104
|
+
lastKey = name;
|
|
105
|
+
}
|
|
106
|
+
const headers = new Headers();
|
|
107
|
+
for (const [k, values] of acc) for (const v of values) try {
|
|
108
|
+
headers.append(k, v);
|
|
109
|
+
} catch {}
|
|
110
|
+
return headers;
|
|
111
|
+
}
|
|
112
|
+
async #pull() {
|
|
113
|
+
const into = this.#buf.writeSync(this.#readChunkSize);
|
|
114
|
+
try {
|
|
115
|
+
const n = await this.#src.read(into);
|
|
116
|
+
this.#buf.disposeWriteSync(n);
|
|
117
|
+
if (n === 0) this.#eof = true;
|
|
118
|
+
} catch (e) {
|
|
119
|
+
this.#buf.disposeWriteSync(0);
|
|
120
|
+
if (e instanceof _fuman_net.ConnectionClosedError) {
|
|
121
|
+
this.#eof = true;
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
throw e;
|
|
125
|
+
} finally {
|
|
126
|
+
this.#buf.reclaim();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async #close() {
|
|
130
|
+
if (this.#closed) return;
|
|
131
|
+
this.#closed = true;
|
|
132
|
+
await this.#src.close();
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var BodyReader = class {
|
|
136
|
+
#src;
|
|
137
|
+
#remaining;
|
|
138
|
+
#maxResponseSize;
|
|
139
|
+
#readSoFar = 0;
|
|
140
|
+
#closed = false;
|
|
141
|
+
close;
|
|
142
|
+
static Options;
|
|
143
|
+
constructor(src, contentLength, opts = {}) {
|
|
144
|
+
this.#src = src;
|
|
145
|
+
this.#remaining = contentLength;
|
|
146
|
+
this.#maxResponseSize = require__utils.parseMaxBytes(opts.maxBodySize);
|
|
147
|
+
this.close = this.#close.bind(this);
|
|
148
|
+
}
|
|
149
|
+
async read(into) {
|
|
150
|
+
if (this.#closed) return 0;
|
|
151
|
+
if (this.#remaining === 0) return 0;
|
|
152
|
+
if (this.#maxResponseSize != null) {
|
|
153
|
+
const remainingLimit = this.#maxResponseSize - this.#readSoFar;
|
|
154
|
+
if (remainingLimit <= 0) throw new Error(`body too large (> ${this.#maxResponseSize} bytes)`);
|
|
155
|
+
if (into.length > remainingLimit) into = into.subarray(0, remainingLimit);
|
|
156
|
+
}
|
|
157
|
+
let max = into.length;
|
|
158
|
+
if (this.#remaining != null) max = Math.min(max, this.#remaining);
|
|
159
|
+
if (max === 0) return 0;
|
|
160
|
+
const view = max === into.length ? into : into.subarray(0, max);
|
|
161
|
+
let n = 0;
|
|
162
|
+
try {
|
|
163
|
+
n = await this.#src.read(view);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
if (e instanceof _fuman_net.ConnectionClosedError) n = 0;
|
|
166
|
+
else throw e;
|
|
167
|
+
}
|
|
168
|
+
if (n === 0) {
|
|
169
|
+
if (this.#remaining != null) throw new Error("Unexpected EOF while reading fixed-length body");
|
|
170
|
+
return 0;
|
|
171
|
+
}
|
|
172
|
+
this.#readSoFar += n;
|
|
173
|
+
if (this.#remaining != null) this.#remaining -= n;
|
|
174
|
+
return n;
|
|
175
|
+
}
|
|
176
|
+
async #close() {
|
|
177
|
+
if (this.#closed) return;
|
|
178
|
+
this.#closed = true;
|
|
179
|
+
await this.#src.close();
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
var ChunkedBodyReader = class {
|
|
183
|
+
#src;
|
|
184
|
+
#buf;
|
|
185
|
+
#codec = new _fuman_io.DelimiterCodec(require_consts.CRLF_BYTES, { strategy: "discard" });
|
|
186
|
+
#readChunkSize;
|
|
187
|
+
#maxLineSize;
|
|
188
|
+
#maxChunkSize;
|
|
189
|
+
#maxResponseSize;
|
|
190
|
+
#readSoFar = 0;
|
|
191
|
+
#eof = false;
|
|
192
|
+
#closed = false;
|
|
193
|
+
#state = { kind: "size" };
|
|
194
|
+
close;
|
|
195
|
+
static Options;
|
|
196
|
+
constructor(src, opts = {}) {
|
|
197
|
+
this.#src = src;
|
|
198
|
+
this.#buf = _fuman_io.Bytes.alloc(opts.bufferSize);
|
|
199
|
+
this.#readChunkSize = opts.readChunkSize ?? 16 * 1024;
|
|
200
|
+
this.#maxLineSize = opts.maxLineSize ?? 64 * 1024;
|
|
201
|
+
this.#maxChunkSize = opts.maxChunkSize ?? 16 * 1024 * 1024;
|
|
202
|
+
this.#maxResponseSize = require__utils.parseMaxBytes(opts.maxBodySize);
|
|
203
|
+
this.close = this.#close.bind(this);
|
|
204
|
+
}
|
|
205
|
+
async read(into) {
|
|
206
|
+
if (this.#closed) return 0;
|
|
207
|
+
for (;;) {
|
|
208
|
+
if (this.#state.kind === "done") return 0;
|
|
209
|
+
let view = into;
|
|
210
|
+
if (this.#maxResponseSize != null) {
|
|
211
|
+
const remainingLimit = this.#maxResponseSize - this.#readSoFar;
|
|
212
|
+
if (remainingLimit <= 0) throw new Error(`body too large (> ${this.#maxResponseSize} bytes)`);
|
|
213
|
+
if (view.length > remainingLimit) view = view.subarray(0, remainingLimit);
|
|
214
|
+
}
|
|
215
|
+
if (view.length === 0) return 0;
|
|
216
|
+
if (this.#state.kind === "data") {
|
|
217
|
+
if (this.#state.remaining === 0) {
|
|
218
|
+
this.#state = { kind: "crlf" };
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (this.#buf.available > 0) {
|
|
222
|
+
const n = Math.min(view.length, this.#state.remaining, this.#buf.available);
|
|
223
|
+
view.set(this.#buf.readSync(n));
|
|
224
|
+
this.#buf.reclaim();
|
|
225
|
+
this.#readSoFar += n;
|
|
226
|
+
this.#state = {
|
|
227
|
+
kind: "data",
|
|
228
|
+
remaining: this.#state.remaining - n
|
|
229
|
+
};
|
|
230
|
+
return n;
|
|
231
|
+
}
|
|
232
|
+
const max = Math.min(view.length, this.#state.remaining);
|
|
233
|
+
const slice = max === view.length ? view : view.subarray(0, max);
|
|
234
|
+
const n = await this.#readFromSrc(slice);
|
|
235
|
+
if (n === 0) throw new Error("Unexpected EOF while reading chunked body");
|
|
236
|
+
this.#readSoFar += n;
|
|
237
|
+
this.#state = {
|
|
238
|
+
kind: "data",
|
|
239
|
+
remaining: this.#state.remaining - n
|
|
240
|
+
};
|
|
241
|
+
return n;
|
|
242
|
+
}
|
|
243
|
+
if (this.#state.kind === "size") {
|
|
244
|
+
const line = await this.#readLine();
|
|
245
|
+
if (line === null) throw new Error("Unexpected EOF while reading chunk size");
|
|
246
|
+
const semi = line.indexOf(";");
|
|
247
|
+
const token = (semi === -1 ? line : line.slice(0, semi)).trim();
|
|
248
|
+
if (token === "") throw new Error(`invalid chunk size line: ${line}`);
|
|
249
|
+
const size = Number.parseInt(token, 16);
|
|
250
|
+
if (!Number.isFinite(size) || Number.isNaN(size) || size < 0) throw new Error(`invalid chunk size: ${token}`);
|
|
251
|
+
if (size > this.#maxChunkSize) throw new Error(`chunk too large (> ${this.#maxChunkSize} bytes)`);
|
|
252
|
+
if (this.#maxResponseSize != null) {
|
|
253
|
+
if (size > this.#maxResponseSize - this.#readSoFar) throw new Error(`body too large (> ${this.#maxResponseSize} bytes)`);
|
|
254
|
+
}
|
|
255
|
+
this.#state = size === 0 ? { kind: "trailers" } : {
|
|
256
|
+
kind: "data",
|
|
257
|
+
remaining: size
|
|
258
|
+
};
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (this.#state.kind === "crlf") {
|
|
262
|
+
await this.#consumeCrlf();
|
|
263
|
+
this.#state = { kind: "size" };
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (this.#state.kind === "trailers") for (;;) {
|
|
267
|
+
const line = await this.#readLine();
|
|
268
|
+
if (line === null) throw new Error("Unexpected EOF while reading chunked trailers");
|
|
269
|
+
if (line === "") {
|
|
270
|
+
this.#state = { kind: "done" };
|
|
271
|
+
return 0;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async #readFromSrc(into) {
|
|
277
|
+
if (this.#eof) return 0;
|
|
278
|
+
try {
|
|
279
|
+
const n = await this.#src.read(into);
|
|
280
|
+
if (n === 0) this.#eof = true;
|
|
281
|
+
return n;
|
|
282
|
+
} catch (e) {
|
|
283
|
+
if (e instanceof _fuman_net.ConnectionClosedError) {
|
|
284
|
+
this.#eof = true;
|
|
285
|
+
return 0;
|
|
286
|
+
}
|
|
287
|
+
throw e;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async #pull() {
|
|
291
|
+
const into = this.#buf.writeSync(this.#readChunkSize);
|
|
292
|
+
try {
|
|
293
|
+
const n = await this.#readFromSrc(into);
|
|
294
|
+
this.#buf.disposeWriteSync(n);
|
|
295
|
+
} catch (e) {
|
|
296
|
+
this.#buf.disposeWriteSync(0);
|
|
297
|
+
if (e instanceof _fuman_net.ConnectionClosedError) {
|
|
298
|
+
this.#eof = true;
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
throw e;
|
|
302
|
+
} finally {
|
|
303
|
+
this.#buf.reclaim();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
async #readLine() {
|
|
307
|
+
for (;;) {
|
|
308
|
+
const frame = this.#codec.decode(this.#buf, this.#eof);
|
|
309
|
+
if (frame !== null) {
|
|
310
|
+
this.#buf.reclaim();
|
|
311
|
+
return _fuman_io.read.rawString(frame, frame.length);
|
|
312
|
+
}
|
|
313
|
+
if (this.#eof) return null;
|
|
314
|
+
if (this.#buf.available > this.#maxLineSize) throw new Error(`chunk line too large (> ${this.#maxLineSize} bytes)`);
|
|
315
|
+
await this.#pull();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
async #consumeCrlf() {
|
|
319
|
+
while (this.#buf.available < 2) {
|
|
320
|
+
if (this.#eof) throw new Error("Unexpected EOF while reading chunk terminator");
|
|
321
|
+
await this.#pull();
|
|
322
|
+
}
|
|
323
|
+
const two = this.#buf.readSync(2);
|
|
324
|
+
this.#buf.reclaim();
|
|
325
|
+
if (two[0] !== require_consts.CRLF_BYTES[0] || two[1] !== require_consts.CRLF_BYTES[1]) throw new Error("Invalid chunked encoding: missing CRLF after chunk data");
|
|
326
|
+
}
|
|
327
|
+
async #close() {
|
|
328
|
+
if (this.#closed) return;
|
|
329
|
+
this.#closed = true;
|
|
330
|
+
await this.#src.close();
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
//#endregion
|
|
334
|
+
exports.BodyReader = BodyReader;
|
|
335
|
+
exports.ChunkedBodyReader = ChunkedBodyReader;
|
|
336
|
+
exports.LineReader = LineReader;
|
|
337
|
+
exports.sanitizeHeaderValue = sanitizeHeaderValue;
|
package/io/readers.d.cts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { IClosable, IReadable } from '@fuman/io';
|
|
2
|
+
type Source = IReadable & IClosable;
|
|
3
|
+
export declare function sanitizeHeaderValue(v: string): string;
|
|
4
|
+
export declare namespace Readers {
|
|
5
|
+
interface BufferingOptions {
|
|
6
|
+
bufferSize?: number;
|
|
7
|
+
readChunkSize?: number;
|
|
8
|
+
}
|
|
9
|
+
interface SizeLimitOptions {
|
|
10
|
+
maxBodySize?: number | string;
|
|
11
|
+
maxDecodedBodySize?: number | string;
|
|
12
|
+
}
|
|
13
|
+
interface DecompressionOptions {
|
|
14
|
+
decompress?: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface DelimiterLimitsOptions {
|
|
17
|
+
maxLineSize?: number;
|
|
18
|
+
maxBufferedBytes?: number;
|
|
19
|
+
}
|
|
20
|
+
interface BodyOptions extends SizeLimitOptions, DecompressionOptions {
|
|
21
|
+
}
|
|
22
|
+
interface ChunkedOptions extends BufferingOptions {
|
|
23
|
+
maxLineSize?: number;
|
|
24
|
+
maxChunkSize?: number;
|
|
25
|
+
}
|
|
26
|
+
type Options = LineReader.Options & BodyOptions & ChunkedOptions;
|
|
27
|
+
}
|
|
28
|
+
export declare class LineReader implements IReadable, IClosable {
|
|
29
|
+
#private;
|
|
30
|
+
close: () => Promise<void> | void;
|
|
31
|
+
static Options: never;
|
|
32
|
+
constructor(src: Source, opts?: LineReader.Options);
|
|
33
|
+
read(into: Uint8Array): Promise<number>;
|
|
34
|
+
readLine(): Promise<string | null>;
|
|
35
|
+
readHeaders(opts?: LineReader.ReadHeadersOptions): Promise<Headers>;
|
|
36
|
+
}
|
|
37
|
+
export declare namespace LineReader {
|
|
38
|
+
interface Options extends Readers.BufferingOptions, Readers.DelimiterLimitsOptions {
|
|
39
|
+
}
|
|
40
|
+
interface ReadHeadersOptions {
|
|
41
|
+
maxHeaderSize?: number;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export declare class BodyReader implements IReadable, IClosable {
|
|
45
|
+
#private;
|
|
46
|
+
close: () => Promise<void> | void;
|
|
47
|
+
static Options: never;
|
|
48
|
+
constructor(src: Source, contentLength: number | null, opts?: BodyReader.Options);
|
|
49
|
+
read(into: Uint8Array): Promise<number>;
|
|
50
|
+
}
|
|
51
|
+
export declare namespace BodyReader {
|
|
52
|
+
interface Options {
|
|
53
|
+
maxBodySize?: number | string;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export declare class ChunkedBodyReader implements IReadable, IClosable {
|
|
57
|
+
#private;
|
|
58
|
+
close: () => Promise<void> | void;
|
|
59
|
+
static Options: never;
|
|
60
|
+
constructor(src: Source, opts?: ChunkedBodyReader.Options);
|
|
61
|
+
read(into: Uint8Array): Promise<number>;
|
|
62
|
+
}
|
|
63
|
+
export declare namespace ChunkedBodyReader {
|
|
64
|
+
interface Options extends BodyReader.Options, Readers.BufferingOptions {
|
|
65
|
+
maxLineSize?: number;
|
|
66
|
+
maxChunkSize?: number;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export {};
|
package/io/readers.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { IClosable, IReadable } from '@fuman/io';
|
|
2
|
+
type Source = IReadable & IClosable;
|
|
3
|
+
export declare function sanitizeHeaderValue(v: string): string;
|
|
4
|
+
export declare namespace Readers {
|
|
5
|
+
interface BufferingOptions {
|
|
6
|
+
bufferSize?: number;
|
|
7
|
+
readChunkSize?: number;
|
|
8
|
+
}
|
|
9
|
+
interface SizeLimitOptions {
|
|
10
|
+
maxBodySize?: number | string;
|
|
11
|
+
maxDecodedBodySize?: number | string;
|
|
12
|
+
}
|
|
13
|
+
interface DecompressionOptions {
|
|
14
|
+
decompress?: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface DelimiterLimitsOptions {
|
|
17
|
+
maxLineSize?: number;
|
|
18
|
+
maxBufferedBytes?: number;
|
|
19
|
+
}
|
|
20
|
+
interface BodyOptions extends SizeLimitOptions, DecompressionOptions {
|
|
21
|
+
}
|
|
22
|
+
interface ChunkedOptions extends BufferingOptions {
|
|
23
|
+
maxLineSize?: number;
|
|
24
|
+
maxChunkSize?: number;
|
|
25
|
+
}
|
|
26
|
+
type Options = LineReader.Options & BodyOptions & ChunkedOptions;
|
|
27
|
+
}
|
|
28
|
+
export declare class LineReader implements IReadable, IClosable {
|
|
29
|
+
#private;
|
|
30
|
+
close: () => Promise<void> | void;
|
|
31
|
+
static Options: never;
|
|
32
|
+
constructor(src: Source, opts?: LineReader.Options);
|
|
33
|
+
read(into: Uint8Array): Promise<number>;
|
|
34
|
+
readLine(): Promise<string | null>;
|
|
35
|
+
readHeaders(opts?: LineReader.ReadHeadersOptions): Promise<Headers>;
|
|
36
|
+
}
|
|
37
|
+
export declare namespace LineReader {
|
|
38
|
+
interface Options extends Readers.BufferingOptions, Readers.DelimiterLimitsOptions {
|
|
39
|
+
}
|
|
40
|
+
interface ReadHeadersOptions {
|
|
41
|
+
maxHeaderSize?: number;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export declare class BodyReader implements IReadable, IClosable {
|
|
45
|
+
#private;
|
|
46
|
+
close: () => Promise<void> | void;
|
|
47
|
+
static Options: never;
|
|
48
|
+
constructor(src: Source, contentLength: number | null, opts?: BodyReader.Options);
|
|
49
|
+
read(into: Uint8Array): Promise<number>;
|
|
50
|
+
}
|
|
51
|
+
export declare namespace BodyReader {
|
|
52
|
+
interface Options {
|
|
53
|
+
maxBodySize?: number | string;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export declare class ChunkedBodyReader implements IReadable, IClosable {
|
|
57
|
+
#private;
|
|
58
|
+
close: () => Promise<void> | void;
|
|
59
|
+
static Options: never;
|
|
60
|
+
constructor(src: Source, opts?: ChunkedBodyReader.Options);
|
|
61
|
+
read(into: Uint8Array): Promise<number>;
|
|
62
|
+
}
|
|
63
|
+
export declare namespace ChunkedBodyReader {
|
|
64
|
+
interface Options extends BodyReader.Options, Readers.BufferingOptions {
|
|
65
|
+
maxLineSize?: number;
|
|
66
|
+
maxChunkSize?: number;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export {};
|