Permalink
Browse files

util.js: add comment

index.js: add 'keyID' and randomId() properties to each module's app
lib/obj.js: add a blacklist() function, which removes a set of properties from an object
lib/arrays.js: add a unique() to return unique elements between two arrays
Restyle part of the code for JSHint/JSLint
  • Loading branch information...
1 parent 4545443 commit 5f04fac8f858b0c463aa6632515685f4cca30bd4 @vpetrov committed Nov 7, 2012
Showing with 115 additions and 52 deletions.
  1. +7 −2 index.js
  2. +81 −40 lib/arrays.js
  3. +15 −0 lib/obj.js
  4. +12 −10 util.js
View
@@ -6,6 +6,7 @@ var path=require('path');
var ejs=require('ejs');
var ursa=require('ursa');
var fs=require('fs');
+var util=require('./util');
ejs.open='{{';
ejs.close='}}';
@@ -34,6 +35,8 @@ function addModule(app,name,mconf)
mserver.use(globalErrorHandler); //register global error handler
mserver.publicKey=app.publicKey; //transfer server public key
mserver.privateKey=app.privateKey; //transfer server private key
+ mserver.keyID=app.keyID;
+ mserver.randomId=util.randomId;
//mount module
app.use(module.config.prefix,mserver);
@@ -113,7 +116,7 @@ function globalErrorHandler(err,req,res,next)
var app=req.app;
var log=app.log; //use app-specific logger
- log.error(err.message);
+ log.error(err.message,err.stack);
res.send({
success:0,
@@ -178,7 +181,8 @@ function readKeys(items)
throw Error("'"+i+"': no key could be found at location '"+keypath+"'");
//read the key and store it instead of the 'key' property
- items[i].key=fs.readFileSync(keypath);
+ items[i].key=ursa.coercePublicKey(fs.readFileSync(keypath));
+ items[i].keyID=items[i].key.toPublicSshFingerprint('hex');
}
return items;
@@ -210,6 +214,7 @@ exports.run=function(config)
app.db=DB;
app.publicKey=ursa.coercePublicKey(key.toPublicPem());
app.privateKey=ursa.coercePrivateKey(key);
+ app.keyID=app.publicKey.toPublicSshFingerprint('hex');
//root module must be added last, to prevent regex paths
//from conflicting
View
@@ -1,74 +1,115 @@
-var util=require('util');
-var obj=require('./obj');
+var util = require('util');
+var obj = require('./obj');
/**
* Computes the intersection of two arrays.
* @param a1
* @param a2
* @return {Array} All elements that are in a1 and in a2 (or an empty array).
*/
-exports.intersect=function(a1,a2)
-{
- var a,b, c,result=[];
+exports.intersect = function (a1, a2) {
+ var a, b, result = [];
- if (!util.isArray(a1) || !util.isArray(a2))
+ if (!util.isArray(a1) || !util.isArray(a2)) {
return [];
+ }
//choose to iterate over the shorter array
- if (a2.length<a1.length)
- {
- a=a2;
- b=a1;
- }
- else
- {
- a=a1;
- b=a2;
+ if (a2.length < a1.length) {
+ a = a2;
+ b = a1;
+ } else {
+ a = a1;
+ b = a2;
}
- for (var i in a)
- if (obj.equal(a[i],b[i]))
- result.push(a[i]);
+ for (var i in a) {
+ if (a.hasOwnProperty(i)) {
+ if (obj.equal(a[i], b[i])) {
+ result.push(a[i]);
+ }
+ }
+ }
return result;
-}
+};
/**
* Computes the differences of two arrays.
* @param a1
* @param a2
* @return {Array} All elements that are not in a1 (or an empty array if they are the same).
*/
-exports.diff=function(a1,a2)
-{
- var a,b, result=[];
+exports.diff = function (a1, a2) {
+ var a, b, result = [];
- if (!util.isArray(a1))
+ if (!util.isArray(a1)) {
return a2;
+ }
- if (!util.isArray(a2))
+ if (!util.isArray(a2)) {
return a1;
+ }
//choose to iterate over the longer array (so that any remaining elements
- if (a2.length>a1.length)
- {
- a=a2;
- b=a1;
- }
- else
- {
- a=a1;
- b=a2;
+ if (a2.length > a1.length) {
+ a = a2;
+ b = a1;
+ } else {
+ a = a1;
+ b = a2;
}
- for (var i in a)
- if (!obj.equal(a[i],b[i]))
- result.push(a[i]);
+ for (var i in a) {
+ if (a.hasOwnProperty(i)) {
+ if (!obj.equal(a[i], b[i]))
+ result.push(a[i]);
+ }
+ }
return result;
-}
+};
+
+/**
+ *
+ * @param a
+ * @param b
+ * @return {*}
+ */
+exports.equal = function (a, b) {
+ return obj.equal(a, b);
+};
-exports.equal=function(a,b)
-{
- return obj.equal(a,b);
+/**
+ *
+ * @param a
+ * @param p
+ */
+exports.blacklist = function (a, p) {
+ for (var i = 0; i < a.length; i += 1) {
+ for (var j in p) {
+ if (p.hasOwnProperty(j)) {
+ if (a[i].hasOwnProperty(j)) {
+ delete a[i][j];
+ }
+ }
+ }
+ }
+};
+
+/**
+ * Returns all elements in a1 that are not in a2
+ * @param a1
+ * @param a2
+ */
+exports.unique = function (a1, a2) {
+ var result = [];
+
+ for (var i = 0; i < a1.length; i += 1) {
+ if (a2.indexOf(a1[i]) < 0) {
+ result.push(a1[i]);
+ }
+ }
+
+ return result;
}
View
@@ -10,6 +10,21 @@ exports.keys=function(o)
return result;
}
+/** Removes the specified properties of an object
+ * @param o {Object} The object
+ * @param p {Array|Object} Properties to remove
+ * @return {Object} The object (not a copy) without the specified properties
+ * @warning The object passed as the first parameter will be modified in-place.
+ */
+exports.blacklist = function (o, p) {
+ for (var i in p) {
+ if (p.hasOwnProperty(i))
+ delete o[i];
+ }
+
+ return o;
+}
+
exports.override=function(obj1,obj2)
{
if ((typeof obj1 === 'undefined') ||
View
22 util.js
@@ -1,18 +1,20 @@
+var alphabet='ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz0123456789'; //removed confusing letters O,o,l,I
+var alphabet_max=alphabet.length-1;
+
/** Generates a unique alphanumeric sequence of characters
* @param $length Number of characters to generate
* @return String
*/
exports.randomId=function(len)
{
- if (typeof(len)==='undefined')
- len=6;
+ if ((typeof(len)==='undefined') || !len)
+ len=6;
+
+ var result="";
- var result="";
- var alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- var max=alphabet.length-1;
+ //build a string, char by char, with random characters from the 'alphabet' string
+ for (var i=0;i<len;++i)
+ result+=alphabet[parseInt(Math.random()*10000) % alphabet_max];
- for (var i=0;i<len;++i)
- result+=alphabet[parseInt(Math.random()*10000) % max];
-
- return result;
- }
+ return result;
+}

0 comments on commit 5f04fac

Please sign in to comment.