From 281fc1387d7247cc7e9d1849b6a07987d12ae504 Mon Sep 17 00:00:00 2001 From: Andrew Peebles Date: Mon, 7 Sep 2015 16:26:36 -0700 Subject: [PATCH 1/3] 1. getCacheByHref() still incorrect, missing this case: https://api.stormpath.com/v1/applications/6fr66lFwpF8NTmFH0kITot/groups 2. Add recursive CacheHandler::get() 3. Recursive CacheHandler::put() was not storing the toplevel resource in some cases. 4. If process.env.NODE_DEBUG_SP=1, then print some debug statements when Stormpath fetches from cloud uri verses from cache. Also stores to cache are displayed. --- lib/cache/CacheHandler.js | 37 +++++++++++++++++++++++++++++-------- lib/ds/RequestExecutor.js | 1 + 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/cache/CacheHandler.js b/lib/cache/CacheHandler.js index 8d2c1df7..a57c5403 100644 --- a/lib/cache/CacheHandler.js +++ b/lib/cache/CacheHandler.js @@ -76,7 +76,7 @@ function getCacheByHref(cacheManager, href) { //href is almost never null, but it is in the case of an AuthenticationResult (at the moment), so check for existence: //see: https://github.com/stormpath/stormpath-sdk-node/issues/11 if (href) { - region = href.match(/customData/) ? 'customData' : (href.split('/').slice(-2)[0]); + region = href.split('/')[4]; } if (!region || CACHE_REGIONS.indexOf(region) === -1) { @@ -87,8 +87,23 @@ function getCacheByHref(cacheManager, href) { } CacheHandler.prototype.get = function getCachedResource(href, callback) { - var _this = this; - return getCacheByHref(_this.cacheManager, href).get(href, callback); + var _this = this; + if ( process.env.NODE_DEBUG_SP ) console.log( 'Fetching CACHE resource:', href ); + return getCacheByHref(_this.cacheManager, href).get(href, function( err, data ) { + if ( err ) return callback( err ); + if ( ! data ) return callback( err, data ); + async.eachSeries( _.keys( data ), function( key, cb ) { + var val = data[ key ]; + if ( ! ( val && val.href ) ) return cb(); + _this.get( val.href, function( err, val ) { + if ( err ) return cb( err ); + if ( val ) data[ key ] = val; + cb(); + }); + }, function( err ) { + return callback( err, data ); + }); + }); }; /* @@ -124,6 +139,11 @@ function buildCacheableResourcesFromParentObject(object){ resourcesToCache.push(parentResource); } } + + // Don't forget to cache the parent! + if ( object.href !== parentResource.href ) + resourcesToCache.push( object ); + return resourcesToCache; } @@ -137,11 +157,12 @@ CacheHandler.prototype.put = function cacheResource(href, data, _new, cb) { async.each( buildCacheableResourcesFromParentObject(data), - function(resource,next) { - getCacheByHref(_this.cacheManager, resource.href) - .put(resource.href, resource, _new, next); - }, - cb + function(resource,next) { + if ( process.env.NODE_DEBUG_SP ) console.log( 'Storing CACHE resource:', resource.href ); + getCacheByHref(_this.cacheManager, resource.href) + .put(resource.href, resource, _new, next); + }, + cb ); }; diff --git a/lib/ds/RequestExecutor.js b/lib/ds/RequestExecutor.js index 8df49e1a..f6b31a88 100644 --- a/lib/ds/RequestExecutor.js +++ b/lib/ds/RequestExecutor.js @@ -89,6 +89,7 @@ RequestExecutor.prototype.execute = function executeRequest(req, callback) { this.requestAuthenticator.authenticate(options); + if ( process.env.NODE_DEBUG_SP ) console.log( 'Fetching stormpath resource:', options.uri ); request(options, function onRequestResult(err, response, body) { if (err) { var wrapper = new Error('Unable to execute http request ' + req + ': ' + err.message); From 34e3a506a1d45b9876f3abe07557269424ab38f2 Mon Sep 17 00:00:00 2001 From: Andrew Peebles Date: Mon, 7 Sep 2015 23:04:20 -0700 Subject: [PATCH 2/3] fixes to pass lint --- lib/cache/CacheHandler.js | 19 ++++++++++--------- lib/ds/RequestExecutor.js | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/cache/CacheHandler.js b/lib/cache/CacheHandler.js index a57c5403..1437ec7a 100644 --- a/lib/cache/CacheHandler.js +++ b/lib/cache/CacheHandler.js @@ -88,16 +88,16 @@ function getCacheByHref(cacheManager, href) { CacheHandler.prototype.get = function getCachedResource(href, callback) { var _this = this; - if ( process.env.NODE_DEBUG_SP ) console.log( 'Fetching CACHE resource:', href ); + if ( process.env.NODE_DEBUG_SP ) { console.log( 'Fetching CACHE resource:', href ); } return getCacheByHref(_this.cacheManager, href).get(href, function( err, data ) { - if ( err ) return callback( err ); - if ( ! data ) return callback( err, data ); + if ( err ) { return callback( err ); } + if ( ! data ) { return callback( err, data ); } async.eachSeries( _.keys( data ), function( key, cb ) { var val = data[ key ]; - if ( ! ( val && val.href ) ) return cb(); + if ( ! ( val && val.href ) ) { return cb(); } _this.get( val.href, function( err, val ) { - if ( err ) return cb( err ); - if ( val ) data[ key ] = val; + if ( err ) { return cb( err ); } + if ( val ) { data[ key ] = val; } cb(); }); }, function( err ) { @@ -141,8 +141,9 @@ function buildCacheableResourcesFromParentObject(object){ } // Don't forget to cache the parent! - if ( object.href !== parentResource.href ) + if ( object.href !== parentResource.href ) { resourcesToCache.push( object ); + } return resourcesToCache; } @@ -158,8 +159,8 @@ CacheHandler.prototype.put = function cacheResource(href, data, _new, cb) { async.each( buildCacheableResourcesFromParentObject(data), function(resource,next) { - if ( process.env.NODE_DEBUG_SP ) console.log( 'Storing CACHE resource:', resource.href ); - getCacheByHref(_this.cacheManager, resource.href) + if ( process.env.NODE_DEBUG_SP ) { console.log( 'Storing CACHE resource:', resource.href ); } + getCacheByHref(_this.cacheManager, resource.href) .put(resource.href, resource, _new, next); }, cb diff --git a/lib/ds/RequestExecutor.js b/lib/ds/RequestExecutor.js index f6b31a88..11e2e501 100644 --- a/lib/ds/RequestExecutor.js +++ b/lib/ds/RequestExecutor.js @@ -89,7 +89,7 @@ RequestExecutor.prototype.execute = function executeRequest(req, callback) { this.requestAuthenticator.authenticate(options); - if ( process.env.NODE_DEBUG_SP ) console.log( 'Fetching stormpath resource:', options.uri ); + if ( process.env.NODE_DEBUG_SP ) { console.log( 'Fetching stormpath resource:', options.uri ); } request(options, function onRequestResult(err, response, body) { if (err) { var wrapper = new Error('Unable to execute http request ' + req + ': ' + err.message); From 99ab78ec5a4a411c3467f0c216849bdd7bec24de Mon Sep 17 00:00:00 2001 From: Andrew Peebles Date: Wed, 23 Sep 2015 09:34:22 -0700 Subject: [PATCH 3/3] debug --- lib/ds/RequestExecutor.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ds/RequestExecutor.js b/lib/ds/RequestExecutor.js index 11e2e501..8c93e535 100644 --- a/lib/ds/RequestExecutor.js +++ b/lib/ds/RequestExecutor.js @@ -89,8 +89,9 @@ RequestExecutor.prototype.execute = function executeRequest(req, callback) { this.requestAuthenticator.authenticate(options); - if ( process.env.NODE_DEBUG_SP ) { console.log( 'Fetching stormpath resource:', options.uri ); } + var _s = new Date().getTime(); request(options, function onRequestResult(err, response, body) { + if ( process.env.NODE_DEBUG_SP ) { console.log( 'Fetched stormpath resource:', options.uri, '=> latency(ms):', new Date().getTime() - _s ); } if (err) { var wrapper = new Error('Unable to execute http request ' + req + ': ' + err.message); wrapper.inner = err;