Skip to content

Commit

Permalink
use callback API and handle rate limit error (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
timaschew authored and yasserf committed Jul 27, 2016
1 parent 8438cf8 commit 35bd402
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 60 deletions.
49 changes: 21 additions & 28 deletions bin/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,22 @@ const downloadRelease = function( releases, type, name, version, outputDir, call
* @return {void}
*/
const downloadArchive = function( urlPath, outStream, callback ) {
try {
needle.get( 'https://github.com' + urlPath, {
follow_max: 5,
headers: {'User-Agent': 'nodejs-client'}
} )
.on( 'readable', function() {
if ( process.env.VERBOSE ) {
process.stdout.write( '.'.grey );
}
} )
.on( 'end', function() {
if ( process.env.VERBOSE ) {
process.stdout.clearLine();
process.stdout.cursorTo( 0 );
process.stdout.write( 'Download complete' + '\n' );
}
return callback();
} )
.pipe( outStream )
.on( 'error', function( err ) {
// TODO: if the outStream throws an error callback will be
// called twice, need to figure out, how to solve, maybe with 'pump'
console.error( 'Error while saving the archive', err );
} );
} catch ( err ) {
return callback( err );
}
needle.get( 'https://github.com' + urlPath, {
follow_max: 5,
headers: {'User-Agent': 'nodejs-client'}
}, (error, response) => {
if ( error ) {
return callback( error );
}
outStream.write(response.body)
outStream.end()
if ( process.env.VERBOSE ) {
process.stdout.clearLine();
process.stdout.cursorTo( 0 );
process.stdout.write( 'Download complete' + '\n' );
}
return callback()
} )
};

/**
Expand All @@ -136,14 +125,18 @@ const fetchReleases = function( type, name, callback ) {
console.log( 'searching for ' + repo );
}
needle.get( 'https://api.github.com' + urlPath, {
headers: {'User-Agent': 'nodejs-client'}
headers: {'User-Agent': 'nodejs-client'},
}, function( error, response ) {
if ( error ) {
return callback( error );
}
if ( response.statusCode === 404 ) {
return callback( new Error( 'Not found, see available connectors on https://deepstream.io/download' ) );
}
if ( response.statusCode == 403 ) {
// API rate limit
return callback( new Error(response.body.message))
}
callback( null, response.body );
} );
};
Expand Down
41 changes: 9 additions & 32 deletions test/bin/installerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ describe( 'installer', function() {
} );
},
// request handler for all other requests, not starting with 'https://api'
dummyReadStream( {error: new Error( 'dummy-stream-read-error' )} )
function( urlPath, options, callback ) {
return callback( new Error('dummy-stream-read-error'))
}
);
spyOn( mkdirp, 'sync' );
needleMock['@noCallThru'] = true;
Expand All @@ -186,35 +188,6 @@ describe( 'installer', function() {
} );
} );

// error handling doesn't work yet, see downloadArchive()
xit( 'error while saving the archive', function( done ) {
const fsMock = {
createWriteStream: dummyWritedStream( {error: new Error( 'dummy-stream-write-error' )} )
};
const needleMock = new Needle(
// request handler for fetching all releases
function( urlPath, options, callback ) {
return callback( null, {
statusCode: 200,
body: [{ tag_name: '1.2.3', assets: assets }]
} );
},
// request handler for all other requests, not starting with 'https://api'
dummyReadStream()
);
spyOn( mkdirp, 'sync' );
needleMock['@noCallThru'] = true;
var installer = proxyquire( '../../bin/installer', {
needle: needleMock,
fs: fsMock
} );

installer( {type: 'foo', name: 'bar', version: '1.2.3', verbose: true}, function( error ) {
expect( error.toString() ).toContain( 'dummy-stream-write-error' );
done();
} );
} );

it( 'error while extracting the archive', function( done ) {
const fsMock = {
createWriteStream: dummyWritedStream()
Expand All @@ -231,7 +204,9 @@ describe( 'installer', function() {
} );
},
// request handler for all other requests, not starting with 'https://api'
dummyReadStream()
function( urlPath, options, callback ) {
return callback( null, {body: ''})
}
);
spyOn( mkdirp, 'sync' );
needleMock['@noCallThru'] = true;
Expand Down Expand Up @@ -263,7 +238,9 @@ describe( 'installer', function() {
} );
},
// request handler for all other requests, not starting with 'https://api'
dummyReadStream()
function( urlPath, options, callback ) {
return callback( null, {body: ''})
}
);
const zipConstructor = jasmine.createSpy( 'callback' );
const zipExtractor = jasmine.createSpy( 'callback' );
Expand Down

0 comments on commit 35bd402

Please sign in to comment.