got 5.1.0 → 5.3.1

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.
Files changed (3) hide show
  1. package/index.js +30 -14
  2. package/package.json +5 -4
  3. package/readme.md +13 -5
package/index.js CHANGED
@@ -5,7 +5,8 @@ var https = require('https');
5
5
  var urlLib = require('url');
6
6
  var querystring = require('querystring');
7
7
  var objectAssign = require('object-assign');
8
- var duplexify = require('duplexify');
8
+ var PassThrough = require('readable-stream').PassThrough;
9
+ var duplexer2 = require('duplexer2');
9
10
  var isStream = require('is-stream');
10
11
  var readAllStream = require('read-all-stream');
11
12
  var timedOut = require('timed-out');
@@ -19,11 +20,6 @@ var nodeStatusCodes = require('node-status-codes');
19
20
  var isPlainObj = require('is-plain-obj');
20
21
  var parseJson = require('parse-json');
21
22
 
22
- function backoff(iter) {
23
- var noise = Math.random() * 1000;
24
- return (1 << iter) * 1000 + noise;
25
- }
26
-
27
23
  function requestAsEventEmitter(opts) {
28
24
  opts = opts || {};
29
25
 
@@ -56,13 +52,14 @@ function requestAsEventEmitter(opts) {
56
52
 
57
53
  // do not write ee.bind(...) instead of function - it will break gzip in Node.js 0.10
58
54
  setImmediate(function () {
59
- ee.emit('response', typeof unzipResponse === 'function' ? unzipResponse(res) : res);
55
+ ee.emit('response', typeof unzipResponse === 'function' && req.method !== 'HEAD' ? unzipResponse(res) : res);
60
56
  });
61
57
  });
62
58
 
