@passcod/faith 0.0.2 → 0.0.5

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/wrapper.d.ts CHANGED
@@ -1,119 +1,380 @@
1
- /**
2
- * Faith Fetch API Wrapper TypeScript Definitions
3
- *
4
- * This provides TypeScript definitions for the spec-compliant Fetch API wrapper.
5
- */
6
-
1
+ import { Agent } from "./index";
7
2
  export {
8
- Agent,
9
- AgentOptions,
10
- AgentStats,
11
- Header,
12
- FAITH_VERSION,
13
- REQWEST_VERSION,
14
- USER_AGENT,
3
+ Agent,
4
+ AgentCacheOptions,
5
+ AgentDnsOptions,
6
+ AgentHttp3Options,
7
+ AgentPoolOptions,
8
+ AgentTimeoutOptions,
9
+ AgentTlsOptions,
10
+ AgentOptions,
11
+ AgentStats,
12
+ CacheMode,
13
+ CacheStore,
14
+ CredentialsOption as Credentials,
15
+ DnsOverride,
16
+ DuplexOption as Duplex,
17
+ Header,
18
+ Http3Congestion,
19
+ Redirect,
20
+ FAITH_VERSION,
21
+ REQWEST_VERSION,
22
+ USER_AGENT,
15
23
  } from "./index";
16
24
 
17
- /**
18
- * Error codes const enum
19
- *
20
- * NOTE: This must be kept in sync with FaithErrorKind in src/error.rs
21
- * Run `npm test` to validate sync (test/error-codes.test.js checks this)
22
- */
25
+ // NOTE: This must be kept in sync with FaithErrorKind in src/error.rs
26
+ // Run `npm test` to validate sync (test/error-codes.test.js checks this)
23
27
  export const ERROR_CODES: {
24
- readonly Aborted: "Aborted";
25
- readonly BodyStream: "BodyStream";
26
- readonly InvalidHeader: "InvalidHeader";
27
- readonly InvalidMethod: "InvalidMethod";
28
- readonly InvalidUrl: "InvalidUrl";
29
- readonly JsonParse: "JsonParse";
30
- readonly Network: "Network";
31
- readonly ResponseAlreadyDisturbed: "ResponseAlreadyDisturbed";
32
- readonly ResponseBodyNotAvailable: "ResponseBodyNotAvailable";
33
- readonly RuntimeThread: "RuntimeThread";
34
- readonly Timeout: "Timeout";
35
- readonly Utf8Parse: "Utf8Parse";
28
+ readonly Aborted: "Aborted";
29
+ readonly AddressParse: "AddressParse";
30
+ readonly BodyStream: "BodyStream";
31
+ readonly Config: "Config";
32
+ readonly InvalidHeader: "InvalidHeader";
33
+ readonly InvalidMethod: "InvalidMethod";
34
+ readonly InvalidUrl: "InvalidUrl";
35
+ readonly JsonParse: "JsonParse";
36
+ readonly Network: "Network";
37
+ readonly PemParse: "PemParse";
38
+ readonly Redirect: "Redirect";
39
+ readonly ResponseAlreadyDisturbed: "ResponseAlreadyDisturbed";
40
+ readonly ResponseBodyNotAvailable: "ResponseBodyNotAvailable";
41
+ readonly RuntimeThread: "RuntimeThread";
42
+ readonly Timeout: "Timeout";
43
+ readonly Utf8Parse: "Utf8Parse";
36
44
  };
37
45
 
