Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added session expiration.

  • Loading branch information...
commit 110f65952e01c74c70000d73143266f5e90bb5b4 1 parent fec1fee
Greg Ritter gjritter authored

Showing 3 changed files with 32 additions and 10 deletions. Show diff stats Hide diff stats

  1. +1 1  TODO.md
  2. +4 2 app.js
  3. +27 7 nerve.js
2  TODO.md
Source Rendered
... ... @@ -1,3 +1,3 @@
1 1 # To Do List
2 2
3   -Session Expiration
  3 +Replace hardcoded 30 minute session expiration with something configurable.
6 app.js
... ... @@ -1,5 +1,4 @@
1 1 var nerve = require("./nerve");
2   -var sys = require("sys");
3 2
4 3 // define an application using request matcher/handler pairs
5 4 var app = [
@@ -20,9 +19,12 @@ var app = [
20 19 // this handler will respond to any request method
21 20 [/^\/goodbye$/, function(req, res) {
22 21
  22 + var name = req.session["name"];
  23 + var message = "Goodbye, " + (name || "I hardly knew thee") + "!";
  24 +
23 25 // respond takes an object specifying content and headers,
24 26 // and uses sensible defaults if not supplied
25   - res.respond({content: "Goodbye, " + req.session["name"] + "!", headers: {"Content-Type": "text/plain"}});
  27 + res.respond({content: message, headers: {"Content-Type": "text/plain"}});
26 28
27 29 }]
28 30
34 nerve.js
@@ -56,16 +56,36 @@ del = function(regexp) {
56 56 // what is a better way of differentiating a regexp from a regular function?
57 57 return typeof matcher.test === "function";
58 58 }
  59 +
  60 + function get_or_create_session(req, res) {
  61 + var session_id = req.get_cookie("session_id");
  62 + if(!session_id) {
  63 + session_id = idgen.generate_id(22);
  64 + res.set_cookie("session_id", session_id);
  65 + }
  66 + sessions[session_id] = (sessions[session_id] || {
  67 + session: {},
  68 + touch: function() {
  69 + // TODO: replace 30 minute expiration with something configurable
  70 + this.expiration = (+ new Date) + 10*1000;
  71 + return this;
  72 + }
  73 + }).touch();
  74 + return sessions[session_id].session;
  75 + }
  76 +
  77 + function cleanup_sessions() {
  78 + for(session_id in sessions) {
  79 + if((+ new Date) > sessions[session_id].expiration) {
  80 + delete sessions[session_id];
  81 + }
  82 + }
  83 + }
59 84
60 85 function create(app) {
  86 + setInterval(cleanup_sessions, 1000);
61 87 function request_handler(req, res) {
62   - var session_id = req.get_cookie("session_id");
63   - if(!session_id) {
64   - session_id = idgen.generate_id(22);
65   - res.set_cookie("session_id", session_id);
66   - }
67   - sessions[session_id] = sessions[session_id] || {};
68   - req.session = sessions[session_id];
  88 + req.session = get_or_create_session(req, res);
69 89 for(var i = 0; i < app.length; i++) {
70 90 var matcher = app[i][0], handler = app[i][1],
71 91 match = req.uri.path.match(is_regexp(matcher) ? matcher : matcher.apply(req));

0 comments on commit 110f659

Please sign in to comment.
Something went wrong with that request. Please try again.