Permalink
Browse files

refactored session lib to more common connect-session usage

  • Loading branch information...
1 parent c6459c5 commit db76ad32d607308feec007a234606cd5ea03676d @Shogun147 committed Jan 28, 2014
Showing with 33 additions and 42 deletions.
  1. +33 −42 lib/session/index.js
View
@@ -1,58 +1,53 @@
var conf = App.get('session');
-var storeProvider = App.get(':stores '+ conf.store.name +' provider');
-var store = null;
+var Store = null;
-var token = App.utils.token;
+var utils = App.utils;
+var token = utils.token;
+var extend = utils.extend;
-var Session = App.session = module.exports = function(id, callback) {
- var self = this;
-
- !(self.id = id) && self.generate();
- self.data = conf.defaults;
-
- store.read(id, function(error, data) {
- if (error) {
- return callback(error);
- }
-
- self.data = data;
+var Session = App.session = module.exports = function(id) {
+ if (!(this.id = id)) {
+ this.generate();
+ }
- callback(null, self);
- });
+ extend(this, conf.defaults);
}
Session.prototype = {
- data: {},
-
- set: function(name, value) {
- this.data[name] = value;
- },
+ load: function(callback) {
+ var self = this;
+ Store.read(this.id, function(error, data) {
+ if (error) { return callback(error); }
- get: function(name) {
- return this.data[name];
+ extend(self, data);
+ callback(null);
+ });
},
- delete: function(name) {
- delete this.data[name];
+ save: function(callback) {
+ Store.save(this.id, this, callback);
},
clear: function() {
- this.data = {};
- },
-
- save: function(callback) {
- store.save(this.id, this.data, callback);
+ var self = this;
+ Object.keys(this).forEach(function(key) {
+ if (key !== 'id') {
+ delete self[key];
+ }
+ });
},
reset: function() {
- store.delete(this.id);
+ Store.delete(this.id);
+ this.clear();
this.generate();
},
generate: function() {
this.id = token(conf.key.length);
- this.data = conf.defaults;
+
+ extend(this, conf.defaults);
}
}
@@ -64,11 +59,9 @@ Session.store = function(name, provider) {
Session.stores.memory = require('./memory');
Session.stores.redis = require('./redis');
Session.stores.mongoose = require('./mongoose');
-// Session.stores.mysql = require('./mysql');
-// Session.stores.memcache = require('./memcache');
App.on('start', function() {
- store = new Session.stores[storeProvider](conf);
+ Store = new Session.stores[App.get(':stores '+ conf.store.name +' provider')](conf);
});
App.use(function(request, response, next) {
@@ -81,20 +74,18 @@ App.use(function(request, response, next) {
var end = response.end;
response.end = function(data, encoding) {
response.end = end;
-
response.session.save(function(error) {
if (error) { console.log(error); }
response.end(data, encoding);
});
}
- new Session(request.cookies.get(conf.key.name), function(error, session) {
- if (error) {
- console.log(error);
- }
+ var sess = new Session(request.cookies.get(conf.key.name));
+ sess.load(function(error) {
+ if (error) { console.log(error); }
- request.session = response.session = session;
+ request.session = response.session = sess;
request.resume();
next();

0 comments on commit db76ad3

Please sign in to comment.