38
46
  export interface FetchOptions {
39
- method?: string;
40
- headers?: Record<string, string> | Headers;
41
- body?: string | Buffer | Uint8Array | Array<number> | ArrayBuffer;
42
- timeout?: number; // milliseconds
43
- credentials?: "omit" | "same-origin" | "include";
44
- duplex?: "half";
45
- signal?: AbortSignal;
46
- agent?: Agent;
47
+ /**
48
+ * This is custom to Fáith.
49
+ *
50
+ * You can create an `Agent`, and pass it here to have the request executed by the `Agent`. See the
51
+ * documentation for the `Agent` options you can set with this, and the agent data you can access.
52
+ * Notably an agent has a DNS cache, and may be configured to handle cookies and/or an HTTP cache.
53
+ *
54
+ * When not provided, a global default `Agent` is created on first use.
55
+ */
56
+ agent?: Agent;
57
+ /**
58
+ * The request body contains content to send to the server, for example in a `POST` or `PUT` request.
59
+ * It is specified as an instance of any of the following types:
60
+ *
61
+ * - a string
62
+ * - `ArrayBuffer`
63
+ * - `Blob`
64
+ * - `DataView`
65
+ * - `File`
66
+ * - `FormData`
67
+ * - `TypedArray`
68
+ * - ~~`URLSearchParams`~~ Not yet implemented.
69
+ * - `ReadableStream` Note that Fáith currently reads this into memory before sending the request.
70
+ *
71
+ * If `body` is a `ReadableStream`, the `duplex` option must also be set.
72
+ */
73
+ body?: string | Buffer | Uint8Array | Array<number> | ArrayBuffer;
74
+ /**
75
+ * The cache mode you want to use for the request. This may be any one of the following values:
76
+ *
77
+ * - `default`: The client looks in its HTTP cache for a response matching the request.
78
+ * - If there is a match and it is fresh, it will be returned from the cache.
79
+ * - If there is a match but it is stale, the client will make a conditional request to the remote
80
+ * server. If the server indicates that the resource has not changed, it will be returned from the
81
+ * cache. Otherwise the resource will be downloaded from the server and the cache will be updated.
82
+ * - If there is no match, the client will make a normal request, and will update the cache with
83
+ * the downloaded resource.
84
+ *
85
+ * - `no-store`: The client fetches the resource from the remote server without first looking in the
86
+ * cache, and will not update the cache with the downloaded resource.
87
+ *
88
+ * - `reload`: The client fetches the resource from the remote server without first looking in the
89
+ * cache, but then will update the cache with the downloaded resource.
90
+ *
91
+ * - `no-cache`: The client looks in its HTTP cache for a response matching the request.
92
+ * - If there is a match, fresh or stale, the client will make a conditional request to the remote
93
+ * server. If the server indicates that the resource has not changed, it will be returned from the
94
+ * cache. Otherwise the resource will be downloaded from the server and the cache will be updated.
95
+ * - If there is no match, the client will make a normal request, and will update the cache with
96
+ * the downloaded resource.
97
+ *
98
+ * - `force-cache`: The client looks in its HTTP cache for a response matching the request.
99
+ * - If there is a match, fresh or stale, it will be returned from the cache.
100
+ * - If there is no match, the client will make a normal request, and will update the cache with
101
+ * the downloaded resource.
102
+ *
103
+ * - `only-if-cached`: The client looks in its HTTP cache for a response matching the request.
104
+ * - If there is a match, fresh or stale, it will be returned from the cache.
105
+ * - If there is no match, a network error is returned.
106
+ *
107
+ * - `ignore-rules`: Custom to Fáith. Overrides the check that determines if a response can be cached
108
+ * to always return true on 200. Uses any response in the HTTP cache matching the request, not
109
+ * paying attention to staleness. If there was no response, it creates a normal request and updates
110
+ * the HTTP cache with the response.
111
+ */
112
+ cache?:
113
+ | "default"
114
+ | "force-cache"
115
+ | "ignore-rules"
116
+ | "no-cache"
117
+ | "no-store"
118
+ | "only-if-cached"
119
+ | "reload";
120
+ /**
121
+ * Controls whether or not the client sends credentials with the request, as well as whether any
122
+ * `Set-Cookie` response headers are respected. Credentials are cookies, ~~TLS client certificates,~~
123
+ * or authentication headers containing a username and password. This option may be any one of the
124
+ * following values:
125
+ *
126
+ * - `omit`: Never send credentials in the request or include credentials in the response.
127
+ * - ~~`same-origin`~~: Fáith does not implement this, as there is no concept of "origin" on the server.
128
+ * - `include`: Always include credentials, ~~even for cross-origin requests.~~
129
+ *
130
+ * Fáith ignores the `Access-Control-Allow-Credentials` and `Access-Control-Allow-Origin` headers.
131
+ *
132
+ * Fáith currently does not `omit` the TLS client certificate when the request's `Agent` has one
133
+ * configured. This is an upstream limitation.
134
+ *
135
+ * If the request's `Agent` has cookies enabled, new cookies from the response will be added to the
136
+ * cookie jar, even as Fáith strips them from the request and response headers returned to the user.
137
+ * This is an upstream limitation.
138
+ *
139
+ * Defaults to `include` (browsers default to `same-origin`).
140
+ */
141
+ credentials?: "omit" | "same-origin" | "include";
142
+ /**
143
+ * Controls duplex behavior of the request. If this is present it must have the value `half`, meaning
144
+ * that Fáith will send the entire request before processing the response.
145
+ *
146
+ * This option must be present when `body` is a `ReadableStream`.
147
+ */
148
+ duplex?: "half";
149
+ /**
150
+ * Any headers you want to add to your request, contained within a `Headers` object or an object
151
+ * literal whose keys are the names of headers and whose values are the header values.
152
+ *
153
+ * Fáith allows all request headers to be set (unlike browsers, which [forbid][1] a number of them).
154
+ *
155
+ * [1]: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_request_header
156
+ */
157
+ headers?: Record<string, string> | Headers;
158
+ /**
159
+ * The request method. Defaults to `GET`.
160
+ */
161
+ method?: string;
162
+ /**
163
+ * An `AbortSignal`. If this option is set, the request can be canceled by calling `abort()` on the
164
+ * corresponding `AbortController`.
165
+ */
166
+ signal?: AbortSignal;
167
+ /**
168
+ * Custom to Fáith. Cancels the request after this many milliseconds.
169
+ *
170
+ * This will give a different error to using `signal` with a timeout, which might be preferable in
171
+ * some cases. It also has a slightly different internal behaviour: `signal` may abort the request
172
+ * only until the response headers have been received, while `timeout` will apply through the entire
173
+ * response receipt.
174
+ */
175
+ timeout?: number;
176
+ }
177
+
178
+ export interface PeerInformation {
179
+ /**
180
+ * The IP address and port of the peer, if available.
181
+ */
182
+ address?: string;
183
+ /**
184
+ * When connected over HTTPS, this is the DER-encoded leaf certificate of the peer.
185
+ */
186
+ certificate?: Buffer;
47
187
  }
