Comparing version 1.1.0 to 1.2.0
49
index.js
'use strict'; | ||
var urlLib = require('url'); | ||
var http = require('http'); | ||
var https = require('https'); | ||
var urlLib = require('url'); | ||
var zlib = require('zlib'); | ||
var PassThrough = require('stream').PassThrough; | ||
var assign = require('object-assign'); | ||
module.exports = function (url, opts, cb) { | ||
var redirectCount = 0; | ||
if (typeof opts === 'function') { | ||
// if `cb` has been specified but `opts` has not | ||
cb = opts; | ||
opts = {}; | ||
} else if (!opts) { | ||
// opts has not been specified | ||
opts = {}; | ||
} | ||
@@ -15,16 +23,23 @@ // extract own options | ||
var get = function (url, opts, cb) { | ||
if (typeof opts === 'function') { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
// returns a proxy stream to the response | ||
// if no callback has been provided | ||
var proxy; | ||
if (!cb) { | ||
proxy = new PassThrough(); | ||
cb = cb || function () {}; | ||
opts = opts || {}; | ||
// forward errors on the stream | ||
cb = function (err) { | ||
proxy.emit('error', err); | ||
}; | ||
} | ||
opts.headers = assign({ | ||
'user-agent': 'https://github.com/sindresorhus/got', | ||
'accept-encoding': 'gzip,deflate' | ||
}, opts.headers || {}); | ||
// merge additional headers | ||
opts.headers = assign({ | ||
'user-agent': 'https://github.com/sindresorhus/got', | ||
'accept-encoding': 'gzip,deflate' | ||
}, opts.headers || {}); | ||
var redirectCount = 0; | ||
var get = function (url, opts, cb) { | ||
var parsedUrl = urlLib.parse(url); | ||
@@ -60,2 +75,8 @@ var fn = parsedUrl.protocol === 'https:' ? https : http; | ||
// pipe the response to the proxy if in proxy mode | ||
if (proxy) { | ||
res.pipe(proxy); | ||
return; | ||
} | ||
res.once('error', cb); | ||
@@ -84,2 +105,4 @@ | ||
get(url, opts, cb); | ||
return proxy; | ||
}; |
{ | ||
"name": "got", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Simplified HTTP/HTTPS requests", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -24,2 +24,3 @@ # got [![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) | ||
// Callback mode. | ||
got('http://todomvc.com', function (err, data, res) { | ||
@@ -29,5 +30,7 @@ console.log(data); | ||
}); | ||
// Stream mode. | ||
got('http://todomvc.com').pipe(fs.createWriteStream('index.html')); | ||
``` | ||
### API | ||
@@ -70,4 +73,9 @@ | ||
## Related | ||
See [sent](https://github.com/floatdrop/sent) if you need to upload something. | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4400
84
79