rekwest 5.0.2 → 5.0.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 +241 -241
- package/dist/cookies.js +19 -15
- package/dist/formdata.js +3 -1
- package/dist/postflight.js +3 -1
- package/dist/preflight.js +3 -1
- package/dist/transfer.js +3 -1
- package/package.json +74 -74
- package/src/cookies.mjs +97 -92
- package/src/formdata.mjs +1 -1
- package/src/postflight.mjs +136 -134
- package/src/preflight.mjs +87 -87
- package/src/transfer.mjs +114 -114
package/README.md
CHANGED
|
@@ -1,241 +1,241 @@
|
|
|
1
|
-
The robust request library that humanity deserves 🌐
|
|
2
|
-
---
|
|
3
|
-
This package provides highly likely functional and **easy-to-use** abstraction atop of
|
|
4
|
-
native [http(s).request](https://nodejs.org/api/https.html#httpsrequesturl-options-callback)
|
|
5
|
-
and [http2.request](https://nodejs.org/api/http2.html#clienthttp2sessionrequestheaders-options).
|
|
6
|
-
|
|
7
|
-
## Abstract
|
|
8
|
-
|
|
9
|
-
* Fetch-alike
|
|
10
|
-
* Cool-beans 🫐 config options (with defaults)
|
|
11
|
-
* Automatic HTTP/2 support (ALPN negotiation)
|
|
12
|
-
* Automatic or opt-in body parse (with non-UTF-8 charset decoding)
|
|
13
|
-
* Automatic and simplistic `Cookies` treatment (with built-in **jar** & **ttl**)
|
|
14
|
-
* Automatic decompression (with opt-in body compression)
|
|
15
|
-
* Built-in streamable `FormData` interface
|
|
16
|
-
* Support redirects & retries with fine-grained tune-ups
|
|
17
|
-
* Support all legit request body types (include blobs & streams)
|
|
18
|
-
* Support both CJS and ESM module systems
|
|
19
|
-
* Fully promise-able and pipe-able
|
|
20
|
-
* Zero dependencies
|
|
21
|
-
|
|
22
|
-
## Prerequisites
|
|
23
|
-
|
|
24
|
-
* Node.js `>= 18.13.0`
|
|
25
|
-
|
|
26
|
-
## Installation
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
npm install rekwest --save
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
### Usage
|
|
33
|
-
|
|
34
|
-
```javascript
|
|
35
|
-
import rekwest, { constants } from 'rekwest';
|
|
36
|
-
|
|
37
|
-
const {
|
|
38
|
-
HTTP2_HEADER_AUTHORIZATION,
|
|
39
|
-
HTTP2_HEADER_CONTENT_ENCODING,
|
|
40
|
-
HTTP2_METHOD_POST,
|
|
41
|
-
HTTP_STATUS_OK,
|
|
42
|
-
} = constants;
|
|
43
|
-
|
|
44
|
-
const url = 'https://somewhe.re/somewhat/endpoint';
|
|
45
|
-
|
|
46
|
-
const res = await rekwest(url, {
|
|
47
|
-
body: { celestial: 'payload' },
|
|
48
|
-
headers: {
|
|
49
|
-
[HTTP2_HEADER_AUTHORIZATION]: 'Bearer [token]',
|
|
50
|
-
[HTTP2_HEADER_CONTENT_ENCODING]: 'br', // enables: body compression
|
|
51
|
-
/** [HTTP2_HEADER_CONTENT_TYPE]
|
|
52
|
-
* is undue for
|
|
53
|
-
* Array/Blob/File/FormData/Object/URLSearchParams body types
|
|
54
|
-
* and will be set automatically, with an option to override it here
|
|
55
|
-
*/
|
|
56
|
-
},
|
|
57
|
-
method: HTTP2_METHOD_POST,
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
console.assert(res.statusCode === HTTP_STATUS_OK);
|
|
61
|
-
console.info(res.headers);
|
|
62
|
-
console.log(res.body);
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
---
|
|
66
|
-
|
|
67
|
-
```javascript
|
|
68
|
-
import { Readable } from 'node:stream';
|
|
69
|
-
import rekwest, {
|
|
70
|
-
constants,
|
|
71
|
-
Blob,
|
|
72
|
-
File,
|
|
73
|
-
FormData,
|
|
74
|
-
} from 'rekwest';
|
|
75
|
-
|
|
76
|
-
const {
|
|
77
|
-
HTTP2_HEADER_AUTHORIZATION,
|
|
78
|
-
HTTP2_HEADER_CONTENT_ENCODING,
|
|
79
|
-
HTTP2_METHOD_POST,
|
|
80
|
-
HTTP_STATUS_OK,
|
|
81
|
-
} = constants;
|
|
82
|
-
|
|
83
|
-
const blob = new Blob(['bits']);
|
|
84
|
-
const file = new File(['bits'], 'file.dab');
|
|
85
|
-
const readable = Readable.from('bits');
|
|
86
|
-
|
|
87
|
-
const fd = new FormData({
|
|
88
|
-
aux: Date.now(), // either [[key, value]] or kv sequenceable
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
fd.append('celestial', 'payload');
|
|
92
|
-
fd.append('blob', blob, 'blob.dab');
|
|
93
|
-
fd.append('file', file);
|
|
94
|
-
fd.append('readable', readable, 'readable.dab');
|
|
95
|
-
|
|
96
|
-
const url = 'https://somewhe.re/somewhat/endpoint';
|
|
97
|
-
|
|
98
|
-
const res = await rekwest(url, {
|
|
99
|
-
body: fd,
|
|
100
|
-
headers: {
|
|
101
|
-
[HTTP2_HEADER_AUTHORIZATION]: 'Bearer [token]',
|
|
102
|
-
[HTTP2_HEADER_CONTENT_ENCODING]: 'br', // enables: body compression
|
|
103
|
-
},
|
|
104
|
-
method: HTTP2_METHOD_POST,
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
console.assert(res.statusCode === HTTP_STATUS_OK);
|
|
108
|
-
console.info(res.headers);
|
|
109
|
-
console.log(res.body);
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### API
|
|
113
|
-
|
|
114
|
-
#### `rekwest(url[, options])`
|
|
115
|
-
|
|
116
|
-
* `url` **{string | URL}** The URL to send the request to
|
|
117
|
-
* `options` **{Object}**
|
|
118
|
-
Extends [http(s).RequestOptions](https://nodejs.org/api/https.html#httpsrequesturl-options-callback) along with
|
|
119
|
-
extra [http2.ClientSessionOptions](https://nodejs.org/api/http2.html#http2connectauthority-options-listener)
|
|
120
|
-
& [http2.ClientSessionRequestOptions](https://nodejs.org/api/http2.html#clienthttp2sessionrequestheaders-options)
|
|
121
|
-
and [tls.ConnectionOptions](https://nodejs.org/api/tls.html#tlsconnectoptions-callback)
|
|
122
|
-
for HTTP/2 attunes
|
|
123
|
-
* `baseURL` **{string | URL}** The base URL to use in cases where `url` is a relative URL
|
|
124
|
-
* `body` **{string | Array | ArrayBuffer | ArrayBufferView | AsyncIterator | Blob | Buffer | DataView | File |
|
|
125
|
-
FormData | Iterator | Object | Readable | ReadableStream | SharedArrayBuffer | URLSearchParams}** The body to send
|
|
126
|
-
with the request
|
|
127
|
-
* `cookies` **{boolean | Array<[k, v]> | Array<string\> | Cookies | Object | URLSearchParams}** `Default: true` The
|
|
128
|
-
cookies to add to
|
|
129
|
-
the request
|
|
130
|
-
* `cookiesTTL` **{boolean}** `Default: false` Controls enablement of TTL for the cookies cache
|
|
131
|
-
* `credentials` **{include | omit | same-origin}** `Default: same-origin` Controls credentials in case of cross-origin
|
|
132
|
-
redirects
|
|
133
|
-
* `digest` **{boolean}** `Default: true` Controls whether to read the response stream or simply add a mixin
|
|
134
|
-
* `follow` **{number}** `Default: 20` The number of redirects to follow
|
|
135
|
-
* `h2` **{boolean}** `Default: false` Forces the use of HTTP/2 protocol
|
|
136
|
-
* `headers` **{Object}** The headers to add to the request
|
|
137
|
-
* `maxRetryAfter` **{number}** The upper limit of `retry-after` header. If unset, it will use `timeout` value
|
|
138
|
-
* `parse` **{boolean}** `Default: true` Controls whether to parse response body or simply return a buffer
|
|
139
|
-
* `redirect` **{error | follow | manual}** `Default: follow` Controls the redirect flows
|
|
140
|
-
* `retry` **{Object}** Represents the retry options
|
|
141
|
-
* `attempts` **{number}** `Default: 0` The number of retry attempts
|
|
142
|
-
* `backoffStrategy` **{string}** `Default: interval * Math.log(Math.random() * (Math.E * Math.E - Math.E) + Math.E)`
|
|
143
|
-
The backoff strategy algorithm that increases logarithmically. To fixate set value to `interval * 1`
|
|
144
|
-
* `interval` **{number}** `Default: 1e3` The initial retry interval
|
|
145
|
-
* `retryAfter` **{boolean}** `Default: true` Controls `retry-after` header receptiveness
|
|
146
|
-
* `statusCodes` **{number[]}** `Default: [429, 503]` The list of status codes to retry on
|
|
147
|
-
* `stripTrailingSlash` **{boolean}** `Default: false` Controls whether to strip trailing slash at the end of the URL
|
|
148
|
-
* `thenable` **{boolean}** `Default: false` Controls the promise resolutions
|
|
149
|
-
* `timeout` **{number}** `Default: 3e5` The number of milliseconds a request can take before termination
|
|
150
|
-
* `trimTrailingSlashes` **{boolean}** `Default: false` Controls whether to trim trailing slashes within the URL
|
|
151
|
-
* **Returns:** Promise that resolves to
|
|
152
|
-
extended [http.IncomingMessage](https://nodejs.org/api/http.html#class-httpincomingmessage)
|
|
153
|
-
or [http2.ClientHttp2Stream](https://nodejs.org/api/http2.html#class-clienthttp2stream) which is respectively
|
|
154
|
-
readable and duplex streams
|
|
155
|
-
* if `digest: true` & `parse: true`
|
|
156
|
-
* `body` **{string | Array | Buffer | Object}** The body based on its content type
|
|
157
|
-
* if `digest: false`
|
|
158
|
-
* `arrayBuffer` **{AsyncFunction}** Reads the response and returns **ArrayBuffer**
|
|
159
|
-
* `blob` **{AsyncFunction}** Reads the response and returns **Blob**
|
|
160
|
-
* `body` **{AsyncFunction}** Reads the response and returns **Buffer** if `parse: false`
|
|
161
|
-
* `json` **{AsyncFunction}** Reads the response and returns **Object**
|
|
162
|
-
* `text` **{AsyncFunction}** Reads the response and returns **String**
|
|
163
|
-
* `bodyUsed` **{boolean}** Indicates whether the response were read or not
|
|
164
|
-
* `cookies` **{undefined | Cookies}** The cookies sent and received with the response
|
|
165
|
-
* `headers` **{Object}** The headers received with the response
|
|
166
|
-
* `httpVersion` **{string}** Indicates protocol version negotiated with the server
|
|
167
|
-
* `ok` **{boolean}** Indicates if the response was successful (statusCode: **200-299**)
|
|
168
|
-
* `redirected` **{boolean}** Indicates if the response is the result of a redirect
|
|
169
|
-
* `statusCode` **{number}** Indicates the status code of the response
|
|
170
|
-
* `trailers` **{undefined | Object}** The trailer headers received with the response
|
|
171
|
-
|
|
172
|
-
---
|
|
173
|
-
|
|
174
|
-
#### `rekwest.defaults`
|
|
175
|
-
|
|
176
|
-
The object to fulfill with default [options](#rekwesturl-options).
|
|
177
|
-
|
|
178
|
-
---
|
|
179
|
-
|
|
180
|
-
#### `rekwest.extend(options)`
|
|
181
|
-
|
|
182
|
-
The method to extend default [options](#rekwesturl-options) per instance.
|
|
183
|
-
|
|
184
|
-
```javascript
|
|
185
|
-
import rekwest, { constants } from 'rekwest';
|
|
186
|
-
|
|
187
|
-
const {
|
|
188
|
-
HTTP_STATUS_OK,
|
|
189
|
-
} = constants;
|
|
190
|
-
|
|
191
|
-
const rk = rekwest.extend({
|
|
192
|
-
baseURL: 'https://somewhe.re',
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
const signal = AbortSignal.timeout(1e4);
|
|
196
|
-
const url = '/somewhat/endpoint';
|
|
197
|
-
|
|
198
|
-
const res = await rk(url, {
|
|
199
|
-
signal,
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
console.assert(res.statusCode === HTTP_STATUS_OK);
|
|
203
|
-
console.info(res.headers);
|
|
204
|
-
console.log(res.body);
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
---
|
|
208
|
-
|
|
209
|
-
#### `rekwest.stream(url[, options])`
|
|
210
|
-
|
|
211
|
-
The method with limited functionality to use with streams and/or pipes.
|
|
212
|
-
|
|
213
|
-
* No automata (redirects & retries)
|
|
214
|
-
* Pass `h2: true` in options to use HTTP/2 protocol
|
|
215
|
-
* Use `ackn({ url: URL })` method in advance to check the available protocols
|
|
216
|
-
|
|
217
|
-
```javascript
|
|
218
|
-
import fs from 'node:fs';
|
|
219
|
-
import { pipeline } from 'node:stream/promises';
|
|
220
|
-
import rekwest, {
|
|
221
|
-
ackn,
|
|
222
|
-
constants,
|
|
223
|
-
} from 'rekwest';
|
|
224
|
-
|
|
225
|
-
const {
|
|
226
|
-
HTTP2_METHOD_POST,
|
|
227
|
-
} = constants;
|
|
228
|
-
|
|
229
|
-
const url = new URL('https://somewhe.re/somewhat/endpoint');
|
|
230
|
-
const options = await ackn({ url });
|
|
231
|
-
|
|
232
|
-
await pipeline(
|
|
233
|
-
fs.createReadStream('/var/tmp/vent/inlet.
|
|
234
|
-
rekwest.stream(url, { ...options, method: HTTP2_METHOD_POST }),
|
|
235
|
-
fs.createWriteStream('/var/tmp/vent/outlet.
|
|
236
|
-
);
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
---
|
|
240
|
-
|
|
241
|
-
For more details, please check tests (coverage: **>97%**) in the repository.
|
|
1
|
+
The robust request library that humanity deserves 🌐
|
|
2
|
+
---
|
|
3
|
+
This package provides highly likely functional and **easy-to-use** abstraction atop of
|
|
4
|
+
native [http(s).request](https://nodejs.org/api/https.html#httpsrequesturl-options-callback)
|
|
5
|
+
and [http2.request](https://nodejs.org/api/http2.html#clienthttp2sessionrequestheaders-options).
|
|
6
|
+
|
|
7
|
+
## Abstract
|
|
8
|
+
|
|
9
|
+
* Fetch-alike
|
|
10
|
+
* Cool-beans 🫐 config options (with defaults)
|
|
11
|
+
* Automatic HTTP/2 support (ALPN negotiation)
|
|
12
|
+
* Automatic or opt-in body parse (with non-UTF-8 charset decoding)
|
|
13
|
+
* Automatic and simplistic `Cookies` treatment (with built-in **jar** & **ttl**)
|
|
14
|
+
* Automatic decompression (with opt-in body compression)
|
|
15
|
+
* Built-in streamable `FormData` interface
|
|
16
|
+
* Support redirects & retries with fine-grained tune-ups
|
|
17
|
+
* Support all legit request body types (include blobs & streams)
|
|
18
|
+
* Support both CJS and ESM module systems
|
|
19
|
+
* Fully promise-able and pipe-able
|
|
20
|
+
* Zero dependencies
|
|
21
|
+
|
|
22
|
+
## Prerequisites
|
|
23
|
+
|
|
24
|
+
* Node.js `>= 18.13.0`
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install rekwest --save
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Usage
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
import rekwest, { constants } from 'rekwest';
|
|
36
|
+
|
|
37
|
+
const {
|
|
38
|
+
HTTP2_HEADER_AUTHORIZATION,
|
|
39
|
+
HTTP2_HEADER_CONTENT_ENCODING,
|
|
40
|
+
HTTP2_METHOD_POST,
|
|
41
|
+
HTTP_STATUS_OK,
|
|
42
|
+
} = constants;
|
|
43
|
+
|
|
44
|
+
const url = 'https://somewhe.re/somewhat/endpoint';
|
|
45
|
+
|
|
46
|
+
const res = await rekwest(url, {
|
|
47
|
+
body: { celestial: 'payload' },
|
|
48
|
+
headers: {
|
|
49
|
+
[HTTP2_HEADER_AUTHORIZATION]: 'Bearer [token]',
|
|
50
|
+
[HTTP2_HEADER_CONTENT_ENCODING]: 'br', // enables: body compression
|
|
51
|
+
/** [HTTP2_HEADER_CONTENT_TYPE]
|
|
52
|
+
* is undue for
|
|
53
|
+
* Array/Blob/File/FormData/Object/URLSearchParams body types
|
|
54
|
+
* and will be set automatically, with an option to override it here
|
|
55
|
+
*/
|
|
56
|
+
},
|
|
57
|
+
method: HTTP2_METHOD_POST,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
console.assert(res.statusCode === HTTP_STATUS_OK);
|
|
61
|
+
console.info(res.headers);
|
|
62
|
+
console.log(res.body);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
import { Readable } from 'node:stream';
|
|
69
|
+
import rekwest, {
|
|
70
|
+
constants,
|
|
71
|
+
Blob,
|
|
72
|
+
File,
|
|
73
|
+
FormData,
|
|
74
|
+
} from 'rekwest';
|
|
75
|
+
|
|
76
|
+
const {
|
|
77
|
+
HTTP2_HEADER_AUTHORIZATION,
|
|
78
|
+
HTTP2_HEADER_CONTENT_ENCODING,
|
|
79
|
+
HTTP2_METHOD_POST,
|
|
80
|
+
HTTP_STATUS_OK,
|
|
81
|
+
} = constants;
|
|
82
|
+
|
|
83
|
+
const blob = new Blob(['bits']);
|
|
84
|
+
const file = new File(['bits'], 'file.dab');
|
|
85
|
+
const readable = Readable.from('bits');
|
|
86
|
+
|
|
87
|
+
const fd = new FormData({
|
|
88
|
+
aux: Date.now(), // either [[key, value]] or kv sequenceable
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
fd.append('celestial', 'payload');
|
|
92
|
+
fd.append('blob', blob, 'blob.dab');
|
|
93
|
+
fd.append('file', file);
|
|
94
|
+
fd.append('readable', readable, 'readable.dab');
|
|
95
|
+
|
|
96
|
+
const url = 'https://somewhe.re/somewhat/endpoint';
|
|
97
|
+
|
|
98
|
+
const res = await rekwest(url, {
|
|
99
|
+
body: fd,
|
|
100
|
+
headers: {
|
|
101
|
+
[HTTP2_HEADER_AUTHORIZATION]: 'Bearer [token]',
|
|
102
|
+
[HTTP2_HEADER_CONTENT_ENCODING]: 'br', // enables: body compression
|
|
103
|
+
},
|
|
104
|
+
method: HTTP2_METHOD_POST,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
console.assert(res.statusCode === HTTP_STATUS_OK);
|
|
108
|
+
console.info(res.headers);
|
|
109
|
+
console.log(res.body);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### API
|
|
113
|
+
|
|
114
|
+
#### `rekwest(url[, options])`
|
|
115
|
+
|
|
116
|
+
* `url` **{string | URL}** The URL to send the request to
|
|
117
|
+
* `options` **{Object}**
|
|
118
|
+
Extends [http(s).RequestOptions](https://nodejs.org/api/https.html#httpsrequesturl-options-callback) along with
|
|
119
|
+
extra [http2.ClientSessionOptions](https://nodejs.org/api/http2.html#http2connectauthority-options-listener)
|
|
120
|
+
& [http2.ClientSessionRequestOptions](https://nodejs.org/api/http2.html#clienthttp2sessionrequestheaders-options)
|
|
121
|
+
and [tls.ConnectionOptions](https://nodejs.org/api/tls.html#tlsconnectoptions-callback)
|
|
122
|
+
for HTTP/2 attunes
|
|
123
|
+
* `baseURL` **{string | URL}** The base URL to use in cases where `url` is a relative URL
|
|
124
|
+
* `body` **{string | Array | ArrayBuffer | ArrayBufferView | AsyncIterator | Blob | Buffer | DataView | File |
|
|
125
|
+
FormData | Iterator | Object | Readable | ReadableStream | SharedArrayBuffer | URLSearchParams}** The body to send
|
|
126
|
+
with the request
|
|
127
|
+
* `cookies` **{boolean | Array<[k, v]> | Array<string\> | Cookies | Object | URLSearchParams}** `Default: true` The
|
|
128
|
+
cookies to add to
|
|
129
|
+
the request
|
|
130
|
+
* `cookiesTTL` **{boolean}** `Default: false` Controls enablement of TTL for the cookies cache
|
|
131
|
+
* `credentials` **{include | omit | same-origin}** `Default: same-origin` Controls credentials in case of cross-origin
|
|
132
|
+
redirects
|
|
133
|
+
* `digest` **{boolean}** `Default: true` Controls whether to read the response stream or simply add a mixin
|
|
134
|
+
* `follow` **{number}** `Default: 20` The number of redirects to follow
|
|
135
|
+
* `h2` **{boolean}** `Default: false` Forces the use of HTTP/2 protocol
|
|
136
|
+
* `headers` **{Object}** The headers to add to the request
|
|
137
|
+
* `maxRetryAfter` **{number}** The upper limit of `retry-after` header. If unset, it will use `timeout` value
|
|
138
|
+
* `parse` **{boolean}** `Default: true` Controls whether to parse response body or simply return a buffer
|
|
139
|
+
* `redirect` **{error | follow | manual}** `Default: follow` Controls the redirect flows
|
|
140
|
+
* `retry` **{Object}** Represents the retry options
|
|
141
|
+
* `attempts` **{number}** `Default: 0` The number of retry attempts
|
|
142
|
+
* `backoffStrategy` **{string}** `Default: interval * Math.log(Math.random() * (Math.E * Math.E - Math.E) + Math.E)`
|
|
143
|
+
The backoff strategy algorithm that increases logarithmically. To fixate set value to `interval * 1`
|
|
144
|
+
* `interval` **{number}** `Default: 1e3` The initial retry interval
|
|
145
|
+
* `retryAfter` **{boolean}** `Default: true` Controls `retry-after` header receptiveness
|
|
146
|
+
* `statusCodes` **{number[]}** `Default: [429, 503]` The list of status codes to retry on
|
|
147
|
+
* `stripTrailingSlash` **{boolean}** `Default: false` Controls whether to strip trailing slash at the end of the URL
|
|
148
|
+
* `thenable` **{boolean}** `Default: false` Controls the promise resolutions
|
|
149
|
+
* `timeout` **{number}** `Default: 3e5` The number of milliseconds a request can take before termination
|
|
150
|
+
* `trimTrailingSlashes` **{boolean}** `Default: false` Controls whether to trim trailing slashes within the URL
|
|
151
|
+
* **Returns:** Promise that resolves to
|
|
152
|
+
extended [http.IncomingMessage](https://nodejs.org/api/http.html#class-httpincomingmessage)
|
|
153
|
+
or [http2.ClientHttp2Stream](https://nodejs.org/api/http2.html#class-clienthttp2stream) which is respectively
|
|
154
|
+
readable and duplex streams
|
|
155
|
+
* if `digest: true` & `parse: true`
|
|
156
|
+
* `body` **{string | Array | Buffer | Object}** The body based on its content type
|
|
157
|
+
* if `digest: false`
|
|
158
|
+
* `arrayBuffer` **{AsyncFunction}** Reads the response and returns **ArrayBuffer**
|
|
159
|
+
* `blob` **{AsyncFunction}** Reads the response and returns **Blob**
|
|
160
|
+
* `body` **{AsyncFunction}** Reads the response and returns **Buffer** if `parse: false`
|
|
161
|
+
* `json` **{AsyncFunction}** Reads the response and returns **Object**
|
|
162
|
+
* `text` **{AsyncFunction}** Reads the response and returns **String**
|
|
163
|
+
* `bodyUsed` **{boolean}** Indicates whether the response were read or not
|
|
164
|
+
* `cookies` **{undefined | Cookies}** The cookies sent and received with the response
|
|
165
|
+
* `headers` **{Object}** The headers received with the response
|
|
166
|
+
* `httpVersion` **{string}** Indicates protocol version negotiated with the server
|
|
167
|
+
* `ok` **{boolean}** Indicates if the response was successful (statusCode: **200-299**)
|
|
168
|
+
* `redirected` **{boolean}** Indicates if the response is the result of a redirect
|
|
169
|
+
* `statusCode` **{number}** Indicates the status code of the response
|
|
170
|
+
* `trailers` **{undefined | Object}** The trailer headers received with the response
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
#### `rekwest.defaults`
|
|
175
|
+
|
|
176
|
+
The object to fulfill with default [options](#rekwesturl-options).
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
#### `rekwest.extend(options)`
|
|
181
|
+
|
|
182
|
+
The method to extend default [options](#rekwesturl-options) per instance.
|
|
183
|
+
|
|
184
|
+
```javascript
|
|
185
|
+
import rekwest, { constants } from 'rekwest';
|
|
186
|
+
|
|
187
|
+
const {
|
|
188
|
+
HTTP_STATUS_OK,
|
|
189
|
+
} = constants;
|
|
190
|
+
|
|
191
|
+
const rk = rekwest.extend({
|
|
192
|
+
baseURL: 'https://somewhe.re',
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const signal = AbortSignal.timeout(1e4);
|
|
196
|
+
const url = '/somewhat/endpoint';
|
|
197
|
+
|
|
198
|
+
const res = await rk(url, {
|
|
199
|
+
signal,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
console.assert(res.statusCode === HTTP_STATUS_OK);
|
|
203
|
+
console.info(res.headers);
|
|
204
|
+
console.log(res.body);
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
#### `rekwest.stream(url[, options])`
|
|
210
|
+
|
|
211
|
+
The method with limited functionality to use with streams and/or pipes.
|
|
212
|
+
|
|
213
|
+
* No automata (redirects & retries)
|
|
214
|
+
* Pass `h2: true` in options to use HTTP/2 protocol
|
|
215
|
+
* Use `ackn({ url: URL })` method in advance to check the available protocols
|
|
216
|
+
|
|
217
|
+
```javascript
|
|
218
|
+
import fs from 'node:fs';
|
|
219
|
+
import { pipeline } from 'node:stream/promises';
|
|
220
|
+
import rekwest, {
|
|
221
|
+
ackn,
|
|
222
|
+
constants,
|
|
223
|
+
} from 'rekwest';
|
|
224
|
+
|
|
225
|
+
const {
|
|
226
|
+
HTTP2_METHOD_POST,
|
|
227
|
+
} = constants;
|
|
228
|
+
|
|
229
|
+
const url = new URL('https://somewhe.re/somewhat/endpoint');
|
|
230
|
+
const options = await ackn({ url });
|
|
231
|
+
|
|
232
|
+
await pipeline(
|
|
233
|
+
fs.createReadStream('/var/tmp/vent/inlet.xyz'),
|
|
234
|
+
rekwest.stream(url, { ...options, method: HTTP2_METHOD_POST }),
|
|
235
|
+
fs.createWriteStream('/var/tmp/vent/outlet.xyz'),
|
|
236
|
+
);
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
For more details, please check tests (coverage: **>97%**) in the repository.
|
package/dist/cookies.js
CHANGED
|
@@ -31,41 +31,45 @@ class Cookies extends URLSearchParams {
|
|
|
31
31
|
return [it.split(';').at(0).trim()];
|
|
32
32
|
}
|
|
33
33
|
const [cookie, ...attrs] = it.split(';').map(it => it.trim());
|
|
34
|
-
const ttl =
|
|
34
|
+
const ttl = {};
|
|
35
|
+
for (const val of attrs) {
|
|
35
36
|
if (/(?:Expires|Max-Age)=/i.test(val)) {
|
|
36
37
|
const [key, value] = val.toLowerCase().split('=');
|
|
37
|
-
|
|
38
|
+
ttl[(0, _utils.toCamelCase)(key)] = !Number.isNaN(Number(value)) ? value * 1e3 : Date.parse(value) - Date.now();
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
}, {});
|
|
40
|
+
}
|
|
41
41
|
return [cookie.replace(/\u0022/g, ''), Object.keys(ttl).length ? ttl : null];
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
44
|
super(Array.isArray(input) ? input.map(it => it.at(0)).join('&') : input);
|
|
45
45
|
if (Array.isArray(input) && cookiesTTL) {
|
|
46
|
-
input.filter(it => it.at(1))
|
|
47
|
-
|
|
48
|
-
if (this.#chronometry.has(
|
|
49
|
-
clearTimeout(this.#chronometry.get(
|
|
50
|
-
this.#chronometry.delete(
|
|
46
|
+
for (const [cookie, ttl] of input.filter(it => it.at(1))) {
|
|
47
|
+
const key = cookie.split('=').at(0);
|
|
48
|
+
if (this.#chronometry.has(key)) {
|
|
49
|
+
clearTimeout(this.#chronometry.get(key));
|
|
50
|
+
this.#chronometry.delete(key);
|
|
51
51
|
}
|
|
52
52
|
const {
|
|
53
53
|
expires,
|
|
54
54
|
maxAge
|
|
55
55
|
} = ttl;
|
|
56
|
-
[maxAge, expires]
|
|
56
|
+
for (const ms of [maxAge, expires]) {
|
|
57
|
+
if (!Number.isInteger(ms)) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
57
60
|
const ref = new WeakRef(this);
|
|
58
61
|
const tid = setTimeout(() => {
|
|
59
62
|
const ctx = ref.deref();
|
|
60
63
|
if (ctx) {
|
|
61
|
-
ctx.#chronometry.delete(
|
|
62
|
-
ctx.delete(
|
|
64
|
+
ctx.#chronometry.delete(key);
|
|
65
|
+
ctx.delete(key);
|
|
63
66
|
}
|
|
64
67
|
}, Math.max(ms, 0));
|
|
65
68
|
this.constructor.#register(this, tid);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
this.#chronometry.set(key, tid);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
69
73
|
}
|
|
70
74
|
}
|
|
71
75
|
toString() {
|
package/dist/formdata.js
CHANGED
|
@@ -81,7 +81,9 @@ class FormData {
|
|
|
81
81
|
if (input.constructor === Object) {
|
|
82
82
|
input = Object.entries(input);
|
|
83
83
|
}
|
|
84
|
-
|
|
84
|
+
for (const [key, value] of input) {
|
|
85
|
+
this.append(key, value);
|
|
86
|
+
}
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
89
|
#ensureArgs(args, expected, method) {
|
package/dist/postflight.js
CHANGED
|
@@ -87,7 +87,9 @@ const postflight = (req, res, options, {
|
|
|
87
87
|
return res.emit('error', new _errors.RequestError(`Unable to ${redirect} redirect with streamable body.`));
|
|
88
88
|
}
|
|
89
89
|
if ([HTTP_STATUS_MOVED_PERMANENTLY, HTTP_STATUS_FOUND].includes(statusCode) && options.method === HTTP2_METHOD_POST || statusCode === HTTP_STATUS_SEE_OTHER && ![HTTP2_METHOD_GET, HTTP2_METHOD_HEAD].includes(options.method)) {
|
|
90
|
-
Object.keys(options.headers).filter(
|
|
90
|
+
for (const it of Object.keys(options.headers).filter(val => /^content-/i.test(val))) {
|
|
91
|
+
Reflect.deleteProperty(options.headers, it);
|
|
92
|
+
}
|
|
91
93
|
options.body = null;
|
|
92
94
|
options.method = HTTP2_METHOD_GET;
|
|
93
95
|
}
|
package/dist/preflight.js
CHANGED
|
@@ -60,7 +60,9 @@ const preflight = options => {
|
|
|
60
60
|
}
|
|
61
61
|
if (credentials === _constants.requestCredentials.omit) {
|
|
62
62
|
options.cookies = false;
|
|
63
|
-
Object.keys(options.headers ?? {}).filter(
|
|
63
|
+
for (const it of Object.keys(options.headers ?? {}).filter(val => new RegExp(`^(${HTTP2_HEADER_AUTHORIZATION}|${HTTP2_HEADER_COOKIE})$`, 'i').test(val))) {
|
|
64
|
+
Reflect.deleteProperty(options.headers, it);
|
|
65
|
+
}
|
|
64
66
|
url.password = url.username = '';
|
|
65
67
|
}
|
|
66
68
|
options.headers = {
|
package/dist/transfer.js
CHANGED
|
@@ -35,7 +35,9 @@ const transfer = async (options, overact) => {
|
|
|
35
35
|
protocol: url.protocol
|
|
36
36
|
};
|
|
37
37
|
} else if (Reflect.has(options, 'alpnProtocol')) {
|
|
38
|
-
['alpnProtocol', 'createConnection', 'h2', 'protocol']
|
|
38
|
+
for (const it of ['alpnProtocol', 'createConnection', 'h2', 'protocol']) {
|
|
39
|
+
Reflect.deleteProperty(options, it);
|
|
40
|
+
}
|
|
39
41
|
}
|
|
40
42
|
try {
|
|
41
43
|
options = await (0, _transform.transform)((0, _preflight.preflight)(options));
|