48
188
 
49
189
  export class Response {
50
- readonly bodyUsed: boolean;
51
- readonly headers: Headers;
52
- readonly ok: boolean;
53
- readonly redirected: boolean;
54
- readonly status: number;
55
- readonly statusText: string;
56
- readonly url: string;
57
- readonly version: string;
190
+ /**
191
+ * The `bodyUsed` read-only property of the `Response` interface is a boolean value that indicates
192
+ * whether the body has been read yet.
193
+ *
194
+ * In Fáith, this indicates whether the body stream has ever been read from or canceled, as defined
195
+ * [in the spec](https://streams.spec.whatwg.org/#is-readable-stream-disturbed). Note that accessing
196
+ * the `.body` property counts as a read, even if you don't actually consume any bytes of content.
197
+ */
198
+ readonly bodyUsed: boolean;
199
+ /**
200
+ * The `headers` read-only property of the `Response` interface contains the `Headers` object
201
+ * associated with the response.
202
+ *
203
+ * Note that Fáith does not provide a custom `Headers` class; instead the Web API `Headers` structure
204
+ * is used directly and constructed by Fáith when needed.
205
+ */
206
+ readonly headers: Headers;
207
+ /**
208
+ * The `ok` read-only property of the `Response` interface contains a boolean stating whether the
209
+ * response was successful (status in the range 200-299) or not.
210
+ */
211
+ readonly ok: boolean;
212
+ /**
213
+ * Custom to Fáith.
214
+ *
215
+ * The `peer` read-only property of the `Response` interface contains an object with information about
216
+ * the remote peer that sent this response:
217
+ */
218
+ readonly peer: PeerInformation;
219
+ /**
220
+ * The `redirected` read-only property of the `Response` interface indicates whether or not the
221
+ * response is the result of a request you made which was redirected.
222
+ *
223
+ * Note that by the time you read this property, the redirect will already have happened, and you
224
+ * cannot prevent it by aborting the fetch at this point.
225
+ */
226
+ readonly redirected: boolean;
227
+ /**
228
+ * The `status` read-only property of the `Response` interface contains the HTTP status codes of the
229
+ * response. For example, 200 for success, 404 if the resource could not be found.
230
+ */
231
+ readonly status: number;
232
+ /**
233
+ * The `statusText` read-only property of the `Response` interface contains the status message
234
+ * corresponding to the HTTP status code in `Response.status`. For example, this would be `OK` for a
235
+ * status code `200`, `Continue` for `100`, `Not Found` for `404`.
236
+ *
237
+ * In HTTP/1, servers can send custom status text. This is returned here. In HTTP/2 and HTTP/3, custom
238
+ * status text is not supported at all, and the `statusText` property is either empty or simulated
239
+ * from well-known status codes.
240
+ */
241
+ readonly statusText: string;
242
+ /**
243
+ * The `type` read-only property of the `Response` interface contains the type of the response. The
244
+ * type determines whether scripts are able to access the response body and headers.
245
+ *
246
+ * In Fáith, this is always set to `basic`.
247
+ */
248
+ readonly type: "basic";
249
+ /**
250
+ * The `url` read-only property of the `Response` interface contains the URL of the response. The
251
+ * value of the `url` property will be the final URL obtained after any redirects.
252
+ */
253
+ readonly url: string;
254
+ /**
255
+ * The `version` read-only property of the `Response` interface contains the HTTP version of the
256
+ * response. The value will be the final HTTP version after any redirects and protocol upgrades.
257
+ *
258
+ * This is custom to Fáith.
259
+ */
260
+ readonly version: string;
261
+
262
+ /**
263
+ * The `body` read-only property of the `Response` interface is a `ReadableStream` of the body
264
+ * contents, or `null` for any actual HTTP response that has no body, such as `HEAD` requests and
265
+ * `204 No Content` responses.
266
+ *
267
+ * Note that browsers currently do not return `null` for those responses, but the spec requires
268
+ * it. Fáith chooses to respect the spec rather than the browsers in this case.
269
+ *
270
+ * An important consideration exists in conjunction with the connection pool: if you start the
271
+ * body stream, this will hold the connection until the stream is fully consumed. If another
272
+ * request is started during that time, and you don't have an available connection in the pool
273
+ * for the host already, the new request will open one.
274
+ */
275
+ readonly body: ReadableStream<Uint8Array> | null;
58
276
 
59
- /**
60
- * Get the response body as a ReadableStream
61
- * This is a getter to match the Fetch API spec
62
- */
63
- readonly body: ReadableStream<Uint8Array> | null;
277
+ /**
278
+ * The `text()` method of the `Response` interface takes a `Response` stream and reads it to
279
+ * completion. It returns a promise that resolves with a `String`. The response is always decoded
280
+ * using UTF-8.
281
+ */
282
+ text(): Promise<string>;
64
283
 
65
- /**
66
- * Convert response body to text (UTF-8)
67
- * @returns Promise that resolves with the response body as text
68
- */
69
- text(): Promise<string>;
284
+ /**
285
+ * The `bytes()` method of the `Response` interface takes a `Response` stream and reads it to
286
+ * completion. It returns a promise that resolves with a `Uint8Array`.
287
+ *
288
+ * In Fáith, this returns a Node.js `Buffer`, which can be used as (and is a subclass of) a `Uint8Array`.
289
+ */
290
+ bytes(): Promise<Uint8Array>;
70
291
 
71
- /**
72
- * Get response body as bytes
73
- * @returns Promise that resolves with the response body as Uint8Array
74
- */
75
- bytes(): Promise<Uint8Array>;
292
+ /**
293
+ * The `arrayBuffer()` method of the `Response` interface takes a `Response` stream and reads it to
294
+ * completion. It returns a promise that resolves with an `ArrayBuffer`.
295
+ */
296
+ arrayBuffer(): Promise<ArrayBuffer>;
76
297
 
77
- /**
78
- * Get response body as ArrayBuffer
79
- * @returns Promise that resolves with the response body as ArrayBuffer
80
- */
81
- arrayBuffer(): Promise<ArrayBuffer>;
298
+ /**
299
+ * The `json()` method of the `Response` interface takes a `Response` stream and reads it to
300
+ * completion. It returns a promise which resolves with the result of parsing the body text as
301
+ * `JSON`.
302
+ *
303
+ * Note that despite the method being named `json()`, the result is not JSON but is instead the
304
+ * result of taking JSON as input and parsing it to produce a JavaScript object.
305
+ *
306
+ * Further note that, at least in Fáith, this method first reads the entire response body as bytes,
307
+ * and then parses that as JSON. This can use up to double the amount of memory. If you need more
308
+ * efficient access, consider handling the response body as a stream.
309
+ */
310
+ json(): Promise<any>;
82
311
 
83
- /**
84
- * Parse response body as JSON
85
- * @returns Promise that resolves with the parsed JSON data
86
- */
87
- json(): Promise<any>;
312
+ /**
313
+ * The `blob()` method of the `Response` interface takes a `Response` stream and reads it to
314
+ * completion. It returns a promise that resolves with a `Blob`.
315
+ *
316
+ * The `type` of the `Blob` is set to the value of the `Content-Type` response header.
317
+ */
318
+ blob(): Promise<Blob>;
88
319
 
89
- /**
90
- * Get response body as Blob
91
- * @returns Promise that resolves with the response body as Blob
92
- */
93
- blob(): Promise<Blob>;
320
+ /**
321
+ * Fáith deliberately does not implement this. It will always throw.
322
+ */
323
+ formData(): Promise<FormData>;
94
324
 
95
- /**
96
- * Create a clone of the Response object
97
- * @returns A new Response object with the same properties
98
- * @throws If response body has already been read
99
- */
100
- clone(): Response;
325
+ /**
326
+ * The `trailers()` method of the `Response` interface returns a promise that resolves to either
327
+ * `null` or a `Headers` structure that contains the HTTP/2 or /3 trailing headers.
328
+ *
329
+ * This was once in the spec as a getter but was removed as it wasn't implemented by any browser.
330
+ *
331
+ * Note that this will never resolve if you don't also consume the body in some way.
332
+ */
333
+ trailers(): Promise<Headers | null>;
101
334
 
102
- /**
103
- * Convert to a Web API Response object
104
- * @returns Web API Response object
105
- * @throws If response body has been disturbed
106
- */
107
- webResponse(): globalThis.Response;
335
+ /**
336
+ * The `clone()` method of the `Response` interface creates a clone of a response object, identical
337
+ * in every way, but stored in a different variable.
338
+ *
339
+ * `clone()` throws an `Error` if the response body has already been used.
340
+ */
341
+ clone(): Response;
342
+
343
+ /**
344
+ * This is entirely custom to Fáith. It returns a Web API `Response` instead of Fáith's custom
345
+ * `Response` class. However, it's not possible to construct a Web API `Response` that has all the
346
+ * properties of a Fáith Response (or of another Web Response, for that matter). So this method only
347
+ * returns a Response from:
348
+ *
349
+ * - the `body` stream
350
+ * - the `status`, `statusCode`, and `headers` properties
351
+ *
352
+ * Note that if `json()`, `bytes()`, etc has been called on the original response, the body stream
353
+ * of the new Web `Response` will be empty or inaccessible. If the body stream of the original
354
+ * response has been partially read, only the remaining bytes will be available in the new `Response`.
355
+ */
356
+ webResponse(): globalThis.Response;
108
357
  }
