Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache available versions and use custom HTTP Agent #18

Merged
merged 2 commits into from
Sep 9, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions lib/plook.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function Plook( root ) {
return new Plook();
}

// Create a custom socket for when getting files
this._agent = new https.Agent();
this._agent.maxSockets = 10;

Object.defineProperty( this, "_cache", {
value: root instanceof Plook ? root._cache : LRU({
max: 500
Expand Down Expand Up @@ -139,22 +143,34 @@ Plook.prototype.findURLs = function( name, version, file ) {
version = latest ? version : version.replace( /^v/, "" );

return this.lookup( name ).then(function( slug ) {
var cached = plook._cache.get( name );
var hasVersion = function() {
return cached.versions && !!~cached.versions.indexOf( version );
};
var createURLs = function() {
return [ "v", "" ].map(function( prefix ) {
return utils.githubUrl( slug, prefix + version, file );
});
};

return new Promise(function( resolve, reject ) {
var cmd = bower.commands.info( name );
var cmd;

if ( !latest && hasVersion() ) {
return resolve( createURLs() );
}

cmd = bower.commands.info( name );
cmd.on( "end", function( pkg ) {
var urls;
cached.versions = pkg.versions;
version = latest ? pkg.versions[ 0 ] : version;

if ( !~pkg.versions.indexOf( version ) ) {
if ( !hasVersion() ) {
plook.logger.error( "version not found: %s#%s", name, version );
return reject( utils.createHttpError( 404, "Version not found" ) );
}

urls = [ "v", "" ].map(function( prefix ) {
return utils.githubUrl( slug, prefix + version, file );
});

resolve( urls );
resolve( createURLs() );
});
});
});
Expand All @@ -181,6 +197,7 @@ Plook.prototype.get = function( name, version, file, etag ) {

promise = this.findURLs( name, version, file ).call( "map", function( reqUrl ) {
var options = url.parse( reqUrl );
options.agent = plook._agent;
options.headers = {
"if-none-match": etag
};
Expand Down