Permalink
Browse files

Added Cookie session-store.

  • Loading branch information...
1 parent 77c3756 commit 33ab7a93871d463fe9ded4c557ec157d6c7e0d4e @mde mde committed Nov 14, 2011
Showing with 119 additions and 66 deletions.
  1. +14 −14 lib/base_controller.js
  2. +0 −1 lib/cookies.js
  3. +12 −15 lib/geddy.js
  4. +3 −0 lib/server.js
  5. +13 −10 lib/sessions/index.js
  6. +46 −0 lib/sessions/stores/cookie.js
  7. +23 −19 lib/sessions/stores/couchdb.js
  8. +8 −7 lib/sessions/stores/memory.js
View
28 lib/base_controller.js
@@ -361,7 +361,7 @@ controller.BaseController.prototype = new (function () {
else {
this.throwUndefinedFormatError();
}
- // Don't look at any more formats if there's a match
+ self// Don't look at any more formats if there's a match
if (match) {
break;
}
@@ -372,27 +372,27 @@ controller.BaseController.prototype = new (function () {
}
, _doResponse = function (stat, headers, content) {
- var _this = this
+ var self = this
, r = new response.Response(this.response)
, action = this.action
, callback = function () {
- r.finalize(_this.content);
- };
-
- if (this.cookies) {
- headers['Set-Cookie'] = this.cookies.toArray();
- }
+ // Set status and headers, can be overridden in after-filters
+ if (self.cookies) {
+ headers['Set-Cookie'] = self.cookies.toArray();
+ }
+ r.setHeaders(stat, headers);
- // Set status and headers, can be overridden in after-filters
- r.setHeaders(stat, headers);
+ // Run after-filters, then finish out the response
+ _execFilters.apply(self, [action, 'after', function () {
+ r.finalize(self.content);
+ }]);
+ };
if (this.session) {
- this.session.close(function () {
- _execFilters.apply(_this, [action, 'after', callback]);
- });
+ this.session.close(callback);
}
else {
- _execFilters.apply(this, [action, 'after', callback]);
+ callback();
}
}
View
1 lib/cookies.js
@@ -90,7 +90,6 @@ cookies.Cookie.prototype.toString = function () {
res.push('HttpOnly');
}
str = res.join('; ');
- console.log('cookie: ' + str);
return str;
};
View
27 lib/geddy.js
@@ -47,17 +47,17 @@ geddy.mixin(geddy, new (function () {
// Load app-config
// ==================
- var _loadConfig = function (callback) {
+ var _loadConfig = function (next) {
var config = worker.config
, dirname = '/config'
, filename = dirname + '/' + config.environment + '.js'
, appConfig = vm.runInThisContext(fs.readFileSync(dir + filename).toString());
this.config = geddy.mixin({}, config, appConfig, true);
- callback();
+ next();
}
// Load controller ctors
// ==================
- , _getControllerConstructors = function (callback) {
+ , _getControllerConstructors = function (next) {
var dirname = '/app/controllers'
, dirList = fs.readdirSync(dir + dirname)
, fileName
@@ -87,35 +87,33 @@ geddy.mixin(geddy, new (function () {
ctor.origPrototype = ctor.prototype;
}
this.controllerRegistry = ctors;
- callback();
+ next();
}
// Load the router
// ==================
- , _loadRouter = function (callback) {
+ , _loadRouter = function (next) {
router = require(dir + '/config/router');
router = router.router || router;
this.router = router;
- callback();
+ next();
}
// Connect to session-store
// ==================
- , _loadSessionStore = function (callback) {
+ , _loadSessionStore = function (next) {
sessionsConfig = this.config.sessions;
if (sessionsConfig) {
- sessions.createStore(sessionsConfig.store, function () {
- callback();
- });
+ sessions.createStore(sessionsConfig.store, next);
}
else {
- callback();
+ next();
}
}
// Register template-paths
// ==================
- , _registerTemplatePaths = function (callback) {
+ , _registerTemplatePaths = function (next) {
var self = this;
exec('find ' + dir + '/app/views', function (err, stdout, stderr) {
var templates
@@ -139,7 +137,7 @@ geddy.mixin(geddy, new (function () {
}
}
self.templateRegistry = templates;
- callback();
+ next();
}
});
};
@@ -158,8 +156,7 @@ geddy.mixin(geddy, new (function () {
, items
, chain;
- // Make some aliases
- // ==================
+ // Set up some aliases
self.worker = worker;
self.server = worker.server;
View
3 lib/server.js
@@ -53,6 +53,7 @@ var server = module.exports = new function () {
// -------------
// Passed-in opts
this.opts = null;
+
// Base config, can be overridden by opts passed to `start`
this.config = {
// Default to prod
@@ -82,6 +83,8 @@ var server = module.exports = new function () {
key: 'sid',
expiry: 14 * 24 * 60 * 60
}
+ // Key for when using Cookie session-store
+ , cookieSessionKey: 'sdata'
};
// Registry of the worker-processes
this.workers = {};
View
23 lib/sessions/index.js
@@ -16,7 +16,7 @@
*
*/
-var session = new function () {
+var session = new (function () {
var KEY_LENGTH = 32;
this.store = null;
@@ -40,22 +40,25 @@ var session = new function () {
session.store = new constructor(callback);
};
-}();
+})();
session.Session = function (controller, callback) {
var self = this
, cookies = controller.cookies
, keyName = geddy.config.sessions.key
, sid = cookies.get(keyName);
- this.store = null;
- this.sid = '';
+
if (!sid) {
sid = session.generateSessionId()
var dt = new Date();
dt.setTime(dt.getTime() + (geddy.config.sessions.expiry * 1000));
cookies.set(keyName, sid, {expires: dt.toGMTString()});
}
+
this.sid = sid;
+ this.controller = controller;
+ this.data = null;
+
setTimeout(function () {
self.init(callback);
}, 0);
@@ -65,26 +68,26 @@ session.Session.prototype = new function () {
this.init = function(appCallback) {
var _this = this;
var localCallback = function (result) {
- _this.store = result;
+ _this.data = result;
appCallback();
};
- session.store.read(this.sid, localCallback);
+ session.store.read(this, localCallback);
};
this.get = function (key) {
- return this.store[key];
+ return this.data[key];
};
this.set = function (key, val) {
- this.store[key] = val;
+ this.data[key] = val;
};
this.unset = function (key) {
- delete this.store[key];
+ delete this.data[key];
};
this.close = function (appCallback) {
- session.store.write(this.sid, this.store, appCallback);
+ session.store.write(this, appCallback);
};
}();
View
46 lib/sessions/stores/cookie.js
@@ -0,0 +1,46 @@
+/*
+ * Geddy JavaScript Web development framework
+ * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+*/
+
+var Cookie = function (callback) {
+ this.setup(callback);
+};
+
+Cookie.prototype = new (function () {
+ this.setup = function (callback) {
+ callback();
+ };
+
+ this.read = function (session, callback) {
+ var controller = session.controller
+ , data = controller.cookies.get(geddy.config.cookieSessionKey);
+ data = data ? JSON.parse(data) : {};
+ callback(data);
+ };
+
+ this.write = function (session, callback) {
+ var controller = session.controller
+ , data = JSON.stringify(session.data);
+ controller.cookies.set(geddy.config.cookieSessionKey, data);
+ callback();
+ };
+
+})();
+
+exports.Cookie = Cookie;
+
+
View
42 lib/sessions/stores/couchdb.js
@@ -16,7 +16,6 @@
*
*/
-var sys = require("sys");
var http = require("http");
var Couchdb = function (callback) {
@@ -27,22 +26,25 @@ Couchdb.prototype = new function () {
var _this = this;
var _sessions = {};
var _appCallback;
-
+
this.setup = function (callback) {
_appCallback = callback;
- this.request({url: '/' + geddy.config.sessions.dbName, method: 'GET'}, this.ensureSetup);
+ this.request({url: '/' + geddy.config.sessions.dbName,
+ method: 'GET'}, this.ensureSetup);
};
this.ensureSetup = function (response) {
if (response.statusCode == 404) {
- _this.request({url: '/' + geddy.config.sessions.dbName, method: 'PUT'}, _this.ensureSetup);
+ _this.request({url: '/' + geddy.config.sessions.dbName,
+ method: 'PUT'}, _this.ensureSetup);
}
else {
_appCallback();
}
};
- this.read = function (sid, callback) {
+ this.read = function (session, callback) {
+ var sid = session.sid;
_appCallback = callback;
this.request({url: '/' + geddy.config.sessions.dbName +
'/' + sid, method: 'GET'}, this.ensureRead);
@@ -88,10 +90,12 @@ Couchdb.prototype = new function () {
throw new Error('could not create CouchDB session.');
}
};
-
- this.write = function (sid, store, callback) {
+
+ this.write = function (session, callback) {
+ var sid = session.sid
+ , data = session.data;
this.request({url: '/' + geddy.config.sessions.dbName +
- '/' + sid, method: 'PUT', data: store}, this.ensureUpdate);
+ '/' + sid, method: 'PUT', data: data}, this.ensureUpdate);
_appCallback = callback;
};
@@ -105,21 +109,21 @@ Couchdb.prototype = new function () {
req.url = params.url;
req.method = params.method || 'GET';
req.data = JSON.stringify(params.data) || null;
-
+
var headers = {host: geddy.config.sessions.hostname};
if (req.data) {
headers['content-length'] = req.data.length;
}
-
+
var client = http.createClient(geddy.config.sessions.dbPort, geddy.config.sessions.hostname);
var request = client.request(req.method, req.url, headers);
- //sys.puts(req.url);
+ //console.log(req.url);
request.addListener('response', function (response) {
- //sys.puts("STATUS: " + response.statusCode);
- //sys.puts("HEADERS: " + JSON.stringify(response.headers));
+ //console.log("STATUS: " + response.statusCode);
+ //console.log("HEADERS: " + JSON.stringify(response.headers));
response.setEncoding("utf8");
response.addListener("data", function (chunk) {
- //sys.puts("BODY: " + chunk);
+ //console.log("BODY: " + chunk);
callback({
url: req.url,
statusCode: response.statusCode,
@@ -128,14 +132,14 @@ Couchdb.prototype = new function () {
});
});
if (req.data) {
- //sys.puts('PUT data:');
- //sys.puts(req.data);
+ //console.log('PUT data:');
+ //console.log(req.data);
request.write(req.data);
}
- request.end();
+ request.end();
};
-
-
+
+
}();
View
15 lib/sessions/stores/memory.js
@@ -20,25 +20,26 @@ var Memory = function (callback) {
this.setup(callback);
};
-Memory.prototype = new function () {
+Memory.prototype = new (function () {
var _sessions = {};
-
+
this.setup = function (callback) {
callback();
};
- this.read = function (sid, callback) {
+ this.read = function (session, callback) {
+ var sid = session.sid;
if (!_sessions[sid]) {
_sessions[sid] = {};
}
callback(_sessions[sid]);
};
-
- this.write = function (sid, store, callback) {
- callback();
+
+ this.write = function (session, callback) {
+ callback();
};
-}();
+})();
exports.Memory = Memory;

0 comments on commit 33ab7a9

Please sign in to comment.