Skip to content

Commit

Permalink
chore(jshint): fix jshint complaints
Browse files Browse the repository at this point in the history
Fixed various JSHint complaints, and also fixed type-loader to not throw when a node module can't be loaded.
  • Loading branch information
jeffbcross committed May 20, 2014
1 parent 69cb192 commit a3e24b0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ module.exports = function (grunt) {
'lib/resources/*.js',
'lib/util/*.js',
'lib/*.js'
]
]
}

});

grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-jshint');

Expand Down
6 changes: 3 additions & 3 deletions lib/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ var validation = require('validation')

function Files(name, options) {
Resource.apply(this, arguments);
if(this.config['public']) {
this['public'] = this.config['public'];
if(this.config.public) {
this.public = this.config.public;
} else {
throw new Error('public root folder location required when creating a file resource');
}
Expand All @@ -33,7 +33,7 @@ Files.prototype.handle = function (ctx, next) {
if(ctx.req && ctx.req.method !== 'GET') return next();

send(ctx.req, url.parse(ctx.url).pathname)
.root(path.resolve(this['public']))
.root(path.resolve(this.public))
.on('error', function (err) {
ctx.res.statusCode = 404;
respond('Resource Not Found', ctx.req, ctx.res);
Expand Down
14 changes: 7 additions & 7 deletions lib/resources/user-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var validation = require('validation')
* Settings:
*
* - `path` the base path a resource should handle
* - `config.properties` the properties of objects the collection should store
* - `config.properties` the properties of objects the collection should store
* - `db` the database a collection will use for persistence
*
* @param {Object} options
Expand Down Expand Up @@ -71,7 +71,7 @@ UserCollection.prototype.handle = function (ctx) {
for (var field in ctx.query.$fields) {
if (ctx.query.$fields.hasOwnProperty(field) && ctx.query.$fields[field] > 0) {
omit = false;
if ('password' in ctx.query.$fields) delete ctx.query.$fields['password'];
if ('password' in ctx.query.$fields) delete ctx.query.$fields.password;
}
break;
}
Expand All @@ -86,9 +86,9 @@ UserCollection.prototype.handle = function (ctx) {
ctx.res.statusCode = 204;
return ctx.done();
}

ctx.query = {id: ctx.session.data.uid, $fields: {password: 0}};

return this.find(ctx, ctx.done);
}

Expand Down Expand Up @@ -134,15 +134,15 @@ UserCollection.prototype.handle = function (ctx) {

function done(err, res) {
if (res) delete res.password;
ctx.done(err, res);
ctx.done(err, res);
}

if(ctx.query.id || ctx.body.id) {
this.save(ctx, done);
} else {
this.store.first({username: ctx.body.username}, function (err, u) {
if(u) return ctx.done({errors: {username: 'is already in use'}});
uc.save(ctx, done);
uc.save(ctx, done);
});
}
break;
Expand All @@ -157,7 +157,7 @@ UserCollection.prototype.handleSession = function (ctx, fn) {
// called when any session has been created
var session = ctx.session
, path = this.path;

if(session && session.data && session.data.path == path && session.data.uid) {
this.store.find({id: session.data.uid, $fields: {password: 0}}, function(err, user) {
session.user = user;
Expand Down
24 changes: 12 additions & 12 deletions lib/type-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ var fs = require('fs')
module.exports = function loadTypes(basepath, fn) {
var types = {}
, defaults = {};

if(typeof basepath == 'function') {
fn = basepath;
fn = basepath;
basepath = undefined;
}

Expand All @@ -27,7 +27,7 @@ module.exports = function loadTypes(basepath, fn) {
fs.readdir(path + '/node_modules', function(err, dir) {
var remaining = 0;
if(dir && dir.length) {
dir.forEach(function(file) {
dir.forEach(function(file) {
if(file.indexOf('.js') == file.length - 3 || file.indexOf('.') === -1) {
var d = domain.create();
remaining++;
Expand All @@ -39,22 +39,22 @@ module.exports = function loadTypes(basepath, fn) {
var c = require(require('path').resolve(path) + '/node_modules/' + file);
if(c && c.prototype && c.prototype.__resource__) {
debug('is a resource ', c && c.name);
types[c.name] = c;
types[c.name] = c;
}
} catch(e) {
console.error();
console.error("Error loading module node_modules/" + file);
console.error(e.stack || e);
if(process.send) process.send({moduleError: e || true});
process.exit(1);
} catch(e) {
/**
* (@jeffbcross): Why should this exit the process? Should not
* restrict an app to only including deployd resources...
* unless I'm missing something.
*/
}

if(remaining === 0) {
fn(defaults, types);
}
});
});

d.on('error', function (err) {
console.error('Error in module node_modules/' + file);
console.error(err.stack || err);
Expand Down
34 changes: 17 additions & 17 deletions lib/util/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ var Cookies = require('cookies')
, parseUrl = require('url').parse
, corser = require('corser')
, ALLOWED_METHODS = ['GET', 'POST', 'PUT', 'DELETE'];

/*!
* A utility for setting up a request and response.
*/

exports.setup = function(req, res, next) {
var remoteHost = req.headers.origin
, corsOpts = {supportsCredentials: true, methods: ALLOWED_METHODS};

if(remoteHost) {
corsOpts.origins = [remoteHost];
} else {
corsOpts.supportsCredentials = false;
}

corsOpts.requestHeaders = corser.simpleRequestHeaders.concat(["X-Requested-With"]);
var handler = corser.create(corsOpts);

handler(req, res, function () {
req.cookies = res.cookies = new Cookies(req, res);

if(~req.url.indexOf('?')) {
try {
req.query = parseQuery(req.url);
req.query = parseQuery(req.url);
var m = req.query._method;
if ( m ) {
req['originalMethod'] = req.method;
req.originalMethod = req.method;
req.method = m.toUpperCase();
delete req.query['_method'];
delete req.query._method;
}
} catch (ex) {
res.setHeader('Content-Type', 'text/plain');
Expand All @@ -40,7 +40,7 @@ exports.setup = function(req, res, next) {
return;
}
}

switch(req.method) {
case 'OPTIONS':
// End CORS preflight request.
Expand Down Expand Up @@ -69,10 +69,10 @@ exports.setup = function(req, res, next) {

/*!
* Attempts to parse the stream. Currently supports the following formats:
*
*
* - application/json
* - application/x-www-form-urlencoded (all values are strings)
*
*
* @param {ServerRequest} req
* @param {String} mime
* @param {Function} callback (err)
Expand All @@ -97,13 +97,13 @@ var parseBody = exports.parseBody = function(req, res, mime, callback) {
res.end('Could not parse invalid JSON');
return;
}

req.body = parser.parse(buf);
var m = req.body['_method'];
var m = req.body._method;
if ( m ) {
req['originalMethod'] = req.method;
req.originalMethod = req.method;
req.method = m.toUpperCase();
delete req.body['_method'];
delete req.body._method;
}
} else {
req.body = {};
Expand All @@ -121,11 +121,11 @@ var parseQuery = exports.parseQuery = function(url) {
var q = url.substr(url.indexOf('?') + 1);

if(q) q = decodeURIComponent(q);

if(q[0] === '{' && q[q.length - 1] === '}') {
return JSON.parse(q);
} else {
return parseNumbersInObject(qs.parse(parseUrl(url).query));
return parseNumbersInObject(qs.parse(parseUrl(url).query));
}
};

Expand Down Expand Up @@ -166,4 +166,4 @@ var parseNumbersInObject = function( obj ){
}
}
return ret;
}
};
9 changes: 5 additions & 4 deletions lib/util/uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function Alea() {
var s2 = 0;
var c = 1;

if (args.length == 0) {
args = [+new Date];
if (args.length === 0) {
args = [+new Date()];
}
var mash = Mash();
s0 = mash(' ');
Expand All @@ -77,7 +77,8 @@ function Alea() {
var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
s0 = s1;
s1 = s2;
return s2 = t - (c = t | 0);
s2 = t - (c = t | 0);
return s2;
};
random.uint32 = function() {
return random() * 0x100000000; // 2^32
Expand Down Expand Up @@ -108,4 +109,4 @@ exports.create = function (length) {

// return the uuid
return s.join('');
}
};

0 comments on commit a3e24b0

Please sign in to comment.