got 3.3.0 → 3.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 (4) hide show
  1. package/index.js +13 -8
  2. package/package.json +3 -4
  3. package/readme.md +20 -20
  4. package/license +0 -21
package/index.js CHANGED
@@ -13,7 +13,6 @@ var readAllStream = require('read-all-stream');
13
13
  var timedOut = require('timed-out');
14
14
  var prependHttp = require('prepend-http');
15
15
  var lowercaseKeys = require('lowercase-keys');
16
- var statuses = require('statuses');
17
16
  var isRedirect = require('is-redirect');
18
17
  var NestedErrorStacks = require('nested-error-stacks');
19
18
 
@@ -138,10 +137,6 @@ function got(url, opts, cb) {
138
137
  var statusCode = response.statusCode;
139
138
  var res = response;
140
139
 
141
- if (proxy) {
142
- proxy.emit('response', res);
143
- }
144
-
145
140
  // auto-redirect only for GET and HEAD methods
146
141
  if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) {
147
142
  // discard response
@@ -153,7 +148,11 @@ function got(url, opts, cb) {
153
148
  }
154
149
 
155
150
  var redirectUrl = urlLib.resolve(url, res.headers.location);
156
- var redirectOpts = objectAssign(opts, urlLib.parse(redirectUrl));
151
+ var redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl));
152
+
153
+ if (opts.agent === infinityAgent.http.globalAgent && redirectOpts.protocol === 'https:' && opts.protocol === 'http:') {
154
+ redirectOpts.agent = undefined;
155
+ }
157
156
 
158
157
  if (proxy) {
159
158
  proxy.emit('redirect', res, redirectOpts);
@@ -163,13 +162,17 @@ function got(url, opts, cb) {
163
162
  return;
164
163
  }
165
164
 
165
+ if (proxy) {
166
+ proxy.emit('response', res);
167
+ }
168
+
166
169
  if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
167
170
  res = res.pipe(zlib.createUnzip());
168
171
  }
169
172
 
