1414 * limitations under the License.
1515 */
1616
17+ /*jshint camelcase:false */
18+
19+ 'use strict' ;
20+
1721var GAPIToken = require ( 'gapitoken' ) ,
18- async = require ( 'async ' ) ,
19- req = require ( 'request ' ) ,
20- pkg = require ( '../../package.json ' ) ;
22+ req = require ( 'request ' ) ,
23+ pkg = require ( '../../package.json ' ) ,
24+ util = require ( './util ' ) ;
2125
2226var USER_AGENT = 'gcloud-node/' + pkg . version ,
23- METADATA_TOKEN_URL = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token' ;
27+ METADATA_TOKEN_URL = 'http://metadata/computeMetadata/v1/instance/' +
28+ 'service-accounts/default/token' ;
2429
2530/**
2631 * Token represents an access token
@@ -48,8 +53,10 @@ Token.prototype.isExpired = function() {
4853 */
4954function Connection ( opts ) {
5055 var credentials = opts . keyFilename && require ( opts . keyFilename ) || { } ;
51- this . email = credentials [ 'client_email' ] ; // client email for the service account
52- this . privateKey = credentials [ 'private_key' ] ; // contains the contents of a pem file
56+ // client email for the service account
57+ this . email = credentials . client_email ;
58+ // contains the contents of a pem file
59+ this . privateKey = credentials . private_key ;
5360
5461 this . scopes = opts . scopes || [ ] ;
5562 this . token = null ; // existing access token, if exists
@@ -84,7 +91,6 @@ Connection.prototype.connect = function(callback) {
8491 * @param {Function } callback Callback function.
8592 */
8693Connection . prototype . fetchToken = function ( callback ) {
87- var that = this ;
8894 if ( ! this . email || ! this . privateKey ) {
8995 // We should be on GCE, try to retrieve token from
9096 // the metadata from server.
@@ -113,7 +119,7 @@ Connection.prototype.fetchToken = function(callback) {
113119 if ( err ) {
114120 return callback ( err ) ;
115121 }
116- gapi . getToken ( function ( err , t ) {
122+ gapi . getToken ( function ( err ) {
117123 if ( err ) {
118124 return callback ( err ) ;
119125 }
@@ -127,41 +133,48 @@ Connection.prototype.fetchToken = function(callback) {
127133 * Makes an authorized request if the current connection token is
128134 * still valid. Tries to reconnect and makes a request otherwise.
129135 * @param {Object } Request options.
130- * @param {Function= } opt_callback
136+ * @param {Function= } callback
131137 */
132- Connection . prototype . req = function ( reqOpts , opt_callback ) {
138+ Connection . prototype . req = function ( reqOpts , callback ) {
133139 var that = this ;
140+ callback = callback || util . noop ;
134141 this . createAuthorizedReq ( reqOpts , function ( err , authorizedReq ) {
135142 if ( err ) {
136- opt_callback && opt_callback ( err ) ;
143+ callback ( err ) ;
137144 return ;
138145 }
139- that . requester ( authorizedReq , opt_callback ) ;
146+ that . requester ( authorizedReq , callback ) ;
140147 } ) ;
141148} ;
142149
143- Connection . prototype . createAuthorizedReq = function ( reqOpts , opt_callback ) {
150+ Connection . prototype . createAuthorizedReq = function ( reqOpts , callback ) {
144151 var that = this ;
145152 // Add user agent.
146153 reqOpts . headers = reqOpts . headers || { } ;
147154 reqOpts . headers [ 'User-Agent' ] = reqOpts . headers [ 'User-Agent' ] ?
148155 reqOpts . headers [ 'User-Agent' ] + '; ' + USER_AGENT : USER_AGENT ;
149156
150157 if ( this . isConnected ( ) ) {
151- return opt_callback && opt_callback ( null , this . authorizeReq ( reqOpts ) ) ;
158+ return callback ( null , this . authorizeReq ( reqOpts ) ) ;
152159 }
153160 if ( this . isConnecting ) {
154161 this . waitQueue = this . waitQueue || [ ] ;
155- this . waitQueue . push ( { req : reqOpts , cb : opt_callback } ) ;
162+ this . waitQueue . push ( { req : reqOpts , cb : callback } ) ;
156163 return ;
157164 }
158165 this . connect ( function ( err ) {
159- that . waitQueue . push ( { req : reqOpts , cb : opt_callback } ) ;
166+ that . waitQueue . push ( { req : reqOpts , cb : callback } ) ;
160167 that . waitQueue . forEach ( function ( v ) {
168+ if ( ! v . cb ) {
169+ return ;
170+ }
171+
161172 if ( err ) {
162- return v . cb && v . cb ( err ) ;
173+ v . cb ( err ) ;
174+ return ;
163175 }
164- v . cb && v . cb ( null , that . authorizeReq ( v . req ) ) ;
176+
177+ v . cb ( null , that . authorizeReq ( v . req ) ) ;
165178 } ) ;
166179 that . waitQueue = [ ] ;
167180 } ) ;
@@ -189,7 +202,7 @@ Connection.prototype.isConnected = function() {
189202Connection . prototype . authorizeReq = function ( reqOpts ) {
190203 // TODO(jbd): Clone the request object.
191204 reqOpts . headers = reqOpts . headers || { } ;
192- reqOpts . headers [ ' Authorization' ] = 'Bearer ' + this . token . accessToken ;
205+ reqOpts . headers . Authorization = 'Bearer ' + this . token . accessToken ;
193206 return reqOpts ;
194207} ;
195208
0 commit comments