Skip to content
Browse files

first commit

  • Loading branch information...
0 parents commit 7ab913395593682a8357c4de57ae5e16b3ca6aa8 @gonzalo123 committed Jul 24, 2011
Showing with 183 additions and 0 deletions.
  1. +1 −0 README
  2. +16 −0 examples/serverSessions.js
  3. +7 −0 index.js
  4. +66 −0 lib/SessionHandler.js
  5. +14 −0 package.json
  6. +79 −0 tests/testNodeSessions.js
1 README
@@ -0,0 +1 @@
+PHP session handler with node.js
16 examples/serverSessions.js
@@ -0,0 +1,16 @@
+var http = require('http'),
+ url = require('url'),
+ session = require('../lib/SessionHandler.js').SessionHandler;
+
+var sessionHandler = new session();
+
+var server = http.createServer(function (req, res) {
+ var parsedUrl = url.parse(req.url, true).query;
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end(sessionHandler.run(parsedUrl));
+});
+
+server.listen(5672, "127.0.0.1", function() {
+ var address = server.address();
+ console.log("opened server on %j", address);
+});
7 index.js
@@ -0,0 +1,7 @@
+/*!
+ * socket.io-node
+ * Copyright(c) 2011 Gonzalo Ayuso <gonzalo123@gmail.com>
+ * MIT Licensed
+ */
+
+module.exports = require('./lib/SessionHandler');
66 lib/SessionHandler.js
@@ -0,0 +1,66 @@
+var SessionHandler = function() {
+ var sessions = {},
+ sessionsTimestamps = {},
+ sessionsData = {};
+
+ this.read = function(id) {
+ return sessions[id];
+ }
+
+ this.readAsArray = function(id) {
+ return sessionsData[id];
+ }
+
+ this.write = function(id, data, time, dataJSON) {
+ sessions[id] = data;
+ sessionsTimestamps[id] = time;
+ sessionsData[id] = eval("(" + dataJSON + ")");
+ }
+
+ this.destroy = function(id) {
+ delete sessions[id];
+ delete sessionsTimestamps[id];
+ delete sessionsData[id];
+ }
+
+ this.gc = function(maxlifetime, time) {
+ var sessisonKey;
+ for (sessisonKey in sessionsTimestamps) {
+ if (parseInt(sessionsTimestamps[sessisonKey]) + parseInt(maxlifetime) < parseInt(time)) {
+ this.destroy(sessisonKey);
+ }
+ }
+ }
+
+ this.run = function(parsedUrl) {
+ var out;
+ var action = parsedUrl.action;
+ var id = parsedUrl.id;
+
+ switch (action) {
+ case 'read':
+ out = this.read(id);
+ break;
+ case 'readAsArray':
+ out = this.readAsArray(id);
+ break;
+ case 'write':
+ var time = parsedUrl.time;
+ var data = parsedUrl.data;
+ var dataJSON = parsedUrl.dataJSON;
+ this.write(id, data, time, dataJSON);
+ break;
+ case 'destroy':
+ this.destroy(id);
+ break;
+ case 'gc':
+ var maxlifetime = parsedUrl.maxlifetime;
+ var time = parsedUrl.time;
+ this.gc(maxlifetime, time);
+ break;
+ }
+ return out;
+ }
+}
+
+exports.SessionHandler = SessionHandler;
14 package.json
@@ -0,0 +1,14 @@
+{
+ "name": "nodePhpSessions"
+ , "version": "0.0.1"
+ , "description": "PHP session handler with node.js"
+ , "homepage": "http://gonzalo123.wordpress.com"
+ , "keywords": ["php", "sessions", "handler", "phpSessionHandler", "comet", "ajax"]
+ , "author": "Gonzalo Ayuso <gonzalo123@gmail.com>"
+ , "repository":{
+ "type": "git"
+ , "url": "https://github.com/gonzalo123/nodePhpSessions.git"
+ }
+ , "main": "index"
+ , "engines": { "node": ">= 0.4.0" }
+}
79 tests/testNodeSessions.js
@@ -0,0 +1,79 @@
+var session = require('../lib/SessionHandler.js').SessionHandler;
+var sessionHandler = new session();
+var parsedUrl;
+
+exports["testReadUndefinedSession"] = function(test){
+ parsedUrl = { action: 'read', id: 'ts49vmf0p732iafr25mdu8gvg2' };
+ test.equal(sessionHandler.run(parsedUrl), undefined);
+ test.done();
+};
+
+exports["oneSessionShouldReturns1"] = function(test){
+ parsedUrl = {
+ action: 'write',
+ id: 'ts49vmf0p732iafr25mdu8gvg2',
+ data: 'gonzalo|i:1;arr|a:1:{s:3:"key";s:13:"4e2b1a40d136a";}',
+ time: '1311447616',
+ dataJSON: '{"gonzalo":1,"arr":{"key":"4e2b1a40d136a"}}' };
+ sessionHandler.run(parsedUrl);
+
+ parsedUrl = { action: 'readAsArray', id: 'ts49vmf0p732iafr25mdu8gvg2' };
+ test.equal(sessionHandler.run(parsedUrl).gonzalo, 1);
+ test.done();
+};
+
+exports["oneSessionShouldReturns2"] = function(test){
+ parsedUrl = {
+ action: 'write',
+ id: 'ts49vmf0p732iafr25mdu8gvg2',
+ data: 'gonzalo|i:2;arr|a:1:{s:3:"key";s:13:"4e2b1a40d136a";}',
+ time: '1311447616',
+ dataJSON: '{"gonzalo":2,"arr":{"key":"4e2b1a40d136a"}}' };
+ sessionHandler.run(parsedUrl);
+ parsedUrl = { action: 'readAsArray', id: 'ts49vmf0p732iafr25mdu8gvg2' };
+ test.equal(sessionHandler.run(parsedUrl).gonzalo, 2);
+ test.done();
+};
+
+exports["destroySession"] = function(test){
+ parsedUrl = {
+ action: 'destroy',
+ id: 'ts49vmf0p732iafr25mdu8gvg2'};
+ sessionHandler.run(parsedUrl);
+
+ parsedUrl = { action: 'readAsArray', id: 'ts49vmf0p732iafr25mdu8gvg2' };
+ test.equal(sessionHandler.run(parsedUrl), undefined);
+
+ test.done();
+};
+
+exports["garbageColector"] = function(test){
+ parsedUrl = {
+ action: 'write',
+ id: 'session1',
+ data: 'gonzalo|i:1;arr|a:1:{s:3:"key";s:13:"4e2b1a40d136a";}',
+ time: '1111111200',
+ dataJSON: '{"gonzalo":1,"arr":{"key":"4e2b1a40d136a"}}' };
+ sessionHandler.run(parsedUrl);
+
+ parsedUrl = {
+ action: 'write',
+ id: 'session2',
+ data: 'gonzalo|i:1;arr|a:1:{s:3:"key";s:13:"4e2b1a40d136a";}',
+ time: '1111111100',
+ dataJSON: '{"gonzalo":1,"arr":{"key":"4e2b1a40d136a"}}' };
+ sessionHandler.run(parsedUrl);
+
+ parsedUrl = { action: 'gc', maxlifetime: '100', time: '1111111210'};
+ sessionHandler.run(parsedUrl);
+
+ parsedUrl = { action: 'readAsArray', id: 'session2' };
+ test.equal(sessionHandler.run(parsedUrl), undefined);
+
+ parsedUrl = { action: 'readAsArray', id: 'session1' };
+ test.equal(sessionHandler.run(parsedUrl).gonzalo, 1);
+
+ test.done();
+};
+
+

0 comments on commit 7ab9133

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