170
173
  if (statusCode < 200 || statusCode > 299) {
171
174
  readAllStream(res, encoding, function (err, data) {
172
- err = new GotError(opts.method + ' ' + url + ' response code is ' + statusCode + ' (' + statuses[statusCode] + ')', err);
175
+ err = new GotError(opts.method + ' ' + url + ' response code is ' + statusCode + ' (' + http.STATUS_CODES[statusCode] + ')', err);
173
176
  err.code = statusCode;
174
177
 
175
178
  if (data && json) {
@@ -195,7 +198,9 @@ function got(url, opts, cb) {
195
198
  readAllStream(res, encoding, function (err, data) {
196
199
  if (err) {
197
200
  err = new GotError('Reading ' + url + ' response failed', err);
198
- } else if (json) {
201
+ } else if (json && statusCode !== 204) {
202
+ // only parse json if the option is enabled, and the response
203
+ // is not a 204 (empty reponse)
199
204
  try {
200
205
  data = JSON.parse(data);
201
206
  } catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "3.3.0",
3
+ "version": "3.3.1",
4
4
  "description": "Simplified HTTP/HTTPS requests",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
@@ -48,10 +48,9 @@
48
48
  "is-stream": "^1.0.0",
49
49
  "lowercase-keys": "^1.0.0",
50
50
  "nested-error-stacks": "^1.0.0",
51
- "object-assign": "^2.0.0",
51
+ "object-assign": "^3.0.0",
52
52
  "prepend-http": "^1.0.0",
53
- "read-all-stream": "^2.0.0",
54
- "statuses": "^1.2.1",
53
+ "read-all-stream": "^3.0.0",
55
54
  "timed-out": "^2.0.0"
56
55
  },
57
56
  "devDependencies": {
package/readme.md CHANGED
@@ -52,11 +52,11 @@ It's a `GET` request by default, but can be changed in `options`.
52
52
  ##### url
53
53
 
54
54
  *Required*
55
- Type: `string`, `Object`
55
+ Type: `string`, `object`
56
56
 
57
57
  The URL to request or bare [http.request options](https://nodejs.org/api/http.html#http_http_request_options_callback) object.
58
58
 
59
- Properties from `options` will override properties in parsed `url`.
59
+ Properties from `options` will override properties in the parsed `url`.
60
60
 
61
61
  ##### options
62
62
 
@@ -68,33 +68,33 @@ Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_op
68
68
 
69
69
  Type: `string`, `Buffer`, `ReadableStream`
70
70
 
71
- _This option and stream mode are mutually exclusive._
71
+ *This option and stream mode are mutually exclusive.*
72
72
 
73
- Body, that will be sent with `POST` request. If present in `options` and `options.method` is not set - `options.method` will be set to `POST`.
73
+ Body that will be sent with a `POST` request. If present in `options` and `options.method` is not set - `options.method` will be set to `POST`.
74
74
 
75
- If `content-length` or `transfer-encoding` is not set in `options.headers` and body is String or Buffer - `content-length` will be set to body length.
75
+ If `content-length` or `transfer-encoding` is not set in `options.headers` and `body` is a string or buffer, `content-length` will be set to the body length.
76
76
 
77
77
  ###### encoding
78
78
 
79
79
  Type: `string`, `null`
80
80
  Default: `'utf8'`
81
81
 
82
- Encoding to be used on `setEncoding` of the response data. If null, the body is returned as a Buffer.
82
+ Encoding to be used on `setEncoding` of the response data. If `null`, the body is returned as a Buffer.
83
83
 
84
84
  ###### json
85
85
 
86
- Type: `Boolean`
86
+ Type: `boolean`
87
87
  Default: `false`
88
88
 
89
- _This option and stream mode are mutually exclusive._
89
+ *This option and stream mode are mutually exclusive.*
90
90
 
91
- If enabled, response body will be parsed with `JSON.parse` and `accept` header will be set to `application/json` by default.
91
+ Parse response body with `JSON.parse` and set `accept` header to `application/json`.
92
92
 
93
93
  ###### query
94
94
 
95
- Type: `string`, `Object`
95
+ Type: `string`, `object`
96
96
 
97
- Query string object, that will be added to request url. This will override query string in `url`.
97
+ Query string object that will be added to the request URL. This will override the query string in `url`.
98
98
 
99
99
  ###### timeout
100
100
 
@@ -106,13 +106,13 @@ Milliseconds after which the request will be aborted and an error event with `ET
106
106
 
107
107
  [http.Agent](http://nodejs.org/api/http.html#http_class_http_agent) instance.
108
108
 
109
- If `undefined` - [`infinity-agent`](https://github.com/floatdrop/infinity-agent) will be used to backport Agent class from Node core.
109
+ If `undefined` - [`infinity-agent`](https://github.com/floatdrop/infinity-agent) will be used to backport Agent class from Node.js core.
110
110
 
111
- To use default [globalAgent](http://nodejs.org/api/http.html#http_http_globalagent) just pass `null` to this option.
111
+ To use default [globalAgent](http://nodejs.org/api/http.html#http_http_globalagent) just pass `null`.
112
112
 
113
- ##### callback(err, data, response)
113
+ ##### callback(error, data, response)
114
114
 
115
- ###### err
115
+ ###### error
116
116
 
117
117
  `Error` object with HTTP status code as `code` property.
118
118
 
@@ -128,15 +128,15 @@ When in stream mode, you can listen for events:
128
128
 
129
129
  ##### .on('response', response)
130
130
 
131
- `response` event to get the response object.
131
+ `response` event to get the response object of the final request.
132
132
 
133
133
  ##### .on('redirect', response, nextOptions)
134
134
 
135
- `redirect` event to get the response object of redirect. Second argument is options for next request to the redirect location.
135
+ `redirect` event to get the response object of a redirect. Second argument is options for the next request to the redirect location.
136
136
 
137
137
  ##### .on('error', error, body, response)
138
138
 
139
- `error` event emitted in case of protocol error (like ENOTFOUND etc.) or status error (4xx or 5xx). Second argument is body of server response in case of status error. Third argument is response object.
139
+ `error` event emitted in case of protocol error (like `ENOTFOUND` etc.) or status error (4xx or 5xx). Second argument is body of server response in case of status error. Third argument is response object.
140
140
 
141
141
  ###### response
142
142
 
@@ -187,8 +187,8 @@ got('todomvc.com', {
187
187
 
188
188
  ## Related
189
189
 
190
- * [`gh-got`](https://github.com/sindresorhus/gh-got) - Convenience wrapper for interacting with the GitHub API
191
- * [`got-promise`](https://github.com/floatdrop/got-promise) - Promise wrapper
190
+ - [gh-got](https://github.com/sindresorhus/gh-got) - Convenience wrapper for interacting with the GitHub API
191
+ - [got-promise](https://github.com/floatdrop/got-promise) - Promise wrapper
192
192
 
193
193
 
194
194
  ## Created by
package/license DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.