rock-req 5.1.1 → 5.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -540
- package/index.js +3 -3
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
<p align="center">
|
|
3
|
-
<img src="https://raw.githubusercontent.com/carboneio/rock-req/master/doc/rock-req-logo.svg" alt="rock-req logo" height="
|
|
3
|
+
<img src="https://raw.githubusercontent.com/carboneio/rock-req/master/doc/rock-req-logo.svg" alt="rock-req logo" height="150"/>
|
|
4
4
|
</p>
|
|
5
5
|
|
|
6
|
-
<h1 align="center" style="border-bottom:none;
|
|
6
|
+
<h1 align="center" style="border-bottom:none;">Rock-req</h1>
|
|
7
7
|
|
|
8
8
|
<p align="center">⭐️⭐️ Ensure your HTTP requests always reach their destination as <b>efficiently</b> as possible! ⭐️⭐️</p>
|
|
9
|
-
<p align="center">Tested on Mac, Linux, Windows with NodeJS 16, 18, 19</p>
|
|
9
|
+
<p align="center">Tested on Mac, Linux, Windows with NodeJS 16, 18, 19, 20</p>
|
|
10
10
|
|
|
11
11
|
[![npm][npm-image]][npm-url] [![ci][ci-image]][ci-url] [![javascript style guide][standard-image]][standard-url]
|
|
12
12
|
|
|
@@ -54,555 +54,39 @@ When the callback is called, the request is 100% finished, even with streams.
|
|
|
54
54
|
Stop using "slow by-default" and "false-light" HTTP request libraries!
|
|
55
55
|
|
|
56
56
|
|
|
57
|
-
| Library |
|
|
58
|
-
| ------------
|
|
59
|
-
| rock-req 🙋♂️ | 21797 req/s
|
|
60
|
-
| simple-get | 3260 req/s
|
|
61
|
-
| axios | 4910 req/s
|
|
62
|
-
| got | 1762 req/s
|
|
63
|
-
| fetch | 2102 req/s
|
|
64
|
-
| request | 1869 req/s
|
|
65
|
-
| superagent | 2100 req/s
|
|
66
|
-
| phin | 1164 req/s
|
|
67
|
-
|
|
|
57
|
+
| Library | NodeJS 16 | NodeJS 18 | NodeJS 20* | Size deps inc. |
|
|
58
|
+
| ------------ |--------------:|--------------:| --------------:| --------------:|
|
|
59
|
+
| rock-req 🙋♂️ | 22816 req/s | 21797 req/s | 21964 req/s | 144 LOC |
|
|
60
|
+
| simple-get | 2937 req/s | 3260 req/s | 21258 req/s | 317 LOC |
|
|
61
|
+
| axios | 5090 req/s | 4910 req/s | 3196 req/s | 13983 LOC |
|
|
62
|
+
| got | 2163 req/s | 1762 req/s | 9961 req/s | 9227 LOC |
|
|
63
|
+
| fetch | 2101 req/s | 2102 req/s | 2020 req/s | 13334 LOC |
|
|
64
|
+
| request | 2249 req/s | 1869 req/s | 15815 req/s | 46572 LOC |
|
|
65
|
+
| superagent | 2776 req/s | 2100 req/s | 2895 req/s | 16109 LOC |
|
|
66
|
+
| phin | 3178 req/s | 1164 req/s | 21299 req/s | 331 LOC |
|
|
67
|
+
| _undici_* | 24095 req/s | 24378 req/s | 24191 req/s | 16225 LOC |
|
|
68
68
|
|
|
69
69
|
|
|
70
|
+
> NodeJS 20 activates HTTP keep-alive by default, like `rock-req`
|
|
71
|
+
|
|
70
72
|
> `undici` is a low-level API, faster alternative to the native NodeJS http module. It is the glass ceiling limit for NodeJS.
|
|
71
73
|
|
|
72
74
|
> `rock-req` uses only the native NodeJS http module and provides many high-level features, a lot more than `phin` and `simple-get` with fewer lines of code.
|
|
73
75
|
|
|
74
|
-
> Tested
|
|
76
|
+
> Tested on Macbook Pro M1 Max
|
|
75
77
|
|
|
76
78
|
|
|
77
79
|
## Install
|
|
78
80
|
|
|
79
|
-
```
|
|
81
|
+
```bash
|
|
80
82
|
npm install rock-req
|
|
81
83
|
```
|
|
82
84
|
|
|
83
|
-
##
|
|
84
|
-
|
|
85
|
-
All functions accept two or three parameters:
|
|
86
|
-
|
|
87
|
-
```js
|
|
88
|
-
const rock = require('rock-req')
|
|
89
|
-
rock(optsOrURL [, bodyOrStreamFn], callback)
|
|
90
|
-
````
|
|
91
|
-
|
|
92
|
-
- `optsOrURL` can be an object or a the URL
|
|
93
|
-
- `bodyOrStreamFn` can be a buffer/string/object or a function returning an input stream for sending the body of POST/PUT/PATCH/DELETE requests
|
|
94
|
-
- `callback(err, res, data)` called only when everything is finished (even with streams).
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
### GET, HEAD requests
|
|
98
|
-
|
|
99
|
-
```js
|
|
100
|
-
// res is the server response, already consumed by rock-req. The result is in data
|
|
101
|
-
rock.get('http://ex.com', (err, res, data) => {
|
|
102
|
-
console.log(res.statusCode) // 200
|
|
103
|
-
console.log(data) // Buffer('server response')
|
|
104
|
-
})
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
Alternative syntax:
|
|
108
|
-
|
|
109
|
-
```js
|
|
110
|
-
rock({ method: 'GET', url: 'http://ex.com' }, function (err, res, data) {} )
|
|
111
|
-
// OR
|
|
112
|
-
rock.concat({ method: 'GET', url: 'http://ex.com' }, function (err, res, data) {} )
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
Head requests:
|
|
116
|
-
|
|
117
|
-
```js
|
|
118
|
-
rock.head('http://example.com', (err, res, data) => {})
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### POST, PUT, PATCH, DELETE requests
|
|
122
|
-
|
|
123
|
-
Use the second paramater to pass the body:
|
|
124
|
-
|
|
125
|
-
```js
|
|
126
|
-
rock.post('http://ex.com', 'POST body', (err, res, data) => {})
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
Alternative syntax:
|
|
130
|
-
|
|
131
|
-
```js
|
|
132
|
-
rock({ method: 'POST', url : 'http://ex.com', body : 'POST body' }, function (err, res, data) {} )
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
### JSON request shortcuts
|
|
136
|
-
|
|
137
|
-
Automatically serialize/deserialize request and response with JSON with `getJSON`, `putJSON`, `postJSON`, `deleteJSON`, ...
|
|
138
|
-
|
|
139
|
-
```js
|
|
140
|
-
rock.putJSON('http://ex.com', { id : 123 }, (err, res, data) => {
|
|
141
|
-
console.log(data) // already JSON.parsed
|
|
142
|
-
})
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
Alternative syntax:
|
|
146
|
-
|
|
147
|
-
```js
|
|
148
|
-
rock({ method: 'PUT', url: 'http://ex.com', body: { id : 123 }, json: true }, function (err, res, data) {} )
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### All options:
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
```js
|
|
155
|
-
const opts = {
|
|
156
|
-
url : 'http://example.com',
|
|
157
|
-
method : 'POST',
|
|
158
|
-
body : 'this is the POST body',
|
|
159
|
-
headers: {
|
|
160
|
-
'user-agent': 'my cool app'
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
rock(opts, function (err, res, data) {} )
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
**opts** can contain any value of NodeJS http.request with rock-req parameters. Here are the most used one:
|
|
167
|
-
|
|
168
|
-
- `maxRedirects <number>`overwrite global maximum number of redirects. Defaults to 10
|
|
169
|
-
- `maxRetry <number>` overwrite global maximum number of retries. Defaults to 1
|
|
170
|
-
- `followRedirects <boolean>` do not follow redirects
|
|
171
|
-
- `body <buffer> | <string> | <object> | <function>` body to post
|
|
172
|
-
- `json <boolean>` automatically stringify/parse request/response Default : false
|
|
173
|
-
- `url <string>` the destination URL
|
|
174
|
-
- `method <string>` A string specifying the HTTP request method. Default: 'GET'.
|
|
175
|
-
- `headers <object>` An object containing request headers. Default: 'accept-encoding': 'gzip, deflate, br'
|
|
176
|
-
- `timeout <number>`: A number specifying the socket timeout in milliseconds.
|
|
177
|
-
- `auth <string>` Basic authentication ('user:password') to compute an Authorization header.
|
|
178
|
-
- `port <number>` Port of remote server. Default: defaultPort if set, else 80.
|
|
179
|
-
- `host <string>` A domain name or IP address of the server to issue the request to. Default: 'localhost'.
|
|
180
|
-
- `hostname <string>` Alias for host.
|
|
181
|
-
- `path <string>` Request path. Should include query string if any. E.G. '/index.html?page=12'.
|
|
182
|
-
- `protocol <string>` Protocol to use. Default: 'http:'.
|
|
183
|
-
- `setHost <boolean>`: Specifies whether or not to automatically add the Host header. Defaults to true.
|
|
184
|
-
- `agent <http.Agent> | <boolean>` Controls Agent behavior. Possible values:
|
|
185
|
-
- `undefined` (default): use http.globalAgent for this host and port.
|
|
186
|
-
- `Agent object`: explicitly use the passed in Agent.
|
|
187
|
-
- `false` causes a new Agent with default values to be used.
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
### Input Stream
|
|
191
|
-
|
|
192
|
-
Rock-req requires that input stream is initialized in a function.
|
|
193
|
-
|
|
194
|
-
This function is invoked by rock-req for every request retry.
|
|
195
|
-
|
|
196
|
-
If something goes wrong, the Readable stream is destroyed automatically and the error can be captured with `'error'` event or `stream.finished` (optional).
|
|
197
|
-
|
|
198
|
-
```js
|
|
199
|
-
const rock = require('rock-req')
|
|
200
|
-
const fs = require('fs')
|
|
201
|
-
|
|
202
|
-
// opts contains options passed in rock(opts). DO NOT MODIFY IT
|
|
203
|
-
function createInputStream(opts) {
|
|
204
|
-
return fs.createReadStream('input.txt');
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const opts = {
|
|
208
|
-
url : 'http://example.com',
|
|
209
|
-
body: createInputStream
|
|
210
|
-
}
|
|
211
|
-
rock(opts, function (err, res, data) {})
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
Alternative syntax:
|
|
215
|
-
|
|
216
|
-
```js
|
|
217
|
-
rock.post('http://example.com', createInputStream, function (err, res, data) {})
|
|
218
|
-
// or with more object options:
|
|
219
|
-
rock.post(opts, createInputStream, function (err, res, data) {})
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
### Output Stream
|
|
223
|
-
|
|
224
|
-
Rock-req requires that output stream is initialized in a function.
|
|
225
|
-
|
|
226
|
-
This function is invoked by rock-req for every request retry.
|
|
227
|
-
|
|
228
|
-
If something goes wrong, the Writable stream is destroyed automatically and the error can be captured with `'error'` event or `stream.finished` (optional).
|
|
229
|
-
|
|
230
|
-
```js
|
|
231
|
-
const rock = require('rock-req')
|
|
232
|
-
const fs = require('fs')
|
|
233
|
-
|
|
234
|
-
// opts contains options passed in rock(opts). DO NOT MODIFY IT
|
|
235
|
-
// res if the http response (res.statusCode, ...). DO NOT MODIFY IT and DO NOT CONSUME THE RES STREAM YOURSELF
|
|
236
|
-
function createOutputStream(opts, res) {
|
|
237
|
-
|
|
238
|
-
const writer = fs.createWriteStream('test_gfg.txt')
|
|
239
|
-
// If you need to do some action (removing temporary files, ...), uses this native NodeJS method:
|
|
240
|
-
writer.on('error', (e) => { /* clean up your stuff */ })
|
|
241
|
-
return writer
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const opts = {
|
|
245
|
-
url : 'http://example.com',
|
|
246
|
-
output : createOutputStream
|
|
247
|
-
}
|
|
248
|
-
rock(opts, function (err, res) {})
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
### Retry on failure
|
|
252
|
-
|
|
253
|
-
By default, rock-req retries with the following errors if `maxRetry > 0`.
|
|
254
|
-
|
|
255
|
-
The callback is called when the request succeed or all retries are done
|
|
256
|
-
|
|
257
|
-
```js
|
|
258
|
-
const rock = require('rock-req');
|
|
259
|
-
|
|
260
|
-
// default values can be overwritten like this:
|
|
261
|
-
rock.defaults.retryOnCode = [
|
|
262
|
-
408, /* Request Timeout */
|
|
263
|
-
429, /* Too Many Requests */
|
|
264
|
-
502, /* Bad Gateway */
|
|
265
|
-
503, /* Service Unavailable */
|
|
266
|
-
504, /* Gateway Timeout*/
|
|
267
|
-
521, /* Web Server Is Down*/
|
|
268
|
-
522, /* Cloudflare Connection Timed Out */
|
|
269
|
-
524 /* Cloudflare A Timeout Occurred */
|
|
270
|
-
];
|
|
271
|
-
rock.defaults.retryOnError = [
|
|
272
|
-
'ETIMEDOUT', /* One of the timeout limits was reached. */
|
|
273
|
-
'ECONNRESET', /* The connection was forcibly closed. */
|
|
274
|
-
'EADDRINUSE', /* Could not bind to any free port */
|
|
275
|
-
'ECONNREFUSED', /* The connection was refused by the server. */
|
|
276
|
-
'EPIPE', /* The remote side of the stream being written has been closed. */
|
|
277
|
-
'ENOTFOUND', /* Could not resolve the hostname to an IP address. */
|
|
278
|
-
'ENETUNREACH', /* No internet connection. */
|
|
279
|
-
'EAI_AGAIN' /* DNS lookup timed out. */
|
|
280
|
-
];
|
|
281
|
-
|
|
282
|
-
const opts = {
|
|
283
|
-
url : 'http://example.com',
|
|
284
|
-
body : 'this is the POST body',
|
|
285
|
-
maxRetry : 1
|
|
286
|
-
}
|
|
287
|
-
rock(opts, function (err, res, data) {} );
|
|
288
|
-
```
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
### Global options & Extend
|
|
292
|
-
|
|
293
|
-
Change default parameters globally (not recommended), or create a new instance with specific paramaters (see below)
|
|
294
|
-
|
|
295
|
-
```js
|
|
296
|
-
rock.defaults = {
|
|
297
|
-
headers : { 'accept-encoding': 'gzip, deflate, br' },
|
|
298
|
-
maxRedirects : 10,
|
|
299
|
-
maxRetry : 1,
|
|
300
|
-
retryDelay : 10, //ms
|
|
301
|
-
retryOnCode : [408, 429, 502, 503, 504, 521, 522, 524 ],
|
|
302
|
-
retryOnError : ['ETIMEDOUT', 'ECONNRESET', 'EADDRINUSE', 'ECONNREFUSED','EPIPE', 'ENOTFOUND', 'ENETUNREACH', 'EAI_AGAIN' ],
|
|
303
|
-
// beforeRequest is called for each request, retry and redirect
|
|
304
|
-
beforeRequest : (opts) => {
|
|
305
|
-
// There options can be overwritted (= parsed opts.url)
|
|
306
|
-
opts.protocol = 'https:' // or 'http:'
|
|
307
|
-
opts.hostname = 'google.com';
|
|
308
|
-
opts.port = 443;
|
|
309
|
-
opts.path = '/mypage.html?bla=1#hash';
|
|
310
|
-
opts.auth = '';
|
|
311
|
-
opts.headers = {};
|
|
312
|
-
opts.body = {};
|
|
313
|
-
opts.method = 'POST';
|
|
314
|
-
opts.remainingRetry;
|
|
315
|
-
opts.remainingRedirects;
|
|
316
|
-
opts.agent = otherHttpAgent;
|
|
317
|
-
|
|
318
|
-
// READ-ONLY options (not exhaustive)
|
|
319
|
-
opts.url; // DOT NOT OVERWRITE
|
|
320
|
-
opts.maxRetry;
|
|
321
|
-
opts.maxRedirects;
|
|
322
|
-
opts.prevError; // error of previous request on retry
|
|
323
|
-
opts.prevStatusCode; // HTTP status code of previous request on retry/redirect
|
|
324
|
-
|
|
325
|
-
// opts must be returned
|
|
326
|
-
return opts;
|
|
327
|
-
},
|
|
328
|
-
}
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
Create a new instance with specific parameters instead of modifying global `rock.defaults`.
|
|
332
|
-
|
|
333
|
-
By default, this new instance inherits values of the instance source if options are not overwritten.
|
|
334
|
-
Headers are merged. Then only the first level of the options object is merged (no deep travelling in sub-objects or arrays).
|
|
335
|
-
|
|
336
|
-
The `keepAliveDuration` can be changed only with `extend` method because `rock-req` creates new http Agent on new instances.
|
|
337
|
-
|
|
338
|
-
```js
|
|
339
|
-
const myInstance = rock.extend({
|
|
340
|
-
keepAliveDuration : 0, // Change keep alive duration. Default to 3000ms. Set 0 to deactivate keep alive.
|
|
341
|
-
headers: {
|
|
342
|
-
'Custom-header': 'x-for-proxy'
|
|
343
|
-
},
|
|
344
|
-
timeout : 1000
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
myInstance.get('http://example.com', function (err, res, data) {})
|
|
348
|
-
```
|
|
349
|
-
|
|
350
|
-
### Intercept retries for Higher Availability / Higher bandwidth
|
|
351
|
-
|
|
352
|
-
`beforeRequest` is always called on each request, each redirect and each retry.
|
|
353
|
-
- on redirect, `opts.url` (and `hostname`, `port`, `protocol`, `path`) is updated to the new location. `opts.url` is null if it is a relative redirect.
|
|
354
|
-
- on retry, `opts.url` (and `hostname`, `port`, `protocol`, `path`) have the same value as they did
|
|
355
|
-
when the rock-req was initially called.
|
|
356
|
-
|
|
357
|
-
For example, you can dynamically change the http Agent to use a another proxy on each request.
|
|
358
|
-
Be careful, in this case, you must provide the right http/https Agent if there is a redirection from http to https.
|
|
359
|
-
Otherwise, rock-req automatically replaces your Agent with the correct one if the protocol changes after redirection.
|
|
360
|
-
|
|
361
|
-
Or, you can rewrite the URL if you want to use [HAProxy as a forward proxy](https://www.haproxy.com/user-spotlight-series/haproxy-as-egress-controller/).
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
```js
|
|
365
|
-
const myInstance = rock.extend({
|
|
366
|
-
beforeRequest: (opts) => {
|
|
367
|
-
const { hostname, port, protocol, path } = opts;
|
|
368
|
-
opts.protocol = 'http:';
|
|
369
|
-
opts.hostname = '10.0.0.1';
|
|
370
|
-
opts.port = 80;
|
|
371
|
-
opts.path = `${hostname}/${port}${path}`;
|
|
372
|
-
return opts;
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
myInstance.get('http://example.com', function (err, res, data) {})
|
|
377
|
-
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
### Timeout
|
|
382
|
-
|
|
383
|
-
You can set a timeout (in milliseconds) on the request with the `timeout` option.
|
|
384
|
-
If the request takes longer than `timeout` to complete, then the entire request
|
|
385
|
-
will fail with an `Error`.
|
|
386
|
-
|
|
387
|
-
```js
|
|
388
|
-
const rock = require('rock-req')
|
|
389
|
-
|
|
390
|
-
const opts = {
|
|
391
|
-
url: 'http://example.com',
|
|
392
|
-
timeout: 2000 // 2 second timeout
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
rock(opts, function (err, res, data) {})
|
|
396
|
-
```
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
### One Quick Tip
|
|
400
|
-
|
|
401
|
-
It's a good idea to set the `'user-agent'` header so the provider can more easily
|
|
402
|
-
see how their resource is used.
|
|
403
|
-
|
|
404
|
-
```js
|
|
405
|
-
const rock = require('rock-req')
|
|
406
|
-
const pkg = require('./package.json')
|
|
407
|
-
|
|
408
|
-
rock({
|
|
409
|
-
url : 'http://example.com',
|
|
410
|
-
headers: {
|
|
411
|
-
'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`
|
|
412
|
-
}
|
|
413
|
-
}, function (err, res, data) {})
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
### Proxies
|
|
417
|
-
|
|
418
|
-
You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with the
|
|
419
|
-
`agent` option to work with proxies:
|
|
420
|
-
|
|
421
|
-
```js
|
|
422
|
-
const rock = require('rock-req')
|
|
423
|
-
const tunnel = require('tunnel')
|
|
424
|
-
|
|
425
|
-
const opts = {
|
|
426
|
-
url: 'http://example.com',
|
|
427
|
-
agent: tunnel.httpOverHttp({
|
|
428
|
-
proxy: {
|
|
429
|
-
host: 'localhost'
|
|
430
|
-
}
|
|
431
|
-
})
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
rock(opts, function (err, res, data) {})
|
|
435
|
-
```
|
|
436
|
-
|
|
437
|
-
### Cookies
|
|
85
|
+
## Documentation
|
|
438
86
|
|
|
439
|
-
|
|
440
|
-
cookies in a request:
|
|
87
|
+
[The full documentation is here](doc/api.md) to reduce Node package file size.
|
|
441
88
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
```js
|
|
445
|
-
const rock = require('rock-req')
|
|
446
|
-
const cookie = require('cookie')
|
|
447
|
-
|
|
448
|
-
const opts = {
|
|
449
|
-
url: 'http://example.com',
|
|
450
|
-
headers: {
|
|
451
|
-
cookie: cookie.serialize('foo', 'bar')
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
rock(opts, function (err, res, data) {})
|
|
456
|
-
```
|
|
457
|
-
|
|
458
|
-
### Form data
|
|
459
|
-
|
|
460
|
-
You can use the [`form-data`](https://github.com/form-data/form-data) module to
|
|
461
|
-
create POST request with form data:
|
|
462
|
-
|
|
463
|
-
```js
|
|
464
|
-
const fs = require('fs')
|
|
465
|
-
const rock = require('rock-req')
|
|
466
|
-
const FormData = require('form-data')
|
|
467
|
-
const form = new FormData()
|
|
468
|
-
|
|
469
|
-
const opts = {
|
|
470
|
-
url: 'http://example.com',
|
|
471
|
-
body: () => {
|
|
472
|
-
form.append('my_file', fs.createReadStream('/foo/bar.jpg'))
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
rock.post(opts, function (err, res, data) {})
|
|
477
|
-
```
|
|
478
|
-
|
|
479
|
-
#### Or, include `application/x-www-form-urlencoded` form data manually:
|
|
480
|
-
|
|
481
|
-
```js
|
|
482
|
-
const rock = require('rock-req')
|
|
483
|
-
|
|
484
|
-
const opts = {
|
|
485
|
-
url: 'http://example.com',
|
|
486
|
-
form: {
|
|
487
|
-
key: 'value'
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
rock.post(opts, function (err, res, data) {})
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
### Specifically disallowing redirects
|
|
494
|
-
|
|
495
|
-
```js
|
|
496
|
-
const rock = require('rock-req')
|
|
497
|
-
|
|
498
|
-
const opts = {
|
|
499
|
-
url: 'http://example.com/will-redirect-elsewhere',
|
|
500
|
-
followRedirects: false
|
|
501
|
-
}
|
|
502
|
-
// res.statusCode will be 301, no error thrown
|
|
503
|
-
rock(opts, function (err, res, data) {})
|
|
504
|
-
```
|
|
505
|
-
|
|
506
|
-
### Basic Auth
|
|
507
|
-
|
|
508
|
-
```js
|
|
509
|
-
const user = 'someuser'
|
|
510
|
-
const pass = 'pa$$word'
|
|
511
|
-
const encodedAuth = Buffer.from(`${user}:${pass}`).toString('base64')
|
|
512
|
-
|
|
513
|
-
rock('http://example.com', {
|
|
514
|
-
headers: {
|
|
515
|
-
authorization: `Basic ${encodedAuth}`
|
|
516
|
-
}
|
|
517
|
-
})
|
|
518
|
-
```
|
|
519
|
-
|
|
520
|
-
### OAuth
|
|
521
|
-
|
|
522
|
-
You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) module to create
|
|
523
|
-
a signed OAuth request:
|
|
524
|
-
|
|
525
|
-
```js
|
|
526
|
-
const rock = require('rock-req')
|
|
527
|
-
const crypto = require('crypto')
|
|
528
|
-
const OAuth = require('oauth-1.0a')
|
|
529
|
-
|
|
530
|
-
const oauth = OAuth({
|
|
531
|
-
consumer: {
|
|
532
|
-
key: process.env.CONSUMER_KEY,
|
|
533
|
-
secret: process.env.CONSUMER_SECRET
|
|
534
|
-
},
|
|
535
|
-
signature_method: 'HMAC-SHA1',
|
|
536
|
-
hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
|
|
537
|
-
})
|
|
538
|
-
|
|
539
|
-
const token = {
|
|
540
|
-
key: process.env.ACCESS_TOKEN,
|
|
541
|
-
secret: process.env.ACCESS_TOKEN_SECRET
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'
|
|
545
|
-
|
|
546
|
-
const opts = {
|
|
547
|
-
url: url,
|
|
548
|
-
headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),
|
|
549
|
-
json: true
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
rock(opts, function (err, res) {})
|
|
553
|
-
```
|
|
554
|
-
|
|
555
|
-
### Throttle requests
|
|
556
|
-
|
|
557
|
-
You can use [limiter](https://github.com/jhurliman/node-rate-limiter) to throttle requests. This is useful when calling an API that is rate limited.
|
|
558
|
-
|
|
559
|
-
```js
|
|
560
|
-
const rock = require('rock-get')
|
|
561
|
-
const RateLimiter = require('limiter').RateLimiter
|
|
562
|
-
const limiter = new RateLimiter(1, 'second')
|
|
563
|
-
|
|
564
|
-
const rock = (opts, cb) => limiter.removeTokens(1, () => rock(opts, cb))
|
|
565
|
-
rock.concat = (opts, cb) => limiter.removeTokens(1, () => rock.concat(opts, cb))
|
|
566
|
-
|
|
567
|
-
var opts = {
|
|
568
|
-
url: 'http://example.com'
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
rock.concat(opts, processResult)
|
|
572
|
-
rock.concat(opts, processResult)
|
|
573
|
-
|
|
574
|
-
function processResult (err, res, data) {
|
|
575
|
-
if (err) throw err
|
|
576
|
-
console.log(data.toString())
|
|
577
|
-
}
|
|
578
|
-
```
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
## Breaking change for people coming from simplet-get
|
|
582
|
-
|
|
583
|
-
Rock-req is a fork of [simple-get](https://github.com/feross/simple-get)
|
|
584
|
-
|
|
585
|
-
- Rock-req concatenates chunks and returns the concatenated buffer/JSON in the third paramater of the callback.
|
|
586
|
-
Why? because 80% of the time, we need to do simple request without streams.
|
|
587
|
-
When you need to pipe the result to another stream, you must use `opts.output` parameter (see "Output Stream" in the doc)
|
|
588
|
-
- All streams must be created in a function. Rock-req return an error if it is not the case
|
|
589
|
-
- before: `body = stream`
|
|
590
|
-
- after `body = () => { const myStream = create(); return myStream; }`
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
## TODO:
|
|
594
|
-
|
|
595
|
-
- [ ] replace deprecated `url.parse` by `new URL` but new URL is slower than url.parse. Let's see if Node 20 LTS is faster
|
|
596
|
-
- [ ] add advanced timeout (response timeout)
|
|
597
|
-
- [ ] test prevError
|
|
598
|
-
- [ ] test HTTP abort signal option
|
|
599
|
-
- [ ] test input stream error with 502 error retry. Does stream.resume destroy all streams?
|
|
600
|
-
- [ ] promisify
|
|
601
|
-
- [ ] typescript type
|
|
602
|
-
- [ ] NodesJS 19 doesn't need agent.timeout to exit https://github.com/nodejs/node/issues/47228 https://github.com/nodejs/node/issues/2642
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
# Supporters
|
|
89
|
+
## Supporters
|
|
606
90
|
|
|
607
91
|
This packaged in maintained by Carbone:
|
|
608
92
|
|
|
@@ -614,6 +98,3 @@ This packaged in maintained by Carbone:
|
|
|
614
98
|
|
|
615
99
|
|
|
616
100
|
Thank you [Feross Aboukhadijeh](https://github.com/feross), creator of `simple-get`
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
package/index.js
CHANGED
|
@@ -23,7 +23,7 @@ function extend (defaultOptions = {}) {
|
|
|
23
23
|
}
|
|
24
24
|
defaultOptions.headers = applyDefault(cloneLowerCase(defaultOptions.headers), _default.headers)
|
|
25
25
|
_default = applyDefault(defaultOptions, _default) // inherits of parent options
|
|
26
|
-
const agents = [http, https].map(h => (_default.keepAliveDuration > 0) ? new h.Agent({ keepAlive: true, keepAliveMsecs: _default.keepAliveDuration
|
|
26
|
+
const agents = [http, https].map(h => (_default.keepAliveDuration > 0) ? new h.Agent({ keepAlive: true, keepAliveMsecs: _default.keepAliveDuration }) : undefined)
|
|
27
27
|
|
|
28
28
|
function rock (opts, directBody, cb) {
|
|
29
29
|
if (typeof opts === 'string') opts = { url: opts }
|
|
@@ -71,7 +71,7 @@ function extend (defaultOptions = {}) {
|
|
|
71
71
|
streamToCleanOnError.forEach(s => s.destroy(err))
|
|
72
72
|
if (opts.retryOnError.indexOf(err?.code) !== -1 && --opts.remainingRetry > 0) {
|
|
73
73
|
opts.prevError = err
|
|
74
|
-
return setTimeout(rock, opts.retryDelay, opts, cb) // retry
|
|
74
|
+
return setTimeout(rock, opts.retryDelay, opts, cb) // retry
|
|
75
75
|
}
|
|
76
76
|
return cb(err)
|
|
77
77
|
}
|
|
@@ -83,7 +83,7 @@ function extend (defaultOptions = {}) {
|
|
|
83
83
|
}
|
|
84
84
|
function listen (stream, isLast = false) {
|
|
85
85
|
streamToCleanOnError.push(stream)
|
|
86
|
-
stream.
|
|
86
|
+
stream.on('error', onRequestEnd) // do not use once()! A stream can emit mutiple error event
|
|
87
87
|
stream.once('close', () => {
|
|
88
88
|
if (stream.readableEnded === false || stream.writableEnded === false) return onRequestEnd(new Error('ERR_STREAM_PREMATURE_CLOSE'))
|
|
89
89
|
if (isLast === true) return onRequestEnd()
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rock-req",
|
|
3
3
|
"description": "Ultra-light (150 LOC, No dependencies) & Ultra-fast request library with reliable retry on failure, http/https, redirects, gzip/deflate/brotli, extensible, proxy, streams, JSON mode, forms, timeout",
|
|
4
|
-
"version": "5.1.
|
|
4
|
+
"version": "5.1.3",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "David Grelaud & Feross Aboukhadijeh"
|
|
7
7
|
},
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"url": "git://github.com/carboneio/rock-req.git"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
|
-
"test": "tape test/*.js"
|
|
45
|
+
"test": "tape test/*.js",
|
|
46
|
+
"standard": "node_modules/.bin/standard"
|
|
46
47
|
}
|
|
47
48
|
}
|