Skip to content

Commit

Permalink
add editorconfig & jshint.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Aug 5, 2014
1 parent 05ce404 commit 547e73c
Show file tree
Hide file tree
Showing 22 changed files with 485 additions and 390 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
20 changes: 20 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"esnext": true,
"freeze": true,
"immed": true,
"indent": 2,
"latedef": "nofunc",
"maxlen": 80,
"newcap": true,
"node": true,
"noarg": true,
"quotmark": "single",
"strict": true,
"trailing": true,
"undef": true,
"unused": true
}
3 changes: 2 additions & 1 deletion CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

Burcu Dogan <jbd@google.com>
Johan Euphrosine <proppy@google.com>
Silvano Luciani <silvano@google.com>
Silvano Luciani <silvano@google.com>
Stephen Sawchuk <sawchuk@gmail.com>
51 changes: 32 additions & 19 deletions lib/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
* limitations under the License.
*/

/*jshint camelcase:false */

'use strict';

var GAPIToken = require('gapitoken'),
async = require('async'),
req = require('request'),
pkg = require('../../package.json');
req = require('request'),
pkg = require('../../package.json'),
util = require('./util');

var USER_AGENT = 'gcloud-node/' + pkg.version,
METADATA_TOKEN_URL = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/token';
METADATA_TOKEN_URL = 'http://metadata/computeMetadata/v1/instance/' +
'service-accounts/default/token';

