Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add editorconfig & jshint. #92

Merged
merged 2 commits into from
Aug 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",

This comment was marked as spam.

This comment was marked as spam.

"maxlen": 80,
"newcap": true,
"node": true,
"noarg": true,
"quotmark": "single",
"strict": true,
"trailing": true,
"undef": true,
"unused": true

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}
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>
64 changes: 41 additions & 23 deletions lib/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
* limitations under the License.
*/

var GAPIToken = require('gapitoken'),
async = require('async'),
req = require('request'),
pkg = require('../../package.json');
/*jshint camelcase:false */

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


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

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

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

/**
* Token represents an access token
Expand Down Expand Up @@ -48,8 +54,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 +92,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 +120,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 +134,52 @@ 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 (reqOpts.headers['User-Agent']) {
reqOpts.headers['User-Agent'] += '; ' + USER_AGENT;
} else {
reqOpts.headers['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 +207,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
72 changes: 40 additions & 32 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
* limitations under the License.
*/

var entityMeta = {};
'use strict';

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

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

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

module.exports.Int = Int;

Expand All @@ -56,10 +59,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,21 +93,26 @@ 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) {
var namespace = null;
var start = 0;
if (key.length % 2 === 1) {
// the first item is the namespace
namespace = key[0];
start = 1;
}
var path = [];
for (var i = start; i < (key.length - start); i += 2) {
var p = { kind: key[i] }, val = key[i+1];
var p = { kind: key[i] };
var val = key[i+1];
if (val) {
// if not numeric, set key name.
if (isNaN(val)) { p.name = val; }
else { p.id = val; }
if (isNaN(val)) {
p.name = val;
} else {
p.id = val;
}
}
path.push(p);
}
Expand All @@ -111,7 +133,7 @@ var keyToKeyProto = function(datasetId, key) {
module.exports.keyToKeyProto = keyToKeyProto;

module.exports.formatArray = function(results) {
return results.map(function (result) {
return results.map(function(result) {
return {
key: keyFromKeyProto(result.entity.key),
data: entityFromEntityProto(result.entity)
Expand Down Expand Up @@ -159,7 +181,7 @@ function propertyToValue(property) {
}
return l;
}
};
}

function propertiesToObject(properties) {
properties = properties || [];
Expand All @@ -168,21 +190,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 +245,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 +393,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 +414,4 @@ module.exports.registerKind = registerKind;
* Exports getKind.
* @type {function}
*/
module.exports.getKind = getKind
module.exports.getKind = getKind;
Loading