Skip to content

Commit f54daea

Browse files
author
Burcu Dogan
committed
Merge pull request #92 from stephenplusplus/tools
add editorconfig & jshint.
2 parents 05ce404 + 74b66cf commit f54daea

23 files changed

+693
-606
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

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

17-
var GAPIToken = require('gapitoken'),
18-
async = require('async'),
19-
req = require('request'),
20-
pkg = require('../../package.json');
17+
/*jshint camelcase:false */
2118

22-
var USER_AGENT = 'gcloud-node/' + pkg.version,
23-
METADATA_TOKEN_URL = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token';
19+
'use strict';
20+
21+
var GAPIToken = require('gapitoken');
22+
var req = require('request');
23+
var pkg = require('../../package.json');
24+
var util = require('./util');
25+
26+
var METADATA_TOKEN_URL =
27+
'http://metadata/computeMetadata/v1/instance/service-accounts/default/' +
28+
'token';
29+
var USER_AGENT = 'gcloud-node/' + pkg.version;
2430

2531
/**
2632
* Token represents an access token
@@ -48,8 +54,10 @@ Token.prototype.isExpired = function() {
4854
*/
4955
function Connection(opts) {
5056
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
57+
// client email for the service account
58+
this.email = credentials.client_email;
59+
// contains the contents of a pem file
60+
this.privateKey = credentials.private_key;
5361

5462
this.scopes = opts.scopes || [];
5563
this.token = null; // existing access token, if exists
@@ -84,7 +92,6 @@ Connection.prototype.connect = function(callback) {
8492
* @param {Function} callback Callback function.
8593
*/
8694
Connection.prototype.fetchToken = function(callback) {
87-
var that = this;
8895
if (!this.email || !this.privateKey) {
8996
// We should be on GCE, try to retrieve token from
9097
// the metadata from server.
@@ -113,7 +120,7 @@ Connection.prototype.fetchToken = function(callback) {
113120
if (err) {
114121
return callback(err);
115122
}
116-
gapi.getToken(function(err, t) {
123+
gapi.getToken(function(err) {
117124
if (err) {
118125
return callback(err);
119126
}
@@ -127,41 +134,52 @@ Connection.prototype.fetchToken = function(callback) {
127134
* Makes an authorized request if the current connection token is
128135
* still valid. Tries to reconnect and makes a request otherwise.
129136
* @param {Object} Request options.
130-
* @param {Function=} opt_callback
137+
* @param {Function=} callback
131138
*/
132-
Connection.prototype.req = function(reqOpts, opt_callback) {
139+
Connection.prototype.req = function(reqOpts, callback) {
133140
var that = this;
141+
callback = callback || util.noop;
134142
this.createAuthorizedReq(reqOpts, function(err, authorizedReq) {
135143
if (err) {
136-
opt_callback && opt_callback(err);
144+
callback(err);
137145
return;
138146
}
139-
that.requester(authorizedReq, opt_callback);
147+
that.requester(authorizedReq, callback);
140148
});
141149
};
142150

143-
Connection.prototype.createAuthorizedReq = function(reqOpts, opt_callback) {
151+
Connection.prototype.createAuthorizedReq = function(reqOpts, callback) {
144152
var that = this;
145153
// Add user agent.
146154
reqOpts.headers = reqOpts.headers || {};
147-
reqOpts.headers['User-Agent'] = reqOpts.headers['User-Agent'] ?
148-
reqOpts.headers['User-Agent'] + '; ' + USER_AGENT : USER_AGENT;
155+
156+
if (reqOpts.headers['User-Agent']) {
157+
reqOpts.headers['User-Agent'] += '; ' + USER_AGENT;
158+
} else {
159+
reqOpts.headers['User-Agent'] = USER_AGENT;
160+
}
149161

150162
if (this.isConnected()) {
151-
return opt_callback && opt_callback(null, this.authorizeReq(reqOpts));
163+
return callback(null, this.authorizeReq(reqOpts));
152164
}
153165
if (this.isConnecting) {
154166
this.waitQueue = this.waitQueue || [];
155-
this.waitQueue.push({ req: reqOpts, cb: opt_callback });
167+
this.waitQueue.push({ req: reqOpts, cb: callback });
156168
return;
157169
}
158170
this.connect(function(err) {
159-
that.waitQueue.push({ req: reqOpts, cb: opt_callback });
171+
that.waitQueue.push({ req: reqOpts, cb: callback });
160172
that.waitQueue.forEach(function(v) {
173+
if (!v.cb) {
174+
return;
175+
}
176+
161177
if (err) {
162-
return v.cb && v.cb(err);
178+
v.cb(err);
179+
return;
163180
}
164-
v.cb && v.cb(null, that.authorizeReq(v.req));
181+
182+
v.cb(null, that.authorizeReq(v.req));
165183
});
166184
that.waitQueue = [];
167185
});
@@ -189,7 +207,7 @@ Connection.prototype.isConnected = function() {
189207
Connection.prototype.authorizeReq = function(reqOpts) {
190208
// TODO(jbd): Clone the request object.
191209
reqOpts.headers = reqOpts.headers || {};
192-
reqOpts.headers['Authorization'] = 'Bearer ' + this.token.accessToken;
210+
reqOpts.headers.Authorization = 'Bearer ' + this.token.accessToken;
193211
return reqOpts;
194212
};
195213

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

+40-32
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
var entityMeta = {};
17+
'use strict';
1818

19-
var namespaceRegex = kindRegex = fieldNameRegex = new RegExp(/^[A-Za-z]*$/);
19+
var entityMeta = {};
20+
var fieldNameRegex = /^[A-Za-z]*$/;
21+
var kindRegex = /^[A-Za-z]*$/;
22+
var namespaceRegex = /^[A-Za-z]*$/;
2023

2124
/**
2225
* Conversion dict for query operation to operation proto value.
@@ -46,7 +49,7 @@ function Int(val) {
4649

4750
Int.prototype.get = function() {
4851
return this.val;
49-
}
52+
};
5053

5154
module.exports.Int = Int;
5255

@@ -56,10 +59,24 @@ function Double(val) {
5659

5760
Double.prototype.get = function() {
5861
return this.val;
59-
}
62+
};
6063

6164
module.exports.Double = Double;
6265

66+
/**
67+
* Converts any entity proto to a plain object.
68+
*/
69+
var entityFromEntityProto = function(proto) {
70+
// TODO(jbd): Use registered metadata if provided.
71+
return propertiesToObject(proto.properties);
72+
};
73+
74+
/**
75+
* Exports entityFromEntityProto.
76+
* @type {Function}
77+
*/
78+
module.exports.entityFromEntityProto = entityFromEntityProto;
79+
6380
var keyFromKeyProto = function(proto) {
6481
var key = [];
6582
if (proto.partitionId.namespace) {
@@ -76,21 +93,26 @@ module.exports.keyFromKeyProto = keyFromKeyProto;
7693

7794
var keyToKeyProto = function(datasetId, key) {
7895
if (key.length < 2) {
79-
throw new Error("A key should contain at least a kind and an identifier.")
96+
throw new Error('A key should contain at least a kind and an identifier.');
8097
}
81-
var namespace = null, start = 0;
82-
if (key.length % 2 == 1) {
98+
var namespace = null;
99+
var start = 0;
100+
if (key.length % 2 === 1) {
83101
// the first item is the namespace
84102
namespace = key[0];
85103
start = 1;
86104
}
87105
var path = [];
88106
for (var i = start; i < (key.length - start); i += 2) {
89-
var p = { kind: key[i] }, val = key[i+1];
107+
var p = { kind: key[i] };
108+
var val = key[i+1];
90109
if (val) {
91110
// if not numeric, set key name.
92-
if (isNaN(val)) { p.name = val; }
93-
else { p.id = val; }
111+
if (isNaN(val)) {
112+
p.name = val;
113+
} else {
114+
p.id = val;
115+
}
94116
}
95117
path.push(p);
96118
}
@@ -111,7 +133,7 @@ var keyToKeyProto = function(datasetId, key) {
111133
module.exports.keyToKeyProto = keyToKeyProto;
112134

113135
module.exports.formatArray = function(results) {
114-
return results.map(function (result) {
136+
return results.map(function(result) {
115137
return {
116138
key: keyFromKeyProto(result.entity.key),
117139
data: entityFromEntityProto(result.entity)
@@ -159,7 +181,7 @@ function propertyToValue(property) {
159181
}
160182
return l;
161183
}
162-
};
184+
}
163185

164186
function propertiesToObject(properties) {
165187
properties = properties || [];
@@ -168,21 +190,7 @@ function propertiesToObject(properties) {
168190
obj[name] = propertyToValue(properties[name]);
169191
}
170192
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;
193+
}
186194

187195
/**
188196
* Convert any object to property value.
@@ -237,7 +245,7 @@ function valueToProperty(v) {
237245
p.entityValue = { properties: properties };
238246
return p;
239247
}
240-
throw new Error('Unsupported field value, ' + v + ', is provided.')
248+
throw new Error('Unsupported field value, ' + v + ', is provided.');
241249
}
242250

243251
/**
@@ -385,11 +393,11 @@ function validateField(name, field) {
385393
if (!field.kind) {
386394
throw new Error('Provide a kind for field ' + name);
387395
}
388-
if (typeof field.kind != 'object' &&
389-
!primitiveKinds.indexOf(field.kind) < 0) {
396+
if (typeof field.kind !== 'object' &&
397+
primitiveKinds.indexOf(field.kind) === -1) {
390398
throw new Error('Unknown kind for field ' + name);
391399
}
392-
if (typeof field.kind == 'object') {
400+
if (typeof field.kind === 'object') {
393401
Object.keys(field.key).forEach(function(key) {
394402
validateField(key, field.key[key]);
395403
});
@@ -406,4 +414,4 @@ module.exports.registerKind = registerKind;
406414
* Exports getKind.
407415
* @type {function}
408416
*/
409-
module.exports.getKind = getKind
417+
module.exports.getKind = getKind;

0 commit comments

Comments
 (0)