63
59
  req.once('error', function (err) {
64
- if (retryCount < opts.retries) {
65
- setTimeout(get, backoff(retryCount++), opts);
60
+ var backoff = opts.retries(++retryCount, err);
61
+ if (backoff) {
62
+ setTimeout(get, backoff, opts);
66
63
  return;
67
64
  }
68
65
 
@@ -144,7 +141,9 @@ function asPromise(opts) {
144
141
  }
145
142
 
146
143
  function asStream(opts) {
147
- var proxy = duplexify();
144
+ var input = new PassThrough();
145
+ var output = new PassThrough();
146
+ var proxy = duplexer2(input, output);
148
147
 
149
148
  if (opts.json) {
150
149
  throw new Error('got can not be used as stream when options.json is used');
@@ -172,7 +171,7 @@ function asStream(opts) {
172
171
  }
173
172
 
174
173
  if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
175
- proxy.setWritable(req);
174
+ input.pipe(req);
176
175
  return;
177
176
  }
178
177
 
@@ -182,7 +181,7 @@ function asStream(opts) {
182
181
  ee.on('response', function (res) {
183
182
  var statusCode = res.statusCode;
184
183
 
185
- proxy.setReadable(res);
184
+ res.pipe(output);
186
185
 
187
186
  if (statusCode < 200 || statusCode > 299) {
188
187
  proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
@@ -270,6 +269,18 @@ function normalizeArguments(url, opts) {
270
269
  }
271
270
  }
272
271
 
272
+ if (typeof opts.retries !== 'function') {
273
+ var retries = opts.retries;
274
+ opts.retries = function backoff(iter) {
275
+ if (iter > retries) {
276
+ return 0;
277
+ }
278
+
279
+ var noise = Math.random() * 100;
280
+ return (1 << iter) * 1000 + noise;
281
+ };
282
+ }
283
+
273
284
  return opts;
274
285
  }
275
286
 
@@ -320,8 +331,13 @@ got.stream = function (url, opts, cb) {
320
331
  };
321
332
 
322
333
  helpers.forEach(function (el) {
323
- got.stream[el] = function (url, opts) {
324
- return got.stream(url, objectAssign({}, opts, {method: el.toUpperCase()}));
334
+ got.stream[el] = function (url, opts, cb) {
335
+ if (typeof opts === 'function') {
336
+ cb = opts;
337
+ opts = {};
338
+ }
339
+
340
+ return got.stream(url, objectAssign({}, opts, {method: el.toUpperCase()}), cb);
325
341
  };
326
342
  });
327
343
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "5.1.0",
3
+ "version": "5.3.1",
4
4
  "description": "Simplified HTTP/HTTPS requests",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
@@ -46,7 +46,7 @@
46
46
  ],
47
47
  "dependencies": {
48
48
  "create-error-class": "^2.0.0",
49
- "duplexify": "^3.2.0",
49
+ "duplexer2": "^0.1.4",
50
50
  "is-plain-obj": "^1.0.0",
51
51
  "is-redirect": "^1.0.0",
52
52
  "is-stream": "^1.0.0",
@@ -54,14 +54,15 @@
54
54
  "node-status-codes": "^1.0.0",
55
55
  "object-assign": "^4.0.1",
56
56
  "parse-json": "^2.1.0",
57
- "pinkie-promise": "^1.0.0",
57
+ "pinkie-promise": "^2.0.0",
58
58
  "read-all-stream": "^3.0.0",
59
+ "readable-stream": "^2.0.5",
59
60
  "timed-out": "^2.0.0",
60
61
  "unzip-response": "^1.0.0",
61
62
  "url-parse-lax": "^1.0.0"
62
63
  },
63
64
  "devDependencies": {
64
- "ava": "^0.3.0",
65
+ "ava": "^0.5.0",
65
66
  "coveralls": "^2.11.4",
66
67
  "get-port": "^2.0.0",
67
68
  "into-stream": "^2.0.0",
package/readme.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  > Simplified HTTP/HTTPS requests
10
10
 
11
- [![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) [![Coverage Status](https://coveralls.io/repos/sindresorhus/got/badge.svg?service=github&branch=master)](https://coveralls.io/github/sindresorhus/got?branch=master)
11
+ [![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) [![Coverage Status](https://coveralls.io/repos/sindresorhus/got/badge.svg?service=github&branch=master)](https://coveralls.io/github/sindresorhus/got?branch=master) [![Downloads](https://img.shields.io/npm/dm/got.svg?style=flat)](https://npmjs.com/got)
12
12
 
13
13
  A nicer interface to the built-in [`http`](http://nodejs.org/api/http.html) module.
14
14
 
@@ -76,7 +76,7 @@ Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_op
76
76
 
77
77
  ###### body
78
78
 
79
- Type: `string`, `Buffer`, `ReadableStream`, `Object`
79
+ Type: `string`, `buffer`, `readableStream`, `object`
80
80
 
81
81
  *This is mutually exclusive with stream mode.*
82
82
 
@@ -118,11 +118,12 @@ Milliseconds after which the request will be aborted and an error event with `ET
118
118
 
119
119
  ###### retries
120
120
 
121
- Type: `number`
121
+ Type: `number`, `function`
122
122
  Default: `5`
123
123
 
124
- Number of request retries when network errors happens.
124
+ Number of request retries when network errors happens. Delays between retries counts with function `Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0).
125
125
 
126
+ Option accepts `function` with `retry` and `error` arguments. Function must return delay in milliseconds (`0` return value cancels retry).
126
127
 
127
128
  ##### callback(error, data, response)
128
129
 
@@ -146,6 +147,13 @@ When in stream mode, you can listen for events:
146
147
 
147
148
  `request` event to get the request object of the request.
148
149
 
150
+ __Tip__: You can use `request` event to abort request:
151
+
152
+ ```js
153
+ got.stream('github.com')
154
+ .on('request', req => setTimeout(() => req.abort(), 50));
155
+ ```
156
+
149
157
  ##### .on('response', response)
150
158
 
151
159
  `response` event to get the response object of the final request.
@@ -158,7 +166,6 @@ When in stream mode, you can listen for events:
158
166
 
159
167
  `error` event emitted in case of protocol error (like `ENOTFOUND` etc.) or status error (4xx or 5xx). The second argument is the body of the server response in case of status error. The third argument is response object.
160
168
 
161
-
162
169
  #### got.get(url, [options], [callback])
163
170
  #### got.post(url, [options], [callback])
164
171
  #### got.put(url, [options], [callback])
@@ -275,6 +282,7 @@ This should only ever be done if you have Node version 0.10.x and at the top-lev
275
282
  ## Related
276
283
 
277
284
  - [gh-got](https://github.com/sindresorhus/gh-got) - Convenience wrapper for interacting with the GitHub API
285
+ - [travis-got](https://github.com/samverschueren/travis-got) - Convenience wrapper for interacting with the Travis API
278
286
 
279
287
 
280
288
  ## Created by