Browse Source

Stream mode (fix #1).

http2
Julien Fontanet 10 years ago
parent
commit
92d6abf9c6
  1. 49
      index.js
  2. 5
      readme.md
  3. 22
      test.js

49
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;
};

5
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);
//=> <!doctype html> ...
});
```
// Stream mode.
got('http://todomvc.com').pipe(fs.createWriteStream('index.html'));
```
### API

22
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();
});
});

Loading…
Cancel
Save