Navigation Menu

Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dimsmol committed Feb 16, 2012
0 parents commit 7b32ecf
Show file tree
Hide file tree
Showing 24 changed files with 944 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.DS_Store
node_modules/
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
.DS_Store
.git*
Empty file added Makefile
Empty file.
Empty file added Readme.md
Empty file.
Empty file added TODO.md
Empty file.
82 changes: 82 additions & 0 deletions example/examples.js
@@ -0,0 +1,82 @@
// old atempts, left for some ideas


User = typedef({
id:
name:
username:
});

// warn about unused args
// warn about 404s

// try non-declarative way!
// start from single url definition

// result (or message) format:
{
meta: {
httpResponseCode: Number
},
error: Object, // usually an exception thrown
data: Object,
objects: {
Object: Object, // id-to-object map of referenced objects
}
}
// any object can have _type property, indicating type
// api method can return:
new HintedResult({
data: Object,
objects: {
objectTypeString: {
objectId: object
}
},
objectIds: {
objectTypeString: [objectsId,]
}
}) // to hint upper middleware to provide referenced objects along with response
var unwrap = function(possibleHinted) {
if (possibleHinted instanceof HintedResult)
{
return possibleHinted.data;
}

return possibleHinted;
}

// possible errors list
// multiple queries in one sharing common dict of referenced objects

{
path: 'user', // list of users
req: [auth],
opt: {read: [range]},
read: [User.username.opt, {_returns: List(User._short)}],
update: [User.except('password'), {_returns: User.id}],

code: require('./api/user').UserList
}

{
path: 'user/:id', // particular user
args: {id: User.id},
req: [auth],
read: {_returns: User.except('password')},
update: User.except('email', 'password'),
del: {},
props: [User.username, User.name],

code: require('./api/user').User
}

{
path: 'user/:id/email', // user's email
args: {id: User.id},
req: [auth, secure],
call: [ctx.authInfo, args.id,
User.email, User.password],

code: require('./api/user').User.updateEmail,
}
64 changes: 64 additions & 0 deletions lib/app.js
@@ -0,0 +1,64 @@
var abstractMethod = require('./tools/abstract_method').abstractMethod;

var Lowlevel = require('./lowlevel');
var Units = require('./units').Units;


var App = function () {
this.config = null;
this.lowlevel = null;
this.units = null;
this.contract = null;
};

App.prototype.defineConfig = abstractMethod;
App.prototype.applyContract = abstractMethod;

App.prototype.start = function () {
this.lowlevel.start();
};

App.prototype.init = function () {
this.defineConfig();
this.prepareLowlevel();
this.prepareUnits();
this.applyContract();
};

App.prototype.prepareLowlevel = function () {
this.defineLowlevel();
this.initLowlevel();
};

App.prototype.defineLowlevel = function () {
this.lowlevel = new Lowlevel(this.config);
};

App.prototype.initLowlevel = function () {
this.lowlevel.init();
};

App.prototype.prepareUnits = function () {
this.defineUnits();
this.addUnits();
this.initUnits();
};

App.prototype.defineUnits = function () {
this.units = new Units();
};

App.prototype.addUnits = function () {
};

App.prototype.initUnits = function () {
this.units.init();
};

App.prototype.setContract = function (contract) {
this.contract = contract;
this.lowlevel.setHandler(contract);
};


module.exports = App;
45 changes: 45 additions & 0 deletions lib/contract.js
@@ -0,0 +1,45 @@
var Resource = require('./resource').Resource;


var Contract = function (path) {
this.path = path;
this.items = [];
};

Contract.prototype.add = function (item) {
if (typeof item === 'object')
{
item = new Resource(item);
}

this.items.push(item);
};

Contract.prototype.handle = function (ctx, next) {
var handlerChain = this.resolve(ctx);
if (handlerChain != null)
{
handlerChain.execute(ctx, next);
}
else
{
next();
}
};

Contract.prototype.resolve = function (ctx) {
for (var k in this.items)
{
var item = this.items[k];
var result = item.resolve(ctx);
if (result != null)
{
return result;
}
}

return null;
};


module.exports = Contract;
16 changes: 16 additions & 0 deletions lib/ctx.js
@@ -0,0 +1,16 @@
var Ctx = function () {
this.web = null;
this.socket = null;

this.path = null;
this.method = null;

this.isProcessed = false;
};

Ctx.prototype.processed = function () {
this.isProcessed = true;
};


module.exports = Ctx;
80 changes: 80 additions & 0 deletions lib/handlers.js
@@ -0,0 +1,80 @@
var socketSupport = function (ctx, next) {
var message = JSON.parse(ctx.socket.message);
var meta = message.meta;

ctx.meta = meta;
ctx.path = meta.path;
ctx.method = meta.method;


result = {
ctx: ctx,
data: message.data
};

next(null, ctx);
};

var socketStickConnection = function () {
var connectionUserData = connection.userData;

if (connectionUserData.userId == null)
{
if (userId == null)
{
throw Error('Invalid auth data');
}
else
{
if (connectionUserData.endPointId != null)
{
throw Error('Connection improperly initialized');
}

connectionUserData.userId = userId;
connectionUserData.endPointId = endPointId;

this.connections.onIdentificationProvided(connection);
}
}
else if (connectionUserData.userId != userId || connectionUserData.endPointId != endPointId)
{
throw Error('Invalid auth data');
}
};

var auth = function (ctx, next) {
var authToken = ctx.meta.auth;
if (authToken != null)
{
userId = this.authenticate(authToken);
}

ctx.auth = {
userId: userId
};

next(null, ctx);
};

var clientData = function (ctx, next) {
var meta = ctx.meta;

ctx.clientInfo = {
endPointId: meta.endPointId,
serial: meta.serial,
userId: ctx.auth.userId
};

return next();
};

var data = function (ctx, next) {

return next();
};

var ret = function (ctx, next) {

return next();
};
13 changes: 13 additions & 0 deletions lib/handlers/handler.js
@@ -0,0 +1,13 @@
var Handler = function () {
};

Handler.prototype.handle = function (ctx, next) {
next(ctx);
};

Handler.prototype.setup = function (resource) {
return true;
};


module.exports = Handler;
25 changes: 25 additions & 0 deletions lib/handlers/impl.js
@@ -0,0 +1,25 @@
var inherits = require('util').inherits;

var Handler = require('./handler');


var Impl = function (f) {
this.impl = f;
};
inherits(Impl, Handler);

Impl.prototype.setup = function (chain) {
chain.impl = this.impl;
return false;
};


var impl = function (f) {
return new Impl(f);
};


module.exports = {
Impl: Impl,
impl: impl
};
33 changes: 33 additions & 0 deletions lib/handlers/ret.js
@@ -0,0 +1,33 @@
var inherits = require('util').inherits;

var Handler = require('./handler');


var Ret = function () {
};
inherits(Ret, Handler);

Ret.prototype.handle = function (ctx, next) {
if (ctx.currentHandlerChain.impl == null)
{
throw new Error('No impl defined');
}

var handleResult = function (error, result) {
ctx.web.res.send(result);
ctx.processed();
next(ctx);
};

ctx.currentHandlerChain.impl(ctx, handleResult);
};

var ret = function () {
return new Ret();
};


module.exports = {
Ret: Ret,
ret: ret
};

0 comments on commit 7b32ecf

Please sign in to comment.