Skip to content

Commit 547e73c

Browse files
add editorconfig & jshint.
1 parent 05ce404 commit 547e73c

22 files changed

+485
-390
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.jshintrc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"bitwise": true,
3+
"camelcase": true,
4+
"curly": true,
5+
"eqeqeq": true,
6+
"esnext": true,
7+
"freeze": true,
8+
"immed": true,
9+
"indent": 2,
10+
"latedef": "nofunc",
11+
"maxlen": 80,
12+
"newcap": true,
13+
"node": true,
14+
"noarg": true,
15+
"quotmark": "single",
16+
"strict": true,
17+
"trailing": true,
18+
"undef": true,
19+
"unused": true
20+
}

CONTRIBUTORS

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313

1414
Burcu Dogan <[email protected]>
1515
Johan Euphrosine <[email protected]>
16-
Silvano Luciani <[email protected]>
16+
Silvano Luciani <[email protected]>
17+
Stephen Sawchuk <[email protected]>

lib/common/connection.js

+32-19
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17+
/*jshint camelcase:false */
18+
19+
'use strict';
20+
1721
var 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

2226
var 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
*/
4954
function 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
*/
8693
Connection.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() {
189202
Connection.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

lib/common/util.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,30 @@
1414
* limitations under the License.
1515
*/
1616

17+
/*jshint strict:false, noarg:false */
18+
1719
var util = require('util');
1820

19-
module.exports.extend = function(from, to) {
20-
if (from == null || typeof from != "object") {
21+
function extend(from, to) {
22+
if (from === null || typeof from !== 'object') {
2123
return from;
2224
}
23-
if (from.constructor != Object && from.constructor != Array) {
25+
if (from.constructor !== Object && from.constructor !== Array) {
2426
return from;
2527
}
26-
if (from.constructor == Date || from.constructor == Function ||
27-
from.constructor == String || from.constructor == Number ||
28-
from.constructor == Boolean) {
28+
if (from.constructor === Date || from.constructor === Function ||
29+
from.constructor === String || from.constructor === Number ||
30+
from.constructor === Boolean) {
2931
return new from.constructor(from);
3032
}
3133
to = to || new from.constructor();
3234
for (var name in from) {
3335
to[name] = to[name] ? extend(from[name], null) : to[name];
3436
}
3537
return to;
36-
};
38+
}
39+
40+
module.exports.extend = extend;
3741

3842
module.exports.arrayize = function(input) {
3943
if (!Array.isArray(input)) {
@@ -59,12 +63,12 @@ function ApiError (errorBody) {
5963
this.errors = errorBody.errors;
6064
this.code = errorBody.code;
6165
this.message = errorBody.message;
62-
};
66+
}
6367

6468
util.inherits(ApiError, Error);
6569

66-
module.exports.handleResp = function(err, resp, body, opt_callback) {
67-
var callback = opt_callback || noop;
70+
module.exports.handleResp = function(err, resp, body, callback) {
71+
callback = callback || noop;
6872
if (err) {
6973
callback(err);
7074
return;

lib/datastore/entity.js

+30-26
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17+
'use strict';
18+
1719
var entityMeta = {};
1820

19-
var namespaceRegex = kindRegex = fieldNameRegex = new RegExp(/^[A-Za-z]*$/);
21+
var namespaceRegex = /^[A-Za-z]*$/,
22+
kindRegex = /^[A-Za-z]*$/,
23+
fieldNameRegex = /^[A-Za-z]*$/;
2024

2125
/**
2226
* Conversion dict for query operation to operation proto value.
@@ -46,7 +50,7 @@ function Int(val) {
4650

4751
Int.prototype.get = function() {
4852
return this.val;
49-
}
53+
};
5054

5155
module.exports.Int = Int;
5256

@@ -56,10 +60,24 @@ function Double(val) {
5660

5761
Double.prototype.get = function() {
5862
return this.val;
59-
}
63+
};
6064

6165
module.exports.Double = Double;
6266

67+
/**
68+
* Converts any entity proto to a plain object.
69+
*/
70+
var entityFromEntityProto = function(proto) {
71+
// TODO(jbd): Use registered metadata if provided.
72+
return propertiesToObject(proto.properties);
73+
};
74+
75+
/**
76+
* Exports entityFromEntityProto.
77+
* @type {Function}
78+
*/
79+
module.exports.entityFromEntityProto = entityFromEntityProto;
80+
6381
var keyFromKeyProto = function(proto) {
6482
var key = [];
6583
if (proto.partitionId.namespace) {
@@ -76,10 +94,10 @@ module.exports.keyFromKeyProto = keyFromKeyProto;
7694

7795
var keyToKeyProto = function(datasetId, key) {
7896
if (key.length < 2) {
79-
throw new Error("A key should contain at least a kind and an identifier.")
97+
throw new Error('A key should contain at least a kind and an identifier.');
8098
}
8199
var namespace = null, start = 0;
82-
if (key.length % 2 == 1) {
100+
if (key.length % 2 === 1) {
83101
// the first item is the namespace
84102
namespace = key[0];
85103
start = 1;
@@ -159,7 +177,7 @@ function propertyToValue(property) {
159177
}
160178
return l;
161179
}
162-
};
180+
}
163181

164182
function propertiesToObject(properties) {
165183
properties = properties || [];
@@ -168,21 +186,7 @@ function propertiesToObject(properties) {
168186
obj[name] = propertyToValue(properties[name]);
169187
}
170188
return obj;
171-
};
172-
173-
/**
174-
* Converts any entity proto to a plain object.
175-
*/
176-
var entityFromEntityProto = function(proto) {
177-
// TODO(jbd): Use registered metadata if provided.
178-
return propertiesToObject(proto.properties);
179-
};
180-
181-
/**
182-
* Exports entityFromEntityProto.
183-
* @type {Function}
184-
*/
185-
module.exports.entityFromEntityProto = entityFromEntityProto;
189+
}
186190

187191
/**
188192
* Convert any object to property value.
@@ -237,7 +241,7 @@ function valueToProperty(v) {
237241
p.entityValue = { properties: properties };
238242
return p;
239243
}
240-
throw new Error('Unsupported field value, ' + v + ', is provided.')
244+
throw new Error('Unsupported field value, ' + v + ', is provided.');
241245
}
242246

243247
/**
@@ -385,11 +389,11 @@ function validateField(name, field) {
385389
if (!field.kind) {
386390
throw new Error('Provide a kind for field ' + name);
387391
}
388-
if (typeof field.kind != 'object' &&
389-
!primitiveKinds.indexOf(field.kind) < 0) {
392+
if (typeof field.kind !== 'object' &&
393+
primitiveKinds.indexOf(field.kind) === -1) {
390394
throw new Error('Unknown kind for field ' + name);
391395
}
392-
if (typeof field.kind == 'object') {
396+
if (typeof field.kind === 'object') {
393397
Object.keys(field.key).forEach(function(key) {
394398
validateField(key, field.key[key]);
395399
});
@@ -406,4 +410,4 @@ module.exports.registerKind = registerKind;
406410
* Exports getKind.
407411
* @type {function}
408412
*/
409-
module.exports.getKind = getKind
413+
module.exports.getKind = getKind;

0 commit comments

Comments
 (0)