got 1.0.1 → 1.2.2

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 +55 -22
  2. package/package.json +2 -2
  3. package/readme.md +16 -1
package/index.js CHANGED
@@ -1,34 +1,51 @@
1
1
  'use strict';
2
- var urlLib = require('url');
3
2
  var http = require('http');
4
3
  var https = require('https');
4
+ var urlLib = require('url');
5
5
  var zlib = require('zlib');
6
+ var PassThrough = require('stream').PassThrough;
6
7
  var assign = require('object-assign');
7
8
 
8
9
  module.exports = function (url, opts, cb) {
10
+ if (typeof opts === 'function') {
11
+ // if `cb` has been specified but `opts` has not
12
+ cb = opts;
13
+ opts = {};
14
+ } else if (!opts) {
15
+ // opts has not been specified
16
+ opts = {};
17
+ }
18
+
19
+ // extract own options
20
+ var encoding = opts.encoding;
21
+ delete opts.encoding;
22
+
23
+ // returns a proxy stream to the response
24
+ // if no callback has been provided
25
+ var proxy;
26
+ if (!cb) {
27
+ proxy = new PassThrough();
28
+
29
+ // forward errors on the stream
30
+ cb = function (err) {
31
+ proxy.emit('error', err);
32
+ };
33
+ }
34
+
35
+ // merge additional headers
36
+ opts.headers = assign({
37
+ 'user-agent': 'https://github.com/sindresorhus/got',
38
+ 'accept-encoding': 'gzip,deflate'
39
+ }, opts.headers || {});
40
+
9
41
  var redirectCount = 0;
10
42
 
11
43
  var get = function (url, opts, cb) {
12
- if (typeof opts === 'function') {
13
- cb = opts;
14
- opts = {};
15
- }
16
-
17
- cb = cb || function () {};
18
- opts = opts || {};
19
-
20
- opts.headers = assign({
21
- 'user-agent': 'https://github.com/sindresorhus/got',
22
- 'accept-encoding': 'gzip,deflate'
23
- }, opts.headers || {});
24
-
25
44
  var parsedUrl = urlLib.parse(url);
26
45
  var fn = parsedUrl.protocol === 'https:' ? https : http;
27
46
  var arg = assign({}, parsedUrl, opts);
28
47
 
29
48
  fn.get(arg, function (res) {
30
- var ret = '';
31
-
32
49
  // redirect
33
50
  if (res.statusCode < 400 && res.statusCode >= 300 && res.headers.location) {
34
51
  res.destroy();
@@ -54,19 +71,35 @@ module.exports = function (url, opts, cb) {
54
71
  res = unzip;
55
72
  }
56
73
 
57
- res.setEncoding('utf8');
58
-
59
- res.on('data', function (data) {
60
- ret += data;
61
- });
74
+ // pipe the response to the proxy if in proxy mode
75
+ if (proxy) {
76
+ res.on('error', proxy.emit.bind(proxy, 'error')).pipe(proxy);
77
+ return;
78
+ }
62
79
 
63
80
  res.once('error', cb);
64
81
 
82
+ var chunks = [];
83
+ var len = 0;
84
+
85
+ res.on('data', function (chunk) {
86
+ chunks.push(chunk);
87
+ len += chunk.length;
88
+ });
89
+
65
90
  res.once('end', function () {
66
- cb(null, ret, res);
91
+ var data = Buffer.concat(chunks, len);
92
+
93
+ if (encoding !== null) {
94
+ data = data.toString(encoding || 'utf8');
95
+ }
96
+
97
+ cb(null, data, res);
67
98
  });
68
99
  }).once('error', cb);
69
100
  };
70
101
 
71
102
  get(url, opts, cb);
103
+
104
+ return proxy;
72
105
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "got",
3
- "version": "1.0.1",
3
+ "version": "1.2.2",
4
4
  "description": "Simplified HTTP/HTTPS requests",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/got",
@@ -31,7 +31,7 @@
31
31
  "simple"
32
32
  ],
33
33
  "dependencies": {
34
- "object-assign": "^0.3.0"
34
+ "object-assign": "^1.0.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "mocha": "*"
package/readme.md CHANGED
@@ -21,12 +21,15 @@ $ npm install --save got
21
21
  ```js
22
22
  var got = require('got');
23
23
 
24
+ // Callback mode.
24
25
  got('http://todomvc.com', function (err, data, res) {
25
26
  console.log(data);
26
27
  //=> <!doctype html> ...
27
28
  });
28
- ```
29
29
 
30
+ // Stream mode.
31
+ got('http://todomvc.com').pipe(fs.createWriteStream('index.html'));
32
+ ```
30
33
 
31
34
  ### API
32
35
 
@@ -47,6 +50,13 @@ Type: `object`
47
50
 
48
51
  Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_options_callback) options.
49
52
 
53
+ ##### options.encoding
54
+
55
+ Type: `string`, `null`
56
+ Default: `'utf8'`
57
+
58
+ Encoding to be used on `setEncoding` of the response data. If null, the body is returned as a Buffer.
59
+
50
60
  ##### callback(err, data, response)
51
61
 
52
62
  ###### data
@@ -58,6 +68,11 @@ The data you requested.
58
68
  The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage).
59
69
 
60
70
 
71
+ ## Related
72
+
73
+ See [sent](https://github.com/floatdrop/sent) if you need to upload something.
74
+
75
+
61
76
  ## License
62
77
 
63
78
  MIT © [Sindre Sorhus](http://sindresorhus.com)