@@ -25,6 +25,28 @@ module.exports = class TokenManager {
2525 throw new Error ( `Missing iamApikey parameter.` )
2626 }
2727 }
28+
29+ // wraps a promise with a promise that supports timing out after a given amount of
30+ // milliseconds. Wrapping promise throws, when the timeout occurs.
31+ getPromiseWithExpiration ( prom , timeout ) {
32+ let timeoutHandle
33+
34+ return new Promise ( ( resolve , reject ) => {
35+ timeoutHandle = setTimeout ( ( ) => {
36+ reject ( new Error ( 'Promise timed out after ' + timeout + ' milliseconds.' ) )
37+ } , timeout )
38+
39+ prom . then ( ( result ) => {
40+ clearTimeout ( timeoutHandle )
41+ resolve ( result )
42+ } )
43+ . catch ( ( error ) => {
44+ clearTimeout ( timeoutHandle )
45+ reject ( error )
46+ } )
47+ } )
48+ }
49+
2850 /**
2951 * This function sends an access token back through a Promise. The source of the token
3052 * is determined by the following logic:
@@ -204,7 +226,9 @@ module.exports = class TokenManager {
204226 * @returns {Promise }
205227 */
206228 sendRequest ( options ) {
207- return needle ( options . method . toLowerCase ( ) ,
229+ options . response_timeout = 60000 // 1 minute max. response time allowed
230+ options . read_timeout = 30000 // 30 seconds read time limit after headers were received
231+ const needleProm = needle ( options . method . toLowerCase ( ) ,
208232 options . url ,
209233 options . body || qs . stringify ( options . form ) ,
210234 options )
@@ -218,11 +242,16 @@ module.exports = class TokenManager {
218242 if ( typeof error . error === 'object' ) {
219243 error . error . error = error . error . errorMessage
220244 }
221- return Promise . reject ( error )
245+ throw error // this will trigger the next available catch() in the Promise chain
222246 } else {
223247 // otherwise, the response body is the expected return value
224248 return resp . body
225249 }
226250 } )
251+
252+ // wrap the actual loading promise which allows the HTTP request to take 60 seconds
253+ // with a promise that times out after 90 seconds, so we don't get stuck with an
254+ // unsettled promise
255+ return this . getPromiseWithExpiration ( needleProm , 90000 )
227256 }
228257}
0 commit comments