@passcod/faith 0.0.2 → 0.0.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/README.md +292 -74
- package/index.d.ts +534 -31
- package/index.js +57 -52
- package/package.json +15 -14
- package/wrapper.d.ts +341 -95
- package/wrapper.js +283 -275
package/README.md
CHANGED
|
@@ -11,8 +11,8 @@ easily work around its limitations. The native fetch implementation, `undici`, e
|
|
|
11
11
|
HTTP/1.1, and doesn't support HTTP/2+, among many other complaints.
|
|
12
12
|
|
|
13
13
|
Fáith tries to bring a Node.js fetch that is closer to the browser's fetch, notably by having
|
|
14
|
-
transparent support for HTTP/2 and HTTP/3, IPv6 and IPv4 using the "Happy Eyeballs" algorithm,
|
|
15
|
-
|
|
14
|
+
transparent support for HTTP/2 and HTTP/3, IPv6 and IPv4 using the "Happy Eyeballs" algorithm, a
|
|
15
|
+
DNS cache, an optional cookie jar, and your choice of two HTTP caches.
|
|
16
16
|
|
|
17
17
|
## Installation
|
|
18
18
|
|
|
@@ -31,7 +31,7 @@ async function example() {
|
|
|
31
31
|
const response = await fetch('https://httpbin.org/get');
|
|
32
32
|
console.log(response.status); // 200
|
|
33
33
|
console.log(response.ok); // true
|
|
34
|
-
|
|
34
|
+
|
|
35
35
|
const data = response.json();
|
|
36
36
|
console.log(data.url); // https://httpbin.org/get
|
|
37
37
|
}
|
|
@@ -90,6 +90,7 @@ practice the `RequestInit` class does not exist in browsers or Node.js, and so t
|
|
|
90
90
|
|
|
91
91
|
*A `Promise` that resolves to a `Response` object.*
|
|
92
92
|
|
|
93
|
+
<!-- //full duplex mode is not yet implemented//
|
|
93
94
|
In `half` duplex mode (the default), the promise resolves when the request body has been fully sent
|
|
94
95
|
and the response headers have been received. In `full` duplex mode (supported by Fáith but not yet
|
|
95
96
|
browsers), the promise resolves as soon as response headers have been received, even if the request
|
|
@@ -97,6 +98,7 @@ body has not yet finished sending. Most HTTP servers will not send response head
|
|
|
97
98
|
finished receiving the body so this distinction doesn't matter, but some do, and it is possible to
|
|
98
99
|
take advantage of this behaviour with `full` duplex mode for decreased latency in specific cases.
|
|
99
100
|
You may even be able to vary the request body stream based on the response body stream.
|
|
101
|
+
-->
|
|
100
102
|
|
|
101
103
|
## `Request`
|
|
102
104
|
|
|
@@ -118,7 +120,7 @@ then the value passed directly into `fetch()` is used.*
|
|
|
118
120
|
|
|
119
121
|
Note that you can include options that Fáith does not support; they will simply be ignored.
|
|
120
122
|
|
|
121
|
-
### `agent`
|
|
123
|
+
### `FetchOptions.agent: Agent`
|
|
122
124
|
|
|
123
125
|
This is custom to Fáith.
|
|
124
126
|
|
|
@@ -128,11 +130,11 @@ Notably an agent has a DNS cache, and may be configured to handle cookies and/or
|
|
|
128
130
|
|
|
129
131
|
When not provided, a global default `Agent` is created on first use.
|
|
130
132
|
|
|
131
|
-
### `attributionReporting`
|
|
133
|
+
### `FetchOptions.attributionReporting`
|
|
132
134
|
|
|
133
135
|
Fáith deliberately does not implement this.
|
|
134
136
|
|
|
135
|
-
### `body`
|
|
137
|
+
### `FetchOptions.body`
|
|
136
138
|
|
|
137
139
|
*The request body contains content to send to the server, for example in a `POST` or `PUT` request.
|
|
138
140
|
It is specified as an instance of any of the following types:*
|
|
@@ -144,23 +146,57 @@ It is specified as an instance of any of the following types:*
|
|
|
144
146
|
- *`File`*
|
|
145
147
|
- *`FormData`*
|
|
146
148
|
- *`TypedArray`*
|
|
147
|
-
-
|
|
149
|
+
- ~~*`URLSearchParams`*~~ Not yet implemented.
|
|
148
150
|
- *`ReadableStream`* Note that Fáith currently reads this into memory before sending the request.
|
|
149
151
|
|
|
150
152
|
*If `body` is a `ReadableStream`, the `duplex` option must also be set.*
|
|
151
153
|
|
|
152
|
-
### `browsingTopics`
|
|
154
|
+
### `FetchOptions.browsingTopics`
|
|
153
155
|
|
|
154
156
|
Fáith deliberately does not implement this.
|
|
155
157
|
|
|
156
|
-
### `cache`
|
|
158
|
+
### `FetchOptions.cache`
|
|
157
159
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
+
*The cache mode you want to use for the request. This may be any one of the following values:*
|
|
161
|
+
|
|
162
|
+
- *`default`: The client looks in its HTTP cache for a response matching the request.*
|
|
163
|
+
- *If there is a match and it is fresh, it will be returned from the cache.*
|
|
164
|
+
- *If there is a match but it is stale, the client will make a conditional request to the remote
|
|
165
|
+
server. If the server indicates that the resource has not changed, it will be returned from the
|
|
166
|
+
cache. Otherwise the resource will be downloaded from the server and the cache will be updated.*
|
|
167
|
+
- *If there is no match, the client will make a normal request, and will update the cache with
|
|
168
|
+
the downloaded resource.*
|
|
169
|
+
|
|
170
|
+
- *`no-store`: The client fetches the resource from the remote server without first looking in the
|
|
171
|
+
cache, and will not update the cache with the downloaded resource.*
|
|
172
|
+
|
|
173
|
+
- *`reload`: The client fetches the resource from the remote server without first looking in the
|
|
174
|
+
cache, but then will update the cache with the downloaded resource.*
|
|
175
|
+
|
|
176
|
+
- *`no-cache`: The client looks in its HTTP cache for a response matching the request.*
|
|
177
|
+
- *If there is a match, fresh or stale, the client will make a conditional request to the remote
|
|
178
|
+
server. If the server indicates that the resource has not changed, it will be returned from the
|
|
179
|
+
cache. Otherwise the resource will be downloaded from the server and the cache will be updated.*
|
|
180
|
+
- *If there is no match, the client will make a normal request, and will update the cache with
|
|
181
|
+
the downloaded resource.*
|
|
160
182
|
|
|
161
|
-
|
|
183
|
+
- *`force-cache`: The client looks in its HTTP cache for a response matching the request.*
|
|
184
|
+
- *If there is a match, fresh or stale, it will be returned from the cache.*
|
|
185
|
+
- *If there is no match, the client will make a normal request, and will update the cache with
|
|
186
|
+
the downloaded resource.*
|
|
162
187
|
|
|
163
|
-
|
|
188
|
+
- *`only-if-cached`: The client looks in its HTTP cache for a response matching the request.*
|
|
189
|
+
- *If there is a match, fresh or stale, it will be returned from the cache.*
|
|
190
|
+
- *If there is no match, a network error is returned.*
|
|
191
|
+
|
|
192
|
+
- `ignore-rules`: Custom to Fáith. Overrides the check that determines if a response can be cached
|
|
193
|
+
to always return true on 200. Uses any response in the HTTP cache matching the request, not
|
|
194
|
+
paying attention to staleness. If there was no response, it creates a normal request and updates
|
|
195
|
+
the HTTP cache with the response.
|
|
196
|
+
|
|
197
|
+
### `FetchOptions.credentials: string`
|
|
198
|
+
|
|
199
|
+
*Controls whether or not the client sends credentials with the request, as well as whether any
|
|
164
200
|
`Set-Cookie` response headers are respected. Credentials are cookies, ~~TLS client certificates,~~
|
|
165
201
|
or authentication headers containing a username and password. This option may be any one of the
|
|
166
202
|
following values:*
|
|
@@ -180,14 +216,14 @@ This is an upstream limitation.
|
|
|
180
216
|
|
|
181
217
|
Defaults to `include` (browsers default to `same-origin`).
|
|
182
218
|
|
|
183
|
-
### `duplex`
|
|
219
|
+
### `FetchOptions.duplex: string`
|
|
184
220
|
|
|
185
221
|
*Controls duplex behavior of the request. If this is present it must have the value `half`, meaning
|
|
186
222
|
that Fáith will send the entire request before processing the response.*
|
|
187
223
|
|
|
188
224
|
*This option must be present when `body` is a `ReadableStream`.*
|
|
189
225
|
|
|
190
|
-
### `headers`
|
|
226
|
+
### `FetchOptions.headers: Headers | object`
|
|
191
227
|
|
|
192
228
|
*Any headers you want to add to your request, contained within a `Headers` object or an object
|
|
193
229
|
literal whose keys are the names of headers and whose values are the header values.*
|
|
@@ -196,7 +232,7 @@ Fáith allows all request headers to be set (unlike browsers, which [forbid][1]
|
|
|
196
232
|
|
|
197
233
|
[1]: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_request_header
|
|
198
234
|
|
|
199
|
-
### `integrity`
|
|
235
|
+
### `FetchOptions.integrity: string`
|
|
200
236
|
|
|
201
237
|
Not implemented yet.
|
|
202
238
|
|
|
@@ -214,7 +250,7 @@ Fáith only checks the integrity when using `bytes()`, `json()`, `text()`, `arra
|
|
|
214
250
|
Note that browsers will throw at the `fetch()` call when integrity fails, but Fáith will only throw
|
|
215
251
|
when the above methods are called, as until then the body contents are not available.
|
|
216
252
|
|
|
217
|
-
### `keepalive`
|
|
253
|
+
### `FetchOptions.keepalive`
|
|
218
254
|
|
|
219
255
|
Not supported.
|
|
220
256
|
|
|
@@ -223,41 +259,41 @@ single `Agent`, so subsequent requests to the same endpoint are faster until the
|
|
|
223
259
|
times out. The `keepalive` option in browsers is instead a way to send a `fetch()` right before the
|
|
224
260
|
page is unloaded, for tracking or analytics purposes. This concept does not exist in Node.js.
|
|
225
261
|
|
|
226
|
-
### `method`
|
|
262
|
+
### `FetchOptions.method: string`
|
|
227
263
|
|
|
228
264
|
*The request method. Defaults to `GET`.*
|
|
229
265
|
|
|
230
|
-
### `mode`
|
|
266
|
+
### `FetchOptions.mode`
|
|
231
267
|
|
|
232
268
|
Fáith deliberately does not implement this, as there is no CORS/origin.
|
|
233
269
|
|
|
234
|
-
### `priority`
|
|
270
|
+
### `FetchOptions.priority`
|
|
235
271
|
|
|
236
272
|
Not supported.
|
|
237
273
|
|
|
238
|
-
### `redirect`
|
|
274
|
+
### `FetchOptions.redirect`
|
|
239
275
|
|
|
240
276
|
Fáith does not respect this option on the `RequestInit` dictionary. Instead, the option is present
|
|
241
277
|
on `Agent` and applies to all requests made with that `Agent`.
|
|
242
278
|
|
|
243
|
-
### `referrer`
|
|
279
|
+
### `FetchOptions.referrer`
|
|
244
280
|
|
|
245
281
|
Fáith deliberately does not implement this, as there is no origin.
|
|
246
282
|
|
|
247
283
|
However, Fáith does set the `Referer` header when redirecting automatically.
|
|
248
284
|
|
|
249
|
-
### `referrerPolicy`
|
|
285
|
+
### `FetchOptions.referrerPolicy`
|
|
250
286
|
|
|
251
287
|
Fáith deliberately does not implement this, as there is no origin.
|
|
252
288
|
|
|
253
289
|
However, Fáith does set the `Referer` header when redirecting automatically.
|
|
254
290
|
|
|
255
|
-
### `signal`
|
|
291
|
+
### `FetchOptions.signal: AbortSignal`
|
|
256
292
|
|
|
257
293
|
*An `AbortSignal`. If this option is set, the request can be canceled by calling `abort()` on the
|
|
258
294
|
corresponding `AbortController`.*
|
|
259
295
|
|
|
260
|
-
### `timeout`
|
|
296
|
+
### `FetchOptions.timeout: number`
|
|
261
297
|
|
|
262
298
|
Custom to Fáith. Cancels the request after this many milliseconds.
|
|
263
299
|
|
|
@@ -273,7 +309,7 @@ response receipt.
|
|
|
273
309
|
Fáith does not allow its `Response` object to be constructed. If you need to, you may use the
|
|
274
310
|
`webResponse()` method to convert one into a Web API `Response` object; note the caveats.
|
|
275
311
|
|
|
276
|
-
### `Response.body`
|
|
312
|
+
### `Response.body: ReadableStream | null`
|
|
277
313
|
|
|
278
314
|
*The `body` read-only property of the `Response` interface is a `ReadableStream` of the body
|
|
279
315
|
contents,* or `null` for any actual HTTP response that has no body, such as `HEAD` requests and
|
|
@@ -282,7 +318,7 @@ contents,* or `null` for any actual HTTP response that has no body, such as `HEA
|
|
|
282
318
|
Note that browsers currently do not return `null` for those responses, but the spec requires it.
|
|
283
319
|
Fáith chooses to respect the spec rather than the browsers in this case.
|
|
284
320
|
|
|
285
|
-
### `Response.bodyUsed`
|
|
321
|
+
### `Response.bodyUsed: boolean`
|
|
286
322
|
|
|
287
323
|
*The `bodyUsed` read-only property of the `Response` interface is a boolean value that indicates
|
|
288
324
|
whether the body has been read yet.*
|
|
@@ -291,7 +327,7 @@ In Fáith, this indicates whether the body stream has ever been read from or can
|
|
|
291
327
|
[in the spec](https://streams.spec.whatwg.org/#is-readable-stream-disturbed). Note that accessing
|
|
292
328
|
the `.body` property counts as a read, even if you don't actually consume any bytes of content.
|
|
293
329
|
|
|
294
|
-
### `Response.headers`
|
|
330
|
+
### `Response.headers: Headers`
|
|
295
331
|
|
|
296
332
|
*The `headers` read-only property of the `Response` interface contains the `Headers` object
|
|
297
333
|
associated with the response.*
|
|
@@ -299,12 +335,27 @@ associated with the response.*
|
|
|
299
335
|
Note that Fáith does not provide a custom `Headers` class; instead the Web API `Headers` structure
|
|
300
336
|
is used directly and constructed by Fáith when needed.
|
|
301
337
|
|
|
302
|
-
### `Response.ok`
|
|
338
|
+
### `Response.ok: boolean`
|
|
303
339
|
|
|
304
340
|
*The `ok` read-only property of the `Response` interface contains a boolean stating whether the
|
|
305
341
|
response was successful (status in the range 200-299) or not.*
|
|
306
342
|
|
|
307
|
-
### `Response.
|
|
343
|
+
### `Response.peer: object`
|
|
344
|
+
|
|
345
|
+
Custom to Fáith.
|
|
346
|
+
|
|
347
|
+
The `peer` read-only property of the `Response` interface contains an object with information about
|
|
348
|
+
the remote peer that sent this response:
|
|
349
|
+
|
|
350
|
+
#### `Response.peer.address: string | null`
|
|
351
|
+
|
|
352
|
+
The IP address and port of the peer, if available.
|
|
353
|
+
|
|
354
|
+
#### `Response.peer.certificate: Buffer | null`
|
|
355
|
+
|
|
356
|
+
When connected over HTTPS, this is the DER-encoded leaf certificate of the peer.
|
|
357
|
+
|
|
358
|
+
### `Response.redirected: boolean`
|
|
308
359
|
|
|
309
360
|
*The `redirected` read-only property of the `Response` interface indicates whether or not the
|
|
310
361
|
response is the result of a request you made which was redirected.*
|
|
@@ -312,14 +363,12 @@ response is the result of a request you made which was redirected.*
|
|
|
312
363
|
*Note that by the time you read this property, the redirect will already have happened, and you
|
|
313
364
|
cannot prevent it by aborting the fetch at this point.*
|
|
314
365
|
|
|
315
|
-
### `Response.status`
|
|
366
|
+
### `Response.status: number`
|
|
316
367
|
|
|
317
368
|
*The `status` read-only property of the `Response` interface contains the HTTP status codes of the
|
|
318
369
|
response. For example, 200 for success, 404 if the resource could not be found.*
|
|
319
370
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
### `Response.statusText`
|
|
371
|
+
### `Response.statusText: string`
|
|
323
372
|
|
|
324
373
|
*The `statusText` read-only property of the `Response` interface contains the status message
|
|
325
374
|
corresponding to the HTTP status code in `Response.status`. For example, this would be `OK` for a
|
|
@@ -329,58 +378,57 @@ In HTTP/1, servers can send custom status text. This is returned here. In HTTP/2
|
|
|
329
378
|
status text is not supported at all, and the `statusText` property is either empty or simulated
|
|
330
379
|
from well-known status codes.
|
|
331
380
|
|
|
332
|
-
### `Response.type`
|
|
381
|
+
### `Response.type: string`
|
|
333
382
|
|
|
334
383
|
*The `type` read-only property of the `Response` interface contains the type of the response. The
|
|
335
384
|
type determines whether scripts are able to access the response body and headers.*
|
|
336
385
|
|
|
337
386
|
In Fáith, this is always set to `basic`.
|
|
338
387
|
|
|
339
|
-
### `Response.url`
|
|
388
|
+
### `Response.url: string`
|
|
340
389
|
|
|
341
390
|
*The `url` read-only property of the `Response` interface contains the URL of the response. The
|
|
342
391
|
value of the `url` property will be the final URL obtained after any redirects.*
|
|
343
392
|
|
|
344
|
-
### `Response.version`
|
|
393
|
+
### `Response.version: string`
|
|
345
394
|
|
|
346
395
|
The `version` read-only property of the `Response` interface contains the HTTP version of the
|
|
347
396
|
response. The value will be the final HTTP version after any redirects and protocol upgrades.
|
|
348
397
|
|
|
349
398
|
This is custom to Fáith.
|
|
350
399
|
|
|
351
|
-
### `Response.arrayBuffer()
|
|
400
|
+
### `Response.arrayBuffer(): Promise<ArrayBuffer>`
|
|
352
401
|
|
|
353
402
|
*The `arrayBuffer()` method of the `Response` interface takes a `Response` stream and reads it to
|
|
354
403
|
completion. It returns a promise that resolves with an `ArrayBuffer`.*
|
|
355
404
|
|
|
356
|
-
### `Response.blob()
|
|
405
|
+
### `Response.blob(): Promise<Blob>`
|
|
357
406
|
|
|
358
407
|
*The `blob()` method of the `Response` interface takes a `Response` stream and reads it to
|
|
359
408
|
completion. It returns a promise that resolves with a `Blob`.*
|
|
360
409
|
|
|
361
410
|
*The `type` of the `Blob` is set to the value of the `Content-Type` response header.*
|
|
362
411
|
|
|
363
|
-
### `Response.bytes()
|
|
412
|
+
### `Response.bytes(): Promise<Buffer>`
|
|
364
413
|
|
|
365
414
|
*The `bytes()` method of the `Response` interface takes a `Response` stream and reads it to
|
|
366
415
|
completion. It returns a promise that resolves with a `Uint8Array`.*
|
|
367
416
|
|
|
368
417
|
In Fáith, this returns a Node.js `Buffer`, which can be used as (and is a subclass of) a `Uint8Array`.
|
|
369
418
|
|
|
370
|
-
### `Response.clone()`
|
|
419
|
+
### `Response.clone(): Response`
|
|
371
420
|
|
|
372
421
|
*The `clone()` method of the `Response` interface creates a clone of a response object, identical
|
|
373
422
|
in every way, but stored in a different variable.*
|
|
374
423
|
|
|
375
|
-
*`clone()` throws
|
|
376
|
-
|
|
377
|
-
(In-spec, this should throw a `TypeError`, but for technical reasons this is not possible with Fáith.)
|
|
424
|
+
*`clone()` throws an error if the response body has already been used.*
|
|
378
425
|
|
|
379
|
-
### `Response.formData()
|
|
426
|
+
### `Response.formData(): !`
|
|
380
427
|
|
|
381
|
-
Fáith deliberately does not implement this.
|
|
428
|
+
Fáith deliberately does not implement this. The method exists so the types work out, but it will
|
|
429
|
+
always throw.
|
|
382
430
|
|
|
383
|
-
### `Response.json()
|
|
431
|
+
### `Response.json(): Promise<unknown>`
|
|
384
432
|
|
|
385
433
|
*The `json()` method of the `Response` interface takes a `Response` stream and reads it to
|
|
386
434
|
completion. It returns a promise which resolves with the result of parsing the body text as
|
|
@@ -393,13 +441,13 @@ Further note that, at least in Fáith, this method first reads the entire respon
|
|
|
393
441
|
and then parses that as JSON. This can use up to double the amount of memory. If you need more
|
|
394
442
|
efficient access, consider handling the response body as a stream.
|
|
395
443
|
|
|
396
|
-
### `Response.text()
|
|
444
|
+
### `Response.text(): Promise<string>`
|
|
397
445
|
|
|
398
446
|
*The `text()` method of the `Response` interface takes a `Response` stream and reads it to
|
|
399
447
|
completion. It returns a promise that resolves with a `String`. The response is always decoded
|
|
400
448
|
using UTF-8.*
|
|
401
449
|
|
|
402
|
-
### `Response.webResponse()`
|
|
450
|
+
### `Response.webResponse(): globalThis.Response`
|
|
403
451
|
|
|
404
452
|
This is entirely custom to Fáith. It returns a Web API `Response` instead of Fáith's custom
|
|
405
453
|
`Response` class. However, it's not possible to construct a Web API `Response` that has all the
|
|
@@ -429,6 +477,8 @@ For this reason, and also because in browsers this behaviour is standard, **all*
|
|
|
429
477
|
Fáith use an `Agent`. For `fetch()` calls that don't specify one explicitly, a global agent with
|
|
430
478
|
default options is created on first use.
|
|
431
479
|
|
|
480
|
+
There are a lot more options that could be exposed here; if you want one, open an issue.
|
|
481
|
+
|
|
432
482
|
### Syntax
|
|
433
483
|
|
|
434
484
|
```javascript
|
|
@@ -436,8 +486,45 @@ new Agent()
|
|
|
436
486
|
new Agent(options)
|
|
437
487
|
```
|
|
438
488
|
|
|
439
|
-
### `cache`
|
|
440
|
-
|
|
489
|
+
### `AgentOptions.cache: object`
|
|
490
|
+
|
|
491
|
+
Settings related to the HTTP cache. This is a nested object.
|
|
492
|
+
|
|
493
|
+
#### `AgentOptions.cache.store: string`
|
|
494
|
+
|
|
495
|
+
Which cache store to use: either `disk` or `memory`.
|
|
496
|
+
|
|
497
|
+
Default: none (cache disabled).
|
|
498
|
+
|
|
499
|
+
#### `AgentOptions.cache.capacity: number`
|
|
500
|
+
|
|
501
|
+
If `cache.store: "memory"`, the maximum amount of items stored.
|
|
502
|
+
|
|
503
|
+
Default: 10_000.
|
|
504
|
+
|
|
505
|
+
#### `AgentOptions.cache.mode: string`
|
|
506
|
+
|
|
507
|
+
Default cache mode. This is the same as [`FetchOptions.cache`](#fetchoptionscache), and is used if
|
|
508
|
+
no cache mode is set on a request.
|
|
509
|
+
|
|
510
|
+
Default: `"default"`.
|
|
511
|
+
|
|
512
|
+
#### `AgentOptions.cache.path: string`
|
|
513
|
+
|
|
514
|
+
If `cache.store: "disk"`, then this is the path at which the cache data is. Must be writeable.
|
|
515
|
+
|
|
516
|
+
Required if `cache.store: "disk"`.
|
|
517
|
+
|
|
518
|
+
#### `AgentOptions.cache.shared: boolean`
|
|
519
|
+
|
|
520
|
+
If `true`, then the response is evaluated from a perspective of a shared cache (i.e. `private` is
|
|
521
|
+
not cacheable and `s-maxage` is respected). If `false`, then the response is evaluated from a
|
|
522
|
+
perspective of a single-user cache (i.e. `private` is cacheable and `s-maxage` is ignored).
|
|
523
|
+
`shared: true` is required for proxies and multi-user caches.
|
|
524
|
+
|
|
525
|
+
Default: true.
|
|
526
|
+
|
|
527
|
+
### `AgentOptions.cookies: bool`
|
|
441
528
|
|
|
442
529
|
Enable a persistent cookie store for the agent. Cookies received in responses will be preserved and
|
|
443
530
|
included in additional requests.
|
|
@@ -447,8 +534,36 @@ Default: `false`.
|
|
|
447
534
|
You may use `agent.getCookie(url: string)` and `agent.addCookie(url: string, value: string)` to add
|
|
448
535
|
and retrieve cookies from the store.
|
|
449
536
|
|
|
450
|
-
### `dns`
|
|
451
|
-
|
|
537
|
+
### `AgentOptions.dns: object`
|
|
538
|
+
|
|
539
|
+
Settings related to DNS. This is a nested object.
|
|
540
|
+
|
|
541
|
+
#### `AgentOptions.dns.system: boolean`
|
|
542
|
+
|
|
543
|
+
Use the system's DNS (via `getaddrinfo` or equivalent) rather than Fáith's own DNS client (based on
|
|
544
|
+
[Hickory]). If you experience issues with DNS where Fáith does not work but e.g. curl or native
|
|
545
|
+
fetch does, this should be your first port of call.
|
|
546
|
+
|
|
547
|
+
Enabling this also disables Happy Eyeballs (for IPv6 / IPv4 best-effort resolution), the in-memory
|
|
548
|
+
DNS cache, and may lead to worse performance even discounting the cache.
|
|
549
|
+
|
|
550
|
+
Default: false.
|
|
551
|
+
|
|
552
|
+
[Hickory]: https://hickory-dns.org/
|
|
553
|
+
|
|
554
|
+
#### `AgentOptions.dns.overrides: Array<{ domain: string; addresses: string[] }>`
|
|
555
|
+
|
|
556
|
+
Override DNS resolution for specific domains. This takes effect even with `dns.system: true`.
|
|
557
|
+
|
|
558
|
+
Will throw if addresses are in invalid formats. You may provide a port number as part of the
|
|
559
|
+
address, it will default to port 0 otherwise, which will select the conventional port for the
|
|
560
|
+
protocol in use (e.g. 80 for plaintext HTTP). If the URL passed to `fetch()` has an explicit port
|
|
561
|
+
number, that one will be used instead. Resolving a domain to an empty `addresses` array effectively
|
|
562
|
+
blocks that domain from this agent.
|
|
563
|
+
|
|
564
|
+
Default: no overrides.
|
|
565
|
+
|
|
566
|
+
### `AgentOptions.headers: Array<{ name: string, value: string, sensitive?: bool }>`
|
|
452
567
|
|
|
453
568
|
Sets the default headers for every request.
|
|
454
569
|
|
|
@@ -457,23 +572,123 @@ Sensitive headers (e.g. `Authorization`) should be marked.
|
|
|
457
572
|
|
|
458
573
|
Default: none.
|
|
459
574
|
|
|
460
|
-
### `http3`
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
#### `
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
575
|
+
### `AgentOptions.http3: object`
|
|
576
|
+
|
|
577
|
+
Settings related to HTTP/3. This is a nested object.
|
|
578
|
+
|
|
579
|
+
#### `AgentOptions.http3.congestion: string`
|
|
580
|
+
|
|
581
|
+
The congestion control algorithm. The default is `cubic`, which is the same used in TCP in the
|
|
582
|
+
Linux stack. It's fair for all traffic, but not the most optimal, especially for networks with
|
|
583
|
+
a lot of available bandwidth, high latency, or a lot of packet loss. Cubic reacts to packet loss by
|
|
584
|
+
dropping the speed by 30%, and takes a long time to recover. BBR instead tries to maximise
|
|
585
|
+
bandwidth use and optimises for round-trip time, while ignoring packet loss.
|
|
586
|
+
|
|
587
|
+
In some networks, BBR can lead to pathological degradation of overall network conditions, by
|
|
588
|
+
flooding the network by up to **100 times** more retransmissions. This is fixed in BBRv2 and BBRv3,
|
|
589
|
+
but Fáith (or rather its underlying QUIC library quinn, [does not implement those yet][2]).
|
|
590
|
+
|
|
591
|
+
[2]: https://github.com/quinn-rs/quinn/issues/1254
|
|
592
|
+
|
|
593
|
+
Default: `cubic`. Accepted values: `cubic`, `bbr1`.
|
|
594
|
+
|
|
595
|
+
#### `AgentOptions.http3.maxIdleTimeout: number`
|
|
596
|
+
|
|
597
|
+
Maximum duration of inactivity to accept before timing out the connection, in seconds. Note that
|
|
598
|
+
this only sets the timeout on this side of the connection: the true idle timeout is the _minimum_
|
|
599
|
+
of this and the peer’s own max idle timeout. While the underlying library has no limits, Fáith
|
|
600
|
+
defines bounds for safety: minimum 1 second, maximum 2 minutes (120 seconds).
|
|
601
|
+
|
|
602
|
+
Default: 30.
|
|
603
|
+
|
|
604
|
+
### `AgentOptions.pool: object`
|
|
605
|
+
|
|
606
|
+
Settings related to the connection pool. This is a nested object.
|
|
607
|
+
|
|
608
|
+
#### `AgentOptions.pool.idleTimeout: number`
|
|
609
|
+
|
|
610
|
+
How many seconds of inactivity before a connection is closed.
|
|
611
|
+
|
|
612
|
+
Default: 90 seconds.
|
|
613
|
+
|
|
614
|
+
#### `AgentOptions.pool.maxIdlePerHost: number | null`
|
|
615
|
+
|
|
616
|
+
The maximum amount of idle connections per host to allow in the pool. Connections will be closed
|
|
617
|
+
to keep the idle connections (per host) under that number.
|
|
618
|
+
|
|
619
|
+
Default: `null` (no limit).
|
|
620
|
+
|
|
621
|
+
### `AgentOptions.redirect: string`
|
|
622
|
+
|
|
623
|
+
*Determines the behavior in case the server replies with a redirect status.
|
|
624
|
+
One of the following values:*
|
|
625
|
+
|
|
626
|
+
- *`follow`: automatically follow redirects.* Fáith limits this to 10 redirects.
|
|
627
|
+
- *`error`: reject the promise with a network error when a redirect status is returned.*
|
|
628
|
+
- ~~*`manual`*:~~ not supported.
|
|
629
|
+
- `stop`: (Fáith custom) don't follow any redirects, return the responses.
|
|
630
|
+
|
|
631
|
+
*Defaults to `follow`.*
|
|
632
|
+
|
|
633
|
+
### `AgentOptions.timeout: object`
|
|
634
|
+
|
|
635
|
+
Timeouts for requests made with this agent. This is a nested object.
|
|
636
|
+
|
|
637
|
+
#### `AgentOptions.timeout.connect: number | null`
|
|
638
|
+
|
|
639
|
+
Set a timeout for only the connect phase, in milliseconds.
|
|
640
|
+
|
|
641
|
+
Default: none.
|
|
642
|
+
|
|
643
|
+
#### `AgentOptions.timeout.read: number | null`
|
|
644
|
+
|
|
645
|
+
Set a timeout for read operations, in milliseconds.
|
|
646
|
+
|
|
647
|
+
The timeout applies to each read operation, and resets after a successful read. This is more
|
|
648
|
+
appropriate for detecting stalled connections when the size isn’t known beforehand.
|
|
649
|
+
|
|
650
|
+
Default: none.
|
|
651
|
+
|
|
652
|
+
#### `AgentOptions.timeout.total: number | null`
|
|
653
|
+
|
|
654
|
+
Set a timeout for the entire request-response cycle, in milliseconds.
|
|
655
|
+
|
|
656
|
+
The timeout applies from when the request starts connecting until the response body has finished.
|
|
657
|
+
Also considered a total deadline.
|
|
658
|
+
|
|
659
|
+
Default: none.
|
|
660
|
+
|
|
661
|
+
### `AgentOptions.tls: object`
|
|
662
|
+
|
|
663
|
+
Settings related to the connection pool. This is a nested object.
|
|
664
|
+
|
|
665
|
+
#### `AgentOptions.tls.earlyData: boolean`
|
|
666
|
+
|
|
667
|
+
Enable TLS 1.3 Early Data. Early data is an optimisation where the client sends the first packet
|
|
668
|
+
of application data alongside the opening packet of the TLS handshake. That can enable the server
|
|
669
|
+
to answer faster, improving latency by up to one round-trip. However, Early Data has significant
|
|
670
|
+
security implications: it's vulnerable to replay attacks and has weaker forward secrecy. It should
|
|
671
|
+
really only be used for static assets or to squeeze out the last drop of performance for endpoints
|
|
672
|
+
that are replay-safe.
|
|
673
|
+
|
|
674
|
+
Default: false.
|
|
675
|
+
|
|
676
|
+
#### `AgentOptions.tls.identity: string | Buffer`
|
|
677
|
+
|
|
678
|
+
Provide a PEM-formatted certificate and private key to present as a TLS client certificate (also
|
|
679
|
+
called mutual TLS or mTLS) authentication.
|
|
680
|
+
|
|
681
|
+
The input should contain a PEM encoded private key and at least one PEM encoded certificate. The
|
|
682
|
+
private key must be in RSA, SEC1 Elliptic Curve or PKCS#8 format. This is one of the few options
|
|
683
|
+
that will cause the `Agent` constructor to throw if the input is in the wrong format.
|
|
684
|
+
|
|
685
|
+
#### `AgentOptions.tls.required`
|
|
686
|
+
|
|
687
|
+
Disables plain-text HTTP.
|
|
688
|
+
|
|
689
|
+
Default: false.
|
|
690
|
+
|
|
691
|
+
### `AgentOptions.userAgent`
|
|
477
692
|
|
|
478
693
|
Custom user agent string.
|
|
479
694
|
|
|
@@ -488,7 +703,7 @@ const agent = new Agent({
|
|
|
488
703
|
});
|
|
489
704
|
```
|
|
490
705
|
|
|
491
|
-
### `addCookie(url: string, cookie: string)`
|
|
706
|
+
### `Agent.addCookie(url: string, cookie: string)`
|
|
492
707
|
|
|
493
708
|
Add a cookie into the agent.
|
|
494
709
|
|
|
@@ -496,7 +711,7 @@ Does nothing if:
|
|
|
496
711
|
- the cookie store is disabled
|
|
497
712
|
- the url is malformed
|
|
498
713
|
|
|
499
|
-
### `getCookie(url: string): string | null`
|
|
714
|
+
### `Agent.getCookie(url: string): string | null`
|
|
500
715
|
|
|
501
716
|
Retrieve a cookie from the store.
|
|
502
717
|
|
|
@@ -506,7 +721,7 @@ Returns `null` if:
|
|
|
506
721
|
- the url is malformed
|
|
507
722
|
- the cookie cannot be represented as a string
|
|
508
723
|
|
|
509
|
-
### `stats(): object`
|
|
724
|
+
### `Agent.stats(): object`
|
|
510
725
|
|
|
511
726
|
Returns statistics gathered by this agent:
|
|
512
727
|
|
|
@@ -524,8 +739,10 @@ error kind, documented in this comprehensive mapping:
|
|
|
524
739
|
- `Timeout` — request timed out
|
|
525
740
|
- JS `NetworkError`:
|
|
526
741
|
- `Network` — network error
|
|
742
|
+
- `Redirect` — when the agent is configured to error on redirects
|
|
527
743
|
- JS `SyntaxError`:
|
|
528
744
|
- `JsonParse` — JSON parse error for `response.json()`
|
|
745
|
+
- `PemParse` — PEM parse error for `AgentOptions.tls.identity`
|
|
529
746
|
- `Utf8Parse` — UTF8 decoding error for `response.text()`
|
|
530
747
|
- JS `TypeError`:
|
|
531
748
|
- `InvalidHeader` — invalid header name or value
|
|
@@ -535,6 +752,7 @@ error kind, documented in this comprehensive mapping:
|
|
|
535
752
|
- `ResponseBodyNotAvailable` — body is null or not available
|
|
536
753
|
- JS generic `Error`:
|
|
537
754
|
- `BodyStream` — internal stream handling error
|
|
755
|
+
- `Config` — invalid agent configuration
|
|
538
756
|
- `RuntimeThread` — failed to start or schedule threads on the internal tokio runtime
|
|
539
757
|
|
|
540
758
|
The library exports an `ERROR_CODES` object which has every error code the library throws, and
|
|
@@ -544,4 +762,4 @@ constant from `ERROR_CODES`, instead of doing string matching on the error messa
|
|
|
544
762
|
`instance of` matching.
|
|
545
763
|
|
|
546
764
|
Due to technical limitations, when reading a body stream, reads might fail, but that error
|
|
547
|
-
will not have a
|
|
765
|
+
will not have a `code` property.
|