109
358
 
110
359
  /**
111
- * Fetch function
112
- * @param input - The URL to fetch, a Request object, URL object, or any object with a toString() method
113
- * @param options - Fetch options (overrides Request properties when provided)
114
- * @returns Promise that resolves with a Response
360
+ * Start fetching a resource from the network, returning a promise that is fulfilled once the
361
+ * response is available.
362
+ *
363
+ * The promise resolves to the Response object representing the response to your request.
364
+ *
365
+ * A `fetch()` promise only rejects when the request fails, for example because of a badly-formed
366
+ * request URL or a network error. A `fetch()` promise does not reject if the server responds with
367
+ * HTTP status codes that indicate errors (404, 504, etc).
115
368
  */
116
369
  export declare function fetch(
117
- input: string | Request | URL | { toString(): string },
118
- options?: FetchOptions,
370
+ /**
371
+ * This defines the resource that you wish to fetch. This can either be: (1) a string or any other
372
+ * object with a stringifier — including a `URL` object — that provides the URL of the resource
373
+ * you want to fetch. The URL must be absolute and include a scheme. Or (2) a `Request` object.
374
+ */
375
+ resource: string | Request | URL | { toString(): string },
376
+ /**
377
+ * A `RequestInit` object containing any custom settings that you want to apply to the request.
378
+ */
379
+ options?: FetchOptions,
119
380
  ): Promise<Response>;