predix-uaa-client
Advanced tools
Comparing version 1.0.0 to 1.1.0
56
index.js
@@ -26,2 +26,14 @@ 'use strict' | ||
// This will hold the promises of pending requests. This avoids requesting | ||
// multiple redundant tokens for a single user or client. | ||
let pending_requests = {}; | ||
// Helper method to create a key that can be used to represent a unique request | ||
const requestKey = (uaaUri, clientId, clientSecret, refreshToken) => { | ||
const crypto = require('crypto'); | ||
const hash = crypto.createHash('sha256'); | ||
hash.update(`${uaaUri}__${clientId}__${clientSecret}${refreshToken ? '__' + refreshToken : ''}`); | ||
return hash.digest('hex'); | ||
}; | ||
/** | ||
@@ -57,5 +69,29 @@ * This function provides 2 modes of operation. | ||
// Pending request key | ||
const request_key = requestKey(uaaUri, clientId, clientSecret, refreshToken); | ||
// Check if an existing request is in progress for this client/user | ||
let makeRequest = false; | ||
if(!Array.isArray(pending_requests[request_key])) { | ||
pending_requests[request_key] = new Array(); | ||
makeRequest = true; | ||
} | ||
// Add a new promise for this request to the array | ||
const getProm = () => { | ||
let resolve = null; | ||
let reject = null; | ||
let p = new Promise((rs, rj) => { | ||
resolve = rs; | ||
reject = rj; | ||
}); | ||
return { prom: p, resolve: resolve, reject: reject }; | ||
}; | ||
let resolvable = getProm(); | ||
pending_requests[request_key].push(resolvable); | ||
// URL for the token is <UAA_Server>/oauth/token | ||
return new Promise((resolve, reject) => { | ||
// Is this the 'thread' that needs to make the real call? | ||
if(makeRequest) { | ||
let alreadyResolved = false; | ||
@@ -81,4 +117,5 @@ let cacheable = false; | ||
if(access_token && access_token.expire_time > now) { | ||
// Already have it. | ||
resolve(access_token); | ||
// Resolve all waiting promises. | ||
pending_requests[request_key].forEach(p => p.resolve(access_token)); | ||
delete pending_requests[request_key]; | ||
alreadyResolved = true; | ||
@@ -117,3 +154,5 @@ } | ||
if(!alreadyResolved) { | ||
reject(err); | ||
// Reject all waiting promises. | ||
pending_requests[request_key].forEach(p => p.reject(err)); | ||
delete pending_requests[request_key]; | ||
} | ||
@@ -135,3 +174,5 @@ } else { | ||
if(!alreadyResolved) { | ||
resolve(access_token); | ||
// Resolve all waiting promises. | ||
pending_requests[request_key].forEach(p => p.resolve(access_token)); | ||
delete pending_requests[request_key]; | ||
} | ||
@@ -146,3 +187,4 @@ | ||
} | ||
}); | ||
}; | ||
return resolvable.prom; | ||
} | ||
@@ -149,0 +191,0 @@ |
{ | ||
"name": "predix-uaa-client", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Node module to get a token from UAA using client credentials or refresh tokens", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha -R spec test/uaa.spec", | ||
"coverage": "istanbul cover _mocha -- -R spec test/uaa.spec" | ||
"test": "mocha -R spec test/uaa.spec.js", | ||
"coverage": "istanbul cover _mocha -- -R spec test/uaa.spec.js" | ||
}, | ||
@@ -10,0 +10,0 @@ "repository": { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
226794
8
449
1