@@ -67,12 +67,19 @@ AWS.MetadataService = inherit({
67
67
var httpRequest = new AWS . HttpRequest ( 'http://' + this . host + path ) ;
68
68
httpRequest . method = 'GET' ;
69
69
70
- http . handleRequest ( httpRequest , this . httpOptions , function ( httpResponse ) {
71
- httpResponse . on ( 'data' , function ( chunk ) { data += chunk . toString ( ) ; } ) ;
72
- httpResponse . on ( 'end' , function ( ) { callback ( null , data ) ; } ) ;
73
- } , callback ) ;
70
+ process . nextTick ( function ( ) {
71
+ http . handleRequest ( httpRequest , this . httpOptions , function ( httpResponse ) {
72
+ httpResponse . on ( 'data' , function ( chunk ) { data += chunk . toString ( ) ; } ) ;
73
+ httpResponse . on ( 'end' , function ( ) { callback ( null , data ) ; } ) ;
74
+ } , callback ) ;
75
+ } ) ;
74
76
} ,
75
77
78
+ /**
79
+ * @api private
80
+ */
81
+ loadCredentialsCallbacks : [ ] ,
82
+
76
83
/**
77
84
* Loads a set of credentials stored in the instance metadata service
78
85
*
@@ -86,18 +93,28 @@ AWS.MetadataService = inherit({
86
93
loadCredentials : function loadCredentials ( callback ) {
87
94
var self = this ;
88
95
var basePath = '/latest/meta-data/iam/security-credentials/' ;
96
+ self . loadCredentialsCallbacks . push ( callback ) ;
97
+ if ( self . loadCredentialsCallbacks . length > 1 ) { return ; }
98
+
99
+ function callbacks ( err , creds ) {
100
+ var cb ;
101
+ while ( ( cb = self . loadCredentialsCallbacks . shift ( ) ) !== undefined ) {
102
+ cb ( err , creds ) ;
103
+ }
104
+ }
105
+
89
106
self . request ( basePath , function ( err , roleName ) {
90
- if ( err ) callback ( err ) ;
107
+ if ( err ) callbacks ( err ) ;
91
108
else {
92
109
roleName = roleName . split ( '\n' ) [ 0 ] ; // grab first (and only) role
93
110
self . request ( basePath + roleName , function ( credErr , credData ) {
94
- if ( credErr ) callback ( credErr ) ;
111
+ if ( credErr ) callbacks ( credErr ) ;
95
112
else {
96
113
try {
97
114
var credentials = JSON . parse ( credData ) ;
98
- callback ( null , credentials ) ;
115
+ callbacks ( null , credentials ) ;
99
116
} catch ( parseError ) {
100
- callback ( parseError ) ;
117
+ callbacks ( parseError ) ;
101
118
}
102
119
}
103
120
} ) ;
0 commit comments