Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add global 'obj' and 'array's utils; Add tests for folders and encryp…

…tion support; Add new 'app' properties: privateKey and publicKey
  • Loading branch information...
commit 21efea786495bcff58e22355e1c9ec378573a382 1 parent 688b26c
@vpetrov authored
View
1  .gitignore
@@ -11,6 +11,7 @@ pids
logs
results
.idea
+private/*
node_modules
npm-debug.log
View
1  config.js
@@ -3,7 +3,6 @@ var path=require('path');
exports.brand="Survana";
exports.module_prefix="survana";
-
exports.to_requirejs=function()
{
var result={};
View
56 index.js
@@ -4,10 +4,15 @@ var sconfig=require("./config");
var log=require('logule').init(module);
var path=require('path');
var ejs=require('ejs');
+var ursa=require('ursa');
+var fs=require('fs');
ejs.open='{{';
ejs.close='}}';
+global.obj=require('./lib/obj');
+global.arrays=require('./lib/arrays');
+
function addModule(app,name,mconf)
{
var mname=sconfig.module_prefix+'-'+name;
@@ -24,8 +29,13 @@ function addModule(app,name,mconf)
app.log.info('Mounting '+mname+' on '+module.config.prefix)
+ var mserver=module.server(app,express); //get access to the module's 'app' instance
+ mserver.use(globalErrorHandler); //register global error handler
+ mserver.publicKey=app.publicKey; //transfer server public key
+ mserver.privateKey=app.privateKey; //transfer server private key
+
//mount module
- app.use(module.config.prefix,module.server(app,express));
+ app.use(module.config.prefix,mserver);
return module;
}
@@ -99,39 +109,51 @@ function routing(app,mroutes)
function globalErrorHandler(err,req,res,next)
{
- throw err;
+ var app=req.app;
+ var log=app.log; //use app-specific logger
+
+ log.error(err.message);
res.send({
success:0,
- message:err.toString()
+ message:err.message
},500);
}
+function getServerKey(encryption)
+{
+ var key=null;
+ var keypath=path.join(module.parent.dirname,encryption.key);
+
+ //if the key has already been generated, use it; if not - generate new key
+ if (fs.exists(keypath))
+ key=ursa.coerceKey(fs.readFileSync(keypath));
+ else
+ {
+ key=ursa.generatePrivateKey(encryption.bits); //generate new key
+ fs.writeFileSync(keypath,key); //save key to disk
+ fs.writeFileSync(keypath+'.pem',key.toPublicPem()); //save public key in PEM format
+ }
+
+ return key;
+}
+
exports.run=function(config)
{
var app=module.app=express.createServer();
module.config=config;
+ var key=getServerKey(config.encryption);
+
log.info('Waking up');
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
- app.log=log;
- app.error(globalErrorHandler);
- });
-
- app.configure('dev',function(){
- app.use(express.errorHandler({
- showStack: true,
- dumpExceptions: true
- }));
- });
+ app.use(globalErrorHandler);
- app.configure('prod',function(){
- app.log.suppress('debug');
- app.use(express.errorHandler());
+ app.log=log;
});
//expose utility methods
@@ -139,6 +161,8 @@ exports.run=function(config)
app.addModule=addModule;
app.routing=routing;
app.db=DB;
+ app.publicKey=ursa.coercePublicKey(key.toPublicPem());
+ app.privateKey=ursa.coercePrivateKey(key);
//root module must be added last, to prevent regex paths
//from conflicting
View
74 lib/arrays.js
@@ -0,0 +1,74 @@
+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=[];
+
+ 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;
+ }
+
+ for (var i in a)
+ 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=[];
+
+ if (!util.isArray(a1))
+ return 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;
+ }
+
+ for (var i in a)
+ if (!obj.equal(a[i],b[i]))
+ result.push(a[i]);
+
+ return result;
+}
+
+exports.equal=function(a,b)
+{
+ return obj.equal(a,b);
+}
View
67 lib/obj.js
@@ -0,0 +1,67 @@
+var util=require('util');
+
+exports.keys=function(o)
+{
+ var result=[];
+
+ for (var i in o)
+ result.push(i);
+
+ return result;
+}
+
+exports.override=function(obj1,obj2)
+{
+ if ((typeof obj1 === 'undefined') ||
+ (typeof obj2 === 'undefined'))
+ return obj1||obj2;
+
+ //override properties of obj1
+ for (var i in obj2)
+ obj1[i]=obj2[i];
+
+ return obj1;
+}
+
+exports.extract=function(obj,property,default_value,callback)
+{
+ if (typeof(obj[property])==='undefined')
+ return default_value;
+
+ var result=obj[property];
+ delete obj[property];
+
+ if (typeof(callback)==='function')
+ callback.call(this,result,obj);
+
+ return result;
+}
+
+exports.equal=function(obj1,obj2)
+{
+ //if one of them is not an object, perform strict equality
+ if ((typeof(obj1)!=='object') || (typeof(obj2)!=='object'))
+ return (obj1===obj2);
+
+ var a=util.isArray(obj1);
+ var b=util.isArray(obj2);
+
+ //one of them isn't an array?
+ if (a ^ b)
+ return false;
+ //if both are arrays or objects
+ else
+ {
+ //arrays with different lengths?
+ if (a && b && (a.length!==b.length))
+ return false;
+
+ //recursively compare these objects/arrays
+ for (var i in a)
+ if (!this.equal(a[i],b[i]))
+ return false;
+ }
+
+ //all elements are equal, so these must be equal
+ return true;
+}
View
3  package.json
@@ -14,7 +14,8 @@
"mongodb": "1.0.x",
"express": "2.5.x",
"ejs": "*",
- "vows": "0.6.x"
+ "vows": "0.6.x",
+ "ursa": "0.7.x"
},
"scripts": {
"test": "vows --spec test/*"
View
0  test/sanity.js → test/0.sanity.js
File renamed without changes
View
19 test/1.folders.js
@@ -0,0 +1,19 @@
+var vows=require('vows');
+var assert=require('assert');
+var fs=require('fs');
+var path=require('path');
+
+var suite=vows.describe('folders');
+
+suite.addBatch({
+ 'private/':{
+ topic:path.join(__dirname,'../private'),
+ 'is writable':function(topic){
+ var file=path.join(topic,'.test');
+ fs.writeFileSync(file,'test');
+ fs.unlinkSync(file);
+ }
+ }
+});
+
+suite.export(module);
View
23 test/2.encryption.js
@@ -0,0 +1,23 @@
+var vows=require('vows');
+var assert=require('assert');
+var ursa=require('ursa');
+
+var suite=vows.describe('encryption');
+
+suite.addBatch({
+ 'RSA':{
+ topic:ursa.generatePrivateKey(),
+ 'can generate keypair':function(topic){
+ assert.isObject(topic);
+ assert.isTrue(ursa.isKey(topic));
+ },
+ 'can export public key':function(topic){
+ assert.include(topic.toPublicPem().toString(),'PUBLIC KEY');
+ },
+ 'can export private key':function(topic){
+ assert.include(topic.toPrivatePem().toString(),'PRIVATE KEY')
+ }
+ }
+});
+
+suite.export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.