got 5.4.1 → 5.6.0

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 +19 -7
  2. package/package.json +2 -2
  3. package/readme.md +13 -7
package/index.js CHANGED
@@ -21,6 +21,7 @@ var nodeStatusCodes = require('node-status-codes');
21
21
  var isPlainObj = require('is-plain-obj');
22
22
  var parseJson = require('parse-json');
23
23
  var isRetryAllowed = require('is-retry-allowed');
24
+ var pkg = require('./package.json');
24
25
 
25
26
  function requestAsEventEmitter(opts) {
26
27
  opts = opts || {};
@@ -35,7 +36,7 @@ function requestAsEventEmitter(opts) {
35
36
  var req = fn.request(opts, function (res) {
36
37
  var statusCode = res.statusCode;
37
38
 
38
- if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
39
+ if (isRedirect(statusCode) && opts.followRedirect && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
39
40
  res.resume();
40
41
 
41
42
  if (++redirectCount > 10) {
@@ -95,22 +96,23 @@ function asCallback(opts, cb) {
95
96
  ee.on('response', function (res) {
96
97
  readAllStream(res, opts.encoding, function (err, data) {
97
98
  var statusCode = res.statusCode;
99
+ var limitStatusCode = opts.followRedirect ? 299 : 399;
98
100
 
99
101
  if (err) {
100
102
  cb(new got.ReadError(err, opts), null, res);
101
103
  return;
102
104
  }
103
105
 
104
- if (statusCode < 200 || statusCode > 299) {
106
+ if (statusCode < 200 || statusCode > limitStatusCode) {
105
107
  err = new got.HTTPError(statusCode, opts);
106
108
  }
107
109
 
108
- if (opts.json && statusCode !== 204) {
110
+ if (opts.json && data) {
109
111
  try {
110
112
  data = parseJson(data);
111
113
  } catch (e) {
112
114
  e.fileName = urlLib.format(opts);
113
- err = new got.ParseError(e, opts);
115
+ err = new got.ParseError(e, statusCode, opts);
114
116
  }
115
117
  }
116
118
 
@@ -182,10 +184,11 @@ function asStream(opts) {
182
184
 
183
185
  ee.on('response', function (res) {
184
186
  var statusCode = res.statusCode;
187
+ var limitStatusCode = opts.followRedirect ? 299 : 399;
185
188
 
186
189
  res.pipe(output);
187
190
 
188
- if (statusCode < 200 || statusCode > 299) {
191
+ if (statusCode < 200 || statusCode > limitStatusCode) {
189
192
  proxy.emit('error', new got.HTTPError(statusCode, opts), null, res);
190
193
  return;
191
194
  }
@@ -220,7 +223,7 @@ function normalizeArguments(url, opts) {
220
223
  );
221
224
 
222
225
  opts.headers = objectAssign({
223
- 'user-agent': 'https://github.com/sindresorhus/got',
226
+ 'user-agent': pkg.name + '/' + pkg.version + ' (https://github.com/sindresorhus/got)',
224
227
  'accept-encoding': 'gzip,deflate'
225
228
  }, lowercaseKeys(opts.headers));
226
229
 
@@ -285,6 +288,10 @@ function normalizeArguments(url, opts) {
285
288
  };
286
289
  }
287
290
 
291
+ if (opts.followRedirect === undefined) {
292
+ opts.followRedirect = true;
293
+ }
294
+
288
295
  return opts;
289
296
  }
290
297
 
@@ -361,7 +368,12 @@ function stdError(error, opts) {
361
368
 
362
369
  got.RequestError = createErrorClass('RequestError', stdError);
363
370
  got.ReadError = createErrorClass('ReadError', stdError);
364
- got.ParseError = createErrorClass('ParseError', stdError);
371
+
372
+ got.ParseError = createErrorClass('ParseError', function (e, statusCode, opts) {
373
+ stdError.call(this, e, opts);
374
+ this.statusCode = statusCode;
375
+ this.statusMessage = nodeStatusCodes[this.statusCode];
376
+ });
365
377
 
366
378
  got.HTTPError = createErrorClass('HTTPError', function (statusCode, opts) {
367
379
  stdError.call(this, {}, opts);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "5.4.1",
3
+ "version": "5.6.0",
4
4
  "description": "Simplified HTTP/HTTPS requests",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
@@ -45,7 +45,7 @@
45
45
  "fetch"
46
46
  ],
47
47
  "dependencies": {
48
- "create-error-class": "^2.0.0",
48
+ "create-error-class": "^3.0.1",
49
49
  "duplexer2": "^0.1.4",
50
50
  "is-plain-obj": "^1.0.0",
51
51
  "is-redirect": "^1.0.0",
package/readme.md CHANGED
@@ -121,12 +121,19 @@ Milliseconds after which the request will be aborted and an error event with `ET
121
121
  Type: `number`, `function`
122
122
  Default: `5`
123
123
 
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
-
126
- **Note:** `ENOTFOUND` and `ENETUNREACH` error will not be retried (see full list in [`is-retry-allowed`](https://github.com/floatdrop/is-retry-allowed/blob/master/index.js#L12) module).
124
+ Number of request retries when network errors happens. Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0).
127
125
 
128
126
  Option accepts `function` with `retry` and `error` arguments. Function must return delay in milliseconds (`0` return value cancels retry).
129
127
 
128
+ **Note:** if `retries` is `number`, `ENOTFOUND` and `ENETUNREACH` error will not be retried (see full list in [`is-retry-allowed`](https://github.com/floatdrop/is-retry-allowed/blob/master/index.js#L12) module).
129
+
130
+ ###### followRedirect
131
+
132
+ Type: `boolean`
133
+ Default: `true`
134
+
135
+ Defines if redirect responses should be followed automatically.
136
+
130
137
  ##### callback(error, data, response)
131
138
 
132
139
  Function to be called when error or data are received. If omitted, a promise will be returned.
@@ -135,8 +142,6 @@ Function to be called when error or data are received. If omitted, a promise wil
135
142
 
136
143
  `Error` object with HTTP status code as `statusCode` property.
137
144
 
138
- ###### data
139
-
140
145
  The data you requested.
141
146
 
142
147
  ###### response
@@ -180,7 +185,7 @@ Sets `options.method` to the method name and makes a request.
180
185
 
181
186
  ## Errors
182
187
 
183
- Each error contains (if available) `host`, `hostname`, `method` and `path` properties to make debugging easier.
188
+ Each error contains (if available) `statusCode`, `statusMessage`, `host`, `hostname`, `method` and `path` properties to make debugging easier.
184
189
 
185
190
  In Promise mode, the `response` is attached to the error.
186
191
 
@@ -260,10 +265,11 @@ It's a good idea to set the `'user-agent'` header so the provider can more easil
260
265
 
261
266
  ```js
262
267
  var got = require('got');
268
+ var pkg = require('./package.json');
263
269
 
264
270
  got('todomvc.com', {
265
271
  headers: {
266
- 'user-agent': 'https://github.com/your-username/repo-name'
272
+ 'user-agent': 'my-module/' + pkg.version + ' (https://github.com/username/my-module)'
267
273
  }
268
274
  }, function () {});
269
275
  ```