diff --git a/index.js b/index.js index 2d8ad83..9738885 100644 --- a/index.js +++ b/index.js @@ -1,31 +1,47 @@ '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 = {}; + } // extract own options var encoding = opts.encoding; delete opts.encoding; - var get = function (url, opts, cb) { - if (typeof opts === 'function') { - cb = opts; - opts = {}; - } + // If no callback has been provided, returns a proxy stream to the + // response. + 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); var fn = parsedUrl.protocol === 'https:' ? https : http; var arg = assign({}, parsedUrl, opts); @@ -56,6 +72,12 @@ module.exports = function (url, opts, cb) { res = unzip; } + // If in proxy mode, simply pipe the response to the proxy. + if (proxy) { + res.pipe(proxy); + return; + } + res.once('error', cb); var chunks = []; @@ -79,4 +101,5 @@ module.exports = function (url, opts, cb) { }; get(url, opts, cb); + return proxy; }; diff --git a/readme.md b/readme.md index 83f0f43..6f47a63 100644 --- a/readme.md +++ b/readme.md @@ -21,12 +21,15 @@ $ npm install --save got ```js var got = require('got'); +// Callback mode. got('http://todomvc.com', function (err, data, res) { console.log(data); //=> ... }); -``` +// Stream mode. +got('http://todomvc.com').pipe(fs.createWriteStream('index.html')); +``` ### API diff --git a/test.js b/test.js index 545628a..f89d8c9 100644 --- a/test.js +++ b/test.js @@ -66,3 +66,25 @@ it('should return a buffer if encoding is set to null', function (done) { done(); }); }); + +it('should return a readable stream without a callback', function (done) { + var stream = got('http://google.com'); + + var data = ''; + stream.on('data', function (chunk) { + data += chunk; + }); + stream.on('end', function () { + assert.ok(/google/.test(data)); + done(); + }); +}); + +it('should proxy errors to the stream', function (done) { + var stream = got('http://sindresorhus.com/sfsadfasdfadsga'); + + stream.on('error', function (error) { + assert.strictEqual(error, 404); + done(); + }); +});