@ -5,33 +5,9 @@ var urlLib = require('url');
var zlib = require ( 'zlib' ) ;
var zlib = require ( 'zlib' ) ;
var PassThrough = require ( 'stream' ) . PassThrough ;
var PassThrough = require ( 'stream' ) . PassThrough ;
var assign = require ( 'object-assign' ) ;
var assign = require ( 'object-assign' ) ;
var read = require ( 'read-all-stream' ) ;
function read ( res , encoding , cb ) {
module . exports = function ( url , opts , cb ) {
if ( ! cb ) {
cb = encoding ;
encoding = undefined ;
}
var chunks = [ ] ;
var len = 0 ;
res . on ( 'data' , function ( chunk ) {
chunks . push ( chunk ) ;
len += chunk . length ;
} ) ;
res . once ( 'end' , function ( ) {
var data = Buffer . concat ( chunks , len ) ;
if ( encoding !== null ) {
data = data . toString ( encoding || 'utf8' ) ;
}
cb ( null , data , res ) ;
} ) ;
}
function got ( url , opts , cb ) {
if ( typeof opts === 'function' ) {
if ( typeof opts === 'function' ) {
// if `cb` has been specified but `opts` has not
// if `cb` has been specified but `opts` has not
cb = opts ;
cb = opts ;
@ -70,9 +46,12 @@ function got (url, opts, cb) {
var fn = parsedUrl . protocol === 'https:' ? https : http ;
var fn = parsedUrl . protocol === 'https:' ? https : http ;
var arg = assign ( { } , parsedUrl , opts ) ;
var arg = assign ( { } , parsedUrl , opts ) ;
fn . get ( arg , function ( res ) {
fn . get ( arg , function ( response ) {
var statusCode = response . statusCode ;
var res = response ;
// redirect
// redirect
if ( res . statusCode < 400 && res . statusCode >= 300 && res . headers . location ) {
if ( statusCode < 400 && statusCode >= 300 && res . headers . location ) {
if ( ++ redirectCount > 10 ) {
if ( ++ redirectCount > 10 ) {
cb ( new Error ( 'Redirected 10 times. Aborting.' ) , undefined , res ) ;
cb ( new Error ( 'Redirected 10 times. Aborting.' ) , undefined , res ) ;
return ;
return ;
@ -82,37 +61,32 @@ function got (url, opts, cb) {
return ;
return ;
}
}
if ( res . statusCode < 200 || res . statusCode > 299 ) {
var err = new Error ( 'Couldn\'t connect to ' + url + '.' ) ;
err . code = res . statusCode ;
cb ( err , undefined , res ) ;
return ;
}
if ( [ 'gzip' , 'deflate' ] . indexOf ( res . headers [ 'content-encoding' ] ) !== - 1 ) {
if ( [ 'gzip' , 'deflate' ] . indexOf ( res . headers [ 'content-encoding' ] ) !== - 1 ) {
var unzip = zlib . createUnzip ( ) ;
var unzip = zlib . createUnzip ( ) ;
res . pipe ( unzip ) ;
res . pipe ( unzip ) ;
res = unzip ;
res = unzip ;
}
}
if ( statusCode < 200 || statusCode > 299 ) {
read ( res , encoding , function ( error , data ) {
var err = error || new Error ( 'Couldn\'t connect to ' + url + '.' ) ;
err . code = statusCode ;
cb ( err , data , response ) ;
} ) ;
return ;
}
// pipe the response to the proxy if in proxy mode
// pipe the response to the proxy if in proxy mode
if ( proxy ) {
if ( proxy ) {
res . on ( 'error' , proxy . emit . bind ( proxy , 'error' ) ) . pipe ( proxy ) ;
res . on ( 'error' , proxy . emit . bind ( proxy , 'error' ) ) . pipe ( proxy ) ;
return ;
return ;
}
}
res . once ( 'error' , cb ) ;
read ( res , encoding , cb , response ) ;
read ( res , encoding , cb ) ;
} ) . once ( 'error' , cb ) ;
} ) . once ( 'error' , cb ) ;
} ;
} ;
get ( url , opts , cb ) ;
get ( url , opts , cb ) ;
return proxy ;
return proxy ;
}
} ;
got . read = read ;
module . exports = got ;