/**
* Token represents an access token
Expand Down Expand Up @@ -48,8 +53,10 @@ Token.prototype.isExpired = function() {
*/
function Connection(opts) {
var credentials = opts.keyFilename && require(opts.keyFilename) || {};
this.email = credentials['client_email']; // client email for the service account
this.privateKey = credentials['private_key']; // contains the contents of a pem file
// client email for the service account
this.email = credentials.client_email;
// contains the contents of a pem file
this.privateKey = credentials.private_key;

this.scopes = opts.scopes || [];
this.token = null; // existing access token, if exists
Expand Down Expand Up @@ -84,7 +91,6 @@ Connection.prototype.connect = function(callback) {
* @param {Function} callback Callback function.
*/
Connection.prototype.fetchToken = function(callback) {
var that = this;
if (!this.email || !this.privateKey) {
// We should be on GCE, try to retrieve token from
// the metadata from server.
Expand Down Expand Up @@ -113,7 +119,7 @@ Connection.prototype.fetchToken = function(callback) {
if (err) {
return callback(err);
}
gapi.getToken(function(err, t) {
gapi.getToken(function(err) {
if (err) {
return callback(err);
}
Expand All @@ -127,41 +133,48 @@ Connection.prototype.fetchToken = function(callback) {
* Makes an authorized request if the current connection token is
* still valid. Tries to reconnect and makes a request otherwise.
* @param {Object} Request options.
* @param {Function=} opt_callback
* @param {Function=} callback
*/
Connection.prototype.req = function(reqOpts, opt_callback) {
Connection.prototype.req = function(reqOpts, callback) {
var that = this;
callback = callback || util.noop;
this.createAuthorizedReq(reqOpts, function(err, authorizedReq) {
if (err) {
opt_callback && opt_callback(err);
callback(err);
return;
}
that.requester(authorizedReq, opt_callback);
that.requester(authorizedReq, callback);
});
};

Connection.prototype.createAuthorizedReq = function(reqOpts, opt_callback) {
Connection.prototype.createAuthorizedReq = function(reqOpts, callback) {
var that = this;
// Add user agent.
reqOpts.headers = reqOpts.headers || {};
reqOpts.headers['User-Agent'] = reqOpts.headers['User-Agent'] ?
reqOpts.headers['User-Agent'] + '; ' + USER_AGENT : USER_AGENT;

if (this.isConnected()) {
return opt_callback && opt_callback(null, this.authorizeReq(reqOpts));
return callback(null, this.authorizeReq(reqOpts));
}
if (this.isConnecting) {
this.waitQueue = this.waitQueue || [];
this.waitQueue.push({ req: reqOpts, cb: opt_callback });
this.waitQueue.push({ req: reqOpts, cb: callback });
return;
}
this.connect(function(err) {
that.waitQueue.push({ req: reqOpts, cb: opt_callback });
that.waitQueue.push({ req: reqOpts, cb: callback });
that.waitQueue.forEach(function(v) {
if (!v.cb) {
return;
}

if (err) {
return v.cb && v.cb(err);
v.cb(err);
return;
}
v.cb && v.cb(null, that.authorizeReq(v.req));

v.cb(null, that.authorizeReq(v.req));
});
that.waitQueue = [];
});
Expand Down Expand Up @@ -189,7 +202,7 @@ Connection.prototype.isConnected = function() {
Connection.prototype.authorizeReq = function(reqOpts) {
// TODO(jbd): Clone the request object.
reqOpts.headers = reqOpts.headers || {};
reqOpts.headers['Authorization'] = 'Bearer ' + this.token.accessToken;
reqOpts.headers.Authorization = 'Bearer ' + this.token.accessToken;
return reqOpts;
};

Expand Down
24 changes: 14 additions & 10 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,30 @@
* limitations under the License.
*/

/*jshint strict:false, noarg:false */

var util = require('util');

module.exports.extend = function(from, to) {
if (from == null || typeof from != "object") {
function extend(from, to) {
if (from === null || typeof from !== 'object') {
return from;
}
if (from.constructor != Object && from.constructor != Array) {
if (from.constructor !== Object && from.constructor !== Array) {
return from;
}
if (from.constructor == Date || from.constructor == Function ||
from.constructor == String || from.constructor == Number ||
from.constructor == Boolean) {
if (from.constructor === Date || from.constructor === Function ||
from.constructor === String || from.constructor === Number ||
from.constructor === Boolean) {
return new from.constructor(from);
}
to = to || new from.constructor();
for (var name in from) {
to[name] = to[name] ? extend(from[name], null) : to[name];
}
return to;
};
}

module.exports.extend = extend;

module.exports.arrayize = function(input) {
if (!Array.isArray(input)) {
Expand All @@ -59,12 +63,12 @@ function ApiError (errorBody) {
this.errors = errorBody.errors;
this.code = errorBody.code;
this.message = errorBody.message;
};
}

util.inherits(ApiError, Error);

module.exports.handleResp = function(err, resp, body, opt_callback) {
var callback = opt_callback || noop;
module.exports.handleResp = function(err, resp, body, callback) {
callback = callback || noop;
if (err) {
callback(err);
return;
Expand Down
56 changes: 30 additions & 26 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
* limitations under the License.
*/

'use strict';

var entityMeta = {};

var namespaceRegex = kindRegex = fieldNameRegex = new RegExp(/^[A-Za-z]*$/);
var namespaceRegex = /^[A-Za-z]*$/,
kindRegex = /^[A-Za-z]*$/,
fieldNameRegex = /^[A-Za-z]*$/;

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

Int.prototype.get = function() {
return this.val;
}
};

module.exports.Int = Int;

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

Double.prototype.get = function() {
return this.val;
}
};

module.exports.Double = Double;

/**
* Converts any entity proto to a plain object.
*/
var entityFromEntityProto = function(proto) {
// TODO(jbd): Use registered metadata if provided.
return propertiesToObject(proto.properties);
};

/**
* Exports entityFromEntityProto.
* @type {Function}
*/
module.exports.entityFromEntityProto = entityFromEntityProto;

var keyFromKeyProto = function(proto) {
var key = [];
if (proto.partitionId.namespace) {
Expand All @@ -76,10 +94,10 @@ module.exports.keyFromKeyProto = keyFromKeyProto;

var keyToKeyProto = function(datasetId, key) {
if (key.length < 2) {
throw new Error("A key should contain at least a kind and an identifier.")
throw new Error('A key should contain at least a kind and an identifier.');
}
var namespace = null, start = 0;
if (key.length % 2 == 1) {
if (key.length % 2 === 1) {
// the first item is the namespace
namespace = key[0];
start = 1;
Expand Down Expand Up @@ -159,7 +177,7 @@ function propertyToValue(property) {
}
return l;
}
};
}

function propertiesToObject(properties) {
properties = properties || [];
Expand All @@ -168,21 +186,7 @@ function propertiesToObject(properties) {
obj[name] = propertyToValue(properties[name]);
}
return obj;
};

/**
* Converts any entity proto to a plain object.
*/
var entityFromEntityProto = function(proto) {
// TODO(jbd): Use registered metadata if provided.
return propertiesToObject(proto.properties);
};

/**
* Exports entityFromEntityProto.
* @type {Function}
*/
module.exports.entityFromEntityProto = entityFromEntityProto;
}

/**
* Convert any object to property value.
Expand Down Expand Up @@ -237,7 +241,7 @@ function valueToProperty(v) {
p.entityValue = { properties: properties };
return p;
}
throw new Error('Unsupported field value, ' + v + ', is provided.')
throw new Error('Unsupported field value, ' + v + ', is provided.');
}

/**
Expand Down Expand Up @@ -385,11 +389,11 @@ function validateField(name, field) {
if (!field.kind) {
throw new Error('Provide a kind for field ' + name);
}
if (typeof field.kind != 'object' &&
!primitiveKinds.indexOf(field.kind) < 0) {
if (typeof field.kind !== 'object' &&
primitiveKinds.indexOf(field.kind) === -1) {
throw new Error('Unknown kind for field ' + name);
}
if (typeof field.kind == 'object') {
if (typeof field.kind === 'object') {
Object.keys(field.key).forEach(function(key) {
validateField(key, field.key[key]);
});
Expand All @@ -406,4 +410,4 @@ module.exports.registerKind = registerKind;
* Exports getKind.
* @type {function}
*/
module.exports.getKind = getKind
module.exports.getKind = getKind;
Loading

0 comments on commit 547e73c

Please sign in to comment.